⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qabstractitemmodel.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************** 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, otherwist returns false.  Note that 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.    Note that 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;}/*!    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();}/*!  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    Returns true if this persistent model index is valid; otherwise returns    false.    A valid index belongs to a model, and has non-negative row and column numbers.    \sa model(), row(), column()*/bool QPersistentModelIndex::isValid() const{    return d && d->index.isValid();}#ifndef QT_NO_DEBUG_STREAMQDebug operator<<(QDebug dbg, const QModelIndex &idx){#ifndef Q_BROKEN_DEBUG_STREAM    dbg.nospace() << "QModelIndex(" << idx.row() << "," << idx.column()                  << "," << idx.internalPointer() << "," << idx.model() << ")";    return dbg.space();#else    qWarning("This compiler doesn't support streaming QModelIndex to QDebug");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -