Update to 5.0.0-beta1
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickflickable_p_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQUICKFLICKABLE_P_P_H
43 #define QQUICKFLICKABLE_P_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "qquickflickable_p.h"
57 #include "qquickitem_p.h"
58 #include "qquickitemchangelistener_p.h"
59
60 #include <QtQml/qqml.h>
61 #include <QtCore/qdatetime.h>
62 #include "qplatformdefs.h"
63
64 #include <private/qquicktimeline_p_p.h>
65 #include <private/qquickanimation_p_p.h>
66 #include <private/qquicktransitionmanager_p_p.h>
67
68 QT_BEGIN_NAMESPACE
69
70 // Really slow flicks can be annoying.
71 const qreal MinimumFlickVelocity = 75.0;
72
73 class QQuickFlickableVisibleArea;
74 class QQuickTransition;
75 class QQuickFlickableReboundTransition;
76
77 class Q_AUTOTEST_EXPORT QQuickFlickablePrivate : public QQuickItemPrivate, public QQuickItemChangeListener
78 {
79     Q_DECLARE_PUBLIC(QQuickFlickable)
80
81 public:
82     static inline QQuickFlickablePrivate *get(QQuickFlickable *o) { return o->d_func(); }
83
84     QQuickFlickablePrivate();
85     void init();
86
87     struct Velocity : public QQuickTimeLineValue
88     {
89         Velocity(QQuickFlickablePrivate *p)
90             : parent(p) {}
91         virtual void setValue(qreal v) {
92             if (v != value()) {
93                 QQuickTimeLineValue::setValue(v);
94                 parent->updateVelocity();
95             }
96         }
97         QQuickFlickablePrivate *parent;
98     };
99
100     struct AxisData {
101         AxisData(QQuickFlickablePrivate *fp, void (QQuickFlickablePrivate::*func)(qreal))
102             : move(fp, func)
103             , transitionToBounds(0)
104             , viewSize(-1), startMargin(0), endMargin(0)
105             , transitionTo(0)
106             , continuousFlickVelocity(0), vTime(0)
107             , smoothVelocity(fp), atEnd(false), atBeginning(true)
108             , transitionToSet(false)
109             , fixingUp(false), inOvershoot(false), moving(false), flicking(false)
110             , dragging(false), extentsChanged(false)
111             , explicitValue(false), minExtentDirty(true), maxExtentDirty(true)
112         {}
113
114         ~AxisData();
115
116         void reset() {
117             velocityBuffer.clear();
118             dragStartOffset = 0;
119             fixingUp = false;
120             inOvershoot = false;
121         }
122
123         void markExtentsDirty() {
124             minExtentDirty = true;
125             maxExtentDirty = true;
126             extentsChanged = true;
127         }
128
129         void resetTransitionTo() {
130             transitionTo = 0;
131             transitionToSet = false;
132         }
133
134         void addVelocitySample(qreal v, qreal maxVelocity);
135         void updateVelocity();
136
137         QQuickTimeLineValueProxy<QQuickFlickablePrivate> move;
138         QQuickFlickableReboundTransition *transitionToBounds;
139         qreal viewSize;
140         qreal pressPos;
141         qreal lastPos;
142         qreal dragStartOffset;
143         qreal dragMinBound;
144         qreal dragMaxBound;
145         qreal velocity;
146         qreal flickTarget;
147         qreal startMargin;
148         qreal endMargin;
149         qreal transitionTo;
150         qreal continuousFlickVelocity;
151         QElapsedTimer velocityTime;
152         int vTime;
153         QQuickFlickablePrivate::Velocity smoothVelocity;
154         QPODVector<qreal,10> velocityBuffer;
155         bool atEnd : 1;
156         bool atBeginning : 1;
157         bool transitionToSet : 1;
158         bool fixingUp : 1;
159         bool inOvershoot : 1;
160         bool moving : 1;
161         bool flicking : 1;
162         bool dragging : 1;
163         bool extentsChanged : 1;
164         bool explicitValue : 1;
165         mutable bool minExtentDirty : 1;
166         mutable bool maxExtentDirty : 1;
167     };
168
169     bool flickX(qreal velocity);
170     bool flickY(qreal velocity);
171     virtual bool flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
172                         QQuickTimeLineCallback::Callback fixupCallback, qreal velocity);
173     void flickingStarted(bool flickingH, bool flickingV);
174
175     void fixupX();
176     void fixupY();
177     virtual void fixup(AxisData &data, qreal minExtent, qreal maxExtent);
178     void adjustContentPos(AxisData &data, qreal toPos);
179     void resetTimeline(AxisData &data);
180     void clearTimeline();
181
182     void updateBeginningEnd();
183
184     bool isOutermostPressDelay() const;
185     void captureDelayedPress(QMouseEvent *event);
186     void clearDelayedPress();
187
188     void setViewportX(qreal x);
189     void setViewportY(qreal y);
190
191     qreal overShootDistance(qreal size);
192
193     void itemGeometryChanged(QQuickItem *, const QRectF &, const QRectF &);
194
195     void draggingStarting();
196     void draggingEnding();
197
198     bool isViewMoving() const;
199
200 public:
201     QQuickItem *contentItem;
202
203     AxisData hData;
204     AxisData vData;
205
206     QQuickTimeLine timeline;
207     bool hMoved : 1;
208     bool vMoved : 1;
209     bool stealMouse : 1;
210     bool pressed : 1;
211     bool interactive : 1;
212     bool calcVelocity : 1;
213     bool pixelAligned : 1;
214     QElapsedTimer timer;
215     qint64 lastPosTime;
216     qint64 lastPressTime;
217     QPointF lastPos;
218     QPointF pressPos;
219     qreal deceleration;
220     qreal maxVelocity;
221     qreal reportedVelocitySmoothing;
222     QMouseEvent *delayedPressEvent;
223     QQuickItem *delayedPressTarget;
224     QBasicTimer delayedPressTimer;
225     int pressDelay;
226     int fixupDuration;
227     qreal flickBoost;
228
229     enum FixupMode { Normal, Immediate, ExtentChanged };
230     FixupMode fixupMode;
231
232     static void fixupY_callback(void *);
233     static void fixupX_callback(void *);
234
235     void updateVelocity();
236     int vTime;
237     QQuickTimeLine velocityTimeline;
238     QQuickFlickableVisibleArea *visibleArea;
239     QQuickFlickable::FlickableDirection flickableDirection;
240     QQuickFlickable::BoundsBehavior boundsBehavior;
241     QQuickTransition *rebound;
242
243     void viewportAxisMoved(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
244                        QQuickTimeLineCallback::Callback fixupCallback);
245
246     void handleMousePressEvent(QMouseEvent *);
247     void handleMouseMoveEvent(QMouseEvent *);
248     void handleMouseReleaseEvent(QMouseEvent *);
249
250     qint64 computeCurrentTime(QInputEvent *event);
251
252     // flickableData property
253     static void data_append(QQmlListProperty<QObject> *, QObject *);
254     static int data_count(QQmlListProperty<QObject> *);
255     static QObject *data_at(QQmlListProperty<QObject> *, int);
256     static void data_clear(QQmlListProperty<QObject> *);
257 };
258
259 class QQuickFlickableVisibleArea : public QObject
260 {
261     Q_OBJECT
262
263     Q_PROPERTY(qreal xPosition READ xPosition NOTIFY xPositionChanged)
264     Q_PROPERTY(qreal yPosition READ yPosition NOTIFY yPositionChanged)
265     Q_PROPERTY(qreal widthRatio READ widthRatio NOTIFY widthRatioChanged)
266     Q_PROPERTY(qreal heightRatio READ heightRatio NOTIFY heightRatioChanged)
267
268 public:
269     QQuickFlickableVisibleArea(QQuickFlickable *parent=0);
270
271     qreal xPosition() const;
272     qreal widthRatio() const;
273     qreal yPosition() const;
274     qreal heightRatio() const;
275
276     void updateVisible();
277
278 signals:
279     void xPositionChanged(qreal xPosition);
280     void yPositionChanged(qreal yPosition);
281     void widthRatioChanged(qreal widthRatio);
282     void heightRatioChanged(qreal heightRatio);
283
284 private:
285     QQuickFlickable *flickable;
286     qreal m_xPosition;
287     qreal m_widthRatio;
288     qreal m_yPosition;
289     qreal m_heightRatio;
290 };
291
292 QT_END_NAMESPACE
293
294 QML_DECLARE_TYPE(QQuickFlickableVisibleArea)
295
296 #endif // QQUICKFLICKABLE_P_P_H