?? qabstractitemmodel.cpp
字號:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qabstractitemmodel.h"#include <private/qabstractitemmodel_p.h>#include <qdatastream.h>#include <qstringlist.h>#include <qsize.h>#include <qmimedata.h>#include <qdebug.h>#include <qvector.h>#include <qstack.h>#include <qbitarray.h>#include <limits.h>QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex &index){ Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list QPersistentModelIndexData *d = 0; QAbstractItemModel *model = const_cast<QAbstractItemModel*>(index.model()); QList<QPersistentModelIndexData*> *persistentIndexes = &(model->d_func()->persistent.indexes); for (int i = 0; i < persistentIndexes->count(); ++i) { if (persistentIndexes->at(i)->index == index) { d = persistentIndexes->at(i); break; } } if (!d) { // not found d = new QPersistentModelIndexData(); d->model = model; d->index = index; persistentIndexes->append(d); } Q_ASSERT(d); return d;}void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data){ Q_ASSERT(data); Q_ASSERT(data->ref == 0); QAbstractItemModel *model = const_cast<QAbstractItemModel*>(data->model); // a valid persistent model index with a null model pointer can only happen if the model was destroyed if (model) { QAbstractItemModelPrivate *p = model->d_func(); Q_ASSERT(p); p->removePersistentIndexData(data); } delete data;}/*! \class QPersistentModelIndex \brief The QPersistentModelIndex class is used to locate data in a data model. \ingroup model-view A QPersistentModelIndex is a model index that can be stored by an application, and later used to access information in a model. Unlike the QModelIndex class, it is safe to store a QPersistentModelIndex since the model will ensure that references to items will continue to be valid as long as they can be accessed by the model. It is good practice to check that persistent model indexes are valid before using them. \sa {Model/View Programming}, QModelIndex, QAbstractItemModel*//*! \fn QPersistentModelIndex::QPersistentModelIndex() \internal*/QPersistentModelIndex::QPersistentModelIndex() : d(0){}/*! \fn QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other) Creates a new QPersistentModelIndex that is a copy of the \a other persistent model index.*/QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other) : d(other.d){ if (d) d->ref.ref();}/*! Creates a new QPersistentModelIndex that is a copy of the model \a index.*/QPersistentModelIndex::QPersistentModelIndex(const QModelIndex &index) : d(0){ if (index.isValid()) { d = QPersistentModelIndexData::create(index); d->ref.ref(); }}/*! \fn QPersistentModelIndex::~QPersistentModelIndex() \internal*/QPersistentModelIndex::~QPersistentModelIndex(){ if (d && !d->ref.deref()) { QPersistentModelIndexData::destroy(d); d = 0; }}/*! Returns true if this persistent model index is equal to the \a other persistent model index; otherwise returns false. All values in the persistent model index are used when comparing with another persistent model index.*/bool QPersistentModelIndex::operator==(const QPersistentModelIndex &other) const{ if (d && other.d) return d->index == other.d->index; return d == other.d;}/*! \since 4.1 Returns true if this persistent model index is smaller than the \a other persistent model index; otherwise returns false. All values in the persistent model index are used when comparing with another persistent model index.*/bool QPersistentModelIndex::operator<(const QPersistentModelIndex &other) const{ if (d && other.d) return d->index < other.d->index; return d < other.d;}/*! \fn bool QPersistentModelIndex::operator!=(const QPersistentModelIndex &other) const \since 4.2 Returns true if this persistent model index is not equal to the \a other persistent model index; otherwise returns false.*//*! Sets the persistent model index to refer to the same item in a model as the \a other persistent model index.*/QPersistentModelIndex &QPersistentModelIndex::operator=(const QPersistentModelIndex &other){ if (d == other.d) return *this; if (d && !d->ref.deref()) QPersistentModelIndexData::destroy(d); d = other.d; if (d) d->ref.ref(); return *this;}/*! Sets the persistent model index to refer to the same item in a model as the \a other model index.*/QPersistentModelIndex &QPersistentModelIndex::operator=(const QModelIndex &other){ if (d && !d->ref.deref()) QPersistentModelIndexData::destroy(d); if (other.isValid()) { d = QPersistentModelIndexData::create(other); if (d) d->ref.ref(); } else { d = 0; } return *this;}/*! \fn QPersistentModelIndex::operator const QModelIndex&() const Cast operator that returns a const QModelIndex&.*/QPersistentModelIndex::operator const QModelIndex&() const{ static const QModelIndex invalid; if (d) return d->index; return invalid;}/*! \fn bool QPersistentModelIndex::operator==(const QModelIndex &other) const Returns true if this persistent model index refers to the same location as the \a other model index; otherwise returns false. Note that all values in the persistent model index are used when comparing with another model index.*/bool QPersistentModelIndex::operator==(const QModelIndex &other) const{ if (d) return d->index == other; return !other.isValid();}/*! \fn bool QPersistentModelIndex::operator!=(const QModelIndex &other) const Returns true if this persistent model index does not refer to the same location as the \a other model index; otherwise returns false.*/bool QPersistentModelIndex::operator!=(const QModelIndex &other) const{ if (d) return d->index != other; return other.isValid();}/*! \fn int QPersistentModelIndex::row() const Returns the row this persistent model index refers to.*/int QPersistentModelIndex::row() const{ if (d) return d->index.row(); return -1;}/*! \fn int QPersistentModelIndex::column() const Returns the column this persistent model index refers to.*/int QPersistentModelIndex::column() const{ if (d) return d->index.column(); return -1;}/*! \fn void *QPersistentModelIndex::internalPointer() const \internal Returns a \c{void} \c{*} pointer used by the model to associate the index with the internal data structure.*/void *QPersistentModelIndex::internalPointer() const{ if (d) return d->index.internalPointer(); return 0;}/*! \fn void *QPersistentModelIndex::internalId() const \internal Returns a \c{qint64} used by the model to associate the index with the internal data structure.*/qint64 QPersistentModelIndex::internalId() const{ if (d) return d->index.internalId(); return 0;}/*! Returns the parent QModelIndex for this persistent index, or QModelIndex() if it has no parent. \sa child() sibling() model()*/QModelIndex QPersistentModelIndex::parent() const{ if (d) return d->index.parent(); return QModelIndex();}/*! Returns the sibling at \a row and \a column or an invalid QModelIndex if there is no sibling at this position. \sa parent() child()*/QModelIndex QPersistentModelIndex::sibling(int row, int column) const{ if (d) return d->index.sibling(row, column); return QModelIndex();}/*! Returns the child of the model index that is stored in the given \a row and \a column. \sa parent() sibling()*/QModelIndex QPersistentModelIndex::child(int row, int column) const{ if (d) return d->index.child(row, column); return QModelIndex();}/*! Returns the data for the given \a role for the item referred to by the index.*/QVariant QPersistentModelIndex::data(int role) const{ if (d) return d->index.data(role); return QVariant();}/*! \since 4.2 Returns the flags for the item referred to by the index.*/Qt::ItemFlags QPersistentModelIndex::flags() const{ if (d) return d->index.flags(); return 0;}/*! Returns the model that the index belongs to.*/const QAbstractItemModel *QPersistentModelIndex::model() const{ if (d) return d->index.model(); return 0;}/*! \fn bool QPersistentModelIndex::isValid() const
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -