source: trunk/tools/designer/shared/domtool.cpp@ 10

Last change on this file since 10 was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 14.3 KB
Line 
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qt Designer.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
12** licenses may use this file in accordance with the Qt Commercial License
13** Agreement provided with the Software.
14**
15** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17**
18** See http://www.trolltech.com/gpl/ for GPL licensing information.
19** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
20** information about Qt Commercial License Agreements.
21**
22** Contact info@trolltech.com if any conditions of this licensing are
23** not clear to you.
24**
25**********************************************************************/
26
27#include "domtool.h"
28
29#include <qsizepolicy.h>
30#include <qcolor.h>
31#include <qcursor.h>
32#include <qdatetime.h>
33#include <qrect.h>
34#include <qsize.h>
35#include <qfont.h>
36#include <qdom.h>
37
38/*!
39 \class DomTool domtool.h
40 \brief Tools for the dom
41
42 A collection of static functions used by Resource (part of the
43 designer) and Uic.
44
45*/
46
47/*!
48 Returns the contents of property \a name of object \a e as
49 variant or the variant passed as \a defValue if the property does
50 not exist.
51
52 \sa hasProperty()
53*/
54QVariant DomTool::readProperty( const QDomElement& e, const QString& name, const QVariant& defValue, QString& comment )
55{
56 QDomElement n;
57 for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
58 if ( n.tagName() == "property" ) {
59 if ( n.attribute( "name" ) != name )
60 continue;
61 return elementToVariant( n.firstChild().toElement(), defValue, comment );
62 }
63 }
64 return defValue;
65}
66
67
68/*!
69 \overload
70 */
71QVariant DomTool::readProperty( const QDomElement& e, const QString& name, const QVariant& defValue )
72{
73 QString comment;
74 return readProperty( e, name, defValue, comment );
75}
76
77/*!
78 Returns wheter object \a e defines property \a name or not.
79
80 \sa readProperty()
81 */
82bool DomTool::hasProperty( const QDomElement& e, const QString& name )
83{
84 QDomElement n;
85 for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
86 if ( n.tagName() == "property" ) {
87 if ( n.attribute( "name" ) != name )
88 continue;
89 return TRUE;
90 }
91 }
92 return FALSE;
93}
94
95QStringList DomTool::propertiesOfType( const QDomElement& e, const QString& type )
96{
97 QStringList result;
98 QDomElement n;
99 for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
100 if ( n.tagName() == "property" ) {
101 QDomElement n2 = n.firstChild().toElement();
102 if ( n2.tagName() == type )
103 result += n.attribute( "name" );
104 }
105 }
106 return result;
107}
108
109
110QVariant DomTool::elementToVariant( const QDomElement& e, const QVariant& defValue )
111{
112 QString dummy;
113 return elementToVariant( e, defValue, dummy );
114}
115
116/*!
117 Interprets element \a e as variant and returns the result of the interpretation.
118 */
119QVariant DomTool::elementToVariant( const QDomElement& e, const QVariant& defValue, QString &comment )
120{
121 QVariant v;
122 if ( e.tagName() == "rect" ) {
123 QDomElement n3 = e.firstChild().toElement();
124 int x = 0, y = 0, w = 0, h = 0;
125 while ( !n3.isNull() ) {
126 if ( n3.tagName() == "x" )
127 x = n3.firstChild().toText().data().toInt();
128 else if ( n3.tagName() == "y" )
129 y = n3.firstChild().toText().data().toInt();
130 else if ( n3.tagName() == "width" )
131 w = n3.firstChild().toText().data().toInt();
132 else if ( n3.tagName() == "height" )
133 h = n3.firstChild().toText().data().toInt();
134 n3 = n3.nextSibling().toElement();
135 }
136 v = QVariant( QRect( x, y, w, h ) );
137 } else if ( e.tagName() == "point" ) {
138 QDomElement n3 = e.firstChild().toElement();
139 int x = 0, y = 0;
140 while ( !n3.isNull() ) {
141 if ( n3.tagName() == "x" )
142 x = n3.firstChild().toText().data().toInt();
143 else if ( n3.tagName() == "y" )
144 y = n3.firstChild().toText().data().toInt();
145 n3 = n3.nextSibling().toElement();
146 }
147 v = QVariant( QPoint( x, y ) );
148 } else if ( e.tagName() == "size" ) {
149 QDomElement n3 = e.firstChild().toElement();
150 int w = 0, h = 0;
151 while ( !n3.isNull() ) {
152 if ( n3.tagName() == "width" )
153 w = n3.firstChild().toText().data().toInt();
154 else if ( n3.tagName() == "height" )
155 h = n3.firstChild().toText().data().toInt();
156 n3 = n3.nextSibling().toElement();
157 }
158 v = QVariant( QSize( w, h ) );
159 } else if ( e.tagName() == "color" ) {
160 v = QVariant( readColor( e ) );
161 } else if ( e.tagName() == "font" ) {
162 QDomElement n3 = e.firstChild().toElement();
163 QFont f( defValue.toFont() );
164 while ( !n3.isNull() ) {
165 if ( n3.tagName() == "family" )
166 f.setFamily( n3.firstChild().toText().data() );
167 else if ( n3.tagName() == "pointsize" )
168 f.setPointSize( n3.firstChild().toText().data().toInt() );
169 else if ( n3.tagName() == "bold" )
170 f.setBold( n3.firstChild().toText().data().toInt() );
171 else if ( n3.tagName() == "italic" )
172 f.setItalic( n3.firstChild().toText().data().toInt() );
173 else if ( n3.tagName() == "underline" )
174 f.setUnderline( n3.firstChild().toText().data().toInt() );
175 else if ( n3.tagName() == "strikeout" )
176 f.setStrikeOut( n3.firstChild().toText().data().toInt() );
177 n3 = n3.nextSibling().toElement();
178 }
179 v = QVariant( f );
180 } else if ( e.tagName() == "string" ) {
181 v = QVariant( e.firstChild().toText().data() );
182 QDomElement n = e;
183 n = n.nextSibling().toElement();
184 if ( n.tagName() == "comment" )
185 comment = n.firstChild().toText().data();
186 } else if ( e.tagName() == "cstring" ) {
187 v = QVariant( QCString( e.firstChild().toText().data() ) );
188 } else if ( e.tagName() == "number" ) {
189 bool ok = TRUE;
190 v = QVariant( e.firstChild().toText().data().toInt( &ok ) );
191 if ( !ok )
192 v = QVariant( e.firstChild().toText().data().toDouble() );
193 } else if ( e.tagName() == "bool" ) {
194 QString t = e.firstChild().toText().data();
195 v = QVariant( t == "true" || t == "1", 0 );
196 } else if ( e.tagName() == "pixmap" ) {
197 v = QVariant( e.firstChild().toText().data() );
198 } else if ( e.tagName() == "iconset" ) {
199 v = QVariant( e.firstChild().toText().data() );
200 } else if ( e.tagName() == "image" ) {
201 v = QVariant( e.firstChild().toText().data() );
202 } else if ( e.tagName() == "enum" ) {
203 v = QVariant( e.firstChild().toText().data() );
204 } else if ( e.tagName() == "set" ) {
205 v = QVariant( e.firstChild().toText().data() );
206 } else if ( e.tagName() == "sizepolicy" ) {
207 QDomElement n3 = e.firstChild().toElement();
208 QSizePolicy sp;
209 while ( !n3.isNull() ) {
210 if ( n3.tagName() == "hsizetype" )
211 sp.setHorData( (QSizePolicy::SizeType)n3.firstChild().toText().data().toInt() );
212 else if ( n3.tagName() == "vsizetype" )
213 sp.setVerData( (QSizePolicy::SizeType)n3.firstChild().toText().data().toInt() );
214 else if ( n3.tagName() == "horstretch" )
215 sp.setHorStretch( n3.firstChild().toText().data().toInt() );
216 else if ( n3.tagName() == "verstretch" )
217 sp.setVerStretch( n3.firstChild().toText().data().toInt() );
218 n3 = n3.nextSibling().toElement();
219 }
220 v = QVariant( sp );
221 } else if ( e.tagName() == "cursor" ) {
222 v = QVariant( QCursor( e.firstChild().toText().data().toInt() ) );
223 } else if ( e.tagName() == "stringlist" ) {
224 QStringList lst;
225 QDomElement n;
226 for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() )
227 lst << n.firstChild().toText().data();
228 v = QVariant( lst );
229 } else if ( e.tagName() == "date" ) {
230 QDomElement n3 = e.firstChild().toElement();
231 int y, m, d;
232 y = m = d = 0;
233 while ( !n3.isNull() ) {
234 if ( n3.tagName() == "year" )
235 y = n3.firstChild().toText().data().toInt();
236 else if ( n3.tagName() == "month" )
237 m = n3.firstChild().toText().data().toInt();
238 else if ( n3.tagName() == "day" )
239 d = n3.firstChild().toText().data().toInt();
240 n3 = n3.nextSibling().toElement();
241 }
242 v = QVariant( QDate( y, m, d ) );
243 } else if ( e.tagName() == "time" ) {
244 QDomElement n3 = e.firstChild().toElement();
245 int h, m, s;
246 h = m = s = 0;
247 while ( !n3.isNull() ) {
248 if ( n3.tagName() == "hour" )
249 h = n3.firstChild().toText().data().toInt();
250 else if ( n3.tagName() == "minute" )
251 m = n3.firstChild().toText().data().toInt();
252 else if ( n3.tagName() == "second" )
253 s = n3.firstChild().toText().data().toInt();
254 n3 = n3.nextSibling().toElement();
255 }
256 v = QVariant( QTime( h, m, s ) );
257 } else if ( e.tagName() == "datetime" ) {
258 QDomElement n3 = e.firstChild().toElement();
259 int h, mi, s, y, mo, d ;
260 h = mi = s = y = mo = d = 0;
261 while ( !n3.isNull() ) {
262 if ( n3.tagName() == "hour" )
263 h = n3.firstChild().toText().data().toInt();
264 else if ( n3.tagName() == "minute" )
265 mi = n3.firstChild().toText().data().toInt();
266 else if ( n3.tagName() == "second" )
267 s = n3.firstChild().toText().data().toInt();
268 else if ( n3.tagName() == "year" )
269 y = n3.firstChild().toText().data().toInt();
270 else if ( n3.tagName() == "month" )
271 mo = n3.firstChild().toText().data().toInt();
272 else if ( n3.tagName() == "day" )
273 d = n3.firstChild().toText().data().toInt();
274 n3 = n3.nextSibling().toElement();
275 }
276 v = QVariant( QDateTime( QDate( y, mo, d ), QTime( h, mi, s ) ) );
277 }
278 return v;
279}
280
281
282/*! Returns the color which is returned in the dom element \a e.
283 */
284
285QColor DomTool::readColor( const QDomElement &e )
286{
287 QDomElement n = e.firstChild().toElement();
288 int r= 0, g = 0, b = 0;
289 while ( !n.isNull() ) {
290 if ( n.tagName() == "red" )
291 r = n.firstChild().toText().data().toInt();
292 else if ( n.tagName() == "green" )
293 g = n.firstChild().toText().data().toInt();
294 else if ( n.tagName() == "blue" )
295 b = n.firstChild().toText().data().toInt();
296 n = n.nextSibling().toElement();
297 }
298
299 return QColor( r, g, b );
300}
301
302/*!
303 Returns the contents of attribute \a name of object \a e as
304 variant or the variant passed as \a defValue if the attribute does
305 not exist.
306
307 \sa hasAttribute()
308 */
309QVariant DomTool::readAttribute( const QDomElement& e, const QString& name, const QVariant& defValue, QString& comment )
310{
311 QDomElement n;
312 for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
313 if ( n.tagName() == "attribute" ) {
314 if ( n.attribute( "name" ) != name )
315 continue;
316 return elementToVariant( n.firstChild().toElement(), defValue, comment );
317 }
318 }
319 return defValue;
320}
321
322/*!
323 \overload
324*/
325QVariant DomTool::readAttribute( const QDomElement& e, const QString& name, const QVariant& defValue )
326{
327 QString comment;
328 return readAttribute( e, name, defValue, comment );
329}
330
331/*!
332 Returns wheter object \a e defines attribute \a name or not.
333
334 \sa readAttribute()
335 */
336bool DomTool::hasAttribute( const QDomElement& e, const QString& name )
337{
338 QDomElement n;
339 for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
340 if ( n.tagName() == "attribute" ) {
341 if ( n.attribute( "name" ) != name )
342 continue;
343 return TRUE;
344 }
345 }
346 return FALSE;
347}
348
349static bool toBool( const QString& s )
350{
351 return s == "true" || s.toInt() != 0;
352}
353
354/*!
355 Convert Qt 2.x format to Qt 3.0 format if necessary
356*/
357void DomTool::fixDocument( QDomDocument& doc )
358{
359 QDomElement e;
360 QDomNode n;
361 QDomNodeList nl;
362 int i = 0;
363
364 e = doc.firstChild().toElement();
365 if ( e.tagName() != "UI" )
366 return;
367
368 // latest version, don't do anything
369 if ( e.hasAttribute("version") && e.attribute("version").toDouble() > 3.0 )
370 return;
371
372 nl = doc.elementsByTagName( "property" );
373
374 // in 3.0, we need to fix a spelling error
375 if ( e.hasAttribute("version") && e.attribute("version").toDouble() == 3.0 ) {
376 for ( i = 0; i < (int) nl.length(); i++ ) {
377 QDomElement el = nl.item(i).toElement();
378 QString s = el.attribute( "name" );
379 if ( s == "resizeable" ) {
380 el.removeAttribute( "name" );
381 el.setAttribute( "name", "resizable" );
382 }
383 }
384 return;
385 }
386
387
388 // in versions smaller than 3.0 we need to change more
389 e.setAttribute( "version", 3.0 );
390
391 e.setAttribute("stdsetdef", 1 );
392 for ( i = 0; i < (int) nl.length(); i++ ) {
393 e = nl.item(i).toElement();
394 QString name;
395 QDomElement n2 = e.firstChild().toElement();
396 if ( n2.tagName() == "name" ) {
397 name = n2.firstChild().toText().data();
398 if ( name == "resizeable" )
399 e.setAttribute( "name", "resizable" );
400 else
401 e.setAttribute( "name", name );
402 e.removeChild( n2 );
403 }
404 bool stdset = toBool( e.attribute( "stdset" ) );
405 if ( stdset || name == "toolTip" || name == "whatsThis" ||
406 name == "buddy" ||
407 e.parentNode().toElement().tagName() == "item" ||
408 e.parentNode().toElement().tagName() == "spacer" ||
409 e.parentNode().toElement().tagName() == "column"
410 )
411 e.removeAttribute( "stdset" );
412 else
413 e.setAttribute( "stdset", 0 );
414 }
415
416 nl = doc.elementsByTagName( "attribute" );
417 for ( i = 0; i < (int) nl.length(); i++ ) {
418 e = nl.item(i).toElement();
419 QString name;
420 QDomElement n2 = e.firstChild().toElement();
421 if ( n2.tagName() == "name" ) {
422 name = n2.firstChild().toText().data();
423 e.setAttribute( "name", name );
424 e.removeChild( n2 );
425 }
426 }
427
428 nl = doc.elementsByTagName( "image" );
429 for ( i = 0; i < (int) nl.length(); i++ ) {
430 e = nl.item(i).toElement();
431 QString name;
432 QDomElement n2 = e.firstChild().toElement();
433 if ( n2.tagName() == "name" ) {
434 name = n2.firstChild().toText().data();
435 e.setAttribute( "name", name );
436 e.removeChild( n2 );
437 }
438 }
439
440 nl = doc.elementsByTagName( "widget" );
441 for ( i = 0; i < (int) nl.length(); i++ ) {
442 e = nl.item(i).toElement();
443 QString name;
444 QDomElement n2 = e.firstChild().toElement();
445 if ( n2.tagName() == "class" ) {
446 name = n2.firstChild().toText().data();
447 e.setAttribute( "class", name );
448 e.removeChild( n2 );
449 }
450 }
451
452}
453
Note: See TracBrowser for help on using the repository browser.