1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the item views module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include <qplatformdefs.h>
30
#include "qitemeditorfactory.h"
31
#include <qcombobox.h>
32
#include <qdatetimeedit.h>
34
#include <qlineedit.h>
39
\class QItemEditorFactory
40
\brief The QItemEditorFactory class provides widgets for editing item data
41
in views and delegates.
43
When editing the data shown by an item delegate, the QItemDelegate responsible
44
requests an editor widget from its item editor factory. The default factory is
45
provided by this class, but it is possible to implement subclasses that provide
46
specialized editing behavior, such as row or column-specific editors, or editors
47
for certain types of data.
53
\fn QItemEditorFactory::QItemEditorFactory()
55
Constructs a new item editor factory.
59
Creates an editor widget with the given \a parent for the specified \a type of data,
60
and returns it as a QWidget.
62
\sa registerEditor()*/
63
QWidget *QItemEditorFactory::createEditor(QVariant::Type type, QWidget *parent) const
65
QItemEditorCreatorBase *creator = creatorMap.value(type, 0);
67
return defaultFactory()->createEditor(type, parent);
68
return creator->createWidget(parent);
72
Returns the property name used to identify the given \a type of data. */
73
QByteArray QItemEditorFactory::valuePropertyName(QVariant::Type type) const
75
QItemEditorCreatorBase *creator = creatorMap.value(type, 0);
77
return defaultFactory()->valuePropertyName(type);
78
return creator->valuePropertyName();
82
Destroys the item editor factory.*/
83
QItemEditorFactory::~QItemEditorFactory()
89
Registers an item editor creator specified by \a creator for the given \a type of data.
92
void QItemEditorFactory::registerEditor(QVariant::Type type, QItemEditorCreatorBase *creator)
94
delete creatorMap.value(type, 0);
95
creatorMap[type] = creator;
98
class QDefaultItemEditorFactory: public QItemEditorFactory
101
inline QDefaultItemEditorFactory() {}
102
QWidget *createEditor(QVariant::Type type, QWidget *parent) const;
103
QByteArray valuePropertyName(QVariant::Type) const;
106
QWidget *QDefaultItemEditorFactory::createEditor(QVariant::Type type, QWidget *parent) const
109
case QVariant::Bool: {
110
QComboBox *cb = new QComboBox(parent);
112
cb->addItem("False");
115
case QVariant::UInt: {
116
QSpinBox *sb = new QSpinBox(parent);
118
sb->setMaximum(INT_MAX);
120
case QVariant::Int: {
121
QSpinBox *sb = new QSpinBox(parent);
123
sb->setMinimum(INT_MIN);
124
sb->setMaximum(INT_MAX);
126
case QVariant::Date: {
127
QDateTimeEdit *ed = new QDateEdit(parent);
130
case QVariant::Time: {
131
QDateTimeEdit *ed = new QTimeEdit(parent);
134
case QVariant::DateTime: {
135
QDateTimeEdit *ed = new QDateTimeEdit(parent);
138
case QVariant::Pixmap:
139
return new QLabel(parent);
140
case QVariant::Double: {
141
QDoubleSpinBox *sb = new QDoubleSpinBox(parent);
144
case QVariant::StringList: {
145
QComboBox *cb = new QComboBox(parent);
148
case QVariant::String:
150
// the default editor is a lineedit
151
QLineEdit *le = new QLineEdit(parent);
157
QByteArray QDefaultItemEditorFactory::valuePropertyName(QVariant::Type type) const
161
return "currentItem";
164
case QVariant::Double:
170
case QVariant::DateTime:
172
case QVariant::StringList:
174
case QVariant::String:
176
// the default editor is a lineedit
181
static QItemEditorFactory *q_default_factory = 0;
182
struct QDefaultFactoryCleaner
184
inline QDefaultFactoryCleaner() {}
185
~QDefaultFactoryCleaner() { delete q_default_factory; q_default_factory = 0; }
189
Returns the default item editor factory.
191
\sa setDefaultFactory()*/
192
const QItemEditorFactory *QItemEditorFactory::defaultFactory()
194
static const QDefaultItemEditorFactory factory;
195
if (q_default_factory)
196
return q_default_factory;
201
Sets the default item editor factory to the given \a factory.
205
void QItemEditorFactory::setDefaultFactory(QItemEditorFactory *factory)
207
static const QDefaultFactoryCleaner cleaner;
208
delete q_default_factory;
209
q_default_factory = factory;
213
\class QItemEditorCreatorBase
214
\brief The QItemEditorCreatorBase class provides an abstract base class that
215
must be subclassed when implementing new item editor creators.
217
Item editor creators are specialized widget factories that provide editor widgets
218
for specific types of item data. QItemEditorFactory finds the appropriate factory
219
for editors using a QVariant-based scheme to associate data types with editor
222
\sa QItemEditorFactory
226
\fn QItemEditorCreatorBase::~QItemEditorCreatorBase()
228
Destroys the editor creator object.
232
\fn QWidget *QItemEditorCreatorBase::createWidget(QWidget *parent) const
234
Returns an editor widget with the given \a parent.
236
When implementing this function in subclasses of this class, you must
237
construct and return new editor widgets with the parent widget specified.
241
\fn QByteArray QItemEditorCreatorBase::valuePropertyName() const
243
Returns the name of the property associated with the creator's editor
246
When implementing this function in subclasses, the property name you
247
must return corresponds to the type of value that your editor widgets
248
are designed to edit.