OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / styleanimator.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
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 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "styleanimator.h"
35
36 #include <QtGui/QStyleOption>
37
38 Animation * StyleAnimator::widgetAnimation(const QWidget *widget) const
39 {
40     if (!widget)
41         return 0;
42     foreach (Animation *a, animations) {
43         if (a->widget() == widget)
44             return a;
45     }
46     return 0;
47 }
48
49 void Animation::paint(QPainter *painter, const QStyleOption *option)
50 {
51     Q_UNUSED(option)
52     Q_UNUSED(painter)
53 }
54
55 void Animation::drawBlendedImage(QPainter *painter, QRect rect, float alpha)
56 {
57     if (m_secondaryImage.isNull() || m_primaryImage.isNull())
58         return;
59
60     if (m_tempImage.isNull())
61         m_tempImage = m_secondaryImage;
62
63     const int a = qRound(alpha*256);
64     const int ia = 256 - a;
65     const int sw = m_primaryImage.width();
66     const int sh = m_primaryImage.height();
67     const int bpl = m_primaryImage.bytesPerLine();
68     switch (m_primaryImage.depth()) {
69     case 32:
70         {
71             uchar *mixed_data = m_tempImage.bits();
72             const uchar *back_data = m_primaryImage.bits();
73             const uchar *front_data = m_secondaryImage.bits();
74             for (int sy = 0; sy < sh; sy++) {
75                 quint32 *mixed = (quint32*)mixed_data;
76                 const quint32* back = (const quint32*)back_data;
77                 const quint32* front = (const quint32*)front_data;
78                 for (int sx = 0; sx < sw; sx++) {
79                     quint32 bp = back[sx];
80                     quint32 fp = front[sx];
81                     mixed[sx] =  qRgba ((qRed(bp)*ia + qRed(fp)*a)>>8,
82                                         (qGreen(bp)*ia + qGreen(fp)*a)>>8,
83                                         (qBlue(bp)*ia + qBlue(fp)*a)>>8,
84                                         (qAlpha(bp)*ia + qAlpha(fp)*a)>>8);
85                 }
86                 mixed_data += bpl;
87                 back_data += bpl;
88                 front_data += bpl;
89             }
90         }
91     default:
92         break;
93     }
94     painter->drawImage(rect, m_tempImage);
95 }
96
97 void Transition::paint(QPainter *painter, const QStyleOption *option)
98 {
99     float alpha = 1.0;
100     if (m_duration > 0) {
101         QTime current = QTime::currentTime();
102
103         if (m_startTime > current)
104             m_startTime = current;
105
106         int timeDiff = m_startTime.msecsTo(current);
107         alpha = timeDiff/(float)m_duration;
108         if (timeDiff > m_duration) {
109             m_running = false;
110             alpha = 1.0;
111         }
112     }
113     else {
114         m_running = false;
115     }
116     drawBlendedImage(painter, option->rect, alpha);
117 }
118
119 void StyleAnimator::timerEvent(QTimerEvent *)
120 {
121     for (int i = animations.size() - 1 ; i >= 0 ; --i) {
122         if (animations[i]->widget())
123             animations[i]->widget()->update();
124
125         if (!animations[i]->widget() ||
126             !animations[i]->widget()->isEnabled() ||
127             !animations[i]->widget()->isVisible() ||
128             animations[i]->widget()->window()->isMinimized() ||
129             !animations[i]->running())
130         {
131             Animation *a = animations.takeAt(i);
132             delete a;
133         }
134     }
135     if (animations.size() == 0 && animationTimer.isActive()) {
136         animationTimer.stop();
137     }
138 }
139
140 void StyleAnimator::stopAnimation(const QWidget *w)
141 {
142     for (int i = animations.size() - 1 ; i >= 0 ; --i) {
143         if (animations[i]->widget() == w) {
144             Animation *a = animations.takeAt(i);
145             delete a;
146             break;
147         }
148     }
149 }
150
151 void StyleAnimator::startAnimation(Animation *t)
152 {
153     stopAnimation(t->widget());
154     animations.append(t);
155     if (animations.size() > 0 && !animationTimer.isActive()) {
156         animationTimer.start(35, this);
157     }
158 }