1 // Commit: caee66da925949cf7aef2ff8e1a86c38dd6e6efd
2 /****************************************************************************
4 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** No Commercial Usage
12 ** This file contains pre-release code and may not be distributed.
13 ** You may use this file in accordance with the terms and conditions
14 ** contained in the Technology Preview License Agreement accompanying
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.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
41 ****************************************************************************/
43 #include "qsgflipable_p.h"
44 #include "qsgitem_p.h"
46 #include <private/qdeclarativeguard_p.h>
48 #include <QtDeclarative/qdeclarativeinfo.h>
52 // XXX todo - i think this needs work and a bit of a re-think
54 class QSGLocalTransform : public QSGTransform
58 QSGLocalTransform(QObject *parent) : QSGTransform(parent) {}
60 void setTransform(const QTransform &t) {
64 virtual void applyTo(QMatrix4x4 *matrix) const {
71 class QSGFlipablePrivate : public QSGItemPrivate
73 Q_DECLARE_PUBLIC(QSGFlipable)
75 QSGFlipablePrivate() : current(QSGFlipable::Front), front(0), back(0), sideDirty(false) {}
77 virtual void transformChanged();
79 void setBackTransform();
81 QSGFlipable::Side current;
82 QDeclarativeGuard<QSGLocalTransform> backTransform;
83 QDeclarativeGuard<QSGItem> front;
84 QDeclarativeGuard<QSGItem> back;
87 bool wantBackXFlipped;
88 bool wantBackYFlipped;
91 QSGFlipable::QSGFlipable(QSGItem *parent)
92 : QSGItem(*(new QSGFlipablePrivate), parent)
96 QSGFlipable::~QSGFlipable()
100 QSGItem *QSGFlipable::front()
102 Q_D(const QSGFlipable);
106 void QSGFlipable::setFront(QSGItem *front)
110 qmlInfo(this) << tr("front is a write-once property");
114 d->front->setParentItem(this);
115 if (Back == d->current)
116 d->front->setOpacity(0.);
120 QSGItem *QSGFlipable::back()
122 Q_D(const QSGFlipable);
126 void QSGFlipable::setBack(QSGItem *back)
130 qmlInfo(this) << tr("back is a write-once property");
136 d->back->setParentItem(this);
138 d->backTransform = new QSGLocalTransform(d->back);
139 d->backTransform->prependToItem(d->back);
141 if (Front == d->current)
142 d->back->setOpacity(0.);
143 connect(back, SIGNAL(widthChanged()),
144 this, SLOT(retransformBack()));
145 connect(back, SIGNAL(heightChanged()),
146 this, SLOT(retransformBack()));
150 void QSGFlipable::retransformBack()
153 if (d->current == QSGFlipable::Back && d->back)
154 d->setBackTransform();
157 QSGFlipable::Side QSGFlipable::side() const
159 Q_D(const QSGFlipable);
161 const_cast<QSGFlipablePrivate *>(d)->updateSide();
165 void QSGFlipablePrivate::transformChanged()
174 QSGItemPrivate::transformChanged();
177 void QSGFlipable::updatePolish()
183 // determination on the currently visible side of the flipable
184 // has to be done on the complete scene transform to give
186 void QSGFlipablePrivate::updateSide()
195 QTransform sceneTransform;
196 itemToParentTransform(sceneTransform);
202 QPointF scenep1 = sceneTransform.map(p1);
203 QPointF scenep2 = sceneTransform.map(p2);
204 QPointF scenep3 = sceneTransform.map(p3);
206 p1 = q->mapToParent(p1);
207 p2 = q->mapToParent(p2);
208 p3 = q->mapToParent(p3);
211 qreal cross = (scenep1.x() - scenep2.x()) * (scenep3.y() - scenep2.y()) -
212 (scenep1.y() - scenep2.y()) * (scenep3.x() - scenep2.x());
214 wantBackYFlipped = scenep1.x() >= scenep2.x();
215 wantBackXFlipped = scenep2.y() >= scenep3.y();
217 QSGFlipable::Side newSide;
219 newSide = QSGFlipable::Back;
221 newSide = QSGFlipable::Front;
224 if (newSide != current) {
226 if (current == QSGFlipable::Back && back)
229 front->setOpacity((current==QSGFlipable::Front)?1.:0.);
231 back->setOpacity((current==QSGFlipable::Back)?1.:0.);
232 emit q->sideChanged();
236 /* Depends on the width/height of the back item, and so needs reevaulating
239 void QSGFlipablePrivate::setBackTransform()
242 mat.translate(back->width()/2,back->height()/2);
243 if (back->width() && wantBackYFlipped)
244 mat.rotate(180, Qt::YAxis);
245 if (back->height() && wantBackXFlipped)
246 mat.rotate(180, Qt::XAxis);
247 mat.translate(-back->width()/2,-back->height()/2);
250 backTransform->setTransform(mat);
255 #include "qsgflipable.moc"