rebound property for Flickable
[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)
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 dragStartOffset;
142         qreal dragMinBound;
143         qreal dragMaxBound;
144         qreal velocity;
145         qreal flickTarget;
146         qreal startMargin;
147         qreal endMargin;
148         qreal transitionTo;
149         qreal continuousFlickVelocity;
150         QQuickFlickablePrivate::Velocity smoothVelocity;
151         QPODVector<qreal,10> velocityBuffer;
152         bool atEnd : 1;
153         bool atBeginning : 1;
154         bool transitionToSet : 1;
155         bool fixingUp : 1;
156         bool inOvershoot : 1;
157         bool moving : 1;
158         bool flicking : 1;
159         bool dragging : 1;
160         bool extentsChanged : 1;
161         bool explicitValue : 1;
162         mutable bool minExtentDirty : 1;
163         mutable bool maxExtentDirty : 1;
164     };
165
166     bool flickX(qreal velocity);
167     bool flickY(qreal velocity);
168     virtual bool flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
169                         QQuickTimeLineCallback::Callback fixupCallback, qreal velocity);
170     void flickingStarted(bool flickingH, bool flickingV);
171
172     void fixupX();
173     void fixupY();
174     virtual void fixup(AxisData &data, qreal minExtent, qreal maxExtent);
175     void adjustContentPos(AxisData &data, qreal toPos);
176     void resetTimeline(AxisData &data);
177     void clearTimeline();
178
179     void updateBeginningEnd();
180
181     bool isOutermostPressDelay() const;
182     void captureDelayedPress(QMouseEvent *event);
183     void clearDelayedPress();
184
185     void setViewportX(qreal x);
186     void setViewportY(qreal y);
187
188     qreal overShootDistance(qreal size);
189
190     void itemGeometryChanged(QQuickItem *, const QRectF &, const QRectF &);
191
192     void draggingStarting();
193     void draggingEnding();
194
195     bool isViewMoving() const;
196
197 public:
198     QQuickItem *contentItem;
199
200     AxisData hData;
201     AxisData vData;
202
203     QQuickTimeLine timeline;
204     bool hMoved : 1;
205     bool vMoved : 1;
206     bool stealMouse : 1;
207     bool pressed : 1;
208     bool interactive : 1;
209     bool calcVelocity : 1;
210     bool pixelAligned : 1;
211     QElapsedTimer timer;
212     qint64 lastPosTime;
213     qint64 lastPressTime;
214     QPointF lastPos;
215     QPointF pressPos;
216     qreal deceleration;
217     qreal maxVelocity;
218     QElapsedTimer velocityTime;
219     QPointF lastFlickablePosition;
220     qreal reportedVelocitySmoothing;
221     QMouseEvent *delayedPressEvent;
222     QQuickItem *delayedPressTarget;
223     QBasicTimer delayedPressTimer;
224     int pressDelay;
225     int fixupDuration;
226     qreal flickBoost;
227
228     enum FixupMode { Normal, Immediate, ExtentChanged };
229     FixupMode fixupMode;
230
231     static void fixupY_callback(void *);
232     static void fixupX_callback(void *);
233
234     void updateVelocity();
235     int vTime;
236     QQuickTimeLine velocityTimeline;
237     QQuickFlickableVisibleArea *visibleArea;
238     QQuickFlickable::FlickableDirection flickableDirection;
239     QQuickFlickable::BoundsBehavior boundsBehavior;
240     QQuickTransition *rebound;
241
242     void handleMousePressEvent(QMouseEvent *);
243     void handleMouseMoveEvent(QMouseEvent *);
244     void handleMouseReleaseEvent(QMouseEvent *);
245
246     qint64 computeCurrentTime(QInputEvent *event);
247
248     // flickableData property
249     static void data_append(QQmlListProperty<QObject> *, QObject *);
250     static int data_count(QQmlListProperty<QObject> *);
251     static QObject *data_at(QQmlListProperty<QObject> *, int);
252     static void data_clear(QQmlListProperty<QObject> *);
253 };
254
255 class QQuickFlickableVisibleArea : public QObject
256 {
257     Q_OBJECT
258
259     Q_PROPERTY(qreal xPosition READ xPosition NOTIFY xPositionChanged)
260     Q_PROPERTY(qreal yPosition READ yPosition NOTIFY yPositionChanged)
261     Q_PROPERTY(qreal widthRatio READ widthRatio NOTIFY widthRatioChanged)
262     Q_PROPERTY(qreal heightRatio READ heightRatio NOTIFY heightRatioChanged)
263
264 public:
265     QQuickFlickableVisibleArea(QQuickFlickable *parent=0);
266
267     qreal xPosition() const;
268     qreal widthRatio() const;
269     qreal yPosition() const;
270     qreal heightRatio() const;
271
272     void updateVisible();
273
274 signals:
275     void xPositionChanged(qreal xPosition);
276     void yPositionChanged(qreal yPosition);
277     void widthRatioChanged(qreal widthRatio);
278     void heightRatioChanged(qreal heightRatio);
279
280 private:
281     QQuickFlickable *flickable;
282     qreal m_xPosition;
283     qreal m_widthRatio;
284     qreal m_yPosition;
285     qreal m_heightRatio;
286 };
287
288 QT_END_NAMESPACE
289
290 QML_DECLARE_TYPE(QQuickFlickableVisibleArea)
291
292 #endif // QQUICKFLICKABLE_P_P_H