source: trunk/src/qt3support/sql/q3sqleditorfactory.cpp@ 858

Last change on this file since 858 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 7.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the Qt3Support module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "q3sqleditorfactory.h"
43
44#ifndef QT_NO_SQL_EDIT_WIDGETS
45
46#include "qsqlfield.h"
47#include "q3cleanuphandler.h"
48#include "qlabel.h"
49#include "qlineedit.h"
50#include "qspinbox.h"
51#include "qcombobox.h"
52#include "qdatetimeedit.h"
53
54QT_BEGIN_NAMESPACE
55
56/*!
57 \class Q3SqlEditorFactory
58 \brief The Q3SqlEditorFactory class is used to create the editors
59 used by Q3DataTable and Q3SqlForm.
60
61 \compat
62
63 Q3SqlEditorFactory is used by Q3DataTable and Q3SqlForm to
64 automatically create appropriate editors for a given QSqlField.
65 For example if the field is a QVariant::String a QLineEdit would
66 be the default editor, whereas a QVariant::Int's default editor
67 would be a QSpinBox.
68
69 If you want to create different editors for fields with the same
70 data type, subclass Q3SqlEditorFactory and reimplement the
71 createEditor() function.
72
73 \sa Q3DataTable, Q3SqlForm
74*/
75
76
77/*!
78 Constructs a SQL editor factory with parent \a parent.
79*/
80
81Q3SqlEditorFactory::Q3SqlEditorFactory (QObject * parent)
82 : Q3EditorFactory(parent)
83{
84
85}
86
87/*!
88 Destroys the object and frees any allocated resources.
89*/
90
91Q3SqlEditorFactory::~Q3SqlEditorFactory()
92{
93
94}
95
96static Q3SqlEditorFactory * defaultfactory = 0;
97static Q3CleanupHandler< Q3SqlEditorFactory > qsql_cleanup_editor_factory;
98
99/*!
100 Returns an instance of a default editor factory.
101*/
102
103Q3SqlEditorFactory * Q3SqlEditorFactory::defaultFactory()
104{
105 if(defaultfactory == 0){
106 defaultfactory = new Q3SqlEditorFactory();
107 qsql_cleanup_editor_factory.add(&defaultfactory);
108 }
109
110 return defaultfactory;
111}
112
113/*!
114 Replaces the default editor factory with \a factory. All
115 Q3DataTable and Q3SqlForm instantiations will use this new factory
116 for creating field editors. \e{Q3SqlEditorFactory takes ownership
117 of \a factory, and destroys it when it is no longer needed.}
118*/
119
120void Q3SqlEditorFactory::installDefaultFactory(Q3SqlEditorFactory * factory)
121{
122 if(factory == 0) return;
123
124 if(defaultfactory != 0){
125 qsql_cleanup_editor_factory.remove(&defaultfactory);
126 delete defaultfactory;
127 }
128 defaultfactory = factory;
129 qsql_cleanup_editor_factory.add(&defaultfactory);
130}
131
132/*!
133 Creates and returns the appropriate editor widget for the QVariant
134 \a variant.
135
136 The widget that is returned has the parent \a parent (which may be
137 zero). If \a variant is invalid, 0 is returned.
138*/
139
140QWidget * Q3SqlEditorFactory::createEditor(QWidget * parent,
141 const QVariant & variant)
142{
143 return Q3EditorFactory::createEditor(parent, variant);
144}
145
146/*!
147 \overload
148
149 Creates and returns the appropriate editor for the QSqlField \a
150 field.
151*/
152
153QWidget * Q3SqlEditorFactory::createEditor(QWidget * parent,
154 const QSqlField * field)
155{
156 if (!field) {
157 return 0;
158 }
159
160 QWidget * w = 0;
161 switch(field->type()){
162 case QVariant::Invalid:
163 w = 0;
164 break;
165 case QVariant::Bool:
166 w = new QComboBox(parent, "qt_editor_bool");
167 ((QComboBox *) w)->insertItem(QLatin1String("False"));
168 ((QComboBox *) w)->insertItem(QLatin1String("True"));
169 break;
170 case QVariant::UInt:
171 w = new QSpinBox(0, 2147483647, 1, parent, "qt_editor_spinbox");
172 break;
173 case QVariant::Int:
174 w = new QSpinBox(-2147483647, 2147483647, 1, parent, "qt_editor_int");
175 break;
176 case QVariant::LongLong:
177 case QVariant::ULongLong:
178 case QVariant::String:
179 case QVariant::Double:
180 w = new QLineEdit(parent, "qt_editor_double");
181 ((QLineEdit*)w)->setFrame(false);
182 break;
183 case QVariant::Date: {
184 QDateTimeEdit *edit = new QDateTimeEdit(parent);
185 edit->setDisplayFormat(QLatin1String("yyyy/MM/dd"));
186 edit->setObjectName(QLatin1String("qt_editor_date"));
187 w = edit; }
188 break;
189 case QVariant::Time: {
190 QDateTimeEdit *edit = new QDateTimeEdit(parent);
191 edit->setDisplayFormat(QLatin1String("hh:mm"));
192 edit->setObjectName(QLatin1String("qt_editor_time"));
193 w = edit; }
194 break;
195 case QVariant::DateTime:
196 w = new QDateTimeEdit(parent);
197 w->setObjectName(QLatin1String("qt_editor_datetime"));
198 break;
199#ifndef QT_NO_LABEL
200 case QVariant::Pixmap:
201 w = new QLabel(parent, "qt_editor_pixmap");
202 break;
203#endif
204 case QVariant::Palette:
205 case QVariant::Color:
206 case QVariant::Font:
207 case QVariant::Brush:
208 case QVariant::Bitmap:
209 case QVariant::Cursor:
210 case QVariant::Map:
211 case QVariant::StringList:
212 case QVariant::Rect:
213 case QVariant::Size:
214 case QVariant::IconSet:
215 case QVariant::Point:
216 case QVariant::PointArray:
217 case QVariant::Region:
218 case QVariant::SizePolicy:
219 case QVariant::ByteArray:
220 default:
221 w = new QWidget(parent, "qt_editor_default");
222 break;
223 }
224 return w;
225}
226
227QT_END_NAMESPACE
228
229#endif // QT_NO_SQL
Note: See TracBrowser for help on using the repository browser.