1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Implementation of QEditorFactory class
|
---|
4 | **
|
---|
5 | ** Created : 2000-11-17
|
---|
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 "qcleanuphandler.h"
|
---|
38 | #include "qlabel.h"
|
---|
39 | #include "qlineedit.h"
|
---|
40 | #include "qspinbox.h"
|
---|
41 | #include "qcombobox.h"
|
---|
42 |
|
---|
43 | #include "qeditorfactory.h"
|
---|
44 | #include "qdatetimeedit.h"
|
---|
45 |
|
---|
46 | #ifndef QT_NO_SQL_EDIT_WIDGETS
|
---|
47 |
|
---|
48 | /*!
|
---|
49 | \class QEditorFactory qeditorfactory.h
|
---|
50 | \brief The QEditorFactory class is used to create editor widgets
|
---|
51 | for QVariant data types.
|
---|
52 |
|
---|
53 | \ingroup database
|
---|
54 | \module sql
|
---|
55 |
|
---|
56 | Each editor factory provides the createEditor() function which
|
---|
57 | given a QVariant will create and return a QWidget that can edit
|
---|
58 | that QVariant. For example if you have a QVariant::String type, a
|
---|
59 | QLineEdit would be the default editor returned, whereas a
|
---|
60 | QVariant::Int's default editor would be a QSpinBox.
|
---|
61 |
|
---|
62 | If you want to create different editors for fields with the same
|
---|
63 | data type, subclass QEditorFactory and reimplement the
|
---|
64 | createEditor() function.
|
---|
65 | */
|
---|
66 |
|
---|
67 | /*!
|
---|
68 | Constructs an editor factory with parent \a parent, called \a name.
|
---|
69 | */
|
---|
70 |
|
---|
71 | QEditorFactory::QEditorFactory ( QObject * parent, const char * name )
|
---|
72 | : QObject( parent, name )
|
---|
73 | {
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | /*!
|
---|
78 | Destroys the object and frees any allocated resources.
|
---|
79 | */
|
---|
80 |
|
---|
81 | QEditorFactory::~QEditorFactory()
|
---|
82 | {
|
---|
83 |
|
---|
84 | }
|
---|
85 |
|
---|
86 | static QEditorFactory * defaultfactory = 0;
|
---|
87 | static QCleanupHandler< QEditorFactory > q_cleanup_editor_factory;
|
---|
88 |
|
---|
89 | /*!
|
---|
90 | Returns an instance of a default editor factory.
|
---|
91 | */
|
---|
92 |
|
---|
93 | QEditorFactory * QEditorFactory::defaultFactory()
|
---|
94 | {
|
---|
95 | if( defaultfactory == 0 ){
|
---|
96 | defaultfactory = new QEditorFactory();
|
---|
97 | q_cleanup_editor_factory.add( &defaultfactory );
|
---|
98 | }
|
---|
99 |
|
---|
100 | return defaultfactory;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /*!
|
---|
104 | Replaces the default editor factory with \a factory.
|
---|
105 | \e{QEditorFactory takes ownership of factory, and destroys it
|
---|
106 | when it is no longer needed.}
|
---|
107 | */
|
---|
108 |
|
---|
109 | void QEditorFactory::installDefaultFactory( QEditorFactory * factory )
|
---|
110 | {
|
---|
111 | if( factory == 0 || factory == defaultfactory ) return;
|
---|
112 |
|
---|
113 | if( defaultfactory != 0 ){
|
---|
114 | q_cleanup_editor_factory.remove( &defaultfactory );
|
---|
115 | delete defaultfactory;
|
---|
116 | }
|
---|
117 | defaultfactory = factory;
|
---|
118 | q_cleanup_editor_factory.add( &defaultfactory );
|
---|
119 | }
|
---|
120 |
|
---|
121 | /*!
|
---|
122 | Creates and returns the appropriate editor for the QVariant \a v.
|
---|
123 | If the QVariant is invalid, 0 is returned. The \a parent is passed
|
---|
124 | to the appropriate editor's constructor.
|
---|
125 | */
|
---|
126 |
|
---|
127 | QWidget * QEditorFactory::createEditor( QWidget * parent, const QVariant & v )
|
---|
128 | {
|
---|
129 | QWidget * w = 0;
|
---|
130 | switch( v.type() ){
|
---|
131 | case QVariant::Invalid:
|
---|
132 | w = 0;
|
---|
133 | break;
|
---|
134 | case QVariant::Bool:
|
---|
135 | w = new QComboBox( parent, "qt_editor_bool" );
|
---|
136 | ((QComboBox *) w)->insertItem( "False" );
|
---|
137 | ((QComboBox *) w)->insertItem( "True" );
|
---|
138 | break;
|
---|
139 | case QVariant::UInt:
|
---|
140 | w = new QSpinBox( 0, 999999, 1, parent, "qt_editor_spinbox" );
|
---|
141 | break;
|
---|
142 | case QVariant::Int:
|
---|
143 | w = new QSpinBox( -999999, 999999, 1, parent, "qt_editor_int" );
|
---|
144 | break;
|
---|
145 | case QVariant::String:
|
---|
146 | case QVariant::CString:
|
---|
147 | case QVariant::Double:
|
---|
148 | w = new QLineEdit( parent, "qt_editor_double" );
|
---|
149 | ((QLineEdit*)w)->setFrame( FALSE );
|
---|
150 | break;
|
---|
151 | case QVariant::Date:
|
---|
152 | w = new QDateEdit( parent, "qt_editor_date" );
|
---|
153 | break;
|
---|
154 | case QVariant::Time:
|
---|
155 | w = new QTimeEdit( parent, "qt_editor_time" );
|
---|
156 | break;
|
---|
157 | case QVariant::DateTime:
|
---|
158 | w = new QDateTimeEdit( parent, "qt_editor_datetime" );
|
---|
159 | break;
|
---|
160 | #ifndef QT_NO_LABEL
|
---|
161 | case QVariant::Pixmap:
|
---|
162 | w = new QLabel( parent, "qt_editor_pixmap" );
|
---|
163 | break;
|
---|
164 | #endif
|
---|
165 | case QVariant::Palette:
|
---|
166 | case QVariant::ColorGroup:
|
---|
167 | case QVariant::Color:
|
---|
168 | case QVariant::Font:
|
---|
169 | case QVariant::Brush:
|
---|
170 | case QVariant::Bitmap:
|
---|
171 | case QVariant::Cursor:
|
---|
172 | case QVariant::Map:
|
---|
173 | case QVariant::StringList:
|
---|
174 | case QVariant::Rect:
|
---|
175 | case QVariant::Size:
|
---|
176 | case QVariant::IconSet:
|
---|
177 | case QVariant::Point:
|
---|
178 | case QVariant::PointArray:
|
---|
179 | case QVariant::Region:
|
---|
180 | case QVariant::SizePolicy:
|
---|
181 | case QVariant::ByteArray:
|
---|
182 | default:
|
---|
183 | w = new QWidget( parent, "qt_editor_default" );
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | return w;
|
---|
187 | }
|
---|
188 | #endif // QT_NO_SQL
|
---|