1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Implementation of QSqlEditorFactory class
|
---|
4 | **
|
---|
5 | ** Created : 2000-11-03
|
---|
6 | **
|
---|
7 | ** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
|
---|
8 | **
|
---|
9 | ** This file is part of the sql module of the Qt GUI Toolkit.
|
---|
10 | **
|
---|
11 | ** This file may be distributed under the terms of the Q Public License
|
---|
12 | ** as defined by Trolltech ASA of Norway and appearing in the file
|
---|
13 | ** LICENSE.QPL included in the packaging of this file.
|
---|
14 | **
|
---|
15 | ** This file may be distributed and/or modified under the terms of the
|
---|
16 | ** GNU General Public License version 2 as published by the Free Software
|
---|
17 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
18 | ** packaging of this file.
|
---|
19 | **
|
---|
20 | ** Licensees holding valid Qt Enterprise Edition licenses may use this
|
---|
21 | ** file in accordance with the Qt Commercial License Agreement provided
|
---|
22 | ** with the Software.
|
---|
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 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
28 | ** information about Qt Commercial License Agreements.
|
---|
29 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
30 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
31 | **
|
---|
32 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
33 | ** not clear to you.
|
---|
34 | **
|
---|
35 | **********************************************************************/
|
---|
36 |
|
---|
37 | #include "qsqleditorfactory.h"
|
---|
38 |
|
---|
39 | #ifndef QT_NO_SQL_EDIT_WIDGETS
|
---|
40 |
|
---|
41 | #include "qsqlfield.h"
|
---|
42 | #include "qcleanuphandler.h"
|
---|
43 | #include "qlabel.h"
|
---|
44 | #include "qlineedit.h"
|
---|
45 | #include "qspinbox.h"
|
---|
46 | #include "qcombobox.h"
|
---|
47 | #include "qdatetimeedit.h"
|
---|
48 |
|
---|
49 | /*!
|
---|
50 | \class QSqlEditorFactory qsqleditorfactory.h
|
---|
51 | \brief The QSqlEditorFactory class is used to create the editors
|
---|
52 | used by QDataTable and QSqlForm.
|
---|
53 |
|
---|
54 | \ingroup database
|
---|
55 | \module sql
|
---|
56 |
|
---|
57 | QSqlEditorFactory is used by QDataTable and QSqlForm to
|
---|
58 | automatically create appropriate editors for a given QSqlField.
|
---|
59 | For example if the field is a QVariant::String a QLineEdit would
|
---|
60 | be the default editor, whereas a QVariant::Int's default editor
|
---|
61 | would be a QSpinBox.
|
---|
62 |
|
---|
63 | If you want to create different editors for fields with the same
|
---|
64 | data type, subclass QSqlEditorFactory and reimplement the
|
---|
65 | createEditor() function.
|
---|
66 |
|
---|
67 | \sa QDataTable, QSqlForm
|
---|
68 | */
|
---|
69 |
|
---|
70 |
|
---|
71 | /*!
|
---|
72 | Constructs a SQL editor factory with parent \a parent, called \a
|
---|
73 | name.
|
---|
74 | */
|
---|
75 |
|
---|
76 | QSqlEditorFactory::QSqlEditorFactory ( QObject * parent, const char * name )
|
---|
77 | : QEditorFactory( parent, name )
|
---|
78 | {
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*!
|
---|
83 | Destroys the object and frees any allocated resources.
|
---|
84 | */
|
---|
85 |
|
---|
86 | QSqlEditorFactory::~QSqlEditorFactory()
|
---|
87 | {
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | static QSqlEditorFactory * defaultfactory = 0;
|
---|
92 | static QCleanupHandler< QSqlEditorFactory > qsql_cleanup_editor_factory;
|
---|
93 |
|
---|
94 | /*!
|
---|
95 | Returns an instance of a default editor factory.
|
---|
96 | */
|
---|
97 |
|
---|
98 | QSqlEditorFactory * QSqlEditorFactory::defaultFactory()
|
---|
99 | {
|
---|
100 | if( defaultfactory == 0 ){
|
---|
101 | defaultfactory = new QSqlEditorFactory();
|
---|
102 | qsql_cleanup_editor_factory.add( &defaultfactory );
|
---|
103 | }
|
---|
104 |
|
---|
105 | return defaultfactory;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*!
|
---|
109 | Replaces the default editor factory with \a factory. All
|
---|
110 | QDataTable and QSqlForm instantiations will use this new factory
|
---|
111 | for creating field editors. \e{QSqlEditorFactory takes ownership
|
---|
112 | of \a factory, and destroys it when it is no longer needed.}
|
---|
113 | */
|
---|
114 |
|
---|
115 | void QSqlEditorFactory::installDefaultFactory( QSqlEditorFactory * factory )
|
---|
116 | {
|
---|
117 | if( factory == 0 ) return;
|
---|
118 |
|
---|
119 | if( defaultfactory != 0 ){
|
---|
120 | qsql_cleanup_editor_factory.remove( &defaultfactory );
|
---|
121 | delete defaultfactory;
|
---|
122 | }
|
---|
123 | defaultfactory = factory;
|
---|
124 | qsql_cleanup_editor_factory.add( &defaultfactory );
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*!
|
---|
128 | Creates and returns the appropriate editor widget for the QVariant
|
---|
129 | \a variant.
|
---|
130 |
|
---|
131 | The widget that is returned has the parent \a parent (which may be
|
---|
132 | zero). If \a variant is invalid, 0 is returned.
|
---|
133 | */
|
---|
134 |
|
---|
135 | QWidget * QSqlEditorFactory::createEditor( QWidget * parent,
|
---|
136 | const QVariant & variant )
|
---|
137 | {
|
---|
138 | return QEditorFactory::createEditor( parent, variant );
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*!
|
---|
142 | \overload
|
---|
143 |
|
---|
144 | Creates and returns the appropriate editor for the QSqlField \a
|
---|
145 | field.
|
---|
146 | */
|
---|
147 |
|
---|
148 | QWidget * QSqlEditorFactory::createEditor( QWidget * parent,
|
---|
149 | const QSqlField * field )
|
---|
150 | {
|
---|
151 | if ( !field ) {
|
---|
152 | return 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 | QWidget * w = 0;
|
---|
156 | switch( field->type() ){
|
---|
157 | case QVariant::Invalid:
|
---|
158 | w = 0;
|
---|
159 | break;
|
---|
160 | case QVariant::Bool:
|
---|
161 | w = new QComboBox( parent, "qt_editor_bool" );
|
---|
162 | ((QComboBox *) w)->insertItem( "False" );
|
---|
163 | ((QComboBox *) w)->insertItem( "True" );
|
---|
164 | break;
|
---|
165 | case QVariant::UInt:
|
---|
166 | w = new QSpinBox( 0, 2147483647, 1, parent, "qt_editor_spinbox" );
|
---|
167 | break;
|
---|
168 | case QVariant::Int:
|
---|
169 | w = new QSpinBox( -2147483647, 2147483647, 1, parent, "qt_editor_int" );
|
---|
170 | break;
|
---|
171 | case QVariant::LongLong:
|
---|
172 | case QVariant::ULongLong:
|
---|
173 | case QVariant::String:
|
---|
174 | case QVariant::CString:
|
---|
175 | case QVariant::Double:
|
---|
176 | w = new QLineEdit( parent, "qt_editor_double" );
|
---|
177 | ((QLineEdit*)w)->setFrame( FALSE );
|
---|
178 | break;
|
---|
179 | case QVariant::Date:
|
---|
180 | w = new QDateEdit( parent, "qt_editor_date" );
|
---|
181 | break;
|
---|
182 | case QVariant::Time:
|
---|
183 | w = new QTimeEdit( parent, "qt_editor_time" );
|
---|
184 | break;
|
---|
185 | case QVariant::DateTime:
|
---|
186 | w = new QDateTimeEdit( parent, "qt_editor_datetime" );
|
---|
187 | break;
|
---|
188 | #ifndef QT_NO_LABEL
|
---|
189 | case QVariant::Pixmap:
|
---|
190 | w = new QLabel( parent, "qt_editor_pixmap" );
|
---|
191 | break;
|
---|
192 | #endif
|
---|
193 | case QVariant::Palette:
|
---|
194 | case QVariant::ColorGroup:
|
---|
195 | case QVariant::Color:
|
---|
196 | case QVariant::Font:
|
---|
197 | case QVariant::Brush:
|
---|
198 | case QVariant::Bitmap:
|
---|
199 | case QVariant::Cursor:
|
---|
200 | case QVariant::Map:
|
---|
201 | case QVariant::StringList:
|
---|
202 | case QVariant::Rect:
|
---|
203 | case QVariant::Size:
|
---|
204 | case QVariant::IconSet:
|
---|
205 | case QVariant::Point:
|
---|
206 | case QVariant::PointArray:
|
---|
207 | case QVariant::Region:
|
---|
208 | case QVariant::SizePolicy:
|
---|
209 | case QVariant::ByteArray:
|
---|
210 | default:
|
---|
211 | w = new QWidget( parent, "qt_editor_default" );
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | return w;
|
---|
215 | }
|
---|
216 |
|
---|
217 | #endif // QT_NO_SQL
|
---|