Improve interaction of nested Flickables and PathView
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickpathview_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 QQUICKPATHVIEW_P_P_H
43 #define QQUICKPATHVIEW_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 "qquickpathview_p.h"
57 #include "qquickitem_p.h"
58 #include "qquickvisualdatamodel_p.h"
59
60 #include <QtQml/qqml.h>
61 #include <QtCore/qdatetime.h>
62 #include <QtCore/qcoreapplication.h>
63
64 #include <private/qquickanimation_p_p.h>
65 #include <private/qqmlguard_p.h>
66 #include <private/qquicktimeline_p_p.h>
67
68 QT_BEGIN_NAMESPACE
69
70 class QQmlOpenMetaObjectType;
71 class QQuickPathViewAttached;
72 class QQuickPathViewPrivate : public QQuickItemPrivate, public QQuickItemChangeListener
73 {
74     Q_DECLARE_PUBLIC(QQuickPathView)
75
76 public:
77     QQuickPathViewPrivate()
78       : path(0), currentIndex(0), currentItemOffset(0.0), startPc(0), lastDist(0)
79         , lastElapsed(0), offset(0.0), offsetAdj(0.0), mappedRange(1.0)
80         , stealMouse(false), ownModel(false), interactive(true), haveHighlightRange(true)
81         , autoHighlight(true), highlightUp(false), layoutScheduled(false)
82         , moving(false), flicking(false), requestedOnPath(false), inRequest(false)
83         , dragMargin(0), deceleration(100)
84         , moveOffset(this, &QQuickPathViewPrivate::setAdjustedOffset), flickDuration(0)
85         , firstIndex(-1), pathItems(-1), requestedIndex(-1), requestedZ(0)
86         , moveReason(Other), moveDirection(Shortest), attType(0), highlightComponent(0), highlightItem(0)
87         , moveHighlight(this, &QQuickPathViewPrivate::setHighlightPosition)
88         , highlightPosition(0)
89         , highlightRangeStart(0), highlightRangeEnd(0)
90         , highlightRangeMode(QQuickPathView::StrictlyEnforceRange)
91         , highlightMoveDuration(300), modelCount(0)
92     {
93     }
94
95     void init();
96
97     void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) {
98         if ((newGeometry.size() != oldGeometry.size())
99             && (!highlightItem || item != highlightItem)) {
100             if (QQuickPathViewAttached *att = attached(item))
101                 att->m_percent = -1;
102             scheduleLayout();
103         }
104     }
105
106     void scheduleLayout() {
107         Q_Q(QQuickPathView);
108         if (!layoutScheduled) {
109             layoutScheduled = true;
110             q->polish();
111         }
112     }
113
114     QQuickItem *getItem(int modelIndex, qreal z = 0, bool onPath=true);
115     void releaseItem(QQuickItem *item);
116     QQuickPathViewAttached *attached(QQuickItem *item);
117     QQmlOpenMetaObjectType *attachedType();
118     void clear();
119     void updateMappedRange();
120     qreal positionOfIndex(qreal index) const;
121     void createHighlight();
122     void updateHighlight();
123     void setHighlightPosition(qreal pos);
124     bool isValid() const {
125         return model && model->count() > 0 && model->isValid() && path;
126     }
127
128     void handleMousePressEvent(QMouseEvent *event);
129     void handleMouseMoveEvent(QMouseEvent *event);
130     void handleMouseReleaseEvent(QMouseEvent *);
131
132     int calcCurrentIndex();
133     void createCurrentItem();
134     void updateCurrent();
135     static void fixOffsetCallback(void*);
136     void fixOffset();
137     void setOffset(qreal offset);
138     void setAdjustedOffset(qreal offset);
139     void regenerate();
140     void updateItem(QQuickItem *, qreal);
141     void snapToCurrent();
142     QPointF pointNear(const QPointF &point, qreal *nearPercent=0) const;
143     void addVelocitySample(qreal v);
144     qreal calcVelocity() const;
145
146     QQuickPath *path;
147     int currentIndex;
148     QQmlGuard<QQuickItem> currentItem;
149     qreal currentItemOffset;
150     qreal startPc;
151     QPointF startPoint;
152     qreal lastDist;
153     int lastElapsed;
154     qreal offset;
155     qreal offsetAdj;
156     qreal mappedRange;
157     bool stealMouse : 1;
158     bool ownModel : 1;
159     bool interactive : 1;
160     bool haveHighlightRange : 1;
161     bool autoHighlight : 1;
162     bool highlightUp : 1;
163     bool layoutScheduled : 1;
164     bool moving : 1;
165     bool flicking : 1;
166     bool requestedOnPath : 1;
167     bool inRequest : 1;
168     QElapsedTimer lastPosTime;
169     QPointF lastPos;
170     qreal dragMargin;
171     qreal deceleration;
172     QQuickTimeLine tl;
173     QQuickTimeLineValueProxy<QQuickPathViewPrivate> moveOffset;
174     int flickDuration;
175     int firstIndex;
176     int pathItems;
177     int requestedIndex;
178     qreal requestedZ;
179     QList<QQuickItem *> items;
180     QList<QQuickItem *> itemCache;
181     QQmlGuard<QQuickVisualModel> model;
182     QVariant modelVariant;
183     enum MovementReason { Other, SetIndex, Mouse };
184     MovementReason moveReason;
185     enum MovementDirection { Shortest, Negative, Positive };
186     MovementDirection moveDirection;
187     QQmlOpenMetaObjectType *attType;
188     QQmlComponent *highlightComponent;
189     QQuickItem *highlightItem;
190     QQuickTimeLineValueProxy<QQuickPathViewPrivate> moveHighlight;
191     qreal highlightPosition;
192     qreal highlightRangeStart;
193     qreal highlightRangeEnd;
194     QQuickPathView::HighlightRangeMode highlightRangeMode;
195     int highlightMoveDuration;
196     int modelCount;
197     QPODVector<qreal,10> velocityBuffer;
198 };
199
200 QT_END_NAMESPACE
201
202 #endif