1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "styleanimator.h"
36 #include <QtGui/QStyleOption>
38 Animation * StyleAnimator::widgetAnimation(const QWidget *widget) const
42 foreach (Animation *a, animations) {
43 if (a->widget() == widget)
49 void Animation::paint(QPainter *painter, const QStyleOption *option)
55 void Animation::drawBlendedImage(QPainter *painter, QRect rect, float alpha)
57 if (m_secondaryImage.isNull() || m_primaryImage.isNull())
60 if (m_tempImage.isNull())
61 m_tempImage = m_secondaryImage;
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()) {
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);
94 painter->drawImage(rect, m_tempImage);
97 void Transition::paint(QPainter *painter, const QStyleOption *option)
100 if (m_duration > 0) {
101 QTime current = QTime::currentTime();
103 if (m_startTime > current)
104 m_startTime = current;
106 int timeDiff = m_startTime.msecsTo(current);
107 alpha = timeDiff/(float)m_duration;
108 if (timeDiff > m_duration) {
116 drawBlendedImage(painter, option->rect, alpha);
119 void StyleAnimator::timerEvent(QTimerEvent *)
121 for (int i = animations.size() - 1 ; i >= 0 ; --i) {
122 if (animations[i]->widget())
123 animations[i]->widget()->update();
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())
131 Animation *a = animations.takeAt(i);
135 if (animations.size() == 0 && animationTimer.isActive()) {
136 animationTimer.stop();
140 void StyleAnimator::stopAnimation(const QWidget *w)
142 for (int i = animations.size() - 1 ; i >= 0 ; --i) {
143 if (animations[i]->widget() == w) {
144 Animation *a = animations.takeAt(i);
151 void StyleAnimator::startAnimation(Animation *t)
153 stopAnimation(t->widget());
154 animations.append(t);
155 if (animations.size() > 0 && !animationTimer.isActive()) {
156 animationTimer.start(35, this);