Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / graphicsitems / qdeclarativeflickable_p_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEFLICKABLE_P_H
43 #define QDECLARATIVEFLICKABLE_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 "private/qdeclarativeflickable_p.h"
57
58 #include "private/qdeclarativeitem_p.h"
59 #include "private/qdeclarativeitemchangelistener_p.h"
60
61 #include <qdeclarative.h>
62 #include <qdeclarativetimeline_p_p.h>
63 #include <qdeclarativeanimation_p_p.h>
64
65 #include <qdatetime.h>
66
67 QT_BEGIN_NAMESPACE
68
69 // Really slow flicks can be annoying.
70 const qreal MinimumFlickVelocity = 75.0;
71
72 class QDeclarativeFlickableVisibleArea;
73 class QDeclarativeFlickablePrivate : public QDeclarativeItemPrivate, public QDeclarativeItemChangeListener
74 {
75     Q_DECLARE_PUBLIC(QDeclarativeFlickable)
76
77 public:
78     QDeclarativeFlickablePrivate();
79     void init();
80
81     struct Velocity : public QDeclarativeTimeLineValue
82     {
83         Velocity(QDeclarativeFlickablePrivate *p)
84             : parent(p) {}
85         virtual void setValue(qreal v) {
86             if (v != value()) {
87                 QDeclarativeTimeLineValue::setValue(v);
88                 parent->updateVelocity();
89             }
90         }
91         QDeclarativeFlickablePrivate *parent;
92     };
93
94     struct AxisData {
95         AxisData(QDeclarativeFlickablePrivate *fp, void (QDeclarativeFlickablePrivate::*func)(qreal))
96             : move(fp, func), viewSize(-1), smoothVelocity(fp), atEnd(false), atBeginning(true)
97             , fixingUp(false), inOvershoot(false)
98         {}
99
100         void reset() {
101             velocityBuffer.clear();
102             dragStartOffset = 0;
103             fixingUp = false;
104             inOvershoot = false;
105         }
106
107         void addVelocitySample(qreal v, qreal maxVelocity);
108         void updateVelocity();
109
110         QDeclarativeTimeLineValueProxy<QDeclarativeFlickablePrivate> move;
111         qreal viewSize;
112         qreal pressPos;
113         qreal dragStartOffset;
114         qreal dragMinBound;
115         qreal dragMaxBound;
116         qreal velocity;
117         qreal flickTarget;
118         QDeclarativeFlickablePrivate::Velocity smoothVelocity;
119         QPODVector<qreal,10> velocityBuffer;
120         bool atEnd : 1;
121         bool atBeginning : 1;
122         bool fixingUp : 1;
123         bool inOvershoot : 1;
124     };
125
126     void flickX(qreal velocity);
127     void flickY(qreal velocity);
128     virtual void flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
129                         QDeclarativeTimeLineCallback::Callback fixupCallback, qreal velocity);
130
131     void fixupX();
132     void fixupY();
133     virtual void fixup(AxisData &data, qreal minExtent, qreal maxExtent);
134
135     void updateBeginningEnd();
136
137     bool isOutermostPressDelay() const;
138     void captureDelayedPress(QGraphicsSceneMouseEvent *event);
139     void clearDelayedPress();
140
141     void setRoundedViewportX(qreal x);
142     void setRoundedViewportY(qreal y);
143
144     qreal overShootDistance(qreal size);
145
146     void itemGeometryChanged(QDeclarativeItem *, const QRectF &, const QRectF &);
147
148 public:
149     QDeclarativeItem *contentItem;
150
151     AxisData hData;
152     AxisData vData;
153
154     QDeclarativeTimeLine timeline;
155     bool flickingHorizontally : 1;
156     bool flickingVertically : 1;
157     bool hMoved : 1;
158     bool vMoved : 1;
159     bool movingHorizontally : 1;
160     bool movingVertically : 1;
161     bool stealMouse : 1;
162     bool pressed : 1;
163     bool interactive : 1;
164     bool calcVelocity : 1;
165     QElapsedTimer lastPosTime;
166     QPointF lastPos;
167     QPointF pressPos;
168     QElapsedTimer pressTime;
169     qreal deceleration;
170     qreal maxVelocity;
171     QElapsedTimer velocityTime;
172     QPointF lastFlickablePosition;
173     qreal reportedVelocitySmoothing;
174     QGraphicsSceneMouseEvent *delayedPressEvent;
175     QGraphicsItem *delayedPressTarget;
176     QBasicTimer delayedPressTimer;
177     int pressDelay;
178     int fixupDuration;
179
180     enum FixupMode { Normal, Immediate, ExtentChanged };
181     FixupMode fixupMode;
182
183     static void fixupY_callback(void *);
184     static void fixupX_callback(void *);
185
186     void updateVelocity();
187     int vTime;
188     QDeclarativeTimeLine velocityTimeline;
189     QDeclarativeFlickableVisibleArea *visibleArea;
190     QDeclarativeFlickable::FlickableDirection flickableDirection;
191     QDeclarativeFlickable::BoundsBehavior boundsBehavior;
192
193     void handleMousePressEvent(QGraphicsSceneMouseEvent *);
194     void handleMouseMoveEvent(QGraphicsSceneMouseEvent *);
195     void handleMouseReleaseEvent(QGraphicsSceneMouseEvent *);
196
197     // flickableData property
198     static void data_append(QDeclarativeListProperty<QObject> *, QObject *);
199     static int data_count(QDeclarativeListProperty<QObject> *);
200     static QObject *data_at(QDeclarativeListProperty<QObject> *, int);
201     static void data_clear(QDeclarativeListProperty<QObject> *);
202 };
203
204 class QDeclarativeFlickableVisibleArea : public QObject
205 {
206     Q_OBJECT
207
208     Q_PROPERTY(qreal xPosition READ xPosition NOTIFY xPositionChanged)
209     Q_PROPERTY(qreal yPosition READ yPosition NOTIFY yPositionChanged)
210     Q_PROPERTY(qreal widthRatio READ widthRatio NOTIFY widthRatioChanged)
211     Q_PROPERTY(qreal heightRatio READ heightRatio NOTIFY heightRatioChanged)
212
213 public:
214     QDeclarativeFlickableVisibleArea(QDeclarativeFlickable *parent=0);
215
216     qreal xPosition() const;
217     qreal widthRatio() const;
218     qreal yPosition() const;
219     qreal heightRatio() const;
220
221     void updateVisible();
222
223 signals:
224     void xPositionChanged(qreal xPosition);
225     void yPositionChanged(qreal yPosition);
226     void widthRatioChanged(qreal widthRatio);
227     void heightRatioChanged(qreal heightRatio);
228
229 private:
230     QDeclarativeFlickable *flickable;
231     qreal m_xPosition;
232     qreal m_widthRatio;
233     qreal m_yPosition;
234     qreal m_heightRatio;
235 };
236
237 QT_END_NAMESPACE
238
239 QML_DECLARE_TYPE(QDeclarativeFlickableVisibleArea)
240
241 #endif