~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/itemviews/qitemeditorfactory.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the item views module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <qplatformdefs.h>
 
30
#include "qitemeditorfactory.h"
 
31
#include <qcombobox.h>
 
32
#include <qdatetimeedit.h>
 
33
#include <qlabel.h>
 
34
#include <qlineedit.h>
 
35
#include <qspinbox.h>
 
36
#include <limits.h>
 
37
 
 
38
/*!
 
39
    \class QItemEditorFactory
 
40
    \brief The QItemEditorFactory class provides widgets for editing item data
 
41
    in views and delegates.
 
42
 
 
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.
 
48
 
 
49
    \sa QItemDelegate
 
50
*/
 
51
 
 
52
/*!
 
53
    \fn QItemEditorFactory::QItemEditorFactory()
 
54
 
 
55
    Constructs a new item editor factory.
 
56
*/
 
57
 
 
58
/*!
 
59
Creates an editor widget with the given \a parent for the specified \a type of data,
 
60
and returns it as a QWidget.
 
61
 
 
62
\sa registerEditor()*/
 
63
QWidget *QItemEditorFactory::createEditor(QVariant::Type type, QWidget *parent) const
 
64
{
 
65
    QItemEditorCreatorBase *creator = creatorMap.value(type, 0);
 
66
    if (!creator)
 
67
        return defaultFactory()->createEditor(type, parent);
 
68
    return creator->createWidget(parent);
 
69
}
 
70
 
 
71
/*!
 
72
Returns the property name used to identify the given \a type of data. */
 
73
QByteArray QItemEditorFactory::valuePropertyName(QVariant::Type type) const
 
74
{
 
75
    QItemEditorCreatorBase *creator = creatorMap.value(type, 0);
 
76
    if (!creator)
 
77
        return defaultFactory()->valuePropertyName(type);
 
78
    return creator->valuePropertyName();
 
79
}
 
80
 
 
81
/*!
 
82
Destroys the item editor factory.*/
 
83
QItemEditorFactory::~QItemEditorFactory()
 
84
{
 
85
 
 
86
}
 
87
 
 
88
/*!
 
89
Registers an item editor creator specified by \a creator for the given \a type of data.
 
90
 
 
91
\sa createEditor()*/
 
92
void QItemEditorFactory::registerEditor(QVariant::Type type, QItemEditorCreatorBase *creator)
 
93
{
 
94
   delete creatorMap.value(type, 0);
 
95
   creatorMap[type] = creator;
 
96
}
 
97
 
 
98
class QDefaultItemEditorFactory: public QItemEditorFactory
 
99
{
 
100
public:
 
101
    inline QDefaultItemEditorFactory() {}
 
102
    QWidget *createEditor(QVariant::Type type, QWidget *parent) const;
 
103
    QByteArray valuePropertyName(QVariant::Type) const;
 
104
};
 
105
 
 
106
QWidget *QDefaultItemEditorFactory::createEditor(QVariant::Type type, QWidget *parent) const
 
107
{
 
108
    switch (type) {
 
109
    case QVariant::Bool: {
 
110
        QComboBox *cb = new QComboBox(parent);
 
111
        cb->setFrame(false);
 
112
        cb->addItem("False");
 
113
        cb->addItem("True");
 
114
        return cb; }
 
115
    case QVariant::UInt: {
 
116
        QSpinBox *sb = new QSpinBox(parent);
 
117
        sb->setFrame(false);
 
118
        sb->setMaximum(INT_MAX);
 
119
        return sb; }
 
120
    case QVariant::Int: {
 
121
        QSpinBox *sb = new QSpinBox(parent);
 
122
        sb->setFrame(false);
 
123
        sb->setMinimum(INT_MIN);
 
124
        sb->setMaximum(INT_MAX);
 
125
        return sb; }
 
126
    case QVariant::Date: {
 
127
        QDateTimeEdit *ed = new QDateEdit(parent);
 
128
        ed->setFrame(false);
 
129
        return ed; }
 
130
    case QVariant::Time: {
 
131
        QDateTimeEdit *ed = new QTimeEdit(parent);
 
132
        ed->setFrame(false);
 
133
        return ed; }
 
134
    case QVariant::DateTime: {
 
135
        QDateTimeEdit *ed = new QDateTimeEdit(parent);
 
136
        ed->setFrame(false);
 
137
        return ed; }
 
138
    case QVariant::Pixmap:
 
139
        return new QLabel(parent);
 
140
    case QVariant::Double: {
 
141
        QDoubleSpinBox *sb = new QDoubleSpinBox(parent);
 
142
        sb->setFrame(false);
 
143
        return sb; }
 
144
    case QVariant::StringList: {
 
145
        QComboBox *cb = new QComboBox(parent);
 
146
        cb->setFrame(false);
 
147
        return cb; }
 
148
    case QVariant::String:
 
149
    default: {
 
150
        // the default editor is a lineedit
 
151
        QLineEdit *le = new QLineEdit(parent);
 
152
        le->setFrame(false);
 
153
        return le; }
 
154
    }
 
155
}
 
