Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgflipable.cpp
1 // Commit: caee66da925949cf7aef2ff8e1a86c38dd6e6efd
2 /****************************************************************************
3 **
4 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 **
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 **
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
15 ** this package.
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, 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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 **
39 ** $QT_END_LICENSE$
40 **
41 ****************************************************************************/
42
43 #include "qsgflipable_p.h"
44 #include "qsgitem_p.h"
45
46 #include <private/qdeclarativeguard_p.h>
47
48 #include <QtDeclarative/qdeclarativeinfo.h>
49
50 QT_BEGIN_NAMESPACE
51
52 // XXX todo - i think this needs work and a bit of a re-think
53
54 class QSGLocalTransform : public QSGTransform
55 {
56     Q_OBJECT
57 public:
58     QSGLocalTransform(QObject *parent) : QSGTransform(parent) {}
59
60     void setTransform(const QTransform &t) {
61         transform = t;
62         update();
63     }
64     virtual void applyTo(QMatrix4x4 *matrix) const {
65         *matrix *= transform;
66     }
67 private:
68     QTransform transform;
69 };
70
71 class QSGFlipablePrivate : public QSGItemPrivate
72 {
73     Q_DECLARE_PUBLIC(QSGFlipable)
74 public:
75     QSGFlipablePrivate() : current(QSGFlipable::Front), front(0), back(0), sideDirty(false) {}
76
77     virtual void transformChanged();
78     void updateSide();
79     void setBackTransform();
80
81     QSGFlipable::Side current;
82     QDeclarativeGuard<QSGLocalTransform> backTransform;
83     QDeclarativeGuard<QSGItem> front;
84     QDeclarativeGuard<QSGItem> back;
85
86     bool sideDirty;
87     bool wantBackXFlipped;
88     bool wantBackYFlipped;
89 };
90
91 QSGFlipable::QSGFlipable(QSGItem *parent)
92 : QSGItem(*(new QSGFlipablePrivate), parent)
93 {
94 }
95
96 QSGFlipable::~QSGFlipable()
97 {
98 }
99
100 QSGItem *QSGFlipable::front()
101 {
102     Q_D(const QSGFlipable);
103     return d->front;
104 }
105
106 void QSGFlipable::setFront(QSGItem *front)
107 {
108     Q_D(QSGFlipable);
109     if (d->front) {
110         qmlInfo(this) << tr("front is a write-once property");
111         return;
112     }
113     d->front = front;
114     d->front->setParentItem(this);
115     if (Back == d->current)
116         d->front->setOpacity(0.);
117     emit frontChanged();
118 }
119
120 QSGItem *QSGFlipable::back()
121 {
122     Q_D(const QSGFlipable);
123     return d->back;
124 }
125
126 void QSGFlipable::setBack(QSGItem *back)
127 {
128     Q_D(QSGFlipable);
129     if (d->back) {
130         qmlInfo(this) << tr("back is a write-once property");
131         return;
132     }
133     if (back == 0)
134         return;
135     d->back = back;
136     d->back->setParentItem(this);
137
138     d->backTransform = new QSGLocalTransform(d->back);
139     d->backTransform->prependToItem(d->back);
140
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()));
147     emit backChanged();
148 }
149
150 void QSGFlipable::retransformBack()
151 {
152     Q_D(QSGFlipable);
153     if (d->current == QSGFlipable::Back && d->back)
154         d->setBackTransform();
155 }
156
157 QSGFlipable::Side QSGFlipable::side() const
158 {
159     Q_D(const QSGFlipable);
160
161     const_cast<QSGFlipablePrivate *>(d)->updateSide();
162     return d->current;
163 }
164
165 void QSGFlipablePrivate::transformChanged()
166 {
167     Q_Q(QSGFlipable);
168
169     if (!sideDirty) {
170         sideDirty = true;
171         q->polish();
172     }
173
174     QSGItemPrivate::transformChanged();
175 }
176
177 void QSGFlipable::updatePolish()
178 {
179     Q_D(QSGFlipable);
180     d->updateSide();
181 }
182
183 // determination on the currently visible side of the flipable
184 // has to be done on the complete scene transform to give
185 // correct results.
186 void QSGFlipablePrivate::updateSide()
187 {
188     Q_Q(QSGFlipable);
189
190     if (!sideDirty)
191         return;
192
193     sideDirty = false;
194
195     QTransform sceneTransform;
196     itemToParentTransform(sceneTransform);
197
198     QPointF p1(0, 0);
199     QPointF p2(1, 0);
200     QPointF p3(1, 1);
201
202     QPointF scenep1 = sceneTransform.map(p1);
203     QPointF scenep2 = sceneTransform.map(p2);
204     QPointF scenep3 = sceneTransform.map(p3);
205 #if 0
206     p1 = q->mapToParent(p1);
207     p2 = q->mapToParent(p2);
208     p3 = q->mapToParent(p3);
209 #endif
210
211     qreal cross = (scenep1.x() - scenep2.x()) * (scenep3.y() - scenep2.y()) -
212                   (scenep1.y() - scenep2.y()) * (scenep3.x() - scenep2.x());
213
214     wantBackYFlipped = scenep1.x() >= scenep2.x();
215     wantBackXFlipped = scenep2.y() >= scenep3.y();
216
217     QSGFlipable::Side newSide;
218     if (cross > 0) {
219         newSide = QSGFlipable::Back;
220     } else {
221         newSide = QSGFlipable::Front;
222     }
223
224     if (newSide != current) {
225         current = newSide;
226         if (current == QSGFlipable::Back && back)
227             setBackTransform();
228         if (front)
229             front->setOpacity((current==QSGFlipable::Front)?1.:0.);
230         if (back)
231             back->setOpacity((current==QSGFlipable::Back)?1.:0.);
232         emit q->sideChanged();
233     }
234 }
235
236 /* Depends on the width/height of the back item, and so needs reevaulating
237    if those change.
238 */
239 void QSGFlipablePrivate::setBackTransform()
240 {
241     QTransform mat;
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);
248
249     if (backTransform)
250         backTransform->setTransform(mat);
251 }
252
253 QT_END_NAMESPACE
254
255 #include "qsgflipable.moc"