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 QABSTRACTITEMMODEL_H
41#define QABSTRACTITEMMODEL_H
42
43#include <QtCore/qvariant.h>
44#include <QtCore/qobject.h>
45#include <QtCore/qhash.h>
46#include <QtCore/qvector.h>
47
48QT_REQUIRE_CONFIG(itemmodel);
49
50QT_BEGIN_NAMESPACE
51
52
53class QAbstractItemModel;
54class QPersistentModelIndex;
55
56class Q_CORE_EXPORT QModelIndex
57{
58 friend class QAbstractItemModel;
59public:
60 Q_DECL_CONSTEXPR inline QModelIndex() noexcept : r(-1), c(-1), i(0), m(nullptr) {}
61 // compiler-generated copy/move ctors/assignment operators are fine!
62 Q_DECL_CONSTEXPR inline int row() const noexcept { return r; }
63 Q_DECL_CONSTEXPR inline int column() const noexcept { return c; }
64 Q_DECL_CONSTEXPR inline quintptr internalId() const noexcept { return i; }
65 inline void *internalPointer() const noexcept { return reinterpret_cast<void*>(i); }
66 inline QModelIndex parent() const;
67 inline QModelIndex sibling(int row, int column) const;
68 inline QModelIndex siblingAtColumn(int column) const;
69 inline QModelIndex siblingAtRow(int row) const;
70#if QT_DEPRECATED_SINCE(5, 8)
71 QT_DEPRECATED_X("Use QAbstractItemModel::index") inline QModelIndex child(int row, int column) const;
72#endif
73 inline QVariant data(int role = Qt::DisplayRole) const;
74 inline Qt::ItemFlags flags() const;
75 Q_DECL_CONSTEXPR inline const QAbstractItemModel *model() const noexcept { return m; }
76 Q_DECL_CONSTEXPR inline bool isValid() const noexcept { return (r >= 0) && (c >= 0) && (m != nullptr); }
77 Q_DECL_CONSTEXPR inline bool operator==(const QModelIndex &other) const noexcept
78 { return (other.r == r) && (other.i == i) && (other.c == c) && (other.m == m); }
79 Q_DECL_CONSTEXPR inline bool operator!=(const QModelIndex &other) const noexcept
80 { return !(*this == other); }
81 Q_DECL_CONSTEXPR inline bool operator<(const QModelIndex &other) const noexcept
82 {
83 return r < other.r
84 || (r == other.r && (c < other.c
85 || (c == other.c && (i < other.i
86 || (i == other.i && std::less<const QAbstractItemModel *>()(m, other.m))))));
87 }
88private:
89 inline QModelIndex(int arow, int acolumn, void *ptr, const QAbstractItemModel *amodel) noexcept
90 : r(arow), c(acolumn), i(reinterpret_cast<quintptr>(ptr)), m(amodel) {}
91 Q_DECL_CONSTEXPR inline QModelIndex(int arow, int acolumn, quintptr id, const QAbstractItemModel *amodel) noexcept
92 : r(arow), c(acolumn), i(id), m(amodel) {}
93 int r, c;
94 quintptr i;
95 const QAbstractItemModel *m;
96};
97Q_DECLARE_TYPEINFO(QModelIndex, Q_MOVABLE_TYPE);
98
99#ifndef QT_NO_DEBUG_STREAM
100Q_CORE_EXPORT QDebug operator<<(QDebug, const QModelIndex &);
101#endif
102
103class QPersistentModelIndexData;
104
105// qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4)
106uint qHash(const QPersistentModelIndex &index, uint seed = 0) noexcept;
107
108class Q_CORE_EXPORT QPersistentModelIndex
109{
110public:
111 QPersistentModelIndex();
112 QPersistentModelIndex(const QModelIndex &index);
113 QPersistentModelIndex(const QPersistentModelIndex &other);
114 ~QPersistentModelIndex();
115 bool operator<(const QPersistentModelIndex &other) const;
116 bool operator==(const QPersistentModelIndex &other) const;
117 inline bool operator!=(const QPersistentModelIndex &other) const
118 { return !operator==(other); }
119 QPersistentModelIndex &operator=(const QPersistentModelIndex &other);
120 inline QPersistentModelIndex(QPersistentModelIndex &&other) noexcept
121 : d(other.d) { other.d = nullptr; }
122 inline QPersistentModelIndex &operator=(QPersistentModelIndex &&other) noexcept
123 { qSwap(value1&: d, value2&: other.d); return *this; }
124 inline void swap(QPersistentModelIndex &other) noexcept { qSwap(value1&: d, value2&: other.d); }
125 bool operator==(const QModelIndex &other) const;
126 bool operator!=(const QModelIndex &other) const;
127 QPersistentModelIndex &operator=(const QModelIndex &other);
128 operator const QModelIndex&() const;
129 int row() const;
130 int column() const;
131 void *internalPointer() const;
132 quintptr internalId() const;
133 QModelIndex parent() const;
134 QModelIndex sibling(int row, int column) const;
135#if QT_DEPRECATED_SINCE(5, 8)
136 QT_DEPRECATED_X("Use QAbstractItemModel::index") QModelIndex child(int row, int column) const;
137#endif
138 QVariant data(int role = Qt::DisplayRole) const;
139 Qt::ItemFlags flags() const;
140 const QAbstractItemModel *model() const;
141 bool isValid() const;
142private:
143 QPersistentModelIndexData *d;
144 friend uint qHash(const QPersistentModelIndex &, uint seed) noexcept;
145#ifndef QT_NO_DEBUG_STREAM
146 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
147#endif
148};
149Q_DECLARE_SHARED(QPersistentModelIndex)
150
151inline uint qHash(const QPersistentModelIndex &index, uint seed) noexcept
152{ return qHash(t: index.d, seed); }
153
154
155#ifndef QT_NO_DEBUG_STREAM
156Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
157#endif
158
159template<typename T> class QList;
160typedef QList<QModelIndex> QModelIndexList;
161
162class QMimeData;
163class QAbstractItemModelPrivate;
164class QTransposeProxyModelPrivate;
165template <class Key, class T> class QMap;
166
167
168class Q_CORE_EXPORT QAbstractItemModel : public QObject
169{
170 Q_OBJECT
171
172 friend class QPersistentModelIndexData;
173 friend class QAbstractItemViewPrivate;
174 friend class QIdentityProxyModel;
175 friend class QTransposeProxyModelPrivate;
176public:
177
178 explicit QAbstractItemModel(QObject *parent = nullptr);
179 virtual ~QAbstractItemModel();
180
181 Q_INVOKABLE bool hasIndex(int row, int column, const QModelIndex &parent = QModelIndex()) const;
182 Q_INVOKABLE virtual QModelIndex index(int row, int column,
183 const QModelIndex &parent = QModelIndex()) const = 0;
184 Q_INVOKABLE virtual QModelIndex parent(const QModelIndex &child) const = 0;
185
186 Q_INVOKABLE virtual QModelIndex sibling(int row, int column, const QModelIndex &idx) const;
187 Q_INVOKABLE virtual int rowCount(const QModelIndex &parent = QModelIndex()) const = 0;
188 Q_INVOKABLE virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0;
189 Q_INVOKABLE virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
190
191 Q_INVOKABLE virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0;
192 Q_INVOKABLE virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
193
194 Q_INVOKABLE virtual QVariant headerData(int section, Qt::Orientation orientation,
195 int role = Qt::DisplayRole) const;
196 virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
197 int role = Qt::EditRole);
198
199 virtual QMap<int, QVariant> itemData(const QModelIndex &index) const;
200 virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
201#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
202 virtual bool clearItemData(const QModelIndex &index);
203#endif
204
205 virtual QStringList mimeTypes() const;
206 virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
207 virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action,
208 int row, int column, const QModelIndex &parent) const;
209 virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action,
210 int row, int column, const QModelIndex &parent);
211 virtual Qt::DropActions supportedDropActions() const;
212
213 virtual Qt::DropActions supportedDragActions() const;
214#if QT_DEPRECATED_SINCE(5, 0)
215 QT_DEPRECATED void setSupportedDragActions(Qt::DropActions actions)
216 { doSetSupportedDragActions(actions); }
217#endif
218
219 virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
220 virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex());
221 virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
222 virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex());
223 virtual bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
224 const QModelIndex &destinationParent, int destinationChild);
225 virtual bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count,
226 const QModelIndex &destinationParent, int destinationChild);
227
228 inline bool insertRow(int row, const QModelIndex &parent = QModelIndex());
229 inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex());
230 inline bool removeRow(int row, const QModelIndex &parent = QModelIndex());
231 inline bool removeColumn(int column, const QModelIndex &parent = QModelIndex());
232 inline bool moveRow(const QModelIndex &sourceParent, int sourceRow,
233 const QModelIndex &destinationParent, int destinationChild);
234 inline bool moveColumn(const QModelIndex &sourceParent, int sourceColumn,
235 const QModelIndex &destinationParent, int destinationChild);
236
237 Q_INVOKABLE virtual void fetchMore(const QModelIndex &parent);
238 Q_INVOKABLE virtual bool canFetchMore(const QModelIndex &parent) const;
239 Q_INVOKABLE virtual Qt::ItemFlags flags(const QModelIndex &index) const;
240 virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
241 virtual QModelIndex buddy(const QModelIndex &index) const;
242 Q_INVOKABLE virtual QModelIndexList match(const QModelIndex &start, int role,
243 const QVariant &value, int hits = 1,
244 Qt::MatchFlags flags =
245 Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const;
246 virtual QSize span(const QModelIndex &index) const;
247
248 virtual QHash<int,QByteArray> roleNames() const;
249
250 using QObject::parent;
251
252 enum LayoutChangeHint
253 {
254 NoLayoutChangeHint,
255 VerticalSortHint,
256 HorizontalSortHint
257 };
258 Q_ENUM(LayoutChangeHint)
259
260 enum class CheckIndexOption {
261 NoOption = 0x0000,
262 IndexIsValid = 0x0001,
263 DoNotUseParent = 0x0002,
264 ParentIsInvalid = 0x0004,
265 };
266 Q_ENUM(CheckIndexOption)
267 Q_DECLARE_FLAGS(CheckIndexOptions, CheckIndexOption)
268
269 Q_REQUIRED_RESULT bool checkIndex(const QModelIndex &index, CheckIndexOptions options = CheckIndexOption::NoOption) const;
270
271Q_SIGNALS:
272 void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
273 void headerDataChanged(Qt::Orientation orientation, int first, int last);
274 void layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
275 void layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
276
277 void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal);
278 void rowsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal);
279
280 void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal);
281 void rowsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal);
282
283 void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal);
284 void columnsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal);
285
286 void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal);
287 void columnsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal);
288
289 void modelAboutToBeReset(QPrivateSignal);
290 void modelReset(QPrivateSignal);
291
292 void rowsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow, QPrivateSignal);
293 void rowsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row, QPrivateSignal);
294
295 void columnsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn, QPrivateSignal);
296 void columnsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column, QPrivateSignal);
297
298public Q_SLOTS:
299 virtual bool submit();
300 virtual void revert();
301
302protected Q_SLOTS:
303#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
304 virtual
305#endif
306 void resetInternalData();
307
308protected:
309 QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = nullptr);
310
311 inline QModelIndex createIndex(int row, int column, void *data = nullptr) const;
312 inline QModelIndex createIndex(int row, int column, quintptr id) const;
313
314 void encodeData(const QModelIndexList &indexes, QDataStream &stream) const;
315 bool decodeData(int row, int column, const QModelIndex &parent, QDataStream &stream);
316
317 void beginInsertRows(const QModelIndex &parent, int first, int last);
318 void endInsertRows();
319
320 void beginRemoveRows(const QModelIndex &parent, int first, int last);
321 void endRemoveRows();
322
323 bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow);
324 void endMoveRows();
325
326 void beginInsertColumns(const QModelIndex &parent, int first, int last);
327 void endInsertColumns();
328
329 void beginRemoveColumns(const QModelIndex &parent, int first, int last);
330 void endRemoveColumns();
331
332 bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn);
333 void endMoveColumns();
334
335
336#if QT_DEPRECATED_SINCE(5,0)
337 QT_DEPRECATED void reset()
338 {
339 beginResetModel();
340 endResetModel();
341 }
342#endif
343
344 void beginResetModel();
345 void endResetModel();
346
347 void changePersistentIndex(const QModelIndex &from, const QModelIndex &to);
348 void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to);
349 QModelIndexList persistentIndexList() const;
350
351#if QT_DEPRECATED_SINCE(5,0)
352 QT_DEPRECATED void setRoleNames(const QHash<int,QByteArray> &theRoleNames)
353 {
354 doSetRoleNames(theRoleNames);
355 }
356#endif
357
358private:
359 void doSetRoleNames(const QHash<int,QByteArray> &roleNames);
360 void doSetSupportedDragActions(Qt::DropActions actions);
361
362 Q_DECLARE_PRIVATE(QAbstractItemModel)
363 Q_DISABLE_COPY(QAbstractItemModel)
364};
365
366Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractItemModel::CheckIndexOptions)
367
368inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
369{ return insertRows(row: arow, count: 1, parent: aparent); }
370inline bool QAbstractItemModel::insertColumn(int acolumn, const QModelIndex &aparent)
371{ return insertColumns(column: acolumn, count: 1, parent: aparent); }
372inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent)
373{ return removeRows(row: arow, count: 1, parent: aparent); }
374inline bool QAbstractItemModel::removeColumn(int acolumn, const QModelIndex &aparent)
375{ return removeColumns(column: acolumn, count: 1, parent: aparent); }
376inline bool QAbstractItemModel::moveRow(const QModelIndex &sourceParent, int sourceRow,
377 const QModelIndex &destinationParent, int destinationChild)
378{ return moveRows(sourceParent, sourceRow, count: 1, destinationParent, destinationChild); }
379inline bool QAbstractItemModel::moveColumn(const QModelIndex &sourceParent, int sourceColumn,
380 const QModelIndex &destinationParent, int destinationChild)
381{ return moveColumns(sourceParent, sourceColumn, count: 1, destinationParent, destinationChild); }
382inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const
383{ return QModelIndex(arow, acolumn, adata, this); }
384inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, quintptr aid) const
385{ return QModelIndex(arow, acolumn, aid, this); }
386
387class Q_CORE_EXPORT QAbstractTableModel : public QAbstractItemModel
388{
389 Q_OBJECT
390
391public:
392 explicit QAbstractTableModel(QObject *parent = nullptr);
393 ~QAbstractTableModel();
394
395 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
396 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
397 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
398 int row, int column, const QModelIndex &parent) override;
399
400 Qt::ItemFlags flags(const QModelIndex &index) const override;
401
402 using QObject::parent;
403
404protected:
405 QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent);
406
407private:
408 Q_DISABLE_COPY(QAbstractTableModel)
409 QModelIndex parent(const QModelIndex &child) const override;
410 bool hasChildren(const QModelIndex &parent) const override;
411};
412
413class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel
414{
415 Q_OBJECT
416
417public:
418 explicit QAbstractListModel(QObject *parent = nullptr);
419 ~QAbstractListModel();
420
421 QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
422 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
423 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
424 int row, int column, const QModelIndex &parent) override;
425
426 Qt::ItemFlags flags(const QModelIndex &index) const override;
427
428 using QObject::parent;
429
430protected:
431 QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent);
432
433private:
434 Q_DISABLE_COPY(QAbstractListModel)
435 QModelIndex parent(const QModelIndex &child) const override;
436 int columnCount(const QModelIndex &parent) const override;
437 bool hasChildren(const QModelIndex &parent) const override;
438};
439
440// inline implementations
441
442inline QModelIndex QModelIndex::parent() const
443{ return m ? m->parent(child: *this) : QModelIndex(); }
444
445inline QModelIndex QModelIndex::sibling(int arow, int acolumn) const
446{ return m ? (r == arow && c == acolumn) ? *this : m->sibling(row: arow, column: acolumn, idx: *this) : QModelIndex(); }
447
448inline QModelIndex QModelIndex::siblingAtColumn(int acolumn) const
449{ return m ? (c == acolumn) ? *this : m->sibling(row: r, column: acolumn, idx: *this) : QModelIndex(); }
450
451inline QModelIndex QModelIndex::siblingAtRow(int arow) const
452{ return m ? (r == arow) ? *this : m->sibling(row: arow, column: c, idx: *this) : QModelIndex(); }
453
454#if QT_DEPRECATED_SINCE(5, 8)
455inline QModelIndex QModelIndex::child(int arow, int acolumn) const
456{ return m ? m->index(row: arow, column: acolumn, parent: *this) : QModelIndex(); }
457#endif
458
459inline QVariant QModelIndex::data(int arole) const
460{ return m ? m->data(index: *this, role: arole) : QVariant(); }
461
462inline Qt::ItemFlags QModelIndex::flags() const
463{ return m ? m->flags(index: *this) : Qt::ItemFlags(); }
464
465inline uint qHash(const QModelIndex &index) noexcept
466{ return uint((uint(index.row()) << 4) + index.column() + index.internalId()); }
467
468QT_END_NAMESPACE
469
470Q_DECLARE_METATYPE(QModelIndexList)
471
472#endif // QABSTRACTITEMMODEL_H
473

source code of qtbase/src/corelib/itemmodels/qabstractitemmodel.h