|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtCore module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
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 |
|
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; |
|
165 public: |
|
166 |
|
167 explicit QAbstractItemModel(QObject *parent = 0); |
|
168 virtual ~QAbstractItemModel(); |
|
169 |
|
170 bool hasIndex(int row, int column, const QModelIndex &parent = QModelIndex()) const; |
|
171 virtual QModelIndex index(int row, int column, |
|
172 const QModelIndex &parent = QModelIndex()) const = 0; |
|
173 virtual QModelIndex parent(const QModelIndex &child) const = 0; |
|
174 |
|
175 inline QModelIndex sibling(int row, int column, const QModelIndex &idx) const |
|
176 { return index(row, column, parent(idx)); } |
|
177 |
|
178 virtual int rowCount(const QModelIndex &parent = QModelIndex()) const = 0; |
|
179 virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0; |
|
180 virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const; |
|
181 |
|
182 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0; |
|
183 virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); |
|
184 |
|
185 virtual QVariant headerData(int section, Qt::Orientation orientation, |
|
186 int role = Qt::DisplayRole) const; |
|
187 virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, |
|
188 int role = Qt::EditRole); |
|
189 |
|
190 virtual QMap<int, QVariant> itemData(const QModelIndex &index) const; |
|
191 virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles); |
|
192 |
|
193 virtual QStringList mimeTypes() const; |
|
194 virtual QMimeData *mimeData(const QModelIndexList &indexes) const; |
|
195 virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
|
196 int row, int column, const QModelIndex &parent); |
|
197 virtual Qt::DropActions supportedDropActions() const; |
|
198 |
|
199 Qt::DropActions supportedDragActions() const; |
|
200 void setSupportedDragActions(Qt::DropActions); |
|
201 |
|
202 virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()); |
|
203 virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()); |
|
204 virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); |
|
205 virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()); |
|
206 |
|
207 inline bool insertRow(int row, const QModelIndex &parent = QModelIndex()); |
|
208 inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex()); |
|
209 inline bool removeRow(int row, const QModelIndex &parent = QModelIndex()); |
|
210 inline bool removeColumn(int column, const QModelIndex &parent = QModelIndex()); |
|
211 |
|
212 virtual void fetchMore(const QModelIndex &parent); |
|
213 virtual bool canFetchMore(const QModelIndex &parent) const; |
|
214 virtual Qt::ItemFlags flags(const QModelIndex &index) const; |
|
215 virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); |
|
216 virtual QModelIndex buddy(const QModelIndex &index) const; |
|
217 virtual QModelIndexList match(const QModelIndex &start, int role, |
|
218 const QVariant &value, int hits = 1, |
|
219 Qt::MatchFlags flags = |
|
220 Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const; |
|
221 virtual QSize span(const QModelIndex &index) const; |
|
222 |
|
223 const QHash<int,QByteArray> &roleNames() const; |
|
224 |
|
225 #ifdef Q_NO_USING_KEYWORD |
|
226 inline QObject *parent() const { return QObject::parent(); } |
|
227 #else |
|
228 using QObject::parent; |
|
229 #endif |
|
230 |
|
231 Q_SIGNALS: |
|
232 void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
|
233 void headerDataChanged(Qt::Orientation orientation, int first, int last); |
|
234 void layoutChanged(); |
|
235 void layoutAboutToBeChanged(); |
|
236 |
|
237 #if !defined(Q_MOC_RUN) && !defined(qdoc) |
|
238 private: // can only be emitted by QAbstractItemModel |
|
239 #endif |
|
240 void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last); |
|
241 void rowsInserted(const QModelIndex &parent, int first, int last); |
|
242 |
|
243 void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last); |
|
244 void rowsRemoved(const QModelIndex &parent, int first, int last); |
|
245 |
|
246 void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last); |
|
247 void columnsInserted(const QModelIndex &parent, int first, int last); |
|
248 |
|
249 void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last); |
|
250 void columnsRemoved(const QModelIndex &parent, int first, int last); |
|
251 |
|
252 void modelAboutToBeReset(); |
|
253 void modelReset(); |
|
254 |
|
255 void rowsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow ); |
|
256 void rowsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row ); |
|
257 |
|
258 void columnsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn ); |
|
259 void columnsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column ); |
|
260 |
|
261 |
|
262 public Q_SLOTS: |
|
263 virtual bool submit(); |
|
264 virtual void revert(); |
|
265 |
|
266 protected: |
|
267 QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = 0); |
|
268 |
|
269 inline QModelIndex createIndex(int row, int column, void *data = 0) const; |
|
270 inline QModelIndex createIndex(int row, int column, int id) const; |
|
271 inline QModelIndex createIndex(int row, int column, quint32 id) const; |
|
272 |
|
273 void encodeData(const QModelIndexList &indexes, QDataStream &stream) const; |
|
274 bool decodeData(int row, int column, const QModelIndex &parent, QDataStream &stream); |
|
275 |
|
276 void beginInsertRows(const QModelIndex &parent, int first, int last); |
|
277 void endInsertRows(); |
|
278 |
|
279 void beginRemoveRows(const QModelIndex &parent, int first, int last); |
|
280 void endRemoveRows(); |
|
281 |
|
282 bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow); |
|
283 void endMoveRows(); |
|
284 |
|
285 void beginInsertColumns(const QModelIndex &parent, int first, int last); |
|
286 void endInsertColumns(); |
|
287 |
|
288 void beginRemoveColumns(const QModelIndex &parent, int first, int last); |
|
289 void endRemoveColumns(); |
|
290 |
|
291 bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn); |
|
292 void endMoveColumns(); |
|
293 |
|
294 void reset(); |
|
295 |
|
296 void beginResetModel(); |
|
297 void endResetModel(); |
|
298 |
|
299 void changePersistentIndex(const QModelIndex &from, const QModelIndex &to); |
|
300 void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to); |
|
301 QModelIndexList persistentIndexList() const; |
|
302 |
|
303 void setRoleNames(const QHash<int,QByteArray> &roleNames); |
|
304 |
|
305 private: |
|
306 Q_DECLARE_PRIVATE(QAbstractItemModel) |
|
307 Q_DISABLE_COPY(QAbstractItemModel) |
|
308 }; |
|
309 |
|
310 inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent) |
|
311 { return insertRows(arow, 1, aparent); } |
|
312 inline bool QAbstractItemModel::insertColumn(int acolumn, const QModelIndex &aparent) |
|
313 { return insertColumns(acolumn, 1, aparent); } |
|
314 inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent) |
|
315 { return removeRows(arow, 1, aparent); } |
|
316 inline bool QAbstractItemModel::removeColumn(int acolumn, const QModelIndex &aparent) |
|
317 { return removeColumns(acolumn, 1, aparent); } |
|
318 |
|
319 inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const |
|
320 { return QModelIndex(arow, acolumn, adata, this); } |
|
321 inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, int aid) const |
|
322 #if defined(Q_CC_MSVC) |
|
323 #pragma warning( push ) |
|
324 #pragma warning( disable : 4312 ) // avoid conversion warning on 64-bit |
|
325 #endif |
|
326 { return QModelIndex(arow, acolumn, reinterpret_cast<void*>(aid), this); } |
|
327 #if defined(Q_CC_MSVC) |
|
328 #pragma warning( pop ) |
|
329 #endif |
|
330 inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, quint32 aid) const |
|
331 #if defined(Q_CC_MSVC) |
|
332 #pragma warning( push ) |
|
333 #pragma warning( disable : 4312 ) // avoid conversion warning on 64-bit |
|
334 #endif |
|
335 { return QModelIndex(arow, acolumn, reinterpret_cast<void*>(aid), this); } |
|
336 #if defined(Q_CC_MSVC) |
|
337 #pragma warning( pop ) |
|
338 #endif |
|
339 |
|
340 |
|
341 class Q_CORE_EXPORT QAbstractTableModel : public QAbstractItemModel |
|
342 { |
|
343 Q_OBJECT |
|
344 |
|
345 public: |
|
346 explicit QAbstractTableModel(QObject *parent = 0); |
|
347 ~QAbstractTableModel(); |
|
348 |
|
349 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; |
|
350 bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
|
351 int row, int column, const QModelIndex &parent); |
|
352 protected: |
|
353 QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent); |
|
354 |
|
355 private: |
|
356 Q_DISABLE_COPY(QAbstractTableModel) |
|
357 QModelIndex parent(const QModelIndex &child) const; |
|
358 bool hasChildren(const QModelIndex &parent) const; |
|
359 }; |
|
360 |
|
361 class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel |
|
362 { |
|
363 Q_OBJECT |
|
364 |
|
365 public: |
|
366 explicit QAbstractListModel(QObject *parent = 0); |
|
367 ~QAbstractListModel(); |
|
368 |
|
369 QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const; |
|
370 bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
|
371 int row, int column, const QModelIndex &parent); |
|
372 protected: |
|
373 QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent); |
|
374 |
|
375 private: |
|
376 Q_DISABLE_COPY(QAbstractListModel) |
|
377 QModelIndex parent(const QModelIndex &child) const; |
|
378 int columnCount(const QModelIndex &parent) const; |
|
379 bool hasChildren(const QModelIndex &parent) const; |
|
380 }; |
|
381 |
|
382 // inline implementations |
|
383 |
|
384 inline QModelIndex::QModelIndex(int arow, int acolumn, void *adata, |
|
385 const QAbstractItemModel *amodel) |
|
386 : r(arow), c(acolumn), p(adata), m(amodel) {} |
|
387 |
|
388 inline QModelIndex QModelIndex::parent() const |
|
389 { return m ? m->parent(*this) : QModelIndex(); } |
|
390 |
|
391 inline QModelIndex QModelIndex::sibling(int arow, int acolumn) const |
|
392 { return m ? (r == arow && c == acolumn) ? *this : m->index(arow, acolumn, m->parent(*this)) : QModelIndex(); } |
|
393 |
|
394 inline QModelIndex QModelIndex::child(int arow, int acolumn) const |
|
395 { return m ? m->index(arow, acolumn, *this) : QModelIndex(); } |
|
396 |
|
397 inline QVariant QModelIndex::data(int arole) const |
|
398 { return m ? m->data(*this, arole) : QVariant(); } |
|
399 |
|
400 inline Qt::ItemFlags QModelIndex::flags() const |
|
401 { return m ? m->flags(*this) : Qt::ItemFlags(0); } |
|
402 |
|
403 inline uint qHash(const QModelIndex &index) |
|
404 { return uint((index.row() << 4) + index.column() + index.internalId()); } |
|
405 |
|
406 QT_END_NAMESPACE |
|
407 |
|
408 QT_END_HEADER |
|
409 |
|
410 #endif // QABSTRACTITEMMODEL_H |