qabstractitemmodel.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QABSTRACTITEMMODEL_H
00043 #define QABSTRACTITEMMODEL_H
00044 
00045 #include <QtCore/qvariant.h>
00046 #include <QtCore/qobject.h>
00047 #include <QtCore/qhash.h>
00048 
00049 QT_BEGIN_HEADER
00050 
00051 QT_BEGIN_NAMESPACE
00052 
00053 QT_MODULE(Core)
00054 
00055 class QAbstractItemModel;
00056 class QPersistentModelIndex;
00057 
00058 class Q_CORE_EXPORT QModelIndex
00059 {
00060     friend class QAbstractItemModel;
00061     friend class QProxyModel;
00062 public:
00063     inline QModelIndex() : r(-1), c(-1), p(0), m(0) {}
00064     inline QModelIndex(const QModelIndex &other)
00065         : r(other.r), c(other.c), p(other.p), m(other.m) {}
00066     inline ~QModelIndex() { p = 0; m = 0; }
00067     inline int row() const { return r; }
00068     inline int column() const { return c; }
00069     inline void *internalPointer() const { return p; }
00070     inline qint64 internalId() const { return reinterpret_cast<qint64>(p); }
00071     inline QModelIndex parent() const;
00072     inline QModelIndex sibling(int row, int column) const;
00073     inline QModelIndex child(int row, int column) const;
00074     inline QVariant data(int role = Qt::DisplayRole) const;
00075     inline Qt::ItemFlags flags() const;
00076     inline const QAbstractItemModel *model() const { return m; }
00077     inline bool isValid() const { return (r >= 0) && (c >= 0) && (m != 0); }
00078     inline bool operator==(const QModelIndex &other) const
00079         { return (other.r == r) && (other.p == p) && (other.c == c) && (other.m == m); }
00080     inline bool operator!=(const QModelIndex &other) const
00081         { return !(*this == other); }
00082     inline bool operator<(const QModelIndex &other) const
00083         {
00084           if (r < other.r) return true;
00085           if (r == other.r) {
00086               if (c < other.c) return true;
00087               if (c == other.c) {
00088                   if (p < other.p) return true;
00089                   if (p == other.p) return m < other.m;
00090               }
00091           }
00092           return false; }
00093 private:
00094     inline QModelIndex(int row, int column, void *ptr, const QAbstractItemModel *model);
00095     int r, c;
00096     void *p;
00097     const QAbstractItemModel *m;
00098 };
00099 Q_DECLARE_TYPEINFO(QModelIndex, Q_MOVABLE_TYPE);
00100 
00101 #ifndef QT_NO_DEBUG_STREAM
00102 Q_CORE_EXPORT QDebug operator<<(QDebug, const QModelIndex &);
00103 #endif
00104 
00105 class QPersistentModelIndexData;
00106 
00107 class Q_CORE_EXPORT QPersistentModelIndex
00108 {
00109 public:
00110     QPersistentModelIndex();
00111     QPersistentModelIndex(const QModelIndex &index);
00112     QPersistentModelIndex(const QPersistentModelIndex &other);
00113     ~QPersistentModelIndex();
00114     bool operator<(const QPersistentModelIndex &other) const;
00115     bool operator==(const QPersistentModelIndex &other) const;
00116     inline bool operator!=(const QPersistentModelIndex &other) const
00117     { return !operator==(other); }
00118     QPersistentModelIndex &operator=(const QPersistentModelIndex &other);
00119     bool operator==(const QModelIndex &other) const;
00120     bool operator!=(const QModelIndex &other) const;
00121     QPersistentModelIndex &operator=(const QModelIndex &other);
00122     operator const QModelIndex&() const;
00123     int row() const;
00124     int column() const;
00125     void *internalPointer() const;
00126     qint64 internalId() const;
00127     QModelIndex parent() const;
00128     QModelIndex sibling(int row, int column) const;
00129     QModelIndex child(int row, int column) const;
00130     QVariant data(int role = Qt::DisplayRole) const;
00131     Qt::ItemFlags flags() const;
00132     const QAbstractItemModel *model() const;
00133     bool isValid() const;
00134 private:
00135     QPersistentModelIndexData *d;
00136     friend uint qHash(const QPersistentModelIndex &);
00137 #ifndef QT_NO_DEBUG_STREAM
00138     friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
00139 #endif
00140 };
00141 Q_DECLARE_TYPEINFO(QPersistentModelIndex, Q_MOVABLE_TYPE);
00142 
00143 inline uint qHash(const QPersistentModelIndex &index)
00144 { return qHash(index.d); }
00145 
00146 
00147 #ifndef QT_NO_DEBUG_STREAM
00148 Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
00149 #endif
00150 
00151 template<typename T> class QList;
00152 typedef QList<QModelIndex> QModelIndexList;
00153 
00154 class QMimeData;
00155 class QAbstractItemModelPrivate;
00156 template <class Key, class T> class QMap;
00157 
00158 
00159 class Q_CORE_EXPORT QAbstractItemModel : public QObject
00160 {
00161     Q_OBJECT
00162 
00163     friend class QPersistentModelIndexData;
00164     friend class QAbstractItemViewPrivate;
00165 public:
00166 
00167     explicit QAbstractItemModel(QObject *parent = 0);
00168     virtual ~QAbstractItemModel();
00169 
00170     bool hasIndex(int row, int column, const QModelIndex &parent = QModelIndex()) const;
00171     virtual QModelIndex index(int row, int column,
00172                               const QModelIndex &parent = QModelIndex()) const = 0;
00173     virtual QModelIndex parent(const QModelIndex &child) const = 0;
00174 
00175     inline QModelIndex sibling(int row, int column, const QModelIndex &idx) const
00176         { return index(row, column, parent(idx)); }
00177 
00178     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const = 0;
00179     virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0;
00180     virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
00181 
00182     virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0;
00183     virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
00184 
00185     virtual QVariant headerData(int section, Qt::Orientation orientation,
00186                                 int role = Qt::DisplayRole) const;
00187     virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
00188                                int role = Qt::EditRole);
00189 
00190     virtual QMap<int, QVariant> itemData(const QModelIndex &index) const;
00191     virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
00192 
00193     virtual QStringList mimeTypes() const;
00194     virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
00195     virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action,
00196                               int row, int column, const QModelIndex &parent);
00197     virtual Qt::DropActions supportedDropActions() const;
00198 
00199     Qt::DropActions supportedDragActions() const;
00200     void setSupportedDragActions(Qt::DropActions);
00201 
00202     virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
00203     virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex());
00204     virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
00205     virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex());
00206 
00207     inline bool insertRow(int row, const QModelIndex &parent = QModelIndex());
00208     inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex());
00209     inline bool removeRow(int row, const QModelIndex &parent = QModelIndex());
00210     inline bool removeColumn(int column, const QModelIndex &parent = QModelIndex());
00211 
00212     virtual void fetchMore(const QModelIndex &parent);
00213     virtual bool canFetchMore(const QModelIndex &parent) const;
00214     virtual Qt::ItemFlags flags(const QModelIndex &index) const;
00215     virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
00216     virtual QModelIndex buddy(const QModelIndex &index) const;
00217     virtual QModelIndexList match(const QModelIndex &start, int role,
00218                                   const QVariant &value, int hits = 1,
00219                                   Qt::MatchFlags flags =
00220                                   Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const;
00221     virtual QSize span(const QModelIndex &index) const;
00222 
00223     const QHash<int,QByteArray> &roleNames() const;
00224 
00225 #ifdef Q_NO_USING_KEYWORD
00226     inline QObject *parent() const { return QObject::parent(); }
00227 #else
00228     using QObject::parent;
00229 #endif
00230 
00231 Q_SIGNALS:
00232     void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
00233     void headerDataChanged(Qt::Orientation orientation, int first, int last);
00234     void layoutChanged();
00235     void layoutAboutToBeChanged();
00236 
00237 #if !defined(Q_MOC_RUN) && !defined(qdoc)
00238 private: // can only be emitted by QAbstractItemModel
00239 #endif
00240     void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last);
00241     void rowsInserted(const QModelIndex &parent, int first, int last);
00242 
00243     void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
00244     void rowsRemoved(const QModelIndex &parent, int first, int last);
00245 
00246     void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last);
00247     void columnsInserted(const QModelIndex &parent, int first, int last);
00248 
00249     void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
00250     void columnsRemoved(const QModelIndex &parent, int first, int last);
00251 
00252     void modelAboutToBeReset();
00253     void modelReset();
00254 
00255     void rowsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow );
00256     void rowsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row );
00257 
00258     void columnsAboutToBeMoved( const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn );
00259     void columnsMoved( const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column );
00260 
00261 
00262 public Q_SLOTS:
00263     virtual bool submit();
00264     virtual void revert();
00265 
00266 protected:
00267     QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = 0);
00268 
00269     inline QModelIndex createIndex(int row, int column, void *data = 0) const;
00270     inline QModelIndex createIndex(int row, int column, int id) const;
00271     inline QModelIndex createIndex(int row, int column, quint32 id) const;
00272 
00273     void encodeData(const QModelIndexList &indexes, QDataStream &stream) const;
00274     bool decodeData(int row, int column, const QModelIndex &parent, QDataStream &stream);
00275 
00276     void beginInsertRows(const QModelIndex &parent, int first, int last);
00277     void endInsertRows();
00278 
00279     void beginRemoveRows(const QModelIndex &parent, int first, int last);
00280     void endRemoveRows();
00281 
00282     bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow);
00283     void endMoveRows();
00284 
00285     void beginInsertColumns(const QModelIndex &parent, int first, int last);
00286     void endInsertColumns();
00287 
00288     void beginRemoveColumns(const QModelIndex &parent, int first, int last);
00289     void endRemoveColumns();
00290 
00291     bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn);
00292     void endMoveColumns();
00293 
00294     void reset();
00295 
00296     void beginResetModel();
00297     void endResetModel();
00298 
00299     void changePersistentIndex(const QModelIndex &from, const QModelIndex &to);
00300     void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to);
00301     QModelIndexList persistentIndexList() const;
00302 
00303     void setRoleNames(const QHash<int,QByteArray> &roleNames);
00304 
00305 private:
00306     Q_DECLARE_PRIVATE(QAbstractItemModel)
00307     Q_DISABLE_COPY(QAbstractItemModel)
00308 };
00309 
00310 inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
00311 { return insertRows(arow, 1, aparent); }
00312 inline bool QAbstractItemModel::insertColumn(int acolumn, const QModelIndex &aparent)
00313 { return insertColumns(acolumn, 1, aparent); }
00314 inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent)
00315 { return removeRows(arow, 1, aparent); }
00316 inline bool QAbstractItemModel::removeColumn(int acolumn, const QModelIndex &aparent)
00317 { return removeColumns(acolumn, 1, aparent); }
00318 
00319 inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const
00320 { return QModelIndex(arow, acolumn, adata, this); }
00321 inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, int aid) const
00322 #if defined(Q_CC_MSVC)
00323 #pragma warning( push )
00324 #pragma warning( disable : 4312 ) // avoid conversion warning on 64-bit
00325 #endif
00326 { return QModelIndex(arow, acolumn, reinterpret_cast<void*>(aid), this); }
00327 #if defined(Q_CC_MSVC)
00328 #pragma warning( pop )
00329 #endif
00330 inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, quint32 aid) const
00331 #if defined(Q_CC_MSVC)
00332 #pragma warning( push )
00333 #pragma warning( disable : 4312 ) // avoid conversion warning on 64-bit
00334 #endif
00335 { return QModelIndex(arow, acolumn, reinterpret_cast<void*>(aid), this); }
00336 #if defined(Q_CC_MSVC)
00337 #pragma warning( pop )
00338 #endif
00339 
00340 
00341 class Q_CORE_EXPORT QAbstractTableModel : public QAbstractItemModel
00342 {
00343     Q_OBJECT
00344 
00345 public:
00346     explicit QAbstractTableModel(QObject *parent = 0);
00347     ~QAbstractTableModel();
00348 
00349     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
00350     bool dropMimeData(const QMimeData *data, Qt::DropAction action,
00351                       int row, int column, const QModelIndex &parent);
00352 protected:
00353     QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent);
00354 
00355 private:
00356     Q_DISABLE_COPY(QAbstractTableModel)
00357     QModelIndex parent(const QModelIndex &child) const;
00358     bool hasChildren(const QModelIndex &parent) const;
00359 };
00360 
00361 class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel
00362 {
00363     Q_OBJECT
00364 
00365 public:
00366     explicit QAbstractListModel(QObject *parent = 0);
00367     ~QAbstractListModel();
00368 
00369     QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
00370     bool dropMimeData(const QMimeData *data, Qt::DropAction action,
00371                       int row, int column, const QModelIndex &parent);
00372 protected:
00373     QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent);
00374 
00375 private:
00376     Q_DISABLE_COPY(QAbstractListModel)
00377     QModelIndex parent(const QModelIndex &child) const;
00378     int columnCount(const QModelIndex &parent) const;
00379     bool hasChildren(const QModelIndex &parent) const;
00380 };
00381 
00382 // inline implementations
00383 
00384 inline QModelIndex::QModelIndex(int arow, int acolumn, void *adata,
00385                                 const QAbstractItemModel *amodel)
00386     : r(arow), c(acolumn), p(adata), m(amodel) {}
00387 
00388 inline QModelIndex QModelIndex::parent() const
00389 { return m ? m->parent(*this) : QModelIndex(); }
00390 
00391 inline QModelIndex QModelIndex::sibling(int arow, int acolumn) const
00392 { return m ? (r == arow && c == acolumn) ? *this : m->index(arow, acolumn, m->parent(*this)) : QModelIndex(); }
00393 
00394 inline QModelIndex QModelIndex::child(int arow, int acolumn) const
00395 { return m ? m->index(arow, acolumn, *this) : QModelIndex(); }
00396 
00397 inline QVariant QModelIndex::data(int arole) const
00398 { return m ? m->data(*this, arole) : QVariant(); }
00399 
00400 inline Qt::ItemFlags QModelIndex::flags() const
00401 { return m ? m->flags(*this) : Qt::ItemFlags(0); }
00402 
00403 inline uint qHash(const QModelIndex &index)
00404 { return uint((index.row() << 4) + index.column() + index.internalId()); }
00405 
00406 QT_END_NAMESPACE
00407 
00408 QT_END_HEADER
00409 
00410 #endif // QABSTRACTITEMMODEL_H