156
 
 
157
QByteArray QDefaultItemEditorFactory::valuePropertyName(QVariant::Type type) const
 
158
{
 
159
    switch (type) {
 
160
    case QVariant::Bool:
 
161
        return "currentItem";
 
162
    case QVariant::UInt:
 
163
    case QVariant::Int:
 
164
    case QVariant::Double:
 
165
        return "value";
 
166
    case QVariant::Date:
 
167
        return "date";
 
168
    case QVariant::Time:
 
169
        return "time";
 
170
    case QVariant::DateTime:
 
171
        return "dateTime";
 
172
    case QVariant::StringList:
 
173
        return "contents";
 
174
    case QVariant::String:
 
175
    default:
 
176
        // the default editor is a lineedit
 
177
        return "text";
 
178
    }
 
179
}
 
180
 
 
181
static QItemEditorFactory *q_default_factory = 0;
 
182
struct QDefaultFactoryCleaner
 
183
{
 
184
    inline QDefaultFactoryCleaner() {}
 
185
    ~QDefaultFactoryCleaner() { delete q_default_factory; q_default_factory = 0; }
 
186
};
 
187
 
 
188
/*!
 
189
Returns the default item editor factory.
 
190
 
 
191
\sa setDefaultFactory()*/
 
192
const QItemEditorFactory *QItemEditorFactory::defaultFactory()
 
193
{
 
194
    static const QDefaultItemEditorFactory factory;
 
195
    if (q_default_factory)
 
196
        return q_default_factory;
 
197
    return &factory;
 
198
}
 
199
 
 
200
/*!
 
201
    Sets the default item editor factory to the given \a factory.
 
202
 
 
203
    \sa defaultFactory()
 
204
*/
 
205
void QItemEditorFactory::setDefaultFactory(QItemEditorFactory *factory)
 
206
{
 
207
    static const QDefaultFactoryCleaner cleaner;
 
208
    delete q_default_factory;
 
209
    q_default_factory = factory;
 
210
}
 
211
 
 
212
/*!
 
213
    \class QItemEditorCreatorBase
 
214
    \brief The QItemEditorCreatorBase class provides an abstract base class that
 
215
    must be subclassed when implementing new item editor creators.
 
216
 
 
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
 
220
    creators.
 
221
 
 
222
    \sa QItemEditorFactory
 
223
*/
 
224
 
 
225
/*!
 
226
    \fn QItemEditorCreatorBase::~QItemEditorCreatorBase()
 
227
 
 
228
    Destroys the editor creator object.
 
229
*/
 
230
 
 
231
/*!
 
232
    \fn QWidget *QItemEditorCreatorBase::createWidget(QWidget *parent) const
 
233
 
 
234
    Returns an editor widget with the given \a parent.
 
235
 
 
236
    When implementing this function in subclasses of this class, you must
 
237
    construct and return new editor widgets with the parent widget specified.
 
238
*/
 
239
 
 
240
/*!
 
241
    \fn QByteArray QItemEditorCreatorBase::valuePropertyName() const
 
242
 
 
243
    Returns the name of the property associated with the creator's editor
 
244
    widgets.
 
245
 
 
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.
 
249
*/