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
qitemselectionmodel.h
Go to the documentation of this file.
1// Copyright (C) 2020 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 QITEMSELECTIONMODEL_H
5#define QITEMSELECTIONMODEL_H
6
7#include <QtCore/qglobal.h>
8
9#include <QtCore/qabstractitemmodel.h>
10#include <QtCore/qlist.h>
11#include <QtCore/qset.h>
12
14
16
18{
19
20public:
21 QItemSelectionRange() = default;
22 QItemSelectionRange(const QModelIndex &topL, const QModelIndex &bottomR) : tl(topL), br(bottomR) {}
23 explicit QItemSelectionRange(const QModelIndex &index) : tl(index), br(tl) {}
24
25 void swap(QItemSelectionRange &other) noexcept
26 {
27 tl.swap(other.tl);
28 br.swap(other.br);
29 }
30
31 inline int top() const { return tl.row(); }
32 inline int left() const { return tl.column(); }
33 inline int bottom() const { return br.row(); }
34 inline int right() const { return br.column(); }
35 inline int width() const { return br.column() - tl.column() + 1; }
36 inline int height() const { return br.row() - tl.row() + 1; }
37
38 inline const QPersistentModelIndex &topLeft() const { return tl; }
39 inline const QPersistentModelIndex &bottomRight() const { return br; }
40 inline QModelIndex parent() const { return tl.parent(); }
41 inline const QAbstractItemModel *model() const { return tl.model(); }
42
43 inline bool contains(const QModelIndex &index) const
44 {
45 return contains(index.row(), index.column(), index.parent());
46 }
47
48 inline bool contains(int row, int column, const QModelIndex &parentIndex) const
49 {
50 return (br.row() >= row && br.column() >= column &&
51 tl.row() <= row && tl.column() <= column &&
52 parent() == parentIndex);
53 }
54
55 bool intersects(const QItemSelectionRange &other) const;
56 QItemSelectionRange intersected(const QItemSelectionRange &other) const;
57
58#if QT_CORE_REMOVED_SINCE(6, 8)
59 inline bool operator==(const QItemSelectionRange &other) const
60 { return comparesEqual(*this, other); }
61 inline bool operator!=(const QItemSelectionRange &other) const
62 { return !operator==(other); }
63#endif
64 inline bool isValid() const
65 {
66 return (tl.isValid() && br.isValid() && tl.parent() == br.parent()
67 && top() <= bottom() && left() <= right());
68 }
69
70 bool isEmpty() const;
71
72 QModelIndexList indexes() const;
73
74private:
75 friend bool comparesEqual(const QItemSelectionRange &lhs,
76 const QItemSelectionRange &rhs) noexcept
77 {
78 return comparesEqual(lhs.tl, rhs.tl) && comparesEqual(lhs.br, rhs.br);
79 }
80 Q_DECLARE_EQUALITY_COMPARABLE(QItemSelectionRange)
81 QPersistentModelIndex tl, br;
82};
83Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_RELOCATABLE_TYPE);
84
85class QItemSelection;
86class QItemSelectionModelPrivate;
87
88class Q_CORE_EXPORT QItemSelectionModel : public QObject
89{
90 Q_OBJECT
91 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged
92 BINDABLE bindableModel)
93 Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectionChanged STORED false
94 DESIGNABLE false)
95 Q_PROPERTY(QModelIndex currentIndex READ currentIndex NOTIFY currentChanged STORED false
96 DESIGNABLE false)
97 Q_PROPERTY(QItemSelection selection READ selection NOTIFY selectionChanged STORED false
98 DESIGNABLE false)
99 Q_PROPERTY(QModelIndexList selectedIndexes READ selectedIndexes NOTIFY selectionChanged
100 STORED false DESIGNABLE false)
101
102 Q_DECLARE_PRIVATE(QItemSelectionModel)
103
104public:
105
106 enum SelectionFlag {
107 NoUpdate = 0x0000,
108 Clear = 0x0001,
109 Select = 0x0002,
110 Deselect = 0x0004,
111 Toggle = 0x0008,
112 Current = 0x0010,
113 Rows = 0x0020,
114 Columns = 0x0040,
115 SelectCurrent = Select | Current,
116 ToggleCurrent = Toggle | Current,
117 ClearAndSelect = Clear | Select
118 };
119
120 Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
121 Q_FLAG(SelectionFlags)
122
123 explicit QItemSelectionModel(QAbstractItemModel *model = nullptr);
124 explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent);
125 virtual ~QItemSelectionModel();
126
127 QModelIndex currentIndex() const;
128
129 Q_INVOKABLE bool isSelected(const QModelIndex &index) const;
130 Q_INVOKABLE bool isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const;
131 Q_INVOKABLE bool isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const;
132
133 Q_INVOKABLE bool rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const;
134 Q_INVOKABLE bool columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const;
135
136 bool hasSelection() const;
137
138 QModelIndexList selectedIndexes() const;
139 Q_INVOKABLE QModelIndexList selectedRows(int column = 0) const;
140 Q_INVOKABLE QModelIndexList selectedColumns(int row = 0) const;
141 const QItemSelection selection() const;
142
143 const QAbstractItemModel *model() const;
144 QAbstractItemModel *model();
145 QBindable<QAbstractItemModel *> bindableModel();
146
147 void setModel(QAbstractItemModel *model);
148
149public Q_SLOTS:
150 virtual void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
151 virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
152 virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
153 virtual void clear();
154 virtual void reset();
155
156 void clearSelection();
157 virtual void clearCurrentIndex();
158
159Q_SIGNALS:
160 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
161 void currentChanged(const QModelIndex &current, const QModelIndex &previous);
162 void currentRowChanged(const QModelIndex &current, const QModelIndex &previous);
163 void currentColumnChanged(const QModelIndex &current, const QModelIndex &previous);
164 void modelChanged(QAbstractItemModel *model);
165
166protected:
167 QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model);
168 void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection);
169
170private:
171 Q_DISABLE_COPY(QItemSelectionModel)
172};
173
174Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags)
175
176// We export each out-of-line method individually to prevent MSVC from
177// exporting the whole QList class.
178class QItemSelection : public QList<QItemSelectionRange>
179{
180public:
181 using QList<QItemSelectionRange>::QList;
182 Q_CORE_EXPORT QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight);
183
184 // reusing QList::swap() here is OK!
185
186 Q_CORE_EXPORT void select(const QModelIndex &topLeft, const QModelIndex &bottomRight);
187 Q_CORE_EXPORT bool contains(const QModelIndex &index) const;
188 Q_CORE_EXPORT QModelIndexList indexes() const;
189 Q_CORE_EXPORT void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command);
190 Q_CORE_EXPORT static void split(const QItemSelectionRange &range,
191 const QItemSelectionRange &other,
192 QItemSelection *result);
193};
194Q_DECLARE_SHARED(QItemSelection)
195
196#ifndef QT_NO_DEBUG_STREAM
197Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
198#endif
199
200QT_END_NAMESPACE
201
202QT_DECL_METATYPE_EXTERN(QItemSelectionRange, Q_CORE_EXPORT)
203QT_DECL_METATYPE_EXTERN(QItemSelection, Q_CORE_EXPORT)
204
205#endif // QITEMSELECTIONMODEL_H
The QAbstractProxyModel class provides a base class for proxy item models that can do sorting,...
QT_REQUIRE_CONFIG(proxymodel)
Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2439
QT_REQUIRE_CONFIG(itemmodel)