1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QLISTVIEW_P_H
41#define QLISTVIEW_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists for the convenience
48// of other Qt classes. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtWidgets/private/qtwidgetsglobal_p.h>
55#include "private/qabstractitemview_p.h"
56#include "qbitarray.h"
57#include "qbsptree_p.h"
58#include <limits.h>
59#include <qscrollbar.h>
60
61QT_REQUIRE_CONFIG(listview);
62
63QT_BEGIN_NAMESPACE
64
65class QListViewItem
66{
67 friend class QListViewPrivate;
68 friend class QListModeViewBase;
69 friend class QIconModeViewBase;
70public:
71 Q_DECL_CONSTEXPR QListViewItem()
72 : x(-1), y(-1), w(0), h(0), indexHint(-1), visited(0xffff) {}
73 Q_DECL_CONSTEXPR QListViewItem(QRect r, int i)
74 : x(r.x()), y(r.y()), w(qMin(a: r.width(), SHRT_MAX)), h(qMin(a: r.height(), SHRT_MAX)),
75 indexHint(i), visited(0xffff) {}
76 Q_DECL_CONSTEXPR bool operator==(const QListViewItem &other) const {
77 return (x == other.x && y == other.y && w == other.w && h == other.h &&
78 indexHint == other.indexHint); }
79 Q_DECL_CONSTEXPR bool operator!=(const QListViewItem &other) const
80 { return !(*this == other); }
81 Q_DECL_CONSTEXPR bool isValid() const
82 { return rect().isValid() && (indexHint > -1); }
83 Q_DECL_RELAXED_CONSTEXPR void invalidate()
84 { x = -1; y = -1; w = 0; h = 0; }
85 Q_DECL_RELAXED_CONSTEXPR void resize(QSize size)
86 { w = qMin(a: size.width(), SHRT_MAX); h = qMin(a: size.height(), SHRT_MAX); }
87 Q_DECL_RELAXED_CONSTEXPR void move(QPoint position)
88 { x = position.x(); y = position.y(); }
89 Q_DECL_CONSTEXPR int width() const { return w; }
90 Q_DECL_CONSTEXPR int height() const { return h; }
91private:
92 Q_DECL_CONSTEXPR QRect rect() const
93 { return QRect(x, y, w, h); }
94 int x, y;
95 short w, h;
96 mutable int indexHint;
97 uint visited;
98};
99Q_DECLARE_TYPEINFO(QListViewItem, Q_PRIMITIVE_TYPE);
100
101struct QListViewLayoutInfo
102{
103 QRect bounds;
104 QSize grid;
105 int spacing;
106 int first;
107 int last;
108 bool wrap;
109 QListView::Flow flow;
110 int max;
111};
112Q_DECLARE_TYPEINFO(QListViewLayoutInfo, Q_PRIMITIVE_TYPE);
113
114class QListView;
115class QListViewPrivate;
116
117class QCommonListViewBase
118{
119public:
120 inline QCommonListViewBase(QListView *q, QListViewPrivate *d) : dd(d), qq(q), batchStartRow(0), batchSavedDeltaSeg(0) {}
121 virtual ~QCommonListViewBase() {}
122
123 //common interface
124 virtual int itemIndex(const QListViewItem &item) const = 0;
125 virtual QListViewItem indexToListViewItem(const QModelIndex &index) const = 0;
126 virtual bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) = 0;
127 virtual void clear() = 0;
128 virtual void setRowCount(int) = 0;
129 virtual QVector<QModelIndex> intersectingSet(const QRect &area) const = 0;
130 virtual void dataChanged(const QModelIndex &, const QModelIndex &) = 0;
131
132 virtual int horizontalScrollToValue(int index, QListView::ScrollHint hint,
133 bool leftOf, bool rightOf, const QRect &area, const QRect &rect) const;
134 virtual int verticalScrollToValue(int index, QListView::ScrollHint hint,
135 bool above, bool below, const QRect &area, const QRect &rect) const;
136 virtual void scrollContentsBy(int dx, int dy, bool scrollElasticBand);
137 virtual QRect mapToViewport(const QRect &rect) const {return rect;}
138 virtual int horizontalOffset() const;
139 virtual int verticalOffset() const { return verticalScrollBar()->value(); }
140 virtual void updateHorizontalScrollBar(const QSize &step);
141 virtual void updateVerticalScrollBar(const QSize &step);
142 virtual void appendHiddenRow(int row);
143 virtual void removeHiddenRow(int row);
144 virtual void setPositionForIndex(const QPoint &, const QModelIndex &) { }
145
146#if QT_CONFIG(draganddrop)
147 virtual void paintDragDrop(QPainter *painter);
148 virtual bool filterDragMoveEvent(QDragMoveEvent *) { return false; }
149 virtual bool filterDragLeaveEvent(QDragLeaveEvent *) { return false; }
150 virtual bool filterDropEvent(QDropEvent *) { return false; }
151 virtual bool filterStartDrag(Qt::DropActions) { return false; }
152#endif
153
154
155 //other inline members
156 inline int spacing() const;
157 inline bool isWrapping() const;
158 inline QSize gridSize() const;
159 inline QListView::Flow flow() const;
160 inline QListView::Movement movement() const;
161
162 inline QPoint offset() const;
163 inline QPoint pressedPosition() const;
164 inline bool uniformItemSizes() const;
165 inline int column() const;
166
167 inline QScrollBar *verticalScrollBar() const;
168 inline QScrollBar *horizontalScrollBar() const;
169 inline QListView::ScrollMode verticalScrollMode() const;
170 inline QListView::ScrollMode horizontalScrollMode() const;
171
172 inline QModelIndex modelIndex(int row) const;
173 inline int rowCount() const;
174
175 inline QStyleOptionViewItem viewOptions() const;
176 inline QWidget *viewport() const;
177 inline QRect clipRect() const;
178
179 inline QSize cachedItemSize() const;
180 inline QRect viewItemRect(const QListViewItem &item) const;
181 inline QSize itemSize(const QStyleOptionViewItem &opt, const QModelIndex &idx) const;
182 inline QAbstractItemDelegate *delegate(const QModelIndex &idx) const;
183
184 inline bool isHidden(int row) const;
185 inline int hiddenCount() const;
186
187 inline bool isRightToLeft() const;
188
189 QListViewPrivate *dd;
190 QListView *qq;
191 QSize contentsSize;
192 int batchStartRow;
193 int batchSavedDeltaSeg;
194};
195
196class QListModeViewBase : public QCommonListViewBase
197{
198public:
199 QListModeViewBase(QListView *q, QListViewPrivate *d);
200
201 QVector<int> flowPositions;
202 QVector<int> segmentPositions;
203 QVector<int> segmentStartRows;
204 QVector<int> segmentExtents;
205 QVector<int> scrollValueMap;
206
207 // used when laying out in batches
208 int batchSavedPosition;
209
210 //reimplementations
211 int itemIndex(const QListViewItem &item) const override { return item.indexHint; }
212 QListViewItem indexToListViewItem(const QModelIndex &index) const override;
213 bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) override;
214 void clear() override;
215 void setRowCount(int rowCount) override { flowPositions.resize(asize: rowCount); }
216 QVector<QModelIndex> intersectingSet(const QRect &area) const override;
217 void dataChanged(const QModelIndex &, const QModelIndex &) override;
218
219 int horizontalScrollToValue(int index, QListView::ScrollHint hint,
220 bool leftOf, bool rightOf,const QRect &area, const QRect &rect) const override;
221 int verticalScrollToValue(int index, QListView::ScrollHint hint,
222 bool above, bool below, const QRect &area, const QRect &rect) const override;
223 void scrollContentsBy(int dx, int dy, bool scrollElasticBand) override;
224 QRect mapToViewport(const QRect &rect) const override;
225 int horizontalOffset() const override;
226 int verticalOffset() const override;
227 inline static QSize viewportSize(const QAbstractItemView *v);
228 void updateHorizontalScrollBar(const QSize &step) override;
229 void updateVerticalScrollBar(const QSize &step) override;
230
231#if QT_CONFIG(draganddrop)
232 // The next two methods are to be used on LefToRight flow only.
233 // WARNING: Plenty of duplicated code from QAbstractItemView{,Private}.
234 QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const;
235 void dragMoveEvent(QDragMoveEvent *e);
236 bool dropOn(QDropEvent *event, int *row, int *col, QModelIndex *index);
237#endif
238
239private:
240 QPoint initStaticLayout(const QListViewLayoutInfo &info);
241 void doStaticLayout(const QListViewLayoutInfo &info);
242 int perItemScrollToValue(int index, int value, int height,
243 QAbstractItemView::ScrollHint hint,
244 Qt::Orientation orientation, bool wrap, int extent) const;
245 int perItemScrollingPageSteps(int length, int bounds, bool wrap) const;
246};
247
248class QIconModeViewBase : public QCommonListViewBase
249{
250public:
251 QIconModeViewBase(QListView *q, QListViewPrivate *d) : QCommonListViewBase(q, d), interSectingVector(nullptr) {}
252
253 QBspTree tree;
254 QVector<QListViewItem> items;
255 QBitArray moved;
256
257 QVector<QModelIndex> draggedItems; // indices to the tree.itemVector
258 mutable QPoint draggedItemsPos;
259
260 // used when laying out in batches
261 QVector<QModelIndex> *interSectingVector; //used from within intersectingSet
262
263 //reimplementations
264 int itemIndex(const QListViewItem &item) const override;
265 QListViewItem indexToListViewItem(const QModelIndex &index) const override;
266 bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) override;
267 void clear() override;
268 void setRowCount(int rowCount) override;
269 QVector<QModelIndex> intersectingSet(const QRect &area) const override;
270
271 void scrollContentsBy(int dx, int dy, bool scrollElasticBand) override;
272 void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) override;
273 void appendHiddenRow(int row) override;
274 void removeHiddenRow(int row) override;
275 void setPositionForIndex(const QPoint &position, const QModelIndex &index) override;
276
277#if QT_CONFIG(draganddrop)
278 bool filterDragMoveEvent(QDragMoveEvent *) override;
279 bool filterDragLeaveEvent(QDragLeaveEvent *) override;
280 bool filterDropEvent(QDropEvent *e) override;
281 bool filterStartDrag(Qt::DropActions) override;
282#endif
283
284private:
285 void initBspTree(const QSize &contents);
286 QPoint initDynamicLayout(const QListViewLayoutInfo &info);
287 void doDynamicLayout(const QListViewLayoutInfo &info);
288 static void addLeaf(QVector<int> &leaf, const QRect &area,
289 uint visited, QBspTree::Data data);
290 QRect itemsRect(const QVector<QModelIndex> &indexes) const;
291 QRect draggedItemsRect() const;
292 QPoint snapToGrid(const QPoint &pos) const;
293 void updateContentsSize();
294 QPoint draggedItemsDelta() const;
295 void drawItems(QPainter *painter, const QVector<QModelIndex> &indexes) const;
296 void moveItem(int index, const QPoint &dest);
297
298};
299
300class Q_AUTOTEST_EXPORT QListViewPrivate: public QAbstractItemViewPrivate
301{
302 Q_DECLARE_PUBLIC(QListView)
303public:
304 QListViewPrivate();
305 ~QListViewPrivate();
306
307 void clear();
308 void prepareItemsLayout();
309
310 bool doItemsLayout(int num);
311
312 inline QVector<QModelIndex> intersectingSet(const QRect &area, bool doLayout = true) const {
313 if (doLayout) executePostedLayout();
314 QRect a = (q_func()->isRightToLeft() ? flipX(r: area.normalized()) : area.normalized());
315 return commonListView->intersectingSet(area: a);
316 }
317
318 inline void resetBatchStartRow() { commonListView->batchStartRow = 0; }
319 inline int batchStartRow() const { return commonListView->batchStartRow; }
320 inline QSize contentsSize() const { return commonListView->contentsSize; }
321 inline void setContentsSize(int w, int h) { commonListView->contentsSize = QSize(w, h); }
322
323 inline int flipX(int x) const
324 { return qMax(a: viewport->width(), b: contentsSize().width()) - x; }
325 inline QPoint flipX(const QPoint &p) const
326 { return QPoint(flipX(x: p.x()), p.y()); }
327 inline QRect flipX(const QRect &r) const
328 { return QRect(flipX(x: r.x()) - r.width(), r.y(), r.width(), r.height()); }
329 inline QRect viewItemRect(const QListViewItem &item) const
330 { if (q_func()->isRightToLeft()) return flipX(r: item.rect()); return item.rect(); }
331
332 QListViewItem indexToListViewItem(const QModelIndex &index) const;
333 inline QModelIndex listViewItemToIndex(const QListViewItem &item) const
334 { return model->index(row: commonListView->itemIndex(item), column, parent: root); }
335
336 inline bool hasRectForIndex(const QModelIndex &index) const
337 {
338 return isIndexValid(index) && index.parent() == root && index.column() == column && !isHidden(row: index.row());
339 }
340
341 QRect rectForIndex(const QModelIndex &index) const
342 {
343 if (!hasRectForIndex(index))
344 return QRect();
345 executePostedLayout();
346 return viewItemRect(item: indexToListViewItem(index));
347 }
348
349 QRect cellRectForIndex(const QModelIndex &index)
350 {
351 if (!hasRectForIndex(index))
352 return QRect();
353 executePostedLayout();
354 auto oldItemAlignment = itemAlignment;
355 itemAlignment = Qt::Alignment();
356 const QRect rect = rectForIndex(index);
357 itemAlignment = oldItemAlignment;
358 return rect;
359 }
360
361 void viewUpdateGeometries() { q_func()->updateGeometries(); }
362
363
364 QRect mapToViewport(const QRect &rect, bool extend = true) const;
365
366 QModelIndex closestIndex(const QRect &target, const QVector<QModelIndex> &candidates) const;
367 QSize itemSize(const QStyleOptionViewItem &option, const QModelIndex &index) const;
368
369 bool selectionAllowed(const QModelIndex &index) const override
370 { if (viewMode == QListView::ListMode && !showElasticBand) return index.isValid(); return true; }
371
372 int horizontalScrollToValue(const QModelIndex &index, const QRect &rect, QListView::ScrollHint hint) const;
373 int verticalScrollToValue(const QModelIndex &index, const QRect &rect, QListView::ScrollHint hint) const;
374
375 QItemSelection selection(const QRect &rect) const;
376 void selectAll(QItemSelectionModel::SelectionFlags command) override;
377
378#if QT_CONFIG(draganddrop)
379 QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const override;
380 bool dropOn(QDropEvent *event, int *row, int *col, QModelIndex *index) override;
381#endif
382
383 inline void setGridSize(const QSize &size) { grid = size; }
384 inline QSize gridSize() const { return grid; }
385 inline void setWrapping(bool b) { wrap = b; }
386 inline bool isWrapping() const { return wrap; }
387 inline void setSpacing(int s) { space = s; }
388 inline int spacing() const { return space; }
389 inline void setSelectionRectVisible(bool visible) { showElasticBand = visible; }
390 inline bool isSelectionRectVisible() const { return showElasticBand; }
391
392 inline QModelIndex modelIndex(int row) const { return model->index(row, column, parent: root); }
393 inline bool isHidden(int row) const {
394 QModelIndex idx = model->index(row, column: 0, parent: root);
395 return isPersistent(index: idx) && hiddenRows.contains(value: idx);
396 }
397 // helper to avoid checking for isPersistent and creating persistent indexes as above in isHidden
398 QVector<int> hiddenRowIds() const {
399 QVector<int> rowIds;
400 rowIds.reserve(asize: hiddenRows.size());
401 for (const auto &idx : hiddenRows)
402 rowIds += idx.row();
403 return rowIds;
404 }
405 inline bool isHiddenOrDisabled(int row) const { return isHidden(row) || !isIndexEnabled(index: modelIndex(row)); }
406
407 void removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex &current) const;
408
409 void scrollElasticBandBy(int dx, int dy);
410
411 QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const override;
412
413 void emitIndexesMoved(const QModelIndexList &indexes) { emit q_func()->indexesMoved(indexes); }
414
415
416 QCommonListViewBase *commonListView;
417
418 // ### FIXME: see if we can move the members into the dynamic/static classes
419
420 bool wrap;
421 int space;
422 QSize grid;
423
424 QListView::Flow flow;
425 QListView::Movement movement;
426 QListView::ResizeMode resizeMode;
427 QListView::LayoutMode layoutMode;
428 QListView::ViewMode viewMode;
429
430 // the properties controlling the
431 // icon- or list-view modes
432 enum ModeProperties {
433 Wrap = 1,
434 Spacing = 2,
435 GridSize = 4,
436 Flow = 8,
437 Movement = 16,
438 ResizeMode = 32,
439 SelectionRectVisible = 64
440 };
441
442 uint modeProperties : 8;
443
444 QRect layoutBounds;
445
446 // timers
447 QBasicTimer batchLayoutTimer;
448
449 // used for hidden items
450 QSet<QPersistentModelIndex> hiddenRows;
451
452 int column;
453 bool uniformItemSizes;
454 mutable QSize cachedItemSize;
455 int batchSize;
456
457 QRect elasticBand;
458 bool showElasticBand;
459
460 Qt::Alignment itemAlignment;
461};
462
463// inline implementations
464
465inline int QCommonListViewBase::spacing() const { return dd->spacing(); }
466inline bool QCommonListViewBase::isWrapping() const { return dd->isWrapping(); }
467inline QSize QCommonListViewBase::gridSize() const { return dd->gridSize(); }
468inline QListView::Flow QCommonListViewBase::flow() const { return dd->flow; }
469inline QListView::Movement QCommonListViewBase::movement() const { return dd->movement; }
470
471inline QPoint QCommonListViewBase::offset() const { return dd->offset(); }
472inline QPoint QCommonListViewBase::pressedPosition() const { return dd->pressedPosition; }
473inline bool QCommonListViewBase::uniformItemSizes() const { return dd->uniformItemSizes; }
474inline int QCommonListViewBase::column() const { return dd->column; }
475
476inline QScrollBar *QCommonListViewBase::verticalScrollBar() const { return qq->verticalScrollBar(); }
477inline QScrollBar *QCommonListViewBase::horizontalScrollBar() const { return qq->horizontalScrollBar(); }
478inline QListView::ScrollMode QCommonListViewBase::verticalScrollMode() const { return qq->verticalScrollMode(); }
479inline QListView::ScrollMode QCommonListViewBase::horizontalScrollMode() const { return qq->horizontalScrollMode(); }
480
481inline QModelIndex QCommonListViewBase::modelIndex(int row) const
482 { return dd->model->index(row, column: dd->column, parent: dd->root); }
483inline int QCommonListViewBase::rowCount() const { return dd->model->rowCount(parent: dd->root); }
484
485inline QStyleOptionViewItem QCommonListViewBase::viewOptions() const { return dd->viewOptionsV1(); }
486inline QWidget *QCommonListViewBase::viewport() const { return dd->viewport; }
487inline QRect QCommonListViewBase::clipRect() const { return dd->clipRect(); }
488
489inline QSize QCommonListViewBase::cachedItemSize() const { return dd->cachedItemSize; }
490inline QRect QCommonListViewBase::viewItemRect(const QListViewItem &item) const { return dd->viewItemRect(item); }
491inline QSize QCommonListViewBase::itemSize(const QStyleOptionViewItem &opt, const QModelIndex &idx) const
492 { return dd->itemSize(option: opt, index: idx); }
493
494inline QAbstractItemDelegate *QCommonListViewBase::delegate(const QModelIndex &idx) const
495 { return dd->delegateForIndex(index: idx); }
496
497inline bool QCommonListViewBase::isHidden(int row) const { return dd->isHidden(row); }
498inline int QCommonListViewBase::hiddenCount() const { return dd->hiddenRows.count(); }
499
500inline bool QCommonListViewBase::isRightToLeft() const { return qq->isRightToLeft(); }
501
502QT_END_NAMESPACE
503
504#endif // QLISTVIEW_P_H
505

source code of qtbase/src/widgets/itemviews/qlistview_p.h