Qt 4.8
qdeclarativeflipable.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativeflipable_p.h"
43 
44 #include "private/qdeclarativeitem_p.h"
45 #include "private/qdeclarativeguard_p.h"
46 
47 #include <qdeclarativeinfo.h>
48 
49 #include <QtGui/qgraphicstransform.h>
50 
52 
54 {
56 public:
58 
60  void setBackTransform();
61 
65 
68 };
69 
117 {
118 }
119 
121 {
122 }
123 
135 {
136  Q_D(const QDeclarativeFlipable);
137  return d->front;
138 }
139 
141 {
143  if (d->front) {
144  qmlInfo(this) << tr("front is a write-once property");
145  return;
146  }
147  d->front = front;
148  d->front->setParentItem(this);
149  if (Back == d->current)
150  d->front->setOpacity(0.);
151  emit frontChanged();
152 }
153 
155 {
156  Q_D(const QDeclarativeFlipable);
157  return d->back;
158 }
159 
161 {
163  if (d->back) {
164  qmlInfo(this) << tr("back is a write-once property");
165  return;
166  }
167  d->back = back;
168  d->back->setParentItem(this);
169  if (Front == d->current)
170  d->back->setOpacity(0.);
171  connect(back, SIGNAL(widthChanged()),
172  this, SLOT(retransformBack()));
173  connect(back, SIGNAL(heightChanged()),
174  this, SLOT(retransformBack()));
175  emit backChanged();
176 }
177 
179 {
181  if (d->current == QDeclarativeFlipable::Back && d->back)
182  d->setBackTransform();
183 }
184 
195 {
196  Q_D(const QDeclarativeFlipable);
197  if (d->dirtySceneTransform)
198  const_cast<QDeclarativeFlipablePrivate *>(d)->ensureSceneTransform();
199 
200  return d->current;
201 }
202 
203 // determination on the currently visible side of the flipable
204 // has to be done on the complete scene transform to give
205 // correct results.
207 {
209 
211  QPointF p1(0, 0);
212  QPointF p2(1, 0);
213  QPointF p3(1, 1);
214 
215  QPointF scenep1 = sceneTransform.map(p1);
216  QPointF scenep2 = sceneTransform.map(p2);
217  QPointF scenep3 = sceneTransform.map(p3);
218  p1 = q->mapToParent(p1);
219  p2 = q->mapToParent(p2);
220  p3 = q->mapToParent(p3);
221 
222  qreal cross = (scenep1.x() - scenep2.x()) * (scenep3.y() - scenep2.y()) -
223  (scenep1.y() - scenep2.y()) * (scenep3.x() - scenep2.x());
224 
225  wantBackYFlipped = p1.x() >= p2.x();
226  wantBackXFlipped = p2.y() >= p3.y();
227 
229  if (cross > 0) {
230  newSide = QDeclarativeFlipable::Back;
231  } else {
232  newSide = QDeclarativeFlipable::Front;
233  }
234 
235  if (newSide != current) {
236  current = newSide;
237  if (current == QDeclarativeFlipable::Back && back)
238  setBackTransform();
239  if (front)
241  if (back)
243  emit q->sideChanged();
244  }
245 }
246 
247 /* Depends on the width/height of the back item, and so needs reevaulating
248  if those change.
249 */
251 {
252  QTransform mat;
254  mat.translate(dBack->width()/2,dBack->height()/2);
255  if (dBack->width() && wantBackYFlipped)
256  mat.rotate(180, Qt::YAxis);
257  if (dBack->height() && wantBackXFlipped)
258  mat.rotate(180, Qt::XAxis);
259  mat.translate(-dBack->width()/2,-dBack->height()/2);
260  back->setTransform(mat);
261 }
262 
QGraphicsObject * front()
QGraphicsItem * parent
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setBack(QGraphicsObject *)
QDeclarativeParserStatus ** d
void setTransform(const QTransform &matrix, bool combine=false)
Sets the item&#39;s current transformation matrix to matrix.
QTransform sceneTransform() const
Returns this item&#39;s scene transformation matrix.
void heightChanged()
virtual qreal height() const
virtual void updateSceneTransformFromParent()
#define SLOT(a)
Definition: qobjectdefs.h:226
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
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...
Definition: qtransform.cpp:417
#define Q_D(Class)
Definition: qglobal.h:2482
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
#define Q_Q(Class)
Definition: qglobal.h:2483
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QDeclarativeItem class provides the most basic of all visual items in QML.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
void setFront(QGraphicsObject *)
QTransform & rotate(qreal a, Qt::Axis axis=Qt::ZAxis)
Rotates the coordinate system counterclockwise by the given angle about the specified axis and return...
Definition: qtransform.cpp:615
#define emit
Definition: qobjectdefs.h:76
QPoint map(const QPoint &p) const
Creates and returns a QPoint object that is a copy of the given point, mapped into the coordinate sys...
QGraphicsObject * back()
QDeclarativeGuard< QGraphicsObject > back
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
virtual qreal width() const
QDeclarativeFlipable::Side current
static const QGraphicsItemPrivate * get(const QGraphicsItem *item)
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
QDeclarativeFlipable(QDeclarativeItem *parent=0)
QDeclarativeInfo qmlInfo(const QObject *me)
void setOpacity(qreal opacity)
Sets this item&#39;s local opacity, between 0.
The QGraphicsObject class provides a base class for all graphics items that require signals...
QDeclarativeGuard< QGraphicsObject > front
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65