Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquickflipable.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
4#include "qquickflipable_p.h"
5#include "qquickitem_p.h"
6#include "qquicktranslate_p.h"
7
8#include <QtQml/qqmlinfo.h>
9
11
12// XXX todo - i think this needs work and a bit of a re-think
13
15{
17public:
19
20 void setTransform(const QTransform &t) {
21 transform = t;
22 update();
23 }
24 void applyTo(QMatrix4x4 *matrix) const override {
25 *matrix *= transform;
26 }
27private:
29};
30
32{
33 Q_DECLARE_PUBLIC(QQuickFlipable)
34public:
36
37 bool transformChanged(QQuickItem *transformedItem) override;
38 void updateSide();
39 void setBackTransform();
40
45
49};
50
96{
97}
98
100{
101}
102
111{
112 Q_D(const QQuickFlipable);
113 return d->front;
114}
115
117{
118 Q_D(QQuickFlipable);
119 if (d->front) {
120 qmlWarning(this) << tr("front is a write-once property");
121 return;
122 }
123 d->front = front;
124 d->front->setParentItem(this);
125 if (Back == d->current) {
126 d->front->setOpacity(0.);
127 d->front->setEnabled(false);
128 }
130}
131
133{
134 Q_D(const QQuickFlipable);
135 return d->back;
136}
137
139{
140 Q_D(QQuickFlipable);
141 if (d->back) {
142 qmlWarning(this) << tr("back is a write-once property");
143 return;
144 }
145 if (back == nullptr)
146 return;
147 d->back = back;
148 d->back->setParentItem(this);
149
150 d->backTransform = new QQuickLocalTransform(d->back);
151 d->backTransform->prependToItem(d->back);
152
153 if (Front == d->current) {
154 d->back->setOpacity(0.);
155 d->back->setEnabled(false);
156 }
157
159 this, SLOT(retransformBack()));
161 this, SLOT(retransformBack()));
163}
164
165void QQuickFlipable::retransformBack()
166{
167 Q_D(QQuickFlipable);
168 if (d->current == QQuickFlipable::Back && d->back)
169 d->setBackTransform();
170}
171
179{
180 Q_D(const QQuickFlipable);
181
182 const_cast<QQuickFlipablePrivate *>(d)->updateSide();
183 return d->current;
184}
185
187{
188 Q_Q(QQuickFlipable);
189
190 if (!sideDirty) {
191 sideDirty = true;
192 q->polish();
193 }
194
195 return QQuickItemPrivate::transformChanged(transformedItem);
196}
197
199{
200 Q_D(QQuickFlipable);
201 d->updateSide();
202}
203
219{
220 Q_Q(QQuickFlipable);
221
222 if (!sideDirty)
223 return;
224
225 sideDirty = false;
226
228
229 const qreal tx = x.value();
230 const qreal ty = y.value();
231 if (!qFuzzyIsNull(tx) || !qFuzzyIsNull(ty))
232 sceneTransform.translate(tx, ty);
233
234 for (const auto *transform : std::as_const(transforms)) {
235 if (const auto *rot = qobject_cast<const QQuickRotation *>(transform)) {
236 // rotation is a special case: we want to call rotate() instead of projectedRotate()
237 const auto angle = rot->angle();
238 const auto axis = rot->axis();
239 if (!(qFuzzyIsNull(angle) || axis.isNull())) {
240 sceneTransform.translate(rot->origin());
241 sceneTransform.rotate(angle, axis.x(), axis.y(), axis.z());
242 sceneTransform.translate(-rot->origin());
243 }
244 } else {
245 transform->applyTo(&sceneTransform);
246 }
247 }
248
249 const bool hasRotation = !qFuzzyIsNull(rotation());
250 const bool hasScale = !qFuzzyCompare(scale(), 1);
251 if (hasScale || hasRotation) {
253 sceneTransform.translate(tp.x(), tp.y());
254 if (hasScale)
255 sceneTransform.scale(scale(), scale());
256 if (hasRotation)
257 sceneTransform.rotate(rotation(), 0, 0, 1);
258 sceneTransform.translate(-tp.x(), -tp.y());
259 }
260
261 const QVector3D origin(sceneTransform.map(QPointF(0, 0)));
263 const QVector3D top = QVector3D(sceneTransform.map(QPointF(0, 1))) - origin;
264
265 wantBackYFlipped = right.x() < 0;
266 wantBackXFlipped = top.y() < 0;
267
268 const QQuickFlipable::Side newSide =
270
271 if (newSide != current) {
272 current = newSide;
275 if (front) {
278 }
279 if (back) {
282 }
283 emit q->sideChanged();
284 }
285}
286
287/* Depends on the width/height of the back item, and so needs reevaulating
288 if those change.
289*/
291{
292 QTransform mat;
293 mat.translate(back->width()/2,back->height()/2);
294 if (back->width() && wantBackYFlipped)
295 mat.rotate(180, Qt::YAxis);
296 if (back->height() && wantBackXFlipped)
297 mat.rotate(180, Qt::XAxis);
298 mat.translate(-back->width()/2,-back->height()/2);
299
300 if (backTransform)
302}
303
305
306#include "qquickflipable.moc"
307#include "moc_qquickflipable_p.cpp"
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
\inmodule QtCore
Definition qpointer.h:18
QPointer< QQuickItem > front
bool transformChanged(QQuickItem *transformedItem) override
QPointer< QQuickLocalTransform > backTransform
QQuickFlipable::Side current
QPointer< QQuickItem > back
void setBack(QQuickItem *)
QQuickItem * back
QQuickItem * front
void frontChanged()
QQuickFlipable(QQuickItem *parent=nullptr)
\qmltype Flipable \instantiates QQuickFlipable \inqmlmodule QtQuick \inherits Item
void backChanged()
void updatePolish() override
This function should perform any layout as required for this item.
void setFront(QQuickItem *)
virtual bool transformChanged(QQuickItem *transformedItem)
qreal rotation() const
QList< QQuickTransform * > transforms
QQuickItem::TransformOrigin origin() const
qreal scale() const
QPointF computeTransformOrigin() const
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
void heightChanged()
void setOpacity(qreal)
void widthChanged()
void setEnabled(bool)
qreal width
This property holds the width of this item.
Definition qquickitem.h:76
qreal height
This property holds the height of this item.
Definition qquickitem.h:77
void setTransform(const QTransform &t)
void applyTo(QMatrix4x4 *matrix) const override
QQuickLocalTransform(QObject *parent)
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
QTransform & rotate(qreal a, Qt::Axis axis=Qt::ZAxis, qreal distanceToPlane=1024.0f)
QTransform & translate(qreal dx, qreal dy)
Moves the coordinate system dx along the x axis and dy along the y axis, and returns a reference to t...
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
static constexpr QVector3D crossProduct(QVector3D v1, QVector3D v2) noexcept
Returns the cross-product of vectors v1 and v2, which is normal to the plane spanned by v1 and v2.
Definition qvectornd.h:775
constexpr float z() const noexcept
Returns the z coordinate of this point.
Definition qvectornd.h:672
Combined button and popup list for selecting options.
@ XAxis
@ YAxis
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:287
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:303
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLint GLint GLint GLint GLint x
[0]
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLfloat angle
GLint y
GLuint GLenum GLenum transform
GLuint GLenum matrix
GLdouble GLdouble t
Definition qopenglext.h:243
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLbyte ty
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
#define tr(X)
#define Q_OBJECT
#define emit
double qreal
Definition qtypes.h:92
QObject::connect nullptr
rect sceneTransform().map(QPointF(0
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent