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*/
421qreal QQuickRotation::distanceToPlane() const
422{
423 Q_D(const QQuickRotation);
424 return d->distanceToPlane;
425}
426
427void QQuickRotation::setDistanceToPlane(qreal newDistanceToPlane)
428{
429 Q_D(QQuickRotation);
430 if (qFuzzyCompare(d->distanceToPlane, newDistanceToPlane))
431 return;
432 d->distanceToPlane = newDistanceToPlane;
433 emit distanceToPlaneChanged();
434}
435
436void QQuickRotation::applyTo(QMatrix4x4 *matrix) const
437{
438 Q_D(const QQuickRotation);
439
440 if (d->angle == 0. || d->axis.isNull())
441 return;
442
443 matrix->translate(d->origin);
444 matrix->projectedRotate(d->angle, d->axis.x(), d->axis.y(), d->axis.z(), d->distanceToPlane);
445 matrix->translate(-d->origin);
446}
447
448/*!
449 \qmltype Shear
450 \nativetype QQuickShear
451 \inqmlmodule QtQuick
452 \ingroup qtquick-visual-transforms
453 \since 6.9
454 \brief Provides a way to shear an Item.
455
456 The Shear type provides a way to transform an \l Item by a two-dimensional shear-type
457 matrix, sometimes known as a \e skew transform.
458
459 \qml
460 Rectangle {
461 width: 100; height: 100
462 color: "blue"
463 transform: Shear {
464 xFactor: 1.0
465 }
466 }
467 \endqml
468
469 This shears the item by a factor of \c 1.0 along the x-axis without modifying anything along the
470 y-axis. Each point \c P is displaced by \c{xFactor(P.y - origin.y)} (the signed vertical
471 distance to the \l{origin} multiplied with the \l{xFactor}). Setting the \l{yFactor} shears the
472 item along the y-axis and proportionally to the horizontal distance.
473
474 \image x-shear.png
475
476 Since the default origin is at \c{(0, 0)}, the top of the item remains untransformed, whereas
477 the bottom is displaced 100 pixels to the right (corresponding to the height of the item.)
478
479 This code is equivalent to the following:
480
481 \qml
482 Rectangle {
483 width: 100; height: 100
484 color: "blue"
485 transform: Shear {
486 xAngle: 45.0
487 }
488 }
489 \endqml
490
491 \note If both \c{xFactor}/\c{yFactor} and \c{xAngle}/\c{yAngle} are set, then the sum of the
492 two displacements will be used.
493*/
503
504QQuickShear::QQuickShear(QObject *parent)
505 : QQuickTransform(*new QQuickShearPrivate, parent)
506{
507}
508
509/*!
510 \qmlpropertygroup QtQuick::Shear::origin
511 \qmlproperty real QtQuick::Shear::origin.x
512 \qmlproperty real QtQuick::Shear::origin.y
513
514 The origin point of the transformation (i.e., the point that stays fixed relative to the parent
515 as the rest of the item is sheared).
516
517 By default the origin is \c (0, 0).
518*/
519QVector3D QQuickShear::origin() const
520{
521 Q_D(const QQuickShear);
522 return d->origin;
523}
524
525void QQuickShear::setOrigin(const QVector3D &point)
526{
527 Q_D(QQuickShear);
528 if (d->origin == point)
529 return;
530 d->origin = point;
531 update();
532 emit originChanged();
533}
534
535/*!
536 \qmlproperty real QtQuick::Shear::xFactor
537
538 The factor by which to shear the item's coordinate system along the x-axis. Each point \c P is
539 displaced by \c{xFactor(P.y - origin.y)}
540
541 This corresponds to the \c sh parameter in \l{QTransform::shear()} and the \c xShear parameter
542 in calls to \l{PlanarTransform::fromShear()}.
543
544 The default value is \c 0.0.
545
546 \sa xAngle
547*/
548qreal QQuickShear::xFactor() const
549{
550 Q_D(const QQuickShear);
551 return d->xFactor;
552}
553void QQuickShear::setXFactor(qreal xFactor)
554{
555 Q_D(QQuickShear);
556 if (d->xFactor == xFactor)
557 return;
558 d->xFactor = xFactor;
559 update();
560 emit xFactorChanged();
561}
562
563/*!
564 \qmlproperty real QtQuick::Shear::yFactor
565
566 The factor by which to shear the item's coordinate system along the y-axis. The factor by which
567 to shear the item's coordinate system along the x-axis. Each point \c P is displaced by
568 \c{xFactor(P.y - origin.y)}
569
570 This corresponds to the \c sv parameter in \l{QTransform::shear()} and the \c yShear parameter
571 in calls to \l{PlanarTransform::fromShear()}.
572
573 The default value is \c 0.0.
574
575 \sa yAngle
576*/
577qreal QQuickShear::yFactor() const
578{
579 Q_D(const QQuickShear);
580 return d->yFactor;
581}
582void QQuickShear::setYFactor(qreal yFactor)
583{
584 Q_D(QQuickShear);
585 if (d->yFactor == yFactor)
586 return;
587 d->yFactor = yFactor;
588 update();
589 emit yFactorChanged();
590}
591
592/*!
593 \qmlproperty real QtQuick::Shear::xAngle
594
595 The angle (in degrees) by which to shear the item's coordinate system along the x-axis. This
596 is equivalent to setting \l{xFactor} to \c{tan(xAngle)}.
597
598 The default value is \c 0.0.
599
600 \sa xFactor
601*/
602qreal QQuickShear::xAngle() const
603{
604 Q_D(const QQuickShear);
605 return d->xAngle;
606}
607void QQuickShear::setXAngle(qreal xAngle)
608{
609 Q_D(QQuickShear);
610 if (d->xAngle == xAngle)
611 return;
612 d->xAngle = xAngle;
613 update();
614 emit xAngleChanged();
615}
616
617/*!
618 \qmlproperty real QtQuick::Shear::yAngle
619
620 The angle (in degrees) by which to shear the item's coordinate system along the y-axis. This
621 is equivalent to setting \l{yFactor} to \c{tan(yAngle)}.
622
623 The default value is \c 0.0.
624
625 \sa yFactor
626*/
627qreal QQuickShear::yAngle() const
628{
629 Q_D(const QQuickShear);
630 return d->yAngle;
631}
632void QQuickShear::setYAngle(qreal yAngle)
633{
634 Q_D(QQuickShear);
635 if (d->yAngle == yAngle)
636 return;
637 d->yAngle = yAngle;
638 update();
639 emit yAngleChanged();
640}
641
642void QQuickShear::applyTo(QMatrix4x4 *matrix) const
643{
644 Q_D(const QQuickShear);
645 if (d->xFactor == 0.0 && d->yFactor == 0.0 && d->xAngle == 0.0 && d->yAngle == 0.0)
646 return;
647
648 const qreal xShear = qTan(qDegreesToRadians(d->xAngle)) + d->xFactor;
649 const qreal yShear = qTan(qDegreesToRadians(d->yAngle)) + d->yFactor;
650
651 matrix->translate(d->origin);
652 *matrix *= QMatrix4x4(1.0, xShear, 0.0, 0.0,
653 yShear, 1.0, 0.0, 0.0,
654 0.0, 0.0, 1.0, 0.0,
655 0.0, 0.0, 0.0, 1.0);
656 matrix->translate(-d->origin);
657}
658
659
667
668/*!
669 \qmltype Matrix4x4
670 \nativetype QQuickMatrix4x4
671 \inqmlmodule QtQuick
672 \ingroup qtquick-visual-transforms
673 \since 5.3
674 \brief Provides a way to apply a 4x4 tranformation matrix to an \l Item.
675
676 The Matrix4x4 type provides a way to apply a transformation to an
677 \l Item through a 4x4 matrix.
678
679 It allows for a combination of rotation, scale, translatation and shearing
680 by using just one tranformation provided in a 4x4-matrix.
681
682 The following example rotates a Rectangle 45 degress (PI/4):
683
684 \qml
685 Rectangle {
686 width: 100
687 height: 100
688 color: "red"
689
690 transform: Matrix4x4 {
691 property real a: Math.PI / 4
692 matrix: Qt.matrix4x4(Math.cos(a), -Math.sin(a), 0, 0,
693 Math.sin(a), Math.cos(a), 0, 0,
694 0, 0, 1, 0,
695 0, 0, 0, 1)
696 }
697 }
698 \endqml
699*/
700QQuickMatrix4x4::QQuickMatrix4x4(QObject *parent)
701 : QQuickTransform(*new QQuickMatrix4x4Private, parent)
702{
703}
704
705/*!
706 \qmlproperty matrix4x4 QtQuick::Matrix4x4::matrix
707
708 4x4-matrix which will be used in the tranformation of an \l Item
709*/
710QMatrix4x4 QQuickMatrix4x4::matrix() const
711{
712 Q_D(const QQuickMatrix4x4);
713 return d->matrix;
714}
715
716void QQuickMatrix4x4::setMatrix(const QMatrix4x4 &matrix)
717{
718 Q_D(QQuickMatrix4x4);
719 if (d->matrix == matrix)
720 return;
721 d->matrix = matrix;
722 update();
723 emit matrixChanged();
724}
725
726void QQuickMatrix4x4::applyTo(QMatrix4x4 *matrix) const
727{
728 Q_D(const QQuickMatrix4x4);
729 *matrix *= d->matrix;
730}
731
732QT_END_NAMESPACE
733
734#include "moc_qquicktranslate_p.cpp"
\qmltype Shear \nativetype QQuickShear \inqmlmodule QtQuick
Combined button and popup list for selecting options.