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 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
50QT_REQUIRE_CONFIG(itemmodel);
51
52QT_BEGIN_NAMESPACE
53
54class Q_CORE_EXPORT QItemSelectionRange
55{
56
57public:
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 bool operator<(const QItemSelectionRange &other) const;
118
119 inline bool isValid() const
120 {
121 return (tl.isValid() && br.isValid() && tl.parent() == br.parent()
122 && top() <= bottom() && left() <= right());
123 }
124
125 bool isEmpty() const;
126
127 QModelIndexList indexes() const;
128
129private:
130 QPersistentModelIndex tl, br;
131};
132Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_MOVABLE_TYPE);
133
134class QItemSelection;
135class QItemSelectionModelPrivate;
136
137class Q_CORE_EXPORT QItemSelectionModel : public QObject
138{
139 Q_OBJECT
140 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
141 Q_PROPERTY(bool hasSelection READ hasSelection NOTIFY selectionChanged STORED false DESIGNABLE false)
142 Q_PROPERTY(QModelIndex currentIndex READ currentIndex NOTIFY currentChanged STORED false DESIGNABLE false)
143 Q_PROPERTY(QItemSelection selection READ selection NOTIFY selectionChanged STORED false DESIGNABLE false)
144 Q_PROPERTY(QModelIndexList selectedIndexes READ selectedIndexes NOTIFY selectionChanged STORED false DESIGNABLE false)
145
146 Q_DECLARE_PRIVATE(QItemSelectionModel)
147
148public:
149
150 enum SelectionFlag {
151 NoUpdate = 0x0000,
152 Clear = 0x0001,
153 Select = 0x0002,
154 Deselect = 0x0004,
155 Toggle = 0x0008,
156 Current = 0x0010,
157 Rows = 0x0020,
158 Columns = 0x0040,
159 SelectCurrent = Select | Current,
160 ToggleCurrent = Toggle | Current,
161 ClearAndSelect = Clear | Select
162 };
163
164 Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
165 Q_FLAG(SelectionFlags)
166
167 explicit QItemSelectionModel(QAbstractItemModel *model = nullptr);
168 explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent);
169 virtual ~QItemSelectionModel();
170
171 QModelIndex currentIndex() const;
172
173 Q_INVOKABLE bool isSelected(const QModelIndex &index) const;
174 Q_INVOKABLE bool isRowSelected(int row, const QModelIndex &parent) const;
175 Q_INVOKABLE bool isColumnSelected(int column, const QModelIndex &parent) const;
176
177 Q_INVOKABLE bool rowIntersectsSelection(int row, const QModelIndex &parent) const;
178 Q_INVOKABLE bool columnIntersectsSelection(int column, const QModelIndex &parent) const;
179
180 bool hasSelection() const;
181
182 QModelIndexList selectedIndexes() const;
183 Q_INVOKABLE QModelIndexList selectedRows(int column = 0) const;
184 Q_INVOKABLE QModelIndexList selectedColumns(int row = 0) const;
185 const QItemSelection selection() const;
186
187 // ### Qt 6: Merge these two as "QAbstractItemModel *model() const"
188 const QAbstractItemModel *model() const;
189 QAbstractItemModel *model();
190
191 void setModel(QAbstractItemModel *model);
192
193public Q_SLOTS:
194 virtual void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
195 virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
196 virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
197 virtual void clear();
198 virtual void reset();
199
200 void clearSelection();
201 virtual void clearCurrentIndex();
202
203Q_SIGNALS:
204 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
205 void currentChanged(const QModelIndex &current, const QModelIndex &previous);
206 void currentRowChanged(const QModelIndex &current, const QModelIndex &previous);
207 void currentColumnChanged(const QModelIndex &current, const QModelIndex &previous);
208 void modelChanged(QAbstractItemModel *model);
209
210protected:
211 QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model);
212 void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection);
213
214private:
215 Q_DISABLE_COPY(QItemSelectionModel)
216 Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int))
217 Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeRemoved(const QModelIndex&, int, int))
218 Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeInserted(const QModelIndex&, int, int))
219 Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex&, int, int))
220 Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoHint))
221 Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoHint))
222};
223
224Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags)
225
226// dummy implentation of qHash() necessary for instantiating QList<QItemSelectionRange>::toSet() with MSVC
227inline uint qHash(const QItemSelectionRange &) { return 0; }
228
229#ifdef Q_CC_MSVC
230
231/*
232 ### Qt 6:
233 ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because
234 ### Qt exports QItemSelection that inherits QList<QItemSelectionRange>.
235*/
236
237# ifndef Q_TEMPLATE_EXTERN
238# if defined(QT_BUILD_CORE_LIB)
239# define Q_TEMPLATE_EXTERN
240# else
241# define Q_TEMPLATE_EXTERN extern
242# endif
243# endif
244Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QList<QItemSelectionRange>;
245#endif // Q_CC_MSVC
246
247class Q_CORE_EXPORT QItemSelection : public QList<QItemSelectionRange>
248{
249public:
250 QItemSelection() noexcept : QList<QItemSelectionRange>() {}
251 QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight);
252
253 // reusing QList::swap() here is OK!
254
255 void select(const QModelIndex &topLeft, const QModelIndex &bottomRight);
256 bool contains(const QModelIndex &index) const;
257 QModelIndexList indexes() const;
258 void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command);
259 static void split(const QItemSelectionRange &range,
260 const QItemSelectionRange &other,
261 QItemSelection *result);
262};
263Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QItemSelection)
264
265#ifndef QT_NO_DEBUG_STREAM
266Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
267#endif
268
269QT_END_NAMESPACE
270
271Q_DECLARE_METATYPE(QItemSelectionRange)
272Q_DECLARE_METATYPE(QItemSelection)
273
274#endif // QITEMSELECTIONMODEL_H
275