Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquicktranslate.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
6#include "qquickitem_p.h"
7
9
11{
12public:
14 : x(0), y(0) {}
15
18};
19
20
21/*!
22 \qmltype Translate
23 \nativetype QQuickTranslate
24 \inqmlmodule QtQuick
25 \ingroup qtquick-visual-transforms
26 \brief Provides a way to move an Item without changing its x or y properties.
27
28 The Translate type provides independent control over position in addition
29 to the Item's x and y properties.
30
31 The following example moves the Y axis of the \l Rectangle items while
32 still allowing the \l Row to lay the items out as if they had not been
33 transformed:
34
35 \qml
36 import QtQuick 2.0
37
38 Row {
39 Rectangle {
40 width: 100; height: 100
41 color: "blue"
42 transform: Translate { y: 20 }
43 }
44 Rectangle {
45 width: 100; height: 100
46 color: "red"
47 transform: Translate { y: -20 }
48 }
49 }
50 \endqml
51
52 \image translate.png
53*/
54QQuickTranslate::QQuickTranslate(QObject *parent)
55: QQuickTransform(*new QQuickTranslatePrivate, parent)
56{
57}
58
59/*!
60 \qmlproperty real QtQuick::Translate::x
61
62 The translation along the X axis.
63
64 The default value is 0.0.
65*/
66qreal QQuickTranslate::x() const
67{
68 Q_D(const QQuickTranslate);
69 return d->x;
70}
71
72void QQuickTranslate::setX(qreal x)
73{
74 Q_D(QQuickTranslate);
75 if (d->x == x)
76 return;
77 d->x = x;
78 update();
79 emit xChanged();
80}
81
82/*!
83 \qmlproperty real QtQuick::Translate::y
84
85 The translation along the Y axis.
86
87 The default value is 0.0.
88*/
89qreal QQuickTranslate::y() const
90{
91 Q_D(const QQuickTranslate);
92 return d->y;
93}
94void QQuickTranslate::setY(qreal y)
95{
96 Q_D(QQuickTranslate);
97 if (d->y == y)
98 return;
99 d->y = y;
100 update();
101 emit yChanged();
102}
103
104void QQuickTranslate::applyTo(QMatrix4x4 *matrix) const
105{
106 Q_D(const QQuickTranslate);
107 matrix->translate(d->x, d->y, 0);
108}
109
120
121/*!
122 \qmltype Scale
123 \nativetype QQuickScale
124 \inqmlmodule QtQuick
125 \ingroup qtquick-visual-transforms
126 \brief Provides a way to scale an Item.
127
128 The Scale type provides a way to scale an \l Item through a scale-type
129 transform.
130
131 It allows different scaling values for the x and y axes, and allows the
132 scale to be relative to an arbitrary point. This gives more control over
133 item scaling than the \l{Item::}{scale} property.
134
135 The following example scales the X axis of the Rectangle, relative to
136 its interior point (25, 25):
137
138 \qml
139 Rectangle {
140 width: 100; height: 100
141 color: "blue"
142 transform: Scale { origin.x: 25; origin.y: 25; xScale: 3}
143 }
144 \endqml
145
146 \sa Rotation, Translate
147*/
148QQuickScale::QQuickScale(QObject *parent)
149 : QQuickTransform(*new QQuickScalePrivate, parent)
150{
151}
152
153/*!
154 \qmlpropertygroup QtQuick::Scale::origin
155 \qmlproperty real QtQuick::Scale::origin.x
156 \qmlproperty real QtQuick::Scale::origin.y
157
158 This property holds the point that the item is scaled from (that is,
159 the point that stays fixed relative to the parent as the rest of the
160 item grows).
161
162 The default value of the origin is (0, 0).
163*/
164QVector3D QQuickScale::origin() const
165{
166 Q_D(const QQuickScale);
167 return d->origin;
168}
169void QQuickScale::setOrigin(const QVector3D &point)
170{
171 Q_D(QQuickScale);
172 if (d->origin == point)
173 return;
174 d->origin = point;
175 update();
176 emit originChanged();
177}
178
179/*!
180 \qmlproperty real QtQuick::Scale::xScale
181
182 The scaling factor for the X axis.
183
184 The default value is 1.0.
185*/
186qreal QQuickScale::xScale() const
187{
188 Q_D(const QQuickScale);
189 return d->xScale;
190}
191void QQuickScale::setXScale(qreal scale)
192{
193 Q_D(QQuickScale);
194 if (d->xScale == scale)
195 return;
196 d->xScale = scale;
197 update();
198 emit xScaleChanged();
199 emit scaleChanged();
200}
201
202/*!
203 \qmlproperty real QtQuick::Scale::yScale
204
205 The scaling factor for the Y axis.
206
207 The default value is 1.0.
208*/
209qreal QQuickScale::yScale() const
210{
211 Q_D(const QQuickScale);
212 return d->yScale;
213}
214void QQuickScale::setYScale(qreal scale)
215{
216 Q_D(QQuickScale);
217 if (d->yScale == scale)
218 return;
219 d->yScale = scale;
220 update();
221 emit yScaleChanged();
222 emit scaleChanged();
223}
224
225/*!
226 \qmlproperty real QtQuick::Scale::zScale
227 \internal
228
229 The scaling factor for the Z axis.
230
231 The default value is 1.0.
232*/
233qreal QQuickScale::zScale() const
234{
235 Q_D(const QQuickScale);
236 return d->zScale;
237}
238void QQuickScale::setZScale(qreal scale)
239{
240 Q_D(QQuickScale);
241 if (d->zScale == scale)
242 return;
243 d->zScale = scale;
244 update();
245 emit zScaleChanged();
246 emit scaleChanged();
247}
248
249void QQuickScale::applyTo(QMatrix4x4 *matrix) const
250{
251 Q_D(const QQuickScale);
252 matrix->translate(d->origin);
253 matrix->scale(d->xScale, d->yScale, d->zScale);
254 matrix->translate(-d->origin);
255}
256
267
268/*!
269 \qmltype Rotation
270 \nativetype QQuickRotation
271 \inqmlmodule QtQuick
272 \ingroup qtquick-visual-transforms
273 \brief Provides a way to rotate an Item.
274
275 The Rotation type provides a way to rotate an \l Item through a
276 rotation-type transform.
277
278 It allows (z axis) rotation to be relative to an arbitrary point, and also
279 provides a way to specify 3D-like rotations for Items. This gives more
280 control over item rotation than the \l{Item::}{rotation} property.
281
282 The following example rotates a Rectangle around its interior point
283 (25, 25):
284
285 \qml
286 Rectangle {
287 width: 100; height: 100
288 color: "blue"
289 transform: Rotation { origin.x: 25; origin.y: 25; angle: 45}
290 }
291 \endqml
292
293 For 3D-like item rotations, you must specify the axis of rotation in
294 addition to the origin point. The following example shows various 3D-like
295 rotations applied to an \l Image.
296
297 \snippet qml/rotation.qml 0
298
299 \image axisrotation.png
300
301 \sa {customitems/dialcontrol}{Dial Control example}, {Qt Quick Demo - Clocks}
302*/
303QQuickRotation::QQuickRotation(QObject *parent)
304 : QQuickTransform(*new QQuickRotationPrivate, parent)
305{
306}
307
308/*!
309 \qmlpropertygroup QtQuick::Rotation::origin
310 \qmlproperty real QtQuick::Rotation::origin.x
311 \qmlproperty real QtQuick::Rotation::origin.y
312
313 The origin point of the rotation (i.e., the point that stays fixed
314 relative to the parent as the rest of the item rotates). By default
315 the origin is (0, 0).
316*/
317QVector3D QQuickRotation::origin() const
318{
319 Q_D(const QQuickRotation);
320 return d->origin;
321}
322
323void QQuickRotation::setOrigin(const QVector3D &point)
324{
325 Q_D(QQuickRotation);
326 if (d->origin == point)
327 return;
328 d->origin = point;
329 update();
330 emit originChanged();
331}
332
333/*!
334 \qmlproperty real QtQuick::Rotation::angle
335
336 The angle to rotate, in degrees clockwise.
337*/
338qreal QQuickRotation::angle() const
339{
340 Q_D(const QQuickRotation);
341 return d->angle;
342}
343void QQuickRotation::setAngle(qreal angle)
344{
345 Q_D(QQuickRotation);
346 if (d->angle == angle)
347 return;
348 d->angle = angle;
349 update();
350 emit angleChanged();
351}
352
353/*!
354 \qmlpropertygroup QtQuick::Rotation::axis
355 \qmlproperty real QtQuick::Rotation::axis.x
356 \qmlproperty real QtQuick::Rotation::axis.y
357 \qmlproperty real QtQuick::Rotation::axis.z
358
359 The axis to rotate around. For simple (2D) rotation around a point, you
360 do not need to specify an axis, as the default axis is the z axis
361 (\c{ axis { x: 0; y: 0; z: 1 } }).
362
363 For a typical 3D-like rotation you will usually specify both the origin
364 and the axis.
365
366 \image 3d-rotation-axis.png
367*/
368QVector3D QQuickRotation::axis() const
369{
370 Q_D(const QQuickRotation);
371 return d->axis;
372}
373void QQuickRotation::setAxis(const QVector3D &axis)
374{
375 Q_D(QQuickRotation);
376 if (d->axis == axis)
377 return;
378 d->axis = axis;
379 update();
380 emit axisChanged();
381}
382
383void QQuickRotation::setAxis(Qt::Axis axis)
384{
385 switch (axis)
386 {
387 case Qt::XAxis:
388 setAxis(QVector3D(1, 0, 0));
389 break;
390 case Qt::YAxis:
391 setAxis(QVector3D(0, 1, 0));
392 break;
393 case Qt::ZAxis:
394 setAxis(QVector3D(0, 0, 1));
395 break;
396 }
397}
398
399/*!
400 \qmlproperty real QtQuick::Rotation::distanceToPlane
401 \since 6.11
402
403 This property defines the distance between the view plane (the virtual screen)
404 and the observer in the perspective projection model.
405
406 A smaller distance produces a stronger perspective effect, making the object
407 appear to recede or advance more dramatically during rotation. A larger value
408 results in a flatter, more orthographic appearance with less visible
409 perspective distortion.
410
411 The default value is \c 1024.0, which provides a moderate perspective suitable
412 for most use cases.
413
414 When this property is set to \c 0, no perspective projection is applied.
415 In this case, the rotation is performed directly in 3D space using the
416 transformation defined by \l QMatrix4x4::rotate().
417
418 This property only affects rotations around the x and y axes. Rotations around
419 the z axis (2D rotations) are not influenced by this property.
420
421 \sa QMatrix4x4::projectedRotate()
422*/
423qreal QQuickRotation::distanceToPlane() const
424{
425 Q_D(const QQuickRotation);
426 return d->distanceToPlane;
427}
428
429void QQuickRotation::setDistanceToPlane(qreal newDistanceToPlane)
430{
431 Q_D(QQuickRotation);
432 if (qFuzzyCompare(d->distanceToPlane, newDistanceToPlane))
433 return;
434 d->distanceToPlane = newDistanceToPlane;
435 emit distanceToPlaneChanged();
436}
437
438void QQuickRotation::applyTo(QMatrix4x4 *matrix) const
439{
440 Q_D(const QQuickRotation);
441
442 if (d->angle == 0. || d->axis.isNull())
443 return;
444
445 matrix->translate(d->origin);
446 matrix->projectedRotate(d->angle, d->axis.x(), d->axis.y(), d->axis.z(), d->distanceToPlane);
447 matrix->translate(-d->origin);
448}
449
450/*!
451 \qmltype Shear
452 \nativetype QQuickShear
453 \inqmlmodule QtQuick
454 \ingroup qtquick-visual-transforms
455 \since 6.9
456 \brief Provides a way to shear an Item.
457
458 The Shear type provides a way to transform an \l Item by a two-dimensional shear-type
459 matrix, sometimes known as a \e skew transform.
460
461 \qml
462 Rectangle {
463 width: 100; height: 100
464 color: "blue"
465 transform: Shear {
466 xFactor: 1.0
467 }
468 }
469 \endqml
470
471 This shears the item by a factor of \c 1.0 along the x-axis without modifying anything along the
472 y-axis. Each point \c P is displaced by \c{xFactor(P.y - origin.y)} (the signed vertical
473 distance to the \l{origin} multiplied with the \l{xFactor}). Setting the \l{yFactor} shears the
474 item along the y-axis and proportionally to the horizontal distance.
475
476 \image x-shear.png
477
478 Since the default origin is at \c{(0, 0)}, the top of the item remains untransformed, whereas
479 the bottom is displaced 100 pixels to the right (corresponding to the height of the item.)
480
481 This code is equivalent to the following:
482
483 \qml
484 Rectangle {
485 width: 100; height: 100
486 color: "blue"
487 transform: Shear {
488 xAngle: 45.0
489 }
490 }
491 \endqml
492
493 \note If both \c{xFactor}/\c{yFactor} and \c{xAngle}/\c{yAngle} are set, then the sum of the
494 two displacements will be used.
495*/
505
506QQuickShear::QQuickShear(QObject *parent)
507 : QQuickTransform(*new QQuickShearPrivate, parent)
508{
509}
510
511/*!
512 \qmlpropertygroup QtQuick::Shear::origin
513 \qmlproperty real QtQuick::Shear::origin.x
514 \qmlproperty real QtQuick::Shear::origin.y
515
516 The origin point of the transformation (i.e., the point that stays fixed relative to the parent
517 as the rest of the item is sheared).
518
519 By default the origin is \c (0, 0).
520*/
521QVector3D QQuickShear::origin() const
522{
523 Q_D(const QQuickShear);
524 return d->origin;
525}
526
527void QQuickShear::setOrigin(const QVector3D &point)
528{
529 Q_D(QQuickShear);
530 if (d->origin == point)
531 return;
532 d->origin = point;
533 update();
534 emit originChanged();
535}
536
537/*!
538 \qmlproperty real QtQuick::Shear::xFactor
539
540 The factor by which to shear the item's coordinate system along the x-axis. Each point \c P is
541 displaced by \c{xFactor(P.y - origin.y)}
542
543 This corresponds to the \c sh parameter in \l{QTransform::shear()} and the \c xShear parameter
544 in calls to \l{PlanarTransform::fromShear()}.
545
546 The default value is \c 0.0.
547
548 \sa xAngle
549*/
550qreal QQuickShear::xFactor() const
551{
552 Q_D(const QQuickShear);
553 return d->xFactor;
554}
555void QQuickShear::setXFactor(qreal xFactor)
556{
557 Q_D(QQuickShear);
558 if (d->xFactor == xFactor)
559 return;
560 d->xFactor = xFactor;
561 update();
562 emit xFactorChanged();
563}
564
565/*!
566 \qmlproperty real QtQuick::Shear::yFactor
567
568 The factor by which to shear the item's coordinate system along the y-axis. The factor by which
569 to shear the item's coordinate system along the x-axis. Each point \c P is displaced by
570 \c{xFactor(P.y - origin.y)}
571
572 This corresponds to the \c sv parameter in \l{QTransform::shear()} and the \c yShear parameter
573 in calls to \l{PlanarTransform::fromShear()}.
574
575 The default value is \c 0.0.
576
577 \sa yAngle
578*/
579qreal QQuickShear::yFactor() const
580{
581 Q_D(const QQuickShear);
582 return d->yFactor;
583}
584void QQuickShear::setYFactor(qreal yFactor)
585{
586 Q_D(QQuickShear);
587 if (d->yFactor == yFactor)
588 return;
589 d->yFactor = yFactor;
590 update();
591 emit yFactorChanged();
592}
593
594/*!
595 \qmlproperty real QtQuick::Shear::xAngle
596
597 The angle (in degrees) by which to shear the item's coordinate system along the x-axis. This
598 is equivalent to setting \l{xFactor} to \c{tan(xAngle)}.
599
600 The default value is \c 0.0.
601
602 \sa xFactor
603*/
604qreal QQuickShear::xAngle() const
605{
606 Q_D(const QQuickShear);
607 return d->xAngle;
608}
609void QQuickShear::setXAngle(qreal xAngle)
610{
611 Q_D(QQuickShear);
612 if (d->xAngle == xAngle)
613 return;
614 d->xAngle = xAngle;
615 update();
616 emit xAngleChanged();
617}
618
619/*!
620 \qmlproperty real QtQuick::Shear::yAngle
621
622 The angle (in degrees) by which to shear the item's coordinate system along the y-axis. This
623 is equivalent to setting \l{yFactor} to \c{tan(yAngle)}.
624
625 The default value is \c 0.0.
626
627 \sa yFactor
628*/
629qreal QQuickShear::yAngle() const
630{
631 Q_D(const QQuickShear);
632 return d->yAngle;
633}
634void QQuickShear::setYAngle(qreal yAngle)
635{
636 Q_D(QQuickShear);
637 if (d->yAngle == yAngle)
638 return;
639 d->yAngle = yAngle;
640 update();
641 emit yAngleChanged();
642}
643
644void QQuickShear::applyTo(QMatrix4x4 *matrix) const
645{
646 Q_D(const QQuickShear);
647 if (d->xFactor == 0.0 && d->yFactor == 0.0 && d->xAngle == 0.0 && d->yAngle == 0.0)
648 return;
649
650 const qreal xShear = qTan(qDegreesToRadians(d->xAngle)) + d->xFactor;
651 const qreal yShear = qTan(qDegreesToRadians(d->yAngle)) + d->yFactor;
652
653 matrix->translate(d->origin);
654 *matrix *= QMatrix4x4(1.0, xShear, 0.0, 0.0,
655 yShear, 1.0, 0.0, 0.0,
656 0.0, 0.0, 1.0, 0.0,
657 0.0, 0.0, 0.0, 1.0);
658 matrix->translate(-d->origin);
659}
660
661
669
670/*!
671 \qmltype Matrix4x4
672 \nativetype QQuickMatrix4x4
673 \inqmlmodule QtQuick
674 \ingroup qtquick-visual-transforms
675 \since 5.3
676 \brief Provides a way to apply a 4x4 tranformation matrix to an \l Item.
677
678 The Matrix4x4 type provides a way to apply a transformation to an
679 \l Item through a 4x4 matrix.
680
681 It allows for a combination of rotation, scale, translatation and shearing
682 by using just one tranformation provided in a 4x4-matrix.
683
684 The following example rotates a Rectangle 45 degress (PI/4):
685
686 \qml
687 Rectangle {
688 width: 100
689 height: 100
690 color: "red"
691
692 transform: Matrix4x4 {
693 property real a: Math.PI / 4
694 matrix: Qt.matrix4x4(Math.cos(a), -Math.sin(a), 0, 0,
695 Math.sin(a), Math.cos(a), 0, 0,
696 0, 0, 1, 0,
697 0, 0, 0, 1)
698 }
699 }
700 \endqml
701*/
702QQuickMatrix4x4::QQuickMatrix4x4(QObject *parent)
703 : QQuickTransform(*new QQuickMatrix4x4Private, parent)
704{
705}
706
707/*!
708 \qmlproperty matrix4x4 QtQuick::Matrix4x4::matrix
709
710 4x4-matrix which will be used in the tranformation of an \l Item
711*/
712QMatrix4x4 QQuickMatrix4x4::matrix() const
713{
714 Q_D(const QQuickMatrix4x4);
715 return d->matrix;
716}
717
718void QQuickMatrix4x4::setMatrix(const QMatrix4x4 &matrix)
719{
720 Q_D(QQuickMatrix4x4);
721 if (d->matrix == matrix)
722 return;
723 d->matrix = matrix;
724 update();
725 emit matrixChanged();
726}
727
728void QQuickMatrix4x4::applyTo(QMatrix4x4 *matrix) const
729{
730 Q_D(const QQuickMatrix4x4);
731 *matrix *= d->matrix;
732}
733
734QT_END_NAMESPACE
735
736#include "moc_qquicktranslate_p.cpp"
\qmltype Shear \nativetype QQuickShear \inqmlmodule QtQuick
Combined button and popup list for selecting options.