/***************************************************************************
                          kdndbutton.cpp  -  description
                             -------------------
    begin                : Sun Aug 19 2001
    copyright            : (C) 2001 by Dieter Landolt
    email                : dieter.landolt@secs.ch
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kdndbutton.h"
#include <qimage.h>
#include <qmovie.h>
#include <qlabel.h>
#include <kpixmapio.h>
#include <kurl.h>
#include <qfile.h>
#include "dockwidget.h"

kDndButton::kDndButton(QWidget *parent, const char *name ) : kMovieButton(parent,name) {
  pressed=false;
}
kDndButton::~kDndButton(){
}
/** Accept the dragEvent and test it it's possible to decode */
void kDndButton::dragEnterEvent(QDragEnterEvent* event){
    event->accept(
        QTextDrag::canDecode(event) /* ||
        QImageDrag::canDecode(event) */
    );
}
/** Accept the droped Item */
void kDndButton::dropEvent(QDropEvent* event){

    // QImage image;
    // KPixmapIO io;
    QString text;
   
    /* Downt care about Images without path
    if ( QImageDrag::decode(event, image) ) {
      //insertImageAt(image, event->pos());
      setPixmap(io.convertToPixmap(image));
    } else */ 
      if ( QTextDrag::decode(event, text) ) {
      //insertTextAt(path, event->pos());
          KURL urltext(text);
          if ( QFile::exists( urltext.path() ) ) {
               path = urltext.path();
               // setPixmap(QPixmap(path));
               setMovie(path);
               newPath(path);
          } else {
              QString mname;
              mname = name();
              path = QString::null;
              newPath(QString::null);
              if( mname.right(2) == "on" ) {
                    setPixmap(DockWidget::paintIcon(DockWidget::On));
              } else {
                    setPixmap(DockWidget::paintIcon(DockWidget::Off));    
              }
          }
     }
}
/** Drag The Pathtext to an other Button */
void kDndButton::startDragging(){
    QDragObject *d = new QTextDrag( path, this );
    if (! QFile::exists(path) ) {
          d->setPixmap((QPixmap)* pixmap());
    } else {
          d->setPixmap(QPixmap(path));
    }
    d->dragCopy();
    // do NOT delete d.
}
/** When the Mouse moves away */
void kDndButton::mouseMoveEvent(QMouseEvent* e){
   if (pressed) {
          pressed=false;
          QPushButton::mouseReleaseEvent(e);
          startDragging();
     } else {
          kMovieButton::mouseMoveEvent(e);
     }
}
/** No descriptions */
void kDndButton::mousePressEvent(QMouseEvent* e) {
     QPushButton::mousePressEvent(e);
     if(e->button() == RightButton) {
          pressed=false;
     } else { if (e->button() == LeftButton) {
            pressed=true;
            // QPushButton::mousePressEvent(e);
         } else {
            pressed=false;
            // QPushButton::mousePressEvent(e);
         }
     }
     
}
/** Starts if the Mouse is released */
void kDndButton::mouseReleaseEvent(QMouseEvent * e){
  pressed=false;
  QPushButton::mouseReleaseEvent(e);
}
