Fix emitting originYChanged signals even when originY wouldn't change
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickflickable_p_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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             , origin(0)
106             , transitionTo(0)
107             , continuousFlickVelocity(0), vTime(0)
108             , smoothVelocity(fp), atEnd(false), atBeginning(true)
109             , transitionToSet(false)
110             , fixingUp(false), inOvershoot(false), moving(false), flicking(false)
111             , dragging(false), extentsChanged(false)
112             , explicitValue(false), minExtentDirty(true), maxExtentDirty(true)
113         {}
114
115         ~AxisData();
116
117         void reset() {
118             velocityBuffer.clear();
119             dragStartOffset = 0;
120             fixingUp = false;
121             inOvershoot = false;
122         }
123
124         void markExtentsDirty() {
125             minExtentDirty = true;
126             maxExtentDirty = true;
127             extentsChanged = true;
128         }
129
130         void resetTransitionTo() {
131             transitionTo = 0;
132             transitionToSet = false;
133         }
134
135         void addVelocitySample(qreal v, qreal maxVelocity);
136         void updateVelocity();
137
138         QQuickTimeLineValueProxy<QQuickFlickablePrivate> move;
139         QQuickFlickableReboundTransition *transitionToBounds;
140         qreal viewSize;
141         qreal pressPos;
142         qreal lastPos;
143         qreal dragStartOffset;
144         qreal dragMinBound;
145         qreal dragMaxBound;
146         qreal velocity;
147         qreal flickTarget;
148         qreal startMargin;
149         qreal endMargin;
150         qreal origin;
151         qreal transitionTo;
152         qreal continuousFlickVelocity;
153         QElapsedTimer velocityTime;
154         int vTime;
155         QQuickFlickablePrivate::Velocity smoothVelocity;
156         QPODVector<qreal,10> velocityBuffer;
157         bool atEnd : 1;
158         bool atBeginning : 1;
159         bool transitionToSet : 1;
160         bool fixingUp : 1;
161         bool inOvershoot : 1;
162         bool moving : 1;
163         bool flicking : 1;
164         bool dragging : 1;
165         bool extentsChanged : 1;
166         bool explicitValue : 1;
167         mutable bool minExtentDirty : 1;
168         mutable bool maxExtentDirty : 1;
169     };
170
171     bool flickX(qreal velocity);
172     bool flickY(qreal velocity);
173     virtual bool flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
174                         QQuickTimeLineCallback::Callback fixupCallback, qreal velocity);
175     void flickingStarted(bool flickingH, bool flickingV);
176
177     void fixupX();
178     void fixupY();
179     virtual void fixup(AxisData &data, qreal minExtent, qreal maxExtent);
180     void adjustContentPos(AxisData &data, qreal toPos);
181     void resetTimeline(AxisData &data);
182     void clearTimeline();
183
184     void updateBeginningEnd();
185
186     bool isOutermostPressDelay() const;
187     void captureDelayedPress(QMouseEvent *event);
188     void clearDelayedPress();
189
190     void setViewportX(qreal x);
191     void setViewportY(qreal y);
192
193     qreal overShootDistance(qreal size);
194
195     void itemGeometryChanged(QQuickItem *, const QRectF &, const QRectF &);
196
197     void draggingStarting();
198     void draggingEnding();
199
200     bool isViewMoving() const;
201
202 public:
203     QQuickItem *contentItem;
204
205     AxisData hData;
206     AxisData vData;
207
208     QQuickTimeLine timeline;
209     bool hMoved : 1;
210     bool vMoved : 1;
211     bool stealMouse : 1;
212     bool pressed : 1;
213     bool interactive : 1;
214     bool calcVelocity : 1;
215     bool pixelAligned : 1;
216     QElapsedTimer timer;
217     qint64 lastPosTime;
218     qint64 lastPressTime;
219     QPointF lastPos;
220     QPointF pressPos;
221     qreal deceleration;
222     qreal maxVelocity;
223     qreal reportedVelocitySmoothing;
224     QMouseEvent *delayedPressEvent;
225     QQuickItem *delayedPressTarget;
226     QBasicTimer delayedPressTimer;
227     int pressDelay;
228     int fixupDuration;
229     qreal flickBoost;
230
231     enum FixupMode { Normal, Immediate, ExtentChanged };
232     FixupMode fixupMode;
233
234     static void fixupY_callback(void *);
235     static void fixupX_callback(void *);
236
237     void updateVelocity();
238     int vTime;
239     QQuickTimeLine velocityTimeline;
240     QQuickFlickableVisibleArea *visibleArea;
241     QQuickFlickable::FlickableDirection flickableDirection;
242     QQuickFlickable::BoundsBehavior boundsBehavior;
243     QQuickTransition *rebound;
244
245     void viewportAxisMoved(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
246                        QQuickTimeLineCallback::Callback fixupCallback);
247
248     void handleMousePressEvent(QMouseEvent *);
249     void handleMouseMoveEvent(QMouseEvent *);
250     void handleMouseReleaseEvent(QMouseEvent *);
251
252     qint64 computeCurrentTime(QInputEvent *event);
253
254     // flickableData property
255     static void data_append(QQmlListProperty<QObject> *, QObject *);
256     static int data_count(QQmlListProperty<QObject> *);
257     static QObject *data_at(QQmlListProperty<QObject> *, int);
258     static void data_clear(QQmlListProperty<QObject> *);
259 };
260
261 class QQuickFlickableVisibleArea : public QObject
262 {
263     Q_OBJECT
264
265     Q_PROPERTY(qreal xPosition READ xPosition NOTIFY xPositionChanged)
266     Q_PROPERTY(qreal yPosition READ yPosition NOTIFY yPositionChanged)
267     Q_PROPERTY(qreal widthRatio READ widthRatio NOTIFY widthRatioChanged)
268     Q_PROPERTY(qreal heightRatio READ heightRatio NOTIFY heightRatioChanged)
269
270 public:
271     QQuickFlickableVisibleArea(QQuickFlickable *parent=0);
272
273     qreal xPosition() const;
274     qreal widthRatio() const;
275     qreal yPosition() const;
276     qreal heightRatio() const;
277
278     void updateVisible();
279
280 signals:
281     void xPositionChanged(qreal xPosition);
282     void yPositionChanged(qreal yPosition);
283     void widthRatioChanged(qreal widthRatio);
284     void heightRatioChanged(qreal heightRatio);
285
286 private:
287     QQuickFlickable *flickable;
288     qreal m_xPosition;
289     qreal m_widthRatio;
290     qreal m_yPosition;
291     qreal m_heightRatio;
292 };
293
294 QT_END_NAMESPACE
295
296 QML_DECLARE_TYPE(QQuickFlickableVisibleArea)
297
298 #endif // QQUICKFLICKABLE_P_P_H