qgraphicstransform.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/widgets/graphicsview/qgraphicstransform.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtDeclarative 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 The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://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 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40/*!-
41 \class QGraphicsTransform-
42 \brief The QGraphicsTransform class is an abstract base class for building-
43 advanced transformations on QGraphicsItems.-
44 \since 4.6-
45 \ingroup graphicsview-api-
46 \inmodule QtWidgets-
47-
48 As an alternative to QGraphicsItem::transform, QGraphicsTransform lets you-
49 create and control advanced transformations that can be configured-
50 independently using specialized properties.-
51-
52 QGraphicsItem allows you to assign any number of QGraphicsTransform-
53 instances to one QGraphicsItem. Each QGraphicsTransform is applied in-
54 order, one at a time, to the QGraphicsItem it's assigned to.-
55-
56 QGraphicsTransform is particularly useful for animations. Whereas-
57 QGraphicsItem::setTransform() lets you assign any transform directly to an-
58 item, there is no direct way to interpolate between two different-
59 transformations (e.g., when transitioning between two states, each for-
60 which the item has a different arbitrary transform assigned). Using-
61 QGraphicsTransform you can interpolate the property values of each-
62 independent transformation. The resulting operation is then combined into a-
63 single transform which is applied to QGraphicsItem.-
64-
65 Transformations are computed in true 3D space using QMatrix4x4.-
66 When the transformation is applied to a QGraphicsItem, it will be-
67 projected back to a 2D QTransform. When multiple QGraphicsTransform-
68 objects are applied to a QGraphicsItem, all of the transformations-
69 are computed in true 3D space, with the projection back to 2D-
70 only occurring after the last QGraphicsTransform is applied.-
71 The exception to this is QGraphicsRotation, which projects back to-
72 2D after each rotation to preserve the perspective effect around-
73 the X and Y axes.-
74-
75 If you want to create your own configurable transformation, you can create-
76 a subclass of QGraphicsTransform (or any or the existing subclasses), and-
77 reimplement the pure virtual applyTo() function, which takes a pointer to a-
78 QMatrix4x4. Each operation you would like to apply should be exposed as-
79 properties (e.g., customTransform->setVerticalShear(2.5)). Inside you-
80 reimplementation of applyTo(), you can modify the provided transform-
81 respectively.-
82-
83 QGraphicsTransform can be used together with QGraphicsItem::setTransform(),-
84 QGraphicsItem::setRotation(), and QGraphicsItem::setScale().-
85-
86 \sa QGraphicsItem::transform(), QGraphicsScale, QGraphicsRotation-
87*/-
88-
89#include "qgraphicstransform.h"-
90#include "qgraphicsitem_p.h"-
91#include "qgraphicstransform_p.h"-
92#include <QDebug>-
93#include <QtCore/qmath.h>-
94#include <QtCore/qnumeric.h>-
95-
96#ifndef QT_NO_GRAPHICSVIEW-
97QT_BEGIN_NAMESPACE-
98-
99QGraphicsTransformPrivate::~QGraphicsTransformPrivate()-
100{-
101}-
102-
103void QGraphicsTransformPrivate::setItem(QGraphicsItem *i)-
104{-
105 if (item == i)
item == iDescription
TRUEnever evaluated
FALSEnever evaluated
0
106 return;
never executed: return;
0
107-
108 if (item) {
itemDescription
TRUEnever evaluated
FALSEnever evaluated
0
109 Q_Q(QGraphicsTransform);-
110 QGraphicsItemPrivate *d_ptr = item->d_ptr.data();-
111-
112 item->prepareGeometryChange();-
113 Q_ASSERT(d_ptr->transformData);-
114 d_ptr->transformData->graphicsTransforms.removeAll(q);-
115 d_ptr->dirtySceneTransform = 1;-
116 item = 0;-
117 }
never executed: end of block
0
118-
119 item = i;-
120}
never executed: end of block
0
121-
122void QGraphicsTransformPrivate::updateItem(QGraphicsItem *item)-
123{-
124 item->prepareGeometryChange();-
125 item->d_ptr->dirtySceneTransform = 1;-
126}
never executed: end of block
0
127-
128/*!-
129 Constructs a new QGraphicsTransform with the given \a parent.-
130*/-
131QGraphicsTransform::QGraphicsTransform(QObject *parent)-
132 : QObject(*new QGraphicsTransformPrivate, parent)-
133{-
134}
never executed: end of block
0
135-
136/*!-
137 Destroys the graphics transform.-
138*/-
139QGraphicsTransform::~QGraphicsTransform()-
140{-
141 Q_D(QGraphicsTransform);-
142 d->setItem(0);-
143}
never executed: end of block
0
144-
145/*!-
146 \internal-
147*/-
148QGraphicsTransform::QGraphicsTransform(QGraphicsTransformPrivate &p, QObject *parent)-
149 : QObject(p, parent)-
150{-
151}
never executed: end of block
0
152-
153/*!-
154 \fn void QGraphicsTransform::applyTo(QMatrix4x4 *matrix) const-
155-
156 This pure virtual method has to be reimplemented in derived classes.-
157-
158 It applies this transformation to \a matrix.-
159-
160 \sa QGraphicsItem::transform(), QMatrix4x4::toTransform()-
161*/-
162-
163/*!-
164 Notifies that this transform operation has changed its parameters in such a-
165 way that applyTo() will return a different result than before.-
166-
167 When implementing you own custom graphics transform, you must call this-
168 function every time you change a parameter, to let QGraphicsItem know that-
169 its transformation needs to be updated.-
170-
171 \sa applyTo()-
172*/-
173void QGraphicsTransform::update()-
174{-
175 Q_D(QGraphicsTransform);-
176 if (d->item)
d->itemDescription
TRUEnever evaluated
FALSEnever evaluated
0
177 d->updateItem(d->item);
never executed: d->updateItem(d->item);
0
178}
never executed: end of block
0
179-
180/*!-
181 \class QGraphicsScale-
182 \brief The QGraphicsScale class provides a scale transformation.-
183 \since 4.6-
184 \inmodule QtWidgets-
185-
186 QGraphicsScene provides certain parameters to help control how the scale-
187 should be applied.-
188-
189 The origin is the point that the item is scaled from (i.e., it stays fixed-
190 relative to the parent as the rest of the item grows). By default the-
191 origin is QPointF(0, 0).-
192-
193 The parameters xScale, yScale, and zScale describe the scale factors to-
194 apply in horizontal, vertical, and depth directions. They can take on any-
195 value, including 0 (to collapse the item to a point) or negative value.-
196 A negative xScale value will mirror the item horizontally. A negative yScale-
197 value will flip the item vertically. A negative zScale will flip the-
198 item end for end.-
199-
200 \sa QGraphicsTransform, QGraphicsItem::setScale(), QTransform::scale()-
201*/-
202-
203class QGraphicsScalePrivate : public QGraphicsTransformPrivate-
204{-
205public:-
206 QGraphicsScalePrivate()-
207 : xScale(1), yScale(1), zScale(1) {}
never executed: end of block
0
208 QVector3D origin;-
209 qreal xScale;-
210 qreal yScale;-
211 qreal zScale;-
212};-
213-
214/*!-
215 Constructs an empty QGraphicsScale object with the given \a parent.-
216*/-
217QGraphicsScale::QGraphicsScale(QObject *parent)-
218 : QGraphicsTransform(*new QGraphicsScalePrivate, parent)-
219{-
220}
never executed: end of block
0
221-
222/*!-
223 Destroys the graphics scale.-
224*/-
225QGraphicsScale::~QGraphicsScale()-
226{-
227}-
228-
229/*!-
230 \property QGraphicsScale::origin-
231 \brief the origin of the scale in 3D space.-
232-
233 All scaling will be done relative to this point (i.e., this point-
234 will stay fixed, relative to the parent, when the item is scaled).-
235-
236 \sa xScale, yScale, zScale-
237*/-
238QVector3D QGraphicsScale::origin() const-
239{-
240 Q_D(const QGraphicsScale);-
241 return d->origin;
never executed: return d->origin;
0
242}-
243void QGraphicsScale::setOrigin(const QVector3D &point)-
244{-
245 Q_D(QGraphicsScale);-
246 if (d->origin == point)
d->origin == pointDescription
TRUEnever evaluated
FALSEnever evaluated
0
247 return;
never executed: return;
0
248 d->origin = point;-
249 update();-
250 emit originChanged();-
251}
never executed: end of block
0
252-
253/*!-
254 \property QGraphicsScale::xScale-
255 \brief the horizontal scale factor.-
256-
257 The scale factor can be any real number; the default value is 1.0. If you-
258 set the factor to 0.0, the item will be collapsed to a single point. If you-
259 provide a negative value, the item will be mirrored horizontally around its-
260 origin.-
261-
262 \sa yScale, zScale, origin-
263*/-
264qreal QGraphicsScale::xScale() const-
265{-
266 Q_D(const QGraphicsScale);-
267 return d->xScale;
never executed: return d->xScale;
0
268}-
269void QGraphicsScale::setXScale(qreal scale)-
270{-
271 Q_D(QGraphicsScale);-
272 if (d->xScale == scale)
d->xScale == scaleDescription
TRUEnever evaluated
FALSEnever evaluated
0
273 return;
never executed: return;
0
274 d->xScale = scale;-
275 update();-
276 emit xScaleChanged();-
277 emit scaleChanged();-
278}
never executed: end of block
0
279-
280/*!-
281 \property QGraphicsScale::yScale-
282 \brief the vertical scale factor.-
283-
284 The scale factor can be any real number; the default value is 1.0. If you-
285 set the factor to 0.0, the item will be collapsed to a single point. If you-
286 provide a negative value, the item will be flipped vertically around its-
287 origin.-
288-
289 \sa xScale, zScale, origin-
290*/-
291qreal QGraphicsScale::yScale() const-
292{-
293 Q_D(const QGraphicsScale);-
294 return d->yScale;
never executed: return d->yScale;
0
295}-
296void QGraphicsScale::setYScale(qreal scale)-
297{-
298 Q_D(QGraphicsScale);-
299 if (d->yScale == scale)
d->yScale == scaleDescription
TRUEnever evaluated
FALSEnever evaluated
0
300 return;
never executed: return;
0
301 d->yScale = scale;-
302 update();-
303 emit yScaleChanged();-
304 emit scaleChanged();-
305}
never executed: end of block
0
306-
307/*!-
308 \property QGraphicsScale::zScale-
309 \brief the depth scale factor.-
310-
311 The scale factor can be any real number; the default value is 1.0. If you-
312 set the factor to 0.0, the item will be collapsed to a single point. If you-
313 provide a negative value, the item will be flipped end for end around its-
314 origin.-
315-
316 \sa xScale, yScale, origin-
317*/-
318qreal QGraphicsScale::zScale() const-
319{-
320 Q_D(const QGraphicsScale);-
321 return d->zScale;
never executed: return d->zScale;
0
322}-
323void QGraphicsScale::setZScale(qreal scale)-
324{-
325 Q_D(QGraphicsScale);-
326 if (d->zScale == scale)
d->zScale == scaleDescription
TRUEnever evaluated
FALSEnever evaluated
0
327 return;
never executed: return;
0
328 d->zScale = scale;-
329 update();-
330 emit zScaleChanged();-
331 emit scaleChanged();-
332}
never executed: end of block
0
333-
334/*!-
335 \reimp-
336*/-
337void QGraphicsScale::applyTo(QMatrix4x4 *matrix) const-
338{-
339 Q_D(const QGraphicsScale);-
340 matrix->translate(d->origin);-
341 matrix->scale(d->xScale, d->yScale, d->zScale);-
342 matrix->translate(-d->origin);-
343}
never executed: end of block
0
344-
345/*!-
346 \fn QGraphicsScale::originChanged()-
347-
348 QGraphicsScale emits this signal when its origin changes.-
349-
350 \sa QGraphicsScale::origin-
351*/-
352-
353/*!-
354 \fn QGraphicsScale::xScaleChanged()-
355 \since 4.7-
356-
357 This signal is emitted whenever the \l xScale property changes.-
358*/-
359-
360/*!-
361 \fn QGraphicsScale::yScaleChanged()-
362 \since 4.7-
363-
364 This signal is emitted whenever the \l yScale property changes.-
365*/-
366-
367/*!-
368 \fn QGraphicsScale::zScaleChanged()-
369 \since 4.7-
370-
371 This signal is emitted whenever the \l zScale property changes.-
372*/-
373-
374/*!-
375 \fn QGraphicsScale::scaleChanged()-
376-
377 This signal is emitted whenever the xScale, yScale, or zScale-
378 of the object changes.-
379-
380 \sa QGraphicsScale::xScale, QGraphicsScale::yScale-
381 \sa QGraphicsScale::zScale-
382*/-
383-
384/*!-
385 \class QGraphicsRotation-
386 \brief The QGraphicsRotation class provides a rotation transformation around-
387 a given axis.-
388 \since 4.6-
389 \inmodule QtWidgets-
390-
391 You can provide the desired axis by assigning a QVector3D to the axis property-
392 or by passing a member if Qt::Axis to the setAxis convenience function.-
393 By default the axis is (0, 0, 1) i.e., rotation around the Z axis.-
394-
395 The angle property, which is provided by QGraphicsRotation, now-
396 describes the number of degrees to rotate around this axis.-
397-
398 QGraphicsRotation provides certain parameters to help control how the-
399 rotation should be applied.-
400-
401 The origin is the point that the item is rotated around (i.e., it stays-
402 fixed relative to the parent as the rest of the item is rotated). By-
403 default the origin is QPointF(0, 0).-
404-
405 The angle property provides the number of degrees to rotate the item-
406 clockwise around the origin. This value also be negative, indicating a-
407 counter-clockwise rotation. For animation purposes it may also be useful to-
408 provide rotation angles exceeding (-360, 360) degrees, for instance to-
409 animate how an item rotates several times.-
410-
411 Note: the final rotation is the combined effect of a rotation in-
412 3D space followed by a projection back to 2D. If several rotations-
413 are performed in succession, they will not behave as expected unless-
414 they were all around the Z axis.-
415-
416 \sa QGraphicsTransform, QGraphicsItem::setRotation(), QTransform::rotate()-
417*/-
418-
419class QGraphicsRotationPrivate : public QGraphicsTransformPrivate-
420{-
421public:-
422 QGraphicsRotationPrivate()-
423 : angle(0), axis(0, 0, 1) {}
never executed: end of block
0
424 QVector3D origin;-
425 qreal angle;-
426 QVector3D axis;-
427};-
428-
429/*!-
430 Constructs a new QGraphicsRotation with the given \a parent.-
431*/-
432QGraphicsRotation::QGraphicsRotation(QObject *parent)-
433 : QGraphicsTransform(*new QGraphicsRotationPrivate, parent)-
434{-
435}
never executed: end of block
0
436-
437/*!-
438 Destroys the graphics rotation.-
439*/-
440QGraphicsRotation::~QGraphicsRotation()-
441{-
442}-
443-
444/*!-
445 \property QGraphicsRotation::origin-
446 \brief the origin of the rotation in 3D space.-
447-
448 All rotations will be done relative to this point (i.e., this point-
449 will stay fixed, relative to the parent, when the item is rotated).-
450-
451 \sa angle-
452*/-
453QVector3D QGraphicsRotation::origin() const-
454{-
455 Q_D(const QGraphicsRotation);-
456 return d->origin;
never executed: return d->origin;
0
457}-
458void QGraphicsRotation::setOrigin(const QVector3D &point)-
459{-
460 Q_D(QGraphicsRotation);-
461 if (d->origin == point)
d->origin == pointDescription
TRUEnever evaluated
FALSEnever evaluated
0
462 return;
never executed: return;
0
463 d->origin = point;-
464 update();-
465 emit originChanged();-
466}
never executed: end of block
0
467-
468/*!-
469 \property QGraphicsRotation::angle-
470 \brief the angle for clockwise rotation, in degrees.-
471-
472 The angle can be any real number; the default value is 0.0. A value of 180-
473 will rotate 180 degrees, clockwise. If you provide a negative number, the-
474 item will be rotated counter-clockwise. Normally the rotation angle will be-
475 in the range (-360, 360), but you can also provide numbers outside of this-
476 range (e.g., a angle of 370 degrees gives the same result as 10 degrees).-
477 Setting the angle to NaN results in no rotation.-
478-
479 \sa origin-
480*/-
481qreal QGraphicsRotation::angle() const-
482{-
483 Q_D(const QGraphicsRotation);-
484 return d->angle;
never executed: return d->angle;
0
485}-
486void QGraphicsRotation::setAngle(qreal angle)-
487{-
488 Q_D(QGraphicsRotation);-
489 if (d->angle == angle)
d->angle == angleDescription
TRUEnever evaluated
FALSEnever evaluated
0
490 return;
never executed: return;
0
491 d->angle = angle;-
492 update();-
493 emit angleChanged();-
494}
never executed: end of block
0
495-
496/*!-
497 \fn QGraphicsRotation::originChanged()-
498-
499 This signal is emitted whenever the origin has changed.-
500-
501 \sa QGraphicsRotation::origin-
502*/-
503-
504/*!-
505 \fn void QGraphicsRotation::angleChanged()-
506-
507 This signal is emitted whenever the angle has changed.-
508-
509 \sa QGraphicsRotation::angle-
510*/-
511-
512/*!-
513 \property QGraphicsRotation::axis-
514 \brief a rotation axis, specified by a vector in 3D space.-
515-
516 This can be any axis in 3D space. By default the axis is (0, 0, 1),-
517 which is aligned with the Z axis. If you provide another axis,-
518 QGraphicsRotation will provide a transformation that rotates-
519 around this axis. For example, if you would like to rotate an item-
520 around its X axis, you could pass (1, 0, 0) as the axis.-
521-
522 \sa QTransform, QGraphicsRotation::angle-
523*/-
524QVector3D QGraphicsRotation::axis() const-
525{-
526 Q_D(const QGraphicsRotation);-
527 return d->axis;
never executed: return d->axis;
0
528}-
529void QGraphicsRotation::setAxis(const QVector3D &axis)-
530{-
531 Q_D(QGraphicsRotation);-
532 if (d->axis == axis)
d->axis == axisDescription
TRUEnever evaluated
FALSEnever evaluated
0
533 return;
never executed: return;
0
534 d->axis = axis;-
535 update();-
536 emit axisChanged();-
537}
never executed: end of block
0
538-
539/*!-
540 \fn void QGraphicsRotation::setAxis(Qt::Axis axis)-
541-
542 Convenience function to set the axis to \a axis.-
543-
544 Note: the Qt::YAxis rotation for QTransform is inverted from the-
545 correct mathematical rotation in 3D space. The QGraphicsRotation-
546 class implements a correct mathematical rotation. The following-
547 two sequences of code will perform the same transformation:-
548-
549 \code-
550 QTransform t;-
551 t.rotate(45, Qt::YAxis);-
552-
553 QGraphicsRotation r;-
554 r.setAxis(Qt::YAxis);-
555 r.setAngle(-45);-
556 \endcode-
557*/-
558void QGraphicsRotation::setAxis(Qt::Axis axis)-
559{-
560 switch (axis)-
561 {-
562 case Qt::XAxis:
never executed: case Qt::XAxis:
0
563 setAxis(QVector3D(1, 0, 0));-
564 break;
never executed: break;
0
565 case Qt::YAxis:
never executed: case Qt::YAxis:
0
566 setAxis(QVector3D(0, 1, 0));-
567 break;
never executed: break;
0
568 case Qt::ZAxis:
never executed: case Qt::ZAxis:
0
569 setAxis(QVector3D(0, 0, 1));-
570 break;
never executed: break;
0
571 }-
572}
never executed: end of block
0
573-
574/*!-
575 \reimp-
576*/-
577void QGraphicsRotation::applyTo(QMatrix4x4 *matrix) const-
578{-
579 Q_D(const QGraphicsRotation);-
580-
581 if (d->angle == 0. || d->axis.isNull() || qIsNaN(d->angle))
d->angle == 0.Description
TRUEnever evaluated
FALSEnever evaluated
d->axis.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
qIsNaN(d->angle)Description
TRUEnever evaluated
FALSEnever evaluated
0
582 return;
never executed: return;
0
583-
584 matrix->translate(d->origin);-
585 matrix->projectedRotate(d->angle, d->axis.x(), d->axis.y(), d->axis.z());-
586 matrix->translate(-d->origin);-
587}
never executed: end of block
0
588-
589/*!-
590 \fn void QGraphicsRotation::axisChanged()-
591-
592 This signal is emitted whenever the axis of the object changes.-
593-
594 \sa QGraphicsRotation::axis-
595*/-
596-
597#include "moc_qgraphicstransform.cpp"-
598-
599QT_END_NAMESPACE-
600#endif //QT_NO_GRAPHICSVIEW-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9