OpenCoverage

qquickspringanimation.cpp

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/quick/util/qquickspringanimation.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtQuick module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** 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 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qquickspringanimation_p.h"-
41-
42#include "qquickanimation_p_p.h"-
43#include <private/qqmlproperty_p.h>-
44#include "private/qcontinuinganimationgroupjob_p.h"-
45-
46#include <QtCore/qdebug.h>-
47-
48#include <private/qobject_p.h>-
49-
50#include <cmath>-
51-
52#define DELAY_STOP_TIMER_INTERVAL 32-
53-
54QT_BEGIN_NAMESPACE-
55-
56class QQuickSpringAnimationPrivate;-
57class Q_AUTOTEST_EXPORT QSpringAnimation : public QAbstractAnimationJob-
58{-
59 Q_DISABLE_COPY(QSpringAnimation)-
60public:-
61 QSpringAnimation(QQuickSpringAnimationPrivate * = nullptr);-
62-
63 ~QSpringAnimation();-
64 int duration() const override;-
65 void restart();-
66 void init();-
67-
68 qreal currentValue;-
69 qreal to;-
70 qreal velocity;-
71 int startTime;-
72 int dura;-
73 int lastTime;-
74 int stopTime;-
75 enum Mode {-
76 Track,-
77 Velocity,-
78 Spring-
79 };-
80 Mode mode;-
81 QQmlProperty target;-
82-
83 qreal velocityms;-
84 qreal maxVelocity;-
85 qreal mass;-
86 qreal spring;-
87 qreal damping;-
88 qreal epsilon;-
89 qreal modulus;-
90-
91 bool useMass : 1;-
92 bool haveModulus : 1;-
93 bool skipUpdate : 1;-
94 typedef QHash<QQmlProperty, QSpringAnimation*> ActiveAnimationHash;-
95 typedef ActiveAnimationHash::Iterator ActiveAnimationHashIt;-
96-
97 void clearTemplate() { animationTemplate = nullptr; }
executed 4 times by 1 test: end of block
Executed by:
  • tst_qquickspringanimation
4
98-
99protected:-
100 void updateCurrentTime(int time) override;-
101 void updateState(QAbstractAnimationJob::State, QAbstractAnimationJob::State) override;-
102 void debugAnimation(QDebug d) const override;-
103-
104private:-
105 QQuickSpringAnimationPrivate *animationTemplate;-
106};-
107-
108class QQuickSpringAnimationPrivate : public QQuickPropertyAnimationPrivate-
109{-
110 Q_DECLARE_PUBLIC(QQuickSpringAnimation)-
111public:-
112 QQuickSpringAnimationPrivate()-
113 : QQuickPropertyAnimationPrivate()-
114 , velocityms(0)-
115 , maxVelocity(0)-
116 , mass(1.0)-
117 , spring(0.)-
118 , damping(0.)-
119 , epsilon(0.01)-
120 , modulus(0.0)-
121 , useMass(false)-
122 , haveModulus(false)-
123 , mode(QSpringAnimation::Track)-
124 { elapsed.start(); }
executed 56 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
56
125-
126 void updateMode();-
127 qreal velocityms;-
128 qreal maxVelocity;-
129 qreal mass;-
130 qreal spring;-
131 qreal damping;-
132 qreal epsilon;-
133 qreal modulus;-
134-
135 bool useMass : 1;-
136 bool haveModulus : 1;-
137 QSpringAnimation::Mode mode;-
138-
139 QSpringAnimation::ActiveAnimationHash activeAnimations;-
140 QElapsedTimer elapsed;-
141};-
142-
143QSpringAnimation::QSpringAnimation(QQuickSpringAnimationPrivate *priv)-
144 : QAbstractAnimationJob()-
145 , currentValue(0)-
146 , to(0)-
147 , velocity(0)-
148 , startTime(0)-
149 , dura(0)-
150 , lastTime(0)-
151 , stopTime(-1)-
152 , mode(Track)-
153 , velocityms(0)-
154 , maxVelocity(0)-
155 , mass(1.0)-
156 , spring(0.)-
157 , damping(0.)-
158 , epsilon(0.01)-
159 , modulus(0.0)-
160 , useMass(false)-
161 , haveModulus(false)-
162 , skipUpdate(false)-
163 , animationTemplate(priv)-
164{-
165}
executed 26 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
26
166-
167QSpringAnimation::~QSpringAnimation()-
168{-
169 if (animationTemplate) {
animationTemplateDescription
TRUEevaluated 20 times by 3 tests
Evaluated by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
4-20
170 if (target.object()) {
target.object()Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 18 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickbehaviors
2-18
171 ActiveAnimationHashIt it = animationTemplate->activeAnimations.find(target);-
172 if (it != animationTemplate->activeAnimations.end() && it.value() == this)
it != animatio...imations.end()Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEnever evaluated
it.value() == thisDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEnever evaluated
0-2
173 animationTemplate->activeAnimations.erase(it);
executed 2 times by 1 test: animationTemplate->activeAnimations.erase(it);
Executed by:
  • tst_qquickspringanimation
2
174 } else {
executed 2 times by 1 test: end of block
Executed by:
  • tst_qquickspringanimation
2
175 //target is no longer valid, need to search linearly-
176 for (ActiveAnimationHashIt it = animationTemplate->activeAnimations.begin(); it != animationTemplate->activeAnimations.end(); ++it) {
it != animatio...imations.end()Description
TRUEevaluated 18 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickbehaviors
FALSEnever evaluated
0-18
177 if (it.value() == this) {
it.value() == thisDescription
TRUEevaluated 18 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickbehaviors
FALSEnever evaluated
0-18
178 animationTemplate->activeAnimations.erase(it);-
179 break;
executed 18 times by 2 tests: break;
Executed by:
  • tst_examples
  • tst_qquickbehaviors
18
180 }-
181 }
never executed: end of block
0
182 }
executed 18 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickbehaviors
18
183 }-
184}
executed 24 times by 3 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
24
185-
186int QSpringAnimation::duration() const-
187{-
188 return -1;
executed 606 times by 4 tests: return -1;
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
606
189}-
190-
191void QSpringAnimation::restart()-
192{-
193 if (isRunning() || (stopTime != -1 && (animationTemplate->elapsed.elapsed() - stopTime) < DELAY_STOP_TIMER_INTERVAL)) {
isRunning()Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickbehaviors
FALSEnever evaluated
stopTime != -1Description
TRUEnever evaluated
FALSEnever evaluated
(animationTemp...stopTime) < 32Description
TRUEnever evaluated
FALSEnever evaluated
0-2
194 skipUpdate = true;-
195 init();-
196 } else {
executed 2 times by 1 test: end of block
Executed by:
  • tst_qquickbehaviors
2
197 skipUpdate = false;-
198 //init() will be triggered when group starts-
199 }
never executed: end of block
0
200}-
201-
202void QSpringAnimation::init()-
203{-
204 lastTime = startTime = 0;-
205 stopTime = -1;-
206}
executed 28 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
28
207-
208void QSpringAnimation::updateCurrentTime(int time)-
209{-
210 if (skipUpdate) {
skipUpdateDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickbehaviors
FALSEevaluated 574 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
2-574
211 skipUpdate = false;-
212 return;
executed 2 times by 1 test: return;
Executed by:
  • tst_qquickbehaviors
2
213 }-
214-
215 if (mode == Track) {
mode == TrackDescription
TRUEnever evaluated
FALSEevaluated 574 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
0-574
216 stop();-
217 return;
never executed: return;
0
218 }-
219-
220 int elapsed = time - lastTime;-
221-
222 if (!elapsed)
!elapsedDescription
TRUEevaluated 26 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 548 times by 3 tests
Evaluated by:
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
26-548
223 return;
executed 26 times by 4 tests: return;
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
26
224-
225 int count = elapsed / 16;-
226-
227 if (mode == Spring) {
mode == SpringDescription
TRUEevaluated 546 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickanimations
2-546
228 if (elapsed < 16) // capped at 62fps.
elapsed < 16Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • tst_qquickbehaviors
FALSEevaluated 541 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
5-541
229 return;
executed 5 times by 1 test: return;
Executed by:
  • tst_qquickbehaviors
5
230 lastTime = time - (elapsed - count * 16);-
231 } else {
executed 541 times by 2 tests: end of block
Executed by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
541
232 lastTime = time;-
233 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_qquickanimations
2
234-
235 qreal srcVal = to;-
236-
237 bool stopped = false;-
238-
239 if (haveModulus) {
haveModulusDescription
TRUEevaluated 218 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 325 times by 3 tests
Evaluated by:
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
218-325
240 currentValue = fmod(currentValue, modulus);-
241 srcVal = fmod(srcVal, modulus);-
242 }
executed 218 times by 1 test: end of block
Executed by:
  • tst_qquickspringanimation
218
243 if (mode == Spring) {
mode == SpringDescription
TRUEevaluated 541 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickanimations
2-541
244 // Real men solve the spring DEs using RK4.-
245 // We'll do something much simpler which gives a result that looks fine.-
246 for (int i = 0; i < count; ++i) {
i < countDescription
TRUEevaluated 546 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 541 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
541-546
247 qreal diff = srcVal - currentValue;-
248 if (haveModulus && qAbs(diff) > modulus / 2) {
haveModulusDescription
TRUEevaluated 218 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 328 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
qAbs(diff) > modulus / 2Description
TRUEnever evaluated
FALSEevaluated 218 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
0-328
249 if (diff < 0)
diff < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
250 diff += modulus;
never executed: diff += modulus;
0
251 else-
252 diff -= modulus;
never executed: diff -= modulus;
0
253 }-
254 if (useMass)
useMassDescription
TRUEevaluated 310 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 236 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
236-310
255 velocity = velocity + (spring * diff - damping * velocity) / mass;
executed 310 times by 2 tests: velocity = velocity + (spring * diff - damping * velocity) / mass;
Executed by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
310
256 else-
257 velocity = velocity + spring * diff - damping * velocity;
executed 236 times by 1 test: velocity = velocity + spring * diff - damping * velocity;
Executed by:
  • tst_qquickspringanimation
236
258 if (maxVelocity > 0.) {
maxVelocity > 0.Description
TRUEevaluated 218 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 328 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
218-328
259 // limit velocity-
260 if (velocity > maxVelocity)
velocity > maxVelocityDescription
TRUEevaluated 136 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 82 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
82-136
261 velocity = maxVelocity;
executed 136 times by 1 test: velocity = maxVelocity;
Executed by:
  • tst_qquickspringanimation
136
262 else if (velocity < -maxVelocity)
velocity < -maxVelocityDescription
TRUEnever evaluated
FALSEevaluated 82 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
0-82
263 velocity = -maxVelocity;
never executed: velocity = -maxVelocity;
0
264 }
executed 218 times by 1 test: end of block
Executed by:
  • tst_qquickspringanimation
218
265 currentValue += velocity * 16.0 / 1000.0;-
266 if (haveModulus) {
haveModulusDescription
TRUEevaluated 218 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 328 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
218-328
267 currentValue = fmod(currentValue, modulus);-
268 if (currentValue < 0.0)
currentValue < 0.0Description
TRUEnever evaluated
FALSEevaluated 218 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
0-218
269 currentValue += modulus;
never executed: currentValue += modulus;
0
270 }
executed 218 times by 1 test: end of block
Executed by:
  • tst_qquickspringanimation
218
271 }
executed 546 times by 2 tests: end of block
Executed by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
546
272 if (qAbs(velocity) < epsilon && qAbs(srcVal - currentValue) < epsilon) {
qAbs(velocity) < epsilonDescription
TRUEevaluated 8 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 533 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
qAbs(srcVal - ...lue) < epsilonDescription
TRUEevaluated 8 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEnever evaluated
0-533
273 velocity = 0.0;-
274 currentValue = srcVal;-
275 stopped = true;-
276 }
executed 8 times by 2 tests: end of block
Executed by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
8
277 } else {
executed 541 times by 2 tests: end of block
Executed by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
541
278 qreal moveBy = elapsed * velocityms;-
279 qreal diff = srcVal - currentValue;-
280 if (haveModulus && qAbs(diff) > modulus / 2) {
haveModulusDescription
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickanimations
qAbs(diff) > modulus / 2Description
TRUEnever evaluated
FALSEnever evaluated
0-2
281 if (diff < 0)
diff < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
282 diff += modulus;
never executed: diff += modulus;
0
283 else-
284 diff -= modulus;
never executed: diff -= modulus;
0
285 }-
286 if (diff > 0) {
diff > 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickanimations
FALSEnever evaluated
0-2
287 currentValue += moveBy;-
288 if (haveModulus)
haveModulusDescription
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickanimations
0-2
289 currentValue = std::fmod(currentValue, modulus);
never executed: currentValue = std::fmod(currentValue, modulus);
0
290 } else {
executed 2 times by 1 test: end of block
Executed by:
  • tst_qquickanimations
2
291 currentValue -= moveBy;-
292 if (haveModulus && currentValue < 0.0)
haveModulusDescription
TRUEnever evaluated
FALSEnever evaluated
currentValue < 0.0Description
TRUEnever evaluated
FALSEnever evaluated
0
293 currentValue = std::fmod(currentValue, modulus) + modulus;
never executed: currentValue = std::fmod(currentValue, modulus) + modulus;
0
294 }
never executed: end of block
0
295 if (lastTime - startTime >= dura) {
lastTime - startTime >= duraDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickanimations
FALSEnever evaluated
0-2
296 currentValue = to;-
297 stopped = true;-
298 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_qquickanimations
2
299 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_qquickanimations
2
300-
301 qreal old_to = to;-
302-
303 QQmlPropertyPrivate::write(target, currentValue,-
304 QQmlPropertyData::BypassInterceptor |-
305 QQmlPropertyData::DontRemoveBinding);-
306-
307 if (stopped && old_to == to) { // do not stop if we got restarted
stoppedDescription
TRUEevaluated 10 times by 3 tests
Evaluated by:
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 533 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
old_to == toDescription
TRUEevaluated 10 times by 3 tests
Evaluated by:
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEnever evaluated
0-533
308 if (animationTemplate)
animationTemplateDescription
TRUEevaluated 8 times by 3 tests
Evaluated by:
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
2-8
309 stopTime = animationTemplate->elapsed.elapsed();
executed 8 times by 3 tests: stopTime = animationTemplate->elapsed.elapsed();
Executed by:
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
8
310 stop();-
311 }
executed 10 times by 3 tests: end of block
Executed by:
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
10
312}
executed 543 times by 3 tests: end of block
Executed by:
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
543
313-
314void QSpringAnimation::updateState(QAbstractAnimationJob::State newState, QAbstractAnimationJob::State /*oldState*/)-
315{-
316 if (newState == QAbstractAnimationJob::Running)
newState == QA...onJob::RunningDescription
TRUEevaluated 26 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 10 times by 3 tests
Evaluated by:
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
10-26
317 init();
executed 26 times by 4 tests: init();
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
26
318}
executed 36 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
36
319-
320void QSpringAnimation::debugAnimation(QDebug d) const-
321{-
322 d << "SpringAnimationJob(" << hex << (const void *) this << dec << ")" << "velocity:" << maxVelocity-
323 << "spring:" << spring << "damping:" << damping << "epsilon:" << epsilon << "modulus:" << modulus-
324 << "mass:" << mass << "target:" << target.object() << "property:" << target.name()-
325 << "to:" << to << "current velocity:" << velocity;-
326}
never executed: end of block
0
327-
328-
329void QQuickSpringAnimationPrivate::updateMode()-
330{-
331 if (spring == 0. && maxVelocity == 0.)
spring == 0.Description
TRUEevaluated 8 times by 3 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickspringanimation
FALSEevaluated 54 times by 3 tests
Evaluated by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
maxVelocity == 0.Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 4 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
4-54
332 mode = QSpringAnimation::Track;
executed 4 times by 1 test: mode = QSpringAnimation::Track;
Executed by:
  • tst_qquickspringanimation
4
333 else if (spring > 0.)
spring > 0.Description
TRUEevaluated 54 times by 3 tests
Evaluated by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 4 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
4-54
334 mode = QSpringAnimation::Spring;
executed 54 times by 3 tests: mode = QSpringAnimation::Spring;
Executed by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
54
335 else {-
336 mode = QSpringAnimation::Velocity;-
337 for (QSpringAnimation::ActiveAnimationHashIt it = activeAnimations.begin(), end = activeAnimations.end(); it != end; ++it) {
it != endDescription
TRUEnever evaluated
FALSEevaluated 4 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
0-4
338 QSpringAnimation *animation = *it;-
339 animation->startTime = animation->lastTime;-
340 qreal dist = qAbs(animation->currentValue - animation->to);-
341 if (haveModulus && dist > modulus / 2)
haveModulusDescription
TRUEnever evaluated
FALSEnever evaluated
dist > modulus / 2Description
TRUEnever evaluated
FALSEnever evaluated
0
342 dist = modulus - fmod(dist, modulus);
never executed: dist = modulus - fmod(dist, modulus);
0
343 animation->dura = dist / velocityms;-
344 }
never executed: end of block
0
345 }
executed 4 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
4
346}-
347-
348/*!-
349 \qmltype SpringAnimation-
350 \instantiates QQuickSpringAnimation-
351 \inqmlmodule QtQuick-
352 \ingroup qtquick-transitions-animations-
353 \inherits NumberAnimation-
354-
355 \brief Allows a property to track a value in a spring-like motion.-
356-
357 SpringAnimation mimics the oscillatory behavior of a spring, with the appropriate \l spring constant to-
358 control the acceleration and the \l damping to control how quickly the effect dies away.-
359-
360 You can also limit the maximum \l velocity of the animation.-
361-
362 The following \l Rectangle moves to the position of the mouse using a-
363 SpringAnimation when the mouse is clicked. The use of the \l Behavior-
364 on the \c x and \c y values indicates that whenever these values are-
365 changed, a SpringAnimation should be applied.-
366-
367 \snippet qml/springanimation.qml 0-
368-
369 Like any other animation type, a SpringAnimation can be applied in a-
370 number of ways, including transitions, behaviors and property value-
371 sources. The \l {Animation and Transitions in Qt Quick} documentation shows a-
372 variety of methods for creating animations.-
373-
374 \sa SmoothedAnimation, {Animation and Transitions in Qt Quick}, {Qt Quick Examples - Animation}, {Qt Quick Demo - Clocks}-
375*/-
376-
377QQuickSpringAnimation::QQuickSpringAnimation(QObject *parent)-
378: QQuickNumberAnimation(*(new QQuickSpringAnimationPrivate),parent)-
379{-
380}
executed 56 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
56
381-
382QQuickSpringAnimation::~QQuickSpringAnimation()-
383{-
384 Q_D(QQuickSpringAnimation);-
385 for (QSpringAnimation::ActiveAnimationHashIt it = d->activeAnimations.begin(), end = d->activeAnimations.end(); it != end; ++it)
it != endDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 54 times by 3 tests
Evaluated by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
2-54
386 it.value()->clearTemplate();
executed 2 times by 1 test: it.value()->clearTemplate();
Executed by:
  • tst_qquickspringanimation
2
387}
executed 54 times by 3 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
54
388-
389/*!-
390 \qmlproperty real QtQuick::SpringAnimation::velocity-
391-
392 This property holds the maximum velocity allowed when tracking the source.-
393-
394 The default value is 0 (no maximum velocity).-
395*/-
396-
397qreal QQuickSpringAnimation::velocity() const-
398{-
399 Q_D(const QQuickSpringAnimation);-
400 return d->maxVelocity;
executed 6 times by 1 test: return d->maxVelocity;
Executed by:
  • tst_qquickspringanimation
6
401}-
402-
403void QQuickSpringAnimation::setVelocity(qreal velocity)-
404{-
405 Q_D(QQuickSpringAnimation);-
406 d->maxVelocity = velocity;-
407 d->velocityms = velocity / 1000.0;-
408 d->updateMode();-
409}
executed 8 times by 3 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickspringanimation
8
410-
411/*!-
412 \qmlproperty real QtQuick::SpringAnimation::spring-
413-
414 This property describes how strongly the target is pulled towards the-
415 source. The default value is 0 (that is, the spring-like motion is disabled).-
416-
417 The useful value range is 0 - 5.0.-
418-
419 When this property is set and the \l velocity value is greater than 0,-
420 the \l velocity limits the maximum speed.-
421*/-
422qreal QQuickSpringAnimation::spring() const-
423{-
424 Q_D(const QQuickSpringAnimation);-
425 return d->spring;
executed 6 times by 1 test: return d->spring;
Executed by:
  • tst_qquickspringanimation
6
426}-
427-
428void QQuickSpringAnimation::setSpring(qreal spring)-
429{-
430 Q_D(QQuickSpringAnimation);-
431 d->spring = spring;-
432 d->updateMode();-
433}
executed 50 times by 3 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
50
434-
435/*!-
436 \qmlproperty real QtQuick::SpringAnimation::damping-
437 This property holds the spring damping value.-
438-
439 This value describes how quickly the spring-like motion comes to rest.-
440 The default value is 0.-
441-
442 The useful value range is 0 - 1.0. The lower the value, the faster it-
443 comes to rest.-
444*/-
445qreal QQuickSpringAnimation::damping() const-
446{-
447 Q_D(const QQuickSpringAnimation);-
448 return d->damping;
executed 6 times by 1 test: return d->damping;
Executed by:
  • tst_qquickspringanimation
6
449}-
450-
451void QQuickSpringAnimation::setDamping(qreal damping)-
452{-
453 Q_D(QQuickSpringAnimation);-
454 if (damping > 1.)
damping > 1.Description
TRUEnever evaluated
FALSEevaluated 50 times by 3 tests
Evaluated by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
0-50
455 damping = 1.;
never executed: damping = 1.;
0
456-
457 d->damping = damping;-
458}
executed 50 times by 3 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
50
459-
460-
461/*!-
462 \qmlproperty real QtQuick::SpringAnimation::epsilon-
463 This property holds the spring epsilon.-
464-
465 The epsilon is the rate and amount of change in the value which is close enough-
466 to 0 to be considered equal to zero. This will depend on the usage of the value.-
467 For pixel positions, 0.25 would suffice. For scale, 0.005 will suffice.-
468-
469 The default is 0.01. Tuning this value can provide small performance improvements.-
470*/-
471qreal QQuickSpringAnimation::epsilon() const-
472{-
473 Q_D(const QQuickSpringAnimation);-
474 return d->epsilon;
executed 6 times by 1 test: return d->epsilon;
Executed by:
  • tst_qquickspringanimation
6
475}-
476-
477void QQuickSpringAnimation::setEpsilon(qreal epsilon)-
478{-
479 Q_D(QQuickSpringAnimation);-
480 d->epsilon = epsilon;-
481}
executed 4 times by 1 test: end of block
Executed by:
  • tst_qquickspringanimation
4
482-
483/*!-
484 \qmlproperty real QtQuick::SpringAnimation::modulus-
485 This property holds the modulus value. The default value is 0.-
486-
487 Setting a \a modulus forces the target value to "wrap around" at the modulus.-
488 For example, setting the modulus to 360 will cause a value of 370 to wrap around to 10.-
489*/-
490qreal QQuickSpringAnimation::modulus() const-
491{-
492 Q_D(const QQuickSpringAnimation);-
493 return d->modulus;
executed 6 times by 1 test: return d->modulus;
Executed by:
  • tst_qquickspringanimation
6
494}-
495-
496void QQuickSpringAnimation::setModulus(qreal modulus)-
497{-
498 Q_D(QQuickSpringAnimation);-
499 if (d->modulus != modulus) {
d->modulus != modulusDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEnever evaluated
0-4
500 d->haveModulus = modulus != 0.0;-
501 d->modulus = modulus;-
502 d->updateMode();-
503 emit modulusChanged();-
504 }
executed 4 times by 1 test: end of block
Executed by:
  • tst_qquickspringanimation
4
505}
executed 4 times by 1 test: end of block
Executed by:
  • tst_qquickspringanimation
4
506-
507/*!-
508 \qmlproperty real QtQuick::SpringAnimation::mass-
509 This property holds the "mass" of the property being moved.-
510-
511 The value is 1.0 by default.-
512-
513 A greater mass causes slower movement and a greater spring-like-
514 motion when an item comes to rest.-
515*/-
516qreal QQuickSpringAnimation::mass() const-
517{-
518 Q_D(const QQuickSpringAnimation);-
519 return d->mass;
executed 6 times by 1 test: return d->mass;
Executed by:
  • tst_qquickspringanimation
6
520}-
521-
522void QQuickSpringAnimation::setMass(qreal mass)-
523{-
524 Q_D(QQuickSpringAnimation);-
525 if (d->mass != mass && mass > 0.0) {
d->mass != massDescription
TRUEevaluated 6 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 32 times by 1 test
Evaluated by:
  • tst_examples
mass > 0.0Description
TRUEevaluated 6 times by 2 tests
Evaluated by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEnever evaluated
0-32
526 d->useMass = mass != 1.0;-
527 d->mass = mass;-
528 emit massChanged();-
529 }
executed 6 times by 2 tests: end of block
Executed by:
  • tst_qquickbehaviors
  • tst_qquickspringanimation
6
530}
executed 38 times by 3 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
38
531-
532QAbstractAnimationJob* QQuickSpringAnimation::transition(QQuickStateActions &actions,-
533 QQmlProperties &modified,-
534 TransitionDirection direction,-
535 QObject *defaultTarget)-
536{-
537 Q_D(QQuickSpringAnimation);-
538 Q_UNUSED(direction);-
539-
540 QContinuingAnimationGroupJob *wrapperGroup = new QContinuingAnimationGroupJob();-
541-
542 QQuickStateActions dataActions = QQuickNumberAnimation::createTransitionActions(actions, modified, defaultTarget);-
543 if (!dataActions.isEmpty()) {
!dataActions.isEmpty()Description
TRUEevaluated 28 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEnever evaluated
0-28
544 QSet<QAbstractAnimationJob*> anims;-
545 for (int i = 0; i < dataActions.size(); ++i) {
i < dataActions.size()Description
TRUEevaluated 28 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
FALSEevaluated 28 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
28
546 QSpringAnimation *animation;-
547 bool needsRestart = false;-
548 const QQmlProperty &property = dataActions.at(i).property;-
549 if (d->activeAnimations.contains(property)) {
d->activeAnima...ains(property)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickbehaviors
FALSEevaluated 26 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
2-26
550 animation = d->activeAnimations[property];-
551 needsRestart = true;-
552 } else {
executed 2 times by 1 test: end of block
Executed by:
  • tst_qquickbehaviors
2
553 animation = new QSpringAnimation(d);-
554 d->activeAnimations.insert(property, animation);-
555 animation->target = property;-
556 }
executed 26 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
26
557 wrapperGroup->appendAnimation(initInstance(animation));-
558-
559 animation->to = dataActions.at(i).toValue.toReal();-
560 animation->startTime = 0;-
561 animation->velocityms = d->velocityms;-
562 animation->mass = d->mass;-
563 animation->spring = d->spring;-
564 animation->damping = d->damping;-
565 animation->epsilon = d->epsilon;-
566 animation->modulus = d->modulus;-
567 animation->useMass = d->useMass;-
568 animation->haveModulus = d->haveModulus;-
569 animation->mode = d->mode;-
570 animation->dura = -1;-
571 animation->maxVelocity = d->maxVelocity;-
572-
573 if (d->fromIsDefined)
d->fromIsDefinedDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 24 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
4-24
574 animation->currentValue = dataActions.at(i).fromValue.toReal();
executed 4 times by 1 test: animation->currentValue = dataActions.at(i).fromValue.toReal();
Executed by:
  • tst_qquickspringanimation
4
575 else-
576 animation->currentValue = property.read().toReal();
executed 24 times by 4 tests: animation->currentValue = property.read().toReal();
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
24
577 if (animation->mode == QSpringAnimation::Velocity) {
animation->mod...tion::VelocityDescription
TRUEevaluated 4 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
FALSEevaluated 24 times by 3 tests
Evaluated by:
  • tst_examples
  • tst_qquickbehaviors
  • tst_qquickspringanimation
4-24
578 qreal dist = qAbs(animation->currentValue - animation->to);-
579 if (d->haveModulus && dist > d->modulus / 2)
d->haveModulusDescription
TRUEnever evaluated
FALSEevaluated 4 times by 2 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
dist > d->modulus / 2Description
TRUEnever evaluated
FALSEnever evaluated
0-4
580 dist = d->modulus - fmod(dist, d->modulus);
never executed: dist = d->modulus - fmod(dist, d->modulus);
0
581 animation->dura = dist / animation->velocityms;-
582 }
executed 4 times by 2 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
4
583-
584 if (needsRestart)
needsRestartDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickbehaviors
FALSEevaluated 26 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
2-26
585 animation->restart();
executed 2 times by 1 test: animation->restart();
Executed by:
  • tst_qquickbehaviors
2
586 anims.insert(animation);-
587 }
executed 28 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
28
588 const auto copy = d->activeAnimations;-
589 for (QSpringAnimation *anim : copy) {-
590 if (!anims.contains(anim)) {
!anims.contains(anim)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qquickspringanimation
FALSEevaluated 28 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
2-28
591 anim->clearTemplate();-
592 d->activeAnimations.remove(anim->target);-
593 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_qquickspringanimation
2
594 }
executed 30 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
30
595 }
executed 28 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
28
596 return wrapperGroup;
executed 28 times by 4 tests: return wrapperGroup;
Executed by:
  • tst_examples
  • tst_qquickanimations
  • tst_qquickbehaviors
  • tst_qquickspringanimation
28
597}-
598-
599QT_END_NAMESPACE-
600-
601#include "moc_qquickspringanimation_p.cpp"-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.0