1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore 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 QITEMSELECTIONMODEL_H |
41 | #define QITEMSELECTIONMODEL_H |
42 | |
43 | #include <QtCore/qglobal.h> |
44 | |
45 | #include <QtCore/qset.h> |
46 | #include <QtCore/qvector.h> |
47 | #include <QtCore/qlist.h> |
48 | #include <QtCore/qabstractitemmodel.h> |
49 | |
50 | QT_REQUIRE_CONFIG(itemmodel); |
51 | |
52 | QT_BEGIN_NAMESPACE |
53 | |
54 | class Q_CORE_EXPORT QItemSelectionRange |
55 | { |
56 | |
57 | public: |
58 | inline QItemSelectionRange() : tl(), br() {} |
59 | #if QT_VERSION < QT_VERSION_CHECK(6,0,0) |
60 | // ### Qt 6: remove them all, the compiler-generated ones are fine |
61 | inline QItemSelectionRange(const QItemSelectionRange &other) |
62 | : tl(other.tl), br(other.br) {} |
63 | QItemSelectionRange(QItemSelectionRange &&other) noexcept |
64 | : tl(std::move(other.tl)), br(std::move(other.br)) {} |
65 | QItemSelectionRange &operator=(QItemSelectionRange &&other) noexcept |
66 | { tl = std::move(other.tl); br = std::move(other.br); return *this; } |
67 | QItemSelectionRange &operator=(const QItemSelectionRange &other) |
68 | { tl = other.tl; br = other.br; return *this; } |
69 | #endif // Qt < 6 |
70 | QItemSelectionRange(const QModelIndex &topL, const QModelIndex &bottomR) : tl(topL), br(bottomR) {} |
71 | explicit QItemSelectionRange(const QModelIndex &index) : tl(index), br(tl) {} |
72 | |
73 | void swap(QItemSelectionRange &other) noexcept |
74 | { |
75 | qSwap(tl, other.tl); |
76 | qSwap(br, other.br); |
77 | } |
78 | |
79 | inline int top() const { return tl.row(); } |
80 | inline int left() const { return tl.column(); } |
81 | inline int bottom() const { return br.row(); } |
82 | inline int right() const { return br.column(); } |
83 | inline int width() const { return br.column() - tl.column() + 1; } |
84 | inline int height() const { return br.row() - tl.row() + 1; } |
85 | |
86 | inline const QPersistentModelIndex &topLeft() const { return tl; } |
87 | inline const QPersistentModelIndex &bottomRight() const { return br; } |
88 | inline QModelIndex parent() const { return tl.parent(); } |
89 | inline const QAbstractItemModel *model() const { return tl.model(); } |
90 | |
91 | inline bool contains(const QModelIndex &index) const |
92 | { |
93 | return (parent() == index.parent() |
94 | && tl.row() <= index.row() && tl.column() <= index.column() |
95 | && br.row() >= index.row() && br.column() >= index.column()); |
96 | } |
97 | |
98 | inline bool contains(int row, int column, const QModelIndex &parentIndex) const |
99 | { |
100 | return (parent() == parentIndex |
101 | && tl.row() <= row && tl.column() <= column |
102 | && br.row() >= row && br.column() >= column); |
103 | } |
104 | |
105 | bool intersects(const QItemSelectionRange &other) const; |
106 | #if QT_DEPRECATED_SINCE(5, 0) |
107 | inline QItemSelectionRange intersect(const QItemSelectionRange &other) const |
108 | { return intersected(other); } |
109 | #endif |
110 | QItemSelectionRange intersected(const QItemSelectionRange &other) const; |
111 | |
112 | |
113 | inline bool operator==(const QItemSelectionRange &other) const |
114 | { return (tl == other.tl && br == other.br); } |
115 | inline bool operator!=(const QItemSelectionRange &other) const |
116 | { return !operator==(other); } |
117 | #if QT_DEPRECATED_SINCE(5, 15) |
118 | QT_DEPRECATED bool operator<(const QItemSelectionRange &other) const; |
119 | #endif |
120 | |
121 | inline bool isValid() const |
122 | { |
123 | return (tl.isValid() && br.isValid() && tl.parent() == br.parent() |
124 | && top() <= bottom() && left() <= right()); |
125 | } |
126 | |
127 | bool isEmpty() const; |
128 | |
129 | QModelIndexList indexes() const; |
130 | |
131 | private: |
132 | QPersistentModelIndex tl, br; |
133 | }; |
134 | Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_MOVABLE_TYPE); |
135 | |
136 | class QItemSelection; |
137 | class QItemSelectionModelPrivate; |
138 | |
139 | class Q_CORE_EXPORT QItemSelectionModel : public QObject |
140 | { |
141 | Q_OBJECT |
142 | Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged) |
143 | Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectionChanged STORED false DESIGNABLE false) |
144 | Q_PROPERTY(QModelIndex currentIndex READ currentIndex NOTIFY currentChanged STORED false DESIGNABLE false) |
145 | Q_PROPERTY(QItemSelection selection READ selection NOTIFY selectionChanged STORED false DESIGNABLE false) |
146 | Q_PROPERTY(QModelIndexList selectedIndexes READ selectedIndexes NOTIFY selectionChanged STORED false DESIGNABLE false) |
147 | |
148 | Q_DECLARE_PRIVATE(QItemSelectionModel) |
149 | |
150 | public: |
151 | |
152 | enum SelectionFlag { |
153 | NoUpdate = 0x0000, |
154 | Clear = 0x0001, |
155 | Select = 0x0002, |
156 | Deselect = 0x0004, |
157 | Toggle = 0x0008, |
158 | Current = 0x0010, |
159 | Rows = 0x0020, |
160 | Columns = 0x0040, |
161 | SelectCurrent = Select | Current, |
162 | ToggleCurrent = Toggle | Current, |
163 | ClearAndSelect = Clear | Select |
164 | }; |
165 | |
166 | Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag) |
167 | Q_FLAG(SelectionFlags) |
168 | |
169 | explicit QItemSelectionModel(QAbstractItemModel *model = nullptr); |
170 | explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent); |
171 | virtual ~QItemSelectionModel(); |
172 | |
173 | QModelIndex currentIndex() const; |
174 | |
175 | Q_INVOKABLE bool isSelected(const QModelIndex &index) const; |
176 | Q_INVOKABLE bool isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const; |
177 | Q_INVOKABLE bool isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const; |
178 | |
179 | Q_INVOKABLE bool rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const; |
180 | Q_INVOKABLE bool columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const; |
181 | |
182 | bool hasSelection() const; |
183 | |
184 | QModelIndexList selectedIndexes() const; |
185 | Q_INVOKABLE QModelIndexList selectedRows(int column = 0) const; |
186 | Q_INVOKABLE QModelIndexList selectedColumns(int row = 0) const; |
187 | const QItemSelection selection() const; |
188 | |
189 | // ### Qt 6: Merge these two as "QAbstractItemModel *model() const" |
190 | const QAbstractItemModel *model() const; |
191 | QAbstractItemModel *model(); |
192 | |
193 | void setModel(QAbstractItemModel *model); |
194 | |
195 | public Q_SLOTS: |
196 | virtual void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command); |
197 | virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command); |
198 | virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command); |
199 | virtual void clear(); |
200 | virtual void reset(); |
201 | |
202 | void clearSelection(); |
203 | virtual void clearCurrentIndex(); |
204 | |
205 | Q_SIGNALS: |
206 | void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); |
207 | void currentChanged(const QModelIndex ¤t, const QModelIndex &previous); |
208 | void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); |
209 | void currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous); |
210 | void modelChanged(QAbstractItemModel *model); |
211 | |
212 | protected: |
213 | QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model); |
214 | void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection); |
215 | |
216 | private: |
217 | Q_DISABLE_COPY(QItemSelectionModel) |
218 | Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int)) |
219 | Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeRemoved(const QModelIndex&, int, int)) |
220 | Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeInserted(const QModelIndex&, int, int)) |
221 | Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex&, int, int)) |
222 | Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoHint)) |
223 | Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoHint)) |
224 | }; |
225 | |
226 | Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags) |
227 | |
228 | // dummy implentation of qHash() necessary for instantiating QList<QItemSelectionRange>::toSet() with MSVC |
229 | inline uint qHash(const QItemSelectionRange &) { return 0; } |
230 | |
231 | #ifdef Q_CC_MSVC |
232 | |
233 | /* |
234 | ### Qt 6: |
235 | ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because |
236 | ### Qt exports QItemSelection that inherits QList<QItemSelectionRange>. |
237 | */ |
238 | |
239 | # ifndef Q_TEMPLATE_EXTERN |
240 | # if defined(QT_BUILD_CORE_LIB) |
241 | # define Q_TEMPLATE_EXTERN |
242 | # else |
243 | # define Q_TEMPLATE_EXTERN extern |
244 | # endif |
245 | # endif |
246 | Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QList<QItemSelectionRange>; |
247 | #endif // Q_CC_MSVC |
248 | |
249 | class Q_CORE_EXPORT QItemSelection : public QList<QItemSelectionRange> |
250 | { |
251 | public: |
252 | QItemSelection() noexcept : QList<QItemSelectionRange>() {} |
253 | QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
254 | |
255 | // reusing QList::swap() here is OK! |
256 | |
257 | void select(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
258 | bool contains(const QModelIndex &index) const; |
259 | QModelIndexList indexes() const; |
260 | void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command); |
261 | static void split(const QItemSelectionRange &range, |
262 | const QItemSelectionRange &other, |
263 | QItemSelection *result); |
264 | }; |
265 | Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QItemSelection) |
266 | |
267 | #ifndef QT_NO_DEBUG_STREAM |
268 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &); |
269 | #endif |
270 | |
271 | QT_END_NAMESPACE |
272 | |
273 | Q_DECLARE_METATYPE(QItemSelectionRange) |
274 | Q_DECLARE_METATYPE(QItemSelection) |
275 | |
276 | #endif // QITEMSELECTIONMODEL_H |
277 | |