itemviews/qdatawidgetmapper.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 "qdatawidgetmapper.h" -
43 -
44#ifndef QT_NO_DATAWIDGETMAPPER -
45 -
46#include "qabstractitemmodel.h" -
47#include "qitemdelegate.h" -
48#include "qmetaobject.h" -
49#include "qwidget.h" -
50#include "private/qobject_p.h" -
51#include "private/qabstractitemmodel_p.h" -
52 -
53QT_BEGIN_NAMESPACE -
54 -
55class QDataWidgetMapperPrivate: public QObjectPrivate -
56{ -
57public: -
58 Q_DECLARE_PUBLIC(QDataWidgetMapper) -
59 -
60 QDataWidgetMapperPrivate() -
61 : model(QAbstractItemModelPrivate::staticEmptyModel()), delegate(0), -
62 orientation(Qt::Horizontal), submitPolicy(QDataWidgetMapper::AutoSubmit) -
63 { -
64 }
executed: }
Execution Count:9
9
65 -
66 QAbstractItemModel *model; -
67 QAbstractItemDelegate *delegate; -
68 Qt::Orientation orientation; -
69 QDataWidgetMapper::SubmitPolicy submitPolicy; -
70 QPersistentModelIndex rootIndex; -
71 QPersistentModelIndex currentTopLeft; -
72 -
73 inline int itemCount() -
74 { -
75 return orientation == Qt::Horizontal
executed: return orientation == Qt::Horizontal ? model->rowCount(rootIndex) : model->columnCount(rootIndex);
Execution Count:32
32
76 ? model->rowCount(rootIndex)
executed: return orientation == Qt::Horizontal ? model->rowCount(rootIndex) : model->columnCount(rootIndex);
Execution Count:32
32
77 : model->columnCount(rootIndex);
executed: return orientation == Qt::Horizontal ? model->rowCount(rootIndex) : model->columnCount(rootIndex);
Execution Count:32
32
78 } -
79 -
80 inline int currentIdx() const -
81 { -
82 return orientation == Qt::Horizontal ? currentTopLeft.row() : currentTopLeft.column();
executed: return orientation == Qt::Horizontal ? currentTopLeft.row() : currentTopLeft.column();
Execution Count:85
85
83 } -
84 -
85 inline QModelIndex indexAt(int itemPos) -
86 { -
87 return orientation == Qt::Horizontal
executed: return orientation == Qt::Horizontal ? model->index(currentIdx(), itemPos, rootIndex) : model->index(itemPos, currentIdx(), rootIndex);
Execution Count:78
78
88 ? model->index(currentIdx(), itemPos, rootIndex)
executed: return orientation == Qt::Horizontal ? model->index(currentIdx(), itemPos, rootIndex) : model->index(itemPos, currentIdx(), rootIndex);
Execution Count:78
78
89 : model->index(itemPos, currentIdx(), rootIndex);
executed: return orientation == Qt::Horizontal ? model->index(currentIdx(), itemPos, rootIndex) : model->index(itemPos, currentIdx(), rootIndex);
Execution Count:78
78
90 } -
91 -
92 inline void flipEventFilters(QAbstractItemDelegate *oldDelegate, -
93 QAbstractItemDelegate *newDelegate) -
94 { -
95 for (int i = 0; i < widgetMap.count(); ++i) {
partially evaluated: i < widgetMap.count()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
96 QWidget *w = widgetMap.at(i).widget;
never executed (the execution status of this line is deduced): QWidget *w = widgetMap.at(i).widget;
-
97 if (!w)
never evaluated: !w
0
98 continue;
never executed: continue;
0
99 w->removeEventFilter(oldDelegate);
never executed (the execution status of this line is deduced): w->removeEventFilter(oldDelegate);
-
100 w->installEventFilter(newDelegate);
never executed (the execution status of this line is deduced): w->installEventFilter(newDelegate);
-
101 }
never executed: }
0
102 }
executed: }
Execution Count:9
9
103 -
104 void populate(); -
105 -
106 // private slots -
107 void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &); -
108 void _q_commitData(QWidget *); -
109 void _q_closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint); -
110 void _q_modelDestroyed(); -
111 -
112 struct WidgetMapper -
113 { -
114 inline WidgetMapper(QWidget *w = 0, int c = 0, const QModelIndex &i = QModelIndex()) -
115 : widget(w), section(c), currentIndex(i) {}
executed: }
Execution Count:15
15
116 inline WidgetMapper(QWidget *w, int c, const QModelIndex &i, const QByteArray &p) -
117 : widget(w), section(c), currentIndex(i), property(p) {}
executed: }
Execution Count:4
4
118 -
119 QPointer<QWidget> widget; -
120 int section; -
121 QPersistentModelIndex currentIndex; -
122 QByteArray property; -
123 }; -
124 -
125 void populate(WidgetMapper &m); -
126 int findWidget(QWidget *w) const; -
127 -
128 bool commit(const WidgetMapper &m); -
129 -
130 QList<WidgetMapper> widgetMap; -
131}; -
132 -
133int QDataWidgetMapperPrivate::findWidget(QWidget *w) const -
134{ -
135 for (int i = 0; i < widgetMap.count(); ++i) {
evaluated: i < widgetMap.count()
TRUEFALSE
yes
Evaluation Count:17
yes
Evaluation Count:17
17
136 if (widgetMap.at(i).widget == w)
evaluated: widgetMap.at(i).widget == w
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:13
4-13
137 return i;
executed: return i;
Execution Count:4
4
138 }
executed: }
Execution Count:13
13
139 return -1;
executed: return -1;
Execution Count:17
17
140} -
141 -
142bool QDataWidgetMapperPrivate::commit(const WidgetMapper &m) -
143{ -
144 if (m.widget.isNull())
partially evaluated: m.widget.isNull()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:14
0-14
145 return true; // just ignore
never executed: return true;
0
146 -
147 if (!m.currentIndex.isValid())
partially evaluated: !m.currentIndex.isValid()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:14
0-14
148 return false;
never executed: return false;
0
149 -
150 // Create copy to avoid passing the widget mappers data -
151 QModelIndex idx = m.currentIndex;
executed (the execution status of this line is deduced): QModelIndex idx = m.currentIndex;
-
152 if (m.property.isEmpty())
evaluated: m.property.isEmpty()
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:4
4-10
153 delegate->setModelData(m.widget, model, idx);
executed: delegate->setModelData(m.widget, model, idx);
Execution Count:10
10
154 else -
155 model->setData(idx, m.widget->property(m.property), Qt::EditRole);
executed: model->setData(idx, m.widget->property(m.property), Qt::EditRole);
Execution Count:4
4
156 -
157 return true;
executed: return true;
Execution Count:14
14
158} -
159 -
160void QDataWidgetMapperPrivate::populate(WidgetMapper &m) -
161{ -
162 if (m.widget.isNull())
evaluated: m.widget.isNull()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:59
1-59
163 return;
executed: return;
Execution Count:1
1
164 -
165 m.currentIndex = indexAt(m.section);
executed (the execution status of this line is deduced): m.currentIndex = indexAt(m.section);
-
166 if (m.property.isEmpty())
evaluated: m.property.isEmpty()
TRUEFALSE
yes
Evaluation Count:48
yes
Evaluation Count:11
11-48
167 delegate->setEditorData(m.widget, m.currentIndex);
executed: delegate->setEditorData(m.widget, m.currentIndex);
Execution Count:48
48
168 else -
169 m.widget->setProperty(m.property, m.currentIndex.data(Qt::EditRole));
executed: m.widget->setProperty(m.property, m.currentIndex.data(Qt::EditRole));
Execution Count:11
11
170} -
171 -
172void QDataWidgetMapperPrivate::populate() -
173{ -
174 for (int i = 0; i < widgetMap.count(); ++i)
evaluated: i < widgetMap.count()
TRUEFALSE
yes
Evaluation Count:46
yes
Evaluation Count:25
25-46
175 populate(widgetMap[i]);
executed: populate(widgetMap[i]);
Execution Count:46
46
176}
executed: }
Execution Count:25
25
177 -
178static bool qContainsIndex(const QModelIndex &idx, const QModelIndex &topLeft, -
179 const QModelIndex &bottomRight) -
180{ -
181 return idx.row() >= topLeft.row() && idx.row() <= bottomRight.row()
executed: return idx.row() >= topLeft.row() && idx.row() <= bottomRight.row() && idx.column() >= topLeft.column() && idx.column() <= bottomRight.column();
Execution Count:28
28
182 && idx.column() >= topLeft.column() && idx.column() <= bottomRight.column();
executed: return idx.row() >= topLeft.row() && idx.row() <= bottomRight.row() && idx.column() >= topLeft.column() && idx.column() <= bottomRight.column();
Execution Count:28
28
183} -
184 -
185void QDataWidgetMapperPrivate::_q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &) -
186{ -
187 if (topLeft.parent() != rootIndex)
partially evaluated: topLeft.parent() != rootIndex
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:11
0-11
188 return; // not in our hierarchy
never executed: return;
0
189 -
190 for (int i = 0; i < widgetMap.count(); ++i) {
evaluated: i < widgetMap.count()
TRUEFALSE
yes
Evaluation Count:28
yes
Evaluation Count:11
11-28
191 WidgetMapper &m = widgetMap[i];
executed (the execution status of this line is deduced): WidgetMapper &m = widgetMap[i];
-
192 if (qContainsIndex(m.currentIndex, topLeft, bottomRight))
evaluated: qContainsIndex(m.currentIndex, topLeft, bottomRight)
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:14
14
193 populate(m);
executed: populate(m);
Execution Count:14
14
194 }
executed: }
Execution Count:28
28
195}
executed: }
Execution Count:11
11
196 -
197void QDataWidgetMapperPrivate::_q_commitData(QWidget *w) -
198{ -
199 if (submitPolicy == QDataWidgetMapper::ManualSubmit)
never evaluated: submitPolicy == QDataWidgetMapper::ManualSubmit
0
200 return;
never executed: return;
0
201 -
202 int idx = findWidget(w);
never executed (the execution status of this line is deduced): int idx = findWidget(w);
-
203 if (idx == -1)
never evaluated: idx == -1
0
204 return; // not our widget
never executed: return;
0
205 -
206 commit(widgetMap.at(idx));
never executed (the execution status of this line is deduced): commit(widgetMap.at(idx));
-
207}
never executed: }
0
208 -
209class QFocusHelper: public QWidget -
210{ -
211public: -
212 bool focusNextPrevChild(bool next) -
213 { -
214 return QWidget::focusNextPrevChild(next);
never executed: return QWidget::focusNextPrevChild(next);
0
215 } -
216 -
217 static inline void focusNextPrevChild(QWidget *w, bool next) -
218 { -
219 static_cast<QFocusHelper *>(w)->focusNextPrevChild(next);
never executed (the execution status of this line is deduced): static_cast<QFocusHelper *>(w)->focusNextPrevChild(next);
-
220 }
never executed: }
0
221}; -
222 -
223void QDataWidgetMapperPrivate::_q_closeEditor(QWidget *w, QAbstractItemDelegate::EndEditHint hint) -
224{ -
225 int idx = findWidget(w);
never executed (the execution status of this line is deduced): int idx = findWidget(w);
-
226 if (idx == -1)
never evaluated: idx == -1
0
227 return; // not our widget
never executed: return;
0
228 -
229 switch (hint) { -
230 case QAbstractItemDelegate::RevertModelCache: { -
231 populate(widgetMap[idx]);
never executed (the execution status of this line is deduced): populate(widgetMap[idx]);
-
232 break; }
never executed: break;
0
233 case QAbstractItemDelegate::EditNextItem: -
234 QFocusHelper::focusNextPrevChild(w, true);
never executed (the execution status of this line is deduced): QFocusHelper::focusNextPrevChild(w, true);
-
235 break;
never executed: break;
0
236 case QAbstractItemDelegate::EditPreviousItem: -
237 QFocusHelper::focusNextPrevChild(w, false);
never executed (the execution status of this line is deduced): QFocusHelper::focusNextPrevChild(w, false);
-
238 break;
never executed: break;
0
239 case QAbstractItemDelegate::SubmitModelCache: -
240 case QAbstractItemDelegate::NoHint: -
241 // nothing -
242 break;
never executed: break;
0
243 } -
244}
never executed: }
0
245 -
246void QDataWidgetMapperPrivate::_q_modelDestroyed() -
247{ -
248 Q_Q(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapper * const q = q_func();
-
249 -
250 model = 0;
executed (the execution status of this line is deduced): model = 0;
-
251 q->setModel(QAbstractItemModelPrivate::staticEmptyModel());
executed (the execution status of this line is deduced): q->setModel(QAbstractItemModelPrivate::staticEmptyModel());
-
252}
executed: }
Execution Count:1
1
253 -
254/*! -
255 \class QDataWidgetMapper -
256 \brief The QDataWidgetMapper class provides mapping between a section -
257 of a data model to widgets. -
258 \since 4.2 -
259 \ingroup model-view -
260 \ingroup advanced -
261 \inmodule QtWidgets -
262 -
263 QDataWidgetMapper can be used to create data-aware widgets by mapping -
264 them to sections of an item model. A section is a column of a model -
265 if the orientation is horizontal (the default), otherwise a row. -
266 -
267 Every time the current index changes, each widget is updated with data -
268 from the model via the property specified when its mapping was made. -
269 If the user edits the contents of a widget, the changes are read using -
270 the same property and written back to the model. -
271 By default, each widget's \l{Q_PROPERTY()}{user property} is used to -
272 transfer data between the model and the widget. Since Qt 4.3, an -
273 additional addMapping() function enables a named property to be used -
274 instead of the default user property. -
275 -
276 It is possible to set an item delegate to support custom widgets. By default, -
277 a QItemDelegate is used to synchronize the model with the widgets. -
278 -
279 Let us assume that we have an item model named \c{model} with the following contents: -
280 -
281 \table -
282 \row \li 1 \li Qt Norway \li Oslo -
283 \row \li 2 \li Qt Australia \li Brisbane -
284 \row \li 3 \li Qt USA \li Palo Alto -
285 \row \li 4 \li Qt China \li Beijing -
286 \row \li 5 \li Qt Germany \li Berlin -
287 \endtable -
288 -
289 The following code will map the columns of the model to widgets called \c mySpinBox, -
290 \c myLineEdit and \c{myCountryChooser}: -
291 -
292 \snippet code/src_gui_itemviews_qdatawidgetmapper.cpp 0 -
293 -
294 After the call to toFirst(), \c mySpinBox displays the value \c{1}, \c myLineEdit -
295 displays \c{Qt Norway} and \c myCountryChooser displays \c{Oslo}. The -
296 navigational functions toFirst(), toNext(), toPrevious(), toLast() and setCurrentIndex() -
297 can be used to navigate in the model and update the widgets with contents from -
298 the model. -
299 -
300 The setRootIndex() function enables a particular item in a model to be -
301 specified as the root index - children of this item will be mapped to -
302 the relevant widgets in the user interface. -
303 -
304 QDataWidgetMapper supports two submit policies, \c AutoSubmit and \c{ManualSubmit}. -
305 \c AutoSubmit will update the model as soon as the current widget loses focus, -
306 \c ManualSubmit will not update the model unless submit() is called. \c ManualSubmit -
307 is useful when displaying a dialog that lets the user cancel all modifications. -
308 Also, other views that display the model won't update until the user finishes -
309 all their modifications and submits. -
310 -
311 Note that QDataWidgetMapper keeps track of external modifications. If the contents -
312 of the model are updated in another module of the application, the widgets are -
313 updated as well. -
314 -
315 \sa QAbstractItemModel, QAbstractItemDelegate -
316 */ -
317 -
318/*! \enum QDataWidgetMapper::SubmitPolicy -
319 -
320 This enum describes the possible submit policies a QDataWidgetMapper -
321 supports. -
322 -
323 \value AutoSubmit Whenever a widget loses focus, the widget's current -
324 value is set to the item model. -
325 \value ManualSubmit The model is not updated until submit() is called. -
326 */ -
327 -
328/*! -
329 \fn void QDataWidgetMapper::currentIndexChanged(int index) -
330 -
331 This signal is emitted after the current index has changed and -
332 all widgets were populated with new data. \a index is the new -
333 current index. -
334 -
335 \sa currentIndex(), setCurrentIndex() -
336 */ -
337 -
338/*! -
339 Constructs a new QDataWidgetMapper with parent object \a parent. -
340 By default, the orientation is horizontal and the submit policy -
341 is \c{AutoSubmit}. -
342 -
343 \sa setOrientation(), setSubmitPolicy() -
344 */ -
345QDataWidgetMapper::QDataWidgetMapper(QObject *parent) -
346 : QObject(*new QDataWidgetMapperPrivate, parent) -
347{ -
348 setItemDelegate(new QItemDelegate(this));
executed (the execution status of this line is deduced): setItemDelegate(new QItemDelegate(this));
-
349}
executed: }
Execution Count:9
9
350 -
351/*! -
352 Destroys the object. -
353 */ -
354QDataWidgetMapper::~QDataWidgetMapper() -
355{ -
356} -
357 -
358/*! -
359 Sets the current model to \a model. If another model was set, -
360 all mappings to that old model are cleared. -
361 -
362 \sa model() -
363 */ -
364void QDataWidgetMapper::setModel(QAbstractItemModel *model) -
365{ -
366 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
367 -
368 if (d->model == model)
partially evaluated: d->model == model
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:10
0-10
369 return;
never executed: return;
0
370 -
371 if (d->model) {
evaluated: d->model
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:1
1-9
372 disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), this,
executed (the execution status of this line is deduced): disconnect(d->model, "2""dataChanged(QModelIndex,QModelIndex,QVector<int>)", this,
-
373 SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)));
executed (the execution status of this line is deduced): "1""_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)");
-
374 disconnect(d->model, SIGNAL(destroyed()), this,
executed (the execution status of this line is deduced): disconnect(d->model, "2""destroyed()", this,
-
375 SLOT(_q_modelDestroyed()));
executed (the execution status of this line is deduced): "1""_q_modelDestroyed()");
-
376 }
executed: }
Execution Count:9
9
377 clearMapping();
executed (the execution status of this line is deduced): clearMapping();
-
378 d->rootIndex = QModelIndex();
executed (the execution status of this line is deduced): d->rootIndex = QModelIndex();
-
379 d->currentTopLeft = QModelIndex();
executed (the execution status of this line is deduced): d->currentTopLeft = QModelIndex();
-
380 -
381 d->model = model;
executed (the execution status of this line is deduced): d->model = model;
-
382 -
383 connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
executed (the execution status of this line is deduced): connect(model, "2""dataChanged(QModelIndex,QModelIndex,QVector<int>)",
-
384 SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)));
executed (the execution status of this line is deduced): "1""_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)");
-
385 connect(model, SIGNAL(destroyed()), SLOT(_q_modelDestroyed()));
executed (the execution status of this line is deduced): connect(model, "2""destroyed()", "1""_q_modelDestroyed()");
-
386}
executed: }
Execution Count:10
10
387 -
388/*! -
389 Returns the current model. -
390 -
391 \sa setModel() -
392 */ -
393QAbstractItemModel *QDataWidgetMapper::model() const -
394{ -
395 Q_D(const QDataWidgetMapper);
executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate * const d = d_func();
-
396 return d->model == QAbstractItemModelPrivate::staticEmptyModel()
executed: return d->model == QAbstractItemModelPrivate::staticEmptyModel() ? static_cast<QAbstractItemModel *>(0) : d->model;
Execution Count:3
3
397 ? static_cast<QAbstractItemModel *>(0)
executed: return d->model == QAbstractItemModelPrivate::staticEmptyModel() ? static_cast<QAbstractItemModel *>(0) : d->model;
Execution Count:3
3
398 : d->model;
executed: return d->model == QAbstractItemModelPrivate::staticEmptyModel() ? static_cast<QAbstractItemModel *>(0) : d->model;
Execution Count:3
3
399} -
400 -
401/*! -
402 Sets the item delegate to \a delegate. The delegate will be used to write -
403 data from the model into the widget and from the widget to the model, -
404 using QAbstractItemDelegate::setEditorData() and QAbstractItemDelegate::setModelData(). -
405 -
406 The delegate also decides when to apply data and when to change the editor, -
407 using QAbstractItemDelegate::commitData() and QAbstractItemDelegate::closeEditor(). -
408 -
409 \warning You should not share the same instance of a delegate between widget mappers -
410 or views. Doing so can cause incorrect or unintuitive editing behavior since each -
411 view connected to a given delegate may receive the \l{QAbstractItemDelegate::}{closeEditor()} -
412 signal, and attempt to access, modify or close an editor that has already been closed. -
413 */ -
414void QDataWidgetMapper::setItemDelegate(QAbstractItemDelegate *delegate) -
415{ -
416 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
417 QAbstractItemDelegate *oldDelegate = d->delegate;
executed (the execution status of this line is deduced): QAbstractItemDelegate *oldDelegate = d->delegate;
-
418 if (oldDelegate) {
partially evaluated: oldDelegate
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
419 disconnect(oldDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(_q_commitData(QWidget*)));
never executed (the execution status of this line is deduced): disconnect(oldDelegate, "2""commitData(QWidget*)", this, "1""_q_commitData(QWidget*)");
-
420 disconnect(oldDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
never executed (the execution status of this line is deduced): disconnect(oldDelegate, "2""closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)",
-
421 this, SLOT(_q_closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
never executed (the execution status of this line is deduced): this, "1""_q_closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)");
-
422 }
never executed: }
0
423 -
424 d->delegate = delegate;
executed (the execution status of this line is deduced): d->delegate = delegate;
-
425 -
426 if (delegate) {
partially evaluated: delegate
TRUEFALSE
yes
Evaluation Count:9
no
Evaluation Count:0
0-9
427 connect(delegate, SIGNAL(commitData(QWidget*)), SLOT(_q_commitData(QWidget*)));
executed (the execution status of this line is deduced): connect(delegate, "2""commitData(QWidget*)", "1""_q_commitData(QWidget*)");
-
428 connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
executed (the execution status of this line is deduced): connect(delegate, "2""closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)",
-
429 SLOT(_q_closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
executed (the execution status of this line is deduced): "1""_q_closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)");
-
430 }
executed: }
Execution Count:9
9
431 -
432 d->flipEventFilters(oldDelegate, delegate);
executed (the execution status of this line is deduced): d->flipEventFilters(oldDelegate, delegate);
-
433}
executed: }
Execution Count:9
9
434 -
435/*! -
436 Returns the current item delegate. -
437 */ -
438QAbstractItemDelegate *QDataWidgetMapper::itemDelegate() const -
439{ -
440 Q_D(const QDataWidgetMapper);
never executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate * const d = d_func();
-
441 return d->delegate;
never executed: return d->delegate;
0
442} -
443 -
444/*! -
445 Sets the root item to \a index. This can be used to display -
446 a branch of a tree. Pass an invalid model index to display -
447 the top-most branch. -
448 -
449 \sa rootIndex() -
450 */ -
451void QDataWidgetMapper::setRootIndex(const QModelIndex &index) -
452{ -
453 Q_D(QDataWidgetMapper);
never executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
454 d->rootIndex = index;
never executed (the execution status of this line is deduced): d->rootIndex = index;
-
455}
never executed: }
0
456 -
457/*! -
458 Returns the current root index. -
459 -
460 \sa setRootIndex() -
461*/ -
462QModelIndex QDataWidgetMapper::rootIndex() const -
463{ -
464 Q_D(const QDataWidgetMapper);
never executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate * const d = d_func();
-
465 return QModelIndex(d->rootIndex);
never executed: return QModelIndex(d->rootIndex);
0
466} -
467 -
468/*! -
469 Adds a mapping between a \a widget and a \a section from the model. -
470 The \a section is a column in the model if the orientation is -
471 horizontal (the default), otherwise a row. -
472 -
473 For the following example, we assume a model \c myModel that -
474 has two columns: the first one contains the names of people in a -
475 group, and the second column contains their ages. The first column -
476 is mapped to the QLineEdit \c nameLineEdit, and the second is -
477 mapped to the QSpinBox \c{ageSpinBox}: -
478 -
479 \snippet code/src_gui_itemviews_qdatawidgetmapper.cpp 1 -
480 -
481 \b{Notes:} -
482 \list -
483 \li If the \a widget is already mapped to a section, the -
484 old mapping will be replaced by the new one. -
485 \li Only one-to-one mappings between sections and widgets are allowed. -
486 It is not possible to map a single section to multiple widgets, or to -
487 map a single widget to multiple sections. -
488 \endlist -
489 -
490 \sa removeMapping(), mappedSection(), clearMapping() -
491 */ -
492void QDataWidgetMapper::addMapping(QWidget *widget, int section) -
493{ -
494 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
495 -
496 removeMapping(widget);
executed (the execution status of this line is deduced): removeMapping(widget);
-
497 d->widgetMap.append(QDataWidgetMapperPrivate::WidgetMapper(widget, section, d->indexAt(section)));
executed (the execution status of this line is deduced): d->widgetMap.append(QDataWidgetMapperPrivate::WidgetMapper(widget, section, d->indexAt(section)));
-
498 widget->installEventFilter(d->delegate);
executed (the execution status of this line is deduced): widget->installEventFilter(d->delegate);
-
499}
executed: }
Execution Count:15
15
500 -
501/*! -
502 \since 4.3 -
503 -
504 Essentially the same as addMapping(), but adds the possibility to specify -
505 the property to use specifying \a propertyName. -
506 -
507 \sa addMapping() -
508*/ -
509 -
510void QDataWidgetMapper::addMapping(QWidget *widget, int section, const QByteArray &propertyName) -
511{ -
512 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
513 -
514 removeMapping(widget);
executed (the execution status of this line is deduced): removeMapping(widget);
-
515 d->widgetMap.append(QDataWidgetMapperPrivate::WidgetMapper(widget, section, d->indexAt(section), propertyName));
executed (the execution status of this line is deduced): d->widgetMap.append(QDataWidgetMapperPrivate::WidgetMapper(widget, section, d->indexAt(section), propertyName));
-
516 widget->installEventFilter(d->delegate);
executed (the execution status of this line is deduced): widget->installEventFilter(d->delegate);
-
517}
executed: }
Execution Count:4
4
518 -
519/*! -
520 Removes the mapping for the given \a widget. -
521 -
522 \sa addMapping(), clearMapping() -
523 */ -
524void QDataWidgetMapper::removeMapping(QWidget *widget) -
525{ -
526 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
527 -
528 int idx = d->findWidget(widget);
executed (the execution status of this line is deduced): int idx = d->findWidget(widget);
-
529 if (idx == -1)
evaluated: idx == -1
TRUEFALSE
yes
Evaluation Count:17
yes
Evaluation Count:3
3-17
530 return;
executed: return;
Execution Count:17
17
531 -
532 d->widgetMap.removeAt(idx);
executed (the execution status of this line is deduced): d->widgetMap.removeAt(idx);
-
533 widget->removeEventFilter(d->delegate);
executed (the execution status of this line is deduced): widget->removeEventFilter(d->delegate);
-
534}
executed: }
Execution Count:3
3
535 -
536/*! -
537 Returns the section the \a widget is mapped to or -1 -
538 if the widget is not mapped. -
539 -
540 \sa addMapping(), removeMapping() -
541 */ -
542int QDataWidgetMapper::mappedSection(QWidget *widget) const -
543{ -
544 Q_D(const QDataWidgetMapper);
executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate * const d = d_func();
-
545 -
546 int idx = d->findWidget(widget);
executed (the execution status of this line is deduced): int idx = d->findWidget(widget);
-
547 if (idx == -1)
partially evaluated: idx == -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
548 return -1;
never executed: return -1;
0
549 -
550 return d->widgetMap.at(idx).section;
executed: return d->widgetMap.at(idx).section;
Execution Count:1
1
551} -
552 -
553/*! -
554 \since 4.3 -
555 Returns the name of the property that is used when mapping -
556 data to the given \a widget. -
557 -
558 \sa mappedSection(), addMapping(), removeMapping() -
559*/ -
560 -
561QByteArray QDataWidgetMapper::mappedPropertyName(QWidget *widget) const -
562{ -
563 Q_D(const QDataWidgetMapper);
never executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate * const d = d_func();
-
564 -
565 int idx = d->findWidget(widget);
never executed (the execution status of this line is deduced): int idx = d->findWidget(widget);
-
566 if (idx == -1)
never evaluated: idx == -1
0
567 return QByteArray();
never executed: return QByteArray();
0
568 const QDataWidgetMapperPrivate::WidgetMapper &m = d->widgetMap.at(idx);
never executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate::WidgetMapper &m = d->widgetMap.at(idx);
-
569 if (m.property.isEmpty())
never evaluated: m.property.isEmpty()
0
570 return m.widget->metaObject()->userProperty().name();
never executed: return m.widget->metaObject()->userProperty().name();
0
571 else -
572 return m.property;
never executed: return m.property;
0
573} -
574 -
575/*! -
576 Returns the widget that is mapped at \a section, or -
577 0 if no widget is mapped at that section. -
578 -
579 \sa addMapping(), removeMapping() -
580 */ -
581QWidget *QDataWidgetMapper::mappedWidgetAt(int section) const -
582{ -
583 Q_D(const QDataWidgetMapper);
executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate * const d = d_func();
-
584 -
585 for (int i = 0; i < d->widgetMap.count(); ++i) {
evaluated: i < d->widgetMap.count()
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:2
2-8
586 if (d->widgetMap.at(i).section == section)
evaluated: d->widgetMap.at(i).section == section
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:4
4
587 return d->widgetMap.at(i).widget;
executed: return d->widgetMap.at(i).widget;
Execution Count:4
4
588 }
executed: }
Execution Count:4
4
589 -
590 return 0;
executed: return 0;
Execution Count:2
2
591} -
592 -
593/*! -
594 Repopulates all widgets with the current data of the model. -
595 All unsubmitted changes will be lost. -
596 -
597 \sa submit(), setSubmitPolicy() -
598 */ -
599void QDataWidgetMapper::revert() -
600{ -
601 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
602 -
603 d->populate();
executed (the execution status of this line is deduced): d->populate();
-
604}
executed: }
Execution Count:1
1
605 -
606/*! -
607 Submits all changes from the mapped widgets to the model. -
608 -
609 For every mapped section, the item delegate reads the current -
610 value from the widget and sets it in the model. Finally, the -
611 model's \l {QAbstractItemModel::}{submit()} method is invoked. -
612 -
613 Returns true if all the values were submitted, otherwise false. -
614 -
615 Note: For database models, QSqlQueryModel::lastError() can be -
616 used to retrieve the last error. -
617 -
618 \sa revert(), setSubmitPolicy() -
619 */ -
620bool QDataWidgetMapper::submit() -
621{ -
622 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
623 -
624 for (int i = 0; i < d->widgetMap.count(); ++i) {
evaluated: i < d->widgetMap.count()
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:5
5-14
625 const QDataWidgetMapperPrivate::WidgetMapper &m = d->widgetMap.at(i);
executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate::WidgetMapper &m = d->widgetMap.at(i);
-
626 if (!d->commit(m))
partially evaluated: !d->commit(m)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:14
0-14
627 return false;
never executed: return false;
0
628 }
executed: }
Execution Count:14
14
629 -
630 return d->model->submit();
executed: return d->model->submit();
Execution Count:5
5
631} -
632 -
633/*! -
634 Populates the widgets with data from the first row of the model -
635 if the orientation is horizontal (the default), otherwise -
636 with data from the first column. -
637 -
638 This is equivalent to calling \c setCurrentIndex(0). -
639 -
640 \sa toLast(), setCurrentIndex() -
641 */ -
642void QDataWidgetMapper::toFirst() -
643{ -
644 setCurrentIndex(0);
executed (the execution status of this line is deduced): setCurrentIndex(0);
-
645}
executed: }
Execution Count:13
13
646 -
647/*! -
648 Populates the widgets with data from the last row of the model -
649 if the orientation is horizontal (the default), otherwise -
650 with data from the last column. -
651 -
652 Calls setCurrentIndex() internally. -
653 -
654 \sa toFirst(), setCurrentIndex() -
655 */ -
656void QDataWidgetMapper::toLast() -
657{ -
658 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
659 setCurrentIndex(d->itemCount() - 1);
executed (the execution status of this line is deduced): setCurrentIndex(d->itemCount() - 1);
-
660}
executed: }
Execution Count:3
3
661 -
662 -
663/*! -
664 Populates the widgets with data from the next row of the model -
665 if the orientation is horizontal (the default), otherwise -
666 with data from the next column. -
667 -
668 Calls setCurrentIndex() internally. Does nothing if there is -
669 no next row in the model. -
670 -
671 \sa toPrevious(), setCurrentIndex() -
672 */ -
673void QDataWidgetMapper::toNext() -
674{ -
675 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
676 setCurrentIndex(d->currentIdx() + 1);
executed (the execution status of this line is deduced): setCurrentIndex(d->currentIdx() + 1);
-
677}
executed: }
Execution Count:5
5
678 -
679/*! -
680 Populates the widgets with data from the previous row of the model -
681 if the orientation is horizontal (the default), otherwise -
682 with data from the previous column. -
683 -
684 Calls setCurrentIndex() internally. Does nothing if there is -
685 no previous row in the model. -
686 -
687 \sa toNext(), setCurrentIndex() -
688 */ -
689void QDataWidgetMapper::toPrevious() -
690{ -
691 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
692 setCurrentIndex(d->currentIdx() - 1);
executed (the execution status of this line is deduced): setCurrentIndex(d->currentIdx() - 1);
-
693}
executed: }
Execution Count:2
2
694 -
695/*! -
696 \property QDataWidgetMapper::currentIndex -
697 \brief the current row or column -
698 -
699 The widgets are populated with with data from the row at \a index -
700 if the orientation is horizontal (the default), otherwise with -
701 data from the column at \a index. -
702 -
703 \sa setCurrentModelIndex(), toFirst(), toNext(), toPrevious(), toLast() -
704*/ -
705void QDataWidgetMapper::setCurrentIndex(int index) -
706{ -
707 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
708 -
709 if (index < 0 || index >= d->itemCount())
evaluated: index < 0
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:29
evaluated: index >= d->itemCount()
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:24
5-29
710 return;
executed: return;
Execution Count:10
10
711 d->currentTopLeft = d->orientation == Qt::Horizontal
evaluated: d->orientation == Qt::Horizontal
TRUEFALSE
yes
Evaluation Count:19
yes
Evaluation Count:5
5-19
712 ? d->model->index(index, 0, d->rootIndex)
executed (the execution status of this line is deduced): ? d->model->index(index, 0, d->rootIndex)
-
713 : d->model->index(0, index, d->rootIndex);
executed (the execution status of this line is deduced): : d->model->index(0, index, d->rootIndex);
-
714 d->populate();
executed (the execution status of this line is deduced): d->populate();
-
715 -
716 emit currentIndexChanged(index);
executed (the execution status of this line is deduced): currentIndexChanged(index);
-
717}
executed: }
Execution Count:24
24
718 -
719int QDataWidgetMapper::currentIndex() const -
720{ -
721 Q_D(const QDataWidgetMapper);
never executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate * const d = d_func();
-
722 return d->currentIdx();
never executed: return d->currentIdx();
0
723} -
724 -
725/*! -
726 Sets the current index to the row of the \a index if the -
727 orientation is horizontal (the default), otherwise to the -
728 column of the \a index. -
729 -
730 Calls setCurrentIndex() internally. This convenience slot can be -
731 connected to the signal \l -
732 {QItemSelectionModel::}{currentRowChanged()} or \l -
733 {QItemSelectionModel::}{currentColumnChanged()} of another view's -
734 \l {QItemSelectionModel}{selection model}. -
735 -
736 The following example illustrates how to update all widgets -
737 with new data whenever the selection of a QTableView named -
738 \c myTableView changes: -
739 -
740 \snippet code/src_gui_itemviews_qdatawidgetmapper.cpp 2 -
741 -
742 \sa currentIndex() -
743*/ -
744void QDataWidgetMapper::setCurrentModelIndex(const QModelIndex &index) -
745{ -
746 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
747 -
748 if (!index.isValid()
evaluated: !index.isValid()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:2
2
749 || index.model() != d->model
partially evaluated: index.model() != d->model
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
750 || index.parent() != d->rootIndex)
partially evaluated: index.parent() != d->rootIndex
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
751 return;
executed: return;
Execution Count:2
2
752 -
753 setCurrentIndex(d->orientation == Qt::Horizontal ? index.row() : index.column());
executed (the execution status of this line is deduced): setCurrentIndex(d->orientation == Qt::Horizontal ? index.row() : index.column());
-
754}
executed: }
Execution Count:2
2
755 -
756/*! -
757 Clears all mappings. -
758 -
759 \sa addMapping(), removeMapping() -
760 */ -
761void QDataWidgetMapper::clearMapping() -
762{ -
763 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
764 -
765 while (!d->widgetMap.isEmpty()) {
evaluated: !d->widgetMap.isEmpty()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:11
3-11
766 QWidget *w = d->widgetMap.takeLast().widget;
executed (the execution status of this line is deduced): QWidget *w = d->widgetMap.takeLast().widget;
-
767 if (w)
partially evaluated: w
TRUEFALSE
yes
Evaluation Count:3
no
Evaluation Count:0
0-3
768 w->removeEventFilter(d->delegate);
executed: w->removeEventFilter(d->delegate);
Execution Count:3
3
769 }
executed: }
Execution Count:3
3
770}
executed: }
Execution Count:11
11
771 -
772/*! -
773 \property QDataWidgetMapper::orientation -
774 \brief the orientation of the model -
775 -
776 If the orientation is Qt::Horizontal (the default), a widget is -
777 mapped to a column of a data model. The widget will be populated -
778 with the model's data from its mapped column and the row that -
779 currentIndex() points at. -
780 -
781 Use Qt::Horizontal for tabular data that looks like this: -
782 -
783 \table -
784 \row \li 1 \li Qt Norway \li Oslo -
785 \row \li 2 \li Qt Australia \li Brisbane -
786 \row \li 3 \li Qt USA \li Silicon Valley -
787 \row \li 4 \li Qt China \li Beijing -
788 \row \li 5 \li Qt Germany \li Berlin -
789 \endtable -
790 -
791 If the orientation is set to Qt::Vertical, a widget is mapped to -
792 a row. Calling setCurrentIndex() will change the current column. -
793 The widget will be populates with the model's data from its -
794 mapped row and the column that currentIndex() points at. -
795 -
796 Use Qt::Vertical for tabular data that looks like this: -
797 -
798 \table -
799 \row \li 1 \li 2 \li 3 \li 4 \li 5 -
800 \row \li Qt Norway \li Qt Australia \li Qt USA \li Qt China \li Qt Germany -
801 \row \li Oslo \li Brisbane \li Silicon Valley \li Beijing \li Berlin -
802 \endtable -
803 -
804 Changing the orientation clears all existing mappings. -
805*/ -
806void QDataWidgetMapper::setOrientation(Qt::Orientation orientation) -
807{ -
808 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
809 -
810 if (d->orientation == orientation)
partially evaluated: d->orientation == orientation
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
811 return;
never executed: return;
0
812 -
813 clearMapping();
executed (the execution status of this line is deduced): clearMapping();
-
814 d->orientation = orientation;
executed (the execution status of this line is deduced): d->orientation = orientation;
-
815}
executed: }
Execution Count:1
1
816 -
817Qt::Orientation QDataWidgetMapper::orientation() const -
818{ -
819 Q_D(const QDataWidgetMapper);
never executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate * const d = d_func();
-
820 return d->orientation;
never executed: return d->orientation;
0
821} -
822 -
823/*! -
824 \property QDataWidgetMapper::submitPolicy -
825 \brief the current submit policy -
826 -
827 Changing the current submit policy will revert all widgets -
828 to the current data from the model. -
829*/ -
830void QDataWidgetMapper::setSubmitPolicy(SubmitPolicy policy) -
831{ -
832 Q_D(QDataWidgetMapper);
executed (the execution status of this line is deduced): QDataWidgetMapperPrivate * const d = d_func();
-
833 if (policy == d->submitPolicy)
partially evaluated: policy == d->submitPolicy
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
834 return;
never executed: return;
0
835 -
836 revert();
executed (the execution status of this line is deduced): revert();
-
837 d->submitPolicy = policy;
executed (the execution status of this line is deduced): d->submitPolicy = policy;
-
838}
executed: }
Execution Count:1
1
839 -
840QDataWidgetMapper::SubmitPolicy QDataWidgetMapper::submitPolicy() const -
841{ -
842 Q_D(const QDataWidgetMapper);
never executed (the execution status of this line is deduced): const QDataWidgetMapperPrivate * const d = d_func();
-
843 return d->submitPolicy;
never executed: return d->submitPolicy;
0
844} -
845 -
846QT_END_NAMESPACE -
847 -
848#include "moc_qdatawidgetmapper.cpp" -
849 -
850#endif // QT_NO_DATAWIDGETMAPPER -
851 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial