dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
styleanimator.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #include "styleanimator.h"
29 
30 #include <QStyleOption>
31 
32 Animation *StyleAnimator::widgetAnimation(const QWidget *widget) const
33 {
34  if (!widget)
35  return nullptr;
36  foreach (Animation *a, animations) {
37  if (a->widget() == widget)
38  return a;
39  }
40  return nullptr;
41 }
42 
43 void Animation::paint(QPainter *painter, const QStyleOption *option)
44 {
45  Q_UNUSED(option)
46  Q_UNUSED(painter)
47 }
48 
49 void Animation::drawBlendedImage(QPainter *painter, QRect rect, float alpha)
50 {
51  if (m_secondaryImage.isNull() || m_primaryImage.isNull())
52  return;
53 
54  if (m_tempImage.isNull())
56 
57  const int a = qRound(alpha * 256);
58  const int ia = 256 - a;
59  const int sw = m_primaryImage.width();
60  const int sh = m_primaryImage.height();
61  const int bpl = m_primaryImage.bytesPerLine();
62  switch (m_primaryImage.depth()) {
63  case 32: {
64  uchar *mixed_data = m_tempImage.bits();
65  const uchar *back_data = m_primaryImage.bits();
66  const uchar *front_data = m_secondaryImage.bits();
67  for (int sy = 0; sy < sh; sy++) {
68  quint32 *mixed = reinterpret_cast<quint32 *>(mixed_data);
69  const quint32 *back = reinterpret_cast<const quint32 *>(back_data);
70  const quint32 *front = reinterpret_cast<const quint32 *>(front_data);
71  for (int sx = 0; sx < sw; sx++) {
72  quint32 bp = back[sx];
73  quint32 fp = front[sx];
74  mixed[sx] = qRgba(
75  (qRed(bp) * ia + qRed(fp) * a) >> 8, (qGreen(bp) * ia + qGreen(fp) * a) >> 8,
76  (qBlue(bp) * ia + qBlue(fp) * a) >> 8, (qAlpha(bp) * ia + qAlpha(fp) * a) >> 8);
77  }
78  mixed_data += bpl;
79  back_data += bpl;
80  front_data += bpl;
81  }
82  }
83  default:
84  break;
85  }
86  painter->drawImage(rect, m_tempImage);
87 }
88 
89 void Transition::paint(QPainter *painter, const QStyleOption *option)
90 {
91  float alpha = 1.0;
92  if (m_duration > 0) {
93  QTime current = QTime::currentTime();
94 
95  if (m_startTime > current)
96  m_startTime = current;
97 
98  int timeDiff = m_startTime.msecsTo(current);
99  alpha = timeDiff / (float)m_duration;
100  if (timeDiff > m_duration) {
101  m_running = false;
102  alpha = 1.0;
103  }
104  } else {
105  m_running = false;
106  }
107  drawBlendedImage(painter, option->rect, alpha);
108 }
109 
110 void StyleAnimator::timerEvent(QTimerEvent *)
111 {
112  for (int i = animations.size() - 1; i >= 0; --i) {
113  if (animations[i]->widget())
114  animations[i]->widget()->update();
115 
116  if (!animations[i]->widget() || !animations[i]->widget()->isEnabled()
117  || !animations[i]->widget()->isVisible()
118  || animations[i]->widget()->window()->isMinimized() || !animations[i]->running()) {
119  Animation *a = animations.takeAt(i);
120  delete a;
121  }
122  }
123  if (animations.size() == 0 && animationTimer.isActive()) {
124  animationTimer.stop();
125  }
126 }
127 
128 void StyleAnimator::stopAnimation(const QWidget *w)
129 {
130  for (int i = animations.size() - 1; i >= 0; --i) {
131  if (animations[i]->widget() == w) {
132  Animation *a = animations.takeAt(i);
133  delete a;
134  break;
135  }
136  }
137 }
138 
140 {
141  stopAnimation(t->widget());
142  animations.append(t);
143  if (animations.size() > 0 && !animationTimer.isActive()) {
144  animationTimer.start(35, this);
145  }
146 }
const char t[]
Definition: coreconstants.h:40
QImage m_primaryImage
Definition: styleanimator.h:65
QImage m_secondaryImage
Definition: styleanimator.h:66
for i
Definition: OPPlots.m:140
bool m_running
Definition: styleanimator.h:68
void drawBlendedImage(QPainter *painter, QRect rect, float value)
end a
Definition: OPPlots.m:98
virtual void paint(QPainter *painter, const QStyleOption *option)
Animation * widgetAnimation(const QWidget *) const
virtual void paint(QPainter *painter, const QStyleOption *option)
QImage m_tempImage
Definition: styleanimator.h:67
void timerEvent(QTimerEvent *)
QWidget * widget() const
Definition: styleanimator.h:53
void stopAnimation(const QWidget *)
QTime m_startTime
Definition: styleanimator.h:63
void startAnimation(Animation *)