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 Qt3Support module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
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
54 QT_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
Q3SqlEditorFactory(QObject * parent)81 Q3SqlEditorFactory::Q3SqlEditorFactory (QObject * parent)
82 : Q3EditorFactory(parent)
83 {
84
85 }
86
87 /*!
88 Destroys the object and frees any allocated resources.
89 */
90
~Q3SqlEditorFactory()91 Q3SqlEditorFactory::~Q3SqlEditorFactory()
92 {
93
94 }
95
96 static Q3SqlEditorFactory * defaultfactory = 0;
97 static Q3CleanupHandler< Q3SqlEditorFactory > qsql_cleanup_editor_factory;
98
99 /*!
100 Returns an instance of a default editor factory.
101 */
102
defaultFactory()103 Q3SqlEditorFactory * 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
installDefaultFactory(Q3SqlEditorFactory * factory)120 void 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
createEditor(QWidget * parent,const QVariant & variant)140 QWidget * 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
createEditor(QWidget * parent,const QSqlField * field)153 QWidget * 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
227 QT_END_NAMESPACE
228
229 #endif // QT_NO_SQL
230