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