animation/qpropertyanimation.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore 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 Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/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 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. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42/*! -
43 \class QPropertyAnimation -
44 \inmodule QtCore -
45 \brief The QPropertyAnimation class animates Qt properties -
46 \since 4.6 -
47 -
48 \ingroup animation -
49 -
50 QPropertyAnimation interpolates over \l{Qt's Property System}{Qt -
51 properties}. As property values are stored in \l{QVariant}s, the -
52 class inherits QVariantAnimation, and supports animation of the -
53 same \l{QMetaType::Type}{meta types} as its super class. -
54 -
55 A class declaring properties must be a QObject. To make it -
56 possible to animate a property, it must provide a setter (so that -
57 QPropertyAnimation can set the property's value). Note that this -
58 makes it possible to animate many of Qt's widgets. Let's look at -
59 an example: -
60 -
61 \code -
62 QPropertyAnimation *animation = new QPropertyAnimation(myWidget, "geometry"); -
63 animation->setDuration(10000); -
64 animation->setStartValue(QRect(0, 0, 100, 30)); -
65 animation->setEndValue(QRect(250, 250, 100, 30)); -
66 -
67 animation->start(); -
68 \endcode -
69 -
70 The property name and the QObject instance of which property -
71 should be animated are passed to the constructor. You can then -
72 specify the start and end value of the property. The procedure is -
73 equal for properties in classes you have implemented -
74 yourself--just check with QVariantAnimation that your QVariant -
75 type is supported. -
76 -
77 The QVariantAnimation class description explains how to set up the -
78 animation in detail. Note, however, that if a start value is not -
79 set, the property will start at the value it had when the -
80 QPropertyAnimation instance was created. -
81 -
82 QPropertyAnimation works like a charm on its own. For complex -
83 animations that, for instance, contain several objects, -
84 QAnimationGroup is provided. An animation group is an animation -
85 that can contain other animations, and that can manage when its -
86 animations are played. Look at QParallelAnimationGroup for an -
87 example. -
88 -
89 \sa QVariantAnimation, QAnimationGroup, {The Animation Framework} -
90*/ -
91 -
92#include "qpropertyanimation.h" -
93#include "qanimationgroup.h" -
94#include "qpropertyanimation_p.h" -
95 -
96#include <QtCore/QMutex> -
97 -
98#ifndef QT_NO_ANIMATION -
99 -
100QT_BEGIN_NAMESPACE -
101 -
102void QPropertyAnimationPrivate::updateMetaProperty() -
103{ -
104 if (!target || propertyName.isEmpty()) {
partially evaluated: !target
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1138
evaluated: propertyName.isEmpty()
TRUEFALSE
yes
Evaluation Count:370
yes
Evaluation Count:768
0-1138
105 propertyType = QVariant::Invalid;
executed (the execution status of this line is deduced): propertyType = QVariant::Invalid;
-
106 propertyIndex = -1;
executed (the execution status of this line is deduced): propertyIndex = -1;
-
107 return;
executed: return;
Execution Count:370
370
108 } -
109 -
110 //propertyType will be set to a valid type only if there is a Q_PROPERTY -
111 //otherwise it will be set to QVariant::Invalid at the end of this function -
112 propertyType = targetValue->property(propertyName).userType();
executed (the execution status of this line is deduced): propertyType = targetValue->property(propertyName).userType();
-
113 propertyIndex = targetValue->metaObject()->indexOfProperty(propertyName);
executed (the execution status of this line is deduced): propertyIndex = targetValue->metaObject()->indexOfProperty(propertyName);
-
114 -
115 if (propertyType != QVariant::Invalid)
partially evaluated: propertyType != QVariant::Invalid
TRUEFALSE
yes
Evaluation Count:768
no
Evaluation Count:0
0-768
116 convertValues(propertyType);
executed: convertValues(propertyType);
Execution Count:768
768
117 if (propertyIndex == -1) {
evaluated: propertyIndex == -1
TRUEFALSE
yes
Evaluation Count:181
yes
Evaluation Count:587
181-587
118 //there is no Q_PROPERTY on the object -
119 propertyType = QVariant::Invalid;
executed (the execution status of this line is deduced): propertyType = QVariant::Invalid;
-
120 if (!targetValue->dynamicPropertyNames().contains(propertyName))
partially evaluated: !targetValue->dynamicPropertyNames().contains(propertyName)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:181
0-181
121 qWarning("QPropertyAnimation: you're trying to animate a non-existing property %s of your QObject", propertyName.constData());
never executed: QMessageLogger("animation/qpropertyanimation.cpp", 121, __PRETTY_FUNCTION__).warning("QPropertyAnimation: you're trying to animate a non-existing property %s of your QObject", propertyName.constData());
0
122 } else if (!targetValue->metaObject()->property(propertyIndex).isWritable()) {
executed: }
Execution Count:181
partially evaluated: !targetValue->metaObject()->property(propertyIndex).isWritable()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:587
0-587
123 qWarning("QPropertyAnimation: you're trying to animate the non-writable property %s of your QObject", propertyName.constData());
never executed (the execution status of this line is deduced): QMessageLogger("animation/qpropertyanimation.cpp", 123, __PRETTY_FUNCTION__).warning("QPropertyAnimation: you're trying to animate the non-writable property %s of your QObject", propertyName.constData());
-
124 }
never executed: }
0
125} -
126 -
127void QPropertyAnimationPrivate::updateProperty(const QVariant &newValue) -
128{ -
129 if (state == QAbstractAnimation::Stopped)
evaluated: state == QAbstractAnimation::Stopped
TRUEFALSE
yes
Evaluation Count:66
yes
Evaluation Count:2088
66-2088
130 return;
executed: return;
Execution Count:66
66
131 -
132 if (!target) {
evaluated: !target
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:2087
1-2087
133 q_func()->stop(); //the target was destroyed we need to stop the animation
executed (the execution status of this line is deduced): q_func()->stop();
-
134 return;
executed: return;
Execution Count:1
1
135 } -
136 -
137 if (newValue.userType() == propertyType) {
evaluated: newValue.userType() == propertyType
TRUEFALSE
yes
Evaluation Count:996
yes
Evaluation Count:1091
996-1091
138 //no conversion is needed, we directly call the QMetaObject::metacall -
139 void *data = const_cast<void*>(newValue.constData());
executed (the execution status of this line is deduced): void *data = const_cast<void*>(newValue.constData());
-
140 QMetaObject::metacall(targetValue, QMetaObject::WriteProperty, propertyIndex, &data);
executed (the execution status of this line is deduced): QMetaObject::metacall(targetValue, QMetaObject::WriteProperty, propertyIndex, &data);
-
141 } else {
executed: }
Execution Count:996
996
142 targetValue->setProperty(propertyName.constData(), newValue);
executed (the execution status of this line is deduced): targetValue->setProperty(propertyName.constData(), newValue);
-
143 }
executed: }
Execution Count:1091
1091
144} -
145 -
146/*! -
147 Construct a QPropertyAnimation object. \a parent is passed to QObject's -
148 constructor. -
149*/ -
150QPropertyAnimation::QPropertyAnimation(QObject *parent) -
151 : QVariantAnimation(*new QPropertyAnimationPrivate, parent) -
152{ -
153}
executed: }
Execution Count:79
79
154 -
155/*! -
156 Construct a QPropertyAnimation object. \a parent is passed to QObject's -
157 constructor. The animation changes the property \a propertyName on \a -
158 target. The default duration is 250ms. -
159 -
160 \sa targetObject, propertyName -
161*/ -
162QPropertyAnimation::QPropertyAnimation(QObject *target, const QByteArray &propertyName, QObject *parent) -
163 : QVariantAnimation(*new QPropertyAnimationPrivate, parent) -
164{ -
165 setTargetObject(target);
executed (the execution status of this line is deduced): setTargetObject(target);
-
166 setPropertyName(propertyName);
executed (the execution status of this line is deduced): setPropertyName(propertyName);
-
167}
executed: }
Execution Count:331
331
168 -
169/*! -
170 Destroys the QPropertyAnimation instance. -
171 */ -
172QPropertyAnimation::~QPropertyAnimation() -
173{ -
174 stop();
executed (the execution status of this line is deduced): stop();
-
175}
executed: }
Execution Count:405
405
176 -
177/*! -
178 \property QPropertyAnimation::targetObject -
179 \brief the target QObject for this animation. -
180 -
181 This property defines the target QObject for this animation. -
182 */ -
183QObject *QPropertyAnimation::targetObject() const -
184{ -
185 return d_func()->target.data();
executed: return d_func()->target.data();
Execution Count:269
269
186} -
187 -
188void QPropertyAnimation::setTargetObject(QObject *target) -
189{ -
190 Q_D(QPropertyAnimation);
executed (the execution status of this line is deduced): QPropertyAnimationPrivate * const d = d_func();
-
191 if (d->target.data() == target)
partially evaluated: d->target.data() == target
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:370
0-370
192 return;
never executed: return;
0
193 -
194 if (d->state != QAbstractAnimation::Stopped) {
partially evaluated: d->state != QAbstractAnimation::Stopped
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:370
0-370
195 qWarning("QPropertyAnimation::setTargetObject: you can't change the target of a running animation");
never executed (the execution status of this line is deduced): QMessageLogger("animation/qpropertyanimation.cpp", 195, __PRETTY_FUNCTION__).warning("QPropertyAnimation::setTargetObject: you can't change the target of a running animation");
-
196 return;
never executed: return;
0
197 } -
198 -
199 d->target = d->targetValue = target;
executed (the execution status of this line is deduced): d->target = d->targetValue = target;
-
200 d->updateMetaProperty();
executed (the execution status of this line is deduced): d->updateMetaProperty();
-
201}
executed: }
Execution Count:370
370
202 -
203/*! -
204 \property QPropertyAnimation::propertyName -
205 \brief the target property name for this animation -
206 -
207 This property defines the target property name for this animation. The -
208 property name is required for the animation to operate. -
209 */ -
210QByteArray QPropertyAnimation::propertyName() const -
211{ -
212 Q_D(const QPropertyAnimation);
executed (the execution status of this line is deduced): const QPropertyAnimationPrivate * const d = d_func();
-
213 return d->propertyName;
executed: return d->propertyName;
Execution Count:46
46
214} -
215 -
216void QPropertyAnimation::setPropertyName(const QByteArray &propertyName) -
217{ -
218 Q_D(QPropertyAnimation);
executed (the execution status of this line is deduced): QPropertyAnimationPrivate * const d = d_func();
-
219 if (d->state != QAbstractAnimation::Stopped) {
partially evaluated: d->state != QAbstractAnimation::Stopped
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:370
0-370
220 qWarning("QPropertyAnimation::setPropertyName: you can't change the property name of a running animation");
never executed (the execution status of this line is deduced): QMessageLogger("animation/qpropertyanimation.cpp", 220, __PRETTY_FUNCTION__).warning("QPropertyAnimation::setPropertyName: you can't change the property name of a running animation");
-
221 return;
never executed: return;
0
222 } -
223 -
224 d->propertyName = propertyName;
executed (the execution status of this line is deduced): d->propertyName = propertyName;
-
225 d->updateMetaProperty();
executed (the execution status of this line is deduced): d->updateMetaProperty();
-
226}
executed: }
Execution Count:370
370
227 -
228 -
229/*! -
230 \reimp -
231 */ -
232bool QPropertyAnimation::event(QEvent *event) -
233{ -
234 return QVariantAnimation::event(event);
executed: return QVariantAnimation::event(event);
Execution Count:189
189
235} -
236 -
237/*! -
238 This virtual function is called by QVariantAnimation whenever the current value -
239 changes. \a value is the new, updated value. It updates the current value -
240 of the property on the target object. -
241 -
242 \sa currentValue, currentTime -
243 */ -
244void QPropertyAnimation::updateCurrentValue(const QVariant &value) -
245{ -
246 Q_D(QPropertyAnimation);
executed (the execution status of this line is deduced): QPropertyAnimationPrivate * const d = d_func();
-
247 d->updateProperty(value);
executed (the execution status of this line is deduced): d->updateProperty(value);
-
248}
executed: }
Execution Count:2154
2154
249 -
250/*! -
251 \reimp -
252 -
253 If the startValue is not defined when the state of the animation changes from Stopped to Running, -
254 the current property value is used as the initial value for the animation. -
255*/ -
256void QPropertyAnimation::updateState(QAbstractAnimation::State newState, -
257 QAbstractAnimation::State oldState) -
258{ -
259 Q_D(QPropertyAnimation);
executed (the execution status of this line is deduced): QPropertyAnimationPrivate * const d = d_func();
-
260 -
261 if (!d->target && oldState == Stopped) {
evaluated: !d->target
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:815
partially evaluated: oldState == Stopped
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:6
0-815
262 qWarning("QPropertyAnimation::updateState (%s): Changing state of an animation without target",
never executed (the execution status of this line is deduced): QMessageLogger("animation/qpropertyanimation.cpp", 262, __PRETTY_FUNCTION__).warning("QPropertyAnimation::updateState (%s): Changing state of an animation without target",
-
263 d->propertyName.constData());
never executed (the execution status of this line is deduced): d->propertyName.constData());
-
264 return;
never executed: return;
0
265 } -
266 -
267 QVariantAnimation::updateState(newState, oldState);
executed (the execution status of this line is deduced): QVariantAnimation::updateState(newState, oldState);
-
268 -
269 QPropertyAnimation *animToStop = 0;
executed (the execution status of this line is deduced): QPropertyAnimation *animToStop = 0;
-
270 { -
271#ifndef QT_NO_THREAD -
272 static QBasicMutex mutex; -
273 QMutexLocker locker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&mutex);
-
274#endif -
275 typedef QPair<QObject *, QByteArray> QPropertyAnimationPair;
executed (the execution status of this line is deduced): typedef QPair<QObject *, QByteArray> QPropertyAnimationPair;
-
276 typedef QHash<QPropertyAnimationPair, QPropertyAnimation*> QPropertyAnimationHash;
executed (the execution status of this line is deduced): typedef QHash<QPropertyAnimationPair, QPropertyAnimation*> QPropertyAnimationHash;
-
277 static QPropertyAnimationHash hash; -
278 //here we need to use value because we need to know to which pointer -
279 //the animation was referring in case stopped because the target was destroyed -
280 QPropertyAnimationPair key(d->targetValue, d->propertyName);
executed (the execution status of this line is deduced): QPropertyAnimationPair key(d->targetValue, d->propertyName);
-
281 if (newState == Running) {
evaluated: newState == Running
TRUEFALSE
yes
Evaluation Count:398
yes
Evaluation Count:423
398-423
282 d->updateMetaProperty();
executed (the execution status of this line is deduced): d->updateMetaProperty();
-
283 animToStop = hash.value(key, 0);
executed (the execution status of this line is deduced): animToStop = hash.value(key, 0);
-
284 hash.insert(key, this);
executed (the execution status of this line is deduced): hash.insert(key, this);
-
285 locker.unlock();
executed (the execution status of this line is deduced): locker.unlock();
-
286 // update the default start value -
287 if (oldState == Stopped) {
evaluated: oldState == Stopped
TRUEFALSE
yes
Evaluation Count:394
yes
Evaluation Count:4
4-394
288 d->setDefaultStartEndValue(d->targetValue->property(d->propertyName.constData()));
executed (the execution status of this line is deduced): d->setDefaultStartEndValue(d->targetValue->property(d->propertyName.constData()));
-
289 //let's check if we have a start value and an end value -
290 if (!startValue().isValid() && (d->direction == Backward || !d->defaultStartEndValue.isValid())) {
evaluated: !startValue().isValid()
TRUEFALSE
yes
Evaluation Count:358
yes
Evaluation Count:36
partially evaluated: d->direction == Backward
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:358
partially evaluated: !d->defaultStartEndValue.isValid()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:358
0-358
291 qWarning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without start value",
never executed (the execution status of this line is deduced): QMessageLogger("animation/qpropertyanimation.cpp", 291, __PRETTY_FUNCTION__).warning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without start value",
-
292 d->propertyName.constData(), d->target.data()->metaObject()->className(),
never executed (the execution status of this line is deduced): d->propertyName.constData(), d->target.data()->metaObject()->className(),
-
293 qPrintable(d->target.data()->objectName()));
never executed (the execution status of this line is deduced): QString(d->target.data()->objectName()).toLocal8Bit().constData());
-
294 }
never executed: }
0
295 if (!endValue().isValid() && (d->direction == Forward || !d->defaultStartEndValue.isValid())) {
evaluated: !endValue().isValid()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:392
partially evaluated: d->direction == Forward
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
partially evaluated: !d->defaultStartEndValue.isValid()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-392
296 qWarning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without end value",
never executed (the execution status of this line is deduced): QMessageLogger("animation/qpropertyanimation.cpp", 296, __PRETTY_FUNCTION__).warning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without end value",
-
297 d->propertyName.constData(), d->target.data()->metaObject()->className(),
never executed (the execution status of this line is deduced): d->propertyName.constData(), d->target.data()->metaObject()->className(),
-
298 qPrintable(d->target.data()->objectName()));
never executed (the execution status of this line is deduced): QString(d->target.data()->objectName()).toLocal8Bit().constData());
-
299 }
never executed: }
0
300 }
executed: }
Execution Count:394
394
301 } else if (hash.value(key) == this) {
executed: }
Execution Count:398
evaluated: hash.value(key) == this
TRUEFALSE
yes
Evaluation Count:397
yes
Evaluation Count:26
26-398
302 hash.remove(key);
executed (the execution status of this line is deduced): hash.remove(key);
-
303 }
executed: }
Execution Count:397
397
304 } -
305 -
306 //we need to do that after the mutex was unlocked -
307 if (animToStop) {
evaluated: animToStop
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:820
1-820
308 // try to stop the top level group -
309 QAbstractAnimation *current = animToStop;
executed (the execution status of this line is deduced): QAbstractAnimation *current = animToStop;
-
310 while (current->group() && current->state() != Stopped)
partially evaluated: current->group()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
never evaluated: current->state() != Stopped
0-1
311 current = current->group();
never executed: current = current->group();
0
312 current->stop();
executed (the execution status of this line is deduced): current->stop();
-
313 }
executed: }
Execution Count:1
1
314}
executed: }
Execution Count:821
821
315 -
316#include "moc_qpropertyanimation.cpp" -
317 -
318QT_END_NAMESPACE -
319 -
320#endif //QT_NO_ANIMATION -
321 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial