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