/***************************************************************************
                          kmoviebutton.cpp  -  description
                             -------------------
    begin                : Mon Mai 6 2002
    copyright            : (C) 2002 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 "kmoviebutton.h"

kMovieButton::kMovieButton(QWidget *parent, const char *name ) : QPushButton(parent,name) {
  movie = new QMovie(""); // Initialize first use of movie object
  movie->connectUpdate(this, SLOT(slotMovieUpdate()));
  setMouseTracking(true);
  show();
}
kMovieButton::~kMovieButton(){
}
/* QSize kMovieButton::sizeHint() const
{
  // return(QPushButton::sizeHint());
  // qWarning("In sizeHint()");
  // qWarning("sizeHint: %d, %d", movie->framePixmap().size().width(), movie->framePixmap().size().height());
  // return(movie->framePixmap().size());
  return(QPushButton::sizeHint());
}  */

void kMovieButton::closeEvent(QCloseEvent *)
{
  qWarning("In ani close event");
  delete movie;
  delete this;
}

void kMovieButton::paintEvent(QPaintEvent *ev)
{
  QPushButton::paintEvent(ev);
  // QRect r = ev->rect();
  // Calculate position of moviepixmap
  QPoint lefttop = QPoint( ( width() - movie->framePixmap().width() ) / 2,(height() - movie->framePixmap().height()) / 2 );
  // Workaround a bug from QT with MNG infiite loops
  movie->connectStatus(this, SLOT(slotStatus(int)));
  // End Workaround
  // Not optimized but working
  bitBlt(this, lefttop, &movie->framePixmap());
}

void kMovieButton::slotMovieUpdate()
{
  repaint(false);
}

void kMovieButton::slotPlay()
{
  movie->unpause(); // QT bug Workaround to be able to restart
  movie->restart();
  qDebug("Slot Play reached\n");
}

void kMovieButton::slotPause()
{
  movie->pause();
}

void kMovieButton::slotStop()
{
  movie->pause();
}

void kMovieButton::slotRestart()
{
  movie->restart();
}
/** set a new Movie to the button */
void kMovieButton::setMovie(const QMovie &mov){
  setPixmap(0);
  delete movie;
  movie = new QMovie(mov);
  movie->connectUpdate(this, SLOT(slotMovieUpdate()));
}
void kMovieButton::setMovie(const QString &fileName){
  setPixmap(0);
  delete movie;
  movie = new QMovie(fileName);
  if(movie->isNull()) {
    // try to set the buttobpixmap with the file
    setPixmap(QPixmap(fileName));
  }
  movie->connectUpdate(this, SLOT(slotMovieUpdate()));
}
/** Restart the Movie if button gets the focus */
void kMovieButton::focusInEvent(QFocusEvent* foc){
  QPushButton::focusInEvent(foc);
  movie->unpause(); // QT bug Workaround to be able to restart
  movie->restart();
}
/** When the Mouse moves away */
void kMovieButton::mouseMoveEvent(QMouseEvent * e){
  QPushButton::mouseMoveEvent(e);
  movie->unpause(); // QT bug Workaround to be able to restart
  movie->restart();
}
