Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgraphicsitemanimation.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
47
48#include "qgraphicsitem.h"
49
50#include <QtCore/qtimeline.h>
51#include <QtCore/qpoint.h>
52#include <QtCore/qpointer.h>
53#include <QtCore/qpair.h>
54
55#include <algorithm>
56
58
59static inline bool check_step_valid(qreal step, const char *method)
60{
61 if (!(step >= 0 && step <= 1)) {
62 qWarning("QGraphicsItemAnimation::%s: invalid step = %f", method, step);
63 return false;
64 }
65 return true;
66}
67
69{
70public:
74
76
77 QPointer<QTimeLine> timeLine;
79
82
84
85 struct Pair {
86 bool operator <(const Pair &other) const
87 { return step < other.step; }
88 bool operator==(const Pair &other) const
89 { return step == other.step; }
92 };
93 QList<Pair> xPosition;
94 QList<Pair> yPosition;
95 QList<Pair> rotation;
96 QList<Pair> verticalScale;
97 QList<Pair> horizontalScale;
98 QList<Pair> verticalShear;
99 QList<Pair> horizontalShear;
100 QList<Pair> xTranslation;
101 QList<Pair> yTranslation;
102
103 qreal linearValueForStep(qreal step, const QList<Pair> &source, qreal defaultValue = 0);
104 void insertUniquePair(qreal step, qreal value, QList<Pair> *binList, const char *method);
105};
107
109 qreal defaultValue)
110{
111 if (source.isEmpty())
112 return defaultValue;
113 step = qMin<qreal>(qMax<qreal>(step, 0), 1);
114
115 if (step == 1)
116 return source.back().value;
117
118 qreal stepBefore = 0;
119 qreal stepAfter = 1;
120 qreal valueBefore = source.front().step == 0 ? source.front().value : defaultValue;
121 qreal valueAfter = source.back().value;
122
123 // Find the closest step and value before the given step.
124 for (int i = 0; i < source.size() && step >= source[i].step; ++i) {
125 stepBefore = source[i].step;
126 valueBefore = source[i].value;
127 }
128
129 // Find the closest step and value after the given step.
130 for (int i = source.size() - 1; i >= 0 && step < source[i].step; --i) {
131 stepAfter = source[i].step;
132 valueAfter = source[i].value;
133 }
134
135 // Do a simple linear interpolation.
136 return valueBefore + (valueAfter - valueBefore) * ((step - stepBefore) / (stepAfter - stepBefore));
137}
138
140 const char *method)
141{
143 return;
144
145 const Pair pair = { step, value };
146
147 const QList<Pair>::iterator result = std::lower_bound(binList->begin(), binList->end(), pair);
148 if (result == binList->end() || pair < *result)
149 binList->insert(result, pair);
150 else
151 result->value = value;
152}
153
162
170
177{
178 return d->item;
179}
180
187{
188 d->item = item;
189 d->startPos = d->item->pos();
190}
191
199{
200 return d->timeLine;
201}
202
210{
211 if (d->timeLine == timeLine)
212 return;
213 if (d->timeLine)
214 delete d->timeLine;
215 if (!timeLine)
216 return;
217 d->timeLine = timeLine;
218 connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(setStep(qreal)));
219}
220
227{
228 check_step_valid(step, "posAt");
229 return QPointF(d->linearValueForStep(step, d->xPosition, d->startPos.x()),
230 d->linearValueForStep(step, d->yPosition, d->startPos.y()));
231}
232
241{
242 d->insertUniquePair(step, pos.x(), &d->xPosition, "setPosAt");
243 d->insertUniquePair(step, pos.y(), &d->yPosition, "setPosAt");
244}
245
251QList<QPair<qreal, QPointF> > QGraphicsItemAnimation::posList() const
252{
253 QList<QPair<qreal, QPointF> > list;
254 const int xPosCount = d->xPosition.size();
255 list.reserve(xPosCount);
256 for (int i = 0; i < xPosCount; ++i)
257 list << QPair<qreal, QPointF>(d->xPosition.at(i).step, QPointF(d->xPosition.at(i).value, d->yPosition.at(i).value));
258
259 return list;
260}
261
268{
269 check_step_valid(step, "transformAt");
270
272 if (!d->rotation.isEmpty())
274 if (!d->verticalScale.isEmpty())
276 if (!d->verticalShear.isEmpty())
278 if (!d->xTranslation.isEmpty())
280 return transform;
281}
282
289{
290 check_step_valid(step, "rotationAt");
291 return d->linearValueForStep(step, d->rotation);
292}
293
300{
301 d->insertUniquePair(step, angle, &d->rotation, "setRotationAt");
302}
303
309QList<QPair<qreal, qreal> > QGraphicsItemAnimation::rotationList() const
310{
311 QList<QPair<qreal, qreal> > list;
312 const int numRotations = d->rotation.size();
313 list.reserve(numRotations);
314 for (int i = 0; i < numRotations; ++i)
315 list << QPair<qreal, qreal>(d->rotation.at(i).step, d->rotation.at(i).value);
316
317 return list;
318}
319
326{
327 check_step_valid(step, "xTranslationAt");
328 return d->linearValueForStep(step, d->xTranslation);
329}
330
337{
338 check_step_valid(step, "yTranslationAt");
339 return d->linearValueForStep(step, d->yTranslation);
340}
341
349{
350 d->insertUniquePair(step, dx, &d->xTranslation, "setTranslationAt");
351 d->insertUniquePair(step, dy, &d->yTranslation, "setTranslationAt");
352}
353
359QList<QPair<qreal, QPointF> > QGraphicsItemAnimation::translationList() const
360{
361 QList<QPair<qreal, QPointF> > list;
362 const int numTranslations = d->xTranslation.size();
363 list.reserve(numTranslations);
364 for (int i = 0; i < numTranslations; ++i)
365 list << QPair<qreal, QPointF>(d->xTranslation.at(i).step, QPointF(d->xTranslation.at(i).value, d->yTranslation.at(i).value));
366
367 return list;
368}
369
376{
377 check_step_valid(step, "verticalScaleAt");
378
379 return d->linearValueForStep(step, d->verticalScale, 1);
380}
381
388{
389 check_step_valid(step, "horizontalScaleAt");
390 return d->linearValueForStep(step, d->horizontalScale, 1);
391}
392
400{
401 d->insertUniquePair(step, sx, &d->horizontalScale, "setScaleAt");
402 d->insertUniquePair(step, sy, &d->verticalScale, "setScaleAt");
403}
404
410QList<QPair<qreal, QPointF> > QGraphicsItemAnimation::scaleList() const
411{
412 QList<QPair<qreal, QPointF> > list;
413 const int numScales = d->horizontalScale.size();
414 list.reserve(numScales);
415 for (int i = 0; i < numScales; ++i)
416 list << QPair<qreal, QPointF>(d->horizontalScale.at(i).step, QPointF(d->horizontalScale.at(i).value, d->verticalScale.at(i).value));
417
418 return list;
419}
420
427{
428 check_step_valid(step, "verticalShearAt");
429 return d->linearValueForStep(step, d->verticalShear, 0);
430}
431
438{
439 check_step_valid(step, "horizontalShearAt");
440 return d->linearValueForStep(step, d->horizontalShear, 0);
441}
442
450{
451 d->insertUniquePair(step, sh, &d->horizontalShear, "setShearAt");
452 d->insertUniquePair(step, sv, &d->verticalShear, "setShearAt");
453}
454
460QList<QPair<qreal, QPointF> > QGraphicsItemAnimation::shearList() const
461{
462 QList<QPair<qreal, QPointF> > list;
463 const int numShears = d->horizontalShear.size();
464 list.reserve(numShears);
465 for (int i = 0; i < numShears; ++i)
466 list << QPair<qreal, QPointF>(d->horizontalShear.at(i).step, QPointF(d->horizontalShear.at(i).value, d->verticalShear.at(i).value));
467
468 return list;
469}
470
476{
477 d->xPosition.clear();
478 d->yPosition.clear();
479 d->rotation.clear();
480 d->verticalScale.clear();
481 d->horizontalScale.clear();
482 d->verticalShear.clear();
483 d->horizontalShear.clear();
484 d->xTranslation.clear();
485 d->yTranslation.clear();
486}
487
495{
496 if (!check_step_valid(step, "setStep"))
497 return;
498
500
501 d->step = step;
502 if (d->item) {
503 if (!d->xPosition.isEmpty() || !d->yPosition.isEmpty())
504 d->item->setPos(posAt(step));
505 if (!d->rotation.isEmpty()
506 || !d->verticalScale.isEmpty()
507 || !d->horizontalScale.isEmpty()
508 || !d->verticalShear.isEmpty()
509 || !d->horizontalShear.isEmpty()
510 || !d->xTranslation.isEmpty()
511 || !d->yTranslation.isEmpty()) {
513 }
514 }
515
516 afterAnimationStep(step);
517}
518
531
544
546
547#include "moc_qgraphicsitemanimation.cpp"
void insertUniquePair(qreal step, qreal value, QList< Pair > *binList, const char *method)
qreal linearValueForStep(qreal step, const QList< Pair > &source, qreal defaultValue=0)
The QGraphicsItemAnimation class provides simple animation support for QGraphicsItem.
qreal horizontalScaleAt(qreal step) const
Returns the horizontal scale for the item at the specified step value.
QList< QPair< qreal, QPointF > > shearList() const
Returns all explicitly inserted shears.
void setTimeLine(QTimeLine *timeLine)
Sets the timeline object used to control the rate of animation to the timeLine specified.
QTransform transformAt(qreal step) const
Returns the transform used for the item at the specified step value.
QGraphicsItem * item() const
Returns the item on which the animation object operates.
qreal horizontalShearAt(qreal step) const
Returns the horizontal shear for the item at the specified step value.
void setRotationAt(qreal step, qreal angle)
Sets the rotation of the item at the given step value to the angle specified.
QList< QPair< qreal, QPointF > > translationList() const
Returns all explicitly inserted translations.
qreal xTranslationAt(qreal step) const
Returns the horizontal translation of the item at the specified step value.
void setStep(qreal x)
Sets the current step value for the animation, causing the transformations scheduled at this step to ...
qreal rotationAt(qreal step) const
Returns the angle at which the item is rotated at the specified step value.
virtual ~QGraphicsItemAnimation()
Destroys the animation object.
virtual void afterAnimationStep(qreal step)
This method is meant to be overridden in subclasses that need to execute additional code after a new ...
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...
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...
void setItem(QGraphicsItem *item)
Sets the specified item to be used in the animation.
QList< QPair< qreal, QPointF > > scaleList() const
Returns all explicitly inserted scales.
QTimeLine * timeLine() const
Returns the timeline object used to control the rate at which the animation occurs.
void clear()
Clears the scheduled transformations used for the animation, but retains the item and timeline.
qreal verticalScaleAt(qreal step) const
Returns the vertical scale for the item at the specified step value.
qreal yTranslationAt(qreal step) const
Returns the vertical translation of the item at the specified step value.
virtual void beforeAnimationStep(qreal step)
This method is meant to be overridden by subclassed that needs to execute additional code before a ne...
QList< QPair< qreal, qreal > > rotationList() const
Returns all explicitly inserted rotations.
qreal verticalShearAt(qreal step) const
Returns the vertical shear for the item at the specified step value.
QList< QPair< qreal, QPointF > > posList() const
Returns all explicitly inserted positions.
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...
void setPosAt(qreal step, const QPointF &pos)
Sets the position of the item at the given step value to the point specified.
QGraphicsItemAnimation(QObject *parent=nullptr)
Constructs an animation object with the given parent.
QPointF posAt(qreal step) const
Returns the position of the item at the given step value.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
void setTransform(const QTransform &matrix, bool combine=false)
QPointF pos() const
Returns the position of the item in parent coordinates.
void setPos(const QPointF &pos)
Sets the position of the item to pos, which is in parent coordinates.
void reserve(qsizetype size)
Definition qlist.h:753
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
\inmodule QtCore
Definition qtimeline.h:19
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
QTransform & rotate(qreal a, Qt::Axis axis=Qt::ZAxis, qreal distanceToPlane=1024.0f)
QTransform & scale(qreal sx, qreal sy)
Scales the coordinate system by sx horizontally and sy vertically, and returns a reference to the mat...
QTransform & shear(qreal sh, qreal sv)
Shears the coordinate system by sh horizontally and sv vertically, and returns a reference to the mat...
QTransform & 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...
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char * method
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
static QT_BEGIN_NAMESPACE bool check_step_valid(qreal step, const char *method)
#define qWarning
Definition qlogging.h:166
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLfloat angle
GLsizei GLsizei GLchar * source
GLuint GLenum GLenum transform
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
#define Q_UNUSED(x)
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
double qreal
Definition qtypes.h:187
QList< int > list
[14]
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
QTimeLine * timeLine
QGraphicsItem * item
bool operator==(const Pair &other) const
bool operator<(const Pair &other) const