| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qproxymodel.h"
|
|---|
| 43 |
|
|---|
| 44 | #ifndef QT_NO_PROXYMODEL
|
|---|
| 45 | #include <private/qproxymodel_p.h>
|
|---|
| 46 | #include <qsize.h>
|
|---|
| 47 | #include <qstringlist.h>
|
|---|
| 48 |
|
|---|
| 49 | QT_BEGIN_NAMESPACE
|
|---|
| 50 |
|
|---|
| 51 | /*!
|
|---|
| 52 | \class QProxyModel
|
|---|
| 53 | \obsolete
|
|---|
| 54 | \brief The QProxyModel class provides support for processing data
|
|---|
| 55 | passed between another model and a view.
|
|---|
| 56 |
|
|---|
| 57 | \ingroup model-view
|
|---|
| 58 |
|
|---|
| 59 | If you want to do filtering and sorting, see QSortFilterProxyModel.
|
|---|
| 60 |
|
|---|
| 61 | Proxy models provide a standard model interface that can be used to
|
|---|
| 62 | manipulate the data retrieved through an underlying model. They can be used to
|
|---|
| 63 | perform operations such as sorting and filtering on the data obtained without
|
|---|
| 64 | changing the contents of the model.
|
|---|
| 65 |
|
|---|
| 66 | Just as with subclasses of QAbstractItemView, QProxyModel provides the setModel()
|
|---|
| 67 | function that is used to specify the model to be acted on by the proxy.
|
|---|
| 68 | Views can be connected to either the underlying model or the proxy model with
|
|---|
| 69 | \l QAbstractItemView::setModel().
|
|---|
| 70 |
|
|---|
| 71 | Since views rely on the information provided in model indexes to identify
|
|---|
| 72 | items of data from models, and to position these items in some visual
|
|---|
| 73 | representation, proxy models must create their own model indexes instead of
|
|---|
| 74 | supplying model indexes from their underlying models.
|
|---|
| 75 |
|
|---|
| 76 | \sa \link model-view-programming.html Model/View Programming\endlink QAbstractItemModel
|
|---|
| 77 |
|
|---|
| 78 | */
|
|---|
| 79 |
|
|---|
| 80 | /*!
|
|---|
| 81 | Constructs a proxy model with the given \a parent.
|
|---|
| 82 | */
|
|---|
| 83 |
|
|---|
| 84 | QProxyModel::QProxyModel(QObject *parent)
|
|---|
| 85 | : QAbstractItemModel(*new QProxyModelPrivate, parent)
|
|---|
| 86 | {
|
|---|
| 87 | Q_D(QProxyModel);
|
|---|
| 88 | setModel(&d->empty);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /*!
|
|---|
| 92 | \internal
|
|---|
| 93 | */
|
|---|
| 94 | QProxyModel::QProxyModel(QProxyModelPrivate &dd, QObject *parent)
|
|---|
| 95 | : QAbstractItemModel(dd, parent)
|
|---|
| 96 | {
|
|---|
| 97 | Q_D(QProxyModel);
|
|---|
| 98 | setModel(&d->empty);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /*!
|
|---|
| 102 | Destroys the proxy model.
|
|---|
| 103 | */
|
|---|
| 104 | QProxyModel::~QProxyModel()
|
|---|
| 105 | {
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /*!
|
|---|
| 109 | Sets the given \a model to be processed by the proxy model.
|
|---|
| 110 | */
|
|---|
| 111 | void QProxyModel::setModel(QAbstractItemModel *model)
|
|---|
| 112 | {
|
|---|
| 113 | Q_D(QProxyModel);
|
|---|
| 114 | if (d->model && d->model != &d->empty)
|
|---|
| 115 | disconnectFromModel(d->model);
|
|---|
| 116 | if (model) {
|
|---|
| 117 | d->model = model;
|
|---|
| 118 | connectToModel(model);
|
|---|
| 119 | } else {
|
|---|
| 120 | d->model = &d->empty;
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /*!
|
|---|
| 125 | Returns the model that contains the data that is available through the
|
|---|
| 126 | proxy model.
|
|---|
| 127 | */
|
|---|
| 128 | QAbstractItemModel *QProxyModel::model() const
|
|---|
| 129 | {
|
|---|
| 130 | Q_D(const QProxyModel);
|
|---|
| 131 | return d->model;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /*!
|
|---|
| 135 | Returns the model index with the given \a row, \a column, and \a parent.
|
|---|
| 136 |
|
|---|
| 137 | \sa QAbstractItemModel::index()
|
|---|
| 138 | */
|
|---|
| 139 | QModelIndex QProxyModel::index(int row, int column, const QModelIndex &parent) const
|
|---|
| 140 | {
|
|---|
| 141 | Q_D(const QProxyModel);
|
|---|
| 142 | return setProxyModel(d->model->index(row, column, setSourceModel(parent)));
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /*!
|
|---|
| 146 | Returns the model index that corresponds to the parent of the given \a child
|
|---|
| 147 | index.
|
|---|
| 148 | */
|
|---|
| 149 | QModelIndex QProxyModel::parent(const QModelIndex &child) const
|
|---|
| 150 | {
|
|---|
| 151 | Q_D(const QProxyModel);
|
|---|
| 152 | return setProxyModel(d->model->parent(setSourceModel(child)));
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /*!
|
|---|
| 156 | Returns the number of rows for the given \a parent.
|
|---|
| 157 |
|
|---|
| 158 | \sa QAbstractItemModel::rowCount()
|
|---|
| 159 | */
|
|---|
| 160 | int QProxyModel::rowCount(const QModelIndex &parent) const
|
|---|
| 161 | {
|
|---|
| 162 | Q_D(const QProxyModel);
|
|---|
| 163 | return d->model->rowCount(setSourceModel(parent));
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /*!
|
|---|
| 167 | Returns the number of columns for the given \a parent.
|
|---|
| 168 |
|
|---|
| 169 | \sa QAbstractItemModel::columnCount()
|
|---|
| 170 | */
|
|---|
| 171 | int QProxyModel::columnCount(const QModelIndex &parent) const
|
|---|
| 172 | {
|
|---|
| 173 | Q_D(const QProxyModel);
|
|---|
| 174 | return d->model->columnCount(setSourceModel(parent));
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /*!
|
|---|
| 178 | Returns true if the item corresponding to the \a parent index has child
|
|---|
| 179 | items; otherwise returns false.
|
|---|
| 180 |
|
|---|
| 181 | \sa QAbstractItemModel::hasChildren()
|
|---|
| 182 | */
|
|---|
| 183 | bool QProxyModel::hasChildren(const QModelIndex &parent) const
|
|---|
| 184 | {
|
|---|
| 185 | Q_D(const QProxyModel);
|
|---|
| 186 | return d->model->hasChildren(setSourceModel(parent));
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /*!
|
|---|
| 190 | Returns the data stored in the item with the given \a index under the
|
|---|
| 191 | specified \a role.
|
|---|
| 192 | */
|
|---|
| 193 | QVariant QProxyModel::data(const QModelIndex &index, int role) const
|
|---|
| 194 | {
|
|---|
| 195 | Q_D(const QProxyModel);
|
|---|
| 196 | return d->model->data(setSourceModel(index), role);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /*!
|
|---|
| 200 | Sets the \a role data for the item at \a index to \a value.
|
|---|
| 201 | Returns true if successful; otherwise returns false.
|
|---|
| 202 |
|
|---|
| 203 | The base class implementation returns false. This function and
|
|---|
| 204 | data() must be reimplemented for editable models.
|
|---|
| 205 |
|
|---|
| 206 | \sa data() itemData() QAbstractItemModel::setData()
|
|---|
| 207 | */
|
|---|
| 208 | bool QProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|---|
| 209 | {
|
|---|
| 210 | Q_D(const QProxyModel);
|
|---|
| 211 | return d->model->setData(setSourceModel(index), value, role);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /*!
|
|---|
| 215 | Returns the data stored in the \a section of the header with specified
|
|---|
| 216 | \a orientation under the given \a role.
|
|---|
| 217 | */
|
|---|
| 218 | QVariant QProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|---|
| 219 | {
|
|---|
| 220 | Q_D(const QProxyModel);
|
|---|
| 221 | return d->model->headerData(section, orientation, role);
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | /*!
|
|---|
| 225 | Sets the \a role data in the \a section of the header with the specified
|
|---|
| 226 | \a orientation to the \a value given.
|
|---|
| 227 |
|
|---|
| 228 | \sa QAbstractItemModel::setHeaderData()
|
|---|
| 229 | */
|
|---|
| 230 | bool QProxyModel::setHeaderData(int section, Qt::Orientation orientation,
|
|---|
| 231 | const QVariant &value, int role)
|
|---|
| 232 | {
|
|---|
| 233 | Q_D(const QProxyModel);
|
|---|
| 234 | return d->model->setHeaderData(section, orientation, value, role);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /*!
|
|---|
| 238 | Returns a list of MIME types that are supported by the model.
|
|---|
| 239 | */
|
|---|
| 240 | QStringList QProxyModel::mimeTypes() const
|
|---|
| 241 | {
|
|---|
| 242 | Q_D(const QProxyModel);
|
|---|
| 243 | return d->model->mimeTypes();
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | /*!
|
|---|
| 247 | Returns MIME data for the specified \a indexes in the model.
|
|---|
| 248 | */
|
|---|
| 249 | QMimeData *QProxyModel::mimeData(const QModelIndexList &indexes) const
|
|---|
| 250 | {
|
|---|
| 251 | Q_D(const QProxyModel);
|
|---|
| 252 | QModelIndexList lst;
|
|---|
| 253 | for (int i = 0; i < indexes.count(); ++i)
|
|---|
| 254 | lst.append(setSourceModel(indexes.at(i)));
|
|---|
| 255 | return d->model->mimeData(lst);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /*!
|
|---|
| 259 | Returns true if the model accepts the \a data dropped onto an attached
|
|---|
| 260 | view for the specified \a action; otherwise returns false.
|
|---|
| 261 |
|
|---|
| 262 | The \a parent, \a row, and \a column details can be used to control
|
|---|
| 263 | which MIME types are acceptable to different parts of a model when
|
|---|
| 264 | received via the drag and drop system.
|
|---|
| 265 | */
|
|---|
| 266 | bool QProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
|
|---|
| 267 | int row, int column, const QModelIndex &parent)
|
|---|
| 268 | {
|
|---|
| 269 | Q_D(const QProxyModel);
|
|---|
| 270 | return d->model->dropMimeData(data, action, row, column, setSourceModel(parent));
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /*!
|
|---|
| 274 | Returns the drop actions that are supported by the model; this is
|
|---|
| 275 | a combination of the individual actions defined in \l Qt::DropActions.
|
|---|
| 276 |
|
|---|
| 277 | The selection of drop actions provided by the model will influence the
|
|---|
| 278 | behavior of the component that started the drag and drop operation.
|
|---|
| 279 |
|
|---|
| 280 | \sa \link dnd.html Drag and Drop\endlink
|
|---|
|
|---|