#include "animatedpixmap.h"
#include <QPropertyAnimation>

// Note that since this object inherits from both QObject and QGraphicsItem,
// there's two different object hierarchies in use. There's no need to use
// the QObject hierarchy, but graphics item hierarchy is needed so the
// coordinate systems work correctly
AnimatedPixmap::AnimatedPixmap(const QPixmap &pixmap, QGraphicsItem *parent) :
    QObject(), QGraphicsPixmapItem(pixmap, parent)
{
    // Rotate around center of pixmap
    setTransformOriginPoint(pixmap.width() / 2, pixmap.height() / 2);

    // Set up property animation, which animates over the "rotation" property
    animation = new QPropertyAnimation(this, "rotation", this);
    animation->setLoopCount(-1);
    animation->setEndValue(360);
    animation->setStartValue(0);
    animation->setDuration(10000);

    // Add some fun stuff
    animation->setEasingCurve(QEasingCurve::OutElastic);
}

void AnimatedPixmap::start()
{
    animation->start();
}

void AnimatedPixmap::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    // This handler gets called whenever user double-clicks
    // the mouse on the animated pixmap component
    if (animation->state() == QAbstractAnimation::Paused) {
        animation->resume();
    } else {
        animation->pause();
    }
}
