animation/qpropertyanimation.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/****************************************************************************-
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/****************************************************************************
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 -
100QT_BEGIN_NAMESPACE -
101 -
102void QPropertyAnimationPrivate::updateMetaProperty() -
103{ -
104 if (!target || propertyName.isEmpty()) { -
105 propertyType = QVariant::Invalid; -
106 propertyIndex = -1; -
107 return; -
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(); -
113 propertyIndex = targetValue->metaObject()->indexOfProperty(propertyName); -
114 -
115 if (propertyType != QVariant::Invalid) -
116 convertValues(propertyType); -
117 if (propertyIndex == -1) { -
118 //there is no Q_PROPERTY on the object -
119 propertyType = QVariant::Invalid; -
120 if (!targetValue->dynamicPropertyNames().contains(propertyName)) -
121 qWarning("QPropertyAnimation: you're trying to animate a non-existing property %s of your QObject", propertyName.constData()); -
122 } else if (!targetValue->metaObject()->property(propertyIndex).isWritable()) { -
123 qWarning("QPropertyAnimation: you're trying to animate the non-writable property %s of your QObject", propertyName.constData()); -
124 } -
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:2212
66-2212
130 return;
executed: return;
Execution Count:66
66
131 -
132 if (!target) {
evaluated: !target
TRUEFALSE
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
TRUEFALSE
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 *dataargv[] = { 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, &dataargv);
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*/ -
153QPropertyAnimation::QPropertyAnimation(QObject *parent) -
154 : QVariantAnimation(*new QPropertyAnimationPrivate, parent) -
155{ -
156} -
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*/ -
165QPropertyAnimation::QPropertyAnimation(QObject *target, const QByteArray &propertyName, QObject *parent) -
166 : QVariantAnimation(*new QPropertyAnimationPrivate, parent) -
167{ -
168 setTargetObject(target); -
169 setPropertyName(propertyName); -
170} -
171 -
172/*! -
173 Destroys the QPropertyAnimation instance. -
174 */ -
175QPropertyAnimation::~QPropertyAnimation() -
176{ -
177 stop(); -
178} -
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 */ -
186QObject *QPropertyAnimation::targetObject() const -
187{ -
188 return d_func()->target.data(); -
189} -
190 -
191void QPropertyAnimation::setTargetObject(QObject *target) -
192{ -
193 Q_D(QPropertyAnimation); -
194 if (d->target.data() == target) -
195 return; -
196 -
197 if (d->state != QAbstractAnimation::Stopped) { -
198 qWarning("QPropertyAnimation::setTargetObject: you can't change the target of a running animation"); -
199 return; -
200 } -
201 -
202 d->target = d->targetValue = target; -
203 d->updateMetaProperty(); -
204} -
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 */ -
213QByteArray QPropertyAnimation::propertyName() const -
214{ -
215 Q_D(const QPropertyAnimation); -
216 return d->propertyName; -
217} -
218 -
219void QPropertyAnimation::setPropertyName(const QByteArray &propertyName) -
220{ -
221 Q_D(QPropertyAnimation); -
222 if (d->state != QAbstractAnimation::Stopped) { -
223 qWarning("QPropertyAnimation::setPropertyName: you can't change the property name of a running animation"); -
224 return; -
225 } -
226 -
227 d->propertyName = propertyName; -
228 d->updateMetaProperty(); -
229} -
230 -
231 -
232/*! -
233 \reimp -
234 */ -
235bool QPropertyAnimation::event(QEvent *event) -
236{ -
237 return QVariantAnimation::event(event); -
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 */ -
247void QPropertyAnimation::updateCurrentValue(const QVariant &value) -
248{ -
249 Q_D(QPropertyAnimation); -
250 d->updateProperty(value); -
251} -
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*/ -
259void QPropertyAnimation::updateState(QAbstractAnimation::State newState, -
260 QAbstractAnimation::State oldState) -
261{ -
262 Q_D(QPropertyAnimation); -
263 -
264 if (!d->target && oldState == Stopped) { -
265 qWarning("QPropertyAnimation::updateState (%s): Changing state of an animation without target", -
266 d->propertyName.constData()); -
267 return; -
268 } -
269 -
270 QVariantAnimation::updateState(newState, oldState); -
271 -
272 QPropertyAnimation *animToStop = 0; -
273 { -
274#ifndef QT_NO_THREAD -
275 static QBasicMutex mutex; -
276 QMutexLocker locker(&mutex); -
277#endif -
278 typedef QPair<QObject *, QByteArray> QPropertyAnimationPair; -
279 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); -
284 if (newState == Running) { -
285 d->updateMetaProperty(); -
286 animToStop = hash.value(key, 0); -
287 hash.insert(key, this); -
288 locker.unlock(); -
289 // update the default start value -
290 if (oldState == Stopped) { -
291 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())) { -
294 qWarning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without start value", -
295 d->propertyName.constData(), d->target.data()->metaObject()->className(), -
296 qPrintable(d->target.data()->objectName())); -
297 } -
298 if (!endValue().isValid() && (d->direction == Forward || !d->defaultStartEndValue.isValid())) { -
299 qWarning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without end value", -
300 d->propertyName.constData(), d->target.data()->metaObject()->className(), -
301 qPrintable(d->target.data()->objectName())); -
302 } -
303 } -
304 } else if (hash.value(key) == this) { -
305 hash.remove(key); -
306 } -
307 } -
308 -
309 //we need to do that after the mutex was unlocked -
310 if (animToStop) { -
311 // try to stop the top level group -
312 QAbstractAnimation *current = animToStop; -
313 while (current->group() && current->state() != Stopped) -
314 current = current->group(); -
315 current->stop(); -
316 } -
317} -
318 -
319#include "moc_qpropertyanimation.cpp" -
320 -
321QT_END_NAMESPACE -
322 -
323#endif //QT_NO_ANIMATION -
324 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial