Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsglistview_p.h
1 // Commit: 95814418f9d6adeba365c795462e8afb00138211
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 QSGLISTVIEW_P_H
44 #define QSGLISTVIEW_P_H
45
46 #include "qsgflickable_p.h"
47
48 #include <private/qdeclarativeguard_p.h>
49
50 QT_BEGIN_HEADER
51
52 QT_BEGIN_NAMESPACE
53
54 QT_MODULE(Declarative)
55
56 class Q_AUTOTEST_EXPORT QSGViewSection : public QObject
57 {
58     Q_OBJECT
59     Q_PROPERTY(QString property READ property WRITE setProperty NOTIFY propertyChanged)
60     Q_PROPERTY(SectionCriteria criteria READ criteria WRITE setCriteria NOTIFY criteriaChanged)
61     Q_PROPERTY(QDeclarativeComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
62     Q_ENUMS(SectionCriteria)
63 public:
64     QSGViewSection(QObject *parent=0) : QObject(parent), m_criteria(FullString), m_delegate(0) {}
65
66     QString property() const { return m_property; }
67     void setProperty(const QString &);
68
69     enum SectionCriteria { FullString, FirstCharacter };
70     SectionCriteria criteria() const { return m_criteria; }
71     void setCriteria(SectionCriteria);
72
73     QDeclarativeComponent *delegate() const { return m_delegate; }
74     void setDelegate(QDeclarativeComponent *delegate);
75
76     QString sectionString(const QString &value);
77
78 Q_SIGNALS:
79     void propertyChanged();
80     void criteriaChanged();
81     void delegateChanged();
82
83 private:
84     QString m_property;
85     SectionCriteria m_criteria;
86     QDeclarativeComponent *m_delegate;
87 };
88
89
90 class QSGVisualModel;
91 class QSGListViewAttached;
92 class QSGListViewPrivate;
93 class Q_AUTOTEST_EXPORT QSGListView : public QSGFlickable
94 {
95     Q_OBJECT
96     Q_DECLARE_PRIVATE(QSGListView)
97
98     Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged)
99     Q_PROPERTY(QDeclarativeComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
100     Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
101     Q_PROPERTY(QSGItem *currentItem READ currentItem NOTIFY currentIndexChanged)
102     Q_PROPERTY(int count READ count NOTIFY countChanged)
103
104     Q_PROPERTY(QDeclarativeComponent *highlight READ highlight WRITE setHighlight NOTIFY highlightChanged)
105     Q_PROPERTY(QSGItem *highlightItem READ highlightItem NOTIFY highlightItemChanged)
106     Q_PROPERTY(bool highlightFollowsCurrentItem READ highlightFollowsCurrentItem WRITE setHighlightFollowsCurrentItem NOTIFY highlightFollowsCurrentItemChanged)
107     Q_PROPERTY(qreal highlightMoveSpeed READ highlightMoveSpeed WRITE setHighlightMoveSpeed NOTIFY highlightMoveSpeedChanged)
108     Q_PROPERTY(int highlightMoveDuration READ highlightMoveDuration WRITE setHighlightMoveDuration NOTIFY highlightMoveDurationChanged)
109     Q_PROPERTY(qreal highlightResizeSpeed READ highlightResizeSpeed WRITE setHighlightResizeSpeed NOTIFY highlightResizeSpeedChanged)
110     Q_PROPERTY(int highlightResizeDuration READ highlightResizeDuration WRITE setHighlightResizeDuration NOTIFY highlightResizeDurationChanged)
111
112     Q_PROPERTY(qreal preferredHighlightBegin READ preferredHighlightBegin WRITE setPreferredHighlightBegin NOTIFY preferredHighlightBeginChanged RESET resetPreferredHighlightBegin)
113     Q_PROPERTY(qreal preferredHighlightEnd READ preferredHighlightEnd WRITE setPreferredHighlightEnd NOTIFY preferredHighlightEndChanged RESET resetPreferredHighlightEnd)
114     Q_PROPERTY(HighlightRangeMode highlightRangeMode READ highlightRangeMode WRITE setHighlightRangeMode NOTIFY highlightRangeModeChanged)
115
116     Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged)
117     Q_PROPERTY(Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged)
118     Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged)
119     Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged)
120     Q_PROPERTY(bool keyNavigationWraps READ isWrapEnabled WRITE setWrapEnabled NOTIFY keyNavigationWrapsChanged)
121     Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged)
122     Q_PROPERTY(QSGViewSection *section READ sectionCriteria CONSTANT)
123     Q_PROPERTY(QString currentSection READ currentSection NOTIFY currentSectionChanged)
124
125     Q_PROPERTY(SnapMode snapMode READ snapMode WRITE setSnapMode NOTIFY snapModeChanged)
126
127     Q_PROPERTY(QDeclarativeComponent *header READ header WRITE setHeader NOTIFY headerChanged)
128     Q_PROPERTY(QDeclarativeComponent *footer READ footer WRITE setFooter NOTIFY footerChanged)
129
130     Q_ENUMS(HighlightRangeMode)
131     Q_ENUMS(Orientation)
132     Q_ENUMS(SnapMode)
133     Q_ENUMS(PositionMode)
134     Q_CLASSINFO("DefaultProperty", "data")
135
136 public:
137     QSGListView(QSGItem *parent=0);
138     ~QSGListView();
139
140     QVariant model() const;
141     void setModel(const QVariant &);
142
143     QDeclarativeComponent *delegate() const;
144     void setDelegate(QDeclarativeComponent *);
145
146     int currentIndex() const;
147     void setCurrentIndex(int idx);
148
149     QSGItem *currentItem();
150     QSGItem *highlightItem();
151     int count() const;
152
153     QDeclarativeComponent *highlight() const;
154     void setHighlight(QDeclarativeComponent *highlight);
155
156     bool highlightFollowsCurrentItem() const;
157     void setHighlightFollowsCurrentItem(bool);
158
159     enum HighlightRangeMode { NoHighlightRange, ApplyRange, StrictlyEnforceRange };
160     HighlightRangeMode highlightRangeMode() const;
161     void setHighlightRangeMode(HighlightRangeMode mode);
162
163     qreal preferredHighlightBegin() const;
164     void setPreferredHighlightBegin(qreal);
165     void resetPreferredHighlightBegin();
166
167     qreal preferredHighlightEnd() const;
168     void setPreferredHighlightEnd(qreal);
169     void resetPreferredHighlightEnd();
170
171     qreal spacing() const;
172     void setSpacing(qreal spacing);
173
174     enum Orientation { Horizontal = Qt::Horizontal, Vertical = Qt::Vertical };
175     Orientation orientation() const;
176     void setOrientation(Orientation);
177
178     Qt::LayoutDirection layoutDirection() const;
179     void setLayoutDirection(Qt::LayoutDirection);
180     Qt::LayoutDirection effectiveLayoutDirection() const;
181
182     bool isWrapEnabled() const;
183     void setWrapEnabled(bool);
184
185     int cacheBuffer() const;
186     void setCacheBuffer(int);
187
188     QSGViewSection *sectionCriteria();
189     QString currentSection() const;
190
191     qreal highlightMoveSpeed() const;
192     void setHighlightMoveSpeed(qreal);
193
194     int highlightMoveDuration() const;
195     void setHighlightMoveDuration(int);
196
197     qreal highlightResizeSpeed() const;
198     void setHighlightResizeSpeed(qreal);
199
200     int highlightResizeDuration() const;
201     void setHighlightResizeDuration(int);
202
203     enum SnapMode { NoSnap, SnapToItem, SnapOneItem };
204     SnapMode snapMode() const;
205     void setSnapMode(SnapMode mode);
206
207     QDeclarativeComponent *footer() const;
208     void setFooter(QDeclarativeComponent *);
209
210     QDeclarativeComponent *header() const;
211     void setHeader(QDeclarativeComponent *);
212
213     virtual void setContentX(qreal pos);
214     virtual void setContentY(qreal pos);
215
216     static QSGListViewAttached *qmlAttachedProperties(QObject *);
217
218     enum PositionMode { Beginning, Center, End, Visible, Contain };
219
220     Q_INVOKABLE void positionViewAtIndex(int index, int mode);
221     Q_INVOKABLE int indexAt(qreal x, qreal y) const;
222     Q_INVOKABLE void positionViewAtBeginning();
223     Q_INVOKABLE void positionViewAtEnd();
224
225 public Q_SLOTS:
226     void incrementCurrentIndex();
227     void decrementCurrentIndex();
228
229 Q_SIGNALS:
230     void countChanged();
231     void spacingChanged();
232     void orientationChanged();
233     void layoutDirectionChanged();
234     void effectiveLayoutDirectionChanged();
235     void currentIndexChanged();
236     void currentSectionChanged();
237     void highlightMoveSpeedChanged();
238     void highlightMoveDurationChanged();
239     void highlightResizeSpeedChanged();
240     void highlightResizeDurationChanged();
241     void highlightChanged();
242     void highlightItemChanged();
243     void modelChanged();
244     void delegateChanged();
245     void highlightFollowsCurrentItemChanged();
246     void preferredHighlightBeginChanged();
247     void preferredHighlightEndChanged();
248     void highlightRangeModeChanged();
249     void keyNavigationWrapsChanged();
250     void cacheBufferChanged();
251     void snapModeChanged();
252     void headerChanged();
253     void footerChanged();
254
255 protected:
256     virtual void updatePolish();
257     virtual void viewportMoved();
258     virtual qreal minYExtent() const;
259     virtual qreal maxYExtent() const;
260     virtual qreal minXExtent() const;
261     virtual qreal maxXExtent() const;
262     virtual void keyPressEvent(QKeyEvent *);
263     virtual void geometryChanged(const QRectF &newGeometry,const QRectF &oldGeometry);
264     virtual void componentComplete();
265
266 private Q_SLOTS:
267     void updateSections();
268     void refill();
269     void trackedPositionChanged();
270     void itemsInserted(int index, int count);
271     void itemsRemoved(int index, int count);
272     void itemsMoved(int from, int to, int count);
273     void itemsChanged(int index, int count);
274     void modelReset();
275     void destroyRemoved();
276     void createdItem(int index, QSGItem *item);
277     void destroyingItem(QSGItem *item);
278     void animStopped();
279 };
280
281 class QSGListViewAttached : public QObject
282 {
283     Q_OBJECT
284 public:
285     QSGListViewAttached(QObject *parent)
286         : QObject(parent), m_view(0), m_isCurrent(false), m_delayRemove(false) {}
287     ~QSGListViewAttached() {}
288
289     Q_PROPERTY(QSGListView *view READ view NOTIFY viewChanged)
290     QSGListView *view() { return m_view; }
291     void setView(QSGListView *view) {
292         if (view != m_view) {
293             m_view = view;
294             emit viewChanged();
295         }
296     }
297
298     Q_PROPERTY(bool isCurrentItem READ isCurrentItem NOTIFY currentItemChanged)
299     bool isCurrentItem() const { return m_isCurrent; }
300     void setIsCurrentItem(bool c) {
301         if (m_isCurrent != c) {
302             m_isCurrent = c;
303             emit currentItemChanged();
304         }
305     }
306
307     Q_PROPERTY(QString previousSection READ prevSection NOTIFY prevSectionChanged)
308     QString prevSection() const { return m_prevSection; }
309     void setPrevSection(const QString &sect) {
310         if (m_prevSection != sect) {
311             m_prevSection = sect;
312             emit prevSectionChanged();
313         }
314     }
315
316     Q_PROPERTY(QString nextSection READ nextSection NOTIFY nextSectionChanged)
317     QString nextSection() const { return m_nextSection; }
318     void setNextSection(const QString &sect) {
319         if (m_nextSection != sect) {
320             m_nextSection = sect;
321             emit nextSectionChanged();
322         }
323     }
324
325     Q_PROPERTY(QString section READ section NOTIFY sectionChanged)
326     QString section() const { return m_section; }
327     void setSection(const QString &sect) {
328         if (m_section != sect) {
329             m_section = sect;
330             emit sectionChanged();
331         }
332     }
333
334     Q_PROPERTY(bool delayRemove READ delayRemove WRITE setDelayRemove NOTIFY delayRemoveChanged)
335     bool delayRemove() const { return m_delayRemove; }
336     void setDelayRemove(bool delay) {
337         if (m_delayRemove != delay) {
338             m_delayRemove = delay;
339             emit delayRemoveChanged();
340         }
341     }
342
343     void emitAdd() { emit add(); }
344     void emitRemove() { emit remove(); }
345
346 Q_SIGNALS:
347     void currentItemChanged();
348     void sectionChanged();
349     void prevSectionChanged();
350     void nextSectionChanged();
351     void delayRemoveChanged();
352     void add();
353     void remove();
354     void viewChanged();
355
356 public:
357     QDeclarativeGuard<QSGListView> m_view;
358     mutable QString m_section;
359     QString m_prevSection;
360     QString m_nextSection;
361     bool m_isCurrent : 1;
362     bool m_delayRemove : 1;
363 };
364
365
366 QT_END_NAMESPACE
367
368 QML_DECLARE_TYPEINFO(QSGListView, QML_HAS_ATTACHED_PROPERTIES)
369 QML_DECLARE_TYPE(QSGListView)
370 QML_DECLARE_TYPE(QSGViewSection)
371
372 QT_END_HEADER
373
374 #endif // QSGLISTVIEW_P_H