OpenCoverage

qdatawidgetmapper.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9