source: trunk/src/sql/qsqlpropertymap.cpp@ 208

Last change on this file since 208 was 196, checked in by rudi, 14 years ago

Add SQL module (currently it isn't build by default, however it's needed for QtDesigner)

File size: 8.7 KB
Line 
1/****************************************************************************
2**
3** Definition of QSqlPropertyMap class
4**
5** Created : 2000-11-20
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 "qsqlpropertymap.h"
38
39#ifndef QT_NO_SQL_FORM
40
41#include "qwidget.h"
42#include "qcleanuphandler.h"
43#include "qmetaobject.h"
44#include "qmap.h"
45
46class QSqlPropertyMapPrivate
47{
48public:
49 QSqlPropertyMapPrivate() {}
50 QMap< QString, QString > propertyMap;
51};
52
53/*!
54 \class QSqlPropertyMap qsqlpropertymap.h
55 \brief The QSqlPropertyMap class is used to map widgets to SQL fields.
56
57 \ingroup database
58 \module sql
59
60 The SQL module uses Qt \link properties.html object
61 properties\endlink to insert and extract values from editor
62 widgets.
63
64 This class is used to map editors to SQL fields. This works by
65 associating SQL editor class names to the properties used to
66 insert and extract values to/from the editor.
67
68 For example, a QLineEdit can be used to edit text strings and
69 other data types in QDataTables or QSqlForms. Several properties
70 are defined in QLineEdit, but only the \e text property is used to
71 insert and extract text from a QLineEdit. Both QDataTable and
72 QSqlForm use the global QSqlPropertyMap for inserting and
73 extracting values to and from an editor widget. The global
74 property map defines several common widgets and properties that
75 are suitable for many applications. You can add and remove widget
76 properties to suit your specific needs.
77
78 If you want to use custom editors with your QDataTable or
79 QSqlForm, you must install your own QSqlPropertyMap for that table
80 or form. Example:
81
82 \code
83 QSqlPropertyMap *myMap = new QSqlPropertyMap();
84 QSqlForm *myForm = new QSqlForm( this );
85 MyEditor myEditor( this );
86
87 // Set the QSqlForm's record buffer to the update buffer of
88 // a pre-existing QSqlCursor called 'cur'.
89 myForm->setRecord( cur->primeUpdate() );
90
91 // Install the customized map
92 myMap->insert( "MyEditor", "content" );
93 myForm->installPropertyMap( myMap ); // myForm now owns myMap
94 ...
95 // Insert a field into the form that uses a myEditor to edit the
96 // field 'somefield'
97 myForm->insert( &myEditor, "somefield" );
98
99 // Update myEditor with the value from the mapped database field
100 myForm->readFields();
101 ...
102 // Let the user edit the form
103 ...
104 // Update the database fields with the values in the form
105 myForm->writeFields();
106 ...
107 \endcode
108
109 You can also replace the global QSqlPropertyMap that is used by
110 default. (Bear in mind that QSqlPropertyMap takes ownership of the
111 new default map.)
112
113 \code
114 QSqlPropertyMap *myMap = new QSqlPropertyMap;
115
116 myMap->insert( "MyEditor", "content" );
117 QSqlPropertyMap::installDefaultMap( myMap );
118 ...
119 \endcode
120
121 \sa QDataTable, QSqlForm, QSqlEditorFactory
122*/
123
124/*!
125
126Constructs a QSqlPropertyMap.
127
128The default property mappings used by Qt widgets are:
129\table
130\header \i Widgets \i Property
131\row \i \l QCheckBox,
132 \l QRadioButton
133 \i checked
134\row \i \l QComboBox,
135 \l QListBox
136 \i currentItem
137\row \i \l QDateEdit
138 \i date
139\row \i \l QDateTimeEdit
140 \i dateTime
141\row \i \l QTextBrowser
142 \i source
143\row \i \l QButton,
144 \l QDial,
145 \l QLabel,
146 \l QLineEdit,
147 \l QMultiLineEdit,
148 \l QPushButton,
149 \l QTextEdit,
150 \i text
151\row \i \l QTimeEdit
152 \i time
153\row \i \l QLCDNumber,
154 \l QScrollBar
155 \l QSlider,
156 \l QSpinBox
157 \i value
158\endtable
159*/
160
161QSqlPropertyMap::QSqlPropertyMap()
162{
163 d = new QSqlPropertyMapPrivate();
164 const struct MapData {
165 const char *classname;
166 const char *property;
167 } mapData[] = {
168 { "QButton", "text" },
169 { "QCheckBox", "checked" },
170 { "QRadioButton", "checked" },
171 { "QComboBox", "currentItem" },
172 { "QDateEdit", "date" },
173 { "QDateTimeEdit", "dateTime" },
174 { "QDial", "value" },
175 { "QLabel", "text" },
176 { "QLCDNumber", "value" },
177 { "QLineEdit", "text" },
178 { "QListBox", "currentItem" },
179 { "QMultiLineEdit", "text" },
180 { "QPushButton", "text" },
181 { "QScrollBar", "value" },
182 { "QSlider", "value" },
183 { "QSpinBox", "value" },
184 { "QTextBrowser", "source" },
185 { "QTextEdit", "text" },
186 { "QTextView", "text" },
187 { "QTimeEdit", "time" }
188 };
189
190 const MapData *m = mapData;
191 for ( uint i = 0; i < sizeof(mapData)/sizeof(MapData); i++, m++ )
192 d->propertyMap.insert( m->classname, m->property );
193}
194
195/*!
196 Destroys the QSqlPropertyMap.
197
198 Note that if the QSqlPropertyMap is installed with
199 installPropertyMap() the object it was installed into, e.g. the
200 QSqlForm, takes ownership and will delete the QSqlPropertyMap when
201 necessary.
202*/
203QSqlPropertyMap::~QSqlPropertyMap()
204{
205 delete d;
206}
207
208/*!
209 Returns the mapped property of \a widget as a QVariant.
210*/
211QVariant QSqlPropertyMap::property( QWidget * widget )
212{
213 if( !widget ) return QVariant();
214 const QMetaObject* mo = widget->metaObject();
215 while ( mo && !d->propertyMap.contains( QString( mo->className() ) ) )
216 mo = mo->superClass();
217
218 if ( !mo ) {
219#ifdef QT_CHECK_RANGE
220 qWarning("QSqlPropertyMap::property: %s does not exist", widget->metaObject()->className() );
221#endif
222 return QVariant();
223 }
224 return widget->property( d->propertyMap[ mo->className() ] );
225}
226
227/*!
228 Sets the property of \a widget to \a value.
229*/
230void QSqlPropertyMap::setProperty( QWidget * widget, const QVariant & value )
231{
232 if( !widget ) return;
233
234 QMetaObject* mo = widget->metaObject();
235 while ( mo && !d->propertyMap.contains( QString( mo->className() ) ) )
236 mo = mo->superClass();
237 if ( !mo ) {
238#ifdef QT_CHECK_RANGE
239 qWarning("QSqlPropertyMap::setProperty: %s not handled by QSqlPropertyMap", widget->metaObject()->className() );
240#endif
241 return;
242 }
243
244 widget->setProperty( d->propertyMap[ mo->className() ], value );
245}
246
247/*!
248 Insert a new classname/property pair, which is used for custom SQL
249 field editors. There \e must be a \c Q_PROPERTY clause in the \a
250 classname class declaration for the \a property.
251*/
252void QSqlPropertyMap::insert( const QString & classname,
253 const QString & property )
254{
255 d->propertyMap[ classname ] = property;
256}
257
258/*!
259 Removes \a classname from the map.
260*/
261void QSqlPropertyMap::remove( const QString & classname )
262{
263 d->propertyMap.remove( classname );
264}
265
266static QSqlPropertyMap * defaultmap = 0;
267static QCleanupHandler< QSqlPropertyMap > qsql_cleanup_property_map;
268
269/*!
270 Returns the application global QSqlPropertyMap.
271*/
272QSqlPropertyMap * QSqlPropertyMap::defaultMap()
273{
274 if( defaultmap == 0 ){
275 defaultmap = new QSqlPropertyMap();
276 qsql_cleanup_property_map.add( &defaultmap );
277 }
278 return defaultmap;
279}
280
281/*!
282 Replaces the global default property map with \a map. All
283 QDataTable and QSqlForm instantiations will use this new map for
284 inserting and extracting values to and from editors.
285 \e{QSqlPropertyMap takes ownership of \a map, and destroys it
286 when it is no longer needed.}
287*/
288void QSqlPropertyMap::installDefaultMap( QSqlPropertyMap * map )
289{
290 if( map == 0 ) return;
291
292 if( defaultmap != 0 ){
293 qsql_cleanup_property_map.remove( &defaultmap );
294 delete defaultmap;
295 }
296 defaultmap = map;
297 qsql_cleanup_property_map.add( &defaultmap );
298}
299
300#endif // QT_NO_SQL_FORM
Note: See TracBrowser for help on using the repository browser.