e5fcad3 by Lars Knoll at 2009-03-23 1
/****************************************************************************
2
**
89c08c0 by Jason McDonald at 2012-01-11 3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
04e3b30 by Jason McDonald at 2009-09-09 4
** All rights reserved.
858c70f by Jason McDonald at 2009-06-16 5
** Contact: Nokia Corporation (qt-info@nokia.com)
e5fcad3 by Lars Knoll at 2009-03-23 6
**
7
** This file is part of the QtCore module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
1eea52e by Jyri Tahtela at 2011-05-13 11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
e5fcad3 by Lars Knoll at 2009-03-23 17
**
04e3b30 by Jason McDonald at 2009-09-09 18
** In addition, as a special exception, Nokia gives you certain additional
1eea52e by Jyri Tahtela at 2011-05-13 19
** rights. These rights are described in the Nokia Qt LGPL Exception
04e3b30 by Jason McDonald at 2009-09-09 20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
e5fcad3 by Lars Knoll at 2009-03-23 21
**
1eea52e by Jyri Tahtela at 2011-05-13 22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
309db73 by Jason McDonald at 2009-08-31 33
**
34
**
35
**
36
**
e5fcad3 by Lars Knoll at 2009-03-23 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
49
QT_BEGIN_HEADER
50
51
QT_BEGIN_NAMESPACE
52
53
QT_MODULE(Core)
54
55
class QAbstractItemModel;
56
class QPersistentModelIndex;
57
58
class Q_CORE_EXPORT QModelIndex
59
{
60
    friend class QAbstractItemModel;
61
    friend class QProxyModel;
62
public:
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; }
93
private:
94
    inline QModelIndex(int row, int column, void *ptr, const QAbstractItemModel *model);
95
    int r, c;
96
    void *p;
97
    const QAbstractItemModel *m;
98
};
99
Q_DECLARE_TYPEINFO(QModelIndex, Q_MOVABLE_TYPE);
100
101
#ifndef QT_NO_DEBUG_STREAM
102
Q_CORE_EXPORT QDebug operator<<(QDebug, const QModelIndex &);
103
#endif
104
105
class QPersistentModelIndexData;
106
107
class Q_CORE_EXPORT QPersistentModelIndex
108
{
109
public:
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;
134
private:
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
};
141
Q_DECLARE_TYPEINFO(QPersistentModelIndex, Q_MOVABLE_TYPE);
142
143
inline uint qHash(const QPersistentModelIndex &index)
144
{ return qHash(index.d); }
145
146
147
#ifndef QT_NO_DEBUG_STREAM
148
Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
149
#endif
150
151
template<typename T> class QList;
152
typedef QList<QModelIndex> QModelIndexList;
153
154
class QMimeData;
155
class QAbstractItemModelPrivate;
156
template <class Key, class T> class QMap;
157
158
159
class Q_CORE_EXPORT QAbstractItemModel : public QObject
160
{
161
    Q_OBJECT
162
163
    friend class QPersistentModelIndexData;
164
    friend class QAbstractItemViewPrivate;
b000892 by Stephen Kelly at 2011-04-12 165
    friend class QIdentityProxyModel;
e5fcad3 by Lars Knoll at 2009-03-23 166
public:
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
c257996 by Aaron Kennedy at 2009-07-31 224
    const QHash<int,QByteArray> &roleNames() const;
225
e5fcad3 by Lars Knoll at 2009-03-23 226
#ifdef Q_NO_USING_KEYWORD
227
    inline QObject *parent() const { return QObject::parent(); }
228
#else
229
    using QObject::parent;
230
#endif
231
232
Q_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)
239
private: // 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
fad842c by Stephen Kelly at 2009-08-26 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
e5fcad3 by Lars Knoll at 2009-03-23 263
public Q_SLOTS:
264
    virtual bool submit();
265
    virtual void revert();
266
267
protected:
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
fad842c by Stephen Kelly at 2009-08-26 283
    bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow);
284
    void endMoveRows();
285
e5fcad3 by Lars Knoll at 2009-03-23 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
fad842c by Stephen Kelly at 2009-08-26 292
    bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn);
293
    void endMoveColumns();
294
e5fcad3 by Lars Knoll at 2009-03-23 295
    void reset();
296
4d197ec by Stephen Kelly at 2009-08-28 297
    void beginResetModel();
298
    void endResetModel();
299
e5fcad3 by Lars Knoll at 2009-03-23 300
    void changePersistentIndex(const QModelIndex &from, const QModelIndex &to);
301
    void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to);
302
    QModelIndexList persistentIndexList() const;
303
c257996 by Aaron Kennedy at 2009-07-31 304
    void setRoleNames(const QHash<int,QByteArray> &roleNames);
305
f1f3799 by Frederik Gladhorn at 2011-06-28 306
protected Q_SLOTS:
0f6853e by Stephen Kelly at 2011-06-27 307
    void resetInternalData();
308
e5fcad3 by Lars Knoll at 2009-03-23 309
private:
310
    Q_DECLARE_PRIVATE(QAbstractItemModel)
311
    Q_DISABLE_COPY(QAbstractItemModel)
312
};
313
314
inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
315
{ return insertRows(arow, 1, aparent); }
316
inline bool QAbstractItemModel::insertColumn(int acolumn, const QModelIndex &aparent)
317
{ return insertColumns(acolumn, 1, aparent); }
318
inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent)
319
{ return removeRows(arow, 1, aparent); }
320
inline bool QAbstractItemModel::removeColumn(int acolumn, const QModelIndex &aparent)
321
{ return removeColumns(acolumn, 1, aparent); }
322
323
inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const
324
{ return QModelIndex(arow, acolumn, adata, this); }
325
inline 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
334
inline 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
345
class Q_CORE_EXPORT QAbstractTableModel : public QAbstractItemModel
346
{
347
    Q_OBJECT
348
349
public:
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);
356
protected:
357
    QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent);
358
359
private:
360
    Q_DISABLE_COPY(QAbstractTableModel)
361
    QModelIndex parent(const QModelIndex &child) const;
362
    bool hasChildren(const QModelIndex &parent) const;
363
};
364
365
class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel
366
{
367
    Q_OBJECT
368
369
public:
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);
376
protected:
377
    QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent);
378
379
private:
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
388
inline QModelIndex::QModelIndex(int arow, int acolumn, void *adata,
389
                                const QAbstractItemModel *amodel)
390
    : r(arow), c(acolumn), p(adata), m(amodel) {}
391
392
inline QModelIndex QModelIndex::parent() const
393
{ return m ? m->parent(*this) : QModelIndex(); }
394
395
inline 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
398
inline QModelIndex QModelIndex::child(int arow, int acolumn) const
399
{ return m ? m->index(arow, acolumn, *this) : QModelIndex(); }
400
401
inline QVariant QModelIndex::data(int arole) const
402
{ return m ? m->data(*this, arole) : QVariant(); }
403
404
inline Qt::ItemFlags QModelIndex::flags() const
405
{ return m ? m->flags(*this) : Qt::ItemFlags(0); }
406
407
inline uint qHash(const QModelIndex &index)
408
{ return uint((index.row() << 4) + index.column() + index.internalId()); }
409
410
QT_END_NAMESPACE
411
412
QT_END_HEADER
413
414
#endif // QABSTRACTITEMMODEL_H