Qt 4.8
qproxymodel.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 "qproxymodel.h"
43 
44 #ifndef QT_NO_PROXYMODEL
45 #include <private/qproxymodel_p.h>
46 #include <qsize.h>
47 #include <qstringlist.h>
48 
50 
89 {
91  setModel(&d->empty);
92 }
93 
98  : QAbstractItemModel(dd, parent)
99 {
100  Q_D(QProxyModel);
101  setModel(&d->empty);
102 }
103 
108 {
109 }
110 
115 {
116  Q_D(QProxyModel);
117  if (d->model && d->model != &d->empty)
118  disconnectFromModel(d->model);
119  if (model) {
120  d->model = model;
121  connectToModel(model);
122  } else {
123  d->model = &d->empty;
124  }
125 }
126 
132 {
133  Q_D(const QProxyModel);
134  return d->model;
135 }
136 
142 QModelIndex QProxyModel::index(int row, int column, const QModelIndex &parent) const
143 {
144  Q_D(const QProxyModel);
145  return setProxyModel(d->model->index(row, column, setSourceModel(parent)));
146 }
147 
153 {
154  Q_D(const QProxyModel);
155  return setProxyModel(d->model->parent(setSourceModel(child)));
156 }
157 
164 {
165  Q_D(const QProxyModel);
166  return d->model->rowCount(setSourceModel(parent));
167 }
168 
175 {
176  Q_D(const QProxyModel);
177  return d->model->columnCount(setSourceModel(parent));
178 }
179 
187 {
188  Q_D(const QProxyModel);
189  return d->model->hasChildren(setSourceModel(parent));
190 }
191 
197 {
198  Q_D(const QProxyModel);
199  return d->model->data(setSourceModel(index), role);
200 }
201 
211 bool QProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
212 {
213  Q_D(const QProxyModel);
214  return d->model->setData(setSourceModel(index), value, role);
215 }
216 
221 QVariant QProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
222 {
223  Q_D(const QProxyModel);
224  return d->model->headerData(section, orientation, role);
225 }
226 
233 bool QProxyModel::setHeaderData(int section, Qt::Orientation orientation,
234  const QVariant &value, int role)
235 {
236  Q_D(const QProxyModel);
237  return d->model->setHeaderData(section, orientation, value, role);
238 }
239 
244 {
245  Q_D(const QProxyModel);
246  return d->model->mimeTypes();
247 }
248 
253 {
254  Q_D(const QProxyModel);
255  QModelIndexList lst;
256  for (int i = 0; i < indexes.count(); ++i)
257  lst.append(setSourceModel(indexes.at(i)));
258  return d->model->mimeData(lst);
259 }
260 
270  int row, int column, const QModelIndex &parent)
271 {
272  Q_D(const QProxyModel);
273  return d->model->dropMimeData(data, action, row, column, setSourceModel(parent));
274 }
275 
285 Qt::DropActions QProxyModel::supportedDropActions() const
286 {
287  Q_D(const QProxyModel);
288  return d->model->supportedDropActions();
289 }
290 
301 bool QProxyModel::insertRows(int row, int count, const QModelIndex &parent)
302 {
303  Q_D(const QProxyModel);
304  return d->model->insertRows(row, count, setSourceModel(parent));
305 }
306 
318 bool QProxyModel::insertColumns(int column, int count, const QModelIndex &parent)
319 {
320  Q_D(const QProxyModel);
321  return d->model->insertColumns(column, count, setSourceModel(parent));
322 }
323 
331 {
332  Q_D(const QProxyModel);
333  d->model->fetchMore(parent);
334 }
335 
339 Qt::ItemFlags QProxyModel::flags(const QModelIndex &index) const
340 {
341  Q_D(const QProxyModel);
342  return d->model->flags(setSourceModel(index));
343 }
344 
351 void QProxyModel::sort(int column, Qt::SortOrder order)
352 {
353  Q_D(QProxyModel);
354  d->model->sort(column, order);
355 }
356 
368  int role, const QVariant &value,
369  int hits, Qt::MatchFlags flags) const
370 {
371  Q_D(const QProxyModel);
372  return d->model->match(start, role, value, hits, flags);
373 }
374 
379 {
380  Q_D(const QProxyModel);
381  return d->model->span(setSourceModel(index));
382 }
383 
387 {
388  Q_D(QProxyModel);
389  return d->model->submit();
390 }
391 
395 {
396  Q_D(QProxyModel);
397  d->model->revert();
398 }
399 
408 {
409  QModelIndex proxy_index = source_index;
410  if (proxy_index.isValid())
411  proxy_index.m = this;
412  return proxy_index;
413 }
414 
423 {
424  Q_D(const QProxyModel);
425  QModelIndex source_index = proxy_index;
426  source_index.m = d->model;
427  return source_index;
428 }
429 
438 {
440  this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
442  this, SIGNAL(headerDataChanged(Qt::Orientation,int,int))); // signal to signal
444  this, SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int)));
445  connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
446  this, SLOT(_q_sourceRowsInserted(QModelIndex,int,int)));
448  this, SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int)));
449  connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
450  this, SLOT(_q_sourceRowsRemoved(QModelIndex,int,int)));
452  this, SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int)));
453  connect(model, SIGNAL(columnsInserted(QModelIndex,int,int)),
454  this, SLOT(_q_sourceColumnsInserted(QModelIndex,int,int)));
456  this, SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int)));
457  connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
458  this, SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int)));
459  connect(model, SIGNAL(modelReset()), this, SIGNAL(modelReset())); // signal to signal
460  connect(model, SIGNAL(layoutAboutToBeChanged()), this, SIGNAL(layoutAboutToBeChanged())); // signal to signal
461  connect(model, SIGNAL(layoutChanged()), this, SIGNAL(layoutChanged())); // signal to signal
462 }
463 
472 {
474  this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
476  this, SIGNAL(headerDataChanged(Qt::Orientation,int,int))); // signal to signal
478  this, SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int)));
479  disconnect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
480  this, SLOT(rowsInserted(QModelIndex,int,int)));
482  this, SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int)));
483  disconnect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
484  this, SLOT(_q_sourceRowsRemoved(QModelIndex,int,int)));
486  this, SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int)));
487  disconnect(model, SIGNAL(columnsInserted(QModelIndex,int,int)),
488  this, SLOT(_q_sourceColumnsInserted(QModelIndex,int,int)));
490  this, SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int)));
491  disconnect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
492  this, SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int)));
493  disconnect(model, SIGNAL(modelReset()), this, SIGNAL(modelReset())); // signal to signal
494  disconnect(model, SIGNAL(layoutAboutToBeChanged()), this, SIGNAL(layoutAboutToBeChanged())); // signal to signal
495  disconnect(model, SIGNAL(layoutChanged()), this, SIGNAL(layoutChanged())); // signal to signal
496 }
497 
504 {
505  Q_Q(QProxyModel);
506  emit q->dataChanged(q->setProxyModel(tl), q->setProxyModel(br));
507 }
508 
510 {
511  Q_Q(QProxyModel);
512  q->beginInsertRows(q->setProxyModel(parent), first, last);
513 }
514 
516 {
517  Q_Q(QProxyModel);
518  q->endInsertRows();
519 }
520 
522 {
523  Q_Q(QProxyModel);
524  q->beginRemoveRows(q->setProxyModel(parent), first, last);
525 }
526 
528 {
529  Q_Q(QProxyModel);
530  q->endRemoveRows();
531 }
532 
534 {
535  Q_Q(QProxyModel);
536  q->beginInsertColumns(q->setProxyModel(parent), first, last);
537 }
538 
540 {
541  Q_Q(QProxyModel);
542  q->endInsertColumns();
543 }
544 
546 {
547  Q_Q(QProxyModel);
548  q->beginRemoveColumns(q->setProxyModel(parent), first, last);
549 }
550 
551 
553 {
554  Q_Q(QProxyModel);
555  q->endRemoveColumns();
556 }
557 
559 
560 #include "moc_qproxymodel.cpp"
561 
562 #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
void _q_sourceColumnsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Returns the data stored in the item with the given index under the specified role.
virtual void setModel(QAbstractItemModel *model)
Sets the given model to be processed by the proxy model.
#define SLOT(a)
Definition: qobjectdefs.h:226
bool insertColumns(int column, int count, const QModelIndex &parent=QModelIndex())
Inserts count columns into the model, creating new items as children of the given parent...
void columnsInserted(const QModelIndex &parent, int first, int last)
This signal is emitted after columns have been inserted into the model.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the model index with the given row, column, and parent.
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void _q_sourceRowsRemoved(const QModelIndex &parent, int first, int last)
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
void _q_sourceColumnsAboutToBeInserted(const QModelIndex &parent, int first, int last)
QModelIndex setSourceModel(const QModelIndex &proxy_index) const
Change the model pointer in the given proxy_index to point to the source model.
void _q_sourceColumnsInserted(const QModelIndex &parent, int first, int last)
bool submit()
Lets the model know that it should submit cached information to permanent storage.
#define Q_Q(Class)
Definition: qglobal.h:2483
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns for the given parent.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last)
This signal is emitted just before columns are inserted into the model.
SortOrder
Definition: qnamespace.h:189
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Sorts the child items in the specified column according to the sort order defined by order...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void _q_sourceRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
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
The QProxyModel class provides support for processing data passed between another model and a view...
Definition: qproxymodel.h:57
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last)
This signal is emitted just before rows are inserted into the model.
void fetchMore(const QModelIndex &parent)
Fetches more child items of the given parent.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void _q_sourceColumnsRemoved(const QModelIndex &parent, int first, int last)
bool hasChildren(const QModelIndex &parent=QModelIndex()) const
Returns true if the item corresponding to the parent index has child items; otherwise returns false...
Qt::ItemFlags flags(const QModelIndex &index) const
Returns the item flags for the given index.
QModelIndex setProxyModel(const QModelIndex &source_index) const
Change the model pointer in the given source_index to point to the proxy model.
const QAbstractItemModel * m
DropAction
Definition: qnamespace.h:1597
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
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.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
The QAbstractItemModel class provides the abstract interface for item model classes.
void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted just before columns are removed from the model.
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
Inserts count rows into the model, creating new items as children of the given parent.
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
void revert()
Lets the model know that it should discard cached information.
void _q_sourceRowsAboutToBeInserted(const QModelIndex &parent, int first, int last)
QStringList mimeTypes() const
Returns a list of MIME types that are supported by the model.
void _q_sourceRowsInserted(const QModelIndex &parent, int first, int last)
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Returns true if the model accepts the data dropped onto an attached view for the specified action; ot...
QAbstractItemModel * model() const
Returns the model that contains the data that is available through the proxy model.
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
QProxyModel(QObject *parent=0)
Constructs a proxy model with the given parent.
Definition: qproxymodel.cpp:87
void rowsInserted(const QModelIndex &parent, int first, int last)
This signal is emitted after rows have been inserted into the model.
QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
Returns a list of model indexes that each contain the given value for the role specified.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QMimeData * mimeData(const QModelIndexList &indexes) const
Returns MIME data for the specified indexes in the model.
void disconnectFromModel(const QAbstractItemModel *model) const
Disconnect from all the signals emitted by the given model.
The QModelIndex class is used to locate data in a data model.
void modelReset()
This signal is emitted when reset() is called, after the model&#39;s internal state (e.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns the data stored in the section of the header with specified orientation under the given role...
void _q_sourceDataChanged(const QModelIndex &tl, const QModelIndex &br)
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
Sets the role data in the section of the header with the specified orientation to the value given...
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted just before rows are removed from the model.
QSize span(const QModelIndex &index) const
Returns the size of the item that corresponds to the specified index.
Orientation
Definition: qnamespace.h:174
void rowsRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted after rows have been removed from the model.
~QProxyModel()
Destroys the proxy model.
int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows for the given parent.
void connectToModel(const QAbstractItemModel *model) const
Connect to all the signals emitted by given model.
void columnsRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted after columns have been removed from the model.
Qt::DropActions supportedDropActions() const
Returns the drop actions that are supported by the model; this is a combination of the individual act...