SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1602 lines
49KB

  1. // Copyright (c) 2000 - 2001 Phil Thompson <phil@river-bank.demon.co.uk>
  2. /**********************************************************************
  3. ** Copyright (C) 2000 Troll Tech AS. All rights reserved.
  4. **
  5. ** This file is part of TQt GUI Designer.
  6. **
  7. ** This file may be distributed under the terms of the GNU General
  8. ** Public License version 2 as published by the Free Software
  9. ** Foundation and appearing in the file COPYING included in the
  10. ** packaging of this file. If you did not get the file, send email
  11. ** to info@trolltech.com
  12. **
  13. ** The file is provided AS IS with NO WARRANTY OF ANY KIND,
  14. ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
  15. ** A PARTICULAR PURPOSE.
  16. **
  17. **********************************************************************/
  18. #include "uic.h"
  19. #include <qapplication.h>
  20. #include <qfile.h>
  21. #include <qfileinfo.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <qstringlist.h>
  25. #include <qstrlist.h>
  26. #include <qdatetime.h>
  27. #include <widgetdatabase.h>
  28. #include <domtool.h>
  29. #include <globaldefs.h>
  30. #include <qregexp.h>
  31. #include <zlib.h>
  32. static TQString mkBool( bool b )
  33. {
  34. return b? "1" : "0";
  35. }
  36. static TQString mkBool( const TQString& s )
  37. {
  38. return mkBool( s == "true" || s == "1" );
  39. }
  40. static bool toBool( const TQString& s )
  41. {
  42. return s == "true" || s.toInt() != 0;
  43. }
  44. // fixString is only used in conjunction with tr(). We need to write out the
  45. // string in utf8 and make sure it's converted from utf8 when created.
  46. static TQString fixString( const TQString &str )
  47. {
  48. TQString s( str );
  49. s.replace( TQRegExp( "\\\\" ), "\\\\" );
  50. s.replace( TQRegExp( "\"" ), "\\\"" );
  51. s.replace( TQRegExp( "\n" ), "\\n\"\n\"" );
  52. s.replace( TQRegExp( "\r" ), "\\r" );
  53. bool onlyAscii = TRUE;
  54. unsigned int i;
  55. for ( i = 0; i < s.length(); i++ ) {
  56. if ( s.at(i).unicode() >= 0x80 ) {
  57. onlyAscii = FALSE;
  58. break;
  59. }
  60. }
  61. if ( onlyAscii )
  62. s = "\"" + s + "\"";
  63. else
  64. s = "TQString.fromUtf8(\"" + s + "\")";
  65. return s;
  66. }
  67. static TQString mkStdSet( const TQString& prop )
  68. {
  69. return TQString( "set" ) + prop[0].upper() + prop.mid(1);
  70. }
  71. /*!
  72. \class Uic uic.h
  73. \brief User Interface Compiler
  74. The class Uic encapsulates the user interface compiler (uic).
  75. */
  76. static TQString className;
  77. Uic::Uic( TQTextStream &outStream, TQDomDocument doc, const TQString &trm )
  78. : out( outStream ), trmacro( trm )
  79. {
  80. item_used = cg_used = pal_used = 0;
  81. initIndent();
  82. layouts << "hbox" << "vbox" << "grid";
  83. tags = layouts;
  84. tags << "widget";
  85. nameOfClass = getClassName( doc.firstChild().toElement() );
  86. TQDomElement firstWidget = doc.firstChild().firstChild().toElement();
  87. while ( firstWidget.tagName() != "widget" )
  88. firstWidget = firstWidget.nextSibling().toElement();
  89. if ( nameOfClass.isEmpty() )
  90. nameOfClass = getObjectName( firstWidget );
  91. // This is a hack.
  92. className = nameOfClass;
  93. createFormImpl( firstWidget );
  94. }
  95. /*! Extracts a class name from \a e
  96. */
  97. TQString Uic::getClassName( const TQDomElement& e )
  98. {
  99. TQDomElement n;
  100. TQString cn;
  101. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  102. if ( n.tagName() == "class" ) {
  103. TQString s = n.firstChild().toText().data();
  104. int i;
  105. while ( ( i = s.find(' ' )) != -1 )
  106. s[i] = '_';
  107. cn = s;
  108. } else if ( n.tagName() == "pixmapfunction" ) {
  109. pixmapLoaderFunction = n.firstChild().toText().data();
  110. }
  111. }
  112. return cn;
  113. }
  114. /*! Extracts an object name from \a e. It's stored in the 'name'
  115. property.
  116. */
  117. TQString Uic::getObjectName( const TQDomElement& e )
  118. {
  119. TQDomElement n;
  120. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  121. if ( n.tagName() == "property" ) {
  122. TQDomElement n2 = n.firstChild().toElement();
  123. if ( n2.tagName() == "name" && n2.firstChild().toText().data() == "name" ) {
  124. return n2.nextSibling().toElement().firstChild().toText().data();
  125. }
  126. }
  127. }
  128. return TQString::null;
  129. }
  130. /*! Extracts an layout name from \a e. It's stored in the 'name'
  131. property of the preceeding sibling (the first child of a TQLayoutWidget).
  132. */
  133. TQString Uic::getLayoutName( const TQDomElement& e )
  134. {
  135. TQDomElement p = e.parentNode().toElement();
  136. TQString tail = TQString::null;
  137. if (getClassName(p) != "TQLayoutWidget")
  138. tail = "Layout";
  139. TQDomElement n;
  140. for ( n = p.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  141. if ( n.tagName() == "property" ) {
  142. TQDomElement n2 = n.firstChild().toElement();
  143. if ( n2.tagName() == "name" && n2.firstChild().toText().data() == "name" ) {
  144. return n2.nextSibling().toElement().firstChild().toText().data() + tail;
  145. }
  146. }
  147. }
  148. return e.tagName();
  149. }
  150. TQByteArray unzipXPM( TQString data, ulong& length )
  151. {
  152. char *ba = new char[ data.length() / 2 ];
  153. for ( int i = 0; i < (int)data.length() / 2; ++i ) {
  154. char h = data[ 2 * i ].latin1();
  155. char l = data[ 2 * i + 1 ].latin1();
  156. uchar r = 0;
  157. if ( h <= '9' )
  158. r += h - '0';
  159. else
  160. r += h - 'a' + 10;
  161. r = r << 4;
  162. if ( l <= '9' )
  163. r += l - '0';
  164. else
  165. r += l - 'a' + 10;
  166. ba[ i ] = r;
  167. }
  168. if ( length < data.length() * 5 )
  169. length = data.length() * 5;
  170. TQByteArray baunzip( length );
  171. ::uncompress( (uchar*) baunzip.data(), &length, (uchar*) ba, data.length()/2 );
  172. return baunzip;
  173. }
  174. /*!
  175. Creates an implementation ( cpp-file ) for the form given in \a e
  176. \sa createFormDecl(), createObjectImpl()
  177. */
  178. void Uic::createFormImpl( const TQDomElement &e )
  179. {
  180. TQStringList::Iterator it;
  181. TQDomElement n;
  182. TQDomNodeList nl;
  183. int i;
  184. TQString objClass = getClassName( e );
  185. if ( objClass.isEmpty() )
  186. return;
  187. TQString objName = getObjectName( e );
  188. // Handle custom widgets.
  189. for (n = e; !n.isNull(); n = n.nextSibling().toElement())
  190. {
  191. if (n.tagName() != "customwidgets")
  192. continue;
  193. TQDomElement n2 = n.firstChild().toElement();
  194. while (!n2.isNull())
  195. {
  196. if (n2.tagName() == "customwidget")
  197. {
  198. TQDomElement n3 = n2.firstChild().toElement();
  199. TQString cname, header;
  200. while (!n3.isNull())
  201. {
  202. if (n3.tagName() == "class")
  203. cname = n3.firstChild().toText().data();
  204. else if (n3.tagName() == "header")
  205. header = n3.firstChild().toText().data();
  206. n3 = n3.nextSibling().toElement();
  207. }
  208. if (cname.isEmpty())
  209. cname = "UnnamedCustomClass";
  210. int ext = header.findRev('.');
  211. if (ext >= 0)
  212. header.truncate(ext);
  213. if (header.isEmpty())
  214. header = cname.lower();
  215. out << "from " << header << " import " << cname << endl;
  216. }
  217. n2 = n2.nextSibling().toElement();
  218. }
  219. }
  220. // find out what images are required
  221. TQStringList requiredImages;
  222. nl = e.elementsByTagName( "pixmap" );
  223. for ( int j = 0; j < (int) nl.length(); j++ ) {
  224. requiredImages += nl.item(j).firstChild().toText().data();
  225. }
  226. TQStringList images;
  227. TQStringList xpmImages;
  228. if ( pixmapLoaderFunction.isEmpty() ) {
  229. // create images
  230. for ( n = e; !n.isNull(); n = n.nextSibling().toElement() ) {
  231. if ( n.tagName() == "images" ) {
  232. nl = n.elementsByTagName( "image" );
  233. for ( i = 0; i < (int) nl.length(); i++ ) {
  234. TQDomElement tmp = nl.item(i).firstChild().toElement();
  235. TQString img = registerObject( tmp.firstChild().toText().data() );
  236. if ( !requiredImages.contains( img ) )
  237. continue;
  238. tmp = tmp.nextSibling().toElement();
  239. TQString format = tmp.attribute("format", "PNG" );
  240. TQString data = tmp.firstChild().toText().data();
  241. out << endl;
  242. out << endl;
  243. if ( format == "XPM.GZ" ) {
  244. xpmImages += img;
  245. ulong length = tmp.attribute("length").toULong();
  246. TQByteArray baunzip = unzipXPM( data, length );
  247. int a = 0;
  248. out << indent << img << "_data = [" << endl;
  249. while ( baunzip[a] != '\"' )
  250. a++;
  251. for ( ; a < (int) length; a++ )
  252. {
  253. char ch;
  254. if ((ch = baunzip[a]) == '}')
  255. {
  256. out << endl << "]" << endl;
  257. break;
  258. }
  259. if (ch == '\"')
  260. ch = '\'';
  261. out << ch;
  262. }
  263. } else {
  264. images += img;
  265. out << indent << img << "_data = \\" << endl;
  266. pushIndent();
  267. out << indent << "'";
  268. int a ;
  269. for ( a = 0; a < (int) (data.length()/2)-1; a++ ) {
  270. out << "\\x" << TQString(data[2*a]) << TQString(data[2*a+1]);
  271. if ( a % 12 == 11 )
  272. out << "' \\" << endl << indent << "'";
  273. }
  274. out << "\\x" << TQString(data[2*a]) << TQString(data[2*a+1]) << "'" << endl;
  275. popIndent();
  276. }
  277. }
  278. }
  279. }
  280. }
  281. // register the object and unify its name
  282. objName = registerObject( objName );
  283. // constructor
  284. out << endl;
  285. out << endl;
  286. out << indent << "class " << nameOfClass << "(" << objClass << "):" << endl;
  287. pushIndent();
  288. if ( objClass == "TQDialog" || objClass == "TQWizard" ) {
  289. out << indent << "def __init__(self,parent = None,name = None,modal = 0,fl = 0):" << endl;
  290. pushIndent();
  291. out << indent << objClass << ".__init__(self,parent,name,modal,fl)" << endl;
  292. } else if ( objClass == "TQWidget" ) { // standard TQWidget
  293. out << indent << "def __init__(self,parent = None,name = None,fl = 0):" << endl;
  294. pushIndent();
  295. out << indent << objClass << ".__init__(self,parent,name,fl)" << endl;
  296. } else {
  297. out << indent << "def __init__(self,parent = None,name = None):" << endl;
  298. pushIndent();
  299. out << indent << objClass << ".__init__(self,parent,name)" << endl;
  300. }
  301. out << endl;
  302. // create pixmaps for all images
  303. if ( !images.isEmpty() ) {
  304. TQStringList::Iterator it;
  305. for ( it = images.begin(); it != images.end(); ++it ) {
  306. out << indent << (*it) << " = TQPixmap()" << endl;
  307. out << indent << (*it) << ".loadFromData(" << (*it) << "_data,'PNG')" << endl;
  308. }
  309. out << endl;
  310. }
  311. // create pixmaps for all images
  312. if ( !xpmImages.isEmpty() ) {
  313. for ( it = xpmImages.begin(); it != xpmImages.end(); ++it ) {
  314. out << indent << (*it) << " = TQPixmap(" << (*it) << "_data)" << endl;
  315. }
  316. out << endl;
  317. }
  318. // set the properties
  319. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  320. if ( n.tagName() == "property" ) {
  321. bool stdset = toBool( n.attribute( "stdset" ) );
  322. TQDomElement n2 = n.firstChild().toElement();
  323. if ( n2.tagName() == "name" ) {
  324. TQString prop = n2.firstChild().toText().data();
  325. TQString value = setObjectProperty( objClass, TQString::null, prop, n2.nextSibling().toElement(), stdset );
  326. if ( value.isEmpty() )
  327. continue;
  328. if ( prop == "name" ) {
  329. out << indent << "if name == None:" << endl;
  330. pushIndent();
  331. }
  332. out << indent;
  333. if ( prop == "geometry" && n2.nextSibling().toElement().tagName() == "rect") {
  334. TQDomElement n3 = n2.nextSibling().toElement().firstChild().toElement();
  335. int w = 0, h = 0;
  336. while ( !n3.isNull() ) {
  337. if ( n3.tagName() == "width" )
  338. w = n3.firstChild().toText().data().toInt();
  339. else if ( n3.tagName() == "height" )
  340. h = n3.firstChild().toText().data().toInt();
  341. n3 = n3.nextSibling().toElement();
  342. }
  343. out << "self.resize(" << w << "," << h << ")" << endl;
  344. } else {
  345. if ( stdset )
  346. out << "self." << mkStdSet(prop) << "(" << value << ")" << endl;
  347. else
  348. out << "self.setProperty('" << prop << "',TQVariant(" << value << "))" << endl;
  349. }
  350. if (prop == "name") {
  351. popIndent();
  352. out << endl;
  353. }
  354. }
  355. }
  356. }
  357. // create all children, some forms have special requirements
  358. if ( objClass == "TQWizard" ) {
  359. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  360. if ( tags.contains( n.tagName() ) ) {
  361. TQString page = createObjectImpl( n, objClass, "self" );
  362. TQString label = DomTool::readAttribute( n, "title", "" ).toString();
  363. out << indent << "self.addPage(" << page << ","<< trmacro << "(" << fixString( label ) << "))" << endl;
  364. TQVariant def( FALSE, 0 );
  365. if ( DomTool::hasAttribute( n, "backEnabled" ) )
  366. out << indent << "self.setBackEnabled(" << page << "," << mkBool( DomTool::readAttribute( n, "backEnabled", def).toBool() ) << ")" << endl;
  367. if ( DomTool::hasAttribute( n, "nextEnabled" ) )
  368. out << indent << "self.setNextEnabled(" << page << "," << mkBool( DomTool::readAttribute( n, "nextEnabled", def).toBool() ) << ")" << endl;
  369. if ( DomTool::hasAttribute( n, "finishEnabled" ) )
  370. out << indent << "self.setFinishEnabled(" << page << "," << mkBool( DomTool::readAttribute( n, "finishEnabled", def).toBool() ) << ")" << endl;
  371. if ( DomTool::hasAttribute( n, "helpEnabled" ) )
  372. out << indent << "self.setHelpEnabled(" << page << "," << mkBool( DomTool::readAttribute( n, "helpEnabled", def).toBool() ) << ")" << endl;
  373. if ( DomTool::hasAttribute( n, "finish" ) )
  374. out << indent << "self.setFinish(" << page << "," << mkBool( DomTool::readAttribute( n, "finish", def).toBool() ) << ")" << endl;
  375. }
  376. }
  377. } else { // standard widgets
  378. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  379. if ( tags.contains( n.tagName() ) )
  380. createObjectImpl( n, objName, "self" );
  381. }
  382. }
  383. // Get the list of any user defined slots.
  384. TQStringList userSlots;
  385. for ( n = e; !n.isNull(); n = n.nextSibling().toElement() ) {
  386. if ( n.tagName() == "connections" ) {
  387. for ( TQDomElement n2 = n.firstChild().toElement(); !n2.isNull(); n2 = n2.nextSibling().toElement() ) {
  388. if ( n2.tagName() == "slot" ) {
  389. userSlots += n2.firstChild().toText().data();
  390. }
  391. }
  392. }
  393. }
  394. for ( n = e; !n.isNull(); n = n.nextSibling().toElement() ) {
  395. if ( n.tagName() == "connections" ) {
  396. // setup signals and slots connections
  397. out << endl;
  398. nl = n.elementsByTagName( "connection" );
  399. for ( i = 0; i < (int) nl.length(); i++ ) {
  400. TQString sender, receiver, signal, slot;
  401. for ( TQDomElement n2 = nl.item(i).firstChild().toElement(); !n2.isNull(); n2 = n2.nextSibling().toElement() ) {
  402. if ( n2.tagName() == "sender" )
  403. sender = n2.firstChild().toText().data();
  404. else if ( n2.tagName() == "receiver" )
  405. receiver = n2.firstChild().toText().data();
  406. else if ( n2.tagName() == "signal" )
  407. signal = n2.firstChild().toText().data();
  408. else if ( n2.tagName() == "slot" )
  409. slot = n2.firstChild().toText().data();
  410. }
  411. if ( sender.isEmpty() || receiver.isEmpty() || signal.isEmpty() || slot.isEmpty() )
  412. continue;
  413. sender = registeredName( sender );
  414. receiver = registeredName( receiver );
  415. // translate formwindow name to "self"
  416. if ( sender == objName )
  417. sender = "self";
  418. else
  419. sender = "self." + sender;
  420. if ( receiver == objName )
  421. receiver = "self";
  422. else
  423. receiver = "self." + receiver;
  424. // Note that we never use the SLOT() macro in case we have
  425. // custom widgets implemented in Python.
  426. out << indent << "self.connect(" << sender
  427. << ",SIGNAL('" << signal << "'),self."
  428. << slot.left(slot.find('(')) << ")" << endl;
  429. }
  430. } else if ( n.tagName() == "tabstops" ) {
  431. // setup tab order
  432. out << endl;
  433. TQString lastName;
  434. TQDomElement n2 = n.firstChild().toElement();
  435. while ( !n2.isNull() ) {
  436. if ( n2.tagName() == "tabstop" ) {
  437. TQString name = n2.firstChild().toText().data();
  438. name = registeredName( name );
  439. if ( !lastName.isEmpty() )
  440. out << indent << "self.setTabOrder(self." << lastName << ",self." << name << ")" << endl;
  441. lastName = name;
  442. }
  443. n2 = n2.nextSibling().toElement();
  444. }
  445. }
  446. }
  447. // buddies
  448. bool firstBuddy = TRUE;
  449. for ( TQValueList<Buddy>::Iterator buddy = buddies.begin(); buddy != buddies.end(); ++buddy ) {
  450. if ( isObjectRegistered( (*buddy).buddy ) ) {
  451. if ( firstBuddy ) {
  452. out << endl;
  453. }
  454. out << indent << "self." << (*buddy).key << ".setBuddy(self." << registeredName( (*buddy).buddy ) << ")" << endl;
  455. firstBuddy = FALSE;
  456. }
  457. }
  458. // end of constructor
  459. popIndent();
  460. // handle application font changes if required
  461. nl = e.elementsByTagName( "widget" );
  462. bool needEventHandler = FALSE;
  463. for ( i = 0; i < (int) nl.length(); i++ ) {
  464. if ( DomTool::hasProperty( nl.item(i).toElement() , "font" ) ) {
  465. needEventHandler = TRUE;
  466. break;
  467. }
  468. }
  469. if ( needEventHandler ) {
  470. out << endl;
  471. out << indent << "def event(self,ev):" << endl;
  472. pushIndent();
  473. out << indent << "ret = " << objClass << ".event(self,ev)" << endl;
  474. out << endl;
  475. out << indent << "if ev.type() == TQEvent.ApplicationFontChange:" << endl;
  476. pushIndent();
  477. for ( i = 0; i < (int) nl.length(); i++ ) {
  478. n = nl.item(i).toElement();
  479. if ( DomTool::hasProperty( n, "font" ) )
  480. createExclusiveProperty( n, "font" );
  481. }
  482. out << endl;
  483. popIndent();
  484. out << indent << "return ret" << endl;
  485. popIndent();
  486. }
  487. // Generate user defined slot hooks.
  488. for (it = userSlots.begin(); it != userSlots.end(); ++it) {
  489. int astart = (*it).find('(');
  490. out << endl;
  491. out << indent << "def " << (*it).left(astart) << "(self";
  492. // We don't reproduce the argument names (if any) because we would have
  493. // to remove the types - too complicated for the moment, so we just
  494. // count them and give them names based on their position.
  495. TQString args = (*it).mid(astart + 1,(*it).find(')') - astart - 1).stripWhiteSpace();
  496. if (!args.isEmpty()) {
  497. int nrargs = args.contains(',') + 1;
  498. for (int i = 0; i < nrargs; ++i)
  499. out << ",a" << i;
  500. }
  501. out << "):" << endl;
  502. pushIndent();
  503. out << indent << "print '" << nameOfClass << "." << *it << ": not implemented yet'" << endl;
  504. popIndent();
  505. }
  506. popIndent();
  507. }
  508. /*!
  509. Creates an implementation for the object given in \a e.
  510. Traverses recursively over all children.
  511. Returns the name of the generated child object.
  512. \sa createObjectDecl()
  513. */
  514. TQString Uic::createObjectImpl( const TQDomElement &e, const TQString& parentClass, const TQString& parent, const TQString& layout )
  515. {
  516. TQDomElement n;
  517. TQString objClass, objName, fullObjName;
  518. if ( layouts.contains( e.tagName() ) )
  519. return createLayoutImpl( e, parentClass, parent, layout );
  520. objClass = getClassName( e );
  521. if ( objClass.isEmpty() )
  522. return objName;
  523. objName = getObjectName( e );
  524. TQString definedName = objName;
  525. bool isTmpObject = objName.isEmpty() || objClass == "TQLayoutWidget";
  526. if ( isTmpObject ) {
  527. if ( objClass[0] == 'Q' )
  528. objName = objClass.mid(1);
  529. else
  530. objName = objClass.lower();
  531. }
  532. bool isLine = objClass == "Line";
  533. if ( isLine )
  534. objClass = "TQFrame";
  535. out << endl;
  536. if ( objClass == "TQLayoutWidget" ) {
  537. if ( layout.isEmpty() ) {
  538. // register the object and unify its name
  539. objName = registerObject( objName );
  540. out << indent << objName << " = TQWidget(" << parent << ",'" << definedName << "')" << endl;
  541. } else {
  542. // the layout widget is not necessary, hide it by creating its child in the parent
  543. TQString result;
  544. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  545. if (tags.contains( n.tagName() ) )
  546. result = createObjectImpl( n, parentClass, parent, layout );
  547. }
  548. return result;
  549. }
  550. // Layouts don't go into the class instance dictionary.
  551. fullObjName = objName;
  552. } else {
  553. // register the object and unify its name
  554. objName = registerObject( objName );
  555. // Temporary objects don't go into the class instance dictionary.
  556. fullObjName = isTmpObject ? objName : "self." + objName;
  557. out << indent << fullObjName << " = " << createObjectInstance( objClass, parent, objName ) << endl;
  558. }
  559. lastItem = "None";
  560. // set the properties and insert items
  561. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  562. if ( n.tagName() == "property" ) {
  563. bool stdset = toBool( n.attribute( "stdset" ) );
  564. TQDomElement n2 = n.firstChild().toElement();
  565. if ( n2.tagName() == "name" ) {
  566. TQString prop = n2.firstChild().toText().data();
  567. TQString value = setObjectProperty( objClass, objName, prop, n2.nextSibling().toElement(), stdset );
  568. if ( value.isEmpty() )
  569. continue;
  570. if ( prop == "name" )
  571. continue;
  572. if ( prop == "buddy" && value[0] == '\'' && value[(int)value.length()-1] == '\'' ) {
  573. buddies << Buddy( objName, value.mid(1, value.length() - 2 ) );
  574. continue;
  575. }
  576. if ( isLine && prop == "orientation" ) {
  577. prop = "frameStyle";
  578. if ( value.right(10) == "Horizontal" )
  579. value = "TQFrame.HLine | TQFrame.Sunken";
  580. else
  581. value = "TQFrame.VLine | TQFrame.Sunken";
  582. }
  583. if ( prop == "buttonGroupId" ) {
  584. if ( parentClass == "TQButtonGroup" )
  585. out << indent << parent << ".insert(" << fullObjName << "," << value << ")" << endl;
  586. continue;
  587. }
  588. if ( prop == "geometry") {
  589. out << indent << fullObjName << ".setGeometry(" << value << ")" << endl;
  590. } else {
  591. if ( stdset )
  592. out << indent << fullObjName << "." << mkStdSet(prop) << "(" << value << ")" << endl;
  593. else
  594. out << indent << fullObjName << ".setProperty('" << prop << "',TQVariant(" << value << "))" << endl;
  595. }
  596. }
  597. } else if ( n.tagName() == "item" ) {
  598. if ( objClass.mid(1) == "ListBox" ) {
  599. TQString s = createListBoxItemImpl( n, fullObjName );
  600. if ( !s.isEmpty() )
  601. out << indent << s << endl;
  602. } else if ( objClass.mid(1) == "ComboBox" ) {
  603. TQString s = createListBoxItemImpl( n, fullObjName );
  604. if ( !s.isEmpty() )
  605. out << indent << s << endl;
  606. } else if ( objClass.mid(1) == "IconView" ) {
  607. TQString s = createIconViewItemImpl( n, fullObjName );
  608. if ( !s.isEmpty() )
  609. out << indent << s << endl;
  610. } else if ( objClass.mid(1) == "ListView" ) {
  611. TQString s = createListViewItemImpl( n, fullObjName, TQString::null );
  612. if ( !s.isEmpty() )
  613. out << s << endl;
  614. }
  615. } else if ( n.tagName() == "column" ) {
  616. if ( objClass.mid(1) == "ListView" ) {
  617. TQString s = createListViewColumnImpl( n, fullObjName );
  618. if ( !s.isEmpty() )
  619. out << s;
  620. }
  621. }
  622. }
  623. // create all children, some widgets have special requirements
  624. if ( objClass == "TQTabWidget" ) {
  625. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  626. if ( tags.contains( n.tagName() ) ) {
  627. TQString page = createObjectImpl( n, objClass, fullObjName );
  628. TQString label = DomTool::readAttribute( n, "title", "" ).toString();
  629. out << indent << fullObjName << ".insertTab(" << page << "," << trmacro << "(" << fixString( label ) << "))" << endl;
  630. }
  631. }
  632. } else { // standard widgets
  633. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  634. if ( tags.contains( n.tagName() ) )
  635. createObjectImpl( n, objClass, fullObjName );
  636. }
  637. }
  638. return fullObjName;
  639. }
  640. /*!
  641. Creates implementation of an listbox item tag.
  642. */
  643. TQString Uic::createListBoxItemImpl( const TQDomElement &e, const TQString &parent )
  644. {
  645. TQDomElement n = e.firstChild().toElement();
  646. TQString txt;
  647. TQString pix;
  648. while ( !n.isNull() ) {
  649. if ( n.tagName() == "property" ) {
  650. TQDomElement n2 = n.firstChild().toElement();
  651. if ( n2.tagName() == "name" ) {
  652. TQString attrib = n2.firstChild().toText().data();
  653. TQVariant v = DomTool::elementToVariant( n2.nextSibling().toElement(), TQVariant() );
  654. if ( attrib == "text" )
  655. txt = v.toString();
  656. else if ( attrib == "pixmap" ) {
  657. pix = v.toString();
  658. if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
  659. pix.prepend( pixmapLoaderFunction + "(" );
  660. pix.append( ")" );
  661. }
  662. }
  663. }
  664. }
  665. n = n.nextSibling().toElement();
  666. }
  667. return pix.isEmpty() ?
  668. parent + ".insertItem(" + trmacro + "(" + fixString( txt ) + "))":
  669. parent + ".insertItem(" + pix + "," + trmacro + "(" + fixString( txt ) + "))";
  670. }
  671. /*!
  672. Creates implementation of an iconview item tag.
  673. */
  674. TQString Uic::createIconViewItemImpl( const TQDomElement &e, const TQString &parent )
  675. {
  676. TQDomElement n = e.firstChild().toElement();
  677. TQString txt;
  678. TQString pix;
  679. while ( !n.isNull() ) {
  680. if ( n.tagName() == "property" ) {
  681. TQDomElement n2 = n.firstChild().toElement();
  682. if ( n2.tagName() == "name" ) {
  683. TQString attrib = n2.firstChild().toText().data();
  684. TQVariant v = DomTool::elementToVariant( n2.nextSibling().toElement(), TQVariant() );
  685. if ( attrib == "text" )
  686. txt = v.toString();
  687. else if ( attrib == "pixmap" ) {
  688. pix = v.toString();
  689. if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
  690. pix.prepend( pixmapLoaderFunction + "( " );
  691. pix.append( " )" );
  692. }
  693. }
  694. }
  695. }
  696. n = n.nextSibling().toElement();
  697. }
  698. if ( pix.isEmpty() )
  699. return "TQIconViewItem(" + parent + "," + trmacro + "(" + fixString( txt ) + "))";
  700. return "TQIconViewItem(" + parent + "," + trmacro + "(" + fixString( txt ) + ")," + pix + ")";
  701. }
  702. /*!
  703. Creates implementation of an listview item tag.
  704. */
  705. TQString Uic::createListViewItemImpl( const TQDomElement &e, const TQString &parent,
  706. const TQString &parentItem )
  707. {
  708. TQString s;
  709. TQDomElement n = e.firstChild().toElement();
  710. bool hasChildren = e.elementsByTagName( "item" ).count() > 0;
  711. TQString item;
  712. if ( hasChildren ) {
  713. item = registerObject( "item" );
  714. s = indent + item + " = ";
  715. } else {
  716. item = "item";
  717. if ( item_used )
  718. s = indent + item + " = ";
  719. else
  720. s = indent + item + " = ";
  721. item_used = TRUE;
  722. }
  723. if ( !parentItem.isEmpty() )
  724. s += "TQListViewItem(" + parentItem + "," + lastItem + ")\n";
  725. else
  726. s += "TQListViewItem(" + parent + "," + lastItem + ")\n";
  727. TQStringList textes;
  728. TQStringList pixmaps;
  729. while ( !n.isNull() ) {
  730. if ( n.tagName() == "property" ) {
  731. TQDomElement n2 = n.firstChild().toElement();
  732. if ( n2.tagName() == "name" ) {
  733. TQString attrib = n2.firstChild().toText().data();
  734. TQVariant v = DomTool::elementToVariant( n2.nextSibling().toElement(), TQVariant() );
  735. if ( attrib == "text" )
  736. textes << v.toString();
  737. else if ( attrib == "pixmap" ) {
  738. TQString pix = v.toString();
  739. if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
  740. pix.prepend( pixmapLoaderFunction + "( " );
  741. pix.append( " )" );
  742. }
  743. pixmaps << pix;
  744. }
  745. }
  746. } else if ( n.tagName() == "item" ) {
  747. s += indent + item + ".setOpen(1)\n";
  748. s += createListViewItemImpl( n, parent, item );
  749. }
  750. n = n.nextSibling().toElement();
  751. }
  752. for ( int i = 0; i < (int)textes.count(); ++i ) {
  753. if ( !textes[ i ].isEmpty() )
  754. s += indent + item + ".setText(" + TQString::number( i ) + "," + trmacro + "(" + fixString( textes[ i ] ) + "))\n";
  755. if ( !pixmaps[ i ].isEmpty() )
  756. s += indent + item + ".setPixmap(" + TQString::number( i ) + "," + pixmaps[ i ] + ")\n";
  757. }
  758. lastItem = item;
  759. return s;
  760. }
  761. /*!
  762. Creates implementation of an listview column tag.
  763. */
  764. TQString Uic::createListViewColumnImpl( const TQDomElement &e, const TQString &parent )
  765. {
  766. TQDomElement n = e.firstChild().toElement();
  767. TQString txt;
  768. TQString pix;
  769. bool clickable = FALSE, resizeable = FALSE;
  770. while ( !n.isNull() ) {
  771. if ( n.tagName() == "property" ) {
  772. TQDomElement n2 = n.firstChild().toElement();
  773. if ( n2.tagName() == "name" ) {
  774. TQString attrib = n2.firstChild().toText().data();
  775. TQVariant v = DomTool::elementToVariant( n2.nextSibling().toElement(), TQVariant() );
  776. if ( attrib == "text" )
  777. txt = v.toString();
  778. else if ( attrib == "pixmap" ) {
  779. pix = v.toString();
  780. if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
  781. pix.prepend( pixmapLoaderFunction + "( " );
  782. pix.append( " )" );
  783. }
  784. } else if ( attrib == "clickable" )
  785. clickable = v.toBool();
  786. else if ( attrib == "resizeable" )
  787. resizeable = v.toBool();
  788. }
  789. }
  790. n = n.nextSibling().toElement();
  791. }
  792. TQString s;
  793. s = indent + parent + ".addColumn(" + trmacro + "(" + fixString( txt ) + "))\n";
  794. if ( !pix.isEmpty() )
  795. s += indent + parent + ".header().setLabel(" + parent + ".header().count() - 1," + pix + "," + trmacro + "(" + fixString( txt ) + "))\n";
  796. if ( !clickable )
  797. s += indent + parent + ".header().setClickEnabled(0," + parent + ".header().count() - 1)\n";
  798. if ( !resizeable )
  799. s += indent + parent + ".header().setResizeEnabled(0," + parent + ".header().count() - 1)\n";
  800. return s;
  801. }
  802. /*!
  803. Creates the implementation of a layout tag. Called from createObjectImpl().
  804. */
  805. TQString Uic::createLayoutImpl( const TQDomElement &e, const TQString& parentClass, const TQString& parent, const TQString& layout )
  806. {
  807. TQDomElement n;
  808. TQString objClass, objName;
  809. objClass = e.tagName();
  810. TQString qlayout = "TQVBoxLayout";
  811. if ( objClass == "hbox" )
  812. qlayout = "TQHBoxLayout";
  813. else if ( objClass == "grid" )
  814. qlayout = "TQGridLayout";
  815. bool isGrid = e.tagName() == "grid" ;
  816. objName = registerObject( getLayoutName( e ) );
  817. layoutObjects += objName;
  818. int margin = DomTool::readProperty( e, "margin", BOXLAYOUT_DEFAULT_MARGIN ).toInt();
  819. int spacing = DomTool::readProperty( e, "spacing", BOXLAYOUT_DEFAULT_SPACING ).toInt();
  820. if ( (parentClass == "TQGroupBox" || parentClass == "TQButtonGroup") && layout.isEmpty() ) {
  821. // special case for group box
  822. out << indent << parent << ".setColumnLayout(0,TQt.Vertical)" << endl;
  823. out << indent << parent << ".layout().setSpacing(0)" << endl;
  824. out << indent << parent << ".layout().setMargin(0)" << endl;
  825. out << indent << objName << " = " << qlayout << "(" << parent << ".layout())" << endl;
  826. out << indent << objName << ".setAlignment(TQt.AlignTop)" << endl;
  827. } else {
  828. if ( layout.isEmpty() )
  829. out << indent << objName << " = " << qlayout << "(" << parent << ")" << endl;
  830. else
  831. out << indent << objName << " = " << qlayout << "()" << endl;
  832. }
  833. out << indent << objName << ".setSpacing(" << spacing << ")" << endl;
  834. out << indent << objName << ".setMargin(" << margin << ")" << endl;
  835. if ( !isGrid ) {
  836. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  837. if ( n.tagName() == "spacer" ) {
  838. TQString child = createSpacerImpl( n, parentClass, parent, objName );
  839. out << indent << objName << ".addItem(" << child << ")" << endl;
  840. } else if ( tags.contains( n.tagName() ) ) {
  841. TQString child = createObjectImpl( n, parentClass, parent, objName );
  842. if ( isLayout( child ) )
  843. out << indent << objName << ".addLayout(" << child << ")" << endl;
  844. else
  845. out << indent << objName << ".addWidget(" << child << ")" << endl;
  846. }
  847. }
  848. } else {
  849. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  850. TQDomElement ae = n;
  851. int row = ae.attribute( "row" ).toInt();
  852. int col = ae.attribute( "column" ).toInt();
  853. int rowspan = ae.attribute( "rowspan" ).toInt();
  854. int colspan = ae.attribute( "colspan" ).toInt();
  855. if ( rowspan < 1 )
  856. rowspan = 1;
  857. if ( colspan < 1 )
  858. colspan = 1;
  859. if ( n.tagName() == "spacer" ) {
  860. TQString child = createSpacerImpl( n, parentClass, parent, objName );
  861. if ( rowspan * colspan != 1 )
  862. out << indent << objName << ".addMultiCell(" << child << ","
  863. << row << "," << row + rowspan - 1 << "," << col << ","
  864. << col + colspan - 1 << ")" << endl;
  865. else
  866. out << indent << objName << ".addItem(" << child << ","
  867. << row << "," << col << ")" << endl;
  868. } else if ( tags.contains( n.tagName() ) ) {
  869. TQString child = createObjectImpl( n, parentClass, parent, objName );
  870. out << endl;
  871. TQString o = "Widget";
  872. if ( isLayout( child ) )
  873. o = "Layout";
  874. if ( rowspan * colspan != 1 )
  875. out << indent << objName << ".addMultiCell" << o << "(" << child << ","
  876. << row << "," << row + rowspan - 1 << "," << col << "," << col + colspan - 1 << ")" << endl;
  877. else
  878. out << indent << objName << ".add" << o << "(" << child << ","
  879. << row << "," << col << ")" << endl;
  880. }
  881. }
  882. }
  883. return objName;
  884. }
  885. TQString Uic::createSpacerImpl( const TQDomElement &e, const TQString& /*parentClass*/, const TQString& /*parent*/, const TQString& /*layout*/)
  886. {
  887. TQDomElement n;
  888. TQString objClass, objName;
  889. objClass = e.tagName();
  890. objName = registerObject( "spacer" );
  891. TQSize size = DomTool::readProperty( e, "sizeHint", TQSize(0,0) ).toSize();
  892. TQString sizeType = DomTool::readProperty( e, "sizeType", "Expanding" ).toString();
  893. bool isVspacer = DomTool::readProperty( e, "orientation", "Horizontal" ) == "Vertical";
  894. if ( sizeType != "Expanding" && sizeType != "MinimumExpanding" &&
  895. DomTool::hasProperty( e, "geometry" ) ) { // compatibility TQt 2.2
  896. TQRect geom = DomTool::readProperty( e, "geometry", TQRect(0,0,0,0) ).toRect();
  897. size = geom.size();
  898. }
  899. if ( isVspacer )
  900. out << indent << objName << " = TQSpacerItem("
  901. << size.width() << "," << size.height()
  902. << ",TQSizePolicy.Minimum,TQSizePolicy." << sizeType << ")" << endl;
  903. else
  904. out << indent << objName << " = TQSpacerItem("
  905. << size.width() << "," << size.height()
  906. << ",TQSizePolicy." << sizeType << ",TQSizePolicy.Minimum)" << endl;
  907. return objName;
  908. }
  909. /*!
  910. Creates a set-call for property \a exclusiveProp of the object
  911. given in \a e.
  912. If the object does not have this property, the function does nothing.
  913. Exclusive properties are used to generate the implementation of
  914. application font or palette change handlers in createFormImpl().
  915. */
  916. void Uic::createExclusiveProperty( const TQDomElement & e, const TQString& exclusiveProp )
  917. {
  918. TQDomElement n;
  919. TQString objClass = getClassName( e );
  920. if ( objClass.isEmpty() )
  921. return;
  922. TQString objName = getObjectName( e );
  923. if ( objClass.isEmpty() )
  924. return;
  925. for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
  926. if ( n.tagName() == "property" ) {
  927. bool stdset = toBool( n.attribute( "stdset" ) );
  928. TQDomElement n2 = n.firstChild().toElement();
  929. if ( n2.tagName() == "name" ) {
  930. TQString prop = n2.firstChild().toText().data();
  931. if ( prop != exclusiveProp )
  932. continue;
  933. TQString value = setObjectProperty( objClass, objName, prop, n2.nextSibling().toElement(), stdset );
  934. if ( value.isEmpty() )
  935. continue;
  936. out << indent << objName << ".setProperty('" << prop << "',TQVariant(" << value << "))" << endl;
  937. }
  938. }
  939. }
  940. }
  941. const char* const ColorRole[] = {
  942. "Foreground", "Button", "Light", "Midlight", "Dark", "Mid",
  943. "Text", "BrightText", "ButtonText", "Base", "Background", "Shadow",
  944. "Highlight", "HighlightedText", 0
  945. };
  946. /*!
  947. Attention: this function has to be in sync with Resource::setWidgetProperty(). If you change one, change both.
  948. */
  949. TQString Uic::setObjectProperty( const TQString& objClass, const TQString& obj, const TQString &prop, const TQDomElement &e, bool stdset )
  950. {
  951. TQString v;
  952. if ( e.tagName() == "rect" ) {
  953. TQDomElement n3 = e.firstChild().toElement();
  954. int x = 0, y = 0, w = 0, h = 0;
  955. while ( !n3.isNull() ) {
  956. if ( n3.tagName() == "x" )
  957. x = n3.firstChild().toText().data().toInt();
  958. else if ( n3.tagName() == "y" )
  959. y = n3.firstChild().toText().data().toInt();
  960. else if ( n3.tagName() == "width" )
  961. w = n3.firstChild().toText().data().toInt();
  962. else if ( n3.tagName() == "height" )
  963. h = n3.firstChild().toText().data().toInt();
  964. n3 = n3.nextSibling().toElement();
  965. }
  966. v = "TQRect(%1,%2,%3,%4)";
  967. v = v.arg(x).arg(y).arg(w).arg(h);
  968. } else if ( e.tagName() == "point" ) {
  969. TQDomElement n3 = e.firstChild().toElement();
  970. int x = 0, y = 0;
  971. while ( !n3.isNull() ) {
  972. if ( n3.tagName() == "x" )
  973. x = n3.firstChild().toText().data().toInt();
  974. else if ( n3.tagName() == "y" )
  975. y = n3.firstChild().toText().data().toInt();
  976. n3 = n3.nextSibling().toElement();
  977. }
  978. v = "TQPoint(%1,%2)";
  979. v = v.arg(x).arg(y);
  980. } else if ( e.tagName() == "size" ) {
  981. TQDomElement n3 = e.firstChild().toElement();
  982. int w = 0, h = 0;
  983. while ( !n3.isNull() ) {
  984. if ( n3.tagName() == "width" )
  985. w = n3.firstChild().toText().data().toInt();
  986. else if ( n3.tagName() == "height" )
  987. h = n3.firstChild().toText().data().toInt();
  988. n3 = n3.nextSibling().toElement();
  989. }
  990. v = "TQSize(%1,%2)";
  991. v = v.arg(w).arg(h);
  992. } else if ( e.tagName() == "color" ) {
  993. TQDomElement n3 = e.firstChild().toElement();
  994. int r= 0, g = 0, b = 0;
  995. while ( !n3.isNull() ) {
  996. if ( n3.tagName() == "red" )
  997. r = n3.firstChild().toText().data().toInt();
  998. else if ( n3.tagName() == "green" )
  999. g = n3.firstChild().toText().data().toInt();
  1000. else if ( n3.tagName() == "blue" )
  1001. b = n3.firstChild().toText().data().toInt();
  1002. n3 = n3.nextSibling().toElement();
  1003. }
  1004. v = "TQColor(%1,%2,%3)";
  1005. v = v.arg(r).arg(g).arg(b);
  1006. } else if ( e.tagName() == "font" ) {
  1007. TQDomElement n3 = e.firstChild().toElement();
  1008. TQString fontname = "f";
  1009. if ( !obj.isEmpty() ) {
  1010. fontname = obj + "_font";
  1011. out << indent << fontname << " = TQFont(self." << obj << ".font())" << endl;
  1012. } else {
  1013. out << indent << fontname << " = TQFont(self.font())" << endl;
  1014. }
  1015. while ( !n3.isNull() ) {
  1016. if ( n3.tagName() == "family" )
  1017. out << indent << fontname << ".setFamily('" << n3.firstChild().toText().data() << "')" << endl;
  1018. else if ( n3.tagName() == "pointsize" )
  1019. out << indent << fontname << ".setPointSize(" << n3.firstChild().toText().data() << ")" << endl;
  1020. else if ( n3.tagName() == "bold" )
  1021. out << indent << fontname << ".setBold(" << mkBool( n3.firstChild().toText().data() ) << ")" << endl;
  1022. else if ( n3.tagName() == "italic" )
  1023. out << indent << fontname << ".setItalic(" << mkBool( n3.firstChild().toText().data() ) << ")" << endl;
  1024. else if ( n3.tagName() == "underline" )
  1025. out << indent << fontname << ".setUnderline(" << mkBool( n3.firstChild().toText().data() ) << ")" << endl;
  1026. else if ( n3.tagName() == "strikeout" )
  1027. out << indent << fontname << ".setStrikeOut(" << mkBool( n3.firstChild().toText().data() ) << ")" << endl;
  1028. n3 = n3.nextSibling().toElement();
  1029. }
  1030. if ( prop == "font" ) {
  1031. if ( !obj.isEmpty() )
  1032. out << indent << "self." << obj << ".setFont(" << fontname << ")" << endl;
  1033. else
  1034. out << indent << "self.setFont(" << fontname << ")" << endl;
  1035. } else {
  1036. v = fontname;
  1037. }
  1038. } else if ( e.tagName() == "string" ) {
  1039. if ( prop == "toolTip" ) {
  1040. if ( !obj.isEmpty() )
  1041. out << indent << "TQToolTip.add(self." << obj << "," + trmacro + "(" << fixString( e.firstChild().toText().data() ) << "))" << endl;
  1042. else
  1043. out << indent << "TQToolTip.add(self," + trmacro + "(" << fixString( e.firstChild().toText().data() ) << "))" << endl;
  1044. } else if ( prop == "whatsThis" ) {
  1045. if ( !obj.isEmpty() )
  1046. out << indent << "TQWhatsThis.add(self." << obj << "," << trmacro << "(" << fixString( e.firstChild().toText().data() ) << "))" << endl;
  1047. else
  1048. out << indent << "TQWhatsThis.add(self," << trmacro << "(" << fixString( e.firstChild().toText().data() ) << "))" << endl;
  1049. } else {
  1050. v = trmacro + "(%1)";
  1051. v = v.arg( fixString( e.firstChild().toText().data() ) );
  1052. }
  1053. } else if ( e.tagName() == "cstring" ) {
  1054. v = "'%1'";
  1055. v = v.arg( e.firstChild().toText().data() );
  1056. } else if ( e.tagName() == "number" ) {
  1057. v = "%1";
  1058. v = v.arg( e.firstChild().toText().data() );
  1059. } else if ( e.tagName() == "bool" ) {
  1060. if ( stdset )
  1061. v = "%1";
  1062. else
  1063. v = "%1,0";
  1064. v = v.arg( mkBool( e.firstChild().toText().data() ) );
  1065. } else if ( e.tagName() == "pixmap" ) {
  1066. v = e.firstChild().toText().data();
  1067. if ( !pixmapLoaderFunction.isEmpty() ) {
  1068. v.prepend( pixmapLoaderFunction + "( " );
  1069. v.append( " )" );
  1070. }
  1071. } else if ( e.tagName() == "iconset" ) {
  1072. v = "TQIconSet(%1)";
  1073. v = v.arg( e.firstChild().toText().data() );
  1074. } else if ( e.tagName() == "image" ) {
  1075. v = e.firstChild().toText().data() + ".convertToImage()";
  1076. } else if ( e.tagName() == "enum" ) {
  1077. v = "%1.%2";
  1078. TQString oc = objClass;
  1079. TQString ev = e.firstChild().toText().data();
  1080. if ( oc == "TQListView" && ev == "Manual" ) // #### workaround, rename TQListView::Manual of WithMode enum in 3.0
  1081. oc = "TQScrollView";
  1082. v = v.arg( oc ).arg( ev );
  1083. } else if ( e.tagName() == "set" ) {
  1084. TQString keys( e.firstChild().toText().data() );
  1085. TQStringList lst = TQStringList::split( '|', keys );
  1086. v = "";
  1087. for ( TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) {
  1088. v += objClass + "." + *it;
  1089. if ( it != lst.fromLast() )
  1090. v += " | ";
  1091. }
  1092. } else if ( e.tagName() == "sizepolicy" ) {
  1093. TQDomElement n3 = e.firstChild().toElement();
  1094. TQSizePolicy sp;
  1095. while ( !n3.isNull() ) {
  1096. if ( n3.tagName() == "hsizetype" )
  1097. sp.setHorData( (TQSizePolicy::SizeType)n3.firstChild().toText().data().toInt() );
  1098. else if ( n3.tagName() == "vsizetype" )
  1099. sp.setVerData( (TQSizePolicy::SizeType)n3.firstChild().toText().data().toInt() );
  1100. n3 = n3.nextSibling().toElement();
  1101. }
  1102. TQString tmp;
  1103. if ( !obj.isEmpty() )
  1104. tmp = "self." + obj;
  1105. else
  1106. tmp = "self";
  1107. v = "TQSizePolicy(%1,%2," + tmp + ".sizePolicy().hasHeightForWidth())";
  1108. v = v.arg( (int)sp.horData() ).arg( (int)sp.verData() );
  1109. } else if ( e.tagName() == "palette" ) {
  1110. TQPalette pal;
  1111. bool no_pixmaps = e.elementsByTagName( "pixmap" ).count() == 0;
  1112. TQDomElement n;
  1113. if ( no_pixmaps ) {
  1114. n = e.firstChild().toElement();
  1115. while ( !n.isNull() ) {
  1116. TQColorGroup cg;
  1117. if ( n.tagName() == "active" ) {
  1118. cg = loadColorGroup( n );
  1119. pal.setActive( cg );
  1120. } else if ( n.tagName() == "inactive" ) {
  1121. cg = loadColorGroup( n );
  1122. pal.setInactive( cg );
  1123. } else if ( n.tagName() == "disabled" ) {
  1124. cg = loadColorGroup( n );
  1125. pal.setDisabled( cg );
  1126. }
  1127. n = n.nextSibling().toElement();
  1128. }
  1129. }
  1130. if ( no_pixmaps && pal == TQPalette( pal.active().button(), pal.active().background() ) ) {
  1131. v = "TQPalette(TQColor(%1,%2,%3),TQColor(%1,%2,%3))";
  1132. v = v.arg( pal.active().button().red() ).arg( pal.active().button().green() ).arg( pal.active().button().blue() );
  1133. v = v.arg( pal.active().background().red() ).arg( pal.active().background().green() ).arg( pal.active().background().blue() );
  1134. } else {
  1135. TQString palette = "pal";
  1136. if ( !pal_used ) {
  1137. out << indent << palette << " = TQPalette()" << endl;
  1138. pal_used = TRUE;
  1139. }
  1140. TQString cg = "cg";
  1141. if ( !cg_used ) {
  1142. out << indent << cg << " = TQColorGroup()" << endl;
  1143. cg_used = TRUE;
  1144. }
  1145. n = e.firstChild().toElement();
  1146. while ( !n.isNull() && n.tagName() != "active")
  1147. n = n.nextSibling().toElement();
  1148. createColorGroupImpl( cg, n );
  1149. out << indent << palette << ".setActive(" << cg << ")" << endl;
  1150. n = e.firstChild().toElement();
  1151. while ( !n.isNull() && n.tagName() != "inactive")
  1152. n = n.nextSibling().toElement();
  1153. createColorGroupImpl( cg, n );
  1154. out << indent << palette << ".setInactive(" << cg << ")" << endl;
  1155. n = e.firstChild().toElement();
  1156. while ( !n.isNull() && n.tagName() != "disabled")
  1157. n = n.nextSibling().toElement();
  1158. createColorGroupImpl( cg, n );
  1159. out << indent << palette << ".setDisabled(" << cg << ")" << endl;
  1160. v = palette;
  1161. }
  1162. } else if ( e.tagName() == "cursor" ) {
  1163. v = "TQCursor(%1)";
  1164. v = v.arg( e.firstChild().toText().data() );
  1165. }
  1166. return v;
  1167. }
  1168. /*!
  1169. Creates a colorgroup with name \a name from the color group \a cg
  1170. */
  1171. void Uic::createColorGroupImpl( const TQString& name, const TQDomElement& e )
  1172. {
  1173. TQColorGroup cg;
  1174. int r = -1;
  1175. TQDomElement n = e.firstChild().toElement();
  1176. TQString color;
  1177. while ( !n.isNull() ) {
  1178. if ( n.tagName() == "color" ) {
  1179. r++;
  1180. TQColor col = DomTool::readColor( n );
  1181. color = "TQColor(%1,%2,%3)";
  1182. color = color.arg( col.red() ).arg( col.green() ).arg( col.blue() );
  1183. if ( col == white )
  1184. color = "TQt.white";
  1185. else if ( col == black )
  1186. color = "TQt.black";
  1187. if ( n.nextSibling().toElement().tagName() != "pixmap" ) {
  1188. out << indent << name << ".setColor(TQColorGroup." << ColorRole[r] << "," << color << ")" << endl;
  1189. }
  1190. } else if ( n.tagName() == "pixmap" ) {
  1191. TQString pixmap = n.firstChild().toText().data();
  1192. if ( !pixmapLoaderFunction.isEmpty() ) {
  1193. pixmap.prepend( pixmapLoaderFunction + "(" );
  1194. pixmap.append( ")" );
  1195. }
  1196. out << indent << name << ".setBrush(TQColorGroup."
  1197. << ColorRole[r] << ",TQBrush(" << color << "," << pixmap << "))" << endl;
  1198. }
  1199. n = n.nextSibling().toElement();
  1200. }
  1201. }
  1202. /*!
  1203. Auxiliary function to load a color group. The colorgroup must not
  1204. contain pixmaps.
  1205. */
  1206. TQColorGroup Uic::loadColorGroup( const TQDomElement &e )
  1207. {
  1208. TQColorGroup cg;
  1209. int r = -1;
  1210. TQDomElement n = e.firstChild().toElement();
  1211. TQColor col;
  1212. while ( !n.isNull() ) {
  1213. if ( n.tagName() == "color" ) {
  1214. r++;
  1215. cg.setColor( (TQColorGroup::ColorRole)r, (col = DomTool::readColor( n ) ) );
  1216. }
  1217. n = n.nextSibling().toElement();
  1218. }
  1219. return cg;
  1220. }
  1221. /*!
  1222. Registers an object with name \a name.
  1223. The returned name is a valid variable identifier, as similar to \a
  1224. name as possible and guaranteed to be unique within the form.
  1225. \sa registeredName(), isObjectRegistered()
  1226. */
  1227. TQString Uic::registerObject( const TQString& name )
  1228. {
  1229. if ( objectNames.isEmpty() ) {
  1230. // some temporary variables we need
  1231. objectNames += "img";
  1232. objectNames += "item";
  1233. objectNames += "cg";
  1234. objectNames += "pal";
  1235. }
  1236. TQString result = name;
  1237. int i;
  1238. while ( ( i = result.find(' ' )) != -1 ) {
  1239. result[i] = '_';
  1240. }
  1241. if ( objectNames.contains( result ) ) {
  1242. int i = 2;
  1243. while ( objectNames.contains( result + "_" + TQString::number(i) ) )
  1244. i++;
  1245. result += "_";
  1246. result += TQString::number(i);
  1247. }
  1248. objectNames += result;
  1249. objectMapper.insert( name, result );
  1250. return result;
  1251. }
  1252. /*!
  1253. Returns the registered name for the original name \a name
  1254. or \a name if \a name wasn't registered.
  1255. \sa registerObject(), isObjectRegistered()
  1256. */
  1257. TQString Uic::registeredName( const TQString& name )
  1258. {
  1259. if ( !objectMapper.contains( name ) )
  1260. return name;
  1261. return objectMapper[name];
  1262. }
  1263. /*!
  1264. Returns whether the object \a name was registered yet or not.
  1265. */
  1266. bool Uic::isObjectRegistered( const TQString& name )
  1267. {
  1268. return objectMapper.contains( name );
  1269. }
  1270. /*!
  1271. Unifies the entries in stringlist \a list. Should really be a TQStringList feature.
  1272. */
  1273. TQStringList Uic::unique( const TQStringList& list )
  1274. {
  1275. TQStringList result;
  1276. if (list.isEmpty() )
  1277. return result;
  1278. TQStringList l = list;
  1279. l.sort();
  1280. result += l.first();
  1281. for ( TQStringList::Iterator it = l.begin(); it != l.end(); ++it ) {
  1282. if ( *it != result.last() )
  1283. result += *it;
  1284. }
  1285. return result;
  1286. }
  1287. /*!
  1288. Creates an instance of class \a objClass, with parent \a parent and name \a objName
  1289. */
  1290. TQString Uic::createObjectInstance( const TQString& objClass, const TQString& parent, const TQString& objName )
  1291. {
  1292. if ( objClass.mid(1) == "ComboBox" ) {
  1293. return objClass + "(0," + parent + ",'" + objName + "')";
  1294. }
  1295. return objClass + "(" + parent + ",'" + objName + "')";
  1296. }
  1297. bool Uic::isLayout( const TQString& name ) const
  1298. {
  1299. return layoutObjects.contains( name );
  1300. }
  1301. #if defined(BLACKADDER)
  1302. #include "ba.h"
  1303. int BA::compilePython(const TQString &fileName,const TQString &outputFile,
  1304. const TQString &trmacro,bool execCode)
  1305. {
  1306. #else
  1307. int main( int argc, char * argv[] )
  1308. {
  1309. const char *error = 0;
  1310. TQString fileName;
  1311. TQString outputFile;
  1312. TQString trmacro;
  1313. bool execCode = FALSE;
  1314. for ( int n = 1; n < argc && error == 0; n++ ) {
  1315. TQCString arg = argv[n];
  1316. if ( arg[0] == '-' ) { // option
  1317. TQCString opt = &arg[1];
  1318. if ( opt[0] == 'o' ) { // output redirection
  1319. if ( opt[1] == '\0' ) {
  1320. if ( !(n < argc-1) ) {
  1321. error = "Missing output-file name";
  1322. break;
  1323. }
  1324. outputFile = argv[++n];
  1325. } else
  1326. outputFile = &opt[1];
  1327. } else if ( opt == "tr" ) {
  1328. if ( opt == "tr" || opt[1] == '\0' ) {
  1329. if ( !(n < argc-1) ) {
  1330. error = "Missing tr macro.";
  1331. break;
  1332. }
  1333. trmacro = argv[++n];
  1334. } else {
  1335. trmacro = &opt[1];
  1336. }
  1337. } else if ( opt == "x" ) {
  1338. execCode = TRUE;
  1339. }
  1340. } else {
  1341. if (!fileName.isEmpty()) // can handle only one file
  1342. error = "Too many input files specified";
  1343. else
  1344. fileName = argv[n];
  1345. }
  1346. }
  1347. if ( argc < 2 || error || fileName.isEmpty() ) {
  1348. fprintf( stderr, "PyTQt user interface compiler\n" );
  1349. if ( error )
  1350. fprintf( stderr, "pyuic: %s\n", error );
  1351. fprintf( stderr, "Usage: %s [options] <uifile>\n"
  1352. "\nGenerate PyTQt implementation:\n"
  1353. " %s [options] <uifile>\n"
  1354. "Options:\n"
  1355. "\t-o file Write output to file rather than stdout\n"
  1356. "\t-tr func Use func(...) rather than tr(...) for i18n\n"
  1357. "\t-x Generate extra code to test the class\n"
  1358. , argv[0], argv[0]);
  1359. exit( 1 );
  1360. }
  1361. #endif
  1362. TQFile file( fileName );
  1363. if ( !file.open( IO_ReadOnly ) )
  1364. qFatal( "pyuic: Could not open file '%s' ", (const char *)fileName );
  1365. TQFile fileOut;
  1366. if (!outputFile.isEmpty()) {
  1367. fileOut.setName( outputFile );
  1368. if (!fileOut.open( IO_WriteOnly ) )
  1369. qFatal( "pyuic: Could not open output file '%s'", (const char *)outputFile );
  1370. } else {
  1371. fileOut.open( IO_WriteOnly, stdout );
  1372. }
  1373. TQTextStream out( &fileOut );
  1374. out.setEncoding( TQTextStream::UnicodeUTF8 );
  1375. TQDomDocument doc;
  1376. if ( !doc.setContent( &file ) )
  1377. qFatal( "pyuic: Failed to parse %s\n", (const char *)fileName );
  1378. out << "# Form implementation generated from reading ui file '" << fileName << "'" << endl;
  1379. out << "#" << endl;
  1380. out << "# Created: " << TQDateTime::currentDateTime().toString() << endl;
  1381. out << "# by: The Python User Interface Compiler (pyuic) " << PYQT_VERSION << endl;
  1382. out << "#" << endl;
  1383. out << "# WARNING! All changes made in this file will be lost!" << endl;
  1384. out << endl;
  1385. out << endl;
  1386. if (execCode)
  1387. out << "import sys" << endl;
  1388. out << "from qt import *" << endl;
  1389. Uic( out, doc, trmacro.isEmpty() ? TQString("self.tr") : trmacro );
  1390. if (execCode) {
  1391. out << endl;
  1392. out << endl;
  1393. out << "if __name__ == '__main__':" << endl;
  1394. out << " a = TQApplication(sys.argv)" << endl;
  1395. out << " TQObject.connect(a,SIGNAL('lastWindowClosed()'),a,SLOT('quit()'))" << endl;
  1396. out << " w = " << className << "()" << endl;
  1397. out << " a.setMainWidget(w)" << endl;
  1398. out << " w.show()" << endl;
  1399. out << " a.exec_loop()" << endl;
  1400. }
  1401. return 0;
  1402. }