1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtDeclarative module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \class QGraphicsTransform
|
---|
44 | \brief The QGraphicsTransform class is an abstract base class for building
|
---|
45 | advanced transformations on QGraphicsItems.
|
---|
46 | \since 4.6
|
---|
47 | \ingroup graphicsview-api
|
---|
48 |
|
---|
49 | As an alternative to QGraphicsItem::transform, QGraphicsTransform lets you
|
---|
50 | create and control advanced transformations that can be configured
|
---|
51 | independently using specialized properties.
|
---|
52 |
|
---|
53 | QGraphicsItem allows you to assign any number of QGraphicsTransform
|
---|
54 | instances to one QGraphicsItem. Each QGraphicsTransform is applied in
|
---|
55 | order, one at a time, to the QGraphicsItem it's assigned to.
|
---|
56 |
|
---|
57 | QGraphicsTransform is particularly useful for animations. Whereas
|
---|
58 | QGraphicsItem::setTransform() lets you assign any transform directly to an
|
---|
59 | item, there is no direct way to interpolate between two different
|
---|
60 | transformations (e.g., when transitioning between two states, each for
|
---|
61 | which the item has a different arbitrary transform assigned). Using
|
---|
62 | QGraphicsTransform you can interpolate the property values of each
|
---|
63 | independent transformation. The resulting operation is then combined into a
|
---|
64 | single transform which is applied to QGraphicsItem.
|
---|
65 |
|
---|
66 | Transformations are computed in true 3D space using QMatrix4x4.
|
---|
67 | When the transformation is applied to a QGraphicsItem, it will be
|
---|
68 | projected back to a 2D QTransform. When multiple QGraphicsTransform
|
---|
69 | objects are applied to a QGraphicsItem, all of the transformations
|
---|
70 | are computed in true 3D space, with the projection back to 2D
|
---|
71 | only occurring after the last QGraphicsTransform is applied.
|
---|
72 | The exception to this is QGraphicsRotation, which projects back to
|
---|
73 | 2D after each rotation to preserve the perspective effect around
|
---|
74 | the X and Y axes.
|
---|
75 |
|
---|
76 | If you want to create your own configurable transformation, you can create
|
---|
77 | a subclass of QGraphicsTransform (or any or the existing subclasses), and
|
---|
78 | reimplement the pure virtual applyTo() function, which takes a pointer to a
|
---|
79 | QMatrix4x4. Each operation you would like to apply should be exposed as
|
---|
80 | properties (e.g., customTransform->setVerticalShear(2.5)). Inside you
|
---|
81 | reimplementation of applyTo(), you can modify the provided transform
|
---|
82 | respectively.
|
---|
83 |
|
---|
84 | QGraphicsTransform can be used together with QGraphicsItem::setTransform(),
|
---|
85 | QGraphicsItem::setRotation(), and QGraphicsItem::setScale().
|
---|
86 |
|
---|
87 | \sa QGraphicsItem::transform(), QGraphicsScale, QGraphicsRotation
|
---|
88 | */
|
---|
89 |
|
---|
90 | #include "qgraphicstransform.h"
|
---|
91 | #include "qgraphicsitem_p.h"
|
---|
92 | #include "qgraphicstransform_p.h"
|
---|
93 | #include <QDebug>
|
---|
94 | #include <QtCore/qmath.h>
|
---|
95 |
|
---|
96 | #ifndef QT_NO_GRAPHICSVIEW
|
---|
97 | QT_BEGIN_NAMESPACE
|
---|
98 | void QGraphicsTransformPrivate::setItem(QGraphicsItem *i)
|
---|
99 | {
|
---|
100 | if (item == i)
|
---|
101 | return;
|
---|
102 |
|
---|
103 | if (item) {
|
---|
104 | Q_Q(QGraphicsTransform);
|
---|
105 | QGraphicsItemPrivate *d_ptr = item->d_ptr.data();
|
---|
106 |
|
---|
107 | item->prepareGeometryChange();
|
---|
108 | Q_ASSERT(d_ptr->transformData);
|
---|
109 | d_ptr->transformData->graphicsTransforms.removeAll(q);
|
---|
110 | d_ptr->dirtySceneTransform = 1;
|
---|
111 | item = 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | item = i;
|
---|
115 | }
|
---|
116 |
|
---|
117 | void QGraphicsTransformPrivate::updateItem(QGraphicsItem *item)
|
---|
118 | {
|
---|
119 | item->prepareGeometryChange();
|
---|
120 | item->d_ptr->dirtySceneTransform = 1;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*!
|
---|
124 | Constructs a new QGraphicsTransform with the given \a parent.
|
---|
125 | */
|
---|
126 | QGraphicsTransform::QGraphicsTransform(QObject *parent)
|
---|
127 | : QObject(*new QGraphicsTransformPrivate, parent)
|
---|
128 | {
|
---|
129 | }
|
---|
130 |
|
---|
131 | /*!
|
---|
132 | Destroys the graphics transform.
|
---|
133 | */
|
---|
134 | QGraphicsTransform::~QGraphicsTransform()
|
---|
135 | {
|
---|
136 | Q_D(QGraphicsTransform);
|
---|
137 | d->setItem(0);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*!
|
---|
141 | \internal
|
---|
142 | */
|
---|
143 | QGraphicsTransform::QGraphicsTransform(QGraphicsTransformPrivate &p, QObject *parent)
|
---|
144 | : QObject(p, parent)
|
---|
145 | {
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*!
|
---|
149 | \fn void QGraphicsTransform::applyTo(QMatrix4x4 *matrix) const
|
---|
150 |
|
---|
151 | This pure virtual method has to be reimplemented in derived classes.
|
---|
152 |
|
---|
153 | It applies this transformation to \a matrix.
|
---|
154 |
|
---|
155 | \sa QGraphicsItem::transform(), QMatrix4x4::toTransform()
|
---|
156 | */
|
---|
157 |
|
---|
158 | /*!
|
---|
159 | Notifies that this transform operation has changed its parameters in such a
|
---|
160 | way that applyTo() will return a different result than before.
|
---|
161 |
|
---|
162 | When implementing you own custom graphics transform, you must call this
|
---|
|
---|