Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
10#include <QtCore/qpointer.h>
11
13
14// XXX todo - i think this needs work and a bit of a re-think
15
17{
19public:
21
22 void setTransform(const QTransform &t) {
23 transform = t;
24 update();
25 }
26 void applyTo(QMatrix4x4 *matrix) const override {
27 *matrix *= transform;
28 }
29private:
31};
32
34{
35 Q_DECLARE_PUBLIC(QQuickFlipable)
36public:
38
39 bool transformChanged(QQuickItem *transformedItem) override;
40 void updateSide();
41 void setBackTransform();
42
44 QPointer<QQuickLocalTransform> backTransform;
45 QPointer<QQuickItem> front;
46 QPointer<QQuickItem> back;
47
51};
52
100
104
113{
114 Q_D(const QQuickFlipable);
115 return d->front;
116}
117
119{
120 Q_D(QQuickFlipable);
121 if (d->front) {
122 qmlWarning(this) << tr("front is a write-once property");
123 return;
124 }
125 d->front = front;
126 d->front->setParentItem(this);
127 if (Back == d->current) {
128 d->front->setOpacity(0.);
129 d->front->setEnabled(false);
130 }
132}
133
135{
136 Q_D(const QQuickFlipable);
137 return d->back;
138}
139
141{
142 Q_D(QQuickFlipable);
143 if (d->back) {
144 qmlWarning(this) << tr("back is a write-once property");
145 return;
146 }
147 if (back == nullptr)
148 return;
149 d->back = back;
150 d->back->setParentItem(this);
151
152 d->backTransform = new QQuickLocalTransform(d->back);
153 d->backTransform->prependToItem(d->back);
154
155 if (Front == d->current) {
156 d->back->setOpacity(0.);
157 d->back->setEnabled(false);
158 }
159
161 this, SLOT(retransformBack()));
163 this, SLOT(retransformBack()));
165}
166
167void QQuickFlipable::retransformBack()
168{
169 Q_D(QQuickFlipable);
170 if (d->current == QQuickFlipable::Back && d->back)
171 d->setBackTransform();
172}
173
181{
182 Q_D(const QQuickFlipable);
183
184 const_cast<QQuickFlipablePrivate *>(d)->updateSide();
185 return d->current;
186}
187
189{
190 Q_Q(QQuickFlipable);
191
192 if (!sideDirty) {
193 sideDirty = true;
194 q->polish();
195 }
196
197 return QQuickItemPrivate::transformChanged(transformedItem);
198}
199
201{
202 Q_D(QQuickFlipable);
203 d->updateSide();
204}
205
221{
222 Q_Q(QQuickFlipable);
223
224 if (!sideDirty)
225 return;
226
227 sideDirty = false;
228
230
231 const qreal tx = x.value();
232 const qreal ty = y.value();
233 if (!qFuzzyIsNull(tx) || !qFuzzyIsNull(ty))
235
236 for (const auto *transform : std::as_const(transforms)) {
237 if (const auto *rot = qobject_cast<const QQuickRotation *>(transform)) {
238 // rotation is a special case: we want to call rotate() instead of projectedRotate()
239 const auto angle = rot->angle();
240 const auto axis = rot->axis();
241 if (!(qFuzzyIsNull(angle) || axis.isNull())) {
242 sceneTransform.translate(rot->origin());
243 sceneTransform.rotate(angle, axis.x(), axis.y(), axis.z());
244 sceneTransform.translate(-rot->origin());
245 }
246 } else {
247 transform->applyTo(&sceneTransform);
248 }
249 }
250
251 const bool hasRotation = !qFuzzyIsNull(rotation());
252 const bool hasScale = !qFuzzyCompare(scale(), 1);
253 if (hasScale || hasRotation) {
255 sceneTransform.translate(tp.x(), tp.y());
256 if (hasScale)
257 sceneTransform.scale(scale(), scale());
258 if (hasRotation)
259 sceneTransform.rotate(rotation(), 0, 0, 1);
260 sceneTransform.translate(-tp.x(), -tp.y());
261 }
262
263 const QVector3D origin(sceneTransform.map(QPointF(0, 0)));
265 const QVector3D top = QVector3D(sceneTransform.map(QPointF(0, 1))) - origin;
266
267 wantBackYFlipped = right.x() < 0;
268 wantBackXFlipped = top.y() < 0;
269
270 const QQuickFlipable::Side newSide =
272
273 if (newSide != current) {
274 current = newSide;
277 if (front) {
280 }
281 if (back) {
284 }
285 emit q->sideChanged();
286 }
287}
288
289/* Depends on the width/height of the back item, and so needs reevaulating
290 if those change.
291*/
293{
294 QTransform mat;
295 mat.translate(back->width()/2,back->height()/2);
296 if (back->width() && wantBackYFlipped)
297 mat.rotate(180, Qt::YAxis);
298 if (back->height() && wantBackXFlipped)
299 mat.rotate(180, Qt::XAxis);
300 mat.translate(-back->width()/2,-back->height()/2);
301
302 if (backTransform)
304}
305
307
308#include "qquickflipable.moc"
309#include "moc_qquickflipable_p.cpp"
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
void translate(const QVector3D &vector)
Multiplies this matrix by another that translates coordinates by the components of vector.
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
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:63
void heightChanged()
void setOpacity(qreal)
void widthChanged()
void setEnabled(bool)
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
qreal height
This property holds the height of this item.
Definition qquickitem.h:76
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
constexpr float x() const noexcept
Returns the x coordinate of this point.
Definition qvectornd.h:670
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
Combined button and popup list for selecting options.
@ XAxis
@ YAxis
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:349
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
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:187
QObject::connect nullptr
rect sceneTransform().map(QPointF(0