1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QABSTRACTITEMMODEL_H
43#define QABSTRACTITEMMODEL_H
44
45#include <QtCore/qvariant.h>
46#include <QtCore/qobject.h>
47#include <QtCore/qhash.h>
48
49QT_BEGIN_HEADER
50
51QT_BEGIN_NAMESPACE
52
53QT_MODULE(Core)
54
55class QAbstractItemModel;
56class QPersistentModelIndex;
57
58class Q_CORE_EXPORT QModelIndex
59{
60 friend class QAbstractItemModel;
61 friend class QProxyModel;
62public:
63 inline QModelIndex() : r(-1), c(-1), p(0), m(0) {}
64 inline QModelIndex(const QModelIndex &other)
65 : r(other.r), c(other.c), p(other.p), m(other.m) {}
66 inline ~QModelIndex() { p = 0; m = 0; }
67 inline int row() const { return r; }
68 inline int column() const { return c; }
69 inline void *internalPointer() const { return p; }
70 inline qint64 internalId() const { return reinterpret_cast<qint64>(p); }
71 inline QModelIndex parent() const;
72 inline QModelIndex sibling(int row, int column) const;
73 inline QModelIndex child(int row, int column) const;
74 inline QVariant data(int role = Qt::DisplayRole) const;
75 inline Qt::ItemFlags flags() const;
76 inline const QAbstractItemModel *model() const { return m; }
77 inline bool isValid() const { return (r >= 0) && (c >= 0) && (m != 0); }
78 inline bool operator==(const QModelIndex &other) const
79 { return (other.r == r) && (other.p == p) && (other.c == c) && (other.m == m); }
80 inline bool operator!=(const QModelIndex &other) const
81 { return !(*this == other); }
82 inline bool operator<(const QModelIndex &other) const
83 {
84 if (r < other.r) return true;
85 if (r == other.r) {
86 if (c < other.c) return true;
87 if (c == other.c) {
88 if (p < other.p) return true;
89 if (p == other.p) return m < other.m;
90 }
91 }
92 return false; }
93private:
94 inline QModelIndex(int row, int column, void *ptr, const QAbstractItemModel *model);
95 int r, c;
96 void *p;
97 const QAbstractItemModel *m;
98};
99Q_DECLARE_TYPEINFO(QModelIndex, Q_MOVABLE_TYPE);
100
101#ifndef QT_NO_DEBUG_STREAM
102Q_CORE_EXPORT QDebug operator<<(QDebug, const QModelIndex &);
103#endif
104
105class QPersistentModelIndexData;
106
107class Q_CORE_EXPORT QPersistentModelIndex
108{
109public:
110 QPersistentModelIndex();
111 QPersistentModelIndex(const QModelIndex &index);
112 QPersistentModelIndex(const QPersistentModelIndex &other);
113 ~QPersistentModelIndex();
114 bool operator<(const QPersistentModelIndex &other) const;
115 bool operator==(const QPersistentModelIndex &other) const;
116 inline bool operator!=(const QPersistentModelIndex &other) const
117 { return !operator==(other); }
118 QPersistentModelIndex &operator=(const QPersistentModelIndex &other);
119 bool operator==(const QModelIndex &other) const;
120 bool operator!=(const QModelIndex &other) const;
121 QPersistentModelIndex &operator=(const QModelIndex &other);
122 operator const QModelIndex&() const;
123 int row() const;
124 int column() const;
125 void *internalPointer() const;
126 qint64 internalId() const;
127 QModelIndex parent() const;
128 QModelIndex sibling(int row, int column) const;
129 QModelIndex child(int row, int column) const;
130 QVariant data(int role = Qt::DisplayRole) const;
131 Qt::ItemFlags flags() const;
132 const QAbstractItemModel *model() const;
133 bool isValid() const;
134private:
135 QPersistentModelIndexData *d;
136 friend uint qHash(const QPersistentModelIndex &);
137#ifndef QT_NO_DEBUG_STREAM
138 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
139#endif
140};
141Q_DECLARE_TYPEINFO(QPersistentModelIndex, Q_MOVABLE_TYPE);
142
143inline uint qHash(const QPersistentModelIndex &index)
144{ return qHash(index.d); }
145
146
147#ifndef QT_NO_DEBUG_STREAM
148Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
149#endif
150
151template<typename T> class QList;
152typedef QList<QModelIndex> QModelIndexList;
153
154class QMimeData;
155class QAbstractItemModelPrivate;
156template <class Key, class T> class QMap;
157
158
159class Q_CORE_EXPORT QAbstractItemModel : public QObject
160{
161 Q_OBJECT
162
163 friend class QPersistentModelIndexData;
164 friend class QAbstractItemViewPrivate;
165 friend class QIdentityProxyModel;
166public:
167
168 explicit QAbstractItemModel(QObject *parent = 0);
169 virtual ~QAbstractItemModel();
170
171 bool hasIndex(int row, int column, const QModelIndex &parent = QModelIndex()) const;
172 virtual QModelIndex index(int row, int column,
173 const QModelIndex &parent = QModelIndex()) const = 0;
174 virtual QModelIndex parent(const QModelIndex &child) const = 0;
175
176 inline QModelIndex sibling(int row, int column, const QModelIndex &idx) const
177 { return index(row, column, parent(idx)); }
178
179 virtual int rowCount(const QModelIndex &parent = QModelIndex()) const = 0;
180 virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0;
181 virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
182
183 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0;
184 virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
185
186 virtual QVariant headerData(int section, Qt::Orientation orientation,
187 int role = Qt::DisplayRole) const;
188 virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
189 int role = Qt::EditRole);
190
191 virtual QMap<int, QVariant> itemData(const QModelIndex &index) const;
192 virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
193
194 virtual QStringList mimeTypes() const;
195 virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
196 virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action,
197 int row, int column, const QModelIndex &parent);
198 virtual Qt::DropActions supportedDropActions() const;
199
200 Qt::DropActions supportedDragActions() const;
201 void setSupportedDragActions(Qt::DropActions);
202
203 virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
204 virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex());
205 virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
206 virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex());
207
208 inline bool insertRow(int row, const QModelIndex &parent = QModelIndex());
209 inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex());
210 inline bool removeRow(int row, const QModelIndex &parent = QModelIndex());
211 inline bool removeColumn(int column, const QModelIndex &parent = QModelIndex());
212
213 virtual void fetchMore(const QModelIndex &parent);
214 virtual bool canFetchMore(const QModelIndex &parent) const;
215 virtual Qt::ItemFlags flags(const QModelIndex &index) const;
216 virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
217 virtual QModelIndex buddy(const QModelIndex &index) const;
218 virtual QModelIndexList match(const QModelIndex &start, int role,
219 const QVariant &value, int hits = 1,
220 Qt::MatchFlags flags =
221 Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const;
222 virtual QSize span(const QModelIndex &index) const;
223
224 const QHash<int,QByteArray> &roleNames() const;
225
226#ifdef Q_NO_USING_KEYWORD
227 inline QObject *parent() const { return QObject::parent(); }
228#else
229 using QObject::parent;
230#endif
231
232Q_SIGNALS:
233 void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
234 void headerDataChanged(Qt::Orientation orientation, int first, int last);
235 void layoutChanged();
236 void layoutAboutToBeChanged();
237
238#if !defined(Q_MOC_RUN) && !defined(qdoc)
239private: // can only be emitted by QAbstractItemModel
240#endif
241 void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last);
242 void rowsInserted(const QModelIndex &parent, int first, int last);
243
244 void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
245 void rowsRemoved(const QModelIndex &parent, int first, int last);
246
247 void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last);
248 void columnsInserted(const QModelIndex &parent, int first, int last);
249
250 void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
251 void columnsRemoved(const QModelIndex &parent, int first, int last);
252
253 void modelAboutToBeReset();
254 void modelReset();
255
256 void rowsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow );
257 void rowsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row );
258
259 void columnsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn );
260 void columnsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column );
261
262
263public Q_SLOTS:
264 virtual bool submit();
265 virtual void revert();
266
267protected:
268 QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = 0);
269
270 inline QModelIndex createIndex(int row, int column, void *data = 0) const;
271 inline QModelIndex createIndex(int row, int column, int id) const;
272 inline QModelIndex createIndex(int row, int column, quint32 id) const;
273
274 void encodeData(const QModelIndexList &indexes, QDataStream &stream) const;
275 bool decodeData(int row, int column, const QModelIndex &parent, QDataStream &stream);
276
277 void beginInsertRows(const QModelIndex &parent, int first, int last);
278 void endInsertRows();
279
280 void beginRemoveRows(const QModelIndex &parent, int first, int last);
281 void endRemoveRows();
282
283 bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow);
284 void endMoveRows();
285
286 void beginInsertColumns(const QModelIndex &parent, int first, int last);
287 void endInsertColumns();
288
289 void beginRemoveColumns(const QModelIndex &parent, int first, int last);
290 void endRemoveColumns();
291
292 bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn);
293 void endMoveColumns();
294
295 void reset();
296
297 void beginResetModel();
298 void endResetModel();
299
300 void changePersistentIndex(const QModelIndex &from, const QModelIndex &to);
301 void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to);
302 QModelIndexList persistentIndexList() const;
303
304 void setRoleNames(const QHash<int,QByteArray> &roleNames);
305
306protected Q_SLOTS:
307 void resetInternalData();
308
309private:
310 Q_DECLARE_PRIVATE(QAbstractItemModel)
311 Q_DISABLE_COPY(QAbstractItemModel)
312};
313
314inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
315{ return insertRows(arow, 1, aparent); }
316inline bool QAbstractItemModel::insertColumn(int acolumn, const QModelIndex &aparent)
317{ return insertColumns(acolumn, 1, aparent); }
318inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent)
319{ return removeRows(arow, 1, aparent); }
320inline bool QAbstractItemModel::removeColumn(int acolumn, const QModelIndex &aparent)
321{ return removeColumns(acolumn, 1, aparent); }
322
323inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const
324{ return QModelIndex(arow, acolumn, adata, this); }
325inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, int aid) const
326#if defined(Q_CC_MSVC)
327#pragma warning( push )
328#pragma warning( disable : 4312 ) // avoid conversion warning on 64-bit
329#endif
330{ return QModelIndex(arow, acolumn, reinterpret_cast<void*>(aid), this); }
331#if defined(Q_CC_MSVC)
332#pragma warning( pop )
333#endif
334inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, quint32 aid) const
335#if defined(Q_CC_MSVC)
336#pragma warning( push )
337#pragma warning( disable : 4312 ) // avoid conversion warning on 64-bit
338#endif
339{ return QModelIndex(arow, acolumn, reinterpret_cast<void*>(aid), this); }
340#if defined(Q_CC_MSVC)
341#pragma warning( pop )
342#endif
343
344
345class Q_CORE_EXPORT QAbstractTableModel : public QAbstractItemModel
346{
347 Q_OBJECT
348
349public:
350 explicit QAbstractTableModel(QObject *parent = 0);
351 ~QAbstractTableModel();
352
353 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
354 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
355 int row, int column, const QModelIndex &parent);
356protected:
357 QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent);
358
359private:
360 Q_DISABLE_COPY(QAbstractTableModel)
361 QModelIndex parent(const QModelIndex &child) const;
362 bool hasChildren(const QModelIndex &parent) const;
363};
364
365class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel
366{
367 Q_OBJECT
368
369public:
370 explicit QAbstractListModel(QObject *parent = 0);
371 ~QAbstractListModel();
372
373 QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
374 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
375 int row, int column, const QModelIndex &parent);
376protected:
377 QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent);
378
379private:
380 Q_DISABLE_COPY(QAbstractListModel)
381 QModelIndex parent(const QModelIndex &child) const;
382 int columnCount(const QModelIndex &parent) const;
383 bool hasChildren(const QModelIndex &parent) const;
384};
385
386// inline implementations
387
388inline QModelIndex::QModelIndex(int arow, int acolumn, void *adata,
389 const QAbstractItemModel *amodel)
390 : r(arow), c(acolumn), p(adata), m(amodel) {}
391
392inline QModelIndex QModelIndex::parent() const
393{ return m ? m->parent(*this) : QModelIndex(); }
394
395inline QModelIndex QModelIndex::sibling(int arow, int acolumn) const
396{ return m ? (r == arow && c == acolumn) ? *this : m->index(arow, acolumn, m->parent(*this)) : QModelIndex(); }
397
398inline QModelIndex QModelIndex::child(int arow, int acolumn) const
399{ return m ? m->index(arow, acolumn, *this) : QModelIndex(); }
400
401inline QVariant QModelIndex::data(int arole) const
402{ return m ? m->data(*this, arole) : QVariant(); }
403
404inline Qt::ItemFlags QModelIndex::flags() const
405{ return m ? m->flags(*this) : Qt::ItemFlags(0); }
406
407inline uint qHash(const QModelIndex &index)
408{ return uint((index.row() << 4) + index.column() + index.internalId()); }
409
410QT_END_NAMESPACE
411
412QT_END_HEADER
413
414#endif // QABSTRACTITEMMODEL_H
415