1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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 "qquickflickable_p_p.h"
47 #include "qquickvisualdatamodel_p.h"
48 #include <private/qdeclarativechangeset_p.h>
58 FxViewItem(QQuickItem *, bool own);
61 // these are positions and sizes along the current direction of scrolling/flicking
62 virtual qreal position() const = 0;
63 virtual qreal endPosition() const = 0;
64 virtual qreal size() const = 0;
65 virtual qreal sectionSize() const = 0;
67 virtual bool contains(qreal x, qreal y) const = 0;
72 QQuickItemViewAttached *attached;
75 class QQuickItemViewChangeSet
78 QQuickItemViewChangeSet();
80 bool hasPendingChanges() const;
81 void prepare(int currentIndex, int count);
84 void applyChanges(const QDeclarativeChangeSet &changeSet);
88 QDeclarativeChangeSet pendingChanges;
89 QHash<QDeclarativeChangeSet::MoveKey, FxViewItem *> removedItems;
92 bool currentChanged : 1;
93 bool currentRemoved : 1;
96 class QQuickItemViewPrivate : public QQuickFlickablePrivate
98 Q_DECLARE_PUBLIC(QQuickItemView)
100 QQuickItemViewPrivate();
102 struct ChangeResult {
103 QDeclarativeNullableValue<qreal> visiblePos;
104 qreal sizeChangesBeforeVisiblePos;
105 qreal sizeChangesAfterVisiblePos;
106 bool changedFirstItem;
107 int changeBeforeVisible;
109 ChangeResult(const QDeclarativeNullableValue<qreal> &p)
110 : visiblePos(p), sizeChangesBeforeVisiblePos(0), sizeChangesAfterVisiblePos(0),
111 changedFirstItem(false), changeBeforeVisible(0) {}
114 sizeChangesBeforeVisiblePos = 0.0;
115 sizeChangesAfterVisiblePos = 0.0;
116 changedFirstItem = false;
117 changeBeforeVisible = 0;
121 enum BufferMode { NoBuffer = 0x00, BufferBefore = 0x01, BufferAfter = 0x02 };
122 enum MovementReason { Other, SetIndex, Mouse };
124 bool isValid() const;
125 qreal position() const;
127 qreal startPosition() const;
128 qreal endPosition() const;
129 qreal contentStartPosition() const;
130 int findLastVisibleIndex(int defaultValue = -1) const;
131 FxViewItem *visibleItem(int modelIndex) const;
132 FxViewItem *firstVisibleItem() const;
133 int mapFromModel(int modelIndex) const;
136 virtual void clear();
137 virtual void updateViewport();
142 void refill(qreal from, qreal to);
145 FxViewItem *createItem(int modelIndex, bool asynchronous = false);
146 virtual void releaseItem(FxViewItem *item);
148 QQuickItem *createHighlightItem();
149 QQuickItem *createComponentItem(QDeclarativeComponent *component, bool receiveItemGeometryChanges, bool createDefault = false);
151 void updateCurrent(int modelIndex);
152 void updateTrackedItem();
153 void updateUnrequestedIndexes();
154 void updateUnrequestedPositions();
155 void updateVisibleIndex();
156 void positionViewAtIndex(int index, int mode);
157 void applyPendingChanges();
158 bool applyModelChanges();
159 bool applyRemovalChange(const QDeclarativeChangeSet::Remove &removal, ChangeResult *changeResult, int *removedCount);
160 void repositionFirstItem(FxViewItem *prevVisibleItemsFirst, qreal prevVisibleItemsFirstPos,
161 FxViewItem *prevFirstVisible, ChangeResult *insertionResult, ChangeResult *removalResult);
163 void checkVisible() const;
165 void markExtentsDirty() {
166 if (layoutOrientation() == Qt::Vertical)
167 vData.markExtentsDirty();
169 hData.markExtentsDirty();
172 QDeclarativeGuard<QQuickVisualModel> model;
173 QVariant modelVariant;
177 Qt::LayoutDirection layoutDirection;
179 MovementReason moveReason;
181 QList<FxViewItem *> visibleItems;
184 FxViewItem *currentItem;
185 FxViewItem *trackedItem;
186 QHash<QQuickItem*,int> unrequestedItems;
188 FxViewItem *requestedItem;
189 QQuickItemViewChangeSet currentChanges;
191 // XXX split into struct
192 QDeclarativeComponent *highlightComponent;
193 FxViewItem *highlight;
194 int highlightRange; // enum value
195 qreal highlightRangeStart;
196 qreal highlightRangeEnd;
197 int highlightMoveDuration;
199 QDeclarativeComponent *headerComponent;
201 QDeclarativeComponent *footerComponent;
204 mutable qreal minExtent;
205 mutable qreal maxExtent;
209 bool inApplyModelChanges : 1;
210 bool inViewportMoved : 1;
211 bool forceLayout : 1;
212 bool currentIndexCleared : 1;
213 bool haveHighlightRange : 1;
214 bool autoHighlight : 1;
215 bool highlightRangeStartValid : 1;
216 bool highlightRangeEndValid : 1;
217 bool fillCacheBuffer : 1;
219 bool requestedAsync : 1;
222 virtual Qt::Orientation layoutOrientation() const = 0;
223 virtual bool isContentFlowReversed() const = 0;
225 virtual qreal positionAt(int index) const = 0;
226 virtual qreal endPositionAt(int index) const = 0;
227 virtual qreal originPosition() const = 0;
228 virtual qreal lastPosition() const = 0;
230 virtual qreal headerSize() const = 0;
231 virtual qreal footerSize() const = 0;
232 virtual bool showHeaderForIndex(int index) const = 0;
233 virtual bool showFooterForIndex(int index) const = 0;
234 virtual void updateHeader() = 0;
235 virtual void updateFooter() = 0;
237 virtual void createHighlight() = 0;
238 virtual void updateHighlight() = 0;
239 virtual void resetHighlightPosition() = 0;
241 virtual void setPosition(qreal pos) = 0;
242 virtual void fixupPosition() = 0;
244 virtual bool addVisibleItems(qreal fillFrom, qreal fillTo, bool doBuffer) = 0;
245 virtual bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo) = 0;
246 virtual void visibleItemsChanged() = 0;
248 virtual FxViewItem *newViewItem(int index, QQuickItem *item) = 0;
249 virtual void repositionPackageItemAt(QQuickItem *item, int index) = 0;
250 virtual void resetFirstItemPosition(qreal pos = 0.0) = 0;
251 virtual void adjustFirstItem(qreal forwards, qreal backwards, int changeBeforeVisible) = 0;
253 virtual void layoutVisibleItems(int fromModelIndex = 0) = 0;
254 virtual void changedVisibleIndex(int newIndex) = 0;
255 virtual bool applyInsertionChange(const QDeclarativeChangeSet::Insert &insert, ChangeResult *changeResult, QList<FxViewItem *> *newItems) = 0;
257 virtual bool needsRefillForAddedOrRemovedIndex(int) const { return false; }
259 virtual void initializeViewItem(FxViewItem *) {}
260 virtual void initializeCurrentItem() {}
261 virtual void updateSections() {}
263 virtual void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
271 #endif // QQUICKITEMVIEW_P_P_H