Qt 4.8
qgraphicsitemanimation.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 QtGui 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 
86 #include "qgraphicsitemanimation.h"
87 
88 #ifndef QT_NO_GRAPHICSVIEW
89 
90 #include "qgraphicsitem.h"
91 
92 #include <QtCore/qtimeline.h>
93 #include <QtCore/qpoint.h>
94 #include <QtCore/qpointer.h>
95 #include <QtCore/qpair.h>
96 #include <QtGui/qmatrix.h>
97 
99 
101 {
102 public:
104  : q(0), timeLine(0), item(0), step(0)
105  { }
106 
108 
111 
114 
116 
117  struct Pair {
118  Pair(qreal a, qreal b) : step(a), value(b) {}
119  bool operator <(const Pair &other) const
120  { return step < other.step; }
121  bool operator==(const Pair &other) const
122  { return step == other.step; }
125  };
135 
136  qreal linearValueForStep(qreal step, QList<Pair> *source, qreal defaultValue = 0);
137  void insertUniquePair(qreal step, qreal value, QList<Pair> *binList, const char* method);
138 };
139 
141 {
142  if (source->isEmpty())
143  return defaultValue;
144  step = qMin<qreal>(qMax<qreal>(step, 0), 1);
145 
146  if (step == 1)
147  return source->last().value;
148 
149  qreal stepBefore = 0;
150  qreal stepAfter = 1;
151  qreal valueBefore = source->first().step == 0 ? source->first().value : defaultValue;
152  qreal valueAfter = source->last().value;
153 
154  // Find the closest step and value before the given step.
155  for (int i = 0; i < source->size() && step >= source->at(i).step; ++i) {
156  stepBefore = source->at(i).step;
157  valueBefore = source->at(i).value;
158  }
159 
160  // Find the closest step and value after the given step.
161  for (int j = source->size() - 1; j >= 0 && step < source->at(j).step; --j) {
162  stepAfter = source->at(j).step;
163  valueAfter = source->at(j).value;
164  }
165 
166  // Do a simple linear interpolation.
167  return valueBefore + (valueAfter - valueBefore) * ((step - stepBefore) / (stepAfter - stepBefore));
168 }
169 
171 {
172  if (step < 0.0 || step > 1.0) {
173  qWarning("QGraphicsItemAnimation::%s: invalid step = %f", method, step);
174  return;
175  }
176 
177  Pair pair(step, value);
178 
179  QList<Pair>::iterator result = qBinaryFind(binList->begin(), binList->end(), pair);
180  if (result != binList->end())
181  result->value = value;
182  else {
183  *binList << pair;
184  qSort(binList->begin(), binList->end());
185  }
186 }
187 
192  : QObject(parent), d(new QGraphicsItemAnimationPrivate)
193 {
194  d->q = this;
195 }
196 
201 {
202  delete d;
203 }
204 
211 {
212  return d->item;
213 }
214 
221 {
222  d->item = item;
223  d->startPos = d->item->pos();
224 }
225 
233 {
234  return d->timeLine;
235 }
236 
244 {
245  if (d->timeLine == timeLine)
246  return;
247  if (d->timeLine)
248  delete d->timeLine;
249  if (!timeLine)
250  return;
251  d->timeLine = timeLine;
252  connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(setStep(qreal)));
253 }
254 
261 {
262  if (step < 0.0 || step > 1.0)
263  qWarning("QGraphicsItemAnimation::posAt: invalid step = %f", step);
264 
265  return QPointF(d->linearValueForStep(step, &d->xPosition, d->startPos.x()),
266  d->linearValueForStep(step, &d->yPosition, d->startPos.y()));
267 }
268 
280 {
281  d->insertUniquePair(step, pos.x(), &d->xPosition, "setPosAt");
282  d->insertUniquePair(step, pos.y(), &d->yPosition, "setPosAt");
283 }
284 
291 {
293  for (int i = 0; i < d->xPosition.size(); ++i)
294  list << QPair<qreal, QPointF>(d->xPosition.at(i).step, QPointF(d->xPosition.at(i).value, d->yPosition.at(i).value));
295 
296  return list;
297 }
298 
303 {
304  if (step < 0.0 || step > 1.0)
305  qWarning("QGraphicsItemAnimation::matrixAt: invalid step = %f", step);
306 
307  QMatrix matrix;
308  if (!d->rotation.isEmpty())
309  matrix.rotate(rotationAt(step));
310  if (!d->verticalScale.isEmpty())
311  matrix.scale(horizontalScaleAt(step), verticalScaleAt(step));
312  if (!d->verticalShear.isEmpty())
313  matrix.shear(horizontalShearAt(step), verticalShearAt(step));
314  if (!d->xTranslation.isEmpty())
315  matrix.translate(xTranslationAt(step), yTranslationAt(step));
316  return matrix;
317 }
318 
325 {
326  if (step < 0.0 || step > 1.0)
327  qWarning("QGraphicsItemAnimation::rotationAt: invalid step = %f", step);
328 
329  return d->linearValueForStep(step, &d->rotation);
330 }
331 
338 {
339  d->insertUniquePair(step, angle, &d->rotation, "setRotationAt");
340 }
341 
348 {
350  for (int i = 0; i < d->rotation.size(); ++i)
351  list << QPair<qreal, qreal>(d->rotation.at(i).step, d->rotation.at(i).value);
352 
353  return list;
354 }
355 
362 {
363  if (step < 0.0 || step > 1.0)
364  qWarning("QGraphicsItemAnimation::xTranslationAt: invalid step = %f", step);
365 
366  return d->linearValueForStep(step, &d->xTranslation);
367 }
368 
375 {
376  if (step < 0.0 || step > 1.0)
377  qWarning("QGraphicsItemAnimation::yTranslationAt: invalid step = %f", step);
378 
379  return d->linearValueForStep(step, &d->yTranslation);
380 }
381 
389 {
390  d->insertUniquePair(step, dx, &d->xTranslation, "setTranslationAt");
391  d->insertUniquePair(step, dy, &d->yTranslation, "setTranslationAt");
392 }
393 
400 {
402  for (int i = 0; i < d->xTranslation.size(); ++i)
403  list << QPair<qreal, QPointF>(d->xTranslation.at(i).step, QPointF(d->xTranslation.at(i).value, d->yTranslation.at(i).value));
404 
405  return list;
406 }
407 
414 {
415  if (step < 0.0 || step > 1.0)
416  qWarning("QGraphicsItemAnimation::verticalScaleAt: invalid step = %f", step);
417 
418  return d->linearValueForStep(step, &d->verticalScale, 1);
419 }
420 
427 {
428  if (step < 0.0 || step > 1.0)
429  qWarning("QGraphicsItemAnimation::horizontalScaleAt: invalid step = %f", step);
430 
431  return d->linearValueForStep(step, &d->horizontalScale, 1);
432 }
433 
441 {
442  d->insertUniquePair(step, sx, &d->horizontalScale, "setScaleAt");
443  d->insertUniquePair(step, sy, &d->verticalScale, "setScaleAt");
444 }
445 
452 {
454  for (int i = 0; i < d->horizontalScale.size(); ++i)
455  list << QPair<qreal, QPointF>(d->horizontalScale.at(i).step, QPointF(d->horizontalScale.at(i).value, d->verticalScale.at(i).value));
456 
457  return list;
458 }
459 
466 {
467  if (step < 0.0 || step > 1.0)
468  qWarning("QGraphicsItemAnimation::verticalShearAt: invalid step = %f", step);
469 
470  return d->linearValueForStep(step, &d->verticalShear, 0);
471 }
472 
479 {
480  if (step < 0.0 || step > 1.0)
481  qWarning("QGraphicsItemAnimation::horizontalShearAt: invalid step = %f", step);
482 
483  return d->linearValueForStep(step, &d->horizontalShear, 0);
484 }
485 
493 {
494  d->insertUniquePair(step, sh, &d->horizontalShear, "setShearAt");
495  d->insertUniquePair(step, sv, &d->verticalShear, "setShearAt");
496 }
497 
504 {
506  for (int i = 0; i < d->horizontalShear.size(); ++i)
507  list << QPair<qreal, QPointF>(d->horizontalShear.at(i).step, QPointF(d->horizontalShear.at(i).value, d->verticalShear.at(i).value));
508 
509  return list;
510 }
511 
517 {
518  d->xPosition.clear();
519  d->yPosition.clear();
520  d->rotation.clear();
521  d->verticalScale.clear();
522  d->horizontalScale.clear();
523  d->verticalShear.clear();
524  d->horizontalShear.clear();
525  d->xTranslation.clear();
526  d->yTranslation.clear();
527 }
528 
539 {
540  if (x < 0.0 || x > 1.0) {
541  qWarning("QGraphicsItemAnimation::setStep: invalid step = %f", x);
542  return;
543  }
544 
546 
547  d->step = x;
548  if (d->item) {
549  if (!d->xPosition.isEmpty() || !d->yPosition.isEmpty())
550  d->item->setPos(posAt(x));
551  if (!d->rotation.isEmpty()
552  || !d->verticalScale.isEmpty()
553  || !d->horizontalScale.isEmpty()
554  || !d->verticalShear.isEmpty()
555  || !d->horizontalShear.isEmpty()
556  || !d->xTranslation.isEmpty()
557  || !d->yTranslation.isEmpty()) {
559  }
560  }
561 
563 }
564 
573 {
574  if (!d->item)
575  return;
576  d->startPos = d->item->pos();
577  d->startMatrix = d->item->matrix();
578 }
579 
592 {
593  Q_UNUSED(step);
594 }
595 
608 {
609  Q_UNUSED(step);
610 }
611 
613 
614 #endif // QT_NO_GRAPHICSVIEW
void insertUniquePair(qreal step, qreal value, QList< Pair > *binList, const char *method)
double d
Definition: qnumeric_p.h:62
QList< QPair< qreal, QPointF > > shearList() const
Returns all explicitly inserted shears.
void setShearAt(qreal step, qreal sh, qreal sv)
Sets the shear of the item at the given step value using the horizontal and vertical shear factors sp...
double qreal
Definition: qglobal.h:1193
void reset()
Resets the item to its starting position and transformation.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void clear()
Clears the scheduled transformations used for the animation, but retains the item and timeline...
QList< QPair< qreal, qreal > > rotationList() const
Returns all explicitly inserted rotations.
void setPosAt(qreal step, const QPointF &pos)
Sets the position of the item at the given step value to the point specified.
void setTimeLine(QTimeLine *timeLine)
Sets the timeline object used to control the rate of animation to the timeLine specified.
qreal verticalScaleAt(qreal step) const
Returns the vertical scale for the item at the specified step value.
The QMatrix class specifies 2D transformations of a coordinate system.
Definition: qmatrix.h:61
void setMatrix(const QMatrix &matrix, bool combine=false)
Sets the item&#39;s affine transformation matrix.
void setTranslationAt(qreal step, qreal dx, qreal dy)
Sets the translation of the item at the given step value using the horizontal and vertical coordinate...
#define SLOT(a)
Definition: qobjectdefs.h:226
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qBinaryFind(RandomAccessIterator begin, RandomAccessIterator end, const T &value)
Definition: qalgorithms.h:295
QPointF pos() const
Returns the position of the item in parent coordinates.
long ASN1_INTEGER_get ASN1_INTEGER * a
void setScaleAt(qreal step, qreal sx, qreal sy)
Sets the scale of the item at the given step value using the horizontal and vertical scale factors sp...
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void setPos(const QPointF &pos)
Sets the position of the item to pos, which is in parent coordinates.
QMatrix & translate(qreal dx, qreal dy)
Moves the coordinate system dx along the x axis and dy along the y axis, and returns a reference to t...
Definition: qmatrix.cpp:922
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
qreal xTranslationAt(qreal step) const
Returns the horizontal translation of the item at the specified step value.
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
The QTimeLine class provides a timeline for controlling animations.
Definition: qtimeline.h:55
#define SIGNAL(a)
Definition: qobjectdefs.h:227
QMatrix matrix() const
Returns the item&#39;s affine transformation matrix.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QGraphicsItemAnimation class provides simple animation support for QGraphicsItem.
QMatrix & rotate(qreal a)
Rotates the coordinate system the given degrees counterclockwise.
Definition: qmatrix.cpp:990
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QMatrix matrixAt(qreal step) const
Returns the matrix used to transform the item at the specified step value.
QGraphicsItem * item() const
Returns the item on which the animation object operates.
Q_CORE_EXPORT void qWarning(const char *,...)
void setRotationAt(qreal step, qreal angle)
Sets the rotation of the item at the given step value to the angle specified.
QMatrix & shear(qreal sh, qreal sv)
Shears the coordinate system by sh horizontally and sv vertically, and returns a reference to the mat...
Definition: qmatrix.cpp:957
QTimeLine * timeLine() const
Returns the timeline object used to control the rate at which the animation occurs.
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
QList< QPair< qreal, QPointF > > posList() const
Returns all explicitly inserted positions.
void setStep(qreal x)
Sets the current step value for the animation, causing the transformations scheduled at this step to ...
The QList::iterator class provides an STL-style non-const iterator for QList and QQueue.
Definition: qlist.h:181
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
qreal horizontalScaleAt(qreal step) const
Returns the horizontal scale for the item at the specified step value.
qreal verticalShearAt(qreal step) const
Returns the vertical shear for the item at the specified step value.
qreal angle(const QPointF &p1, const QPointF &p2)
virtual void beforeAnimationStep(qreal step)
This method is meant to be overridden by subclassed that needs to execute additional code before a ne...
qreal linearValueForStep(qreal step, QList< Pair > *source, qreal defaultValue=0)
qreal horizontalShearAt(qreal step) const
Returns the horizontal shear for the item at the specified step value.
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QGraphicsItemAnimation(QObject *parent=0)
Constructs an animation object with the given parent.
QGraphicsItemAnimationPrivate * d
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
QList< QPair< qreal, QPointF > > scaleList() const
Returns all explicitly inserted scales.
bool operator<(const Pair &other) const
QList< QPair< qreal, QPointF > > translationList() const
Returns all explicitly inserted translations.
bool operator==(const Pair &other) const
QMatrix & scale(qreal sx, qreal sy)
Scales the coordinate system by sx horizontally and sy vertically, and returns a reference to the mat...
Definition: qmatrix.cpp:941
QPointF posAt(qreal step) const
Returns the position of the item at the given step value.
qreal rotationAt(qreal step) const
Returns the angle at which the item is rotated at the specified step value.
virtual void afterAnimationStep(qreal step)
This method is meant to be overridden in subclasses that need to execute additional code after a new ...
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
virtual ~QGraphicsItemAnimation()
Destroys the animation object.
void setItem(QGraphicsItem *item)
Sets the specified item to be used in the animation.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
qreal yTranslationAt(qreal step) const
Returns the vertical translation of the item at the specified step value.