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