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 QQmlGuard<QQuickVisualModel> model;
251 QVariant modelVariant;
255 Qt::LayoutDirection layoutDirection;
256 QQuickItemView::VerticalLayoutDirection verticalLayoutDirection;
258 MovementReason moveReason;
260 QList<FxViewItem *> visibleItems;
263 FxViewItem *currentItem;
264 FxViewItem *trackedItem;
265 QHash<QQuickItem*,int> unrequestedItems;
267 QQuickItemViewChangeSet currentChanges;
268 QQuickItemViewChangeSet bufferedChanges;
269 QPauseAnimationJob bufferPause;
271 QQmlComponent *highlightComponent;
272 FxViewItem *highlight;
273 int highlightRange; // enum value
274 qreal highlightRangeStart;
275 qreal highlightRangeEnd;
276 int highlightMoveDuration;
278 QQmlComponent *headerComponent;
280 QQmlComponent *footerComponent;
285 QQuickChangeSet::MoveKey moveKey;
286 MovedItem(FxViewItem *i, QQuickChangeSet::MoveKey k)
287 : item(i), moveKey(k) {}
289 QQuickItemViewTransitioner *transitioner;
290 QList<FxViewItem *> releasePendingTransition;
292 mutable qreal minExtent;
293 mutable qreal maxExtent;
298 bool inViewportMoved : 1;
299 bool forceLayout : 1;
300 bool currentIndexCleared : 1;
301 bool haveHighlightRange : 1;
302 bool autoHighlight : 1;
303 bool highlightRangeStartValid : 1;
304 bool highlightRangeEndValid : 1;
305 bool fillCacheBuffer : 1;
307 bool runDelayedRemoveTransition : 1;
310 virtual Qt::Orientation layoutOrientation() const = 0;
311 virtual bool isContentFlowReversed() const = 0;
313 virtual qreal positionAt(int index) const = 0;
314 virtual qreal endPositionAt(int index) const = 0;
315 virtual qreal originPosition() const = 0;
316 virtual qreal lastPosition() const = 0;
318 virtual qreal headerSize() const = 0;
319 virtual qreal footerSize() const = 0;
320 virtual bool showHeaderForIndex(int index) const = 0;
321 virtual bool showFooterForIndex(int index) const = 0;
322 virtual void updateHeader() = 0;
323 virtual void updateFooter() = 0;
325 virtual void createHighlight() = 0;
326 virtual void updateHighlight() = 0;
327 virtual void resetHighlightPosition() = 0;
329 virtual void setPosition(qreal pos) = 0;
330 virtual void fixupPosition() = 0;
332 virtual bool addVisibleItems(qreal fillFrom, qreal fillTo, qreal bufferFrom, qreal bufferTo, bool doBuffer) = 0;
333 virtual bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo) = 0;
334 virtual void visibleItemsChanged() {}
336 virtual FxViewItem *newViewItem(int index, QQuickItem *item) = 0;
337 virtual void repositionItemAt(FxViewItem *item, int index, qreal sizeBuffer) = 0;
338 virtual void repositionPackageItemAt(QQuickItem *item, int index) = 0;
339 virtual void resetFirstItemPosition(qreal pos = 0.0) = 0;
340 virtual void adjustFirstItem(qreal forwards, qreal backwards, int changeBeforeVisible) = 0;
342 virtual void layoutVisibleItems(int fromModelIndex = 0) = 0;
343 virtual void changedVisibleIndex(int newIndex) = 0;
345 virtual bool applyInsertionChange(const QQuickChangeSet::Insert &insert, ChangeResult *changeResult,
346 QList<FxViewItem *> *newItems, QList<MovedItem> *movingIntoView) = 0;
348 virtual bool needsRefillForAddedOrRemovedIndex(int) const { return false; }
349 virtual void translateAndTransitionItemsAfter(int afterIndex, const ChangeResult &insertionResult, const ChangeResult &removalResult) = 0;
351 virtual void initializeViewItem(FxViewItem *) {}
352 virtual void initializeCurrentItem() {}
353 virtual void updateSections() {}
355 virtual void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
363 #endif // QQUICKITEMVIEW_P_P_H