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