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
266
267/*!
268 \qmltype Rotation
269 \nativetype QQuickRotation
270 \inqmlmodule QtQuick
271 \ingroup qtquick-visual-transforms
272 \brief Provides a way to rotate an Item.
273
274 The Rotation type provides a way to rotate an \l Item through a
275 rotation-type transform.
276
277 It allows (z axis) rotation to be relative to an arbitrary point, and also
278 provides a way to specify 3D-like rotations for Items. This gives more
279 control over item rotation than the \l{Item::}{rotation} property.
280
281 The following example rotates a Rectangle around its interior point
282 (25, 25):
283
284 \qml
285 Rectangle {
286 width: 100; height: 100
287 color: "blue"
288 transform: Rotation { origin.x: 25; origin.y: 25; angle: 45}
289 }
290 \endqml
291
292 For 3D-like item rotations, you must specify the axis of rotation in
293 addition to the origin point. The following example shows various 3D-like
294 rotations applied to an \l Image.
295
296 \snippet qml/rotation.qml 0
297
298 \image axisrotation.png
299
300 \sa {customitems/dialcontrol}{Dial Control example}, {Qt Quick Demo - Clocks}
301*/
302QQuickRotation::QQuickRotation(QObject *parent)
303 : QQuickTransform(*new QQuickRotationPrivate, parent)
304{
305}
306
307/*!
308 \qmlpropertygroup QtQuick::Rotation::origin
309 \qmlproperty real QtQuick::Rotation::origin.x
310 \qmlproperty real QtQuick::Rotation::origin.y
311
312 The origin point of the rotation (i.e., the point that stays fixed
313 relative to the parent as the rest of the item rotates). By default
314 the origin is (0, 0).
315*/
316QVector3D QQuickRotation::origin() const
317{
318 Q_D(const QQuickRotation);
319 return d->origin;
320}
321
322void QQuickRotation::setOrigin(const QVector3D &point)
323{
324 Q_D(QQuickRotation);
325 if (d->origin == point)
326 return;
327 d->origin = point;
328 update();
329 emit originChanged();
330}
331
332/*!
333 \qmlproperty real QtQuick::Rotation::angle
334
335 The angle to rotate, in degrees clockwise.
336*/
337qreal QQuickRotation::angle() const
338{
339 Q_D(const QQuickRotation);
340 return d->angle;
341}
342void QQuickRotation::setAngle(qreal angle)
343{
344 Q_D(QQuickRotation);
345 if (d->angle == angle)
346 return;
347 d->angle = angle;
348 update();
349 emit angleChanged();
350}
351
352/*!
353 \qmlpropertygroup QtQuick::Rotation::axis
354 \qmlproperty real QtQuick::Rotation::axis.x
355 \qmlproperty real QtQuick::Rotation::axis.y
356 \qmlproperty real QtQuick::Rotation::axis.z
357
358 The axis to rotate around. For simple (2D) rotation around a point, you
359 do not need to specify an axis, as the default axis is the z axis
360 (\c{ axis { x: 0; y: 0; z: 1 } }).
361
362 For a typical 3D-like rotation you will usually specify both the origin
363 and the axis.
364
365 \image 3d-rotation-axis.png
366*/
367QVector3D QQuickRotation::axis() const
368{
369 Q_D(const QQuickRotation);
370 return d->axis;
371}
372void QQuickRotation::setAxis(const QVector3D &axis)
373{
374 Q_D(QQuickRotation);
375 if (d->axis == axis)
376 return;
377 d->axis = axis;
378 update();
379 emit axisChanged();
380}
381
382void QQuickRotation::setAxis(Qt::Axis axis)
383{
384 switch (axis)
385 {
386 case Qt::XAxis:
387 setAxis(QVector3D(1, 0, 0));
388 break;
389 case Qt::YAxis:
390 setAxis(QVector3D(0, 1, 0));
391 break;
392 case Qt::ZAxis:
393 setAxis(QVector3D(0, 0, 1));
394 break;
395 }
396}
397
398void QQuickRotation::applyTo(QMatrix4x4 *matrix) const
399{
400 Q_D(const QQuickRotation);
401
402 if (d->angle == 0. || d->axis.isNull())
403 return;
404
405 matrix->translate(d->origin);
406 matrix->projectedRotate(d->angle, d->axis.x(), d->axis.y(), d->axis.z());
407 matrix->translate(-d->origin);
408}
409
410/*!
411 \qmltype Shear
412 \nativetype QQuickShear
413 \inqmlmodule QtQuick
414 \ingroup qtquick-visual-transforms
415 \since 6.9
416 \brief Provides a way to shear an Item.
417
418 The Shear type provides a way to transform an \l Item by a two-dimensional shear-type
419 matrix, sometimes known as a \e skew transform.
420
421 \qml
422 Rectangle {
423 width: 100; height: 100
424 color: "blue"
425 transform: Shear {
426 xFactor: 1.0
427 }
428 }
429 \endqml
430
431 This shears the item by a factor of \c 1.0 along the x-axis without modifying anything along the
432 y-axis. Each point \c P is displaced by \c{xFactor(P.y - origin.y)} (the signed vertical
433 distance to the \l{origin} multiplied with the \l{xFactor}). Setting the \l{yFactor} shears the
434 item along the y-axis and proportionally to the horizontal distance.
435
436 \image x-shear.png
437
438 Since the default origin is at \c{(0, 0)}, the top of the item remains untransformed, whereas
439 the bottom is displaced 100 pixels to the right (corresponding to the height of the item.)
440
441 This code is equivalent to the following:
442
443 \qml
444 Rectangle {
445 width: 100; height: 100
446 color: "blue"
447 transform: Shear {
448 xAngle: 45.0
449 }
450 }
451 \endqml
452
453 \note If both \c{xFactor}/\c{yFactor} and \c{xAngle}/\c{yAngle} are set, then the sum of the
454 two displacements will be used.
455*/
465
466QQuickShear::QQuickShear(QObject *parent)
467 : QQuickTransform(*new QQuickShearPrivate, parent)
468{
469}
470
471/*!
472 \qmlpropertygroup QtQuick::Shear::origin
473 \qmlproperty real QtQuick::Shear::origin.x
474 \qmlproperty real QtQuick::Shear::origin.y
475
476 The origin point of the transformation (i.e., the point that stays fixed relative to the parent
477 as the rest of the item is sheared).
478
479 By default the origin is \c (0, 0).
480*/
481QVector3D QQuickShear::origin() const
482{
483 Q_D(const QQuickShear);
484 return d->origin;
485}
486
487void QQuickShear::setOrigin(const QVector3D &point)
488{
489 Q_D(QQuickShear);
490 if (d->origin == point)
491 return;
492 d->origin = point;
493 update();
494 emit originChanged();
495}
496
497/*!
498 \qmlproperty real QtQuick::Shear::xFactor
499
500 The factor by which to shear the item's coordinate system along the x-axis. Each point \c P is
501 displaced by \c{xFactor(P.y - origin.y)}
502
503 This corresponds to the \c sh parameter in \l{QTransform::shear()} and the \c xShear parameter
504 in calls to \l{PlanarTransform::fromShear()}.
505
506 The default value is \c 0.0.
507
508 \sa xAngle
509*/
510qreal QQuickShear::xFactor() const
511{
512 Q_D(const QQuickShear);
513 return d->xFactor;
514}
515void QQuickShear::setXFactor(qreal xFactor)
516{
517 Q_D(QQuickShear);
518 if (d->xFactor == xFactor)
519 return;
520 d->xFactor = xFactor;
521 update();
522 emit xFactorChanged();
523}
524
525/*!
526 \qmlproperty real QtQuick::Shear::yFactor
527
528 The factor by which to shear the item's coordinate system along the y-axis. The factor by which
529 to shear the item's coordinate system along the x-axis. Each point \c P is displaced by
530 \c{xFactor(P.y - origin.y)}
531
532 This corresponds to the \c sv parameter in \l{QTransform::shear()} and the \c yShear parameter
533 in calls to \l{PlanarTransform::fromShear()}.
534
535 The default value is \c 0.0.
536
537 \sa yAngle
538*/
539qreal QQuickShear::yFactor() const
540{
541 Q_D(const QQuickShear);
542 return d->yFactor;
543}
544void QQuickShear::setYFactor(qreal yFactor)
545{
546 Q_D(QQuickShear);
547 if (d->yFactor == yFactor)
548 return;
549 d->yFactor = yFactor;
550 update();
551 emit yFactorChanged();
552}
553
554/*!
555 \qmlproperty real QtQuick::Shear::xAngle
556
557 The angle (in degrees) by which to shear the item's coordinate system along the x-axis. This
558 is equivalent to setting \l{xFactor} to \c{tan(xAngle)}.
559
560 The default value is \c 0.0.
561
562 \sa xFactor
563*/
564qreal QQuickShear::xAngle() const
565{
566 Q_D(const QQuickShear);
567 return d->xAngle;
568}
569void QQuickShear::setXAngle(qreal xAngle)
570{
571 Q_D(QQuickShear);
572 if (d->xAngle == xAngle)
573 return;
574 d->xAngle = xAngle;
575 update();
576 emit xAngleChanged();
577}
578
579/*!
580 \qmlproperty real QtQuick::Shear::yAngle
581
582 The angle (in degrees) by which to shear the item's coordinate system along the y-axis. This
583 is equivalent to setting \l{yFactor} to \c{tan(yAngle)}.
584
585 The default value is \c 0.0.
586
587 \sa yFactor
588*/
589qreal QQuickShear::yAngle() const
590{
591 Q_D(const QQuickShear);
592 return d->yAngle;
593}
594void QQuickShear::setYAngle(qreal yAngle)
595{
596 Q_D(QQuickShear);
597 if (d->yAngle == yAngle)
598 return;
599 d->yAngle = yAngle;
600 update();
601 emit yAngleChanged();
602}
603
604void QQuickShear::applyTo(QMatrix4x4 *matrix) const
605{
606 Q_D(const QQuickShear);
607 if (d->xFactor == 0.0 && d->yFactor == 0.0 && d->xAngle == 0.0 && d->yAngle == 0.0)
608 return;
609
610 const qreal xShear = qTan(qDegreesToRadians(d->xAngle)) + d->xFactor;
611 const qreal yShear = qTan(qDegreesToRadians(d->yAngle)) + d->yFactor;
612
613 matrix->translate(d->origin);
614 *matrix *= QMatrix4x4(1.0, xShear, 0.0, 0.0,
615 yShear, 1.0, 0.0, 0.0,
616 0.0, 0.0, 1.0, 0.0,
617 0.0, 0.0, 0.0, 1.0);
618 matrix->translate(-d->origin);
619}
620
621
629
630/*!
631 \qmltype Matrix4x4
632 \nativetype QQuickMatrix4x4
633 \inqmlmodule QtQuick
634 \ingroup qtquick-visual-transforms
635 \since 5.3
636 \brief Provides a way to apply a 4x4 tranformation matrix to an \l Item.
637
638 The Matrix4x4 type provides a way to apply a transformation to an
639 \l Item through a 4x4 matrix.
640
641 It allows for a combination of rotation, scale, translatation and shearing
642 by using just one tranformation provided in a 4x4-matrix.
643
644 The following example rotates a Rectangle 45 degress (PI/4):
645
646 \qml
647 Rectangle {
648 width: 100
649 height: 100
650 color: "red"
651
652 transform: Matrix4x4 {
653 property real a: Math.PI / 4
654 matrix: Qt.matrix4x4(Math.cos(a), -Math.sin(a), 0, 0,
655 Math.sin(a), Math.cos(a), 0, 0,
656 0, 0, 1, 0,
657 0, 0, 0, 1)
658 }
659 }
660 \endqml
661*/
662QQuickMatrix4x4::QQuickMatrix4x4(QObject *parent)
663 : QQuickTransform(*new QQuickMatrix4x4Private, parent)
664{
665}
666
667/*!
668 \qmlproperty matrix4x4 QtQuick::Matrix4x4::matrix
669
670 4x4-matrix which will be used in the tranformation of an \l Item
671*/
672QMatrix4x4 QQuickMatrix4x4::matrix() const
673{
674 Q_D(const QQuickMatrix4x4);
675 return d->matrix;
676}
677
678void QQuickMatrix4x4::setMatrix(const QMatrix4x4 &matrix)
679{
680 Q_D(QQuickMatrix4x4);
681 if (d->matrix == matrix)
682 return;
683 d->matrix = matrix;
684 update();
685 emit matrixChanged();
686}
687
688void QQuickMatrix4x4::applyTo(QMatrix4x4 *matrix) const
689{
690 Q_D(const QQuickMatrix4x4);
691 *matrix *= d->matrix;
692}
693
694QT_END_NAMESPACE
695
696#include "moc_qquicktranslate_p.cpp"
\qmltype Shear \nativetype QQuickShear \inqmlmodule QtQuick