Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 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 | | - |
100 | QT_BEGIN_NAMESPACE | - |
101 | | - |
102 | void QPropertyAnimationPrivate::updateMetaProperty() | - |
103 | { | - |
104 | if (!target || propertyName.isEmpty()) { partially evaluated: !target no Evaluation Count:0 | yes Evaluation Count:1330 |
evaluated: propertyName.isEmpty() yes Evaluation Count:434 | yes Evaluation Count:896 |
| 0-1330 |
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:434 | 434 |
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 yes Evaluation Count:896 | no Evaluation Count:0 |
| 0-896 |
116 | convertValues(propertyType); executed: convertValues(propertyType); Execution Count:896 | 896 |
117 | if (propertyIndex == -1) { evaluated: propertyIndex == -1 yes Evaluation Count:181 | yes Evaluation Count:715 |
| 181-715 |
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) 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() no Evaluation Count:0 | yes Evaluation Count:715 |
| 0-715 |
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 | } | 0 |
125 | } | - |
126 | | - |
127 | void QPropertyAnimationPrivate::updateProperty(const QVariant &newValue) | - |
128 | { | - |
129 | if (state == QAbstractAnimation::Stopped) evaluated: state == QAbstractAnimation::Stopped yes Evaluation Count:66 | yes Evaluation Count:2212 |
| 66-2212 |
130 | return; executed: return; Execution Count:66 | 66 |
131 | | - |
132 | if (!target) { evaluated: !target yes Evaluation Count:1 | yes Evaluation Count:2211 |
| 1-2211 |
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 yes Evaluation Count:1124 | yes Evaluation Count:1087 |
| 1087-1124 |
138 | //no conversion is needed, we directly call the QMetaObject::metacall | - |
139 | //check QMetaProperty::write for an explanation of these | - |
140 | int status = -1; executed (the execution status of this line is deduced): int status = -1; | - |
141 | int flags = 0; executed (the execution status of this line is deduced): int flags = 0; | - |
142 | void *argv[] = { const_cast<void *>(newValue.constData()), const_cast<QVariant *>(&newValue), &status, &flags }; executed (the execution status of this line is deduced): void *argv[] = { const_cast<void *>(newValue.constData()), const_cast<QVariant *>(&newValue), &status, &flags }; | - |
143 | QMetaObject::metacall(targetValue, QMetaObject::WriteProperty, propertyIndex, argv); executed (the execution status of this line is deduced): QMetaObject::metacall(targetValue, QMetaObject::WriteProperty, propertyIndex, argv); | - |
144 | } else { executed: } Execution Count:1124 | 1124 |
145 | targetValue->setProperty(propertyName.constData(), newValue); executed (the execution status of this line is deduced): targetValue->setProperty(propertyName.constData(), newValue); | - |
146 | } executed: } Execution Count:1087 | 1087 |
147 | } | - |
148 | | - |
149 | /*! | - |
150 | Construct a QPropertyAnimation object. \a parent is passed to QObject's | - |
151 | constructor. | - |
152 | */ | - |
153 | QPropertyAnimation::QPropertyAnimation(QObject *parent) | - |
154 | : QVariantAnimation(*new QPropertyAnimationPrivate, parent) | - |
155 | { | - |
156 | } executed: } Execution Count:79 | 79 |
157 | | - |
158 | /*! | - |
159 | Construct a QPropertyAnimation object. \a parent is passed to QObject's | - |
160 | constructor. The animation changes the property \a propertyName on \a | - |
161 | target. The default duration is 250ms. | - |
162 | | - |
163 | \sa targetObject, propertyName | - |
164 | */ | - |
165 | QPropertyAnimation::QPropertyAnimation(QObject *target, const QByteArray &propertyName, QObject *parent) | - |
166 | : QVariantAnimation(*new QPropertyAnimationPrivate, parent) | - |
167 | { | - |
168 | setTargetObject(target); executed (the execution status of this line is deduced): setTargetObject(target); | - |
169 | setPropertyName(propertyName); executed (the execution status of this line is deduced): setPropertyName(propertyName); | - |
170 | } executed: } Execution Count:395 | 395 |
171 | | - |
172 | /*! | - |
173 | Destroys the QPropertyAnimation instance. | - |
174 | */ | - |
175 | QPropertyAnimation::~QPropertyAnimation() | - |
176 | { | - |
177 | stop(); executed (the execution status of this line is deduced): stop(); | - |
178 | } executed: } Execution Count:470 | 470 |
179 | | - |
180 | /*! | - |
181 | \property QPropertyAnimation::targetObject | - |
182 | \brief the target QObject for this animation. | - |
183 | | - |
184 | This property defines the target QObject for this animation. | - |
185 | */ | - |
186 | QObject *QPropertyAnimation::targetObject() const | - |
187 | { | - |
188 | return d_func()->target.data(); executed: return d_func()->target.data(); Execution Count:333 | 333 |
189 | } | - |
190 | | - |
191 | void QPropertyAnimation::setTargetObject(QObject *target) | - |
192 | { | - |
193 | Q_D(QPropertyAnimation); executed (the execution status of this line is deduced): QPropertyAnimationPrivate * const d = d_func(); | - |
194 | if (d->target.data() == target) partially evaluated: d->target.data() == target no Evaluation Count:0 | yes Evaluation Count:434 |
| 0-434 |
195 | return; | 0 |
196 | | - |
197 | if (d->state != QAbstractAnimation::Stopped) { partially evaluated: d->state != QAbstractAnimation::Stopped no Evaluation Count:0 | yes Evaluation Count:434 |
| 0-434 |
198 | 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", 198, __PRETTY_FUNCTION__).warning("QPropertyAnimation::setTargetObject: you can't change the target of a running animation"); | - |
199 | return; | 0 |
200 | } | - |
201 | | - |
202 | d->target = d->targetValue = target; executed (the execution status of this line is deduced): d->target = d->targetValue = target; | - |
203 | d->updateMetaProperty(); executed (the execution status of this line is deduced): d->updateMetaProperty(); | - |
204 | } executed: } Execution Count:434 | 434 |
205 | | - |
206 | /*! | - |
207 | \property QPropertyAnimation::propertyName | - |
208 | \brief the target property name for this animation | - |
209 | | - |
210 | This property defines the target property name for this animation. The | - |
211 | property name is required for the animation to operate. | - |
212 | */ | - |
213 | QByteArray QPropertyAnimation::propertyName() const | - |
214 | { | - |
215 | Q_D(const QPropertyAnimation); executed (the execution status of this line is deduced): const QPropertyAnimationPrivate * const d = d_func(); | - |
216 | return d->propertyName; executed: return d->propertyName; Execution Count:46 | 46 |
217 | } | - |
218 | | - |
219 | void QPropertyAnimation::setPropertyName(const QByteArray &propertyName) | - |
220 | { | - |
221 | Q_D(QPropertyAnimation); executed (the execution status of this line is deduced): QPropertyAnimationPrivate * const d = d_func(); | - |
222 | if (d->state != QAbstractAnimation::Stopped) { partially evaluated: d->state != QAbstractAnimation::Stopped no Evaluation Count:0 | yes Evaluation Count:434 |
| 0-434 |
223 | 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", 223, __PRETTY_FUNCTION__).warning("QPropertyAnimation::setPropertyName: you can't change the property name of a running animation"); | - |
224 | return; | 0 |
225 | } | - |
226 | | - |
227 | d->propertyName = propertyName; executed (the execution status of this line is deduced): d->propertyName = propertyName; | - |
228 | d->updateMetaProperty(); executed (the execution status of this line is deduced): d->updateMetaProperty(); | - |
229 | } executed: } Execution Count:434 | 434 |
230 | | - |
231 | | - |
232 | /*! | - |
233 | \reimp | - |
234 | */ | - |
235 | bool QPropertyAnimation::event(QEvent *event) | - |
236 | { | - |
237 | return QVariantAnimation::event(event); executed: return QVariantAnimation::event(event); Execution Count:249 | 249 |
238 | } | - |
239 | | - |
240 | /*! | - |
241 | This virtual function is called by QVariantAnimation whenever the current value | - |
242 | changes. \a value is the new, updated value. It updates the current value | - |
243 | of the property on the target object. | - |
244 | | - |
245 | \sa currentValue, currentTime | - |
246 | */ | - |
247 | void QPropertyAnimation::updateCurrentValue(const QVariant &value) | - |
248 | { | - |
249 | Q_D(QPropertyAnimation); executed (the execution status of this line is deduced): QPropertyAnimationPrivate * const d = d_func(); | - |
250 | d->updateProperty(value); executed (the execution status of this line is deduced): d->updateProperty(value); | - |
251 | } executed: } Execution Count:2278 | 2278 |
252 | | - |
253 | /*! | - |
254 | \reimp | - |
255 | | - |
256 | If the startValue is not defined when the state of the animation changes from Stopped to Running, | - |
257 | the current property value is used as the initial value for the animation. | - |
258 | */ | - |
259 | void QPropertyAnimation::updateState(QAbstractAnimation::State newState, | - |
260 | QAbstractAnimation::State oldState) | - |
261 | { | - |
262 | Q_D(QPropertyAnimation); executed (the execution status of this line is deduced): QPropertyAnimationPrivate * const d = d_func(); | - |
263 | | - |
264 | if (!d->target && oldState == Stopped) { evaluated: !d->target yes Evaluation Count:6 | yes Evaluation Count:943 |
partially evaluated: oldState == Stopped no Evaluation Count:0 | yes Evaluation Count:6 |
| 0-943 |
265 | 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", 265, __PRETTY_FUNCTION__).warning("QPropertyAnimation::updateState (%s): Changing state of an animation without target", | - |
266 | d->propertyName.constData()); never executed (the execution status of this line is deduced): d->propertyName.constData()); | - |
267 | return; | 0 |
268 | } | - |
269 | | - |
270 | QVariantAnimation::updateState(newState, oldState); executed (the execution status of this line is deduced): QVariantAnimation::updateState(newState, oldState); | - |
271 | | - |
272 | QPropertyAnimation *animToStop = 0; executed (the execution status of this line is deduced): QPropertyAnimation *animToStop = 0; | - |
273 | { | - |
274 | #ifndef QT_NO_THREAD | - |
275 | static QBasicMutex mutex; | - |
276 | QMutexLocker locker(&mutex); executed (the execution status of this line is deduced): QMutexLocker locker(&mutex); | - |
277 | #endif | - |
278 | typedef QPair<QObject *, QByteArray> QPropertyAnimationPair; executed (the execution status of this line is deduced): typedef QPair<QObject *, QByteArray> QPropertyAnimationPair; | - |
279 | typedef QHash<QPropertyAnimationPair, QPropertyAnimation*> QPropertyAnimationHash; executed (the execution status of this line is deduced): typedef QHash<QPropertyAnimationPair, QPropertyAnimation*> QPropertyAnimationHash; | - |
280 | static QPropertyAnimationHash hash; | - |
281 | //here we need to use value because we need to know to which pointer | - |
282 | //the animation was referring in case stopped because the target was destroyed | - |
283 | QPropertyAnimationPair key(d->targetValue, d->propertyName); executed (the execution status of this line is deduced): QPropertyAnimationPair key(d->targetValue, d->propertyName); | - |
284 | if (newState == Running) { evaluated: newState == Running yes Evaluation Count:462 | yes Evaluation Count:487 |
| 462-487 |
285 | d->updateMetaProperty(); executed (the execution status of this line is deduced): d->updateMetaProperty(); | - |
286 | animToStop = hash.value(key, 0); executed (the execution status of this line is deduced): animToStop = hash.value(key, 0); | - |
287 | hash.insert(key, this); executed (the execution status of this line is deduced): hash.insert(key, this); | - |
288 | locker.unlock(); executed (the execution status of this line is deduced): locker.unlock(); | - |
289 | // update the default start value | - |
290 | if (oldState == Stopped) { evaluated: oldState == Stopped yes Evaluation Count:458 | yes Evaluation Count:4 |
| 4-458 |
291 | 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())); | - |
292 | //let's check if we have a start value and an end value | - |
293 | if (!startValue().isValid() && (d->direction == Backward || !d->defaultStartEndValue.isValid())) { evaluated: !startValue().isValid() yes Evaluation Count:422 | yes Evaluation Count:36 |
partially evaluated: d->direction == Backward no Evaluation Count:0 | yes Evaluation Count:422 |
partially evaluated: !d->defaultStartEndValue.isValid() no Evaluation Count:0 | yes Evaluation Count:422 |
| 0-422 |
294 | 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", 294, __PRETTY_FUNCTION__).warning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without start value", | - |
295 | 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(), | - |
296 | qPrintable(d->target.data()->objectName())); never executed (the execution status of this line is deduced): QString(d->target.data()->objectName()).toLocal8Bit().constData()); | - |
297 | } | 0 |
298 | if (!endValue().isValid() && (d->direction == Forward || !d->defaultStartEndValue.isValid())) { evaluated: !endValue().isValid() yes Evaluation Count:2 | yes Evaluation Count:456 |
partially evaluated: d->direction == Forward no Evaluation Count:0 | yes Evaluation Count:2 |
partially evaluated: !d->defaultStartEndValue.isValid() no Evaluation Count:0 | yes Evaluation Count:2 |
| 0-456 |
299 | 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", 299, __PRETTY_FUNCTION__).warning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without end value", | - |
300 | 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(), | - |
301 | qPrintable(d->target.data()->objectName())); never executed (the execution status of this line is deduced): QString(d->target.data()->objectName()).toLocal8Bit().constData()); | - |
302 | } | 0 |
303 | } executed: } Execution Count:458 | 458 |
304 | } else if (hash.value(key) == this) { executed: } Execution Count:462 evaluated: hash.value(key) == this yes Evaluation Count:461 | yes Evaluation Count:26 |
| 26-462 |
305 | hash.remove(key); executed (the execution status of this line is deduced): hash.remove(key); | - |
306 | } executed: } Execution Count:461 | 461 |
307 | } | - |
308 | | - |
309 | //we need to do that after the mutex was unlocked | - |
310 | if (animToStop) { evaluated: animToStop yes Evaluation Count:1 | yes Evaluation Count:948 |
| 1-948 |
311 | // try to stop the top level group | - |
312 | QAbstractAnimation *current = animToStop; executed (the execution status of this line is deduced): QAbstractAnimation *current = animToStop; | - |
313 | while (current->group() && current->state() != Stopped) partially evaluated: current->group() no Evaluation Count:0 | yes Evaluation Count:1 |
never evaluated: current->state() != Stopped | 0-1 |
314 | current = current->group(); never executed: current = current->group(); | 0 |
315 | current->stop(); executed (the execution status of this line is deduced): current->stop(); | - |
316 | } executed: } Execution Count:1 | 1 |
317 | } executed: } Execution Count:949 | 949 |
318 | | - |
319 | #include "moc_qpropertyanimation.cpp" | - |
320 | | - |
321 | QT_END_NAMESPACE | - |
322 | | - |
323 | #endif //QT_NO_ANIMATION | - |
324 | | - |
| | |