source: trunk/tools/designer/plugins/dlg/dlg2ui.cpp

Last change on this file was 197, checked in by rudi, 14 years ago

Added QtDesigner

File size: 52.4 KB
Line 
1/**********************************************************************
2**
3** Converts a Qt Architect 2.1+ .dlg file into a .ui file.
4**
5** Copyright (C) 2000-2007 Trolltech ASA. All rights reserved.
6**
7** This file is part of Qt Designer.
8**
9** This file may be distributed and/or modified under the terms of the
10** GNU General Public License version 2 as published by the Free Software
11** Foundation and appearing in the file LICENSE.GPL included in the
12** packaging of this file.
13**
14** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
15** licenses may use this file in accordance with the Qt Commercial License
16** Agreement provided with the Software.
17**
18** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20**
21** See http://www.trolltech.com/gpl/ for GPL licensing information.
22** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
23** information about Qt Commercial License Agreements.
24**
25** Contact info@trolltech.com if any conditions of this licensing are
26** not clear to you.
27**
28**********************************************************************/
29
30#include "dlg2ui.h"
31#include <qfile.h>
32#include <qframe.h>
33#include <qmessagebox.h>
34#include <qregexp.h>
35#include <qtextstream.h>
36
37/*
38 Possible improvements:
39
40 1. Convert layout stretch factors to size policy stretches, now
41 that Qt Designer supports the latter.
42*/
43
44/*
45 These big tables could be more or less eliminated by using Qt's
46 QMetaObject and QMetaProperty classes. However, the interface of
47 these classes is unwieldy for an otherwise non-GUI program like this
48 one, as we would have to create one dummy object for most QObject
49 subclasses in Qt. Let's take the safe road.
50*/
51
52static const char *widgetTypes[] = {
53 "Button", "ButtonGroup", "CheckBox", "ComboBox", "Dial", "DlgWidget",
54 "Frame", "Grid", "GroupBox", "HBox", "HButtonGroup", "HGroupBox",
55 "IconView", "LCDNumber", "Label", "LineEdit", "ListBox", "ListView",
56 "MenuBar", "MultiLineEdit", "ProgressBar", "PushButton", "RadioButton",
57 "ScrollBar", "ScrollView", "Slider", "SpinBox", "Splitter", "TabBar",
58 "TextBrowser", "TextView", "User", "VBox", "VButtonGroup", "VGroupBox", 0
59};
60
61/*
62 This table maps Qt Architect properties to Qt Designer properties.
63 If there is no corresponding Qt Designer property, qtName is 0 and
64 the property can be handled explicitly.
65*/
66static const struct {
67 const char *widgetName;
68 const char *architectName;
69 const char *qtName;
70 const char *type;
71} propertyDefs[] = {
72 { "Button", "AutoRepeat", "autoRepeat", "boolean" },
73 { "Button", "AutoResize", 0, 0 },
74 { "Button", "Text", "text", "qstring" },
75 { "ButtonGroup", "Exclusive", "exclusive", "boolean" },
76 { "ButtonGroup", "RadioButtonExclusive", "radioButtonExclusive",
77 "boolean" },
78 { "CheckBox", "Checked", "checked", "boolean" },
79 { "ComboBox", "AutoCompletion", "autoCompletion", "boolean" },
80 { "ComboBox", "AutoResize", 0, 0 },
81 { "ComboBox", "DuplicatesEnabled", "duplicatesEnabled", "boolean" },
82 { "ComboBox", "MaxCount", "maxCount", "integer" },
83 { "ComboBox", "Policy", "insertionPolicy", "enum" },
84 { "ComboBox", "SizeLimit", "sizeLimit", "integer" },
85 { "ComboBox", "Style", 0, 0 },
86 { "Dial", "Initial", "value", "integer" },
87 { "Dial", "LineStep", "lineStep", "integer" },
88 { "Dial", "MaxValue", "maxValue", "integer" },
89 { "Dial", "MinValue", "minValue", "integer" },
90 { "Dial", "NotchTarget", "notchTarget", "double" },
91 { "Dial", "PageStep", "pageStep", "integer" },
92 { "Dial", "ShowNotches", "notchesVisible", "boolean" },
93 { "Dial", "Tracking", "tracking", "boolean" },
94 { "Dial", "Wrapping", "wrapping", "boolean" },
95 { "DlgWidget", "AdjustSize", 0, 0 },
96 { "DlgWidget", "BackgroundMode", "backgroundMode", "enum" },
97 { "DlgWidget", "BackgroundOrigin", "backgroundOrigin", "enum" },
98 { "DlgWidget", "BackgroundPixmap", "backgroundPixmap", "qpixmap" },
99 { "DlgWidget", "DataLenName", 0, 0 },
100 { "DlgWidget", "DataVarName", 0, 0 },
101 { "DlgWidget", "Enabled", "enabled", "boolean" },
102 { "DlgWidget", "FocusPolicy", "focusPolicy", "enum" },
103 { "DlgWidget", "Font", "font", "qfont" },
104 { "DlgWidget", "FontPropagation", 0, 0 },
105 { "DlgWidget", "MaximumSize", "maximumSize", "qsize" },
106 { "DlgWidget", "MinimumSize", "minimumSize", "qsize" },
107 { "DlgWidget", "Name", 0, 0 },
108 { "DlgWidget", "Palette", "palette", "qpalette" },
109 { "DlgWidget", "PalettePropagation", 0, 0 },
110 { "DlgWidget", "ReadPixmapFromData", 0, 0 },
111 { "DlgWidget", "Rect", 0, 0 },
112 { "DlgWidget", "SignalConnection", 0, 0 },
113 { "DlgWidget", "UseBackgroudPixmap", 0, 0 },
114 { "DlgWidget", "Variable", 0, 0 },
115 { "Frame", "FrameMargin", "margin", "integer" },
116 { "Frame", "LineWidth", "lineWidth", "integer" },
117 { "Frame", "MidLineWidth", "midLineWidth", "integer" },
118 { "Frame", "Style", 0, 0 },
119 { "GroupBox", "Title", "title", "qstring" },
120 { "IconView", "AlignMode", 0, 0 },
121 { "IconView", "Aligning", 0, 0 },
122 { "IconView", "Arrangement", "arrangement", "enum" },
123 { "IconView", "AutoArrange", "autoArrange", "boolean" },
124 { "IconView", "EnableMoveItems", "itemsMovable", "boolean" },
125 { "IconView", "GridX", "gridX", "integer" },
126 { "IconView", "GridY", "gridY", "integer" },
127 { "IconView", "ItemTextPos", "itemTextPos", "enum" },
128 { "IconView", "ItemsMovable", "itemsMovable", "boolean" },
129 { "IconView", "MaxItemTextLength", "maxItemTextLength", "integer" },
130 { "IconView", "MaxItemWidth", "maxItemWidth", "integer" },
131 { "IconView", "ResizeMode", "resizeMode", "enum" },
132 { "IconView", "SelectionMode", "selectionMode", "enum" },
133 { "IconView", "ShowToolTips", "showToolTips", "boolean" },
134 { "IconView", "SortAscending", "sortDirection", "bool" },
135 { "IconView", "Spacing", "spacing", "integer" },
136 { "IconView", "WordWrapIconText", "wordWrapIconText", "boolean" },
137 { "LCDNumber", "Digits", "numDigits", "integer" },
138 { "LCDNumber", "Mode", "mode", "enum" },
139 { "LCDNumber", "SegmentStyle", "segmentStyle", "enum" },
140 { "LCDNumber", "SmallDecimalPoint", "smallDecimalPoint", "boolean" },
141 { "LCDNumber", "Value", 0, 0 },
142 { "Label", "AutoResize", 0, 0 },
143 { "Label", "Indent", "indent", "integer" },
144 { "Label", "Text", "text", "qstring" },
145 { "Label", "TextFormat", "textFormat", "enum" },
146 { "LineEdit", "EchoMode", "echoMode", "enum" },
147 { "LineEdit", "FrameShown", "frame", "boolean" },
148 { "LineEdit", "MaxLength", "maxLength", "integer" },
149 { "LineEdit", "Text", "text", "qstring" },
150 { "ListBox", "AutoScroll", 0, 0 },
151 { "ListBox", "AutoUpdate", 0, 0 },
152 { "ListBox", "ColumnMode", "columnMode", "enum" },
153 { "ListBox", "DragSelect", 0, 0 },
154 { "ListBox", "RowMode", "rowMode", "enum" },
155 { "ListBox", "SelectionMode", "selectionMode", "enum" },
156 { "ListBox", "SmoothScrolling", 0, 0 },
157 { "ListView", "AllColumnsShowFocus", "allColumnsShowFocus", "boolean" },
158 { "ListView", "HeaderInformation", 0, 0 },
159 { "ListView", "ItemMargin", "itemMargin", "integer" },
160 { "ListView", "MultiSelection", "multiSelection", "boolean" },
161 { "ListView", "RootIsDecorated", "rootIsDecorated", "boolean" },
162 { "ListView", "TreeStepSize", "treeStepSize", "boolean" },
163 { "MultiLineEdit", "AutoUpdate", 0, 0 },
164 { "MultiLineEdit", "EchoMode", 0, 0 },
165 { "MultiLineEdit", "HorizontalMargin", 0, 0 },
166 { "MultiLineEdit", "MaxLength", 0, 0 },
167 { "MultiLineEdit", "MaxLineLength", 0, 0 },
168 { "MultiLineEdit", "MaxLines", 0, 0 },
169 { "MultiLineEdit", "OverwriteMode", 0, 0 },
170 { "MultiLineEdit", "ReadOnly", 0, 0 },
171 { "MultiLineEdit", "Text", 0, 0 },
172 { "MultiLineEdit", "UndoDepth", "undoDepth", "integer" },
173 { "MultiLineEdit", "UndoEnabled", 0, 0 },
174 { "MultiLineEdit", "WordWrap", 0, 0 },
175 { "MultiLineEdit", "WrapColumnOrWidth", 0, 0 },
176 { "MultiLineEdit", "WrapPolicy", 0, 0 },
177 { "ProgressBar", "CenterIndicator", "centerIndicator", "boolean" },
178 { "ProgressBar", "IndicatorFollowsStyle", "indicatorFollowsStyle",
179 "boolean" },
180 { "ProgressBar", "Progress", "progress", "integer" },
181 { "ProgressBar", "TotalSteps", "totalSteps", "integer" },
182 { "PushButton", "AutoDefault", "autoDefault", "boolean" },
183 { "PushButton", "Default", "default", "boolean" },
184 { "PushButton", "IsMenuButton", 0, 0 },
185 { "PushButton", "ToggleButton", "toggleButton", "boolean" },
186 { "RadioButton", "Checked", "checked", "boolean" },
187 { "ScrollBar", "Initial", "value", "integer" },
188 { "ScrollBar", "LineStep", "lineStep", "integer" },
189 { "ScrollBar", "MaxValue", "maxValue", "integer" },
190 { "ScrollBar", "MinValue", "minValue", "integer" },
191 { "ScrollBar", "Orientation", "orientation", "enum" },
192 { "ScrollBar", "PageStep", "pageStep", "integer" },
193 { "ScrollBar", "Tracking", "tracking", "boolean" },
194 { "ScrollView", "DragAutoScroll", "dragAutoScroll", "boolean" },
195 { "ScrollView", "HScrollBarMode", "hScrollBarMode", "enum" },
196 { "ScrollView", "ResizePolicy", "resizePolicy", "enum" },
197 { "ScrollView", "VScrollBarMode", "vScrollBarMode", "enum" },
198 { "Slider", "Initial", "value", "integer" },
199 { "Slider", "LineStep", "lineStep", "integer" },
200 { "Slider", "MaxValue", "maxValue", "integer" },
201 { "Slider", "MinValue", "minValue", "integer" },
202 { "Slider", "Orientation", "orientation", "enum" },
203 { "Slider", "PageStep", "pageStep", "integer" },
204 { "Slider", "TickInterval", "tickInterval", "integer" },
205 { "Slider", "Tickmarks", "tickmarks", "enum" },
206 { "Slider", "Tracking", "tracking", "boolean" },
207 { "SpinBox", "ButtonSymbols", "buttonSymbols", "enum" },
208 { "SpinBox", "MaxValue", "maxValue", "integer" },
209 { "SpinBox", "MinValue", "minValue", "integer" },
210 { "SpinBox", "Prefix", "prefix", "qstring" },
211 { "SpinBox", "SpecialValue", "specialValueText", "qstring" },
212 { "SpinBox", "Step", "lineStep", "integer" },
213 { "SpinBox", "Suffix", "suffix", "qstring" },
214 { "SpinBox", "Wrapping", "wrapping", "boolean" },
215 { "Splitter", "OpaqueResize", 0, 0 },
216 { "Splitter", "Orientation", "orientation", "enum" },
217 { "TabBar", "Shape", "shape", "enum" },
218 { "TabBar", "TabNames", 0, 0 },
219 { "TextView", "Context", 0, 0 },
220 { "TextView", "LinkUnderline", "linkUnderline", "boolean" },
221 { "TextView", "Text", "text", "qstring" },
222 { "TextView", "TextFormat", "textFormat", "enum" },
223 { "User", "UserClassHeader", 0, 0 },
224 { "User", "UserClassName", 0, 0 },
225 { 0, 0, 0, 0 }
226};
227
228/*
229 This is the number of supported color groups in a palette, and
230 supported color roles in a color group. Changing these constants is
231 dangerous.
232*/
233static const int NumColorRoles = 14;
234
235static bool isTrue( const QString& val )
236{
237 return val.lower() == QString( "true" );
238}
239
240static AttributeMap attribute( const QString& name, const QString& val )
241{
242 AttributeMap attr;
243 attr.insert( name, val );
244 return attr;
245}
246
247static QString entitize( const QString& str )
248{
249 QString t = str;
250 t.replace( '&', QString("&amp;") );
251 t.replace( '>', QString("&gt;") );
252 t.replace( '<', QString("&lt;") );
253 t.replace( '"', QString("&quot;") );
254 t.replace( '\'', QString("&apos;") );
255 return t;
256}
257
258QString Dlg2Ui::alias( const QString& name ) const
259{
260 if ( yyAliasMap.contains(name) )
261 return yyAliasMap[name];
262 else
263 return name;
264}
265
266QString Dlg2Ui::opening( const QString& tag, const AttributeMap& attr )
267{
268 QString t = QChar( '<' ) + tag;
269 AttributeMap::ConstIterator a = attr.begin();
270 while ( a != attr.end() ) {
271 t += QChar( ' ' ) + a.key() + QString( "=\"" ) + entitize( *a ) +
272 QChar( '"' );
273 ++a;
274 }
275 t += QChar( '>' );
276 return t;
277}
278
279QString Dlg2Ui::closing( const QString& tag )
280{
281 return opening( QChar('/') + tag );
282}
283
284void Dlg2Ui::error( const QString& message )
285{
286 if ( numErrors++ == 0 )
287 QMessageBox::warning( 0, yyFileName, message );
288}
289
290void Dlg2Ui::syntaxError()
291{
292 error( QString("Sorry, I met a random syntax error. I did what I could, but"
293 " that was not enough."
294 "<p>You might want to write to"
295 " <tt>qt-bugs@trolltech.com</tt> about this incident.") );
296}
297
298QString Dlg2Ui::getTextValue( const QDomNode& node )
299{
300 if ( node.childNodes().count() > 1 ) {
301 syntaxError();
302 return QString::null;
303 }
304
305 if ( node.childNodes().count() == 0 )
306 return QString::null;
307
308 QDomText child = node.firstChild().toText();
309 if ( child.isNull() ) {
310 syntaxError();
311 return QString::null;
312 }
313 QString t = child.data().stripWhiteSpace();
314 t.replace( "\\\\", "\\" );
315 t.replace( "\\n", "\n" );
316 return t;
317}
318
319QVariant Dlg2Ui::getValue( const QDomNodeList& children, const QString& tagName,
320 const QString& type )
321{
322 for ( int i = 0; i < (int) children.count(); i++ ) {
323 QDomNode n = children.item( i );
324 if ( n.toElement().tagName() == tagName )
325 return getValue( n.toElement(), tagName, type );
326 }
327 return QVariant();
328}
329
330void Dlg2Ui::emitHeader()
331{
332 yyOut += QString( "<!DOCTYPE UI><UI version=\"3.0\" stdsetdef=\"1\">\n" );
333}
334
335void Dlg2Ui::emitFooter()
336{
337 yyOut += QString( "</UI>\n" );
338}
339
340void Dlg2Ui::emitSimpleValue( const QString& tag, const QString& value,
341 const AttributeMap& attr )
342{
343 yyOut += yyIndentStr + opening( tag, attr ) + entitize( value ) +
344 closing( tag ) + QChar( '\n' );
345}
346
347void Dlg2Ui::emitOpening( const QString& tag, const AttributeMap& attr )
348{
349 yyOut += yyIndentStr + opening( tag, attr ) + QChar( '\n' );
350 yyIndentStr += QString( " " );
351}
352
353void Dlg2Ui::emitClosing( const QString& tag )
354{
355 yyIndentStr.truncate( yyIndentStr.length() - 4 );
356 yyOut += yyIndentStr + closing( tag ) + QChar( '\n' );
357}
358
359void Dlg2Ui::emitOpeningWidget( const QString& className )
360{
361 AttributeMap attr = attribute( QString("class"), className );
362 if ( yyGridColumn >= 0 ) {
363 attr.insert( QString("row"), QString::number(yyGridRow) );
364 attr.insert( QString("column"), QString::number(yyGridColumn) );
365 yyGridColumn = -1;
366 }
367 emitOpening( QString("widget"), attr );
368}
369
370QString Dlg2Ui::widgetClassName( const QDomElement& e )
371{
372 if ( e.tagName() == QString("User") ) {
373 return getValue( e.childNodes(), QString("UserClassName") )
374 .toString();
375 } else if ( e.tagName() == QString("DlgWidget") ) {
376 return QString( "QWidget" );
377 } else {
378 return QChar( 'Q' ) + e.tagName();
379 }
380}
381
382void Dlg2Ui::emitColor( const QColor& color )
383{
384 emitOpening( QString("color") );
385 emitSimpleValue( QString("red"), QString::number(color.red()) );
386 emitSimpleValue( QString("green"), QString::number(color.green()) );
387 emitSimpleValue( QString("blue"), QString::number(color.blue()) );
388 emitClosing( QString("color") );
389}
390
391void Dlg2Ui::emitColorGroup( const QString& name, const QColorGroup& group )
392{
393 emitOpening( name );
394 for ( int i = 0; i < NumColorRoles; i++ )
395 emitColor( group.color((QColorGroup::ColorRole) i) );
396 emitClosing( name );
397}
398
399void Dlg2Ui::emitVariant( const QVariant& val, const QString& stringType )
400{
401 if ( val.isValid() ) {
402 switch ( val.type() ) {
403 case QVariant::String:
404 emitSimpleValue( stringType, val.toString() );
405 break;
406 case QVariant::CString:
407 emitSimpleValue( QString("cstring"), val.toString() );
408 break;
409 case QVariant::Bool:
410 emitSimpleValue( QString("bool"),
411 QString(val.toBool() ? "true" : "false") );
412 break;
413 case QVariant::Int:
414 case QVariant::UInt:
415 emitSimpleValue( QString("number"), val.toString() );
416 break;
417 case QVariant::Rect:
418 emitOpening( QString("rect") );
419 emitSimpleValue( QString("x"), QString::number(val.toRect().x()) );
420 emitSimpleValue( QString("y"), QString::number(val.toRect().y()) );
421 emitSimpleValue( QString("width"),
422 QString::number(val.toRect().width()) );
423 emitSimpleValue( QString("height"),
424 QString::number(val.toRect().height()) );
425 emitClosing( QString("rect") );
426 break;
427 case QVariant::Point:
428 emitOpening( QString("point") );
429 emitSimpleValue( QString("x"), QString::number(val.toPoint().x()) );
430 emitSimpleValue( QString("y"), QString::number(val.toPoint().y()) );
431 emitClosing( QString("point") );
432 break;
433 case QVariant::Size:
434 emitOpening( QString("size") );
435 emitSimpleValue( QString("width"),
436 QString::number(val.toSize().width()) );
437 emitSimpleValue( QString("height"),
438 QString::number(val.toSize().height()) );
439 emitClosing( QString("size") );
440 break;
441 case QVariant::Color:
442 emitColor( val.toColor() );
443 break;
444 case QVariant::Font:
445 emitOpening( QString("font") );
446 emitSimpleValue( QString("family"), val.toFont().family() );
447 emitSimpleValue( QString("pointsize"),
448 QString::number(val.toFont().pointSize()) );
449 if ( val.toFont().bold() )
450 emitSimpleValue( QString("bold"), QChar('1') );
451 if ( val.toFont().italic() )
452 emitSimpleValue( QString("italic"), QChar('1') );
453 if ( val.toFont().underline() )
454 emitSimpleValue( QString("underline"), QChar('1') );
455 if ( val.toFont().strikeOut() )
456 emitSimpleValue( QString("strikeout"), QChar('1') );
457 emitClosing( QString("font") );
458 break;
459 case QVariant::Palette:
460 emitOpening( QString("palette") );
461 emitColorGroup( QString("active"), val.toPalette().active() );
462 emitColorGroup( QString("disabled"), val.toPalette().disabled() );
463 emitColorGroup( QString("inactive"), val.toPalette().inactive() );
464 emitClosing( QString("palette") );
465 break;
466 default:
467 emitSimpleValue( QString("fnord"), QString::null );
468 }
469 }
470}
471
472void Dlg2Ui::emitProperty( const QString& prop, const QVariant& val,
473 const QString& stringType )
474{
475 emitOpening( QString("property"), attribute(QString("name"), prop) );
476 emitVariant( val, stringType );
477 emitClosing( QString("property") );
478}
479
480void Dlg2Ui::emitAttribute( const QString& attr, const QVariant& val,
481 const QString& stringType )
482{
483 emitOpening( QString("attribute"), attribute(QString("name"), attr) );
484 emitVariant( val, stringType );
485 emitClosing( QString("attribute") );
486}
487
488void Dlg2Ui::emitOpeningLayout( bool needsWidget, const QString& layoutKind,
489 const QString& name, int border,
490 int autoBorder )
491{
492 QString namex = name;
493
494 if ( namex.isEmpty() )
495 namex = QString( "Layout%1" ).arg( uniqueLayout++ );
496
497 if ( needsWidget ) {
498 emitOpeningWidget( QString("QLayoutWidget") );
499 emitProperty( QString("name"), namex.latin1() );
500 }
501 emitOpening( layoutKind );
502 if ( !needsWidget )
503 emitProperty( QString("name"), namex.latin1() );
504 if ( border != 5 )
505 emitProperty( QString("margin"), border );
506 if ( autoBorder != 5 )
507 emitProperty( QString("spacing"), autoBorder );
508 yyLayoutDepth++;
509}
510
511void Dlg2Ui::flushWidgets()
512{
513 QRegExp widgetForLayout( QString("Q(?:[HV]Box|Grid)") );
514
515 while ( !yyWidgetMap.isEmpty() ) {
516 QString className = widgetClassName( *yyWidgetMap.begin() );
517 if ( !widgetForLayout.exactMatch(className) ) {
518 emitOpeningWidget( className );
519 emitWidgetBody( *yyWidgetMap.begin(), FALSE );
520 emitClosing( QString("widget") );
521 }
522 yyWidgetMap.remove( yyWidgetMap.begin() );
523 }
524}
525
526void Dlg2Ui::emitClosingLayout( bool needsWidget, const QString& layoutKind )
527{
528 yyLayoutDepth--;
529 /*
530 Qt Designer can deal with layouted widgets and with
531 fixed-position widgets, but not both at the same time. If such a
532 thing happens, we arbitrarily put the fixed-position widgets in
533 the layout so that they at least show up in Qt Designer.
534 */
535 if ( yyLayoutDepth == 0 )
536 flushWidgets();
537
538 emitClosing( layoutKind );
539 if ( needsWidget )
540 emitClosing( QString("widget") );
541}
542
543bool Dlg2Ui::isWidgetType( const QDomElement& e )
544{
545 return yyWidgetTypeSet.contains( e.tagName() );
546}
547
548void Dlg2Ui::emitSpacer( int spacing, int stretch )
549{
550 QString orientationStr;
551 QSize sizeHint;
552 QString sizeType = QString( "Fixed" );
553
554 if ( yyBoxKind == QString("hbox") ) {
555 orientationStr = QString( "Horizontal" );
556 sizeHint = QSize( spacing, 20 );
557 } else {
558 orientationStr = QString( "Vertical" );
559 sizeHint = QSize( 20, spacing );
560 }
561 if ( stretch > 0 )
562 sizeType = QString( "Expanding" );
563
564 emitOpening( QString("spacer") );
565 emitProperty( QString("name"),
566 QString("Spacer%1").arg(uniqueSpacer++).latin1() );
567 emitProperty( QString("orientation"), orientationStr, QString("enum") );
568 if ( spacing > 0 )
569 emitProperty( QString("sizeHint"), sizeHint, QString("qsize") );
570 emitProperty( QString("sizeType"), sizeType, QString("enum") );
571 emitClosing( QString("spacer") );
572}
573
574QString Dlg2Ui::filteredFlags( const QString& flags, const QRegExp& filter )
575{
576 QRegExp evil( QString("[^0-9A-Z_a-z|]") );
577
578 QString f = flags;
579 f.replace( evil, QString::null );
580 QStringList splitted = QStringList::split( QChar('|'), f );
581 return splitted.grep( filter ).join( QChar('|') );
582}
583
584void Dlg2Ui::emitFrameStyleProperty( int style )
585{
586 QString shape;
587 QString shadow;
588
589 switch ( style & QFrame::MShape ) {
590 case QFrame::Box:
591 shape = QString( "Box" );
592 break;
593 case QFrame::Panel:
594 shape = QString( "Panel" );
595 break;
596 case QFrame::WinPanel:
597 shape = QString( "WinPanel" );
598 break;
599 case QFrame::HLine:
600 shape = QString( "HLine" );
601 break;
602 case QFrame::VLine:
603 shape = QString( "VLine" );
604 break;
605 case QFrame::StyledPanel:
606 shape = QString( "StyledPanel" );
607 break;
608 case QFrame::PopupPanel:
609 shape = QString( "PopupPanel" );
610 break;
611 case QFrame::MenuBarPanel:
612 shape = QString( "MenuBarPanel" );
613 break;
614 case QFrame::ToolBarPanel:
615 shape = QString( "ToolBarPanel" );
616 break;
617 case QFrame::LineEditPanel:
618 shape = QString( "LineEditPanel" );
619 break;
620 case QFrame::TabWidgetPanel:
621 shape = QString( "TabWidgetPanel" );
622 break;
623 case QFrame::GroupBoxPanel:
624 shape = QString( "GroupBoxPanel" );
625 break;
626 default:
627 shape = QString( "NoFrame" );
628 }
629
630 switch ( style & QFrame::MShadow ) {
631 case QFrame::Raised:
632 shadow = QString( "Raised" );
633 break;
634 case QFrame::Sunken:
635 shadow = QString( "Sunken" );
636 break;
637 default:
638 shadow = QString( "Plain" );
639 }
640
641 emitProperty( QString("frameShape"), shape, QString("enum") );
642 emitProperty( QString("frameShadow"), shadow, QString("enum") );
643}
644
645void Dlg2Ui::emitWidgetBody( const QDomElement& e, bool layouted )
646{
647 QRegExp align( QString("^(?:Align|WordBreak$)") );
648 QRegExp frameShape( QString(
649 "^(?:NoFrame|Box|(?:Win|Styled|Popup|(?:Menu|Tool)Bar)?Panel|"
650 "[HV]Line)$") );
651 QRegExp frameShadow( QString( "^(?:Plain|Raised|Sunken)$") );
652 QRegExp numeric( QString("[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+") );
653 QRegExp connex( QString(
654 "\\s*\\[(BaseClass|P(?:ublic|rotected))\\]\\s*([0-9A-Z_a-z]+)\\s*"
655 "-->\\s*([0-9A-Z_a-z]+)\\s*(\\([^()]*\\))\\s*") );
656 QRegExp qdialogSlots( QString(
657 "done\\(\\s*int\\s*\\)|(?:accept|reject)\\(\\s*\\)") );
658
659 QString userClassHeader;
660 QString userClassName;
661 QString parentTagName;
662 QString name;
663 QString variableName;
664 QMap<QString, int> pp;
665
666 QDomNode n = e;
667 while ( !n.isNull() ) {
668 if ( isWidgetType(n.toElement()) ) {
669 parentTagName = n.toElement().tagName();
670 pp = yyPropertyMap[parentTagName];
671 n = n.firstChild();
672 } else {
673 QString tagName = n.toElement().tagName();
674
675 QMap<QString, int>::ConstIterator p = pp.find( tagName );
676 if ( p == pp.end() ) {
677 /*
678 These properties are not in the propertyDefs table,
679 since they are found in many classes anyway and need
680 to be treated the same in each case.
681 */
682 if ( tagName == QString("Alignement") ||
683 tagName == QString("Alignment") ) {
684 QString flags = getValue( n.toElement(), tagName )
685 .toString();
686 flags = filteredFlags( flags, align );
687 if ( !flags.isEmpty() )
688 emitProperty( QString("alignment"), flags,
689 QString("set") );
690 } else if ( tagName == QString("ItemList") ) {
691 QDomNode child = n.firstChild();
692 while ( !child.isNull() ) {
693 if ( child.toElement().tagName() == QString("Item") ) {
694 QString text = getTextValue( child );
695 emitOpening( QString("item") );
696 emitProperty( QString("text"), text );
697 emitClosing( QString("item") );
698 }
699 child = child.nextSibling();
700 }
701 }
702 } else {
703 QString propertyName( propertyDefs[*p].qtName );
704
705 if ( propertyName.isEmpty() ) {
706 /*
707 These properties are in the propertyDefs table,
708 but they have no direct Qt equivalent.
709 */
710 if ( parentTagName == QString("ComboBox") ) {
711 if ( tagName == QString("Style") ) {
712 if ( getTextValue(n) == QString("ReadWrite") )
713 emitProperty( QString("editable"),
714 QVariant(TRUE, 0) );
715 }
716 } else if ( parentTagName == QString("DlgWidget") ) {
717 if ( tagName == QString("Name") ) {
718 name = getTextValue( n );
719 } else if ( tagName == QString("Rect") ) {
720 QRect rect = getValue( n.toElement(), tagName,
721 QString("qrect") )
722 .toRect();
723 if ( !layouted )
724 emitProperty( QString("geometry"), rect,
725 QString("qrect") );
726 } else if ( tagName == QString("SignalConnection") ) {
727 QDomNode child = n.firstChild();
728 while ( !child.isNull() ) {
729 if ( child.toElement().tagName() ==
730 QString("Signal") ) {
731 QString text = getTextValue( child );
732 if ( connex.exactMatch(text) ) {
733 DlgConnection c;
734 c.sender = getValue(
735 n.parentNode().childNodes(),
736 QString("Name") ).toString();
737 c.signal = connex.cap( 2 ) +
738 connex.cap( 4 );
739 c.slot = connex.cap( 3 ) +
740 connex.cap( 4 );
741 yyConnections.append( c );
742
743 if ( connex.cap(1) !=
744 QString("BaseClass") &&
745 !qdialogSlots.exactMatch(c.slot) )
746 yySlots.insert( c.slot,
747 connex.cap(1) );
748 }
749 }
750 child = child.nextSibling();
751 }
752 } else if ( tagName == QString("Variable") ) {
753 variableName = getTextValue( n );
754 }
755 } else if ( parentTagName == QString("Frame") ) {
756 if ( tagName == QString("Style") ) {
757 int style = getValue( n.toElement(), tagName,
758 QString("integer") ).toInt();
759 emitFrameStyleProperty( style );
760 }
761 } else if ( parentTagName == QString("LCDNumber") ) {
762 if ( tagName == QString("Value") ) {
763 QString text = getValue( n.toElement(), tagName )
764 .toString();
765 if ( numeric.exactMatch(text) )
766 emitProperty( QString("value"),
767 text.toDouble() );
768 }
769 } else if ( parentTagName == QString("ListView") ) {
770 if ( tagName == QString("HeaderInformation") ) {
771 int columnNo = 1;
772 QDomNode child = n.firstChild();
773 while ( !child.isNull() ) {
774 if ( child.toElement().tagName() ==
775 QString("Header") ) {
776 QString text = getValue( child.childNodes(),
777 QString("Text") )
778 .toString();
779 if ( text.isEmpty() )
780 text = QString( "Column %1" )
781 .arg( columnNo );
782 emitOpening( QString("column") );
783 emitProperty( QString("text"), text );
784 emitClosing( QString("column") );
785 }
786 child = child.nextSibling();
787 columnNo++;
788 }
789 }
790 } else if ( parentTagName == QString("TabBar") ) {
791 if ( tagName == QString("TabNames") ) {
792 QDomNode child = n.firstChild();
793 while ( !child.isNull() ) {
794 if ( child.toElement().tagName() ==
795 QString("Tab") ) {
796 QString text = getTextValue( child );
797 emitOpeningWidget( QString("QWidget") );
798 emitProperty( QString("name"), "tab" );
799 emitAttribute( QString("title"), text );
800 emitClosing( QString("widget") );
801 }
802 child = child.nextSibling();
803 }
804 }
805 } else if ( parentTagName == QString("User") ) {
806 if ( tagName == QString("UserClassHeader") ) {
807 userClassHeader = getTextValue( n );
808 } else if ( tagName == QString("UserClassName") ) {
809 userClassName = getTextValue( n );
810 }
811 }
812 } else {
813 /*
814 These properties are in the propertyDefs table;
815 they have a direct Qt equivalent.
816 */
817 QString type( propertyDefs[*p].type );
818 QVariant val = getValue( n.toElement(), tagName, type );
819
820 if ( type == QString("qstring") )
821 type = QString( "string" );
822
823 bool omit = FALSE;
824 if ( propertyName == QString("backgroundOrigin") &&
825 val.toString() == QString("WidgetOrigin") )
826 omit = TRUE;
827 if ( propertyName == QString("enabled") && val.toBool() )
828 omit = TRUE;
829 if ( propertyName == QString("minimumSize") &&
830 val.toSize() == QSize(-1, -1) )
831 omit = TRUE;
832 if ( propertyName == QString("maximumSize") &&
833 val.toSize() == QSize(32767, 32767) )
834 omit = TRUE;
835
836 if ( !omit )
837 emitProperty( propertyName, val, type );
838 }
839 }
840 n = n.nextSibling();
841 }
842 }
843
844 if ( !variableName.isEmpty() ) {
845 yyAliasMap.insert( name, variableName );
846 name = variableName;
847 }
848 if ( !name.isEmpty() )
849 emitProperty( QString("name"), name.latin1() );
850
851 if ( !userClassName.isEmpty() )
852 yyCustomWidgets.insert( userClassName, userClassHeader );
853}
854
855bool Dlg2Ui::checkTagName( const QDomElement& e, const QString& tagName )
856{
857 bool ok = ( e.tagName() == tagName );
858 if ( !ok )
859 syntaxError();
860 return ok;
861}
862
863QString Dlg2Ui::normalizeType( const QString& type )
864{
865 QString t = type;
866 if ( t.isEmpty() || t == QString("enum") || t == QString( "qcstring" ) ||
867 t == QString("set") )
868 t = QString( "qstring" );
869 return t;
870}
871
872QVariant Dlg2Ui::getValue( const QDomElement& e, const QString& tagName,
873 const QString& type )
874{
875 QVariant val;
876
877 if ( e.tagName() != tagName )
878 return val;
879
880 QString t = e.attributes().namedItem( "type" ).toAttr().value();
881 if ( normalizeType(t) != normalizeType(type) )
882 return val;
883
884 if ( type == QString("integer") ) {
885 return getTextValue( e ).toInt();
886 } else if ( type == QString("boolean") ) {
887 return QVariant( isTrue(getTextValue(e)), 0 );
888 } else if ( type == QString("double") ) {
889 return getTextValue( e ).toDouble();
890 } else if ( type == QString("qcstring") ) {
891 return getTextValue( e ).latin1();
892 } else if ( type == QString("enum") || type == QString("qstring") ||
893 type == QString("set") ) {
894 return getTextValue( e );
895 } else {
896 QDomNodeList children = e.childNodes();
897
898 if ( type == QString("qsize") ) {
899 int width = getValue( children, QString("Width"),
900 QString("integer") ).toInt();
901 int height = getValue( children, QString("Height"),
902 QString("integer") ).toInt();
903 return QSize( width, height );
904 } else if ( type == QString("qrect") ) {
905 int x = getValue( children, QString("X"), QString("integer") )
906 .toInt();
907 int y = getValue( children, QString("Y"), QString("integer") )
908 .toInt();
909 int width = getValue( children, QString("Width"),
910 QString("integer") ).toInt();
911 int height = getValue( children, QString("Height"),
912 QString("integer") ).toInt();
913 return QRect( x, y, width, height );
914 } else if ( type == QString("qpoint") ) {
915 int x = getValue( children, QString("X"), QString("integer") )
916 .toInt();
917 int y = getValue( children, QString("Y"), QString("integer") )
918 .toInt();
919 return QPoint( x, y );
920 } else if ( type == QString("qpalette") ) {
921 QColorGroup active = getValue( children, QString("Active"),
922 QString("qcolorgroup") )
923 .toColorGroup();
924 QColorGroup disabled = getValue( children, QString("Disabled"),
925 QString("qcolorgroup") )
926 .toColorGroup();
927 QColorGroup inactive = getValue( children, QString("Inactive"),
928 QString("qcolorgroup") )
929 .toColorGroup();
930 return QPalette( active, disabled, inactive );
931 } else if ( type == QString("qfont") ) {
932 QString family = getValue( children, QString("Family"),
933 QString("qstring") ).toString();
934 int pointSize = getValue( children, QString("PointSize"),
935 QString("integer") ).toInt();
936 int weight = getValue( children, QString("weight"),
937 QString("integer") ).toInt();
938 bool italic = getValue( children, QString("Italic"),
939 QString("boolean") ).toBool();
940 bool underline = getValue( children, QString("Underline"),
941 QString("boolean") ).toBool();
942 bool strikeOut = getValue( children, QString("StrikeOut"),
943 QString("boolean") ).toBool();
944 int styleHint = getValue( children, QString("StyleHint"),
945 QString("integer") ).toInt();
946
947 QFont f;
948 if ( !family.isEmpty() )
949 f.setFamily( family );
950 if ( pointSize != 0 )
951 f.setPointSize( pointSize );
952 if ( weight != 0 )
953 f.setWeight( weight );
954 f.setItalic( italic );
955 f.setUnderline( underline );
956 f.setStrikeOut( strikeOut );
957 if ( styleHint != 0 )
958 f.setStyleHint( (QFont::StyleHint) styleHint );
959 return f;
960 } else if ( type == QString("qcolor") ) {
961 // if any component missing, zero is to be assumed
962 int red = getValue( children, QString("Red"), QString("integer") )
963 .toInt();
964 int green = getValue( children, QString("Green"),
965 QString("integer") ).toInt();
966 int blue = getValue( children, QString("Blue"), QString("integer") )
967 .toInt();
968 return QColor( red, green, blue );
969 } else if ( type == QString("qcolorgroup") ) {
970 static const QColorGroup::ColorRole roles[NumColorRoles] = {
971 QColorGroup::Foreground, QColorGroup::Button,
972 QColorGroup::Light, QColorGroup::Midlight, QColorGroup::Dark,
973 QColorGroup::Mid, QColorGroup::Text, QColorGroup::BrightText,
974 QColorGroup::ButtonText, QColorGroup::Base,
975 QColorGroup::Background, QColorGroup::Shadow,
976 QColorGroup::Highlight, QColorGroup::HighlightedText
977 };
978 static const char * const roleNames[NumColorRoles] = {
979 "Foreground", "Button", "Light", "MidLight", "Dark", "Mid",
980 "Text", "BrightText", "ButtonText", "Base", "Background",
981 "Shadow", "HighLighted", "HighLightedText"
982 };
983 QColorGroup group;
984
985 for ( int i = 0; i < NumColorRoles; i++ )
986 group.setColor( roles[i],
987 getValue(children, QString(roleNames[i]),
988 QString("qcolor")).toColor() );
989 return group;
990 } else {
991 syntaxError();
992 }
993 }
994 return val;
995}
996
997void Dlg2Ui::matchDialogCommon( const QDomElement& dialogCommon )
998{
999 if ( !checkTagName(dialogCommon, QString("DialogCommon")) )
1000 return;
1001
1002 QString sourceDir;
1003 QString classHeader;
1004 QString classSource;
1005 QString dataHeader;
1006 QString dataSource;
1007 QString dataName;
1008 QString windowBaseClass( "QDialog" );
1009 bool isCustom = FALSE;
1010 QString customBaseHeader;
1011 QString windowCaption;
1012
1013 yyClassName = "Form1";
1014
1015 QDomNode n = dialogCommon.firstChild();
1016 while ( !n.isNull() ) {
1017 QString tagName = n.toElement().tagName();
1018 QString val = getTextValue( n );
1019
1020 if ( tagName == QString("SourceDir") ) {
1021 sourceDir = val;
1022 } else if ( tagName == QString("ClassHeader") ) {
1023 classHeader = val;
1024 } else if ( tagName == QString("ClassSource") ) {
1025 classSource = val;
1026 } else if ( tagName == QString("ClassName") ) {
1027 yyClassName = val;
1028 } else if ( tagName == QString("DataHeader") ) {
1029 dataHeader = val;
1030 } else if ( tagName == QString("DataSource") ) {
1031 dataSource = val;
1032 } else if ( tagName == QString("DataName") ) {
1033 dataName = val;
1034 } else if ( tagName == QString("WindowBaseClass") ) {
1035 if ( val == QString("Custom") )
1036 isCustom = TRUE;
1037 else
1038 windowBaseClass = val;
1039 } else if ( tagName == QString("IsModal") ) {
1040 } else if ( tagName == QString("CustomBase") ) {
1041 windowBaseClass = val;
1042 } else if ( tagName == QString("CustomBaseHeader") ) {
1043 customBaseHeader = val;
1044 } else if ( tagName == QString("WindowCaption") ) {
1045 windowCaption = val;
1046 }
1047 n = n.nextSibling();
1048 }
1049
1050 emitSimpleValue( QString("class"), yyClassName );
1051 emitOpeningWidget( windowBaseClass );
1052
1053 if ( windowCaption.isEmpty() )
1054 windowCaption = yyClassName;
1055 emitProperty( QString("name"), yyClassName.latin1() );
1056 emitProperty( QString("caption"), windowCaption );
1057
1058 if ( isCustom )
1059 yyCustomWidgets.insert( windowBaseClass, customBaseHeader );
1060}
1061
1062bool Dlg2Ui::needsQLayoutWidget( const QDomElement& e )
1063{
1064 QRegExp widgetExists( QString("WidgetLayout|Layout_Widget") );
1065
1066 // we should also check that the widget is not a QHBox, QVBox, or QGrid
1067 QString grandpa = e.parentNode().parentNode().toElement().tagName();
1068 return !widgetExists.exactMatch( grandpa );
1069}
1070
1071void Dlg2Ui::matchBoxLayout( const QDomElement& boxLayout )
1072{
1073 QString directionStr;
1074 QString prevBoxKind = yyBoxKind;
1075 int border = 5;
1076 int autoBorder = 5;
1077 QString name;
1078 bool needsWidget = needsQLayoutWidget( boxLayout );
1079 bool opened = FALSE;
1080
1081 QDomNode n = boxLayout.firstChild();
1082 while ( !n.isNull() ) {
1083 QString tagName = n.toElement().tagName();
1084
1085 if ( tagName == QString("Children") ) {
1086 if ( !opened ) {
1087 emitOpeningLayout( needsWidget, yyBoxKind, name, border,
1088 autoBorder );
1089 if ( !directionStr.isEmpty() )
1090 emitProperty( QString("direction"), directionStr,
1091 QString("enum") );
1092 opened = TRUE;
1093 }
1094 matchLayout( n.toElement() );
1095 } else {
1096 QString val = getTextValue( n );
1097
1098 if ( tagName == QString("Direction") ) {
1099 if ( val == QString("LeftToRight") ) {
1100 yyBoxKind = QString( "hbox" );
1101 } else if ( val == QString("RightToLeft") ) {
1102 directionStr = val;
1103 yyBoxKind = QString( "hbox" );
1104 } else if ( val == QString("TopToBottom") ) {
1105 yyBoxKind = QString( "vbox" );
1106 } else if ( val == QString("BottomToTop") ) {
1107 directionStr = val;
1108 yyBoxKind = QString( "vbox" );
1109 } else {
1110 syntaxError();
1111 }
1112 } else if ( tagName == QString("Border") ) {
1113 border = val.toInt();
1114 } else if ( tagName == QString("AutoBorder") ) {
1115 autoBorder = val.toInt();
1116 } else if ( tagName == QString("Name") ) {
1117 name = val;
1118 }
1119 }
1120
1121 n = n.nextSibling();
1122 }
1123 if ( opened ) {
1124 emitClosingLayout( needsWidget, yyBoxKind );
1125 yyBoxKind = prevBoxKind;
1126 }
1127}
1128
1129void Dlg2Ui::matchBoxSpacing( const QDomElement& boxSpacing )
1130{
1131 int spacing = 7;
1132
1133 QDomNode n = boxSpacing.firstChild();
1134 while ( !n.isNull() ) {
1135 QString val = getTextValue( n );
1136
1137 if ( n.toElement().tagName() == QString("Spacing") )
1138 spacing = val.toInt();
1139 n = n.nextSibling();
1140 }
1141 emitSpacer( spacing, 0 );
1142}
1143
1144void Dlg2Ui::matchBoxStretch( const QDomElement& boxStretch )
1145{
1146 int stretch = 1;
1147
1148 QDomNode n = boxStretch.firstChild();
1149 while ( !n.isNull() ) {
1150 QString val = getTextValue( n );
1151
1152 if ( n.toElement().tagName() == QString("Stretch") )
1153 stretch = val.toInt();
1154 n = n.nextSibling();
1155 }
1156 emitSpacer( 0, stretch );
1157}
1158
1159void Dlg2Ui::matchGridLayout( const QDomElement& gridLayout )
1160{
1161 int oldGridRow = yyGridRow;
1162 int oldGridColumn = yyGridColumn;
1163 int border = 5;
1164 int autoBorder = 5;
1165 QString name;
1166 QString menu;
1167 bool needsWidget = needsQLayoutWidget( gridLayout );
1168 bool opened = FALSE;
1169
1170 QDomNode n = gridLayout.firstChild();
1171 while ( !n.isNull() ) {
1172 QString tagName = n.toElement().tagName();
1173
1174 if ( tagName == QString("Children") ) {
1175 if ( !opened ) {
1176 emitOpeningLayout( needsWidget, QString("grid"), name, border,
1177 autoBorder );
1178 yyGridRow = -1;
1179 yyGridColumn = -1;
1180 opened = TRUE;
1181 }
1182 matchLayout( n.toElement() );
1183 } else {
1184 if ( tagName == QString("Border") ) {
1185 border = getTextValue( n ).toInt();
1186 } else if ( tagName == QString("AutoBorder") ) {
1187 autoBorder = getTextValue( n ).toInt();
1188 } else if ( tagName == QString("Name") ) {
1189 name = getTextValue( n );
1190 } else if ( tagName == QString("Menu") ) {
1191 menu = getTextValue( n );
1192 }
1193 }
1194 n = n.nextSibling();
1195 }
1196 if ( opened )
1197 emitClosingLayout( needsWidget, QString("grid") );
1198 yyGridRow = oldGridRow;
1199 yyGridColumn = oldGridColumn;
1200}
1201
1202void Dlg2Ui::matchGridRow( const QDomElement& gridRow )
1203{
1204 yyGridRow++;
1205
1206 QDomNode n = gridRow.firstChild();
1207 while ( !n.isNull() ) {
1208 QString tagName = n.toElement().tagName();
1209
1210 if ( tagName == QString("Children") ) {
1211 yyGridColumn = 0;
1212 matchLayout( n.toElement() );
1213 }
1214 n = n.nextSibling();
1215 }
1216}
1217
1218void Dlg2Ui::matchGridSpacer( const QDomElement& gridSpacer )
1219{
1220 if ( !gridSpacer.firstChild().isNull() )
1221 syntaxError();
1222}
1223
1224void Dlg2Ui::matchLayoutWidget( const QDomElement& layoutWidget )
1225{
1226 QDomElement children;
1227 QString widget;
1228
1229 QDomNode n = layoutWidget.firstChild();
1230 while ( !n.isNull() ) {
1231 QString tagName = n.toElement().tagName();
1232
1233 if ( tagName == QString("Children") )
1234 children = n.toElement();
1235 else if ( tagName == QString("Widget") )
1236 widget = getTextValue( n );
1237 n = n.nextSibling();
1238 }
1239
1240 if ( !widget.isEmpty() ) {
1241 QMap<QString, QDomElement>::Iterator w = yyWidgetMap.find( widget );
1242 if ( w == yyWidgetMap.end() ) {
1243 syntaxError();
1244 } else {
1245 QString className = widgetClassName( *w );
1246 if ( className == QString("QHBox") ||
1247 className == QString("QVBox") ) {
1248 bool needsWidget = needsQLayoutWidget( layoutWidget );
1249
1250 QString prevBoxKind = yyBoxKind;
1251 yyBoxKind = className.mid( 1 ).lower();
1252
1253 int spacing = getValue( (*w).childNodes(), QString("Spacing"),
1254 QString("integer") ).toInt();
1255 if ( spacing < 1 )
1256 spacing = 5;
1257
1258 emitOpeningLayout( needsWidget, yyBoxKind, widget, 0, spacing );
1259 if ( !children.isNull() )
1260 matchLayout( children );
1261 emitClosingLayout( needsWidget, yyBoxKind );
1262 yyBoxKind = prevBoxKind;
1263 } else if ( className == QString("QGrid") ) {
1264 bool needsWidget = needsQLayoutWidget( layoutWidget );
1265 int n = 0;
1266
1267 QString direction = getValue( (*w).childNodes(),
1268 QString("Direction") ).toString();
1269 int rowsCols = getValue( (*w).childNodes(), QString("RowCols"),
1270 QString("integer") ).toInt();
1271 if ( rowsCols == 0 )
1272 rowsCols = getValue( (*w).childNodes(),
1273 QString("RowsCols"),
1274 QString("integer") ).toInt();
1275 if ( rowsCols < 1 )
1276 rowsCols = 5;
1277 int spacing = getValue( (*w).childNodes(), QString("Spacing"),
1278 QString("integer") ).toInt();
1279 if ( spacing < 1 )
1280 spacing = 5;
1281
1282 emitOpeningLayout( needsWidget, QString("grid"), widget, 0,
1283 spacing );
1284
1285 QDomNode child = children.firstChild();
1286 while ( !child.isNull() ) {
1287 if ( direction == QString("Vertical") ) {
1288 yyGridColumn = n / rowsCols;
1289 yyGridRow = n % rowsCols;
1290 } else {
1291 yyGridColumn = n % rowsCols;
1292 yyGridRow = n / rowsCols;
1293 }
1294 matchBox( child.toElement() );
1295 n++;
1296 child = child.nextSibling();
1297 }
1298 yyGridColumn = -1;
1299 yyGridRow = -1;
1300 emitClosingLayout( needsWidget, QString("grid") );
1301 } else {
1302 emitOpeningWidget( widgetClassName(*w) );
1303 emitWidgetBody( *w, TRUE );
1304 if ( !children.isNull() )
1305 matchLayout( children );
1306 emitClosing( QString("widget") );
1307 }
1308 yyWidgetMap.remove( w );
1309 }
1310 }
1311}
1312
1313void Dlg2Ui::matchBox( const QDomElement& box )
1314{
1315 /*
1316 What is this jump table doing in here?
1317 */
1318 static const struct {
1319 const char *tagName;
1320 void (Dlg2Ui::*matchFunc)( const QDomElement& );
1321 } jumpTable[] = {
1322 { "Box_Layout", &Dlg2Ui::matchBoxLayout },
1323 { "Box_Spacing", &Dlg2Ui::matchBoxSpacing },
1324 { "Box_Stretch", &Dlg2Ui::matchBoxStretch },
1325 { "Grid_Layout", &Dlg2Ui::matchGridLayout },
1326 { "Grid_Row", &Dlg2Ui::matchGridRow },
1327 { "Grid_Spacer", &Dlg2Ui::matchGridSpacer },
1328 { "Layout_Widget", &Dlg2Ui::matchLayoutWidget },
1329 { 0, 0 }
1330 };
1331
1332 int i = 0;
1333 while ( jumpTable[i].tagName != 0 ) {
1334 if ( QString(jumpTable[i].tagName) == box.tagName() ) {
1335 (this->*jumpTable[i].matchFunc)( box );
1336 break;
1337 }
1338 i++;
1339 }
1340 if ( jumpTable[i].tagName == 0 )
1341 syntaxError();
1342}
1343
1344void Dlg2Ui::matchLayout( const QDomElement& layout )
1345{
1346 int column = yyGridColumn;
1347
1348 QDomNode n = layout.firstChild();
1349 while ( !n.isNull() ) {
1350 if ( column != -1 )
1351 yyGridColumn = column++;
1352 matchBox( n.toElement() );
1353 n = n.nextSibling();
1354 }
1355}
1356
1357void Dlg2Ui::matchWidgetLayoutCommon( const QDomElement& widgetLayoutCommon )
1358{
1359 QDomNodeList children = widgetLayoutCommon.childNodes();
1360
1361 /*
1362 Since we do not respect the spacing and margins specified in
1363 the .dlg file, the specified geometry is slightly wrong (too
1364 small). It still seems to be better to take it in.
1365 */
1366#if 1
1367 QPoint initialPos = getValue( children, QString("InitialPos"),
1368 QString("qpoint") ).toPoint();
1369 QSize size = getValue( children, QString("Size"), QString("qsize") )
1370 .toSize();
1371#endif
1372 QSize minSize = getValue( children, QString("MinSize"), QString("qsize") )
1373 .toSize();
1374 QSize maxSize = getValue( children, QString("MaxSize"), QString("qsize") )
1375 .toSize();
1376
1377#if 1
1378 if ( initialPos == QPoint(-1, -1) )
1379 initialPos = QPoint( 0, 0 );
1380
1381 emitProperty( QString("geometry"), QRect(initialPos, size) );
1382#endif
1383 if ( minSize != QSize(-1, -1) )
1384 emitProperty( QString("minimumSize"), minSize );
1385 if ( maxSize != QSize(32767, 32767) )
1386 emitProperty( QString("maximumSize"), maxSize );
1387}
1388
1389void Dlg2Ui::matchWidget( const QDomElement& widget )
1390{
1391 QString name;
1392
1393 QDomNode n = widget;
1394 while ( !n.isNull() ) {
1395 if ( isWidgetType(n.toElement()) ) {
1396 n = n.firstChild();
1397 } else {
1398 if ( n.toElement().tagName() == QString("Name") ) {
1399 name = getTextValue( n );
1400 break;
1401 }
1402 n = n.nextSibling();
1403 }
1404 }
1405 if ( name.isEmpty() )
1406 name = QString( "Widget%1" ).arg( uniqueWidget++ );
1407
1408 if ( yyWidgetMap.contains(name) )
1409 syntaxError();
1410 yyWidgetMap.insert( name, widget );
1411}
1412
1413void Dlg2Ui::matchWidgets( const QDomElement& widgets )
1414{
1415 QDomNode n = widgets.firstChild();
1416 while ( !n.isNull() ) {
1417 matchWidget( n.toElement() );
1418 n = n.nextSibling();
1419 }
1420}
1421
1422void Dlg2Ui::matchTabOrder( const QDomElement& tabOrder )
1423{
1424 QDomNode n = tabOrder.firstChild();
1425 while ( !n.isNull() ) {
1426 if ( n.toElement().tagName() == QString("Widget") )
1427 yyTabStops.append( getTextValue(n.toElement()) );
1428 n = n.nextSibling();
1429 }
1430}
1431
1432void Dlg2Ui::matchWidgetLayout( const QDomElement& widgetLayout )
1433{
1434 if ( !checkTagName(widgetLayout, QString("WidgetLayout")) )
1435 return;
1436
1437 QDomNode n = widgetLayout.firstChild();
1438 while ( !n.isNull() ) {
1439 QString tagName = n.toElement().tagName();
1440
1441 if ( tagName == QString("WidgetLayoutCommon") ) {
1442 matchWidgetLayoutCommon( n.toElement() );
1443 } else if ( tagName == QString("Widgets") ) {
1444 matchWidgets( n.toElement() );
1445 } else if ( tagName == QString("TabOrder") ) {
1446 matchTabOrder( n.toElement() );
1447 } else if ( tagName == QString("Layout") ) {
1448 matchLayout( n.toElement() );
1449 }
1450 n = n.nextSibling();
1451 }
1452}
1453
1454void Dlg2Ui::matchDialog( const QDomElement& dialog )
1455{
1456 if ( !checkTagName(dialog, QString("Dialog")) )
1457 return;
1458
1459 QDomNodeList nodes = dialog.childNodes();
1460 if ( nodes.count() == 2 ) {
1461 matchDialogCommon( nodes.item(0).toElement() );
1462 matchWidgetLayout( nodes.item(1).toElement() );
1463 flushWidgets();
1464 emitClosing( QString("widget") );
1465
1466 if ( !yyCustomWidgets.isEmpty() ) {
1467 emitOpening( QString("customwidgets") );
1468
1469 QMap<QString, QString>::Iterator w = yyCustomWidgets.begin();
1470 while ( w != yyCustomWidgets.end() ) {
1471 emitOpening( QString("customwidget") );
1472 emitSimpleValue( QString("class"), w.key() );
1473 if ( !(*w).isEmpty() )
1474 emitSimpleValue( QString("header"), *w,
1475 attribute(QString("location"),
1476 QString("local")) );
1477 emitClosing( QString("customwidget") );
1478 ++w;
1479 }
1480 emitClosing( QString("customwidgets") );
1481 }
1482
1483 if ( yyConnections.count() + yySlots.count() > 0 ) {
1484 emitOpening( QString("connections") );
1485
1486 QValueList<DlgConnection>::Iterator c = yyConnections.begin();
1487 while ( c != yyConnections.end() ) {
1488 emitOpening( QString("connection") );
1489 emitSimpleValue( QString("sender"), alias((*c).sender) );
1490 emitSimpleValue( QString("signal"), (*c).signal );
1491 emitSimpleValue( QString("receiver"), yyClassName );
1492 emitSimpleValue( QString("slot"), (*c).slot );
1493 emitClosing( QString("connection") );
1494 ++c;
1495 }
1496
1497 QMap<QString, QString>::Iterator s = yySlots.begin();
1498 while ( s != yySlots.end() ) {
1499 AttributeMap attr;
1500 attr.insert( QString("access"), *s );
1501 attr.insert( QString("language"), QString("C++") );
1502 attr.insert( QString("returntype"), QString("void") );
1503 emitSimpleValue( QString("slot"), s.key(), attr );
1504 ++s;
1505 }
1506 emitClosing( QString("connections") );
1507 }
1508
1509 if ( !yyTabStops.isEmpty() ) {
1510 emitOpening( QString("tabstops") );
1511 QStringList::ConstIterator t = yyTabStops.begin();
1512 while ( t != yyTabStops.end() ) {
1513 emitSimpleValue( QString("tabstop"), alias(*t) );
1514 ++t;
1515 }
1516 emitClosing( QString("tabstops") );
1517 }
1518 }
1519}
1520
1521QStringList Dlg2Ui::convertQtArchitectDlgFile( const QString& fileName )
1522{
1523 int i;
1524
1525 yyFileName = fileName;
1526 yyLayoutDepth = 0;
1527 yyGridRow = -1;
1528 yyGridColumn = -1;
1529
1530 numErrors = 0;
1531 uniqueLayout = 1;
1532 uniqueSpacer = 1;
1533 uniqueWidget = 1;
1534
1535 i = 0;
1536 while ( widgetTypes[i] != 0 ) {
1537 yyWidgetTypeSet.insert( QString(widgetTypes[i]), 0 );
1538 i++;
1539 }
1540
1541 i = 0;
1542 while ( propertyDefs[i].widgetName != 0 ) {
1543 yyPropertyMap[QString(propertyDefs[i].widgetName)]
1544 .insert( QString(propertyDefs[i].architectName), i );
1545 i++;
1546 }
1547
1548 QDomDocument doc( QString("QtArch") );
1549 QFile f( fileName );
1550 if ( !f.open(IO_ReadOnly) ) {
1551 return QStringList();
1552 }
1553 if ( !doc.setContent(&f) ) {
1554 QString firstLine;
1555 f.at( 0 );
1556 f.readLine( firstLine, 128 );
1557 firstLine = firstLine.stripWhiteSpace();
1558 if ( firstLine.startsWith(QString("DlgEdit:v1")) ) {
1559 error( QString("This file is a Qt Architect 1.x file. Qt Designer"
1560 " can only read XML dialog files, as generated by Qt"
1561 " Architect 2.1 or above."
1562 "<p>To convert this file to the right format,"
1563 " first install Qt Architect 2.1 (available at"
1564 " <tt>http://qtarch.sourceforge.net/</tt>). Use the"
1565 " <i>update20.pl</i> Perl script to update the file"
1566 " to the 2.0 format. Load that file in Qt"
1567 " Architect and save it. The file should now be in"
1568 " XML format and loadable in Qt Designer.") );
1569 } else if ( firstLine.startsWith(QString("DlgEdit::v2")) ) {
1570 error( QString("This file is a Qt Architect 2.0 file. Qt Designer"
1571 " can only read XML dialog files, as generated by Qt"
1572 " Architect 2.1 or above."
1573 "<p>To convert this file to the right format,"
1574 " first install Qt Architect 2.1 (available at"
1575 " <tt>http://qtarch.sourceforge.net/</tt>). Load the"
1576 " 2.0 file in Qt Architect and save it. The file"
1577 " should now be in XML format and loadable in Qt"
1578 " Designer.") );
1579 } else {
1580 error( QString("The file you gave me is not an XML file, as far as"
1581 " I can tell.") );
1582 }
1583
1584 f.close();
1585 return QStringList();
1586 }
1587 f.close();
1588
1589 QDomElement root = doc.documentElement();
1590 if ( root.tagName() != QString("QtArch") ||
1591 root.attributeNode("type").value() != QString("Dialog") ) {
1592 error( QString("The file you gave me is not a Qt Architect dialog"
1593 " file.") );
1594 return QStringList();
1595 }
1596
1597 emitHeader();
1598
1599 QDomNode n = root.firstChild();
1600 while ( !n.isNull() ) {
1601 // there should be only one
1602 matchDialog( n.toElement() );
1603 n = n.nextSibling();
1604 }
1605
1606 emitFooter();
1607
1608 QFile outf;
1609 QString outFileName = yyClassName + QString( ".ui" );
1610
1611 outf.setName( outFileName );
1612 if ( !outf.open(IO_WriteOnly) ) {
1613 qWarning( "dlg2ui: Could not open output file '%s'",
1614 outFileName.latin1() );
1615 return QStringList();
1616 }
1617
1618 QTextStream out;
1619 out.setEncoding( QTextStream::Latin1 );
1620 out.setDevice( &outf );
1621 out << yyOut;
1622 outf.close();
1623
1624 return QStringList( outFileName );
1625}
Note: See TracBrowser for help on using the repository browser.