Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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
63
65{
66 _fps = fps;
67}
68
70{
72 event.setAccepted(false);
74 if (!event.isAccepted())
75 stop();
76}
77
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
155
157{
158 return _end;
159}
160
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
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 break;
260 default:
261 break;
262 }
263 return blended;
264}
265
267{
269
270 float alpha = 1.0;
271 if (duration() > 0) {
272 if (_type == Pulse) {
273 time = time % duration() * 2;
274 if (time > duration())
275 time = duration() * 2 - time;
276 }
277
278 alpha = time / static_cast<float>(duration());
279
280 if (_type == Transition && time > duration()) {
281 alpha = 1.0;
282 stop();
283 }
284 } else if (time > 0) {
285 stop();
286 }
287
288 _current = blendedImage(_start, _end, alpha);
289}
290
307
312
314{
315 return _active;
316}
317
319{
320 _active = active;
321}
322
329
331
332#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
void setStartValue(qreal value)
void setEndValue(qreal value)
QNumberStyleAnimation(QObject *target)
bool isUpdateNeeded() const override
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
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:215
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:333
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:349
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
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:50
unsigned char uchar
Definition qtypes.h:32
ptrdiff_t qsizetype
Definition qtypes.h:165
double qreal
Definition qtypes.h:187
static double currentTime()
Definition moc.h:23