Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/itemviews/qitemeditorfactory.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
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 <qplatformdefs.h> | - | ||||||
41 | #include "qitemeditorfactory.h" | - | ||||||
42 | #include "qitemeditorfactory_p.h" | - | ||||||
43 | - | |||||||
44 | #ifndef QT_NO_ITEMVIEWS | - | ||||||
45 | - | |||||||
46 | #include <qcombobox.h> | - | ||||||
47 | #include <qdatetimeedit.h> | - | ||||||
48 | #include <qlabel.h> | - | ||||||
49 | #include <qlineedit.h> | - | ||||||
50 | #include <qspinbox.h> | - | ||||||
51 | #include <limits.h> | - | ||||||
52 | #include <float.h> | - | ||||||
53 | #include <qapplication.h> | - | ||||||
54 | #include <qdebug.h> | - | ||||||
55 | - | |||||||
56 | #include <algorithm> | - | ||||||
57 | QT_BEGIN_NAMESPACE | - | ||||||
58 | - | |||||||
59 | - | |||||||
60 | #ifndef QT_NO_COMBOBOX | - | ||||||
61 | - | |||||||
62 | class QBooleanComboBox : public QComboBox | - | ||||||
63 | { | - | ||||||
64 | Q_OBJECT | - | ||||||
65 | Q_PROPERTY(bool value READ value WRITE setValue USER true) | - | ||||||
66 | - | |||||||
67 | public: | - | ||||||
68 | QBooleanComboBox(QWidget *parent); | - | ||||||
69 | void setValue(bool); | - | ||||||
70 | bool value() const; | - | ||||||
71 | }; | - | ||||||
72 | - | |||||||
73 | #endif // QT_NO_COMBOBOX | - | ||||||
74 | - | |||||||
75 | - | |||||||
76 | #ifndef QT_NO_SPINBOX | - | ||||||
77 | - | |||||||
78 | class QUIntSpinBox : public QSpinBox | - | ||||||
79 | { | - | ||||||
80 | Q_OBJECT | - | ||||||
81 | Q_PROPERTY(uint value READ uintValue WRITE setUIntValue NOTIFY uintValueChanged USER true) | - | ||||||
82 | public: | - | ||||||
83 | explicit QUIntSpinBox(QWidget *parent = 0) | - | ||||||
84 | : QSpinBox(parent) | - | ||||||
85 | { | - | ||||||
86 | connect(this, SIGNAL(valueChanged(int)), SIGNAL(uintValueChanged())); | - | ||||||
87 | } never executed: end of block | 0 | ||||||
88 | - | |||||||
89 | uint uintValue() | - | ||||||
90 | { | - | ||||||
91 | return value(); never executed: return value(); | 0 | ||||||
92 | } | - | ||||||
93 | - | |||||||
94 | void setUIntValue(uint value_) | - | ||||||
95 | { | - | ||||||
96 | return setValue(value_); never executed: return setValue(value_); | 0 | ||||||
97 | } | - | ||||||
98 | - | |||||||
99 | Q_SIGNALS: | - | ||||||
100 | void uintValueChanged(); | - | ||||||
101 | }; | - | ||||||
102 | - | |||||||
103 | #endif // QT_NO_SPINBOX | - | ||||||
104 | - | |||||||
105 | /*! | - | ||||||
106 | \class QItemEditorFactory | - | ||||||
107 | \brief The QItemEditorFactory class provides widgets for editing item data | - | ||||||
108 | in views and delegates. | - | ||||||
109 | \since 4.2 | - | ||||||
110 | \ingroup model-view | - | ||||||
111 | \inmodule QtWidgets | - | ||||||
112 | - | |||||||
113 | When editing data in an item view, editors are created and | - | ||||||
114 | displayed by a delegate. QItemDelegate, which is the delegate by | - | ||||||
115 | default installed on Qt's item views, uses a QItemEditorFactory to | - | ||||||
116 | create editors for it. A default unique instance provided by | - | ||||||
117 | QItemEditorFactory is used by all item delegates. If you set a | - | ||||||
118 | new default factory with setDefaultFactory(), the new factory will | - | ||||||
119 | be used by existing and new delegates. | - | ||||||
120 | - | |||||||
121 | A factory keeps a collection of QItemEditorCreatorBase | - | ||||||
122 | instances, which are specialized editors that produce editors | - | ||||||
123 | for one particular QVariant data type (All Qt models store | - | ||||||
124 | their data in \l{QVariant}s). | - | ||||||
125 | - | |||||||
126 | \section1 Standard Editing Widgets | - | ||||||
127 | - | |||||||
128 | The standard factory implementation provides editors for a variety of data | - | ||||||
129 | types. These are created whenever a delegate needs to provide an editor for | - | ||||||
130 | data supplied by a model. The following table shows the relationship between | - | ||||||
131 | types and the standard editors provided. | - | ||||||
132 | - | |||||||
133 | \table | - | ||||||
134 | \header \li Type \li Editor Widget | - | ||||||
135 | \row \li bool \li QComboBox | - | ||||||
136 | \row \li double \li QDoubleSpinBox | - | ||||||
137 | \row \li int \li{1,2} QSpinBox | - | ||||||
138 | \row \li unsigned int | - | ||||||
139 | \row \li QDate \li QDateEdit | - | ||||||
140 | \row \li QDateTime \li QDateTimeEdit | - | ||||||
141 | \row \li QPixmap \li QLabel | - | ||||||
142 | \row \li QString \li QLineEdit | - | ||||||
143 | \row \li QTime \li QTimeEdit | - | ||||||
144 | \endtable | - | ||||||
145 | - | |||||||
146 | Additional editors can be registered with the registerEditor() function. | - | ||||||
147 | - | |||||||
148 | \sa QItemDelegate, {Model/View Programming}, {Color Editor Factory Example} | - | ||||||
149 | */ | - | ||||||
150 | - | |||||||
151 | /*! | - | ||||||
152 | \fn QItemEditorFactory::QItemEditorFactory() | - | ||||||
153 | - | |||||||
154 | Constructs a new item editor factory. | - | ||||||
155 | */ | - | ||||||
156 | - | |||||||
157 | /*! | - | ||||||
158 | Creates an editor widget with the given \a parent for the specified \a userType of data, | - | ||||||
159 | and returns it as a QWidget. | - | ||||||
160 | - | |||||||
161 | \sa registerEditor() | - | ||||||
162 | */ | - | ||||||
163 | QWidget *QItemEditorFactory::createEditor(int userType, QWidget *parent) const | - | ||||||
164 | { | - | ||||||
165 | QItemEditorCreatorBase *creator = creatorMap.value(userType, 0); | - | ||||||
166 | if (!creator) {
| 0 | ||||||
167 | const QItemEditorFactory *dfactory = defaultFactory(); | - | ||||||
168 | return dfactory == this ? 0 : dfactory->createEditor(userType, parent); never executed: return dfactory == this ? 0 : dfactory->createEditor(userType, parent); | 0 | ||||||
169 | } | - | ||||||
170 | return creator->createWidget(parent); never executed: return creator->createWidget(parent); | 0 | ||||||
171 | } | - | ||||||
172 | - | |||||||
173 | /*! | - | ||||||
174 | Returns the property name used to access data for the given \a userType of data. | - | ||||||
175 | */ | - | ||||||
176 | QByteArray QItemEditorFactory::valuePropertyName(int userType) const | - | ||||||
177 | { | - | ||||||
178 | QItemEditorCreatorBase *creator = creatorMap.value(userType, 0); | - | ||||||
179 | if (!creator) {
| 0 | ||||||
180 | const QItemEditorFactory *dfactory = defaultFactory(); | - | ||||||
181 | return dfactory == this ? QByteArray() : dfactory->valuePropertyName(userType); never executed: return dfactory == this ? QByteArray() : dfactory->valuePropertyName(userType); | 0 | ||||||
182 | } | - | ||||||
183 | return creator->valuePropertyName(); never executed: return creator->valuePropertyName(); | 0 | ||||||
184 | } | - | ||||||
185 | - | |||||||
186 | /*! | - | ||||||
187 | Destroys the item editor factory. | - | ||||||
188 | */ | - | ||||||
189 | QItemEditorFactory::~QItemEditorFactory() | - | ||||||
190 | { | - | ||||||
191 | //we make sure we delete all the QItemEditorCreatorBase | - | ||||||
192 | //this has to be done only once, hence the QSet | - | ||||||
193 | QSet<QItemEditorCreatorBase*> set = creatorMap.values().toSet(); | - | ||||||
194 | qDeleteAll(set); | - | ||||||
195 | } never executed: end of block | 0 | ||||||
196 | - | |||||||
197 | /*! | - | ||||||
198 | Registers an item editor creator specified by \a creator for the given \a userType of data. | - | ||||||
199 | - | |||||||
200 | \b{Note:} The factory takes ownership of the item editor creator and will destroy | - | ||||||
201 | it if a new creator for the same type is registered later. | - | ||||||
202 | - | |||||||
203 | \sa createEditor() | - | ||||||
204 | */ | - | ||||||
205 | void QItemEditorFactory::registerEditor(int userType, QItemEditorCreatorBase *creator) | - | ||||||
206 | { | - | ||||||
207 | const auto it = creatorMap.constFind(userType); | - | ||||||
208 | if (it != creatorMap.cend()) {
| 0 | ||||||
209 | QItemEditorCreatorBase *oldCreator = it.value(); | - | ||||||
210 | Q_ASSERT(oldCreator); | - | ||||||
211 | creatorMap.erase(it); | - | ||||||
212 | if (std::find(creatorMap.cbegin(), creatorMap.cend(), oldCreator) == creatorMap.cend())
| 0 | ||||||
213 | delete oldCreator; // if it is no more in use we can delete it never executed: delete oldCreator; | 0 | ||||||
214 | } never executed: end of block | 0 | ||||||
215 | - | |||||||
216 | creatorMap[userType] = creator; | - | ||||||
217 | } never executed: end of block | 0 | ||||||
218 | - | |||||||
219 | class QDefaultItemEditorFactory : public QItemEditorFactory | - | ||||||
220 | { | - | ||||||
221 | public: | - | ||||||
222 | inline QDefaultItemEditorFactory() {} | - | ||||||
223 | QWidget *createEditor(int userType, QWidget *parent) const Q_DECL_OVERRIDE; | - | ||||||
224 | QByteArray valuePropertyName(int) const Q_DECL_OVERRIDE; | - | ||||||
225 | }; | - | ||||||
226 | - | |||||||
227 | QWidget *QDefaultItemEditorFactory::createEditor(int userType, QWidget *parent) const | - | ||||||
228 | { | - | ||||||
229 | switch (userType) { | - | ||||||
230 | #ifndef QT_NO_COMBOBOX | - | ||||||
231 | case QVariant::Bool: { never executed: case QVariant::Bool: | 0 | ||||||
232 | QBooleanComboBox *cb = new QBooleanComboBox(parent); | - | ||||||
233 | cb->setFrame(false); | - | ||||||
234 | return cb; } never executed: return cb; | 0 | ||||||
235 | #endif | - | ||||||
236 | #ifndef QT_NO_SPINBOX | - | ||||||
237 | case QVariant::UInt: { never executed: case QVariant::UInt: | 0 | ||||||
238 | QSpinBox *sb = new QUIntSpinBox(parent); | - | ||||||
239 | sb->setFrame(false); | - | ||||||
240 | sb->setMinimum(0); | - | ||||||
241 | sb->setMaximum(INT_MAX); | - | ||||||
242 | return sb; } never executed: return sb; | 0 | ||||||
243 | case QVariant::Int: { never executed: case QVariant::Int: | 0 | ||||||
244 | QSpinBox *sb = new QSpinBox(parent); | - | ||||||
245 | sb->setFrame(false); | - | ||||||
246 | sb->setMinimum(INT_MIN); | - | ||||||
247 | sb->setMaximum(INT_MAX); | - | ||||||
248 | return sb; } never executed: return sb; | 0 | ||||||
249 | #endif | - | ||||||
250 | #ifndef QT_NO_DATETIMEEDIT | - | ||||||
251 | case QVariant::Date: { never executed: case QVariant::Date: | 0 | ||||||
252 | QDateTimeEdit *ed = new QDateEdit(parent); | - | ||||||
253 | ed->setFrame(false); | - | ||||||
254 | return ed; } never executed: return ed; | 0 | ||||||
255 | case QVariant::Time: { never executed: case QVariant::Time: | 0 | ||||||
256 | QDateTimeEdit *ed = new QTimeEdit(parent); | - | ||||||
257 | ed->setFrame(false); | - | ||||||
258 | return ed; } never executed: return ed; | 0 | ||||||
259 | case QVariant::DateTime: { never executed: case QVariant::DateTime: | 0 | ||||||
260 | QDateTimeEdit *ed = new QDateTimeEdit(parent); | - | ||||||
261 | ed->setFrame(false); | - | ||||||
262 | return ed; } never executed: return ed; | 0 | ||||||
263 | #endif | - | ||||||
264 | case QVariant::Pixmap: never executed: case QVariant::Pixmap: | 0 | ||||||
265 | return new QLabel(parent); never executed: return new QLabel(parent); | 0 | ||||||
266 | #ifndef QT_NO_SPINBOX | - | ||||||
267 | case QVariant::Double: { never executed: case QVariant::Double: | 0 | ||||||
268 | QDoubleSpinBox *sb = new QDoubleSpinBox(parent); | - | ||||||
269 | sb->setFrame(false); | - | ||||||
270 | sb->setMinimum(-DBL_MAX); | - | ||||||
271 | sb->setMaximum(DBL_MAX); | - | ||||||
272 | return sb; } never executed: return sb; | 0 | ||||||
273 | #endif | - | ||||||
274 | #ifndef QT_NO_LINEEDIT | - | ||||||
275 | case QVariant::String: never executed: case QVariant::String: | 0 | ||||||
276 | default: { never executed: default: | 0 | ||||||
277 | // the default editor is a lineedit | - | ||||||
278 | QExpandingLineEdit *le = new QExpandingLineEdit(parent); | - | ||||||
279 | le->setFrame(le->style()->styleHint(QStyle::SH_ItemView_DrawDelegateFrame, 0, le)); | - | ||||||
280 | if (!le->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, 0, le))
| 0 | ||||||
281 | le->setWidgetOwnsGeometry(true); never executed: le->setWidgetOwnsGeometry(true); | 0 | ||||||
282 | return le; } never executed: return le; | 0 | ||||||
283 | #else | - | ||||||
284 | default: | - | ||||||
285 | break; | - | ||||||
286 | #endif | - | ||||||
287 | } | - | ||||||
288 | return 0; dead code: return 0; | - | ||||||
289 | } | - | ||||||
290 | - | |||||||
291 | QByteArray QDefaultItemEditorFactory::valuePropertyName(int userType) const | - | ||||||
292 | { | - | ||||||
293 | switch (userType) { | - | ||||||
294 | #ifndef QT_NO_COMBOBOX | - | ||||||
295 | case QVariant::Bool: never executed: case QVariant::Bool: | 0 | ||||||
296 | return "currentIndex"; never executed: return "currentIndex"; | 0 | ||||||
297 | #endif | - | ||||||
298 | #ifndef QT_NO_SPINBOX | - | ||||||
299 | case QVariant::UInt: never executed: case QVariant::UInt: | 0 | ||||||
300 | case QVariant::Int: never executed: case QVariant::Int: | 0 | ||||||
301 | case QVariant::Double: never executed: case QVariant::Double: | 0 | ||||||
302 | return "value"; never executed: return "value"; | 0 | ||||||
303 | #endif | - | ||||||
304 | #ifndef QT_NO_DATETIMEEDIT | - | ||||||
305 | case QVariant::Date: never executed: case QVariant::Date: | 0 | ||||||
306 | return "date"; never executed: return "date"; | 0 | ||||||
307 | case QVariant::Time: never executed: case QVariant::Time: | 0 | ||||||
308 | return "time"; never executed: return "time"; | 0 | ||||||
309 | case QVariant::DateTime: never executed: case QVariant::DateTime: | 0 | ||||||
310 | return "dateTime"; never executed: return "dateTime"; | 0 | ||||||
311 | #endif | - | ||||||
312 | case QVariant::String: never executed: case QVariant::String: | 0 | ||||||
313 | default: never executed: default: | 0 | ||||||
314 | // the default editor is a lineedit | - | ||||||
315 | return "text"; never executed: return "text"; | 0 | ||||||
316 | } | - | ||||||
317 | } | - | ||||||
318 | - | |||||||
319 | static QItemEditorFactory *q_default_factory = 0; | - | ||||||
320 | struct QDefaultFactoryCleaner | - | ||||||
321 | { | - | ||||||
322 | inline QDefaultFactoryCleaner() {} | - | ||||||
323 | ~QDefaultFactoryCleaner() { delete q_default_factory; q_default_factory = 0; } never executed: end of block | 0 | ||||||
324 | }; | - | ||||||
325 | - | |||||||
326 | /*! | - | ||||||
327 | Returns the default item editor factory. | - | ||||||
328 | - | |||||||
329 | \sa setDefaultFactory() | - | ||||||
330 | */ | - | ||||||
331 | const QItemEditorFactory *QItemEditorFactory::defaultFactory() | - | ||||||
332 | { | - | ||||||
333 | static const QDefaultItemEditorFactory factory; | - | ||||||
334 | if (q_default_factory)
| 0 | ||||||
335 | return q_default_factory; never executed: return q_default_factory; | 0 | ||||||
336 | return &factory; never executed: return &factory; | 0 | ||||||
337 | } | - | ||||||
338 | - | |||||||
339 | /*! | - | ||||||
340 | Sets the default item editor factory to the given \a factory. | - | ||||||
341 | Both new and existing delegates will use the new factory. | - | ||||||
342 | - | |||||||
343 | \sa defaultFactory() | - | ||||||
344 | */ | - | ||||||
345 | void QItemEditorFactory::setDefaultFactory(QItemEditorFactory *factory) | - | ||||||
346 | { | - | ||||||
347 | static const QDefaultFactoryCleaner cleaner; | - | ||||||
348 | delete q_default_factory; | - | ||||||
349 | q_default_factory = factory; | - | ||||||
350 | } never executed: end of block | 0 | ||||||
351 | - | |||||||
352 | /*! | - | ||||||
353 | \class QItemEditorCreatorBase | - | ||||||
354 | \brief The QItemEditorCreatorBase class provides an abstract base class that | - | ||||||
355 | must be subclassed when implementing new item editor creators. | - | ||||||
356 | \since 4.2 | - | ||||||
357 | \ingroup model-view | - | ||||||
358 | \inmodule QtWidgets | - | ||||||
359 | - | |||||||
360 | QItemEditorCreatorBase objects are specialized widget factories that | - | ||||||
361 | provide editor widgets for one particular QVariant data type. They | - | ||||||
362 | are used by QItemEditorFactory to create editors for | - | ||||||
363 | \l{QItemDelegate}s. Creator bases must be registered with | - | ||||||
364 | QItemEditorFactory::registerEditor(). | - | ||||||
365 | - | |||||||
366 | An editor should provide a user property for the data it edits. | - | ||||||
367 | QItemDelagates can then access the property using Qt's | - | ||||||
368 | \l{Meta-Object System}{meta-object system} to set and retrieve the | - | ||||||
369 | editing data. A property is set as the user property with the USER | - | ||||||
370 | keyword: | - | ||||||
371 | - | |||||||
372 | \snippet code/src_gui_itemviews_qitemeditorfactory.cpp 0 | - | ||||||
373 | - | |||||||
374 | If the editor does not provide a user property, it must return the | - | ||||||
375 | name of the property from valuePropertyName(); delegates will then | - | ||||||
376 | use the name to access the property. If a user property exists, | - | ||||||
377 | item delegates will not call valuePropertyName(). | - | ||||||
378 | - | |||||||
379 | QStandardItemEditorCreator is a convenience template class that can be used | - | ||||||
380 | to register widgets without the need to subclass QItemEditorCreatorBase. | - | ||||||
381 | - | |||||||
382 | \sa QStandardItemEditorCreator, QItemEditorFactory, | - | ||||||
383 | {Model/View Programming}, {Color Editor Factory Example} | - | ||||||
384 | */ | - | ||||||
385 | - | |||||||
386 | /*! | - | ||||||
387 | \fn QItemEditorCreatorBase::~QItemEditorCreatorBase() | - | ||||||
388 | - | |||||||
389 | Destroys the editor creator object. | - | ||||||
390 | */ | - | ||||||
391 | QItemEditorCreatorBase::~QItemEditorCreatorBase() | - | ||||||
392 | { | - | ||||||
393 | - | |||||||
394 | } | - | ||||||
395 | - | |||||||
396 | /*! | - | ||||||
397 | \fn QWidget *QItemEditorCreatorBase::createWidget(QWidget *parent) const | - | ||||||
398 | - | |||||||
399 | Returns an editor widget with the given \a parent. | - | ||||||
400 | - | |||||||
401 | When implementing this function in subclasses of this class, you must | - | ||||||
402 | construct and return new editor widgets with the parent widget specified. | - | ||||||
403 | */ | - | ||||||
404 | - | |||||||
405 | /*! | - | ||||||
406 | \fn QByteArray QItemEditorCreatorBase::valuePropertyName() const | - | ||||||
407 | - | |||||||
408 | Returns the name of the property used to get and set values in the creator's | - | ||||||
409 | editor widgets. | - | ||||||
410 | - | |||||||
411 | When implementing this function in subclasses, you must ensure that the | - | ||||||
412 | editor widget's property specified by this function can accept the type | - | ||||||
413 | the creator is registered for. For example, a creator which constructs | - | ||||||
414 | QCheckBox widgets to edit boolean values would return the | - | ||||||
415 | \l{QCheckBox::checkable}{checkable} property name from this function, | - | ||||||
416 | and must be registered in the item editor factory for the QVariant::Bool | - | ||||||
417 | type. | - | ||||||
418 | - | |||||||
419 | Note: Since Qt 4.2 the item delegates query the user property of widgets, | - | ||||||
420 | and only call this function if the widget has no user property. You can | - | ||||||
421 | override this behavior by reimplementing QAbstractItemDelegate::setModelData() | - | ||||||
422 | and QAbstractItemDelegate::setEditorData(). | - | ||||||
423 | - | |||||||
424 | \sa QMetaObject::userProperty(), QItemEditorFactory::registerEditor() | - | ||||||
425 | */ | - | ||||||
426 | - | |||||||
427 | /*! | - | ||||||
428 | \class QItemEditorCreator | - | ||||||
429 | \brief The QItemEditorCreator class makes it possible to create | - | ||||||
430 | item editor creator bases without subclassing | - | ||||||
431 | QItemEditorCreatorBase. | - | ||||||
432 | - | |||||||
433 | \since 4.2 | - | ||||||
434 | \ingroup model-view | - | ||||||
435 | \inmodule QtWidgets | - | ||||||
436 | - | |||||||
437 | QItemEditorCreator is a convenience template class. It uses | - | ||||||
438 | the template class to create editors for QItemEditorFactory. | - | ||||||
439 | This way, it is not necessary to subclass | - | ||||||
440 | QItemEditorCreatorBase. | - | ||||||
441 | - | |||||||
442 | \snippet code/src_gui_itemviews_qitemeditorfactory.cpp 1 | - | ||||||
443 | - | |||||||
444 | The constructor takes the name of the property that contains the | - | ||||||
445 | editing data. QItemDelegate can then access the property by name | - | ||||||
446 | when it sets and retrieves editing data. Only use this class if | - | ||||||
447 | your editor does not define a user property (using the USER | - | ||||||
448 | keyword in the Q_PROPERTY macro). If the widget has a user | - | ||||||
449 | property, you should use QStandardItemEditorCreator instead. | - | ||||||
450 | - | |||||||
451 | \sa QItemEditorCreatorBase, QStandardItemEditorCreator, | - | ||||||
452 | QItemEditorFactory, {Color Editor Factory Example} | - | ||||||
453 | */ | - | ||||||
454 | - | |||||||
455 | /*! | - | ||||||
456 | \fn QItemEditorCreator::QItemEditorCreator(const QByteArray &valuePropertyName) | - | ||||||
457 | - | |||||||
458 | Constructs an editor creator object using \a valuePropertyName | - | ||||||
459 | as the name of the property to be used for editing. The | - | ||||||
460 | property name is used by QItemDelegate when setting and | - | ||||||
461 | getting editor data. | - | ||||||
462 | - | |||||||
463 | Note that the \a valuePropertyName is only used if the editor | - | ||||||
464 | widget does not have a user property defined. | - | ||||||
465 | */ | - | ||||||
466 | - | |||||||
467 | /*! | - | ||||||
468 | \fn QWidget *QItemEditorCreator::createWidget(QWidget *parent) const | - | ||||||
469 | \reimp | - | ||||||
470 | */ | - | ||||||
471 | - | |||||||
472 | /*! | - | ||||||
473 | \fn QByteArray QItemEditorCreator::valuePropertyName() const | - | ||||||
474 | \reimp | - | ||||||
475 | */ | - | ||||||
476 | - | |||||||
477 | /*! | - | ||||||
478 | \class QStandardItemEditorCreator | - | ||||||
479 | - | |||||||
480 | \brief The QStandardItemEditorCreator class provides the | - | ||||||
481 | possibility to register widgets without having to subclass | - | ||||||
482 | QItemEditorCreatorBase. | - | ||||||
483 | - | |||||||
484 | \since 4.2 | - | ||||||
485 | \ingroup model-view | - | ||||||
486 | \inmodule QtWidgets | - | ||||||
487 | - | |||||||
488 | This convenience template class makes it possible to register widgets without | - | ||||||
489 | having to subclass QItemEditorCreatorBase. | - | ||||||
490 | - | |||||||
491 | Example: | - | ||||||
492 | - | |||||||
493 | \snippet code/src_gui_itemviews_qitemeditorfactory.cpp 2 | - | ||||||
494 | - | |||||||
495 | Setting the \c editorFactory created above in an item delegate via | - | ||||||
496 | QItemDelegate::setItemEditorFactory() makes sure that all values of type | - | ||||||
497 | QVariant::DateTime will be edited in \c{MyFancyDateTimeEdit}. | - | ||||||
498 | - | |||||||
499 | The editor must provide a user property that will contain the | - | ||||||
500 | editing data. The property is used by \l{QItemDelegate}s to set | - | ||||||
501 | and retrieve the data (using Qt's \l{Meta-Object | - | ||||||
502 | System}{meta-object system}). You set the user property with | - | ||||||
503 | the USER keyword: | - | ||||||
504 | - | |||||||
505 | \snippet code/src_gui_itemviews_qitemeditorfactory.cpp 3 | - | ||||||
506 | - | |||||||
507 | \sa QItemEditorCreatorBase, QItemEditorCreator, | - | ||||||
508 | QItemEditorFactory, QItemDelegate, {Color Editor Factory Example} | - | ||||||
509 | */ | - | ||||||
510 | - | |||||||
511 | /*! | - | ||||||
512 | \fn QStandardItemEditorCreator::QStandardItemEditorCreator() | - | ||||||
513 | - | |||||||
514 | Constructs an editor creator object. | - | ||||||
515 | */ | - | ||||||
516 | - | |||||||
517 | /*! | - | ||||||
518 | \fn QWidget *QStandardItemEditorCreator::createWidget(QWidget *parent) const | - | ||||||
519 | \reimp | - | ||||||
520 | */ | - | ||||||
521 | - | |||||||
522 | /*! | - | ||||||
523 | \fn QByteArray QStandardItemEditorCreator::valuePropertyName() const | - | ||||||
524 | \reimp | - | ||||||
525 | */ | - | ||||||
526 | - | |||||||
527 | #ifndef QT_NO_LINEEDIT | - | ||||||
528 | - | |||||||
529 | QExpandingLineEdit::QExpandingLineEdit(QWidget *parent) | - | ||||||
530 | : QLineEdit(parent), originalWidth(-1), widgetOwnsGeometry(false) | - | ||||||
531 | { | - | ||||||
532 | connect(this, SIGNAL(textChanged(QString)), this, SLOT(resizeToContents())); | - | ||||||
533 | updateMinimumWidth(); | - | ||||||
534 | } never executed: end of block | 0 | ||||||
535 | - | |||||||
536 | void QExpandingLineEdit::changeEvent(QEvent *e) | - | ||||||
537 | { | - | ||||||
538 | switch (e->type()) | - | ||||||
539 | { | - | ||||||
540 | case QEvent::FontChange: never executed: case QEvent::FontChange: | 0 | ||||||
541 | case QEvent::StyleChange: never executed: case QEvent::StyleChange: | 0 | ||||||
542 | case QEvent::ContentsRectChange: never executed: case QEvent::ContentsRectChange: | 0 | ||||||
543 | updateMinimumWidth(); | - | ||||||
544 | break; never executed: break; | 0 | ||||||
545 | default: never executed: default: | 0 | ||||||
546 | break; never executed: break; | 0 | ||||||
547 | } | - | ||||||
548 | - | |||||||
549 | QLineEdit::changeEvent(e); | - | ||||||
550 | } never executed: end of block | 0 | ||||||
551 | - | |||||||
552 | void QExpandingLineEdit::updateMinimumWidth() | - | ||||||
553 | { | - | ||||||
554 | int left, right; | - | ||||||
555 | getTextMargins(&left, 0, &right, 0); | - | ||||||
556 | int width = left + right + 4 /*horizontalMargin in qlineedit.cpp*/; | - | ||||||
557 | getContentsMargins(&left, 0, &right, 0); | - | ||||||
558 | width += left + right; | - | ||||||
559 | - | |||||||
560 | QStyleOptionFrame opt; | - | ||||||
561 | initStyleOption(&opt); | - | ||||||
562 | - | |||||||
563 | int minWidth = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(width, 0). | - | ||||||
564 | expandedTo(QApplication::globalStrut()), this).width(); | - | ||||||
565 | setMinimumWidth(minWidth); | - | ||||||
566 | } never executed: end of block | 0 | ||||||
567 | - | |||||||
568 | void QExpandingLineEdit::resizeToContents() | - | ||||||
569 | { | - | ||||||
570 | int oldWidth = width(); | - | ||||||
571 | if (originalWidth == -1)
| 0 | ||||||
572 | originalWidth = oldWidth; never executed: originalWidth = oldWidth; | 0 | ||||||
573 | if (QWidget *parent = parentWidget()) {
| 0 | ||||||
574 | QPoint position = pos(); | - | ||||||
575 | int hintWidth = minimumWidth() + fontMetrics().width(displayText()); | - | ||||||
576 | int parentWidth = parent->width(); | - | ||||||
577 | int maxWidth = isRightToLeft() ? position.x() + oldWidth : parentWidth - position.x();
| 0 | ||||||
578 | int newWidth = qBound(originalWidth, hintWidth, maxWidth); | - | ||||||
579 | if (widgetOwnsGeometry)
| 0 | ||||||
580 | setMaximumWidth(newWidth); never executed: setMaximumWidth(newWidth); | 0 | ||||||
581 | if (isRightToLeft())
| 0 | ||||||
582 | move(position.x() - newWidth + oldWidth, position.y()); never executed: move(position.x() - newWidth + oldWidth, position.y()); | 0 | ||||||
583 | resize(newWidth, height()); | - | ||||||
584 | } never executed: end of block | 0 | ||||||
585 | } never executed: end of block | 0 | ||||||
586 | - | |||||||
587 | #endif // QT_NO_LINEEDIT | - | ||||||
588 | - | |||||||
589 | #ifndef QT_NO_COMBOBOX | - | ||||||
590 | - | |||||||
591 | QBooleanComboBox::QBooleanComboBox(QWidget *parent) | - | ||||||
592 | : QComboBox(parent) | - | ||||||
593 | { | - | ||||||
594 | addItem(QComboBox::tr("False")); | - | ||||||
595 | addItem(QComboBox::tr("True")); | - | ||||||
596 | } never executed: end of block | 0 | ||||||
597 | - | |||||||
598 | void QBooleanComboBox::setValue(bool value) | - | ||||||
599 | { | - | ||||||
600 | setCurrentIndex(value ? 1 : 0); | - | ||||||
601 | } never executed: end of block | 0 | ||||||
602 | - | |||||||
603 | bool QBooleanComboBox::value() const | - | ||||||
604 | { | - | ||||||
605 | return (currentIndex() == 1); never executed: return (currentIndex() == 1); | 0 | ||||||
606 | } | - | ||||||
607 | - | |||||||
608 | #endif // QT_NO_COMBOBOX | - | ||||||
609 | - | |||||||
610 | QT_END_NAMESPACE | - | ||||||
611 | - | |||||||
612 | #if !defined(QT_NO_LINEEDIT) || !defined(QT_NO_COMBOBOX) | - | ||||||
613 | #include "qitemeditorfactory.moc" | - | ||||||
614 | #endif | - | ||||||
615 | - | |||||||
616 | #include "moc_qitemeditorfactory_p.cpp" | - | ||||||
617 | - | |||||||
618 | #endif // QT_NO_ITEMVIEWS | - | ||||||
Source code | Switch to Preprocessed file |