1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
|
/**********************************************************************
** Copyright (C) 2000 Trolltech AS. All rights reserved.
** Copyright (c) 2001 Phil Thompson <[email protected]>
** Copyright (c) 2002 Germain Garand <[email protected]>
**
** This file is part of TQt Designer.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact [email protected] if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
/*
** 06/2002 : Initial release of puic, the PerlTQt User Interface Compiler,
** a work derivated from uic (the TQt User Interface Compiler)
** and pyuic (the PyTQt User Interface Compiler).
**
** G.Garand
**
**********************************************************************/
#include "uic.h"
#include "parser.h"
#include "widgetdatabase.h"
#include "domtool.h"
#include <ntqfile.h>
#include <ntqstringlist.h>
#include <ntqdatetime.h>
#define NO_STATIC_COLORS
#include <globaldefs.h>
#include <ntqregexp.h>
#include <stdio.h>
#include <stdlib.h>
bool Uic::isMainWindow = FALSE;
PyIndent Uic::indent;
// Re-calculate the indent string based on the current number and nature of the
// indent.
void PyIndent::calc()
{
indstr.truncate(0);
for (uint i = current; i > 0; --i)
if (tabStop == 0)
indstr += '\t';
else
for (uint t = 0; t < tabStop; ++t)
indstr += ' ';
}
TQString Uic::getComment( const TQDomNode& n )
{
TQDomNode child = n.firstChild();
while ( !child.isNull() ) {
if ( child.toElement().tagName() == "comment" )
return child.toElement().firstChild().toText().data();
child = child.nextSibling();
}
return TQString::null;
}
TQString Uic::mkBool( bool b )
{
return b? "1" : "0";
}
TQString Uic::mkBool( const TQString& s )
{
return mkBool( s == "true" || s == "1" );
}
bool Uic::toBool( const TQString& s )
{
return s == "true" || s.toInt() != 0;
}
TQString Uic::fixString( const TQString &str, bool encode )
{
TQString s;
if ( !encode ) {
s = str;
// PerlTQt: we don't want to use replace( TQString, TQString ) because we support TQt 3.0
s.replace( TQRegExp("\\\\"), "\\\\" );
s.replace( TQRegExp("\""), "\\\"" );
s.replace( TQRegExp("\\r?\\n"), "\\n\" .\n" + indent + "\"" );
} else {
TQCString utf8 = str.utf8();
const int l = utf8.length();
for ( int i = 0; i < l; ++i )
s += "\\x" + TQString::number( (uchar)utf8[i], 16 );
}
return "\"" + s + "\"";
}
TQString Uic::trcall( const TQString& sourceText, const TQString& comment )
{
if ( sourceText.isEmpty() && comment.isEmpty() )
return "\"\"";
TQString t = trmacro;
bool encode = FALSE;
if ( t.isNull() ) {
t = "tr";
for ( int i = 0; i < (int) sourceText.length(); i++ ) {
if ( sourceText[i].unicode() >= 0x80 ) {
t = "trUtf8";
encode = TRUE;
break;
}
}
}
if ( comment.isEmpty() )
return trmacro + "(" + fixString( sourceText, encode ) + ")";
return trmacro + "(" + fixString( sourceText, encode ) + "," + fixString( comment, encode ) + ")";
}
TQString Uic::mkStdSet( const TQString& prop )
{
return TQString( "set" ) + prop[0].upper() + prop.mid(1);
}
/*!
\class Uic uic.h
\brief User Interface Compiler
The class Uic encapsulates the user interface compiler (uic).
*/
Uic::Uic( const TQString &fn, const char *outputFn, TQTextStream &outStream,
TQDomDocument doc, bool subcl, const TQString &trm,
const TQString& subClass, bool omitForwardDecls, TQString &uicClass )
: out( outStream ), trout( &languageChangeBody ),
outputFileName( outputFn ), trmacro( trm ), nofwd( omitForwardDecls )
{
fileName = fn;
writeFunctImpl = TRUE;
defMargin = BOXLAYOUT_DEFAULT_MARGIN;
defSpacing = BOXLAYOUT_DEFAULT_SPACING;
externPixmaps = FALSE;
item_used = cg_used = pal_used = 0;
layouts << "hbox" << "vbox" << "grid";
tags = layouts;
tags << "widget";
pixmapLoaderFunction = getPixmapLoaderFunction( doc.firstChild().toElement() );
nameOfClass = getFormClassName( doc.firstChild().toElement() );
stdsetdef = toBool( doc.firstChild().toElement().attribute("stdsetdef") );
if ( doc.firstChild().isNull() || doc.firstChild().firstChild().isNull() )
return;
TQDomElement e = doc.firstChild().firstChild().toElement();
TQDomElement widget;
while ( !e.isNull() ) {
if ( e.tagName() == "widget" ) {
widget = e;
} else if ( e.tagName() == "pixmapinproject" ) {
externPixmaps = TRUE;
} else if ( e.tagName() == "layoutdefaults" ) {
defSpacing = e.attribute( "spacing", defSpacing.toString() );
defMargin = e.attribute( "margin", defMargin.toString() );
} else if ( e.tagName() == "layoutfunctions" ) {
defSpacing = e.attribute( "spacing", defSpacing.toString() );
bool ok;
defSpacing.toInt( &ok );
if ( !ok ) {
TQString buf = defSpacing.toString();
defSpacing = buf.append( "()" );
}
defMargin = e.attribute( "margin", defMargin.toString() );
defMargin.toInt( &ok );
if ( !ok ) {
TQString buf = defMargin.toString();
defMargin = buf.append( "()" );
}
}
e = e.nextSibling().toElement();
}
e = widget;
if ( nameOfClass.isEmpty() )
nameOfClass = getObjectName( e );
namespaces = TQStringList::split( "::", nameOfClass );
bareNameOfClass = namespaces.last();
namespaces.remove( namespaces.fromLast() );
uicClass = nameOfClass;
if ( subcl ) {
createSubImpl( e, subClass );
} else {
createFormImpl( e );
}
}
/*! Extracts a pixmap loader function from \a e
*/
TQString Uic::getPixmapLoaderFunction( const TQDomElement& e )
{
TQDomElement n;
for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
if ( n.tagName() == "pixmapfunction" )
return n.firstChild().toText().data();
}
return TQString::null;
}
/*! Extracts the forms class name from \a e
*/
TQString Uic::getFormClassName( const TQDomElement& e )
{
TQDomElement n;
TQString cn;
for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
if ( n.tagName() == "class" ) {
TQString s = n.firstChild().toText().data();
int i;
while ( ( i = s.find(' ' )) != -1 )
s[i] = '_';
cn = s;
}
}
return cn;
}
/*! Extracts a Perl class name from \a e.
*/
TQString Uic::getClassName( const TQDomElement& e )
{
TQString s = e.attribute( "class" );
if ( s.isEmpty() && e.tagName() == "toolbar" )
s = "TQt::ToolBar";
else if ( s.isEmpty() && e.tagName() == "menubar" )
s = "TQt::MenuBar";
else if( WidgetDatabase::idFromClassName( s ) == -1)
return s;
else
{
TQRegExp r("^([QK])(\\S+)");
if( r.search( s ) != -1 )
s = (r.cap(1) == 'K'?"KDE::":"TQt::") + r.cap(2);
}
return s;
}
/*! Returns TRUE if database framework code is generated, else FALSE.
*/
bool Uic::isFrameworkCodeGenerated( const TQDomElement& e )
{
TQDomElement n = getObjectProperty( e, "frameworkCode" );
if ( n.attribute("name") == "frameworkCode" &&
!DomTool::elementToVariant( n.firstChild().toElement(), TQVariant( TRUE, 0 ) ).toBool() )
return FALSE;
return TRUE;
}
/*! Extracts an object name from \a e. It's stored in the 'name'
property.
*/
TQString Uic::getObjectName( const TQDomElement& e )
{
TQDomElement n = getObjectProperty( e, "name" );
if ( n.firstChild().toElement().tagName() == "cstring" )
return n.firstChild().toElement().firstChild().toText().data();
return TQString::null;
}
/*! Extracts an layout name from \a e. It's stored in the 'name'
property of the preceeding sibling (the first child of a TQLayoutWidget).
*/
TQString Uic::getLayoutName( const TQDomElement& e )
{
TQDomElement p = e.parentNode().toElement();
TQString name;
if ( getClassName(p) != "TQt::LayoutWidget" )
name = "Layout";
TQDomElement n = getObjectProperty( p, "name" );
if ( n.firstChild().toElement().tagName() == "cstring" ) {
name.prepend( n.firstChild().toElement().firstChild().toText().data() );
return TQStringList::split( "::", name ).last();
}
return e.tagName();
}
TQString Uic::getDatabaseInfo( const TQDomElement& e, const TQString& tag )
{
TQDomElement n;
TQDomElement n1;
int child = 0;
// database info is a stringlist stored in this order
if ( tag == "connection" )
child = 0;
else if ( tag == "table" )
child = 1;
else if ( tag == "field" )
child = 2;
else
return TQString::null;
n = getObjectProperty( e, "database" );
if ( n.firstChild().toElement().tagName() == "stringlist" ) {
// find correct stringlist entry
TQDomElement n1 = n.firstChild().firstChild().toElement();
for ( int i = 0; i < child && !n1.isNull(); ++i )
n1 = n1.nextSibling().toElement();
if ( n1.isNull() )
return TQString::null;
return n1.firstChild().toText().data();
}
return TQString::null;
}
void Uic::registerLayouts( const TQDomElement &e )
{
if ( layouts.contains(e.tagName()) )
createObjectDecl(e);
TQDomNodeList nl = e.childNodes();
for ( int i = 0; i < (int) nl.length(); ++i )
registerLayouts( nl.item(i).toElement() );
}
/*!
Returns include file for class \a className or a null string.
*/
TQString Uic::getInclude( const TQString& className )
{
int wid = WidgetDatabase::idFromClassName( className );
if ( wid != -1 )
return WidgetDatabase::includeFile( wid );
return TQString::null;
}
void Uic::createActionDecl( const TQDomElement& e )
{
TQString objName = getObjectName( e );
if ( objName.isEmpty() )
return;
out << indent << objName << endl;
if ( e.tagName() == "actiongroup" ) {
for ( TQDomElement n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
if ( n.tagName() == "action" || n.tagName() == "actiongroup" )
createActionDecl( n );
}
}
}
void Uic::createActionImpl( const TQDomElement &n, const TQString &parent )
{
for ( TQDomElement ae = n; !ae.isNull(); ae = ae.nextSibling().toElement() ) {
TQString objName = registerObject( getObjectName( ae ) );
if ( ae.tagName() == "action" )
out << indent << objName << "= TQt::Action(" << parent << ", \"" << objName << "\");" << endl;
else if ( ae.tagName() == "actiongroup" )
out << indent << objName << "= TQt::ActionGroup(" << parent << ", \"" << objName << "\");" << endl;
else
continue;
bool subActionsDone = FALSE;
for ( TQDomElement n2 = ae.firstChild().toElement(); !n2.isNull(); n2 = n2.nextSibling().toElement() ) {
if ( n2.tagName() == "property" ) {
bool stdset = stdsetdef;
if ( n2.hasAttribute( "stdset" ) )
stdset = toBool( n2.attribute( "stdset" ) );
TQString prop = n2.attribute("name");
if ( prop == "name" )
continue;
TQString value = setObjectProperty( "TQt::Action", objName, prop, n2.firstChild().toElement(), stdset );
if ( value.isEmpty() )
continue;
TQString call = objName + "->";
bool needClose = false;
if ( stdset ) {
call += mkStdSet( prop ) + "( ";
} else {
call += "setProperty( \"" + prop + "\", TQt::Variant(" ;
needClose = true;
}
if ( prop == "accel" )
call += "TQt::KeySequence( " + value + " )"+ (needClose ? " )": "") + " );";
else
call += value + (needClose ? " )": "") + " );";
if ( n2.firstChild().toElement().tagName() == "string" ) {
trout << indent << call << endl;
} else {
out << indent << call << endl;
}
} else if ( !subActionsDone && ( n2.tagName() == "actiongroup" || n2.tagName() == "action" ) ) {
createActionImpl( n2, objName );
subActionsDone = TRUE;
}
}
}
}
TQString get_dock( const TQString &d )
{
if ( d == "0" )
return "&DockUnmanaged";
if ( d == "1" )
return "&DockTornOff";
if ( d == "2" )
return "&DockTop";
if ( d == "3" )
return "&DockBottom";
if ( d == "4" )
return "&DockRight";
if ( d == "5" )
return "&DockLeft";
if ( d == "6" )
return "&DockMinimized";
return "";
}
void Uic::createToolbarImpl( const TQDomElement &n, const TQString &parentClass, const TQString &parent )
{
TQDomNodeList nl = n.elementsByTagName( "toolbar" );
for ( int i = 0; i < (int) nl.length(); i++ ) {
TQDomElement ae = nl.item( i ).toElement();
TQString dock = get_dock( ae.attribute( "dock" ) );
TQString objName = getObjectName( ae );
out << indent << objName << " = TQt::ToolBar(\"\", this, " << dock << ");" << endl;
createObjectImpl( ae, parentClass, parent );
for ( TQDomElement n2 = ae.firstChild().toElement(); !n2.isNull(); n2 = n2.nextSibling().toElement() ) {
if ( n2.tagName() == "action" ) {
out << indent << n2.attribute( "name" ) << "->addTo(" << objName << ");" << endl;
} else if ( n2.tagName() == "separator" ) {
out << indent << objName << "->addSeparator;" << endl;
} else if ( n2.tagName() == "widget" ) {
if ( n2.attribute( "class" ) != "Spacer" ) {
createObjectImpl( n2, "TQt::ToolBar", objName );
} else {
TQString child = createSpacerImpl( n, parentClass, parent, objName );
out << indent << "TQt::Application->sendPostedEvents( $" << objName
<< ", &TQt::Event::ChildInserted);" << endl;
out << indent << "$" << objName << "->boxLayout()->addItem(" << child << ");" << endl;
}
}
}
}
}
void Uic::createMenuBarImpl( const TQDomElement &n, const TQString &parentClass, const TQString &parent )
{
TQString objName = getObjectName( n );
out << indent << objName << "= TQt::MenuBar( this, \"" << objName << "\");" << endl;
createObjectImpl( n, parentClass, parent );
TQDomNodeList nl = n.elementsByTagName( "item" );
int i = 0;
TQDomElement c = n.firstChild().toElement();
while ( !c.isNull() ) {
if ( c.tagName() == "item" ) {
TQString itemName = c.attribute( "name" );
out << endl;
out << indent << itemName << " = TQt::PopupMenu( this );" << endl;
createPopupMenuImpl( c, parentClass, itemName );
out << indent << objName << "->insertItem( \"\", " << itemName << ", " << i << " );" << endl;
trout << indent << objName << "->findItem( " << i << " )->setText( " << trcall( c.attribute( "text" ) ) << " );" << endl;
} else if ( c.tagName() == "separator" ) {
out << endl;
out << indent << objName << "->insertSeparator( " << i << " );" << endl;
}
c = c.nextSibling().toElement();
i++;
}
}
void Uic::createPopupMenuImpl( const TQDomElement &e, const TQString &parentClass, const TQString &parent )
{
for ( TQDomElement n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
if ( n.tagName() == "action" ) {
TQDomElement n2 = n.nextSibling().toElement();
if ( n2.tagName() == "item" ) { // the action has a sub menu
TQString itemName = n2.attribute( "name" );
TQString itemText = n2.attribute( "text" );
out << indent << itemName << " = TQt::PopupMenu( this );" << endl;
out << indent << parent << "->setAccel( tr( \"" << n2.attribute( "accel" ) << "\" ), " << endl;
++indent;
out << indent << parent << "->insertItem( " << n.attribute( "name" ) << "->iconSet(),";
out << trcall( itemText ) << ", " << itemName << " ) );" << endl;
--indent;
createPopupMenuImpl( n2, parentClass, itemName );
n = n2;
} else {
out << indent << n.attribute( "name" ) << "->addTo( " << parent << " );" << endl;
}
} else if ( n.tagName() == "separator" ) {
out << indent << parent << "->insertSeparator();" << endl;
}
}
}
/*!
Creates implementation of an listbox item tag.
*/
TQString Uic::createListBoxItemImpl( const TQDomElement &e, const TQString &parent,
TQString *value )
{
TQDomElement n = e.firstChild().toElement();
TQString txt;
TQString com;
TQString pix;
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TQString attrib = n.attribute( "name" );
TQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TQVariant() );
if ( attrib == "text" ) {
txt = v.toString();
com = getComment( n );
} else if ( attrib == "pixmap" ) {
pix = v.toString();
if( !pix.isEmpty() && !externPixmaps )
pix.prepend( '$' );
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TQString( externPixmaps ? "\"" : "" ) );
pix.append( TQString( externPixmaps ? "\"" : "" ) + ")" );
}
}
}
n = n.nextSibling().toElement();
}
if ( value )
*value = trcall( txt, com );
if ( pix.isEmpty() ) {
return parent + "->insertItem( " + trcall( txt, com ) + " );";
} else {
return parent + "->insertItem( " + pix + ", " + trcall( txt, com ) + " );";
}
}
/*!
Creates implementation of an iconview item tag.
*/
TQString Uic::createIconViewItemImpl( const TQDomElement &e, const TQString &parent )
{
TQDomElement n = e.firstChild().toElement();
TQString txt;
TQString com;
TQString pix;
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TQString attrib = n.attribute( "name" );
TQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TQVariant() );
if ( attrib == "text" ) {
txt = v.toString();
com = getComment( n );
} else if ( attrib == "pixmap" ) {
pix = v.toString();
if( !pix.isEmpty() && !externPixmaps )
pix.prepend( '$' );
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TQString( externPixmaps ? "\"" : "" ) );
pix.append( TQString( externPixmaps ? "\"" : "" ) + ")" );
}
}
}
n = n.nextSibling().toElement();
}
if ( pix.isEmpty() )
return "TQt::IconViewItem(" + parent + ", " + trcall( txt, com ) + ");";
else
return "TQt::IconViewItem(" + parent + ", " + trcall( txt, com ) + ", " + pix + ");";
}
/*!
Creates implementation of an listview item tag.
*/
TQString Uic::createListViewItemImpl( const TQDomElement &e, const TQString &parent,
const TQString &parentItem )
{
TQString s;
TQDomElement n = e.firstChild().toElement();
bool hasChildren = e.elementsByTagName( "item" ).count() > 0;
TQString item;
if ( hasChildren ) {
item = "$" + registerObject( "item" );
s = indent + "my " + item + " = ";
} else {
item = "$item";
if ( item_used )
s = indent + item + " = ";
else
s = indent + "my " + item + " = ";
item_used = TRUE;
}
if ( !parentItem.isEmpty() )
s += "TQt::ListViewItem(" + parentItem + ", " + lastItem + ");\n";
else
s += "TQt::ListViewItem(" + parent + ", " + lastItem + ");\n";
TQStringList texts;
TQStringList pixmaps;
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TQString attrib = n.attribute("name");
TQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TQVariant() );
if ( attrib == "text" )
texts << v.toString();
else if ( attrib == "pixmap" ) {
TQString pix = v.toString();
if( !pix.isEmpty() && !externPixmaps )
pix.prepend( '$' );
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TQString( externPixmaps ? "\"" : "" ) );
pix.append( TQString( externPixmaps ? "\"" : "" ) + ")" );
}
pixmaps << pix;
}
} else if ( n.tagName() == "item" ) {
s += indent + item + "->setOpen(1);\n";
s += createListViewItemImpl( n, parent, item );
}
n = n.nextSibling().toElement();
}
for ( int i = 0; i < (int)texts.count(); ++i ) {
if ( !texts[ i ].isEmpty() )
s += indent + item + "->setText(" + TQString::number( i ) + ", " + trcall( texts[ i ] ) + ");\n";
if ( !pixmaps[ i ].isEmpty() )
s += indent + item + "->setPixmap(" + TQString::number( i ) + ", " + pixmaps[ i ] + ");\n";
}
lastItem = item;
return s;
}
/*!
Creates implementation of an listview column tag.
*/
TQString Uic::createListViewColumnImpl( const TQDomElement &e, const TQString &parent,
TQString *value )
{
TQDomElement n = e.firstChild().toElement();
TQString txt;
TQString com;
TQString pix;
bool clickable = FALSE, resizable = FALSE;
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TQString attrib = n.attribute("name");
TQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TQVariant() );
if ( attrib == "text" ) {
txt = v.toString();
com = getComment( n );
} else if ( attrib == "pixmap" ) {
pix = v.toString();
if( !pix.isEmpty() && !externPixmaps )
pix.prepend( '$' );
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TQString( externPixmaps ? "\"" : "" ) );
pix.append( TQString( externPixmaps ? "\"" : "" ) + ")" );
}
} else if ( attrib == "clickable" )
clickable = v.toBool();
else if ( attrib == "resizable" || attrib == "resizeable" )
resizable = v.toBool();
}
n = n.nextSibling().toElement();
}
if ( value )
*value = trcall( txt, com );
TQString s;
s = indent + parent + "->addColumn(" + trcall( txt, com ) + ");\n";
if ( !pix.isEmpty() )
s += indent + parent + "->header()->setLabel(" + parent + "->header()->count() - 1," + pix + ", " + trcall( txt, com ) + ");\n";
if ( !clickable )
s += indent + parent + "->header()->setClickEnabled( 0, " + parent + "->header()->count() - 1 );\n";
if ( !resizable )
s += indent + parent + "->header()->setResizeEnabled( 0, " + parent + "->header()->count() - 1 );\n";
return s;
}
TQString Uic::createTableRowColumnImpl( const TQDomElement &e, const TQString &parent,
TQString *value )
{
TQString objClass = getClassName( e.parentNode().toElement() );
TQDomElement n = e.firstChild().toElement();
TQString txt;
TQString com;
TQString pix;
TQString field;
bool isRow = e.tagName() == "row";
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TQString attrib = n.attribute("name");
TQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TQVariant() );
if ( attrib == "text" ) {
txt = v.toString();
com = getComment( n );
} else if ( attrib == "pixmap" ) {
pix = v.toString();
if( !pix.isEmpty() && !externPixmaps )
pix.prepend( '$' );
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TQString( externPixmaps ? "\"" : "" ) );
pix.append( TQString( externPixmaps ? "\"" : "" ) + ")" );
}
} else if ( attrib == "field" )
field = v.toString();
}
n = n.nextSibling().toElement();
}
if ( value )
*value = trcall( txt, com );
// ### This generated code sucks! We have to set the number of
// rows/cols before and then only do setLabel/()
// ### careful, though, since TQDataTable has an API which makes this code pretty good
TQString s;
if ( isRow ) {
s = indent + parent + "->setNumRows(" + parent + "->numRows() + 1 );\n";
if ( pix.isEmpty() )
s += indent + parent + "->verticalHeader()->setLabel(" + parent + "->numRows() - 1, "
+ trcall( txt, com ) + ");\n";
else
s += indent + parent + "->verticalHeader()->setLabel(" + parent + "->numRows() - 1, TQt::IconSet("
+ pix + " ), " + trcall( txt, com ) + ");\n";
} else {
if ( objClass == "TQt::Table" ) {
s = indent + parent + "->setNumCols(" + parent + "->numCols() + 1);\n";
if ( pix.isEmpty() )
s += indent + parent + "->horizontalHeader()->setLabel(" + parent + "->numCols() - 1, "
+ trcall( txt, com ) + ");\n";
else
s += indent + parent + "->horizontalHeader()->setLabel(" + parent + "->numCols() - 1, TQt::IconSet("
+ pix + " ), " + trcall( txt, com ) + ");\n";
} else if ( objClass == "TQt::DataTable" ) {
if ( !txt.isEmpty() && !field.isEmpty() ) {
if ( pix.isEmpty() )
out << indent << parent << "->addColumn(" << fixString( field ) << ", " << trcall( txt, com ) << ");" << endl;
else
out << indent << parent << "->addColumn(" << fixString( field ) << ", " << trcall( txt, com ) << ", TQt::IconSet(" << pix << "));" << endl;
}
}
}
return s;
}
/*!
Creates the implementation of a layout tag. Called from createObjectImpl().
*/
TQString Uic::createLayoutImpl( const TQDomElement &e, const TQString& parentClass, const TQString& parent, const TQString& layout )
{
TQDomElement n;
TQString objClass, objName;
objClass = e.tagName();
TQString qlayout = "TQt::VBoxLayout";
if ( objClass == "hbox" )
qlayout = "TQt::HBoxLayout";
else if ( objClass == "grid" )
qlayout = "TQt::GridLayout";
bool isGrid = e.tagName() == "grid" ;
objName = registerObject( "$" + getLayoutName( e ) );
layoutObjects += objName;
TQString margin = DomTool::readProperty( e, "margin", defMargin ).toString();
TQString spacing = DomTool::readProperty( e, "spacing", defSpacing ).toString();
TQString resizeMode = DomTool::readProperty( e, "resizeMode", TQString::null ).toString();
TQString optcells;
if ( isGrid )
optcells = "1, 1, ";
if ( (parentClass == "TQt::GroupBox" || parentClass == "TQt::ButtonGroup") && layout.isEmpty() ) {
// special case for group box
out << indent << parent << "->setColumnLayout( 0, &Vertical );" << endl;
out << indent << parent << "->layout()->setSpacing(" << spacing << ");" << endl;
out << indent << parent << "->layout()->setMargin(" << margin << ");" << endl;
out << indent << "my " << objName << " = " << qlayout << "(" << parent << "->layout() );" << endl;
out << indent << objName << "->setAlignment( &AlignTop );" << endl;
} else {
out << indent << "my " << objName << " = " << qlayout << "(";
if ( layout.isEmpty() )
out << parent;
else {
out << "undef";
if ( !DomTool::hasProperty( e, "margin" ) )
margin = "0";
}
out << ", " << optcells << margin << ", " << spacing << ", '" << objName << "');" << endl;
}
if ( !resizeMode.isEmpty() )
out << indent << objName << "->setResizeMode( &TQt::Layout::" << resizeMode << " );" << endl;
if ( !isGrid ) {
for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
if ( n.tagName() == "spacer" ) {
TQString child = createSpacerImpl( n, parentClass, parent, objName );
out << indent << objName << "->addItem(" << child << ");" << endl;
} else if ( tags.contains( n.tagName() ) ) {
TQString child = createObjectImpl( n, parentClass, parent, objName );
if ( isLayout( child ) )
out << indent << objName << "->addLayout(" << child << ");" << endl;
else
out << indent << objName << "->addWidget(" << child << ");" << endl;
}
}
} else {
for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
TQDomElement ae = n;
int row = ae.attribute( "row" ).toInt();
int col = ae.attribute( "column" ).toInt();
int rowspan = ae.attribute( "rowspan" ).toInt();
int colspan = ae.attribute( "colspan" ).toInt();
if ( rowspan < 1 )
rowspan = 1;
if ( colspan < 1 )
colspan = 1;
if ( n.tagName() == "spacer" ) {
TQString child = createSpacerImpl( n, parentClass, parent, objName );
if ( rowspan * colspan != 1 )
out << indent << objName << "->addMultiCell(" << child << ", "
<< row << ", " << ( row + rowspan - 1 ) << ", " << col << ", " << col + colspan - 1 << ");" << endl;
else
out << indent << objName << "->addItem(" << child << ", "
<< row << ", " << col << ");" << endl;
} else if ( tags.contains( n.tagName() ) ) {
TQString child = createObjectImpl( n, parentClass, parent, objName );
out << endl;
TQString o = "Widget";
if ( isLayout( child ) )
o = "Layout";
if ( rowspan * colspan != 1 )
out << indent << objName << "->addMultiCell" << o << "(" << child << ", "
<< row << ", " << ( row + rowspan - 1 ) << ", " << col << ", " << col + colspan - 1 << ");" << endl;
else
out << indent << objName << "->add" << o << "(" << child << ", "
<< row << ", " << col << ");" << endl;
}
}
}
return objName;
}
TQString Uic::createSpacerImpl( const TQDomElement &e, const TQString& /*parentClass*/, const TQString& /*parent*/, const TQString& /*layout*/)
{
TQDomElement n;
TQString objClass, objName;
objClass = e.tagName();
objName = registerObject( "$spacer" );
TQSize size = DomTool::readProperty( e, "sizeHint", TQSize( 0, 0 ) ).toSize();
TQString sizeType = DomTool::readProperty( e, "sizeType", "Expanding" ).toString();
bool isVspacer = DomTool::readProperty( e, "orientation", "Horizontal" ) == "Vertical";
if ( sizeType != "Expanding" && sizeType != "MinimumExpanding" &&
DomTool::hasProperty( e, "geometry" ) ) { // compatibility TQt 2.2
TQRect geom = DomTool::readProperty( e, "geometry", TQRect(0,0,0,0) ).toRect();
size = geom.size();
}
if ( isVspacer )
out << indent << "my " << objName << " = TQt::SpacerItem("
<< size.width() << ", " << size.height()
<< ", &TQt::SizePolicy::Minimum, &TQt::SizePolicy::" << sizeType << ");" << endl;
else
out << indent << "my " << objName << " = TQt::SpacerItem("
<< size.width() << ", " << size.height()
<< ", &TQt::SizePolicy::" << sizeType << ", &TQt::SizePolicy::Minimum);" << endl;
return objName;
}
static const char* const ColorRole[] = {
"Foreground", "Button", "Light", "Midlight", "Dark", "Mid",
"Text", "BrightText", "ButtonText", "Base", "Background", "Shadow",
"Highlight", "HighlightedText", "Link", "LinkVisited", 0
};
/*!
Creates a colorgroup with name \a name from the color group \a cg
*/
void Uic::createColorGroupImpl( const TQString& name, const TQDomElement& e )
{
TQColorGroup cg;
int r = -1;
TQDomElement n = e.firstChild().toElement();
TQString color;
while ( !n.isNull() ) {
if ( n.tagName() == "color" ) {
r++;
TQColor col = DomTool::readColor( n );
color = "TQt::Color(%1,%2,%3)";
color = color.arg( col.red() ).arg( col.green() ).arg( col.blue() );
if ( col == white )
color = "&white";
else if ( col == black )
color = "&black";
if ( n.nextSibling().toElement().tagName() != "pixmap" ) {
out << indent << name << "->setColor(&TQt::ColorGroup::" << ColorRole[r] << ", " << color << ");" << endl;
}
} else if ( n.tagName() == "pixmap" ) {
TQString pixmap = n.firstChild().toText().data();
if( !pixmap.isEmpty() && !externPixmaps )
pixmap.prepend( '$' );
if ( !pixmapLoaderFunction.isEmpty() ) {
pixmap.prepend( pixmapLoaderFunction + "(" + TQString( externPixmaps ? "\"" : "" ) );
pixmap.append( TQString( externPixmaps ? "\"" : "" ) + ")" );
}
out << indent << name << "->setBrush(&TQt::ColorGroup::"
<< ColorRole[r] << ", TQt::Brush(" << color << ", " << pixmap << "));" << endl;
}
n = n.nextSibling().toElement();
}
}
/*!
Auxiliary function to load a color group. The colorgroup must not
contain pixmaps.
*/
TQColorGroup Uic::loadColorGroup( const TQDomElement &e )
{
TQColorGroup cg;
int r = -1;
TQDomElement n = e.firstChild().toElement();
TQColor col;
while ( !n.isNull() ) {
if ( n.tagName() == "color" ) {
r++;
cg.setColor( (TQColorGroup::ColorRole)r, (col = DomTool::readColor( n ) ) );
}
n = n.nextSibling().toElement();
}
return cg;
}
/*! Returns TRUE if the widget properties specify that it belongs to
the database \a connection and \a table.
*/
bool Uic::isWidgetInTable( const TQDomElement& e, const TQString& connection, const TQString& table )
{
TQString conn = getDatabaseInfo( e, "connection" );
TQString tab = getDatabaseInfo( e, "table" );
if ( conn == connection && tab == table )
return TRUE;
return FALSE;
}
/*!
Registers all database connections, cursors and forms.
*/
void Uic::registerDatabases( const TQDomElement& e )
{
TQDomElement n;
TQDomNodeList nl;
int i;
nl = e.parentNode().toElement().elementsByTagName( "widget" );
for ( i = 0; i < (int) nl.length(); ++i ) {
n = nl.item(i).toElement();
TQString conn = getDatabaseInfo( n, "connection" );
TQString tab = getDatabaseInfo( n, "table" );
TQString fld = getDatabaseInfo( n, "field" );
if ( !conn.isNull() ) {
dbConnections += conn;
if ( !tab.isNull() ) {
dbCursors[conn] += tab;
if ( !fld.isNull() )
dbForms[conn] += tab;
}
}
}
}
/*!
Registers an object with name \a name.
The returned name is a valid variable identifier, as similar to \a
name as possible and guaranteed to be unique within the form.
\sa registeredName(), isObjectRegistered()
*/
TQString Uic::registerObject( const TQString& name )
{
if ( objectNames.isEmpty() ) {
// some temporary variables we need
objectNames += "img";
objectNames += "item";
objectNames += "cg";
objectNames += "pal";
}
TQString result = name;
int i;
while ( ( i = result.find(' ' )) != -1 ) {
result[i] = '_';
}
if ( objectNames.contains( result ) ) {
int i = 2;
while ( objectNames.contains( result + "_" + TQString::number(i) ) )
i++;
result += "_";
result += TQString::number(i);
}
objectNames += result;
objectMapper.insert( name, result );
return result;
}
/*!
Returns the registered name for the original name \a name
or \a name if \a name wasn't registered.
\sa registerObject(), isObjectRegistered()
*/
TQString Uic::registeredName( const TQString& name )
{
if ( !objectMapper.contains( name ) )
return name;
return objectMapper[name];
}
/*!
Returns whether the object \a name was registered yet or not.
*/
bool Uic::isObjectRegistered( const TQString& name )
{
return objectMapper.contains( name );
}
/*!
Unifies the entries in stringlist \a list. Should really be a TQStringList feature.
*/
TQStringList Uic::unique( const TQStringList& list )
{
if ( list.isEmpty() )
return list;
TQStringList result;
for ( TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
if ( !result.contains(*it) )
result += *it;
}
return result;
}
/*!
Creates an instance of class \a objClass, with parent \a parent and name \a objName
*/
TQString Uic::createObjectInstance( const TQString& objClass, const TQString& parent, const TQString& objName )
{
if ( objClass.mid( 4 ) == "ComboBox" ) {
return objClass + "(0, " + parent + ", \"" + objName + "\")";
}
return objClass + "(" + parent + ", \"" + objName + "\")";
}
bool Uic::isLayout( const TQString& name ) const
{
return layoutObjects.contains( name );
}
|