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