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>
63 FxViewItem(QQuickItem *, bool own, bool trackGeometry);
64 virtual ~FxViewItem();
69 void moveTo(const QPointF &pos, bool immediate);
70 void setVisible(bool visible);
72 QQuickItemViewTransitioner::TransitionType scheduledTransitionType() const;
73 bool transitionScheduledOrRunning() const;
74 bool transitionRunning() const;
75 bool isPendingRemoval() const;
77 void transitionNextReposition(QQuickItemViewTransitioner *transitioner, QQuickItemViewTransitioner::TransitionType type, bool asTarget);
78 bool prepareTransition(QQuickItemViewTransitioner *transitioner, const QRectF &viewBounds);
79 void startTransition(QQuickItemViewTransitioner *transitioner);
81 // these are positions and sizes along the current direction of scrolling/flicking
82 virtual qreal position() const = 0;
83 virtual qreal endPosition() const = 0;
84 virtual qreal size() const = 0;
85 virtual qreal sectionSize() const = 0;
87 virtual bool contains(qreal x, qreal y) const = 0;
90 QQuickItemViewTransitionableItem *transitionableItem;
91 QQuickItemViewAttached *attached;
94 bool releaseAfterTransition;
99 class QQuickItemViewChangeSet
102 QQuickItemViewChangeSet();
104 bool hasPendingChanges() const;
105 void prepare(int currentIndex, int count);
108 void applyChanges(const QQuickChangeSet &changeSet);
110 void applyBufferedChanges(const QQuickItemViewChangeSet &other);
114 QQuickChangeSet pendingChanges;
115 QHash<QQuickChangeSet::MoveKey, FxViewItem *> removedItems;
118 bool currentChanged : 1;
119 bool currentRemoved : 1;
123 class QQuickItemViewPrivate : public QQuickFlickablePrivate, public QQuickItemViewTransitionChangeListener, public QAnimationJobChangeListener
125 Q_DECLARE_PUBLIC(QQuickItemView)
127 QQuickItemViewPrivate();
128 ~QQuickItemViewPrivate();
130 static inline QQuickItemViewPrivate *get(QQuickItemView *o) { return o->d_func(); }
132 struct ChangeResult {
133 QQmlNullableValue<qreal> visiblePos;
134 bool changedFirstItem;
135 qreal sizeChangesBeforeVisiblePos;
136 qreal sizeChangesAfterVisiblePos;
137 int countChangeBeforeVisible;
138 int countChangeAfterVisibleItems;
141 : visiblePos(0), changedFirstItem(false),
142 sizeChangesBeforeVisiblePos(0), sizeChangesAfterVisiblePos(0),
143 countChangeBeforeVisible(0), countChangeAfterVisibleItems(0) {}
145 ChangeResult(const QQmlNullableValue<qreal> &p)
146 : visiblePos(p), changedFirstItem(false),
147 sizeChangesBeforeVisiblePos(0), sizeChangesAfterVisiblePos(0),
148 countChangeBeforeVisible(0), countChangeAfterVisibleItems(0) {}
150 ChangeResult &operator+=(const ChangeResult &other) {
153 changedFirstItem &= other.changedFirstItem;
154 sizeChangesBeforeVisiblePos += other.sizeChangesBeforeVisiblePos;
155 sizeChangesAfterVisiblePos += other.sizeChangesAfterVisiblePos;
156 countChangeBeforeVisible += other.countChangeBeforeVisible;
157 countChangeAfterVisibleItems += other.countChangeAfterVisibleItems;
162 changedFirstItem = false;
163 sizeChangesBeforeVisiblePos = 0.0;
164 sizeChangesAfterVisiblePos = 0.0;
165 countChangeBeforeVisible = 0;
166 countChangeAfterVisibleItems = 0;
170 enum BufferMode { NoBuffer = 0x00, BufferBefore = 0x01, BufferAfter = 0x02 };
171 enum MovementReason { Other, SetIndex, Mouse };
173 bool isValid() const;
174 qreal position() const;
176 qreal startPosition() const;
177 qreal endPosition() const;
178 qreal contentStartOffset() const;
179 int findLastVisibleIndex(int defaultValue = -1) const;
180 FxViewItem *visibleItem(int modelIndex) const;
181 FxViewItem *firstVisibleItem() const;
182 int findLastIndexInView() const;
183 int mapFromModel(int modelIndex) const;
186 virtual void clear();
187 virtual void updateViewport();
191 virtual void animationFinished(QAbstractAnimationJob *);
193 void refill(qreal from, qreal to);
196 FxViewItem *createItem(int modelIndex, bool asynchronous = false);
197 virtual bool releaseItem(FxViewItem *item);
199 QQuickItem *createHighlightItem();
200 QQuickItem *createComponentItem(QQmlComponent *component, qreal zValue, bool createDefault = false);
202 void updateCurrent(int modelIndex);
203 void updateTrackedItem();
204 void updateUnrequestedIndexes();
205 void updateUnrequestedPositions();
206 void updateVisibleIndex();
207 void positionViewAtIndex(int index, int mode);
209 qreal minExtentForAxis(const AxisData &axisData, bool forXAxis) const;
210 qreal maxExtentForAxis(const AxisData &axisData, bool forXAxis) const;
212 void applyPendingChanges();
213 bool applyModelChanges(ChangeResult *insertionResult, ChangeResult *removalResult);
214 bool applyRemovalChange(const QQuickChangeSet::Remove &removal, ChangeResult *changeResult, int *removedCount);
215 void removeItem(FxViewItem *item, const QQuickChangeSet::Remove &removal, ChangeResult *removeResult);
216 void repositionFirstItem(FxViewItem *prevVisibleItemsFirst, qreal prevVisibleItemsFirstPos,
217 FxViewItem *prevFirstVisible, ChangeResult *insertionResult, ChangeResult *removalResult);
219 void createTransitioner();
220 void prepareVisibleItemTransitions();
221 void prepareRemoveTransitions(QHash<QQuickChangeSet::MoveKey, FxViewItem *> *removedItems);
222 bool prepareNonVisibleItemTransition(FxViewItem *item, const QRectF &viewBounds);
223 virtual void viewItemTransitionFinished(QQuickItemViewTransitionableItem *item);
225 int findMoveKeyIndex(QQuickChangeSet::MoveKey key, const QVector<QQuickChangeSet::Remove> &changes) const;
227 void checkVisible() const;
228 void showVisibleItems() const;
230 void markExtentsDirty() {
231 if (layoutOrientation() == Qt::Vertical)
232 vData.markExtentsDirty();
234 hData.markExtentsDirty();
237 bool hasPendingChanges() const {
238 return currentChanges.hasPendingChanges()
239 || bufferedChanges.hasPendingChanges()
240 ||runDelayedRemoveTransition;
243 void refillOrLayout() {
244 if (hasPendingChanges())
250 void forceLayoutPolish() {
256 QQmlGuard<QQuickVisualModel> model;
257 QVariant modelVariant;
261 Qt::LayoutDirection layoutDirection;
262 QQuickItemView::VerticalLayoutDirection verticalLayoutDirection;
264 MovementReason moveReason;
266 QList<FxViewItem *> visibleItems;
269 FxViewItem *currentItem;
270 FxViewItem *trackedItem;
271 QHash<QQuickItem*,int> unrequestedItems;
273 QQuickItemViewChangeSet currentChanges;
274 QQuickItemViewChangeSet bufferedChanges;
275 QPauseAnimationJob bufferPause;
277 QQmlComponent *highlightComponent;
278 FxViewItem *highlight;
279 int highlightRange; // enum value
280 qreal highlightRangeStart;
281 qreal highlightRangeEnd;
282 int highlightMoveDuration;
284 QQmlComponent *headerComponent;
286 QQmlComponent *footerComponent;
291 QQuickChangeSet::MoveKey moveKey;
292 MovedItem(FxViewItem *i, QQuickChangeSet::MoveKey k)
293 : item(i), moveKey(k) {}
295 QQuickItemViewTransitioner *transitioner;
296 QList<FxViewItem *> releasePendingTransition;
298 mutable qreal minExtent;
299 mutable qreal maxExtent;
304 bool inViewportMoved : 1;
305 bool forceLayout : 1;
306 bool currentIndexCleared : 1;
307 bool haveHighlightRange : 1;
308 bool autoHighlight : 1;
309 bool highlightRangeStartValid : 1;
310 bool highlightRangeEndValid : 1;
311 bool fillCacheBuffer : 1;
313 bool runDelayedRemoveTransition : 1;
316 virtual Qt::Orientation layoutOrientation() const = 0;
317 virtual bool isContentFlowReversed() const = 0;
319 virtual qreal positionAt(int index) const = 0;
320 virtual qreal endPositionAt(int index) const = 0;
321 virtual qreal originPosition() const = 0;
322 virtual qreal lastPosition() const = 0;
324 virtual qreal headerSize() const = 0;
325 virtual qreal footerSize() const = 0;
326 virtual bool showHeaderForIndex(int index) const = 0;
327 virtual bool showFooterForIndex(int index) const = 0;
328 virtual void updateHeader() = 0;
329 virtual void updateFooter() = 0;
331 virtual void createHighlight() = 0;
332 virtual void updateHighlight() = 0;
333 virtual void resetHighlightPosition() = 0;
335 virtual void setPosition(qreal pos) = 0;
336 virtual void fixupPosition() = 0;
338 virtual bool addVisibleItems(qreal fillFrom, qreal fillTo, qreal bufferFrom, qreal bufferTo, bool doBuffer) = 0;
339 virtual bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo) = 0;
340 virtual void visibleItemsChanged() {}
342 virtual FxViewItem *newViewItem(int index, QQuickItem *item) = 0;
343 virtual void repositionItemAt(FxViewItem *item, int index, qreal sizeBuffer) = 0;
344 virtual void repositionPackageItemAt(QQuickItem *item, int index) = 0;
345 virtual void resetFirstItemPosition(qreal pos = 0.0) = 0;
346 virtual void adjustFirstItem(qreal forwards, qreal backwards, int changeBeforeVisible) = 0;
348 virtual void layoutVisibleItems(int fromModelIndex = 0) = 0;
349 virtual void changedVisibleIndex(int newIndex) = 0;
351 virtual bool applyInsertionChange(const QQuickChangeSet::Insert &insert, ChangeResult *changeResult,
352 QList<FxViewItem *> *newItems, QList<MovedItem> *movingIntoView) = 0;
354 virtual bool needsRefillForAddedOrRemovedIndex(int) const { return false; }
355 virtual void translateAndTransitionItemsAfter(int afterIndex, const ChangeResult &insertionResult, const ChangeResult &removalResult) = 0;
357 virtual void initializeViewItem(FxViewItem *) {}
358 virtual void initializeCurrentItem() {}
359 virtual void updateSectionCriteria() {}
360 virtual void updateSections() {}
362 virtual void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
370 #endif // QQUICKITEMVIEW_P_P_H