1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQUICKITEMVIEW_P_H
5#define QQUICKITEMVIEW_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQuick/private/qtquickglobal_p.h>
19
20QT_REQUIRE_CONFIG(quick_itemview);
21
22#include "qquickflickable_p.h"
23
24#include <private/qqmldelegatemodel_p.h>
25
26#include <qpointer.h>
27#include <QtCore/QLoggingCategory>
28
29QT_BEGIN_NAMESPACE
30
31Q_DECLARE_LOGGING_CATEGORY(lcItemViewDelegateLifecycle)
32
33class QQmlChangeSet;
34class QQmlComponent;
35
36class QQuickItemViewPrivate;
37
38class Q_QUICK_EXPORT QQuickItemView : public QQuickFlickable
39{
40 Q_OBJECT
41
42 Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged)
43 Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
44 Q_PROPERTY(int count READ count NOTIFY countChanged)
45
46 Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
47 Q_PROPERTY(QQuickItem *currentItem READ currentItem NOTIFY currentItemChanged)
48
49 Q_PROPERTY(bool keyNavigationWraps READ isWrapEnabled WRITE setWrapEnabled NOTIFY keyNavigationWrapsChanged)
50 Q_PROPERTY(bool keyNavigationEnabled READ isKeyNavigationEnabled WRITE setKeyNavigationEnabled NOTIFY keyNavigationEnabledChanged REVISION(2, 7))
51 Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged)
52 Q_PROPERTY(int displayMarginBeginning READ displayMarginBeginning WRITE setDisplayMarginBeginning NOTIFY displayMarginBeginningChanged REVISION(2, 3))
53 Q_PROPERTY(int displayMarginEnd READ displayMarginEnd WRITE setDisplayMarginEnd NOTIFY displayMarginEndChanged REVISION(2, 3))
54
55 Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged)
56 Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged)
57 Q_PROPERTY(VerticalLayoutDirection verticalLayoutDirection READ verticalLayoutDirection WRITE setVerticalLayoutDirection NOTIFY verticalLayoutDirectionChanged)
58
59 Q_PROPERTY(QQmlComponent *header READ header WRITE setHeader NOTIFY headerChanged)
60 Q_PROPERTY(QQuickItem *headerItem READ headerItem NOTIFY headerItemChanged)
61 Q_PROPERTY(QQmlComponent *footer READ footer WRITE setFooter NOTIFY footerChanged)
62 Q_PROPERTY(QQuickItem *footerItem READ footerItem NOTIFY footerItemChanged)
63
64#if QT_CONFIG(quick_viewtransitions)
65 Q_PROPERTY(QQuickTransition *populate READ populateTransition WRITE setPopulateTransition NOTIFY populateTransitionChanged)
66 Q_PROPERTY(QQuickTransition *add READ addTransition WRITE setAddTransition NOTIFY addTransitionChanged)
67 Q_PROPERTY(QQuickTransition *addDisplaced READ addDisplacedTransition WRITE setAddDisplacedTransition NOTIFY addDisplacedTransitionChanged)
68 Q_PROPERTY(QQuickTransition *move READ moveTransition WRITE setMoveTransition NOTIFY moveTransitionChanged)
69 Q_PROPERTY(QQuickTransition *moveDisplaced READ moveDisplacedTransition WRITE setMoveDisplacedTransition NOTIFY moveDisplacedTransitionChanged)
70 Q_PROPERTY(QQuickTransition *remove READ removeTransition WRITE setRemoveTransition NOTIFY removeTransitionChanged)
71 Q_PROPERTY(QQuickTransition *removeDisplaced READ removeDisplacedTransition WRITE setRemoveDisplacedTransition NOTIFY removeDisplacedTransitionChanged)
72 Q_PROPERTY(QQuickTransition *displaced READ displacedTransition WRITE setDisplacedTransition NOTIFY displacedTransitionChanged)
73#endif
74
75 Q_PROPERTY(QQmlComponent *highlight READ highlight WRITE setHighlight NOTIFY highlightChanged)
76 Q_PROPERTY(QQuickItem *highlightItem READ highlightItem NOTIFY highlightItemChanged)
77 Q_PROPERTY(bool highlightFollowsCurrentItem READ highlightFollowsCurrentItem WRITE setHighlightFollowsCurrentItem NOTIFY highlightFollowsCurrentItemChanged)
78 Q_PROPERTY(HighlightRangeMode highlightRangeMode READ highlightRangeMode WRITE setHighlightRangeMode NOTIFY highlightRangeModeChanged)
79 Q_PROPERTY(qreal preferredHighlightBegin READ preferredHighlightBegin WRITE setPreferredHighlightBegin NOTIFY preferredHighlightBeginChanged RESET resetPreferredHighlightBegin)
80 Q_PROPERTY(qreal preferredHighlightEnd READ preferredHighlightEnd WRITE setPreferredHighlightEnd NOTIFY preferredHighlightEndChanged RESET resetPreferredHighlightEnd)
81 Q_PROPERTY(int highlightMoveDuration READ highlightMoveDuration WRITE setHighlightMoveDuration NOTIFY highlightMoveDurationChanged)
82
83 Q_PROPERTY(bool reuseItems READ reuseItems WRITE setReuseItems NOTIFY reuseItemsChanged REVISION(2, 15))
84 Q_PROPERTY(QQmlDelegateModel::DelegateModelAccess delegateModelAccess READ delegateModelAccess
85 WRITE setDelegateModelAccess NOTIFY delegateModelAccessChanged REVISION(6, 10) FINAL)
86
87 QML_NAMED_ELEMENT(ItemView)
88 QML_UNCREATABLE("ItemView is an abstract base class.")
89 QML_ADDED_IN_VERSION(2, 1)
90
91public:
92 // this holds all layout enum values so they can be referred to by other enums
93 // to ensure consistent values - e.g. QML references to GridView.TopToBottom flow
94 // and GridView.TopToBottom vertical layout direction should have same value
95 enum LayoutDirection {
96 LeftToRight = Qt::LeftToRight,
97 RightToLeft = Qt::RightToLeft,
98 VerticalTopToBottom,
99 VerticalBottomToTop
100 };
101 Q_ENUM(LayoutDirection)
102
103 enum VerticalLayoutDirection {
104 TopToBottom = VerticalTopToBottom,
105 BottomToTop = VerticalBottomToTop
106 };
107 Q_ENUM(VerticalLayoutDirection)
108
109 QQuickItemView(QQuickFlickablePrivate &dd, QQuickItem *parent = nullptr);
110 ~QQuickItemView();
111
112 QVariant model() const;
113 void setModel(const QVariant &);
114
115 QQmlComponent *delegate() const;
116 void setDelegate(QQmlComponent *);
117
118 int count() const;
119
120 int currentIndex() const;
121 void setCurrentIndex(int idx);
122
123 QQuickItem *currentItem() const;
124
125 bool isWrapEnabled() const;
126 void setWrapEnabled(bool);
127
128 bool isKeyNavigationEnabled() const;
129 void setKeyNavigationEnabled(bool);
130
131 int cacheBuffer() const;
132 void setCacheBuffer(int);
133
134 int displayMarginBeginning() const;
135 void setDisplayMarginBeginning(int);
136
137 int displayMarginEnd() const;
138 void setDisplayMarginEnd(int);
139
140 Qt::LayoutDirection layoutDirection() const;
141 void setLayoutDirection(Qt::LayoutDirection);
142 Qt::LayoutDirection effectiveLayoutDirection() const;
143
144 VerticalLayoutDirection verticalLayoutDirection() const;
145 void setVerticalLayoutDirection(VerticalLayoutDirection layoutDirection);
146
147 QQmlComponent *footer() const;
148 void setFooter(QQmlComponent *);
149 QQuickItem *footerItem() const;
150
151 QQmlComponent *header() const;
152 void setHeader(QQmlComponent *);
153 QQuickItem *headerItem() const;
154
155#if QT_CONFIG(quick_viewtransitions)
156 QQuickTransition *populateTransition() const;
157 void setPopulateTransition(QQuickTransition *transition);
158
159 QQuickTransition *addTransition() const;
160 void setAddTransition(QQuickTransition *transition);
161
162 QQuickTransition *addDisplacedTransition() const;
163 void setAddDisplacedTransition(QQuickTransition *transition);
164
165 QQuickTransition *moveTransition() const;
166 void setMoveTransition(QQuickTransition *transition);
167
168 QQuickTransition *moveDisplacedTransition() const;
169 void setMoveDisplacedTransition(QQuickTransition *transition);
170
171 QQuickTransition *removeTransition() const;
172 void setRemoveTransition(QQuickTransition *transition);
173
174 QQuickTransition *removeDisplacedTransition() const;
175 void setRemoveDisplacedTransition(QQuickTransition *transition);
176
177 QQuickTransition *displacedTransition() const;
178 void setDisplacedTransition(QQuickTransition *transition);
179#endif
180
181 QQmlComponent *highlight() const;
182 void setHighlight(QQmlComponent *);
183
184 QQuickItem *highlightItem() const;
185
186 bool highlightFollowsCurrentItem() const;
187 virtual void setHighlightFollowsCurrentItem(bool);
188
189 enum HighlightRangeMode { NoHighlightRange, ApplyRange, StrictlyEnforceRange };
190 Q_ENUM(HighlightRangeMode)
191 HighlightRangeMode highlightRangeMode() const;
192 void setHighlightRangeMode(HighlightRangeMode mode);
193
194 qreal preferredHighlightBegin() const;
195 void setPreferredHighlightBegin(qreal);
196 void resetPreferredHighlightBegin();
197
198 qreal preferredHighlightEnd() const;
199 void setPreferredHighlightEnd(qreal);
200 void resetPreferredHighlightEnd();
201
202 int highlightMoveDuration() const;
203 virtual void setHighlightMoveDuration(int);
204
205 bool reuseItems() const;
206 void setReuseItems(bool reuse);
207
208 enum PositionMode { Beginning, Center, End, Visible, Contain, SnapPosition };
209 Q_ENUM(PositionMode)
210
211 Q_INVOKABLE void positionViewAtIndex(int index, int mode);
212 Q_INVOKABLE int indexAt(qreal x, qreal y) const;
213 Q_INVOKABLE QQuickItem *itemAt(qreal x, qreal y) const;
214 Q_REVISION(2, 13) Q_INVOKABLE QQuickItem *itemAtIndex(int index) const;
215 Q_INVOKABLE void positionViewAtBeginning();
216 Q_INVOKABLE void positionViewAtEnd();
217 Q_REVISION(2, 1) Q_INVOKABLE void forceLayout();
218
219 void setContentX(qreal pos) override;
220 void setContentY(qreal pos) override;
221 qreal originX() const override;
222 qreal originY() const override;
223
224 QQmlDelegateModel::DelegateModelAccess delegateModelAccess() const;
225 void setDelegateModelAccess(QQmlDelegateModel::DelegateModelAccess delegateModelAccess);
226
227Q_SIGNALS:
228 void modelChanged();
229 void delegateChanged();
230 void countChanged();
231 void currentIndexChanged();
232 void currentItemChanged();
233
234 void keyNavigationWrapsChanged();
235 Q_REVISION(2, 7) void keyNavigationEnabledChanged();
236 void cacheBufferChanged();
237 void displayMarginBeginningChanged();
238 void displayMarginEndChanged();
239
240 void layoutDirectionChanged();
241 void effectiveLayoutDirectionChanged();
242 void verticalLayoutDirectionChanged();
243
244 void headerChanged();
245 void footerChanged();
246 void headerItemChanged();
247 void footerItemChanged();
248
249#if QT_CONFIG(quick_viewtransitions)
250 void populateTransitionChanged();
251 void addTransitionChanged();
252 void addDisplacedTransitionChanged();
253 void moveTransitionChanged();
254 void moveDisplacedTransitionChanged();
255 void removeTransitionChanged();
256 void removeDisplacedTransitionChanged();
257 void displacedTransitionChanged();
258#endif
259
260 void highlightChanged();
261 void highlightItemChanged();
262 void highlightFollowsCurrentItemChanged();
263 void highlightRangeModeChanged();
264 void preferredHighlightBeginChanged();
265 void preferredHighlightEndChanged();
266 void highlightMoveDurationChanged();
267
268 Q_REVISION(2, 15) void reuseItemsChanged();
269 Q_REVISION(6, 10) void delegateModelAccessChanged();
270
271protected:
272 void updatePolish() override;
273 void componentComplete() override;
274 void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
275 qreal minYExtent() const override;
276 qreal maxYExtent() const override;
277 qreal minXExtent() const override;
278 qreal maxXExtent() const override;
279
280protected Q_SLOTS:
281 void destroyRemoved();
282 void createdItem(int index, QObject *item);
283 virtual void initItem(int index, QObject *item);
284 void modelUpdated(const QQmlChangeSet &changeSet, bool reset);
285 void destroyingItem(QObject *item);
286 Q_REVISION(2, 15) void onItemPooled(int modelIndex, QObject *object);
287 Q_REVISION(2, 15) void onItemReused(int modelIndex, QObject *object);
288 void animStopped();
289 void trackedPositionChanged();
290
291private:
292 Q_DECLARE_PRIVATE(QQuickItemView)
293};
294
295
296class Q_QUICK_EXPORT QQuickItemViewAttached : public QObject
297{
298 Q_OBJECT
299
300 Q_PROPERTY(bool isCurrentItem READ isCurrentItem NOTIFY currentItemChanged FINAL)
301 Q_PROPERTY(bool delayRemove READ delayRemove WRITE setDelayRemove NOTIFY delayRemoveChanged FINAL)
302
303 Q_PROPERTY(QString section READ section NOTIFY sectionChanged FINAL)
304 Q_PROPERTY(QString previousSection READ prevSection NOTIFY prevSectionChanged FINAL)
305 Q_PROPERTY(QString nextSection READ nextSection NOTIFY nextSectionChanged FINAL)
306
307public:
308 QQuickItemViewAttached(QObject *parent)
309 : QObject(parent), m_isCurrent(false), m_delayRemove(false) {}
310 ~QQuickItemViewAttached() {}
311
312 bool isCurrentItem() const { return m_isCurrent; }
313 void setIsCurrentItem(bool c) {
314 if (m_isCurrent != c) {
315 m_isCurrent = c;
316 Q_EMIT currentItemChanged();
317 }
318 }
319
320 bool delayRemove() const { return m_delayRemove; }
321 void setDelayRemove(bool delay) {
322 if (m_delayRemove != delay) {
323 m_delayRemove = delay;
324 Q_EMIT delayRemoveChanged();
325 }
326 }
327
328 QString section() const { return m_section; }
329 void setSection(const QString &sect) {
330 if (m_section != sect) {
331 m_section = sect;
332 Q_EMIT sectionChanged();
333 }
334 }
335
336 QString prevSection() const { return m_prevSection; }
337 void setPrevSection(const QString &sect) {
338 if (m_prevSection != sect) {
339 m_prevSection = sect;
340 Q_EMIT prevSectionChanged();
341 }
342 }
343
344 QString nextSection() const { return m_nextSection; }
345 void setNextSection(const QString &sect) {
346 if (m_nextSection != sect) {
347 m_nextSection = sect;
348 Q_EMIT nextSectionChanged();
349 }
350 }
351
352 void setSections(const QString &prev, const QString &sect, const QString &next) {
353 bool prevChanged = prev != m_prevSection;
354 bool sectChanged = sect != m_section;
355 bool nextChanged = next != m_nextSection;
356 m_prevSection = prev;
357 m_section = sect;
358 m_nextSection = next;
359 if (prevChanged)
360 Q_EMIT prevSectionChanged();
361 if (sectChanged)
362 Q_EMIT sectionChanged();
363 if (nextChanged)
364 Q_EMIT nextSectionChanged();
365 }
366
367 void emitAdd() { Q_EMIT add(); }
368 void emitRemove() { Q_EMIT remove(); }
369
370Q_SIGNALS:
371 void viewChanged();
372 void currentItemChanged();
373 void delayRemoveChanged();
374
375 void add();
376 void remove();
377
378 void sectionChanged();
379 void prevSectionChanged();
380 void nextSectionChanged();
381
382 void pooled();
383 void reused();
384
385public:
386 bool m_isCurrent : 1;
387 bool m_delayRemove : 1;
388
389 // current only used by list view
390 mutable QString m_section;
391 QString m_prevSection;
392 QString m_nextSection;
393};
394
395
396QT_END_NAMESPACE
397
398#endif // QQUICKITEMVIEW_P_H
399
400

source code of qtdeclarative/src/quick/items/qquickitemview_p.h