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

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