80dacc0cb495188769a1a85df83c4b0aaa6cc910
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickitemview_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 QQUICKITEMVIEW_P_P_H
43 #define QQUICKITEMVIEW_P_P_H
44
45 #include "qquickitemview_p.h"
46 #include "qquickitemviewtransition_p.h"
47 #include "qquickflickable_p_p.h"
48 #include "qquickvisualdatamodel_p.h"
49 #include "qquickvisualitemmodel_p.h"
50 #include <private/qquickchangeset_p.h>
51
52
53 QT_BEGIN_HEADER
54
55 QT_BEGIN_NAMESPACE
56
57 QT_MODULE(Quick)
58
59
60 class FxViewItem : public QQuickViewItem
61 {
62 public:
63     FxViewItem(QQuickItem *, bool own);
64     virtual ~FxViewItem();
65
66     // these are positions and sizes along the current direction of scrolling/flicking
67     virtual qreal position() const = 0;
68     virtual qreal endPosition() const = 0;
69     virtual qreal size() const = 0;
70     virtual qreal sectionSize() const = 0;
71
72     virtual bool contains(qreal x, qreal y) const = 0;
73
74     QQuickItemViewAttached *attached;
75     bool ownItem;
76     bool releaseAfterTransition;
77 };
78
79
80 class QQuickItemViewChangeSet
81 {
82 public:
83     QQuickItemViewChangeSet();
84
85     bool hasPendingChanges() const;
86     void prepare(int currentIndex, int count);
87     void reset();
88
89     void applyChanges(const QQuickChangeSet &changeSet);
90
91     int itemCount;
92     int newCurrentIndex;
93     QQuickChangeSet pendingChanges;
94     QHash<QQuickChangeSet::MoveKey, FxViewItem *> removedItems;
95
96     bool active : 1;
97     bool currentChanged : 1;
98     bool currentRemoved : 1;
99 };
100
101
102 class QQuickItemViewPrivate : public QQuickFlickablePrivate, public QQuickItemViewTransitionChangeListener
103 {
104     Q_DECLARE_PUBLIC(QQuickItemView)
105 public:
106     QQuickItemViewPrivate();
107     ~QQuickItemViewPrivate();
108
109     struct ChangeResult {
110         QQmlNullableValue<qreal> visiblePos;
111         bool changedFirstItem;
112         qreal sizeChangesBeforeVisiblePos;
113         qreal sizeChangesAfterVisiblePos;
114         int countChangeBeforeVisible;
115         int countChangeAfterVisibleItems;
116
117         ChangeResult()
118             : visiblePos(0), changedFirstItem(false),
119               sizeChangesBeforeVisiblePos(0), sizeChangesAfterVisiblePos(0),
120               countChangeBeforeVisible(0), countChangeAfterVisibleItems(0) {}
121
122         ChangeResult(const QQmlNullableValue<qreal> &p)
123             : visiblePos(p), changedFirstItem(false),
124               sizeChangesBeforeVisiblePos(0), sizeChangesAfterVisiblePos(0),
125               countChangeBeforeVisible(0), countChangeAfterVisibleItems(0) {}
126
127         ChangeResult &operator+=(const ChangeResult &other) {
128             if (&other == this)
129                 return *this;
130             changedFirstItem &= other.changedFirstItem;
131             sizeChangesBeforeVisiblePos += other.sizeChangesBeforeVisiblePos;
132             sizeChangesAfterVisiblePos += other.sizeChangesAfterVisiblePos;
133             countChangeBeforeVisible += other.countChangeBeforeVisible;
134             countChangeAfterVisibleItems += other.countChangeAfterVisibleItems;
135             return *this;
136         }
137
138         void reset() {
139             changedFirstItem = false;
140             sizeChangesBeforeVisiblePos = 0.0;
141             sizeChangesAfterVisiblePos = 0.0;
142             countChangeBeforeVisible = 0;
143             countChangeAfterVisibleItems = 0;
144         }
145     };
146
147     enum BufferMode { NoBuffer = 0x00, BufferBefore = 0x01, BufferAfter = 0x02 };
148     enum MovementReason { Other, SetIndex, Mouse };
149
150     bool isValid() const;
151     qreal position() const;
152     qreal size() const;
153     qreal startPosition() const;
154     qreal endPosition() const;
155     qreal contentStartOffset() const;
156     int findLastVisibleIndex(int defaultValue = -1) const;
157     FxViewItem *visibleItem(int modelIndex) const;
158     FxViewItem *firstVisibleItem() const;
159     int findLastIndexInView() const;
160     int mapFromModel(int modelIndex) const;
161
162     virtual void init();
163     virtual void clear();
164     virtual void updateViewport();
165
166     void regenerate();
167     void layout();
168     void refill();
169     void refill(qreal from, qreal to);
170     void mirrorChange();
171
172     FxViewItem *createItem(int modelIndex, bool asynchronous = false);
173     virtual bool releaseItem(FxViewItem *item);
174
175     QQuickItem *createHighlightItem();
176     QQuickItem *createComponentItem(QQmlComponent *component, bool receiveItemGeometryChanges, bool createDefault = false);
177
178     void updateCurrent(int modelIndex);
179     void updateTrackedItem();
180     void updateUnrequestedIndexes();
181     void updateUnrequestedPositions();
182     void updateVisibleIndex();
183     void positionViewAtIndex(int index, int mode);
184     void applyPendingChanges();
185     bool applyModelChanges(ChangeResult *insertionResult, ChangeResult *removalResult);
186     bool applyRemovalChange(const QQuickChangeSet::Remove &removal, ChangeResult *changeResult, int *removedCount);
187     void removeItem(FxViewItem *item, const QQuickChangeSet::Remove &removal, ChangeResult *removeResult);
188     void repositionFirstItem(FxViewItem *prevVisibleItemsFirst, qreal prevVisibleItemsFirstPos,
189             FxViewItem *prevFirstVisible, ChangeResult *insertionResult, ChangeResult *removalResult);
190
191     void createTransitioner();
192     void prepareVisibleItemTransitions();
193     void prepareRemoveTransitions(QHash<QQuickChangeSet::MoveKey, FxViewItem *> *removedItems);
194     bool prepareNonVisibleItemTransition(FxViewItem *item, const QRectF &viewBounds);
195     virtual void viewItemTransitionFinished(QQuickViewItem *item);
196
197     int findMoveKeyIndex(QQuickChangeSet::MoveKey key, const QVector<QQuickChangeSet::Remove> &changes) const;
198
199     void checkVisible() const;
200     void showVisibleItems() const;
201
202     void markExtentsDirty() {
203         if (layoutOrientation() == Qt::Vertical)
204             vData.markExtentsDirty();
205         else
206             hData.markExtentsDirty();
207     }
208
209     QQmlGuard<QQuickVisualModel> model;
210     QVariant modelVariant;
211     int itemCount;
212     int buffer;
213     int bufferMode;
214     Qt::LayoutDirection layoutDirection;
215
216     MovementReason moveReason;
217
218     QList<FxViewItem *> visibleItems;
219     int visibleIndex;
220     int currentIndex;
221     FxViewItem *currentItem;
222     FxViewItem *trackedItem;
223     QHash<QQuickItem*,int> unrequestedItems;
224     int requestedIndex;
225     FxViewItem *requestedItem;
226     QQuickItemViewChangeSet currentChanges;
227
228     QQmlComponent *highlightComponent;
229     FxViewItem *highlight;
230     int highlightRange;     // enum value
231     qreal highlightRangeStart;
232     qreal highlightRangeEnd;
233     int highlightMoveDuration;
234
235     QQmlComponent *headerComponent;
236     FxViewItem *header;
237     QQmlComponent *footerComponent;
238     FxViewItem *footer;
239
240     struct MovedItem {
241         FxViewItem *item;
242         QQuickChangeSet::MoveKey moveKey;
243         MovedItem(FxViewItem *i, QQuickChangeSet::MoveKey k)
244             : item(i), moveKey(k) {}
245     };
246     QQuickItemViewTransitioner *transitioner;
247     QList<FxViewItem *> releasePendingTransition;
248
249     mutable qreal minExtent;
250     mutable qreal maxExtent;
251
252     bool ownModel : 1;
253     bool wrap : 1;
254     bool disableLayout : 1;
255     bool inViewportMoved : 1;
256     bool forceLayout : 1;
257     bool currentIndexCleared : 1;
258     bool haveHighlightRange : 1;
259     bool autoHighlight : 1;
260     bool highlightRangeStartValid : 1;
261     bool highlightRangeEndValid : 1;
262     bool fillCacheBuffer : 1;
263     bool inRequest : 1;
264     bool requestedAsync : 1;
265     bool runDelayedRemoveTransition : 1;
266
267 protected:
268     virtual Qt::Orientation layoutOrientation() const = 0;
269     virtual bool isContentFlowReversed() const = 0;
270
271     virtual qreal positionAt(int index) const = 0;
272     virtual qreal endPositionAt(int index) const = 0;
273     virtual qreal originPosition() const = 0;
274     virtual qreal lastPosition() const = 0;
275
276     virtual qreal headerSize() const = 0;
277     virtual qreal footerSize() const = 0;
278     virtual bool showHeaderForIndex(int index) const = 0;
279     virtual bool showFooterForIndex(int index) const = 0;
280     virtual void updateHeader() = 0;
281     virtual void updateFooter() = 0;
282
283     virtual void createHighlight() = 0;
284     virtual void updateHighlight() = 0;
285     virtual void resetHighlightPosition() = 0;
286
287     virtual void setPosition(qreal pos) = 0;
288     virtual void fixupPosition() = 0;
289
290     virtual bool addVisibleItems(qreal fillFrom, qreal fillTo, bool doBuffer) = 0;
291     virtual bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo) = 0;
292     virtual void visibleItemsChanged() {}
293
294     virtual FxViewItem *newViewItem(int index, QQuickItem *item) = 0;
295     virtual void repositionItemAt(FxViewItem *item, int index, qreal sizeBuffer) = 0;
296     virtual void repositionPackageItemAt(QQuickItem *item, int index) = 0;
297     virtual void resetFirstItemPosition(qreal pos = 0.0) = 0;
298     virtual void adjustFirstItem(qreal forwards, qreal backwards, int changeBeforeVisible) = 0;
299
300     virtual void layoutVisibleItems(int fromModelIndex = 0) = 0;
301     virtual void changedVisibleIndex(int newIndex) = 0;
302
303     virtual bool applyInsertionChange(const QQuickChangeSet::Insert &insert, ChangeResult *changeResult,
304                 QList<FxViewItem *> *newItems, QList<MovedItem> *movingIntoView) = 0;
305
306     virtual bool needsRefillForAddedOrRemovedIndex(int) const { return false; }
307     virtual void translateAndTransitionItemsAfter(int afterIndex, const ChangeResult &insertionResult, const ChangeResult &removalResult) = 0;
308
309     virtual void initializeViewItem(FxViewItem *) {}
310     virtual void initializeCurrentItem() {}
311     virtual void updateSections() {}
312
313     virtual void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
314 };
315
316
317 QT_END_NAMESPACE
318
319 QT_END_HEADER
320
321 #endif // QQUICKITEMVIEW_P_P_H