1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact:  Qt Software Information (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at qt-sales@nokia.com.
27 **
28 **************************************************************************/
29 
30 #ifndef Header_Animation
31 #define Header_Animation
32 
33 #include <QtCore/QPointer>
34 #include <QtCore/QTime>
35 #include <QtCore/QBasicTimer>
36 #include <QStyle>
37 #include <QPainter>
38 #include <QWidget>
39 
40 /*
41  * This is a set of helper classes to allow for widget animations in
42  * the style. Its mostly taken from Vista style so it should be fully documented
43  * there.
44  *
45  */
46 
47 class Animation
48 {
49 public :
Animation()50     Animation() : m_running(true) { }
~Animation()51     virtual ~Animation() { }
widget()52     QWidget * widget() const { return m_widget; }
running()53     bool running() const { return m_running; }
startTime()54     const QTime &startTime() const { return m_startTime; }
setRunning(bool val)55     void setRunning(bool val) { m_running = val; }
setWidget(QWidget * widget)56     void setWidget(QWidget *widget) { m_widget = widget; }
setStartTime(const QTime & startTime)57     void setStartTime(const QTime &startTime) { m_startTime = startTime; }
58     virtual void paint(QPainter *painter, const QStyleOption *option);
59 
60 protected:
61     void drawBlendedImage(QPainter *painter, QRect rect, float value);
62     QTime m_startTime;
63     QPointer<QWidget> m_widget;
64     QImage m_primaryImage;
65     QImage m_secondaryImage;
66     QImage m_tempImage;
67     bool m_running;
68 };
69 
70 // Handles state transition animations
71 class Transition : public Animation
72 {
73 public :
Transition()74     Transition() : Animation(),m_duration(130) {}
~Transition()75     virtual ~Transition() {}
setDuration(int duration)76     void setDuration(int duration) { m_duration = duration; }
setStartImage(const QImage & image)77     void setStartImage(const QImage &image) { m_primaryImage = image; }
setEndImage(const QImage & image)78     void setEndImage(const QImage &image) { m_secondaryImage = image; }
79     virtual void paint(QPainter *painter, const QStyleOption *option);
duration()80     int duration() const { return m_duration; }
81     int m_duration; //set time in ms to complete a state transition
82 };
83 
84 class StyleAnimator : public QObject
85 {
86     Q_OBJECT
87 
88 public:
QObject(parent)89     StyleAnimator(QObject *parent = nullptr) : QObject(parent) {}
90 
91     void timerEvent(QTimerEvent *);
92     void startAnimation(Animation *);
93     void stopAnimation(const QWidget *);
94     Animation* widgetAnimation(const QWidget *) const;
95 
96 private:
97     QBasicTimer animationTimer;
98     QList <Animation*> animations;
99 };
100 #endif // ANIMATION_H
101