Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsggridview_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 QSGGRIDVIEW_P_H
44 #define QSGGRIDVIEW_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 class QSGVisualModel;
56 class QSGGridViewAttached;
57 class QSGGridViewPrivate;
58 class Q_AUTOTEST_EXPORT QSGGridView : public QSGFlickable
59 {
60     Q_OBJECT
61     Q_DECLARE_PRIVATE(QSGGridView)
62
63     Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged)
64     Q_PROPERTY(QDeclarativeComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
65     Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
66     Q_PROPERTY(QSGItem *currentItem READ currentItem NOTIFY currentIndexChanged)
67     Q_PROPERTY(int count READ count NOTIFY countChanged)
68
69     Q_PROPERTY(QDeclarativeComponent *highlight READ highlight WRITE setHighlight NOTIFY highlightChanged)
70     Q_PROPERTY(QSGItem *highlightItem READ highlightItem NOTIFY highlightItemChanged)
71     Q_PROPERTY(bool highlightFollowsCurrentItem READ highlightFollowsCurrentItem WRITE setHighlightFollowsCurrentItem)
72     Q_PROPERTY(int highlightMoveDuration READ highlightMoveDuration WRITE setHighlightMoveDuration NOTIFY highlightMoveDurationChanged)
73
74     Q_PROPERTY(qreal preferredHighlightBegin READ preferredHighlightBegin WRITE setPreferredHighlightBegin NOTIFY preferredHighlightBeginChanged RESET resetPreferredHighlightBegin)
75     Q_PROPERTY(qreal preferredHighlightEnd READ preferredHighlightEnd WRITE setPreferredHighlightEnd NOTIFY preferredHighlightEndChanged RESET resetPreferredHighlightEnd)
76     Q_PROPERTY(HighlightRangeMode highlightRangeMode READ highlightRangeMode WRITE setHighlightRangeMode NOTIFY highlightRangeModeChanged)
77
78     Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged)
79     Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged)
80     Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged)
81     Q_PROPERTY(bool keyNavigationWraps READ isWrapEnabled WRITE setWrapEnabled NOTIFY keyNavigationWrapsChanged)
82     Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged)
83     Q_PROPERTY(int cellWidth READ cellWidth WRITE setCellWidth NOTIFY cellWidthChanged)
84     Q_PROPERTY(int cellHeight READ cellHeight WRITE setCellHeight NOTIFY cellHeightChanged)
85
86     Q_PROPERTY(SnapMode snapMode READ snapMode WRITE setSnapMode NOTIFY snapModeChanged)
87
88     Q_PROPERTY(QDeclarativeComponent *header READ header WRITE setHeader NOTIFY headerChanged)
89     Q_PROPERTY(QDeclarativeComponent *footer READ footer WRITE setFooter NOTIFY footerChanged)
90
91     Q_ENUMS(HighlightRangeMode)
92     Q_ENUMS(SnapMode)
93     Q_ENUMS(Flow)
94     Q_ENUMS(PositionMode)
95     Q_CLASSINFO("DefaultProperty", "data")
96
97 public:
98     QSGGridView(QSGItem *parent=0);
99     ~QSGGridView();
100
101     QVariant model() const;
102     int modelCount() const;
103     void setModel(const QVariant &);
104
105     QDeclarativeComponent *delegate() const;
106     void setDelegate(QDeclarativeComponent *);
107
108     int currentIndex() const;
109     void setCurrentIndex(int idx);
110
111     QSGItem *currentItem();
112     QSGItem *highlightItem();
113     int count() const;
114
115     QDeclarativeComponent *highlight() const;
116     void setHighlight(QDeclarativeComponent *highlight);
117
118     bool highlightFollowsCurrentItem() const;
119     void setHighlightFollowsCurrentItem(bool);
120
121     int highlightMoveDuration() const;
122     void setHighlightMoveDuration(int);
123
124     enum HighlightRangeMode { NoHighlightRange, ApplyRange, StrictlyEnforceRange };
125     HighlightRangeMode highlightRangeMode() const;
126     void setHighlightRangeMode(HighlightRangeMode mode);
127
128     qreal preferredHighlightBegin() const;
129     void setPreferredHighlightBegin(qreal);
130     void resetPreferredHighlightBegin();
131
132     qreal preferredHighlightEnd() const;
133     void setPreferredHighlightEnd(qreal);
134     void resetPreferredHighlightEnd();
135
136     Qt::LayoutDirection layoutDirection() const;
137     void setLayoutDirection(Qt::LayoutDirection);
138     Qt::LayoutDirection effectiveLayoutDirection() const;
139
140     enum Flow { LeftToRight, TopToBottom };
141     Flow flow() const;
142     void setFlow(Flow);
143
144     bool isWrapEnabled() const;
145     void setWrapEnabled(bool);
146
147     int cacheBuffer() const;
148     void setCacheBuffer(int);
149
150     int cellWidth() const;
151     void setCellWidth(int);
152
153     int cellHeight() const;
154     void setCellHeight(int);
155
156     enum SnapMode { NoSnap, SnapToRow, SnapOneRow };
157     SnapMode snapMode() const;
158     void setSnapMode(SnapMode mode);
159
160     QDeclarativeComponent *footer() const;
161     void setFooter(QDeclarativeComponent *);
162
163     QDeclarativeComponent *header() const;
164     void setHeader(QDeclarativeComponent *);
165
166     virtual void setContentX(qreal pos);
167     virtual void setContentY(qreal pos);
168
169     enum PositionMode { Beginning, Center, End, Visible, Contain };
170
171     Q_INVOKABLE void positionViewAtIndex(int index, int mode);
172     Q_INVOKABLE int indexAt(qreal x, qreal y) const;
173     Q_INVOKABLE void positionViewAtBeginning();
174     Q_INVOKABLE void positionViewAtEnd();
175
176     static QSGGridViewAttached *qmlAttachedProperties(QObject *);
177
178 public Q_SLOTS:
179     void moveCurrentIndexUp();
180     void moveCurrentIndexDown();
181     void moveCurrentIndexLeft();
182     void moveCurrentIndexRight();
183
184 Q_SIGNALS:
185     void countChanged();
186     void currentIndexChanged();
187     void cellWidthChanged();
188     void cellHeightChanged();
189     void highlightChanged();
190     void highlightItemChanged();
191     void preferredHighlightBeginChanged();
192     void preferredHighlightEndChanged();
193     void highlightRangeModeChanged();
194     void highlightMoveDurationChanged();
195     void modelChanged();
196     void delegateChanged();
197     void flowChanged();
198     void layoutDirectionChanged();
199     void effectiveLayoutDirectionChanged();
200     void keyNavigationWrapsChanged();
201     void cacheBufferChanged();
202     void snapModeChanged();
203     void headerChanged();
204     void footerChanged();
205
206 protected:
207     virtual void updatePolish();
208     virtual void viewportMoved();
209     virtual qreal minYExtent() const;
210     virtual qreal maxYExtent() const;
211     virtual qreal minXExtent() const;
212     virtual qreal maxXExtent() const;
213     virtual void keyPressEvent(QKeyEvent *);
214     virtual void componentComplete();
215
216 private Q_SLOTS:
217     void trackedPositionChanged();
218     void itemsInserted(int index, int count);
219     void itemsRemoved(int index, int count);
220     void itemsMoved(int from, int to, int count);
221     void modelReset();
222     void destroyRemoved();
223     void createdItem(int index, QSGItem *item);
224     void destroyingItem(QSGItem *item);
225     void animStopped();
226
227 private:
228     void refill();
229 };
230
231 class QSGGridViewAttached : public QObject
232 {
233     Q_OBJECT
234 public:
235     QSGGridViewAttached(QObject *parent)
236         : QObject(parent), m_view(0), m_isCurrent(false), m_delayRemove(false) {}
237     ~QSGGridViewAttached() {}
238
239     Q_PROPERTY(QSGGridView *view READ view NOTIFY viewChanged)
240     QSGGridView *view() { return m_view; }
241     void setView(QSGGridView *view) {
242         if (view != m_view) {
243             m_view = view;
244             emit viewChanged();
245         }
246     }
247
248     Q_PROPERTY(bool isCurrentItem READ isCurrentItem NOTIFY currentItemChanged)
249     bool isCurrentItem() const { return m_isCurrent; }
250     void setIsCurrentItem(bool c) {
251         if (m_isCurrent != c) {
252             m_isCurrent = c;
253             emit currentItemChanged();
254         }
255     }
256
257     Q_PROPERTY(bool delayRemove READ delayRemove WRITE setDelayRemove NOTIFY delayRemoveChanged)
258     bool delayRemove() const { return m_delayRemove; }
259     void setDelayRemove(bool delay) {
260         if (m_delayRemove != delay) {
261             m_delayRemove = delay;
262             emit delayRemoveChanged();
263         }
264     }
265
266     void emitAdd() { emit add(); }
267     void emitRemove() { emit remove(); }
268
269 Q_SIGNALS:
270     void currentItemChanged();
271     void delayRemoveChanged();
272     void add();
273     void remove();
274     void viewChanged();
275
276 public:
277     QDeclarativeGuard<QSGGridView> m_view;
278     bool m_isCurrent : 1;
279     bool m_delayRemove : 1;
280 };
281
282
283 QT_END_NAMESPACE
284
285 QML_DECLARE_TYPE(QSGGridView)
286 QML_DECLARE_TYPEINFO(QSGGridView, QML_HAS_ATTACHED_PROPERTIES)
287
288 QT_END_HEADER
289
290 #endif // QSGGRIDVIEW_P_H