Qt 4.8
qpropertyanimation.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 
94 #include "qpropertyanimation.h"
95 #include "qanimationgroup.h"
96 #include "qpropertyanimation_p.h"
97 
98 #include <private/qmutexpool_p.h>
99 
100 #ifndef QT_NO_ANIMATION
101 
103 
105 {
106  if (!target || propertyName.isEmpty()) {
108  propertyIndex = -1;
109  return;
110  }
111 
112  //propertyType will be set to a valid type only if there is a propertyName).userType();
113  ;->indexOfProperty(propertyName);
114 
117  if (propertyIndex == -1) {
118  //there is no !targetValue->dynamicPropertyNames().contains(propertyName))
119  ;);
121  qWarning("QPropertyAnimation: you're trying to animate the non-writable property %s of your QObject", propertyName.constData());
122  }
123 }
124 
126 {
128  return;
129 
130  if (!target) {
131  q_func()->stop(); //the target was destroyed we need to stop the animation
132  return;
133  }
134 
135  if (newValue.userType() == propertyType) {
136  //no conversion is needed, we directly call the QMetaObject::metacall
137  //check QMetaProperty::write for an explanation of these
138  int status = -1;
139  int flags = 0;
140  void *argv[] = { const_cast<void *>(newValue.constData()), const_cast<QVariant *>(&newValue), &status, &flags };
142  } else {
144  }
145 }
146 
153 {
154 }
155 
165 {
166  setTargetObject(target);
167  setPropertyName(propertyName);
168 }
169 
174 {
175  stop();
176 }
177 
188 {
189  return d_func()->target.data();
190 }
191 
193 {
195  if (d->targetValue == target)
196  return;
197 
198  if (d->state != QAbstractAnimation::Stopped) {
199  qWarning("QPropertyAnimation::setTargetObject: you can't change the target of a running animation");
200  return;
201  }
202 
203  d->target = d->targetValue = target;
204  d->updateMetaProperty();
205 }
206 
218 {
219  Q_D(const QPropertyAnimation);
220  return d->propertyName;
221 }
222 
224 {
226  if (d->state != QAbstractAnimation::Stopped) {
227  qWarning("QPropertyAnimation::setPropertyName: you can't change the property name of a running animation");
228  return;
229  }
230 
231  d->propertyName = propertyName;
232  d->updateMetaProperty();
233 }
234 
235 
240 {
241  return QVariantAnimation::event(event);
242 }
243 
252 {
254  d->updateProperty(value);
255 }
256 
267  QAbstractAnimation::State oldState)
268 {
270 
271  if (!d->target && oldState == Stopped) {
272  qWarning("QPropertyAnimation::updateState (%s): Changing state of an animation without target",
273  d->propertyName.constData());
274  return;
275  }
276 
277  QVariantAnimation::updateState(newState, oldState);
278 
279  QPropertyAnimation *animToStop = 0;
280  {
281 #ifndef QT_NO_THREAD
283 #endif
284  typedef QPair<QObject *, QByteArray> QPropertyAnimationPair;
285  typedef QHash<QPropertyAnimationPair, QPropertyAnimation*> QPropertyAnimationHash;
286  static QPropertyAnimationHash hash;
287  //here we need to use value because we need to know to which pointer
288  //the animation was referring in case stopped because the target was destroyed
289  QPropertyAnimationPair key(d->targetValue, d->propertyName);
290  if (newState == Running) {
291  d->updateMetaProperty();
292  animToStop = hash.value(key, 0);
293  hash.insert(key, this);
294  // update the default start value
295  if (oldState == Stopped) {
296  d->setDefaultStartEndValue(d->targetValue->property(d->propertyName.constData()));
297  //let's check if we have a start value and an end value
298  if (!startValue().isValid() && (d->direction == Backward || !d->defaultStartEndValue.isValid())) {
299  qWarning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without start value",
300  d->propertyName.constData(), d->target.data()->metaObject()->className(),
301  qPrintable(d->target.data()->objectName()));
302  }
303  if (!endValue().isValid() && (d->direction == Forward || !d->defaultStartEndValue.isValid())) {
304  qWarning("QPropertyAnimation::updateState (%s, %s, %s): starting an animation without end value",
305  d->propertyName.constData(), d->target.data()->metaObject()->className(),
306  qPrintable(d->target.data()->objectName()));
307  }
308  }
309  } else if (hash.value(key) == this) {
310  hash.remove(key);
311  }
312  }
313 
314  //we need to do that after the mutex was unlocked
315  if (animToStop) {
316  // try to stop the top level group
317  QAbstractAnimation *current = animToStop;
318  while (current->group() && current->state() != Stopped)
319  current = current->group();
320  current->stop();
321  }
322 }
323 
324 #include "moc_qpropertyanimation.cpp"
325 
327 
328 #endif //QT_NO_ANIMATION
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
static uint hash(const uchar *p, int n)
Definition: qhash.cpp:68
The QPropertyAnimation class animates Qt properties.
bool isWritable() const
Returns true if this property is writable; otherwise returns false.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
State
This enum describes the state of the animation.
bool event(QEvent *event)
Reimplemented Function
static int metacall(QObject *, Call, int, void **)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object&#39;s name property to value.
Definition: qobject.cpp:3755
QVariant endValue() const
static const QMetaObject staticMetaObject
This variable stores the meta-object for the class.
Definition: qobject.h:128
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
State state
state of the animation.
void stop()
Stops the animation.
QAnimationGroup * group() const
If this animation is part of a QAnimationGroup, this function returns a pointer to the group; otherwi...
QAbstractAnimation::State state
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QVariantAnimation class provides an abstract base class for animations.
void updateProperty(const QVariant &)
Q_CORE_EXPORT void qWarning(const char *,...)
QObject * targetObject() const
void setPropertyName(const QByteArray &propertyName)
The QAbstractAnimation class is the base of all animations.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QVariant startValue() const
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
void updateCurrentValue(const QVariant &value)
This virtual function is called by QVariantAnimation whenever the current value changes.
QPropertyAnimation(QObject *parent=0)
Construct a QPropertyAnimation object.
void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
If the startValue is not defined when the state of the animation changes from Stopped to Running...
void setTargetObject(QObject *target)
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
Reimplemented Function
const void * constData() const
Definition: qvariant.cpp:3065
int key
QByteArray propertyName() const
QObject * parent
Definition: qobject.h:92
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
#define qPrintable(string)
Definition: qglobal.h:1750
QWeakPointer< QObject > target
bool event(QEvent *event)
Reimplemented Function
static QMutex * globalInstanceGet(const void *address)
Returns a QMutex from the global mutex pool.
Definition: qmutexpool.cpp:150
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
QMetaProperty property(int index) const
Returns the meta-data for the property with the given index.
~QPropertyAnimation()
Destroys the QPropertyAnimation instance.