Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgflickable_p_p.h
1 // Commit: cb0a6844705802564c81b581f24a76c5d5adf6d1
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 #ifndef QSGFLICKABLE_P_P_H
44 #define QSGFLICKABLE_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 "qsgflickable_p.h"
58 #include "qsgitem_p.h"
59 #include "qsgitemchangelistener_p.h"
60
61 #include <QtDeclarative/qdeclarative.h>
62 #include <QtCore/qdatetime.h>
63
64 #include <private/qdeclarativetimeline_p_p.h>
65 #include <private/qdeclarativeanimation_p_p.h>
66
67 QT_BEGIN_NAMESPACE
68
69 // Really slow flicks can be annoying.
70 const qreal MinimumFlickVelocity = 75.0;
71
72 class QSGFlickableVisibleArea;
73 class QSGFlickablePrivate : public QSGItemPrivate, public QSGItemChangeListener
74 {
75     Q_DECLARE_PUBLIC(QSGFlickable)
76
77 public:
78     static inline QSGFlickablePrivate *get(QSGFlickable *o) { return o->d_func(); }
79
80     QSGFlickablePrivate();
81     void init();
82
83     struct Velocity : public QDeclarativeTimeLineValue
84     {
85         Velocity(QSGFlickablePrivate *p)
86             : parent(p) {}
87         virtual void setValue(qreal v) {
88             if (v != value()) {
89                 QDeclarativeTimeLineValue::setValue(v);
90                 parent->updateVelocity();
91             }
92         }
93         QSGFlickablePrivate *parent;
94     };
95
96     struct AxisData {
97         AxisData(QSGFlickablePrivate *fp, void (QSGFlickablePrivate::*func)(qreal))
98             : move(fp, func), viewSize(-1), smoothVelocity(fp), atEnd(false), atBeginning(true)
99             , fixingUp(false)
100         {}
101
102         QDeclarativeTimeLineValueProxy<QSGFlickablePrivate> move;
103         qreal viewSize;
104         qreal pressPos;
105         qreal dragStartOffset;
106         qreal dragMinBound;
107         qreal dragMaxBound;
108         qreal velocity;
109         qreal flickTarget;
110         QSGFlickablePrivate::Velocity smoothVelocity;
111         bool atEnd : 1;
112         bool atBeginning : 1;
113         bool fixingUp : 1;
114     };
115
116     void flickX(qreal velocity);
117     void flickY(qreal velocity);
118     virtual void flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
119                         QDeclarativeTimeLineCallback::Callback fixupCallback, qreal velocity);
120
121     void fixupX();
122     void fixupY();
123     virtual void fixup(AxisData &data, qreal minExtent, qreal maxExtent);
124
125     void updateBeginningEnd();
126
127     bool isOutermostPressDelay() const;
128     void captureDelayedPress(QGraphicsSceneMouseEvent *event);
129     void clearDelayedPress();
130
131     void setRoundedViewportX(qreal x);
132     void setRoundedViewportY(qreal y);
133
134     qreal overShootDistance(qreal velocity, qreal size);
135
136     void itemGeometryChanged(QSGItem *, const QRectF &, const QRectF &);
137
138 public:
139     QSGItem *contentItem;
140
141     AxisData hData;
142     AxisData vData;
143
144     QDeclarativeTimeLine timeline;
145     bool flickingHorizontally : 1;
146     bool flickingVertically : 1;
147     bool hMoved : 1;
148     bool vMoved : 1;
149     bool movingHorizontally : 1;
150     bool movingVertically : 1;
151     bool stealMouse : 1;
152     bool pressed : 1;
153     bool interactive : 1;
154     bool calcVelocity : 1;
155     QElapsedTimer lastPosTime;
156     QPointF lastPos;
157     QPointF pressPos;
158     QElapsedTimer pressTime;
159     qreal deceleration;
160     qreal maxVelocity;
161     QElapsedTimer velocityTime;
162     QPointF lastFlickablePosition;
163     qreal reportedVelocitySmoothing;
164     QGraphicsSceneMouseEvent *delayedPressEvent;
165     QSGItem *delayedPressTarget;
166     QBasicTimer delayedPressTimer;
167     int pressDelay;
168     int fixupDuration;
169
170     enum FixupMode { Normal, Immediate, ExtentChanged };
171     FixupMode fixupMode;
172
173     static void fixupY_callback(void *);
174     static void fixupX_callback(void *);
175
176     void updateVelocity();
177     int vTime;
178     QDeclarativeTimeLine velocityTimeline;
179     QSGFlickableVisibleArea *visibleArea;
180     QSGFlickable::FlickableDirection flickableDirection;
181     QSGFlickable::BoundsBehavior boundsBehavior;
182
183     void handleMousePressEvent(QGraphicsSceneMouseEvent *);
184     void handleMouseMoveEvent(QGraphicsSceneMouseEvent *);
185     void handleMouseReleaseEvent(QGraphicsSceneMouseEvent *);
186
187     // flickableData property
188     static void data_append(QDeclarativeListProperty<QObject> *, QObject *);
189     static int data_count(QDeclarativeListProperty<QObject> *);
190     static QObject *data_at(QDeclarativeListProperty<QObject> *, int);
191     static void data_clear(QDeclarativeListProperty<QObject> *);
192 };
193
194 class QSGFlickableVisibleArea : public QObject
195 {
196     Q_OBJECT
197
198     Q_PROPERTY(qreal xPosition READ xPosition NOTIFY xPositionChanged)
199     Q_PROPERTY(qreal yPosition READ yPosition NOTIFY yPositionChanged)
200     Q_PROPERTY(qreal widthRatio READ widthRatio NOTIFY widthRatioChanged)
201     Q_PROPERTY(qreal heightRatio READ heightRatio NOTIFY heightRatioChanged)
202
203 public:
204     QSGFlickableVisibleArea(QSGFlickable *parent=0);
205
206     qreal xPosition() const;
207     qreal widthRatio() const;
208     qreal yPosition() const;
209     qreal heightRatio() const;
210
211     void updateVisible();
212
213 signals:
214     void xPositionChanged(qreal xPosition);
215     void yPositionChanged(qreal yPosition);
216     void widthRatioChanged(qreal widthRatio);
217     void heightRatioChanged(qreal heightRatio);
218
219 private:
220     QSGFlickable *flickable;
221     qreal m_xPosition;
222     qreal m_widthRatio;
223     qreal m_yPosition;
224     qreal m_heightRatio;
225 };
226
227 QT_END_NAMESPACE
228
229 QML_DECLARE_TYPE(QSGFlickableVisibleArea)
230
231 #endif // QSGFLICKABLE_P_P_H