1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQuick 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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QQUICKPATHVIEW_P_H
41 #define QQUICKPATHVIEW_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <private/qtquickglobal_p.h>
55 
56 QT_REQUIRE_CONFIG(quick_pathview);
57 
58 #include "qquickitem.h"
59 
60 #include <private/qtquickglobal_p.h>
61 #include <private/qquickpath_p.h>
62 
63 QT_BEGIN_NAMESPACE
64 
65 class QQmlChangeSet;
66 
67 class QQuickPathViewPrivate;
68 class QQuickPathViewAttached;
69 class Q_QUICK_PRIVATE_EXPORT QQuickPathView : public QQuickItem
70 {
71     Q_OBJECT
72 
73     Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged)
74     Q_PROPERTY(QQuickPath *path READ path WRITE setPath NOTIFY pathChanged)
75     Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
76     Q_PROPERTY(QQuickItem *currentItem READ currentItem NOTIFY currentItemChanged)
77     Q_PROPERTY(qreal offset READ offset WRITE setOffset NOTIFY offsetChanged)
78 
79     Q_PROPERTY(QQmlComponent *highlight READ highlight WRITE setHighlight NOTIFY highlightChanged)
80     Q_PROPERTY(QQuickItem *highlightItem READ highlightItem NOTIFY highlightItemChanged)
81 
82     Q_PROPERTY(qreal preferredHighlightBegin READ preferredHighlightBegin WRITE setPreferredHighlightBegin NOTIFY preferredHighlightBeginChanged)
83     Q_PROPERTY(qreal preferredHighlightEnd READ preferredHighlightEnd WRITE setPreferredHighlightEnd NOTIFY preferredHighlightEndChanged)
84     Q_PROPERTY(HighlightRangeMode highlightRangeMode READ highlightRangeMode WRITE setHighlightRangeMode NOTIFY highlightRangeModeChanged)
85     Q_PROPERTY(int highlightMoveDuration READ highlightMoveDuration WRITE setHighlightMoveDuration NOTIFY highlightMoveDurationChanged)
86 
87     Q_PROPERTY(qreal dragMargin READ dragMargin WRITE setDragMargin NOTIFY dragMarginChanged)
88     Q_PROPERTY(qreal maximumFlickVelocity READ maximumFlickVelocity WRITE setMaximumFlickVelocity NOTIFY maximumFlickVelocityChanged)
89     Q_PROPERTY(qreal flickDeceleration READ flickDeceleration WRITE setFlickDeceleration NOTIFY flickDecelerationChanged)
90     Q_PROPERTY(bool interactive READ isInteractive WRITE setInteractive NOTIFY interactiveChanged)
91 
92     Q_PROPERTY(bool moving READ isMoving NOTIFY movingChanged)
93     Q_PROPERTY(bool flicking READ isFlicking NOTIFY flickingChanged)
94     Q_PROPERTY(bool dragging READ isDragging NOTIFY draggingChanged)
95 
96     Q_PROPERTY(int count READ count NOTIFY countChanged)
97     Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
98     Q_PROPERTY(int pathItemCount READ pathItemCount WRITE setPathItemCount RESET resetPathItemCount NOTIFY pathItemCountChanged)
99     Q_PROPERTY(SnapMode snapMode READ snapMode WRITE setSnapMode NOTIFY snapModeChanged)
100     Q_PROPERTY(MovementDirection movementDirection READ movementDirection WRITE setMovementDirection NOTIFY movementDirectionChanged REVISION 7)
101 
102     Q_PROPERTY(int cacheItemCount READ cacheItemCount WRITE setCacheItemCount NOTIFY cacheItemCountChanged)
103     QML_NAMED_ELEMENT(PathView)
104     QML_ATTACHED(QQuickPathViewAttached)
105 
106 public:
107     QQuickPathView(QQuickItem *parent = nullptr);
108     virtual ~QQuickPathView();
109 
110     QVariant model() const;
111     void setModel(const QVariant &);
112 
113     QQuickPath *path() const;
114     void setPath(QQuickPath *);
115 
116     int currentIndex() const;
117     void setCurrentIndex(int idx);
118 
119     QQuickItem *currentItem() const;
120 
121     qreal offset() const;
122     void setOffset(qreal offset);
123 
124     QQmlComponent *highlight() const;
125     void setHighlight(QQmlComponent *highlight);
126     QQuickItem *highlightItem() const;
127 
128     enum HighlightRangeMode { NoHighlightRange, ApplyRange, StrictlyEnforceRange };
129     Q_ENUM(HighlightRangeMode)
130     HighlightRangeMode highlightRangeMode() const;
131     void setHighlightRangeMode(HighlightRangeMode mode);
132 
133     qreal preferredHighlightBegin() const;
134     void setPreferredHighlightBegin(qreal);
135 
136     qreal preferredHighlightEnd() const;
137     void setPreferredHighlightEnd(qreal);
138 
139     int highlightMoveDuration() const;
140     void setHighlightMoveDuration(int);
141 
142     qreal dragMargin() const;
143     void setDragMargin(qreal margin);
144 
145     qreal flickDeceleration() const;
146     void setFlickDeceleration(qreal dec);
147 
148     qreal maximumFlickVelocity() const;
149     void setMaximumFlickVelocity(qreal);
150 
151     bool isInteractive() const;
152     void setInteractive(bool);
153 
154     bool isMoving() const;
155     bool isFlicking() const;
156     bool isDragging() const;
157 
158     int count() const;
159 
160     QQmlComponent *delegate() const;
161     void setDelegate(QQmlComponent *);
162 
163     int pathItemCount() const;
164     void setPathItemCount(int);
165     void resetPathItemCount();
166 
167     int cacheItemCount() const;
168     void setCacheItemCount(int);
169 
170     enum SnapMode { NoSnap, SnapToItem, SnapOneItem };
171     Q_ENUM(SnapMode)
172     SnapMode snapMode() const;
173     void setSnapMode(SnapMode mode);
174 
175     enum MovementDirection { Shortest, Negative, Positive };
176     Q_ENUM(MovementDirection)
177     MovementDirection movementDirection() const;
178     void setMovementDirection(MovementDirection dir);
179 
180     enum PositionMode { Beginning, Center, End, Contain=4, SnapPosition }; // 3 == Visible in other views
181     Q_ENUM(PositionMode)
182     Q_INVOKABLE void positionViewAtIndex(int index, int mode);
183     Q_INVOKABLE int indexAt(qreal x, qreal y) const;
184     Q_INVOKABLE QQuickItem *itemAt(qreal x, qreal y) const;
185     Q_REVISION(13) Q_INVOKABLE QQuickItem *itemAtIndex(int index) const;
186 
187     static QQuickPathViewAttached *qmlAttachedProperties(QObject *);
188 
189 public Q_SLOTS:
190     void incrementCurrentIndex();
191     void decrementCurrentIndex();
192 
193 Q_SIGNALS:
194     void currentIndexChanged();
195     void currentItemChanged();
196     void offsetChanged();
197     void modelChanged();
198     void countChanged();
199     void pathChanged();
200     void preferredHighlightBeginChanged();
201     void preferredHighlightEndChanged();
202     void highlightRangeModeChanged();
203     void dragMarginChanged();
204     void snapPositionChanged();
205     void delegateChanged();
206     void pathItemCountChanged();
207     void maximumFlickVelocityChanged();
208     void flickDecelerationChanged();
209     void interactiveChanged();
210     void movingChanged();
211     void flickingChanged();
212     void draggingChanged();
213     void highlightChanged();
214     void highlightItemChanged();
215     void highlightMoveDurationChanged();
216     void movementStarted();
217     void movementEnded();
218     Q_REVISION(7) void movementDirectionChanged();
219     void flickStarted();
220     void flickEnded();
221     void dragStarted();
222     void dragEnded();
223     void snapModeChanged();
224     void cacheItemCountChanged();
225 
226 protected:
227     void updatePolish() override;
228     void mousePressEvent(QMouseEvent *event) override;
229     void mouseMoveEvent(QMouseEvent *event) override;
230     void mouseReleaseEvent(QMouseEvent *) override;
231     bool sendMouseEvent(QMouseEvent *event);
232     bool childMouseEventFilter(QQuickItem *, QEvent *) override;
233     void mouseUngrabEvent() override;
234     void componentComplete() override;
235 
236 private Q_SLOTS:
237     void refill();
238     void ticked();
239     void movementEnding();
240     void modelUpdated(const QQmlChangeSet &changeSet, bool reset);
241     void createdItem(int index, QObject *item);
242     void initItem(int index, QObject *item);
243     void destroyingItem(QObject *item);
244     void pathUpdated();
245 
246 private:
247     friend class QQuickPathViewAttached;
248     Q_DISABLE_COPY(QQuickPathView)
249     Q_DECLARE_PRIVATE(QQuickPathView)
250 };
251 
252 class QQmlOpenMetaObject;
253 class QQuickPathViewAttached : public QObject
254 {
255     Q_OBJECT
256 
257     Q_PROPERTY(QQuickPathView *view READ view CONSTANT)
258     Q_PROPERTY(bool isCurrentItem READ isCurrentItem NOTIFY currentItemChanged)
259     Q_PROPERTY(bool onPath READ isOnPath NOTIFY pathChanged)
260 
261 public:
262     QQuickPathViewAttached(QObject *parent);
263     ~QQuickPathViewAttached();
264 
view()265     QQuickPathView *view() const { return m_view; }
266 
isCurrentItem()267     bool isCurrentItem() const { return m_isCurrent; }
setIsCurrentItem(bool c)268     void setIsCurrentItem(bool c) {
269         if (m_isCurrent != c) {
270             m_isCurrent = c;
271             Q_EMIT currentItemChanged();
272         }
273     }
274 
275     QVariant value(const QByteArray &name) const;
276     void setValue(const QByteArray &name, const QVariant &val);
277 
isOnPath()278     bool isOnPath() const { return m_onPath; }
setOnPath(bool on)279     void setOnPath(bool on) {
280         if (on != m_onPath) {
281             m_onPath = on;
282             Q_EMIT pathChanged();
283         }
284     }
285     qreal m_percent;
286 
287 Q_SIGNALS:
288     void currentItemChanged();
289     void pathChanged();
290 
291 private:
292     friend class QQuickPathViewPrivate;
293     friend class QQuickPathView;
294     QQuickPathView *m_view;
295     QQmlOpenMetaObject *m_metaobject;
296     bool m_onPath : 1;
297     bool m_isCurrent : 1;
298 };
299 
300 
301 QT_END_NAMESPACE
302 
303 QML_DECLARE_TYPE(QQuickPathView)
304 
305 #endif // QQUICKPATHVIEW_P_H
306