Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qstyleanimation.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qstyleanimation_p.h"
5
6#include <qcoreapplication.h>
7#include <qwidget.h>
8#include <qevent.h>
9
11
12static const qreal ScrollBarFadeOutDuration = 200.0;
13static const qreal ScrollBarFadeOutDelay = 450.0;
14
16 _delay(0), _duration(-1), _startTime(QTime::currentTime()), _fps(ThirtyFps), _skip(0)
17{
18}
19
21{
22}
23
25{
26 return parent();
27}
28
30{
31 return _duration;
32}
33
35{
36 _duration = duration;
37}
38
40{
41 return _delay;
42}
43
45{
46 _delay = delay;
47}
48
50{
51 return _startTime;
52}
53
55{
56 _startTime = time;
57}
58
60{
61 return _fps;
62}
63
65{
66 _fps = fps;
67}
68
70{
72 event.setAccepted(false);
74 if (!event.isAccepted())
75 stop();
76}
77
79{
80 _skip = 0;
82}
83
85{
86 return currentTime() > _delay;
87}
88
90{
91 if (++_skip >= _fps || time >= duration()) {
92 _skip = 0;
93 if (target() && isUpdateNeeded())
95 }
96}
97
99 QStyleAnimation(target), _speed(speed), _step(-1)
100{
101}
102
104{
105 return currentTime() / (1000.0 / _speed);
106}
107
109{
110 int step = animationStep();
111 int progress = (step * width / _speed) % width;
112 if (((step * width / _speed) % (2 * width)) >= width)
113 progress = width - progress;
114 return progress;
115}
116
118{
119 return _speed;
120}
121
123{
124 _speed = speed;
125}
126
128{
130 int current = animationStep();
131 if (_step == -1 || _step != current)
132 {
133 _step = current;
134 return true;
135 }
136 }
137 return false;
138}
139
141 QStyleAnimation(target), _start(0.0), _end(1.0), _prev(0.0)
142{
143 setDuration(250);
144}
145
147{
148 return _start;
149}
150
152{
153 _start = value;
154}
155
157{
158 return _end;
159}
160
162{
163 _end = value;
164}
165
167{
168 qreal step = qreal(currentTime() - delay()) / (duration() - delay());
169 return _start + qMax(qreal(0), step) * (_end - _start);
170}
171
173{
175 qreal current = currentValue();
176 if (!qFuzzyCompare(_prev, current))
177 {
178 _prev = current;
179 return true;
180 }
181 }
182 return false;
183}
184
187{
188 setDuration(250);
189}
190
192{
193 return _start;
194}
195
197{
198 _start = image;
199}
200
202{
203 return _end;
204}
205
207{
208 _end = image;
209}
210
212{
213 return _current;
214}
215
223static QImage blendedImage(const QImage &start, const QImage &end, float alpha)
224{
225 if (start.isNull() || end.isNull())
226 return QImage();
227
228 QImage blended;
229 const int a = qRound(alpha*256);
230 const int ia = 256 - a;
231 const int sw = start.width();
232 const int sh = start.height();
233 const qsizetype bpl = start.bytesPerLine();
234 switch (start.depth()) {
235 case 32:
236 {
237 blended = QImage(sw, sh, start.format());
238 blended.setDevicePixelRatio(start.devicePixelRatio());
239 uchar *mixed_data = blended.bits();
240 const uchar *back_data = start.bits();
241 const uchar *front_data = end.bits();
242 for (int sy = 0; sy < sh; sy++) {
243 quint32* mixed = (quint32*)mixed_data;
244 const quint32* back = (const quint32*)back_data;
245 const quint32* front = (const quint32*)front_data;
246 for (int sx = 0; sx < sw; sx++) {
247 quint32 bp = back[sx];
248 quint32 fp = front[sx];
249 mixed[sx] = qRgba ((qRed(bp)*ia + qRed(fp)*a)>>8,
250 (qGreen(bp)*ia + qGreen(fp)*a)>>8,
251 (qBlue(bp)*ia + qBlue(fp)*a)>>8,
252 (qAlpha(bp)*ia + qAlpha(fp)*a)>>8);
253 }
254 mixed_data += bpl;
255 back_data += bpl;
256 front_data += bpl;
257 }
258 }
259 default:
260 break;
261 }
262 return blended;
263}
264
266{
268
269 float alpha = 1.0;
270 if (duration() > 0) {
271 if (_type == Pulse) {
272 time = time % duration() * 2;
273 if (time > duration())
274 time = duration() * 2 - time;
275 }
276
277 alpha = time / static_cast<float>(duration());
278
279 if (_type == Transition && time > duration()) {
280 alpha = 1.0;
281 stop();
282 }
283 } else if (time > 0) {
284 stop();
285 }
286
287 _current = blendedImage(_start, _end, alpha);
288}
289
291{
292 switch (mode) {
293 case Activating:
295 setStartValue(0.0);
296 setEndValue(1.0);
297 break;
298 case Deactivating:
301 setStartValue(1.0);
302 setEndValue(0.0);
303 break;
304 }
305}
306
308{
309 return _mode;
310}
311
313{
314 return _active;
315}
316
318{
319 _active = active;
320}
321
323{
325 if (_mode == Deactivating && qFuzzyIsNull(currentValue()))
326 target()->setProperty("visible", false);
327}
328
330
331#include "moc_qstyleanimation_p.cpp"
void stop()
Stops the animation.
virtual void updateCurrentTime(int currentTime)=0
This pure virtual function is called every time the animation's currentTime changes.
void start(QAbstractAnimation::DeletionPolicy policy=KeepWhenStopped)
Starts the animation.
int currentTime
the current time and progress of the animation
QImage currentImage() const
virtual void updateCurrentTime(int time) override
This pure virtual function is called every time the animation's currentTime changes.
void setEndImage(const QImage &image)
QBlendStyleAnimation(Type type, QObject *target)
void setStartImage(const QImage &image)
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
\inmodule QtCore
Definition qcoreevent.h:45
@ StyleAnimationUpdate
Definition qcoreevent.h:272
\inmodule QtGui
Definition qimage.h:37
uchar * bits()
Returns a pointer to the first pixel data.
Definition qimage.cpp:1677
void setDevicePixelRatio(qreal scaleFactor)
Sets the device pixel ratio for the image.
Definition qimage.cpp:1488
void setStartValue(qreal value)
void setEndValue(qreal value)
QNumberStyleAnimation(QObject *target)
bool isUpdateNeeded() const override
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object's name property to value.
QProgressStyleAnimation(int speed, QObject *target)
int progressStep(int width) const
bool isUpdateNeeded() const override
void updateCurrentTime(int time) override
QScrollbarStyleAnimation(Mode mode, QObject *target)
QTime startTime() const
virtual void updateCurrentTime(int time) override
This pure virtual function is called every time the animation's currentTime changes.
QStyleAnimation(QObject *target)
void setDelay(int delay)
virtual ~QStyleAnimation()
virtual bool isUpdateNeeded() const
QObject * target() const
void setFrameRate(FrameRate fps)
int duration() const override
This pure virtual function returns the duration of the animation, and defines for how long QAbstractA...
void setDuration(int duration)
FrameRate frameRate() const
void setStartTime(QTime time)
\inmodule QtCore \reentrant
Definition qdatetime.h:189
Combined button and popup list for selecting options.
Definition image.cpp:4
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:287
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:303
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:281
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLenum mode
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLuint end
GLint GLsizei width
GLenum type
GLenum target
GLuint start
struct _cl_event * event
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
constexpr int qRed(QRgb rgb)
Definition qrgb.h:18
constexpr int qGreen(QRgb rgb)
Definition qrgb.h:21
constexpr QRgb qRgba(int r, int g, int b, int a)
Definition qrgb.h:33
constexpr int qBlue(QRgb rgb)
Definition qrgb.h:24
constexpr int qAlpha(QRgb rgb)
Definition qrgb.h:27
static const qreal ScrollBarFadeOutDelay
static QImage blendedImage(const QImage &start, const QImage &end, float alpha)
static QT_BEGIN_NAMESPACE const qreal ScrollBarFadeOutDuration
#define fp
unsigned int quint32
Definition qtypes.h:45
unsigned char uchar
Definition qtypes.h:27
ptrdiff_t qsizetype
Definition qtypes.h:70
double qreal
Definition qtypes.h:92
static double currentTime()
Definition moc.h:24