itemmodels/qabstractproxymodel.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 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 -
52QT_BEGIN_NAMESPACE -
53 -
54/*! -
55 \since 4.1 -
56 \class QAbstractProxyModel -
57 \brief The QAbstractProxyModel class provides a base class for proxy item -
58 models that can do sorting, filtering or other data processing tasks. -
59 \ingroup model-view -
60 \inmodule QtCore -
61 -
62 This class defines the standard interface that proxy models must use to be -
63 able to interoperate correctly with other model/view components. It is not -
64 supposed to be instantiated directly. -
65 -
66 All standard proxy models are derived from the QAbstractProxyModel class. -
67 If you need to create a new proxy model class, it is usually better to -
68 subclass an existing class that provides the closest behavior to the one -
69 you want to provide. -
70 -
71 Proxy models that filter or sort items of data from a source model should -
72 be created by using or subclassing QSortFilterProxyModel. -
73 -
74 To subclass QAbstractProxyModel, you need to implement mapFromSource() and -
75 mapToSource(). The mapSelectionFromSource() and mapSelectionToSource() -
76 functions only need to be reimplemented if you need a behavior different -
77 from the default behavior. -
78 -
79 \note If the source model is deleted or no source model is specified, the -
80 proxy model operates on a empty placeholder model. -
81 -
82 \sa QSortFilterProxyModel, QAbstractItemModel, {Model/View Programming} -
83*/ -
84 -
85/*! -
86 \property QAbstractProxyModel::sourceModel -
87 -
88 \brief the source model this proxy model. -
89*/ -
90 -
91//detects the deletion of the source model -
92void QAbstractProxyModelPrivate::_q_sourceModelDestroyed() -
93{ -
94 model = QAbstractItemModelPrivate::staticEmptyModel();
executed (the execution status of this line is deduced): model = QAbstractItemModelPrivate::staticEmptyModel();
-
95}
executed: }
Execution Count:220
220
96 -
97/*! -
98 Constructs a proxy model with the given \a parent. -
99*/ -
100 -
101QAbstractProxyModel::QAbstractProxyModel(QObject *parent) -
102 :QAbstractItemModel(*new QAbstractProxyModelPrivate, parent) -
103{ -
104 setSourceModel(QAbstractItemModelPrivate::staticEmptyModel());
executed (the execution status of this line is deduced): setSourceModel(QAbstractItemModelPrivate::staticEmptyModel());
-
105}
executed: }
Execution Count:17
17
106 -
107/*! -
108 \internal -
109*/ -
110 -
111QAbstractProxyModel::QAbstractProxyModel(QAbstractProxyModelPrivate &dd, QObject *parent) -
112 : QAbstractItemModel(dd, parent) -
113{ -
114 setSourceModel(QAbstractItemModelPrivate::staticEmptyModel());
executed (the execution status of this line is deduced): setSourceModel(QAbstractItemModelPrivate::staticEmptyModel());
-
115}
executed: }
Execution Count:640
640
116 -
117/*! -
118 Destroys the proxy model. -
119*/ -
120QAbstractProxyModel::~QAbstractProxyModel() -
121{ -
122 -
123} -
124 -
125/*! -
126 Sets the given \a sourceModel to be processed by the proxy model. -
127 -
128 Subclasses should call beginResetModel() at the beginning of the method, -
129 disconnect from the old model, call this method, connect to the new model, -
130 and call endResetModel(). -
131*/ -
132void QAbstractProxyModel::setSourceModel(QAbstractItemModel *sourceModel) -
133{ -
134 Q_D(QAbstractProxyModel);
executed (the execution status of this line is deduced): QAbstractProxyModelPrivate * const d = d_func();
-
135 if (sourceModel != d->model) {
evaluated: sourceModel != d->model
TRUEFALSE
yes
Evaluation Count:1687
yes
Evaluation Count:1
1-1687
136 if (d->model)
evaluated: d->model
TRUEFALSE
yes
Evaluation Count:1030
yes
Evaluation Count:657
657-1030
137 disconnect(d->model, SIGNAL(destroyed()), this, SLOT(_q_sourceModelDestroyed()));
executed: disconnect(d->model, "2""destroyed()", this, "1""_q_sourceModelDestroyed()");
Execution Count:1030
1030
138 -
139 if (sourceModel) {
evaluated: sourceModel
TRUEFALSE
yes
Evaluation Count:1455
yes
Evaluation Count:232
232-1455
140 d->model = sourceModel;
executed (the execution status of this line is deduced): d->model = sourceModel;
-
141 connect(d->model, SIGNAL(destroyed()), this, SLOT(_q_sourceModelDestroyed()));
executed (the execution status of this line is deduced): connect(d->model, "2""destroyed()", this, "1""_q_sourceModelDestroyed()");
-
142 } else {
executed: }
Execution Count:1455
1455
143 d->model = QAbstractItemModelPrivate::staticEmptyModel();
executed (the execution status of this line is deduced): d->model = QAbstractItemModelPrivate::staticEmptyModel();
-
144 }
executed: }
Execution Count:232
232
145 d->roleNames = d->model->roleNames();
executed (the execution status of this line is deduced): d->roleNames = d->model->roleNames();
-
146 emit sourceModelChanged(QPrivateSignal());
executed (the execution status of this line is deduced): sourceModelChanged(QPrivateSignal());
-
147 }
executed: }
Execution Count:1687
1687
148}
executed: }
Execution Count:1688
1688
149 -
150/*! -
151 Returns the model that contains the data that is available through the proxy model. -
152*/ -
153QAbstractItemModel *QAbstractProxyModel::sourceModel() const -
154{ -
155 Q_D(const QAbstractProxyModel);
executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
156 if (d->model == QAbstractItemModelPrivate::staticEmptyModel())
evaluated: d->model == QAbstractItemModelPrivate::staticEmptyModel()
TRUEFALSE
yes
Evaluation Count:1290
yes
Evaluation Count:15625
1290-15625
157 return 0;
executed: return 0;
Execution Count:1290
1290
158 return d->model;
executed: return d->model;
Execution Count:15625
15625
159} -
160 -
161/*! -
162 \reimp -
163 */ -
164bool QAbstractProxyModel::submit() -
165{ -
166 Q_D(QAbstractProxyModel);
executed (the execution status of this line is deduced): QAbstractProxyModelPrivate * const d = d_func();
-
167 return d->model->submit();
executed: return d->model->submit();
Execution Count:181
181
168} -
169 -
170/*! -
171 \reimp -
172 */ -
173void QAbstractProxyModel::revert() -
174{ -
175 Q_D(QAbstractProxyModel);
executed (the execution status of this line is deduced): QAbstractProxyModelPrivate * const d = d_func();
-
176 d->model->revert();
executed (the execution status of this line is deduced): d->model->revert();
-
177}
executed: }
Execution Count:5
5
178 -
179 -
180/*! -
181 \fn QModelIndex QAbstractProxyModel::mapToSource(const QModelIndex &proxyIndex) const -
182 -
183 Reimplement this function to return the model index in the source model that -
184 corresponds to the \a proxyIndex in the proxy model. -
185 -
186 \sa mapFromSource() -
187*/ -
188 -
189/*! -
190 \fn QModelIndex QAbstractProxyModel::mapFromSource(const QModelIndex &sourceIndex) const -
191 -
192 Reimplement this function to return the model index in the proxy model that -
193 corresponds to the \a sourceIndex from the source model. -
194 -
195 \sa mapToSource() -
196*/ -
197 -
198/*! -
199 Returns a source selection mapped from the specified \a proxySelection. -
200 -
201 Reimplement this method to map proxy selections to source selections. -
202 */ -
203QItemSelection QAbstractProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) const -
204{ -
205 QModelIndexList proxyIndexes = proxySelection.indexes();
executed (the execution status of this line is deduced): QModelIndexList proxyIndexes = proxySelection.indexes();
-
206 QItemSelection sourceSelection;
executed (the execution status of this line is deduced): QItemSelection sourceSelection;
-
207 for (int i = 0; i < proxyIndexes.size(); ++i) {
partially evaluated: i < proxyIndexes.size()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:3
0-3
208 const QModelIndex proxyIdx = mapToSource(proxyIndexes.at(i));
never executed (the execution status of this line is deduced): const QModelIndex proxyIdx = mapToSource(proxyIndexes.at(i));
-
209 if (!proxyIdx.isValid())
never evaluated: !proxyIdx.isValid()
0
210 continue;
never executed: continue;
0
211 sourceSelection << QItemSelectionRange(proxyIdx);
never executed (the execution status of this line is deduced): sourceSelection << QItemSelectionRange(proxyIdx);
-
212 }
never executed: }
0
213 return sourceSelection;
executed: return sourceSelection;
Execution Count:3
3
214} -
215 -
216/*! -
217 Returns a proxy selection mapped from the specified \a sourceSelection. -
218 -
219 Reimplement this method to map source selections to proxy selections. -
220*/ -
221QItemSelection QAbstractProxyModel::mapSelectionFromSource(const QItemSelection &sourceSelection) const -
222{ -
223 QModelIndexList sourceIndexes = sourceSelection.indexes();
executed (the execution status of this line is deduced): QModelIndexList sourceIndexes = sourceSelection.indexes();
-
224 QItemSelection proxySelection;
executed (the execution status of this line is deduced): QItemSelection proxySelection;
-
225 for (int i = 0; i < sourceIndexes.size(); ++i) {
evaluated: i < sourceIndexes.size()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:4
3-4
226 const QModelIndex srcIdx = mapFromSource(sourceIndexes.at(i));
executed (the execution status of this line is deduced): const QModelIndex srcIdx = mapFromSource(sourceIndexes.at(i));
-
227 if (!srcIdx.isValid())
evaluated: !srcIdx.isValid()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:1
1-2
228 continue;
executed: continue;
Execution Count:2
2
229 proxySelection << QItemSelectionRange(srcIdx);
executed (the execution status of this line is deduced): proxySelection << QItemSelectionRange(srcIdx);
-
230 }
executed: }
Execution Count:1
1
231 return proxySelection;
executed: return proxySelection;
Execution Count:4
4
232} -
233 -
234/*! -
235 \reimp -
236 */ -
237QVariant QAbstractProxyModel::data(const QModelIndex &proxyIndex, int role) const -
238{ -
239 Q_D(const QAbstractProxyModel);
executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
240 return d->model->data(mapToSource(proxyIndex), role);
executed: return d->model->data(mapToSource(proxyIndex), role);
Execution Count:99
99
241} -
242 -
243/*! -
244 \reimp -
245 */ -
246QVariant QAbstractProxyModel::headerData(int section, Qt::Orientation orientation, int role) const -
247{ -
248 Q_D(const QAbstractProxyModel);
executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
249 int sourceSection;
executed (the execution status of this line is deduced): int sourceSection;
-
250 if (orientation == Qt::Horizontal) {
evaluated: orientation == Qt::Horizontal
TRUEFALSE
yes
Evaluation Count:306
yes
Evaluation Count:1764
306-1764
251 const QModelIndex proxyIndex = index(0, section);
executed (the execution status of this line is deduced): const QModelIndex proxyIndex = index(0, section);
-
252 sourceSection = mapToSource(proxyIndex).column();
executed (the execution status of this line is deduced): sourceSection = mapToSource(proxyIndex).column();
-
253 } else {
executed: }
Execution Count:306
306
254 const QModelIndex proxyIndex = index(section, 0);
executed (the execution status of this line is deduced): const QModelIndex proxyIndex = index(section, 0);
-
255 sourceSection = mapToSource(proxyIndex).row();
executed (the execution status of this line is deduced): sourceSection = mapToSource(proxyIndex).row();
-
256 }
executed: }
Execution Count:1764
1764
257 return d->model->headerData(sourceSection, orientation, role);
executed: return d->model->headerData(sourceSection, orientation, role);
Execution Count:2070
2070
258} -
259 -
260/*! -
261 \reimp -
262 */ -
263QMap<int, QVariant> QAbstractProxyModel::itemData(const QModelIndex &proxyIndex) const -
264{ -
265 Q_D(const QAbstractProxyModel);
executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
266 return d->model->itemData(mapToSource(proxyIndex));
executed: return d->model->itemData(mapToSource(proxyIndex));
Execution Count:43
43
267} -
268 -
269/*! -
270 \reimp -
271 */ -
272Qt::ItemFlags QAbstractProxyModel::flags(const QModelIndex &index) const -
273{ -
274 Q_D(const QAbstractProxyModel);
executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
275 return d->model->flags(mapToSource(index));
executed: return d->model->flags(mapToSource(index));
Execution Count:355
355
276} -
277 -
278/*! -
279 \reimp -
280 */ -
281bool QAbstractProxyModel::setData(const QModelIndex &index, const QVariant &value, int role) -
282{ -
283 Q_D(QAbstractProxyModel);
never executed (the execution status of this line is deduced): QAbstractProxyModelPrivate * const d = d_func();
-
284 return d->model->setData(mapToSource(index), value, role);
never executed: return d->model->setData(mapToSource(index), value, role);
0
285} -
286 -
287/*! -
288 \reimp -
289 */ -
290bool QAbstractProxyModel::setItemData(const QModelIndex &index, const QMap< int, QVariant >& roles) -
291{ -
292 Q_D(QAbstractProxyModel);
executed (the execution status of this line is deduced): QAbstractProxyModelPrivate * const d = d_func();
-
293 return d->model->setItemData(mapToSource(index), roles);
executed: return d->model->setItemData(mapToSource(index), roles);
Execution Count:3
3
294} -
295 -
296/*! -
297 \reimp -
298 */ -
299bool QAbstractProxyModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) -
300{ -
301 Q_D(QAbstractProxyModel);
executed (the execution status of this line is deduced): QAbstractProxyModelPrivate * const d = d_func();
-
302 int sourceSection;
executed (the execution status of this line is deduced): int sourceSection;
-
303 if (orientation == Qt::Horizontal) {
evaluated: orientation == Qt::Horizontal
TRUEFALSE
yes
Evaluation Count:78
yes
Evaluation Count:10
10-78
304 const QModelIndex proxyIndex = index(0, section);
executed (the execution status of this line is deduced): const QModelIndex proxyIndex = index(0, section);
-
305 sourceSection = mapToSource(proxyIndex).column();
executed (the execution status of this line is deduced): sourceSection = mapToSource(proxyIndex).column();
-
306 } else {
executed: }
Execution Count:78
78
307 const QModelIndex proxyIndex = index(section, 0);
executed (the execution status of this line is deduced): const QModelIndex proxyIndex = index(section, 0);
-
308 sourceSection = mapToSource(proxyIndex).row();
executed (the execution status of this line is deduced): sourceSection = mapToSource(proxyIndex).row();
-
309 }
executed: }
Execution Count:10
10
310 return d->model->setHeaderData(sourceSection, orientation, value, role);
executed: return d->model->setHeaderData(sourceSection, orientation, value, role);
Execution Count:88
88
311} -
312 -
313/*! -
314 \reimp -
315 */ -
316QModelIndex QAbstractProxyModel::buddy(const QModelIndex &index) const -
317{ -
318 Q_D(const QAbstractProxyModel);
executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
319 return mapFromSource(d->model->buddy(mapToSource(index)));
executed: return mapFromSource(d->model->buddy(mapToSource(index)));
Execution Count:33
33
320} -
321 -
322/*! -
323 \reimp -
324 */ -
325bool QAbstractProxyModel::canFetchMore(const QModelIndex &parent) const -
326{ -
327 Q_D(const QAbstractProxyModel);
executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
328 return d->model->canFetchMore(mapToSource(parent));
executed: return d->model->canFetchMore(mapToSource(parent));
Execution Count:29
29
329} -
330 -
331/*! -
332 \reimp -
333 */ -
334void QAbstractProxyModel::fetchMore(const QModelIndex &parent) -
335{ -
336 Q_D(QAbstractProxyModel);
never executed (the execution status of this line is deduced): QAbstractProxyModelPrivate * const d = d_func();
-
337 d->model->fetchMore(mapToSource(parent));
never executed (the execution status of this line is deduced): d->model->fetchMore(mapToSource(parent));
-
338}
never executed: }
0
339 -
340/*! -
341 \reimp -
342 */ -
343void QAbstractProxyModel::sort(int column, Qt::SortOrder order) -
344{ -
345 Q_D(QAbstractProxyModel);
never executed (the execution status of this line is deduced): QAbstractProxyModelPrivate * const d = d_func();
-
346 d->model->sort(column, order);
never executed (the execution status of this line is deduced): d->model->sort(column, order);
-
347}
never executed: }
0
348 -
349/*! -
350 \reimp -
351 */ -
352QSize QAbstractProxyModel::span(const QModelIndex &index) const -
353{ -
354 Q_D(const QAbstractProxyModel);
never executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
355 return d->model->span(mapToSource(index));
never executed: return d->model->span(mapToSource(index));
0
356} -
357 -
358/*! -
359 \reimp -
360 */ -
361bool QAbstractProxyModel::hasChildren(const QModelIndex &parent) const -
362{ -
363 Q_D(const QAbstractProxyModel);
executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
364 return d->model->hasChildren(mapToSource(parent));
executed: return d->model->hasChildren(mapToSource(parent));
Execution Count:97
97
365} -
366 -
367/*! -
368 \reimp -
369 */ -
370QModelIndex QAbstractProxyModel::sibling(int row, int column, const QModelIndex &idx) const -
371{ -
372 Q_D(const QAbstractProxyModel);
executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
373 return mapFromSource(d->model->sibling(row, column, mapToSource(idx)));
executed: return mapFromSource(d->model->sibling(row, column, mapToSource(idx)));
Execution Count:54
54
374} -
375 -
376/*! -
377 \reimp -
378 */ -
379QMimeData* QAbstractProxyModel::mimeData(const QModelIndexList &indexes) const -
380{ -
381 Q_D(const QAbstractProxyModel);
never executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
382 QModelIndexList list;
never executed (the execution status of this line is deduced): QModelIndexList list;
-
383 foreach(const QModelIndex &index, indexes)
never executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(indexes)> _container_(indexes); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QModelIndex &index = *_container_.i;; __extension__ ({--_container_.brk; break;}))
-
384 list << mapToSource(index);
never executed: list << mapToSource(index);
0
385 return d->model->mimeData(list);
never executed: return d->model->mimeData(list);
0
386} -
387 -
388/*! -
389 \reimp -
390 */ -
391QStringList QAbstractProxyModel::mimeTypes() const -
392{ -
393 Q_D(const QAbstractProxyModel);
never executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
394 return d->model->mimeTypes();
never executed: return d->model->mimeTypes();
0
395} -
396 -
397/*! -
398 \reimp -
399 */ -
400Qt::DropActions QAbstractProxyModel::supportedDropActions() const -
401{ -
402 Q_D(const QAbstractProxyModel);
never executed (the execution status of this line is deduced): const QAbstractProxyModelPrivate * const d = d_func();
-
403 return d->model->supportedDropActions();
never executed: return d->model->supportedDropActions();
0
404} -
405 -
406QT_END_NAMESPACE -
407 -
408#include "moc_qabstractproxymodel.cpp" -
409 -
410#endif // QT_NO_PROXYMODEL -
411 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial