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 constexpr QListViewItem()
72 : x(-1), y(-1), w(0), h(0), indexHint(-1), visited(0xffff) {}
73 constexpr QListViewItem(QRect r, int i)
74 : x(r.x()), y(r.y()), w(qMin(r.width(), SHRT_MAX)), h(qMin(r.height(), SHRT_MAX)),
75 indexHint(i), visited(0xffff) {}
76 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 constexpr bool operator!=(const QListViewItem &other) const
80 { return !(*this == other); }
81 constexpr bool isValid() const
82 { return rect().isValid() && (indexHint > -1); }
83 constexpr void invalidate()
84 { x = -1; y = -1; w = 0; h = 0; }
85 constexpr void resize(QSize size)
86 { w = qMin(size.width(), SHRT_MAX); h = qMin(size.height(), SHRT_MAX); }
87 constexpr void move(QPoint position)
88 { x = position.x(); y = position.y(); }
89 constexpr int width() const { return w; }
90 constexpr int height() const { return h; }
91private:
92 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 QList<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 void initViewItemOption(QStyleOptionViewItem *option) 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 QList<int> flowPositions;
202 QList<int> segmentPositions;
203 QList<int> segmentStartRows;
204 QList<int> segmentExtents;
205 QList<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(rowCount); }
216 QList<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 QList<QListViewItem> items;
255 QBitArray moved;
256
257 QList<QModelIndex> draggedItems; // indices to the tree.itemVector
258 mutable QPoint draggedItemsPos;
259
260 // used when laying out in batches
261 QList<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 QList<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(QList<int> &leaf, const QRect &area, uint visited, QBspTree::Data data);
289 QRect itemsRect(const QList<QModelIndex> &indexes) const;
290 QRect draggedItemsRect() const;
291 QPoint snapToGrid(const QPoint &pos) const;
292 void updateContentsSize();
293 QPoint draggedItemsDelta() const;
294 void drawItems(QPainter *painter, const QList<QModelIndex> &indexes) const;
295 void moveItem(int index, const QPoint &dest);
296
297};
298
299class Q_AUTOTEST_EXPORT QListViewPrivate: public QAbstractItemViewPrivate
300{
301 Q_DECLARE_PUBLIC(QListView)
302public:
303 QListViewPrivate();
304 ~QListViewPrivate();
305
306 void clear();
307 void prepareItemsLayout();
308
309 bool doItemsLayout(int num);
310
311 inline QList<QModelIndex> intersectingSet(const QRect &area, bool doLayout = true) const
312 {
313 if (doLayout) executePostedLayout();
314 QRect a = (q_func()->isRightToLeft() ? flipX(area.normalized()) : area.normalized());
315 return commonListView->intersectingSet(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(viewport->width(), contentsSize().width()) - x; }
325 inline QPoint flipX(const QPoint &p) const
326 { return QPoint(flipX(p.x()), p.y()); }
327 inline QRect flipX(const QRect &r) const
328 { return QRect(flipX(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(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(commonListView->itemIndex(item), column, root); }
335
336 inline bool hasRectForIndex(const QModelIndex &index) const
337 {
338 return isIndexValid(index) && index.parent() == root && index.column() == column && !isHidden(index.row());
339 }
340
341 QRect rectForIndex(const QModelIndex &index) const
342 {
343 if (!hasRectForIndex(index))
344 return QRect();
345 executePostedLayout();
346 return viewItemRect(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 QList<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, root); }
393 inline bool isHidden(int row) const {
394 QModelIndex idx = model->index(row, 0, root);
395 return isPersistent(idx) && hiddenRows.contains(idx);
396 }
397 // helper to avoid checking for isPersistent and creating persistent indexes as above in isHidden
398 QList<int> hiddenRowIds() const
399 {
400 QList<int> rowIds;
401 rowIds.reserve(hiddenRows.size());
402 for (const auto &idx : hiddenRows)
403 rowIds += idx.row();
404 return rowIds;
405 }
406 inline bool isHiddenOrDisabled(int row) const { return isHidden(row) || !isIndexEnabled(modelIndex(row)); }
407
408 void removeCurrentAndDisabled(QList<QModelIndex> *indexes, const QModelIndex &current) const;
409
410 void scrollElasticBandBy(int dx, int dy);
411
412 QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const override;
413
414 void emitIndexesMoved(const QModelIndexList &indexes) { emit q_func()->indexesMoved(indexes); }
415
416
417 QCommonListViewBase *commonListView;
418
419 // ### FIXME: see if we can move the members into the dynamic/static classes
420
421 bool wrap;
422 int space;
423 QSize grid;
424
425 QListView::Flow flow;
426 QListView::Movement movement;
427 QListView::ResizeMode resizeMode;
428 QListView::LayoutMode layoutMode;
429 QListView::ViewMode viewMode;
430
431 // the properties controlling the
432 // icon- or list-view modes
433 enum ModeProperties {
434 Wrap = 1,
435 Spacing = 2,
436 GridSize = 4,
437 Flow = 8,
438 Movement = 16,
439 ResizeMode = 32,
440 SelectionRectVisible = 64
441 };
442
443 uint modeProperties : 8;
444
445 QRect layoutBounds;
446
447 // timers
448 QBasicTimer batchLayoutTimer;
449
450 // used for hidden items
451 QSet<QPersistentModelIndex> hiddenRows;
452
453 int column;
454 bool uniformItemSizes;
455 mutable QSize cachedItemSize;
456 int batchSize;
457
458 QRect elasticBand;
459 bool showElasticBand;
460
461 Qt::Alignment itemAlignment;
462};
463
464// inline implementations
465
466inline int QCommonListViewBase::spacing() const { return dd->spacing(); }
467inline bool QCommonListViewBase::isWrapping() const { return dd->isWrapping(); }
468inline QSize QCommonListViewBase::gridSize() const { return dd->gridSize(); }
469inline QListView::Flow QCommonListViewBase::flow() const { return dd->flow; }
470inline QListView::Movement QCommonListViewBase::movement() const { return dd->movement; }
471
472inline QPoint QCommonListViewBase::offset() const { return dd->offset(); }
473inline QPoint QCommonListViewBase::pressedPosition() const { return dd->pressedPosition; }
474inline bool QCommonListViewBase::uniformItemSizes() const { return dd->uniformItemSizes; }
475inline int QCommonListViewBase::column() const { return dd->column; }
476
477inline QScrollBar *QCommonListViewBase::verticalScrollBar() const { return qq->verticalScrollBar(); }
478inline QScrollBar *QCommonListViewBase::horizontalScrollBar() const { return qq->horizontalScrollBar(); }
479inline QListView::ScrollMode QCommonListViewBase::verticalScrollMode() const { return qq->verticalScrollMode(); }
480inline QListView::ScrollMode QCommonListViewBase::horizontalScrollMode() const { return qq->horizontalScrollMode(); }
481
482inline QModelIndex QCommonListViewBase::modelIndex(int row) const
483 { return dd->model->index(row, dd->column, dd->root); }
484inline int QCommonListViewBase::rowCount() const { return dd->model->rowCount(dd->root); }
485
486inline void QCommonListViewBase::initViewItemOption(QStyleOptionViewItem *option) const { qq->initViewItemOption(option); }
487inline QWidget *QCommonListViewBase::viewport() const { return dd->viewport; }
488inline QRect QCommonListViewBase::clipRect() const { return dd->clipRect(); }
489
490inline QSize QCommonListViewBase::cachedItemSize() const { return dd->cachedItemSize; }
491inline QRect QCommonListViewBase::viewItemRect(const QListViewItem &item) const { return dd->viewItemRect(item); }
492inline QSize QCommonListViewBase::itemSize(const QStyleOptionViewItem &opt, const QModelIndex &idx) const
493 { return dd->itemSize(opt, idx); }
494
495inline QAbstractItemDelegate *QCommonListViewBase::delegate(const QModelIndex &idx) const
496 { return qq->itemDelegateForIndex(idx); }
497
498inline bool QCommonListViewBase::isHidden(int row) const { return dd->isHidden(row); }
499inline int QCommonListViewBase::hiddenCount() const { return dd->hiddenRows.count(); }
500
501inline bool QCommonListViewBase::isRightToLeft() const { return qq->isRightToLeft(); }
502
503QT_END_NAMESPACE
504
505#endif // QLISTVIEW_P_H
506