Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlistview_p.h
Go to the documentation of this file.
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 QLISTVIEW_P_H
5#define QLISTVIEW_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 for the convenience
12// of other Qt classes. 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 <QtWidgets/private/qtwidgetsglobal_p.h>
19#include "qlistview.h"
20#include "private/qabstractitemview_p.h"
21#include "qbitarray.h"
22#include "qbsptree_p.h"
23#include <limits.h>
24#include <qscrollbar.h>
25
27
28QT_BEGIN_NAMESPACE
29
30class QListViewItem
31{
32 friend class QListViewPrivate;
33 friend class QListModeViewBase;
34 friend class QIconModeViewBase;
35public:
36 constexpr QListViewItem()
37 : x(-1), y(-1), w(0), h(0), indexHint(-1), visited(0xffff) {}
38 constexpr QListViewItem(QRect r, int i)
39 : x(r.x()), y(r.y()), w(qMin(r.width(), SHRT_MAX)), h(qMin(r.height(), SHRT_MAX)),
40 indexHint(i), visited(0xffff) {}
41 constexpr bool operator==(const QListViewItem &other) const {
42 return (x == other.x && y == other.y && w == other.w && h == other.h &&
43 indexHint == other.indexHint); }
44 constexpr bool operator!=(const QListViewItem &other) const
45 { return !(*this == other); }
46 constexpr bool isValid() const
47 { return rect().isValid() && (indexHint > -1); }
48 constexpr void invalidate()
49 { x = -1; y = -1; w = 0; h = 0; }
50 constexpr void resize(QSize size)
51 { w = qMin(size.width(), SHRT_MAX); h = qMin(size.height(), SHRT_MAX); }
52 constexpr void move(QPoint position)
53 { x = position.x(); y = position.y(); }
54 constexpr int width() const { return w; }
55 constexpr int height() const { return h; }
56private:
57 constexpr QRect rect() const
58 { return QRect(x, y, w, h); }
59 int x, y;
60 short w, h;
61 mutable int indexHint;
62 uint visited;
63};
65
78
79class QListView;
80class QListViewPrivate;
81
83{
84public:
85 inline QCommonListViewBase(QListView *q, QListViewPrivate *d) : dd(d), qq(q), batchStartRow(0), batchSavedDeltaSeg(0) {}
86 virtual ~QCommonListViewBase() {}
87
88 //common interface
89 virtual int itemIndex(const QListViewItem &item) const = 0;
90 virtual QListViewItem indexToListViewItem(const QModelIndex &index) const = 0;
91 virtual bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) = 0;
92 virtual void clear() = 0;
93 virtual void setRowCount(int) = 0;
94 virtual QList<QModelIndex> intersectingSet(const QRect &area) const = 0;
95 virtual void dataChanged(const QModelIndex &, const QModelIndex &) = 0;
96
97 virtual int horizontalScrollToValue(int index, QListView::ScrollHint hint,
98 bool leftOf, bool rightOf, const QRect &area, const QRect &rect) const;
99 virtual int verticalScrollToValue(int index, QListView::ScrollHint hint,
100 bool above, bool below, const QRect &area, const QRect &rect) const;
101 virtual void scrollContentsBy(int dx, int dy, bool scrollElasticBand);
102 virtual QRect mapToViewport(const QRect &rect) const {return rect;}
103 virtual int horizontalOffset() const;
104 virtual int verticalOffset() const { return verticalScrollBar()->value(); }
105 virtual void updateHorizontalScrollBar(const QSize &step);
106 virtual void updateVerticalScrollBar(const QSize &step);
107 virtual void appendHiddenRow(int row);
108 virtual void removeHiddenRow(int row);
109 virtual void setPositionForIndex(const QPoint &, const QModelIndex &) { }
110
111#if QT_CONFIG(draganddrop)
112 virtual void paintDragDrop(QPainter *painter);
113 virtual bool filterDragMoveEvent(QDragMoveEvent *) { return false; }
114 virtual bool filterDragLeaveEvent(QDragLeaveEvent *) { return false; }
115 virtual bool filterDropEvent(QDropEvent *) { return false; }
116 virtual bool filterStartDrag(Qt::DropActions) { return false; }
117#endif
118
119
120 //other inline members
121 inline int spacing() const;
122 inline bool isWrapping() const;
123 inline QSize gridSize() const;
124 inline QListView::Flow flow() const;
125 inline QListView::Movement movement() const;
126
127 inline QPoint offset() const;
128 inline QPoint pressedPosition() const;
129 inline bool uniformItemSizes() const;
130 inline int column() const;
131
132 inline QScrollBar *verticalScrollBar() const;
133 inline QScrollBar *horizontalScrollBar() const;
136
137 inline QModelIndex modelIndex(int row) const;
138 inline int rowCount() const;
139
140 inline void initViewItemOption(QStyleOptionViewItem *option) const;
141 inline QWidget *viewport() const;
142 inline QRect clipRect() const;
143
144 inline QSize cachedItemSize() const;
145 inline QRect viewItemRect(const QListViewItem &item) const;
146 inline QSize itemSize(const QStyleOptionViewItem &opt, const QModelIndex &idx) const;
147 inline QAbstractItemDelegate *delegate(const QModelIndex &idx) const;
148
149 inline bool isHidden(int row) const;
150 inline int hiddenCount() const;
151
152 inline bool isRightToLeft() const;
153
154 QListViewPrivate *dd;
155 QListView *qq;
159};
160
162{
163public:
164 QListModeViewBase(QListView *q, QListViewPrivate *d);
165
171
172 // used when laying out in batches
174
175 //reimplementations
176 int itemIndex(const QListViewItem &item) const override { return item.indexHint; }
177 QListViewItem indexToListViewItem(const QModelIndex &index) const override;
178 bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) override;
179 void clear() override;
180 void setRowCount(int rowCount) override { flowPositions.resize(rowCount); }
181 QList<QModelIndex> intersectingSet(const QRect &area) const override;
182 void dataChanged(const QModelIndex &, const QModelIndex &) override;
183
184 int horizontalScrollToValue(int index, QListView::ScrollHint hint,
185 bool leftOf, bool rightOf,const QRect &area, const QRect &rect) const override;
186 int verticalScrollToValue(int index, QListView::ScrollHint hint,
187 bool above, bool below, const QRect &area, const QRect &rect) const override;
188 void scrollContentsBy(int dx, int dy, bool scrollElasticBand) override;
189 QRect mapToViewport(const QRect &rect) const override;
190 int horizontalOffset() const override;
191 int verticalOffset() const override;
192 inline static QSize viewportSize(const QAbstractItemView *v);
193 void updateHorizontalScrollBar(const QSize &step) override;
194 void updateVerticalScrollBar(const QSize &step) override;
195
196#if QT_CONFIG(draganddrop)
197 // The next two methods are to be used on LefToRight flow only.
198 // WARNING: Plenty of duplicated code from QAbstractItemView{,Private}.
201 bool dropOn(QDropEvent *event, int *row, int *col, QModelIndex *index);
202#endif
203
204private:
205 QPoint initStaticLayout(const QListViewLayoutInfo &info);
206 void doStaticLayout(const QListViewLayoutInfo &info);
207 int perItemScrollToValue(int index, int value, int height,
208 QAbstractItemView::ScrollHint hint,
209 Qt::Orientation orientation, bool wrap, int extent) const;
210 int perItemScrollingPageSteps(int length, int bounds, bool wrap) const;
211};
212
214{
215public:
216 QIconModeViewBase(QListView *q, QListViewPrivate *d) : QCommonListViewBase(q, d), interSectingVector(nullptr) {}
217
219 QList<QListViewItem> items;
221
222 QList<QModelIndex> draggedItems; // indices to the tree.itemVector
224
225 // used when laying out in batches
226 QList<QModelIndex> *interSectingVector; // used from within intersectingSet
227
228 //reimplementations
229 int itemIndex(const QListViewItem &item) const override;
230 QListViewItem indexToListViewItem(const QModelIndex &index) const override;
231 bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) override;
232 void clear() override;
233 void setRowCount(int rowCount) override;
234 QList<QModelIndex> intersectingSet(const QRect &area) const override;
235
236 void scrollContentsBy(int dx, int dy, bool scrollElasticBand) override;
237 void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) override;
238 void appendHiddenRow(int row) override;
239 void removeHiddenRow(int row) override;
240 void setPositionForIndex(const QPoint &position, const QModelIndex &index) override;
241
242#if QT_CONFIG(draganddrop)
247#endif
248
249private:
250 void initBspTree(const QSize &contents);
251 QPoint initDynamicLayout(const QListViewLayoutInfo &info);
252 void doDynamicLayout(const QListViewLayoutInfo &info);
253 static void addLeaf(QList<int> &leaf, const QRect &area, uint visited, QBspTree::Data data);
254 QRect itemsRect(const QList<QModelIndex> &indexes) const;
255 QRect draggedItemsRect() const;
256 QPoint snapToGrid(const QPoint &pos) const;
257 void updateContentsSize();
258 QPoint draggedItemsDelta() const;
259 void drawItems(QPainter *painter, const QList<QModelIndex> &indexes) const;
260 void moveItem(int index, const QPoint &dest);
261
262};
263
264class Q_AUTOTEST_EXPORT QListViewPrivate: public QAbstractItemViewPrivate
265{
266 Q_DECLARE_PUBLIC(QListView)
267public:
268 QListViewPrivate();
269 ~QListViewPrivate();
270
271 void clear();
272 void prepareItemsLayout();
273
274 bool doItemsLayout(int num);
275
276 inline QList<QModelIndex> intersectingSet(const QRect &area, bool doLayout = true) const
277 {
278 if (doLayout) executePostedLayout();
279 QRect a = (q_func()->isRightToLeft() ? flipX(area.normalized()) : area.normalized());
280 return commonListView->intersectingSet(a);
281 }
282
283 inline void resetBatchStartRow() { commonListView->batchStartRow = 0; }
284 inline int batchStartRow() const { return commonListView->batchStartRow; }
285 inline QSize contentsSize() const { return commonListView->contentsSize; }
286 inline void setContentsSize(int w, int h) { commonListView->contentsSize = QSize(w, h); }
287
288 inline int flipX(int x) const
289 { return qMax(viewport->width(), contentsSize().width()) - x; }
290 inline QPoint flipX(const QPoint &p) const
291 { return QPoint(flipX(p.x()), p.y()); }
292 inline QRect flipX(const QRect &r) const
293 { return QRect(flipX(r.x()) - r.width(), r.y(), r.width(), r.height()); }
294 inline QRect viewItemRect(const QListViewItem &item) const
295 { if (q_func()->isRightToLeft()) return flipX(item.rect()); return item.rect(); }
296
297 QListViewItem indexToListViewItem(const QModelIndex &index) const;
298 inline QModelIndex listViewItemToIndex(const QListViewItem &item) const
299 { return model->index(commonListView->itemIndex(item), column, root); }
300
301 inline bool hasRectForIndex(const QModelIndex &index) const
302 {
303 return isIndexValid(index) && index.parent() == root && index.column() == column && !isHidden(index.row());
304 }
305
306 QRect rectForIndex(const QModelIndex &index) const
307 {
308 if (!hasRectForIndex(index))
309 return QRect();
310 executePostedLayout();
311 return viewItemRect(indexToListViewItem(index));
312 }
313
314 QRect cellRectForIndex(const QModelIndex &index)
315 {
316 if (!hasRectForIndex(index))
317 return QRect();
318 executePostedLayout();
319 auto oldItemAlignment = itemAlignment;
320 itemAlignment = Qt::Alignment();
321 const QRect rect = rectForIndex(index);
322 itemAlignment = oldItemAlignment;
323 return rect;
324 }
325
326 void viewUpdateGeometries() { q_func()->updateGeometries(); }
327
328
329 QRect mapToViewport(const QRect &rect, bool extend = true) const;
330
331 QModelIndex closestIndex(const QRect &target, const QList<QModelIndex> &candidates) const;
332 QSize itemSize(const QStyleOptionViewItem &option, const QModelIndex &index) const;
333
334 bool selectionAllowed(const QModelIndex &index) const override
335 { if (viewMode == QListView::ListMode && !showElasticBand) return index.isValid(); return true; }
336
337 int horizontalScrollToValue(const QModelIndex &index, const QRect &rect, QListView::ScrollHint hint) const;
338 int verticalScrollToValue(const QModelIndex &index, const QRect &rect, QListView::ScrollHint hint) const;
339
340 QItemSelection selection(const QRect &rect) const;
341 void selectAll(QItemSelectionModel::SelectionFlags command) override;
342
343#if QT_CONFIG(draganddrop)
344 QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const override;
345 bool dropOn(QDropEvent *event, int *row, int *col, QModelIndex *index) override;
346#endif
347
348 inline void setGridSize(const QSize &size) { grid = size; }
349 inline QSize gridSize() const { return grid; }
350 inline void setWrapping(bool b) { wrap = b; }
351 inline bool isWrapping() const { return wrap; }
352 inline void setSpacing(int s) { space = s; }
353 inline int spacing() const { return space; }
354 inline void setSelectionRectVisible(bool visible) { showElasticBand = visible; }
355 inline bool isSelectionRectVisible() const { return showElasticBand; }
356
357 inline QModelIndex modelIndex(int row) const { return model->index(row, column, root); }
358 inline bool isHidden(int row) const {
359 QModelIndex idx = model->index(row, 0, root);
360 return isPersistent(idx) && hiddenRows.contains(idx);
361 }
362 // helper to avoid checking for isPersistent and creating persistent indexes as above in isHidden
363 QList<int> hiddenRowIds() const
364 {
365 QList<int> rowIds;
366 rowIds.reserve(hiddenRows.size());
367 for (const auto &idx : hiddenRows)
368 rowIds += idx.row();
369 return rowIds;
370 }
371 inline bool isHiddenOrDisabled(int row) const { return isHidden(row) || !isIndexEnabled(modelIndex(row)); }
372
373 void removeCurrentAndDisabled(QList<QModelIndex> *indexes, const QModelIndex &current) const;
374
375 void scrollElasticBandBy(int dx, int dy);
376
377 QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const override;
378
379 void emitIndexesMoved(const QModelIndexList &indexes) { emit q_func()->indexesMoved(indexes); }
380
381
382 QCommonListViewBase *commonListView;
383
384 // ### FIXME: see if we can move the members into the dynamic/static classes
385
386 bool wrap;
387 int space;
388 QSize grid;
389
390 QListView::Flow flow;
391 QListView::Movement movement;
392 QListView::ResizeMode resizeMode;
393 QListView::LayoutMode layoutMode;
394 QListView::ViewMode viewMode;
395
396 // the properties controlling the
397 // icon- or list-view modes
398 enum ModeProperties {
399 Wrap = 1,
400 Spacing = 2,
401 GridSize = 4,
402 Flow = 8,
403 Movement = 16,
404 ResizeMode = 32,
405 SelectionRectVisible = 64
406 };
407
408 uint modeProperties : 8;
409
410 QRect layoutBounds;
411
412 // timers
413 QBasicTimer batchLayoutTimer;
414
415 // used for hidden items
416 QSet<QPersistentModelIndex> hiddenRows;
417
418 int column;
419 bool uniformItemSizes;
420 mutable QSize cachedItemSize;
421 int batchSize;
422
423 QRect elasticBand;
424 bool showElasticBand;
425
426 Qt::Alignment itemAlignment;
427};
428
429// inline implementations
430
431inline int QCommonListViewBase::spacing() const { return dd->spacing(); }
432inline bool QCommonListViewBase::isWrapping() const { return dd->isWrapping(); }
433inline QSize QCommonListViewBase::gridSize() const { return dd->gridSize(); }
434inline QListView::Flow QCommonListViewBase::flow() const { return dd->flow; }
435inline QListView::Movement QCommonListViewBase::movement() const { return dd->movement; }
436
437inline QPoint QCommonListViewBase::offset() const { return dd->offset(); }
438inline QPoint QCommonListViewBase::pressedPosition() const { return dd->pressedPosition; }
439inline bool QCommonListViewBase::uniformItemSizes() const { return dd->uniformItemSizes; }
440inline int QCommonListViewBase::column() const { return dd->column; }
441
442inline QScrollBar *QCommonListViewBase::verticalScrollBar() const { return qq->verticalScrollBar(); }
443inline QScrollBar *QCommonListViewBase::horizontalScrollBar() const { return qq->horizontalScrollBar(); }
444inline QListView::ScrollMode QCommonListViewBase::verticalScrollMode() const { return qq->verticalScrollMode(); }
445inline QListView::ScrollMode QCommonListViewBase::horizontalScrollMode() const { return qq->horizontalScrollMode(); }
446
448 { return dd->model->index(row, dd->column, dd->root); }
449inline int QCommonListViewBase::rowCount() const { return dd->model->rowCount(dd->root); }
450
451inline void QCommonListViewBase::initViewItemOption(QStyleOptionViewItem *option) const { qq->initViewItemOption(option); }
452inline QWidget *QCommonListViewBase::viewport() const { return dd->viewport; }
453inline QRect QCommonListViewBase::clipRect() const { return dd->clipRect(); }
454
455inline QSize QCommonListViewBase::cachedItemSize() const { return dd->cachedItemSize; }
456inline QRect QCommonListViewBase::viewItemRect(const QListViewItem &item) const { return dd->viewItemRect(item); }
457inline QSize QCommonListViewBase::itemSize(const QStyleOptionViewItem &opt, const QModelIndex &idx) const
458 { return dd->itemSize(opt, idx); }
459
460inline QAbstractItemDelegate *QCommonListViewBase::delegate(const QModelIndex &idx) const
461 { return qq->itemDelegateForIndex(idx); }
462
463inline bool QCommonListViewBase::isHidden(int row) const { return dd->isHidden(row); }
464inline int QCommonListViewBase::hiddenCount() const { return dd->hiddenRows.size(); }
465
466inline bool QCommonListViewBase::isRightToLeft() const { return qq->isRightToLeft(); }
467
468QT_END_NAMESPACE
469
470#endif // QLISTVIEW_P_H
virtual void setPositionForIndex(const QPoint &, const QModelIndex &)
int hiddenCount() const
virtual void dataChanged(const QModelIndex &, const QModelIndex &)=0
virtual ~QCommonListViewBase()
Definition qlistview_p.h:86
virtual int horizontalScrollToValue(int index, QListView::ScrollHint hint, bool leftOf, bool rightOf, const QRect &area, const QRect &rect) const
virtual void clear()=0
virtual QList< QModelIndex > intersectingSet(const QRect &area) const =0
QAbstractItemDelegate * delegate(const QModelIndex &idx) const
QListView::Movement movement() const
QRect viewItemRect(const QListViewItem &item) const
QListView::ScrollMode horizontalScrollMode() const
QListView::ScrollMode verticalScrollMode() const
virtual void updateHorizontalScrollBar(const QSize &step)
virtual int horizontalOffset() const
virtual int itemIndex(const QListViewItem &item) const =0
QWidget * viewport() const
void initViewItemOption(QStyleOptionViewItem *option) const
virtual bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max)=0
virtual void appendHiddenRow(int row)
virtual void updateVerticalScrollBar(const QSize &step)
virtual QListViewItem indexToListViewItem(const QModelIndex &index) const =0
virtual void removeHiddenRow(int row)
bool uniformItemSizes() const
QSize cachedItemSize() const
virtual void scrollContentsBy(int dx, int dy, bool scrollElasticBand)
virtual int verticalOffset() const
QPoint offset() const
bool isHidden(int row) const
QListViewPrivate * dd
QScrollBar * verticalScrollBar() const
QSize itemSize(const QStyleOptionViewItem &opt, const QModelIndex &idx) const
QRect clipRect() const
QListView::Flow flow() const
int rowCount() const
virtual void setRowCount(int)=0
virtual QRect mapToViewport(const QRect &rect) const
QScrollBar * horizontalScrollBar() const
QSize gridSize() const
int spacing() const
bool isRightToLeft() const
bool isWrapping() const
QModelIndex modelIndex(int row) const
virtual int verticalScrollToValue(int index, QListView::ScrollHint hint, bool above, bool below, const QRect &area, const QRect &rect) const
QPoint pressedPosition() const
QCommonListViewBase(QListView *q, QListViewPrivate *d)
Definition qlistview_p.h:85
QList< QListViewItem > items
int itemIndex(const QListViewItem &item) const override
QList< QModelIndex > draggedItems
QListViewItem indexToListViewItem(const QModelIndex &index) const override
void setPositionForIndex(const QPoint &position, const QModelIndex &index) override
void setRowCount(int rowCount) override
void scrollContentsBy(int dx, int dy, bool scrollElasticBand) override
QList< QModelIndex > intersectingSet(const QRect &area) const override
QIconModeViewBase(QListView *q, QListViewPrivate *d)
void appendHiddenRow(int row) override
void clear() override
QList< QModelIndex > * interSectingVector
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) override
void removeHiddenRow(int row) override
bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) override
void clear() override
void updateHorizontalScrollBar(const QSize &step) override
bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) override
QList< int > segmentExtents
int itemIndex(const QListViewItem &item) const override
QList< int > segmentPositions
QList< int > flowPositions
void updateVerticalScrollBar(const QSize &step) override
void scrollContentsBy(int dx, int dy, bool scrollElasticBand) override
QList< QModelIndex > intersectingSet(const QRect &area) const override
int horizontalOffset() const override
QRect mapToViewport(const QRect &rect) const override
int verticalOffset() const override
QListViewItem indexToListViewItem(const QModelIndex &index) const override
void setRowCount(int rowCount) override
int verticalScrollToValue(int index, QListView::ScrollHint hint, bool above, bool below, const QRect &area, const QRect &rect) const override
int horizontalScrollToValue(int index, QListView::ScrollHint hint, bool leftOf, bool rightOf, const QRect &area, const QRect &rect) const override
QListModeViewBase(QListView *q, QListViewPrivate *d)
static QSize viewportSize(const QAbstractItemView *v)
void dataChanged(const QModelIndex &, const QModelIndex &) override
QList< int > segmentStartRows
QList< int > scrollValueMap
Definition qlist.h:76
QT_REQUIRE_CONFIG(animation)
Q_DECLARE_TYPEINFO(QListViewItem, Q_PRIMITIVE_TYPE)
Q_DECLARE_TYPEINFO(QListViewLayoutInfo, Q_PRIMITIVE_TYPE)