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