1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QQUICKITEMVIEW_P_P_H
43 #define QQUICKITEMVIEW_P_P_H
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>
60 class FxViewItem : public QQuickViewItem
63 FxViewItem(QQuickItem *, bool own);
64 virtual ~FxViewItem();
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;
72 virtual bool contains(qreal x, qreal y) const = 0;
74 QQuickItemViewAttached *attached;
76 bool releaseAfterTransition;
80 class QQuickItemViewChangeSet
83 QQuickItemViewChangeSet();
85 bool hasPendingChanges() const;
86 void prepare(int currentIndex, int count);
89 void applyChanges(const QQuickChangeSet &changeSet);
93 QQuickChangeSet pendingChanges;
94 QHash<QQuickChangeSet::MoveKey, FxViewItem *> removedItems;
97 bool currentChanged : 1;
98 bool currentRemoved : 1;
102 class QQuickItemViewPrivate : public QQuickFlickablePrivate, public QQuickItemViewTransitionChangeListener
104 Q_DECLARE_PUBLIC(QQuickItemView)
106 QQuickItemViewPrivate();
107 ~QQuickItemViewPrivate();
109 struct ChangeResult {
110 QQmlNullableValue<qreal> visiblePos;
111 bool changedFirstItem;
112 qreal sizeChangesBeforeVisiblePos;
113 qreal sizeChangesAfterVisiblePos;
114 int countChangeBeforeVisible;
115 int countChangeAfterVisibleItems;
118 : visiblePos(0), changedFirstItem(false),
119 sizeChangesBeforeVisiblePos(0), sizeChangesAfterVisiblePos(0),
120 countChangeBeforeVisible(0), countChangeAfterVisibleItems(0) {}
122 ChangeResult(const QQmlNullableValue<qreal> &p)
123 : visiblePos(p), changedFirstItem(false),
124 sizeChangesBeforeVisiblePos(0), sizeChangesAfterVisiblePos(0),
125 countChangeBeforeVisible(0), countChangeAfterVisibleItems(0) {}
127 ChangeResult &operator+=(const ChangeResult &other) {
130 changedFirstItem &= other.changedFirstItem;
131 sizeChangesBeforeVisiblePos += other.sizeChangesBeforeVisiblePos;
132 sizeChangesAfterVisiblePos += other.sizeChangesAfterVisiblePos;
133 countChangeBeforeVisible += other.countChangeBeforeVisible;
134 countChangeAfterVisibleItems += other.countChangeAfterVisibleItems;
139 changedFirstItem = false;
140 sizeChangesBeforeVisiblePos = 0.0;
141 sizeChangesAfterVisiblePos = 0.0;
142 countChangeBeforeVisible = 0;
143 countChangeAfterVisibleItems = 0;
147 enum BufferMode { NoBuffer = 0x00, BufferBefore = 0x01, BufferAfter = 0x02 };
148 enum MovementReason { Other, SetIndex, Mouse };
150 bool isValid() const;
151 qreal position() 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;
163 virtual void clear();
164 virtual void updateViewport();
169 void refill(qreal from, qreal to);
172 FxViewItem *createItem(int modelIndex, bool asynchronous = false);
173 virtual bool releaseItem(FxViewItem *item);
175 QQuickItem *createHighlightItem();
176 QQuickItem *createComponentItem(QQmlComponent *component, bool receiveItemGeometryChanges, bool createDefault = false);
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);
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);
197 int findMoveKeyIndex(QQuickChangeSet::MoveKey key, const QVector<QQuickChangeSet::Remove> &changes) const;
199 void checkVisible() const;
200 void showVisibleItems() const;
202 void markExtentsDirty() {
203 if (layoutOrientation() == Qt::Vertical)
204 vData.markExtentsDirty();
206 hData.markExtentsDirty();
209 QQmlGuard<QQuickVisualModel> model;
210 QVariant modelVariant;
214 Qt::LayoutDirection layoutDirection;
216 MovementReason moveReason;
218 QList<FxViewItem *> visibleItems;
221 FxViewItem *currentItem;
222 FxViewItem *trackedItem;
223 QHash<QQuickItem*,int> unrequestedItems;
225 FxViewItem *requestedItem;
226 QQuickItemViewChangeSet currentChanges;
228 QQmlComponent *highlightComponent;
229 FxViewItem *highlight;
230 int highlightRange; // enum value
231 qreal highlightRangeStart;
232 qreal highlightRangeEnd;
233 int highlightMoveDuration;
235 QQmlComponent *headerComponent;
237 QQmlComponent *footerComponent;
242 QQuickChangeSet::MoveKey moveKey;
243 MovedItem(FxViewItem *i, QQuickChangeSet::MoveKey k)
244 : item(i), moveKey(k) {}
246 QQuickItemViewTransitioner *transitioner;
247 QList<FxViewItem *> releasePendingTransition;
249 mutable qreal minExtent;
250 mutable qreal maxExtent;
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;
264 bool requestedAsync : 1;
265 bool runDelayedRemoveTransition : 1;
268 virtual Qt::Orientation layoutOrientation() const = 0;
269 virtual bool isContentFlowReversed() const = 0;
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;
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;
283 virtual void createHighlight() = 0;
284 virtual void updateHighlight() = 0;
285 virtual void resetHighlightPosition() = 0;
287 virtual void setPosition(qreal pos) = 0;
288 virtual void fixupPosition() = 0;
290 virtual bool addVisibleItems(qreal fillFrom, qreal fillTo, bool doBuffer) = 0;
291 virtual bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo) = 0;
292 virtual void visibleItemsChanged() {}
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;
300 virtual void layoutVisibleItems(int fromModelIndex = 0) = 0;
301 virtual void changedVisibleIndex(int newIndex) = 0;
303 virtual bool applyInsertionChange(const QQuickChangeSet::Insert &insert, ChangeResult *changeResult,
304 QList<FxViewItem *> *newItems, QList<MovedItem> *movingIntoView) = 0;
306 virtual bool needsRefillForAddedOrRemovedIndex(int) const { return false; }
307 virtual void translateAndTransitionItemsAfter(int afterIndex, const ChangeResult &insertionResult, const ChangeResult &removalResult) = 0;
309 virtual void initializeViewItem(FxViewItem *) {}
310 virtual void initializeCurrentItem() {}
311 virtual void updateSections() {}
313 virtual void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
321 #endif // QQUICKITEMVIEW_P_P_H