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