1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #ifndef QABSTRACTITEMMODEL_H |
6 | #define QABSTRACTITEMMODEL_H |
7 | |
8 | #include <QtCore/qhash.h> |
9 | #include <QtCore/qlist.h> |
10 | #include <QtCore/qobject.h> |
11 | #include <QtCore/qvariant.h> |
12 | |
13 | QT_REQUIRE_CONFIG(itemmodel); |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | class QModelRoleData |
18 | { |
19 | int m_role; |
20 | QVariant m_data; |
21 | |
22 | public: |
23 | explicit QModelRoleData(int role) noexcept |
24 | : m_role(role) |
25 | {} |
26 | |
27 | constexpr int role() const noexcept { return m_role; } |
28 | constexpr QVariant &data() noexcept { return m_data; } |
29 | constexpr const QVariant &data() const noexcept { return m_data; } |
30 | |
31 | template <typename T> |
32 | constexpr void setData(T &&value) noexcept(noexcept(m_data.setValue(std::forward<T>(value)))) |
33 | { m_data.setValue(std::forward<T>(value)); } |
34 | |
35 | void clearData() noexcept { m_data.clear(); } |
36 | }; |
37 | |
38 | Q_DECLARE_TYPEINFO(QModelRoleData, Q_RELOCATABLE_TYPE); |
39 | |
40 | class QModelRoleDataSpan; |
41 | |
42 | namespace QtPrivate { |
43 | template <typename T, typename Enable = void> |
44 | struct IsContainerCompatibleWithModelRoleDataSpan : std::false_type {}; |
45 | |
46 | template <typename T> |
47 | struct IsContainerCompatibleWithModelRoleDataSpan<T, std::enable_if_t<std::conjunction_v< |
48 | // lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ... |
49 | std::is_convertible<decltype( std::data(std::declval<T &>()) ), QModelRoleData *>, |
50 | // ... and that has a suitable size ... |
51 | std::is_convertible<decltype( std::size(std::declval<T &>()) ), qsizetype>, |
52 | // ... and it's a range as it defines an iterator-like API |
53 | std::is_convertible< |
54 | typename std::iterator_traits<decltype( std::begin(std::declval<T &>()) )>::value_type, |
55 | QModelRoleData |
56 | >, |
57 | std::is_convertible< |
58 | decltype( std::begin(std::declval<T &>()) != std::end(std::declval<T &>()) ), |
59 | bool>, |
60 | // Don't make an accidental copy constructor |
61 | std::negation<std::is_same<std::decay_t<T>, QModelRoleDataSpan>> |
62 | >>> : std::true_type {}; |
63 | } // namespace QtPrivate |
64 | |
65 | class QModelRoleDataSpan |
66 | { |
67 | QModelRoleData *m_modelRoleData = nullptr; |
68 | qsizetype m_len = 0; |
69 | |
70 | template <typename T> |
71 | using if_compatible_container = std::enable_if_t<QtPrivate::IsContainerCompatibleWithModelRoleDataSpan<T>::value, bool>; |
72 | |
73 | public: |
74 | constexpr QModelRoleDataSpan() noexcept {} |
75 | |
76 | constexpr QModelRoleDataSpan(QModelRoleData &modelRoleData) noexcept |
77 | : m_modelRoleData(&modelRoleData), |
78 | m_len(1) |
79 | {} |
80 | |
81 | constexpr QModelRoleDataSpan(QModelRoleData *modelRoleData, qsizetype len) |
82 | : m_modelRoleData(modelRoleData), |
83 | m_len(len) |
84 | {} |
85 | |
86 | template <typename Container, if_compatible_container<Container> = true> |
87 | constexpr QModelRoleDataSpan(Container &c) noexcept(noexcept(std::data(c)) && noexcept(std::size(c))) |
88 | : m_modelRoleData(std::data(c)), |
89 | m_len(qsizetype(std::size(c))) |
90 | {} |
91 | |
92 | constexpr qsizetype size() const noexcept { return m_len; } |
93 | constexpr qsizetype length() const noexcept { return m_len; } |
94 | constexpr QModelRoleData *data() const noexcept { return m_modelRoleData; } |
95 | constexpr QModelRoleData *begin() const noexcept { return m_modelRoleData; } |
96 | constexpr QModelRoleData *end() const noexcept { return m_modelRoleData + m_len; } |
97 | constexpr QModelRoleData &operator[](qsizetype index) const { return m_modelRoleData[index]; } |
98 | |
99 | constexpr QVariant *dataForRole(int role) const |
100 | { |
101 | #ifdef __cpp_lib_constexpr_algorithms |
102 | auto result = std::find_if(begin(), end(), [role](const QModelRoleData &roleData) { |
103 | return roleData.role() == role; |
104 | }); |
105 | #else |
106 | auto result = begin(); |
107 | const auto e = end(); |
108 | for (; result != e; ++result) { |
109 | if (result->role() == role) |
110 | break; |
111 | } |
112 | #endif |
113 | |
114 | return Q_ASSERT(result != end()), &result->data(); |
115 | } |
116 | }; |
117 | |
118 | Q_DECLARE_TYPEINFO(QModelRoleDataSpan, Q_RELOCATABLE_TYPE); |
119 | |
120 | class QAbstractItemModel; |
121 | class QPersistentModelIndex; |
122 | |
123 | class QModelIndex |
124 | { |
125 | friend class QAbstractItemModel; |
126 | public: |
127 | constexpr inline QModelIndex() noexcept : r(-1), c(-1), i(0), m(nullptr) {} |
128 | // compiler-generated copy/move ctors/assignment operators are fine! |
129 | constexpr inline int row() const noexcept { return r; } |
130 | constexpr inline int column() const noexcept { return c; } |
131 | constexpr inline quintptr internalId() const noexcept { return i; } |
132 | inline void *internalPointer() const noexcept { return reinterpret_cast<void*>(i); } |
133 | inline const void *constInternalPointer() const noexcept { return reinterpret_cast<const void *>(i); } |
134 | inline QModelIndex parent() const; |
135 | inline QModelIndex sibling(int row, int column) const; |
136 | inline QModelIndex siblingAtColumn(int column) const; |
137 | inline QModelIndex siblingAtRow(int row) const; |
138 | inline QVariant data(int role = Qt::DisplayRole) const; |
139 | inline void multiData(QModelRoleDataSpan roleDataSpan) const; |
140 | inline Qt::ItemFlags flags() const; |
141 | constexpr inline const QAbstractItemModel *model() const noexcept { return m; } |
142 | constexpr inline bool isValid() const noexcept { return (r >= 0) && (c >= 0) && (m != nullptr); } |
143 | constexpr inline bool operator==(const QModelIndex &other) const noexcept |
144 | { return (other.r == r) && (other.i == i) && (other.c == c) && (other.m == m); } |
145 | constexpr inline bool operator!=(const QModelIndex &other) const noexcept |
146 | { return !(*this == other); } |
147 | constexpr inline bool operator<(const QModelIndex &other) const noexcept |
148 | { |
149 | return r < other.r |
150 | || (r == other.r && (c < other.c |
151 | || (c == other.c && (i < other.i |
152 | || (i == other.i && std::less<const QAbstractItemModel *>()(m, other.m)))))); |
153 | } |
154 | private: |
155 | inline QModelIndex(int arow, int acolumn, const void *ptr, const QAbstractItemModel *amodel) noexcept |
156 | : r(arow), c(acolumn), i(reinterpret_cast<quintptr>(ptr)), m(amodel) {} |
157 | constexpr inline QModelIndex(int arow, int acolumn, quintptr id, const QAbstractItemModel *amodel) noexcept |
158 | : r(arow), c(acolumn), i(id), m(amodel) {} |
159 | int r, c; |
160 | quintptr i; |
161 | const QAbstractItemModel *m; |
162 | }; |
163 | Q_DECLARE_TYPEINFO(QModelIndex, Q_RELOCATABLE_TYPE); |
164 | |
165 | #ifndef QT_NO_DEBUG_STREAM |
166 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QModelIndex &); |
167 | #endif |
168 | |
169 | class QPersistentModelIndexData; |
170 | |
171 | // qHash is a friend, but we can't use default arguments for friends (§8.3.6.4) |
172 | size_t qHash(const QPersistentModelIndex &index, size_t seed = 0) noexcept; |
173 | |
174 | class Q_CORE_EXPORT QPersistentModelIndex |
175 | { |
176 | public: |
177 | QPersistentModelIndex(); |
178 | QPersistentModelIndex(const QModelIndex &index); |
179 | QPersistentModelIndex(const QPersistentModelIndex &other); |
180 | ~QPersistentModelIndex(); |
181 | bool operator<(const QPersistentModelIndex &other) const; |
182 | bool operator==(const QPersistentModelIndex &other) const; |
183 | inline bool operator!=(const QPersistentModelIndex &other) const |
184 | { return !operator==(other); } |
185 | QPersistentModelIndex &operator=(const QPersistentModelIndex &other); |
186 | inline QPersistentModelIndex(QPersistentModelIndex &&other) noexcept |
187 | : d(std::exchange(obj&: other.d, new_val: nullptr)) {} |
188 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPersistentModelIndex) |
189 | void swap(QPersistentModelIndex &other) noexcept { qt_ptr_swap(lhs&: d, rhs&: other.d); } |
190 | bool operator==(const QModelIndex &other) const; |
191 | bool operator!=(const QModelIndex &other) const; |
192 | QPersistentModelIndex &operator=(const QModelIndex &other); |
193 | operator QModelIndex() const; |
194 | int row() const; |
195 | int column() const; |
196 | void *internalPointer() const; |
197 | const void *constInternalPointer() const; |
198 | quintptr internalId() const; |
199 | QModelIndex parent() const; |
200 | QModelIndex sibling(int row, int column) const; |
201 | QVariant data(int role = Qt::DisplayRole) const; |
202 | void multiData(QModelRoleDataSpan roleDataSpan) const; |
203 | Qt::ItemFlags flags() const; |
204 | const QAbstractItemModel *model() const; |
205 | bool isValid() const; |
206 | private: |
207 | QPersistentModelIndexData *d; |
208 | friend size_t qHash(const QPersistentModelIndex &, size_t seed) noexcept; |
209 | friend bool qHashEquals(const QPersistentModelIndex &a, const QPersistentModelIndex &b) noexcept |
210 | { return a.d == b.d; } |
211 | #ifndef QT_NO_DEBUG_STREAM |
212 | friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &); |
213 | #endif |
214 | }; |
215 | Q_DECLARE_SHARED(QPersistentModelIndex) |
216 | |
217 | inline size_t qHash(const QPersistentModelIndex &index, size_t seed) noexcept |
218 | { return qHash(t: index.d, seed); } |
219 | |
220 | |
221 | #ifndef QT_NO_DEBUG_STREAM |
222 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &); |
223 | #endif |
224 | |
225 | typedef QList<QModelIndex> QModelIndexList; |
226 | |
227 | class QMimeData; |
228 | class QAbstractItemModelPrivate; |
229 | class QTransposeProxyModelPrivate; |
230 | template <class Key, class T> class QMap; |
231 | |
232 | |
233 | class Q_CORE_EXPORT QAbstractItemModel : public QObject |
234 | { |
235 | Q_OBJECT |
236 | |
237 | friend class QPersistentModelIndexData; |
238 | friend class QAbstractItemViewPrivate; |
239 | friend class QAbstractProxyModel; |
240 | public: |
241 | |
242 | explicit QAbstractItemModel(QObject *parent = nullptr); |
243 | virtual ~QAbstractItemModel(); |
244 | |
245 | Q_INVOKABLE bool hasIndex(int row, int column, const QModelIndex &parent = QModelIndex()) const; |
246 | Q_INVOKABLE virtual QModelIndex index(int row, int column, |
247 | const QModelIndex &parent = QModelIndex()) const = 0; |
248 | Q_INVOKABLE virtual QModelIndex parent(const QModelIndex &child) const = 0; |
249 | |
250 | Q_INVOKABLE virtual QModelIndex sibling(int row, int column, const QModelIndex &idx) const; |
251 | Q_INVOKABLE virtual int rowCount(const QModelIndex &parent = QModelIndex()) const = 0; |
252 | Q_INVOKABLE virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0; |
253 | Q_INVOKABLE virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const; |
254 | |
255 | Q_INVOKABLE virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0; |
256 | Q_INVOKABLE virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); |
257 | |
258 | Q_INVOKABLE virtual QVariant (int section, Qt::Orientation orientation, |
259 | int role = Qt::DisplayRole) const; |
260 | virtual bool (int section, Qt::Orientation orientation, const QVariant &value, |
261 | int role = Qt::EditRole); |
262 | |
263 | virtual QMap<int, QVariant> itemData(const QModelIndex &index) const; |
264 | virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles); |
265 | virtual bool clearItemData(const QModelIndex &index); |
266 | |
267 | virtual QStringList mimeTypes() const; |
268 | virtual QMimeData *mimeData(const QModelIndexList &indexes) const; |
269 | virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, |
270 | int row, int column, const QModelIndex &parent) const; |
271 | virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
272 | int row, int column, const QModelIndex &parent); |
273 | virtual Qt::DropActions supportedDropActions() const; |
274 | virtual Qt::DropActions supportedDragActions() const; |
275 | |
276 | Q_INVOKABLE Q_REVISION(6, 4) virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()); |
277 | Q_INVOKABLE Q_REVISION(6, 4) virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()); |
278 | Q_INVOKABLE Q_REVISION(6, 4) virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); |
279 | Q_INVOKABLE Q_REVISION(6, 4) virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()); |
280 | Q_INVOKABLE Q_REVISION(6, 4) virtual bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, |
281 | const QModelIndex &destinationParent, int destinationChild); |
282 | Q_INVOKABLE Q_REVISION(6, 4) virtual bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, |
283 | const QModelIndex &destinationParent, int destinationChild); |
284 | |
285 | Q_INVOKABLE Q_REVISION(6, 4) inline bool insertRow(int row, const QModelIndex &parent = QModelIndex()); |
286 | Q_INVOKABLE Q_REVISION(6, 4) inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex()); |
287 | Q_INVOKABLE Q_REVISION(6, 4) inline bool removeRow(int row, const QModelIndex &parent = QModelIndex()); |
288 | Q_INVOKABLE Q_REVISION(6, 4) inline bool removeColumn(int column, const QModelIndex &parent = QModelIndex()); |
289 | Q_INVOKABLE Q_REVISION(6, 4) inline bool moveRow(const QModelIndex &sourceParent, int sourceRow, |
290 | const QModelIndex &destinationParent, int destinationChild); |
291 | Q_INVOKABLE Q_REVISION(6, 4) inline bool moveColumn(const QModelIndex &sourceParent, int sourceColumn, |
292 | const QModelIndex &destinationParent, int destinationChild); |
293 | |
294 | Q_INVOKABLE virtual void fetchMore(const QModelIndex &parent); |
295 | Q_INVOKABLE virtual bool canFetchMore(const QModelIndex &parent) const; |
296 | Q_INVOKABLE virtual Qt::ItemFlags flags(const QModelIndex &index) const; |
297 | Q_INVOKABLE Q_REVISION(6, 4) virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); |
298 | virtual QModelIndex buddy(const QModelIndex &index) const; |
299 | Q_INVOKABLE virtual QModelIndexList match(const QModelIndex &start, int role, |
300 | const QVariant &value, int hits = 1, |
301 | Qt::MatchFlags flags = |
302 | Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const; |
303 | virtual QSize span(const QModelIndex &index) const; |
304 | |
305 | virtual QHash<int,QByteArray> roleNames() const; |
306 | |
307 | using QObject::parent; |
308 | |
309 | enum LayoutChangeHint |
310 | { |
311 | NoLayoutChangeHint, |
312 | VerticalSortHint, |
313 | HorizontalSortHint |
314 | }; |
315 | Q_ENUM(LayoutChangeHint) |
316 | |
317 | enum class CheckIndexOption { |
318 | NoOption = 0x0000, |
319 | IndexIsValid = 0x0001, |
320 | DoNotUseParent = 0x0002, |
321 | ParentIsInvalid = 0x0004, |
322 | }; |
323 | Q_ENUM(CheckIndexOption) |
324 | Q_DECLARE_FLAGS(CheckIndexOptions, CheckIndexOption) |
325 | |
326 | [[nodiscard]] bool checkIndex(const QModelIndex &index, CheckIndexOptions options = CheckIndexOption::NoOption) const; |
327 | |
328 | virtual void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const; |
329 | |
330 | Q_SIGNALS: |
331 | void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, |
332 | const QList<int> &roles = QList<int>()); |
333 | void (Qt::Orientation orientation, int first, int last); |
334 | void layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint); |
335 | void layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint); |
336 | |
337 | void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal); |
338 | void rowsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal); |
339 | |
340 | void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal); |
341 | void rowsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal); |
342 | |
343 | void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal); |
344 | void columnsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal); |
345 | |
346 | void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal); |
347 | void columnsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal); |
348 | |
349 | void modelAboutToBeReset(QPrivateSignal); |
350 | void modelReset(QPrivateSignal); |
351 | |
352 | void rowsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow, QPrivateSignal); |
353 | void rowsMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow, QPrivateSignal); |
354 | |
355 | void columnsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn, QPrivateSignal); |
356 | void columnsMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn, QPrivateSignal); |
357 | |
358 | public Q_SLOTS: |
359 | virtual bool submit(); |
360 | virtual void revert(); |
361 | |
362 | protected Q_SLOTS: |
363 | virtual void resetInternalData(); |
364 | |
365 | protected: |
366 | QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = nullptr); |
367 | |
368 | inline QModelIndex createIndex(int row, int column, const void *data = nullptr) const; |
369 | inline QModelIndex createIndex(int row, int column, quintptr id) const; |
370 | |
371 | void encodeData(const QModelIndexList &indexes, QDataStream &stream) const; |
372 | bool decodeData(int row, int column, const QModelIndex &parent, QDataStream &stream); |
373 | |
374 | void beginInsertRows(const QModelIndex &parent, int first, int last); |
375 | void endInsertRows(); |
376 | |
377 | void beginRemoveRows(const QModelIndex &parent, int first, int last); |
378 | void endRemoveRows(); |
379 | |
380 | bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow); |
381 | void endMoveRows(); |
382 | |
383 | void beginInsertColumns(const QModelIndex &parent, int first, int last); |
384 | void endInsertColumns(); |
385 | |
386 | void beginRemoveColumns(const QModelIndex &parent, int first, int last); |
387 | void endRemoveColumns(); |
388 | |
389 | bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn); |
390 | void endMoveColumns(); |
391 | |
392 | void beginResetModel(); |
393 | void endResetModel(); |
394 | |
395 | void changePersistentIndex(const QModelIndex &from, const QModelIndex &to); |
396 | void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to); |
397 | QModelIndexList persistentIndexList() const; |
398 | |
399 | private: |
400 | Q_DECLARE_PRIVATE(QAbstractItemModel) |
401 | Q_DISABLE_COPY(QAbstractItemModel) |
402 | }; |
403 | |
404 | Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractItemModel::CheckIndexOptions) |
405 | |
406 | inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent) |
407 | { return insertRows(row: arow, count: 1, parent: aparent); } |
408 | inline bool QAbstractItemModel::insertColumn(int acolumn, const QModelIndex &aparent) |
409 | { return insertColumns(column: acolumn, count: 1, parent: aparent); } |
410 | inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent) |
411 | { return removeRows(row: arow, count: 1, parent: aparent); } |
412 | inline bool QAbstractItemModel::removeColumn(int acolumn, const QModelIndex &aparent) |
413 | { return removeColumns(column: acolumn, count: 1, parent: aparent); } |
414 | inline bool QAbstractItemModel::moveRow(const QModelIndex &sourceParent, int sourceRow, |
415 | const QModelIndex &destinationParent, int destinationChild) |
416 | { return moveRows(sourceParent, sourceRow, count: 1, destinationParent, destinationChild); } |
417 | inline bool QAbstractItemModel::moveColumn(const QModelIndex &sourceParent, int sourceColumn, |
418 | const QModelIndex &destinationParent, int destinationChild) |
419 | { return moveColumns(sourceParent, sourceColumn, count: 1, destinationParent, destinationChild); } |
420 | inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, const void *adata) const |
421 | { return QModelIndex(arow, acolumn, adata, this); } |
422 | inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, quintptr aid) const |
423 | { return QModelIndex(arow, acolumn, aid, this); } |
424 | |
425 | class Q_CORE_EXPORT QAbstractTableModel : public QAbstractItemModel |
426 | { |
427 | Q_OBJECT |
428 | |
429 | public: |
430 | explicit QAbstractTableModel(QObject *parent = nullptr); |
431 | ~QAbstractTableModel(); |
432 | |
433 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; |
434 | QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; |
435 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
436 | int row, int column, const QModelIndex &parent) override; |
437 | |
438 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
439 | |
440 | using QObject::parent; |
441 | |
442 | protected: |
443 | QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent); |
444 | |
445 | private: |
446 | Q_DISABLE_COPY(QAbstractTableModel) |
447 | QModelIndex parent(const QModelIndex &child) const override; |
448 | bool hasChildren(const QModelIndex &parent) const override; |
449 | }; |
450 | |
451 | class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel |
452 | { |
453 | Q_OBJECT |
454 | |
455 | public: |
456 | explicit QAbstractListModel(QObject *parent = nullptr); |
457 | ~QAbstractListModel(); |
458 | |
459 | QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override; |
460 | QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; |
461 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
462 | int row, int column, const QModelIndex &parent) override; |
463 | |
464 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
465 | |
466 | using QObject::parent; |
467 | |
468 | protected: |
469 | QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent); |
470 | |
471 | private: |
472 | Q_DISABLE_COPY(QAbstractListModel) |
473 | QModelIndex parent(const QModelIndex &child) const override; |
474 | int columnCount(const QModelIndex &parent) const override; |
475 | bool hasChildren(const QModelIndex &parent) const override; |
476 | }; |
477 | |
478 | // inline implementations |
479 | |
480 | inline QModelIndex QModelIndex::parent() const |
481 | { return m ? m->parent(child: *this) : QModelIndex(); } |
482 | |
483 | inline QModelIndex QModelIndex::sibling(int arow, int acolumn) const |
484 | { return m ? (r == arow && c == acolumn) ? *this : m->sibling(row: arow, column: acolumn, idx: *this) : QModelIndex(); } |
485 | |
486 | inline QModelIndex QModelIndex::siblingAtColumn(int acolumn) const |
487 | { return m ? (c == acolumn) ? *this : m->sibling(row: r, column: acolumn, idx: *this) : QModelIndex(); } |
488 | |
489 | inline QModelIndex QModelIndex::siblingAtRow(int arow) const |
490 | { return m ? (r == arow) ? *this : m->sibling(row: arow, column: c, idx: *this) : QModelIndex(); } |
491 | |
492 | inline QVariant QModelIndex::data(int arole) const |
493 | { return m ? m->data(index: *this, role: arole) : QVariant(); } |
494 | |
495 | inline void QModelIndex::multiData(QModelRoleDataSpan roleDataSpan) const |
496 | { if (m) m->multiData(index: *this, roleDataSpan); } |
497 | |
498 | inline Qt::ItemFlags QModelIndex::flags() const |
499 | { return m ? m->flags(index: *this) : Qt::ItemFlags(); } |
500 | |
501 | inline size_t qHash(const QModelIndex &index, size_t seed = 0) noexcept |
502 | { |
503 | #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) |
504 | return qHashMulti(seed, index.row(), index.column(), index.internalId()); |
505 | #else |
506 | return size_t((size_t(index.row()) << 4) + size_t(index.column()) + index.internalId()) ^ seed; |
507 | #endif |
508 | } |
509 | |
510 | QT_END_NAMESPACE |
511 | |
512 | QT_DECL_METATYPE_EXTERN(QModelIndexList, Q_CORE_EXPORT) |
513 | |
514 | #endif // QABSTRACTITEMMODEL_H |
515 | |