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() Q_DECL_NOTHROW : 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 Q_DECL_NOTHROW { return r; }
63 Q_DECL_CONSTEXPR inline int column() const Q_DECL_NOTHROW { return c; }
64 Q_DECL_CONSTEXPR inline quintptr internalId() const Q_DECL_NOTHROW { return i; }
65 inline void *internalPointer() const Q_DECL_NOTHROW { 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 Q_DECL_NOTHROW { return m; }
76 Q_DECL_CONSTEXPR inline bool isValid() const Q_DECL_NOTHROW { return (r >= 0) && (c >= 0) && (m != nullptr); }
77 Q_DECL_CONSTEXPR inline bool operator==(const QModelIndex &other) const Q_DECL_NOTHROW
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 Q_DECL_NOTHROW
80 { return !(*this == other); }
81 Q_DECL_CONSTEXPR inline bool operator<(const QModelIndex &other) const Q_DECL_NOTHROW
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) Q_DECL_NOTHROW
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) Q_DECL_NOTHROW
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) Q_DECL_NOTHROW;
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#ifdef Q_COMPILER_RVALUE_REFS
121 inline QPersistentModelIndex(QPersistentModelIndex &&other) Q_DECL_NOTHROW
122 : d(other.d) { other.d = nullptr; }
123 inline QPersistentModelIndex &operator=(QPersistentModelIndex &&other) Q_DECL_NOTHROW
124 { qSwap(d, other.d); return *this; }
125#endif
126 inline void swap(QPersistentModelIndex &other) Q_DECL_NOTHROW { qSwap(d, other.d); }
127 bool operator==(const QModelIndex &other) const;
128 bool operator!=(const QModelIndex &other) const;
129 QPersistentModelIndex &operator=(const QModelIndex &other);
130 operator const QModelIndex&() const;
131 int row() const;
132 int column() const;
133 void *internalPointer() const;
134 quintptr internalId() const;
135 QModelIndex parent() const;
136 QModelIndex sibling(int row, int column) const;
137#if QT_DEPRECATED_SINCE(5, 8)
138 QT_DEPRECATED_X("Use QAbstractItemModel::index") QModelIndex child(int row, int column) const;
139#endif
140 QVariant data(int role = Qt::DisplayRole) const;
141 Qt::ItemFlags flags() const;
142 const QAbstractItemModel *model() const;
143 bool isValid() const;
144private:
145 QPersistentModelIndexData *d;
146 friend uint qHash(const QPersistentModelIndex &, uint seed) Q_DECL_NOTHROW;
147#ifndef QT_NO_DEBUG_STREAM
148 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
149#endif
150};
151Q_DECLARE_SHARED(QPersistentModelIndex)
152
153inline uint qHash(const QPersistentModelIndex &index, uint seed) Q_DECL_NOTHROW
154{ return qHash(index.d, seed); }
155
156
157#ifndef QT_NO_DEBUG_STREAM
158Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
159#endif
160
161template<typename T> class QList;
162typedef QList<QModelIndex> QModelIndexList;
163
164class QMimeData;
165class QAbstractItemModelPrivate;
166template <class Key, class T> class QMap;
167
168
169class Q_CORE_EXPORT QAbstractItemModel : public QObject
170{
171 Q_OBJECT
172
173 friend class QPersistentModelIndexData;
174 friend class QAbstractItemViewPrivate;
175 friend class QIdentityProxyModel;
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
202 virtual QStringList mimeTypes() const;
203 virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
204 virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action,
205 int row, int column, const QModelIndex &parent) const;
206 virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action,
207 int row, int column, const QModelIndex &parent);
208 virtual Qt::DropActions supportedDropActions() const;
209
210 virtual Qt::DropActions supportedDragActions() const;
211#if QT_DEPRECATED_SINCE(5, 0)
212 QT_DEPRECATED void setSupportedDragActions(Qt::DropActions actions)
213 { doSetSupportedDragActions(actions); }
214#endif
215
216 virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
217 virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex());
218 virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
219 virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex());
220 virtual bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
221 const QModelIndex &destinationParent, int destinationChild);
222 virtual bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count,
223 const QModelIndex &destinationParent, int destinationChild);
224
225 inline bool insertRow(int row, const QModelIndex &parent = QModelIndex());
226 inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex());
227 inline bool removeRow(int row, const QModelIndex &parent = QModelIndex());
228 inline bool removeColumn(int column, const QModelIndex &parent = QModelIndex());
229 inline bool moveRow(const QModelIndex &sourceParent, int sourceRow,
230 const QModelIndex &destinationParent, int destinationChild);
231 inline bool moveColumn(const QModelIndex &sourceParent, int sourceColumn,
232 const QModelIndex &destinationParent, int destinationChild);
233
234 Q_INVOKABLE virtual void fetchMore(const QModelIndex &parent);
235 Q_INVOKABLE virtual bool canFetchMore(const QModelIndex &parent) const;
236 Q_INVOKABLE virtual Qt::ItemFlags flags(const QModelIndex &index) const;
237 virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
238 virtual QModelIndex buddy(const QModelIndex &index) const;
239 Q_INVOKABLE virtual QModelIndexList match(const QModelIndex &start, int role,
240 const QVariant &value, int hits = 1,
241 Qt::MatchFlags flags =
242 Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const;
243 virtual QSize span(const QModelIndex &index) const;
244
245 virtual QHash<int,QByteArray> roleNames() const;
246
247 using QObject::parent;
248
249 enum LayoutChangeHint
250 {
251 NoLayoutChangeHint,
252 VerticalSortHint,
253 HorizontalSortHint
254 };
255 Q_ENUM(LayoutChangeHint)
256
257 enum class CheckIndexOption {
258 NoOption = 0x0000,
259 IndexIsValid = 0x0001,
260 DoNotUseParent = 0x0002,
261 ParentIsInvalid = 0x0004,
262 };
263 Q_ENUM(CheckIndexOption)
264 Q_DECLARE_FLAGS(CheckIndexOptions, CheckIndexOption)
265
266 Q_REQUIRED_RESULT bool checkIndex(const QModelIndex &index, CheckIndexOptions options = CheckIndexOption::NoOption) const;
267
268Q_SIGNALS:
269 void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
270 void headerDataChanged(Qt::Orientation orientation, int first, int last);
271 void layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
272 void layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
273
274 void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal);
275 void rowsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal);
276
277 void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal);
278 void rowsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal);
279
280 void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal);
281 void columnsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal);
282
283 void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal);
284 void columnsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal);
285
286 void modelAboutToBeReset(QPrivateSignal);
287 void modelReset(QPrivateSignal);
288
289 void rowsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow, QPrivateSignal);
290 void rowsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row, QPrivateSignal);
291
292 void columnsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn, QPrivateSignal);
293 void columnsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column, QPrivateSignal);
294
295public Q_SLOTS:
296 virtual bool submit();
297 virtual void revert();
298
299protected Q_SLOTS:
300 // Qt 6: Make virtual
301 void resetInternalData();
302
303protected:
304 QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = nullptr);
305
306 inline QModelIndex createIndex(int row, int column, void *data = nullptr) const;
307 inline QModelIndex createIndex(int row, int column, quintptr id) const;
308
309 void encodeData(const QModelIndexList &indexes, QDataStream &stream) const;
310 bool decodeData(int row, int column, const QModelIndex &parent, QDataStream &stream);
311
312 void beginInsertRows(const QModelIndex &parent, int first, int last);
313 void endInsertRows();
314
315 void beginRemoveRows(const QModelIndex &parent, int first, int last);
316 void endRemoveRows();
317
318 bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow);
319 void endMoveRows();
320
321 void beginInsertColumns(const QModelIndex &parent, int first, int last);
322 void endInsertColumns();
323
324 void beginRemoveColumns(const QModelIndex &parent, int first, int last);
325 void endRemoveColumns();
326
327 bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn);
328 void endMoveColumns();
329
330
331#if QT_DEPRECATED_SINCE(5,0)
332 QT_DEPRECATED void reset()
333 {
334 beginResetModel();
335 endResetModel();
336 }
337#endif
338
339 void beginResetModel();
340 void endResetModel();
341
342 void changePersistentIndex(const QModelIndex &from, const QModelIndex &to);
343 void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to);
344 QModelIndexList persistentIndexList() const;
345
346#if QT_DEPRECATED_SINCE(5,0)
347 QT_DEPRECATED void setRoleNames(const QHash<int,QByteArray> &theRoleNames)
348 {
349 doSetRoleNames(theRoleNames);
350 }
351#endif
352
353private:
354 void doSetRoleNames(const QHash<int,QByteArray> &roleNames);
355 void doSetSupportedDragActions(Qt::DropActions actions);
356
357 Q_DECLARE_PRIVATE(QAbstractItemModel)
358 Q_DISABLE_COPY(QAbstractItemModel)
359};
360
361Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractItemModel::CheckIndexOptions)
362
363inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
364{ return insertRows(arow, 1, aparent); }
365inline bool QAbstractItemModel::insertColumn(int acolumn, const QModelIndex &aparent)
366{ return insertColumns(acolumn, 1, aparent); }
367inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent)
368{ return removeRows(arow, 1, aparent); }
369inline bool QAbstractItemModel::removeColumn(int acolumn, const QModelIndex &aparent)
370{ return removeColumns(acolumn, 1, aparent); }
371inline bool QAbstractItemModel::moveRow(const QModelIndex &sourceParent, int sourceRow,
372 const QModelIndex &destinationParent, int destinationChild)
373{ return moveRows(sourceParent, sourceRow, 1, destinationParent, destinationChild); }
374inline bool QAbstractItemModel::moveColumn(const QModelIndex &sourceParent, int sourceColumn,
375 const QModelIndex &destinationParent, int destinationChild)
376{ return moveColumns(sourceParent, sourceColumn, 1, destinationParent, destinationChild); }
377inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const
378{ return QModelIndex(arow, acolumn, adata, this); }
379inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, quintptr aid) const
380{ return QModelIndex(arow, acolumn, aid, this); }
381
382class Q_CORE_EXPORT QAbstractTableModel : public QAbstractItemModel
383{
384 Q_OBJECT
385
386public:
387 explicit QAbstractTableModel(QObject *parent = nullptr);
388 ~QAbstractTableModel();
389
390 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
391 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
392 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
393 int row, int column, const QModelIndex &parent) override;
394
395 Qt::ItemFlags flags(const QModelIndex &index) const override;
396
397 using QObject::parent;
398
399protected:
400 QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent);
401
402private:
403 Q_DISABLE_COPY(QAbstractTableModel)
404 QModelIndex parent(const QModelIndex &child) const override;
405 bool hasChildren(const QModelIndex &parent) const override;
406};
407
408class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel
409{
410 Q_OBJECT
411
412public:
413 explicit QAbstractListModel(QObject *parent = nullptr);
414 ~QAbstractListModel();
415
416 QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
417 QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
418 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
419 int row, int column, const QModelIndex &parent) override;
420
421 Qt::ItemFlags flags(const QModelIndex &index) const override;
422
423 using QObject::parent;
424
425protected:
426 QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent);
427
428private:
429 Q_DISABLE_COPY(QAbstractListModel)
430 QModelIndex parent(const QModelIndex &child) const override;
431 int columnCount(const QModelIndex &parent) const override;
432 bool hasChildren(const QModelIndex &parent) const override;
433};
434
435// inline implementations
436
437inline QModelIndex QModelIndex::parent() const
438{ return m ? m->parent(*this) : QModelIndex(); }
439
440inline QModelIndex QModelIndex::sibling(int arow, int acolumn) const
441{ return m ? (r == arow && c == acolumn) ? *this : m->sibling(arow, acolumn, *this) : QModelIndex(); }
442
443inline QModelIndex QModelIndex::siblingAtColumn(int acolumn) const
444{ return m ? (c == acolumn) ? *this : m->sibling(r, acolumn, *this) : QModelIndex(); }
445
446inline QModelIndex QModelIndex::siblingAtRow(int arow) const
447{ return m ? (r == arow) ? *this : m->sibling(arow, c, *this) : QModelIndex(); }
448
449#if QT_DEPRECATED_SINCE(5, 8)
450inline QModelIndex QModelIndex::child(int arow, int acolumn) const
451{ return m ? m->index(arow, acolumn, *this) : QModelIndex(); }
452#endif
453
454inline QVariant QModelIndex::data(int arole) const
455{ return m ? m->data(*this, arole) : QVariant(); }
456
457inline Qt::ItemFlags QModelIndex::flags() const
458{ return m ? m->flags(*this) : Qt::ItemFlags(); }
459
460inline uint qHash(const QModelIndex &index) Q_DECL_NOTHROW
461{ return uint((uint(index.row()) << 4) + index.column() + index.internalId()); }
462
463QT_END_NAMESPACE
464
465Q_DECLARE_METATYPE(QModelIndexList)
466
467#endif // QABSTRACTITEMMODEL_H
468