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