Qt 4.8
qabstractproxymodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qabstractproxymodel.h"
43 
44 #ifndef QT_NO_PROXYMODEL
45 
46 #include "qitemselectionmodel.h"
47 #include <private/qabstractproxymodel_p.h>
48 #include <QtCore/QSize>
49 #include <QtCore/QStringList>
50 
51 
53 
87 //detects the deletion of the source model
89 {
91 }
92 
99 {
101 }
102 
108  : QAbstractItemModel(dd, parent)
109 {
111 }
112 
117 {
118 
119 }
120 
125 {
127  if (d->model)
128  disconnect(d->model, SIGNAL(destroyed()), this, SLOT(_q_sourceModelDestroyed()));
129 
130  if (sourceModel) {
131  d->model = sourceModel;
132  connect(d->model, SIGNAL(destroyed()), this, SLOT(_q_sourceModelDestroyed()));
133  } else {
135  }
136  d->roleNames = d->model->roleNames();
137 }
138 
143 {
144  Q_D(const QAbstractProxyModel);
146  return 0;
147  return d->model;
148 }
149 
154 {
156  return d->model->submit();
157 }
158 
163 {
165  d->model->revert();
166 }
167 
168 
199 {
200  QModelIndexList proxyIndexes = proxySelection.indexes();
201  QItemSelection sourceSelection;
202  for (int i = 0; i < proxyIndexes.size(); ++i) {
203  const QModelIndex proxyIdx = mapToSource(proxyIndexes.at(i));
204  if (!proxyIdx.isValid())
205  continue;
206  sourceSelection << QItemSelectionRange(proxyIdx);
207  }
208  return sourceSelection;
209 }
210 
217 {
218  QModelIndexList sourceIndexes = sourceSelection.indexes();
219  QItemSelection proxySelection;
220  for (int i = 0; i < sourceIndexes.size(); ++i) {
221  const QModelIndex srcIdx = mapFromSource(sourceIndexes.at(i));
222  if (!srcIdx.isValid())
223  continue;
224  proxySelection << QItemSelectionRange(srcIdx);
225  }
226  return proxySelection;
227 }
228 
232 QVariant QAbstractProxyModel::data(const QModelIndex &proxyIndex, int role) const
233 {
234  Q_D(const QAbstractProxyModel);
235  return d->model->data(mapToSource(proxyIndex), role);
236 }
237 
241 QVariant QAbstractProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
242 {
243  Q_D(const QAbstractProxyModel);
244  int sourceSection;
245  if (orientation == Qt::Horizontal) {
246  const QModelIndex proxyIndex = index(0, section);
247  sourceSection = mapToSource(proxyIndex).column();
248  } else {
249  const QModelIndex proxyIndex = index(section, 0);
250  sourceSection = mapToSource(proxyIndex).row();
251  }
252  return d->model->headerData(sourceSection, orientation, role);
253 }
254 
259 {
260  return QAbstractItemModel::itemData(proxyIndex);
261 }
262 
266 Qt::ItemFlags QAbstractProxyModel::flags(const QModelIndex &index) const
267 {
268  Q_D(const QAbstractProxyModel);
269  return d->model->flags(mapToSource(index));
270 }
271 
275 bool QAbstractProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
276 {
278  return d->model->setData(mapToSource(index), value, role);
279 }
280 
285 {
286  return QAbstractItemModel::setItemData(index, roles);
287 }
288 
292 bool QAbstractProxyModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
293 {
295  int sourceSection;
296  if (orientation == Qt::Horizontal) {
297  const QModelIndex proxyIndex = index(0, section);
298  sourceSection = mapToSource(proxyIndex).column();
299  } else {
300  const QModelIndex proxyIndex = index(section, 0);
301  sourceSection = mapToSource(proxyIndex).row();
302  }
303  return d->model->setHeaderData(sourceSection, orientation, value, role);
304 }
305 
311 {
312  Q_D(const QAbstractProxyModel);
313  return mapFromSource(d->model->buddy(mapToSource(index)));
314 }
315 
321 {
322  Q_D(const QAbstractProxyModel);
323  return d->model->canFetchMore(mapToSource(parent));
324 }
325 
331 {
333  d->model->fetchMore(mapToSource(parent));
334 }
335 
341 {
343  d->model->sort(column, order);
344 }
345 
351 {
352  Q_D(const QAbstractProxyModel);
353  return d->model->span(mapToSource(index));
354 }
355 
361 {
362  Q_D(const QAbstractProxyModel);
363  return d->model->hasChildren(mapToSource(parent));
364 }
365 
371 {
372  Q_D(const QAbstractProxyModel);
373  QModelIndexList list;
374  foreach(const QModelIndex &index, indexes)
375  list << mapToSource(index);
376  return d->model->mimeData(list);
377 }
378 
384 {
385  Q_D(const QAbstractProxyModel);
386  return d->model->mimeTypes();
387 }
388 
394 {
395  Q_D(const QAbstractProxyModel);
396  return d->model->supportedDropActions();
397 }
398 
400 
401 #include "moc_qabstractproxymodel.cpp"
402 
403 #endif // QT_NO_PROXYMODEL
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
static QAbstractItemModel * staticEmptyModel()
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual QMap< int, QVariant > itemData(const QModelIndex &index) const
Returns a map with values for all predefined roles in the model for the item at the given index...
virtual QItemSelection mapSelectionToSource(const QItemSelection &selection) const
Returns a source selection mapped from the specified proxySelection.
virtual QItemSelection mapSelectionFromSource(const QItemSelection &selection) const
Returns a proxy selection mapped from the specified sourceSelection.
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Reimplemented Function
virtual void setSourceModel(QAbstractItemModel *sourceModel)
Sets the given sourceModel to be processed by the proxy model.
#define SLOT(a)
Definition: qobjectdefs.h:226
The QItemSelectionRange class manages information about a range of selected items in a model...
virtual bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles)
Sets the role data for the item at index to the associated value in roles, for every Qt::ItemDataRole...
void fetchMore(const QModelIndex &parent)
Reimplemented Function
bool canFetchMore(const QModelIndex &parent) const
Reimplemented Function
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const =0
Reimplement this function to return the model index in the source model that corresponds to the proxy...
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const =0
Reimplement this function to return the model index in the proxy model that corresponds to the source...
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
Reimplemented Function
QAbstractProxyModel(QObject *parent=0)
Constructs a proxy model with the given parent.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const =0
Returns the index of the item in the model specified by the given row, column and parent index...
#define SIGNAL(a)
Definition: qobjectdefs.h:227
SortOrder
Definition: qnamespace.h:189
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
QAbstractItemModel * sourceModel() const
Returns the model that contains the data that is available through the proxy model.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
void revert()
Reimplemented Function
int row() const
Returns the row this model index refers to.
Qt::DropActions supportedDropActions() const
Reimplemented Function
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Reimplemented Function
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QMap< int, QVariant > itemData(const QModelIndex &index) const
Reimplemented Function
QStringList mimeTypes() const
Reimplemented Function
bool hasChildren(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
QModelIndex buddy(const QModelIndex &index) const
Reimplemented Function
QSize span(const QModelIndex &index) const
Reimplemented Function
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QAbstractItemModel class provides the abstract interface for item model classes.
The QAbstractProxyModel class provides a base class for proxy item models that can do sorting...
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
The QItemSelection class manages information about selected items in a model.
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles)
Reimplemented Function
QMimeData * mimeData(const QModelIndexList &indexes) const
Reimplemented Function
QVariant data(const QModelIndex &proxyIndex, int role=Qt::DisplayRole) const
Reimplemented Function
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
The QModelIndex class is used to locate data in a data model.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QObject * parent
Definition: qobject.h:92
Qt::ItemFlags flags(const QModelIndex &index) const
Reimplemented Function
~QAbstractProxyModel()
Destroys the proxy model.
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Reimplemented Function
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QModelIndexList indexes() const
Returns a list of model indexes that correspond to the selected items.
bool submit()
Reimplemented Function
Orientation
Definition: qnamespace.h:174
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
int column() const
Returns the column this model index refers to.