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