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.
tdebindings/qtruby/rubylib/designer/rbuic/uic.cpp

1105 lines
34 KiB

/**********************************************************************
** Copyright (C) 2000 Trolltech AS. All rights reserved.
** Copyright (c) 2001 Phil Thompson <phil@river-bank.demon.co.uk>
** Copyright (c) 2002 Germain Garand <germain@ebooksfrance.com>
**
** This file is part of Qt 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 info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
/*
** 06/2002 : Initial release of puic, the PerlQt User Interface Compiler,
** a work derivated from uic (the Qt User Interface Compiler)
** and pyuic (the PyQt User Interface Compiler).
**
** G.Garand
**
** 08/2003 : Initial release of rbuic, the QtRuby User Interface Compiler,
** a work derived from the PerlQt puic.
**
** Richard Dale
**
**********************************************************************/
#include "uic.h"
#include "parser.h"
#include "widgetdatabase.h"
#include "domtool.h"
#include <tqfile.h>
#include <tqstringlist.h>
#include <tqdatetime.h>
#define NO_STATIC_COLORS
#include <globaldefs.h>
#include <tqregexp.h>
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
bool Uic::hasKDEwidget = false;
bool Uic::isMainWindow = false;
RubyIndent Uic::indent;
// Re-calculate the indent string based on the current number and nature of the
// indent.
void RubyIndent::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 += ' ';
}
TTQString Uic::getComment( const TTQDomNode& n )
{
TTQDomNode child = n.firstChild();
while ( !child.isNull() ) {
if ( child.toElement().tagName() == "comment" )
return child.toElement().firstChild().toText().data();
child = child.nextSibling();
}
return TTQString::null;
}
TTQString Uic::mkBool( bool b )
{
return b? "true" : "false";
}
TTQString Uic::mkBool( const TTQString& s )
{
return mkBool( s == "true" || s == "1" );
}
bool Uic::toBool( const TTQString& s )
{
return s == "true" || s.toInt() != 0;
}
TTQString Uic::fixString( const TTQString &str )
{
TTQString s( str );
s.replace( TTQRegExp( "\\\\" ), "\\\\" );
s.replace( TTQRegExp( "\"" ), "\\\"" );
s.replace( TTQRegExp( "\r?\n" ), "\\n\" +\n" + indent + "\"" );
return "\"" + s + "\"";
}
TTQString Uic::trcall( const TTQString& sourceText, const TTQString& comment )
{
if ( sourceText.isEmpty() && comment.isEmpty() )
return "nil";
if ( comment.isEmpty() )
return trmacro + "(" + fixString( sourceText ) + ")";
return trmacro + "(" + fixString( sourceText ) + "," + fixString( comment ) + ")";
}
TTQString Uic::mkStdSet( const TTQString& prop )
{
return TTQString( "set" ) + prop[0].upper() + prop.mid(1);
}
bool Uic::isEmptyFunction( const TTQString& fname )
{
TTQMap<TTQString, TTQString>::Iterator fit = functionImpls.find( Parser::cleanArgs( fname ) );
if ( fit != functionImpls.end() ) {
int begin = (*fit).find( "{" );
TTQString body = (*fit).mid( begin + 1, (*fit).findRev( "}" ) - begin - 1 );
return body.simplifyWhiteSpace().isEmpty();
}
// For now ruby functions are always empty, until a rubyeditor Qt Designer plugin exists..
return true;
}
/*!
\class Uic uic.h
\brief User Interface Compiler
The class Uic encapsulates the user interface compiler (uic).
*/
Uic::Uic( const TTQString &fn, TTQTextStream &outStream, TTQDomDocument doc,
bool subcl, const TTQString &trm, const TTQString& subClass,
bool omitForwardDecls, TTQString &uicClass, bool useKDE )
: out( outStream ), trout( &languageChangeBody ),
trmacro( trm ), nofwd( omitForwardDecls )
{
Uic::hasKDEwidget = useKDE;
fileName = fn;
writeSlotImpl = 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() );
uiFileVersion = doc.firstChild().toElement().attribute("version");
stdsetdef = toBool( doc.firstChild().toElement().attribute("stdsetdef") );
TTQDomElement e = doc.firstChild().firstChild().toElement();
TTQDomElement 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", TTQString::number( defSpacing ) ).toInt();
defMargin = e.attribute( "margin", TTQString::number( defMargin ) ).toInt();
}
e = e.nextSibling().toElement();
}
e = widget;
if ( nameOfClass.isEmpty() )
nameOfClass = getObjectName( e );
uicClass = nameOfClass;
if ( subcl ) {
createSubImpl( e, subClass );
} else {
createFormImpl( e );
}
}
/*! Extracts a pixmap loader function from \a e
*/
TTQString Uic::getPixmapLoaderFunction( const TTQDomElement& e )
{
TTQDomElement n;
for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
if ( n.tagName() == "pixmapfunction" )
return n.firstChild().toText().data();
}
return TTQString::null;
}
/*! Extracts the forms class name from \a e
*/
TTQString Uic::getFormClassName( const TTQDomElement& e )
{
TTQDomElement n;
TTQString cn;
for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
if ( n.tagName() == "class" ) {
TTQString s = n.firstChild().toText().data();
int i;
while ( ( i = s.find(' ' )) != -1 )
s[i] = '_';
cn = s;
}
}
return cn;
}
/*! Extracts a Ruby class name from \a e.
*/
TTQString Uic::getClassName( const TTQDomElement& e )
{
TTQString s = e.attribute( "class" );
if ( s.isEmpty() && e.tagName() == "toolbar" )
s = "TQt::ToolBar";
else if ( s.isEmpty() && e.tagName() == "menubar" )
s = "TQt::MenuBar";
else
{
TTQRegExp r("^([QK])(\\S+)");
if( r.search( s ) != -1 ) {
if (r.cap(1) == "K") {
hasKDEwidget = true;
s = "KDE::" + r.cap(2);
} else {
if (s.startsWith("Qext")) {
s = "Qext::" + r.cap(2).mid(4);
} else {
s = "TQt::" + r.cap(2);
}
}
}
}
return s;
}
/*! Returns TRUE if database framework code is generated, else FALSE.
*/
bool Uic::isFrameworkCodeGenerated( const TTQDomElement& e )
{
TTQDomElement n = getObjectProperty( e, "frameworkCode" );
if ( n.attribute("name") == "frameworkCode" &&
!DomTool::elementToVariant( n.firstChild().toElement(), TTQVariant( true, 0 ) ).toBool() )
return false;
return true;
}
/*! Extracts an object name from \a e. It's stored in the 'name'
property.
*/
TTQString Uic::getObjectName( const TTQDomElement& e )
{
TTQDomElement n = getObjectProperty( e, "name" );
if ( n.firstChild().toElement().tagName() == "cstring" )
return n.firstChild().toElement().firstChild().toText().data();
return TTQString::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).
*/
TTQString Uic::getLayoutName( const TTQDomElement& e )
{
TTQDomElement p = e.parentNode().toElement();
TTQString tail = TTQString::null;
if ( getClassName(p) != "TQt::LayoutWidget" )
tail = "Layout";
TTQDomElement n = getObjectProperty( p, "name" );
if ( n.firstChild().toElement().tagName() == "cstring" )
return n.firstChild().toElement().firstChild().toText().data() + tail;
return e.tagName();
}
TTQString Uic::getDatabaseInfo( const TTQDomElement& e, const TTQString& tag )
{
TTQDomElement n;
TTQDomElement 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 TTQString::null;
n = getObjectProperty( e, "database" );
if ( n.firstChild().toElement().tagName() == "stringlist" ) {
// find correct stringlist entry
TTQDomElement n1 = n.firstChild().firstChild().toElement();
for ( int i = 0; i < child && !n1.isNull(); ++i )
n1 = n1.nextSibling().toElement();
if ( n1.isNull() )
return TTQString::null;
return n1.firstChild().toText().data();
}
return TTQString::null;
}
void Uic::registerLayouts( const TTQDomElement &e )
{
if ( layouts.contains(e.tagName()) )
createObjectDecl(e);
TTQDomNodeList 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.
*/
TTQString Uic::getInclude( const TTQString& className )
{
int wid = WidgetDatabase::idFromClassName( className );
if ( wid != -1 )
return WidgetDatabase::includeFile( wid );
return TTQString::null;
}
void Uic::createActionDecl( const TTQDomElement& e )
{
TTQString objName = getObjectName( e );
if ( objName.isEmpty() )
return;
out << indent << objName << endl;
if ( e.tagName() == "actiongroup" ) {
for ( TTQDomElement n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
if ( n.tagName() == "action" || n.tagName() == "actiongroup" )
createActionDecl( n );
}
}
}
void Uic::createActionImpl( const TTQDomElement &n, const TTQString &parent )
{
for ( TTQDomElement ae = n; !ae.isNull(); ae = ae.nextSibling().toElement() ) {
TTQString objName = registerObject( getObjectName( ae ) );
if ( ae.tagName() == "action" )
out << indent << objName << "= TQt::Action.new(" << parent << ", \"" << objName.mid(1) << "\")" << endl;
else if ( ae.tagName() == "actiongroup" )
out << indent << objName << "= TQt::ActionGroup.new(" << parent << ", \"" << objName.mid(1) << "\")" << endl;
else
continue;
bool subActionsDone = false;
bool hasMenuText = false;
TTQString actionText;
for ( TTQDomElement 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" ) );
TTQString prop = n2.attribute("name");
if ( prop == "name" )
continue;
TTQString value = setObjectProperty( "TQt::Action", objName, prop, n2.firstChild().toElement(), stdset );
if ( value.isEmpty() )
continue;
TTQString call = objName + ".";
if ( stdset ) {
call += mkStdSet( prop ) + "(" + value + ")";
} else {
call += "setProperty( \"" + prop + "\", ";
call += "TQt::Variant.new(" + value + "))";
}
if (prop == "menuText")
hasMenuText = true;
else if (prop == "text")
actionText = value;
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;
}
}
// workaround for loading pre-3.3 files expecting bogus TTQAction behavior
if (!hasMenuText && !actionText.isEmpty() && uiFileVersion < "3.3")
trout << indent << objName << ".setMenuText(" << actionText << ")" << endl;
}
}
TTQString get_dock( const TTQString &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 TTQDomElement &n, const TTQString &parentClass, const TTQString &parent )
{
TTQDomNodeList nl = n.elementsByTagName( "toolbar" );
for ( int i = 0; i < (int) nl.length(); i++ ) {
TTQDomElement ae = nl.item( i ).toElement();
TTQString dock = get_dock( ae.attribute( "dock" ) );
TTQString objName = "@" + getObjectName( ae );
out << indent << objName << " = TQt::ToolBar.new(\"\", self, " << dock << ")" << endl;
createObjectImpl( ae, parentClass, parent );
for ( TTQDomElement 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 {
TTQString child = createSpacerImpl( n2, parentClass, parent, objName );
out << indent << "TQt::Application.sendPostedEvents( " << objName
<< ", TQt::Event::ChildInserted)" << endl;
out << indent << objName << ".boxLayout().addItem(" << child << ")" << endl;
}
}
}
}
}
void Uic::createMenuBarImpl( const TTQDomElement &n, const TTQString &parentClass, const TTQString &parent )
{
TTQString objName = "@" + getObjectName( n );
out << indent << objName << " = TQt::MenuBar.new( self, \"" << objName.mid(1) << "\" )" << endl;
createObjectImpl( n, parentClass, parent );
int i = 0;
TTQDomElement c = n.firstChild().toElement();
while ( !c.isNull() ) {
if ( c.tagName() == "item" ) {
TTQString itemName = "@" + c.attribute( "name" );
out << endl;
out << indent << itemName << " = TQt::PopupMenu.new( self )" << endl;
createPopupMenuImpl( c, parentClass, itemName );
out << indent << objName << ".insertItem( \"\", " << itemName << ", " << i << " )" << endl;
TTQString findItem(objName + ".findItem(%1)");
findItem = findItem.arg(i);
trout << indent << "if !" << findItem << ".nil?" << endl;
trout << indent << indent << findItem << ".setText( " << trcall( c.attribute( "text" ) ) << " )" << endl;
trout << indent << "end" << endl;
} else if ( c.tagName() == "separator" ) {
out << endl;
out << indent << objName << ".insertSeparator( " << i << " )" << endl;
}
c = c.nextSibling().toElement();
i++;
}
}
void Uic::createPopupMenuImpl( const TTQDomElement &e, const TTQString &parentClass, const TTQString &parent )
{
int i = 0;
for ( TTQDomElement n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) {
if ( n.tagName() == "action" || n.tagName() == "actiongroup" ) {
TTQDomElement n2 = n.nextSibling().toElement();
if ( n2.tagName() == "item" ) { // the action has a sub menu
TTQString itemName = "@" + n2.attribute( "name" );
TTQString itemText = n2.attribute( "text" );
out << indent << itemName << " = TQt::PopupMenu.new( self )" << endl;
out << indent << indent << parent << ".insertItem( @" << n.attribute( "name" ) << ".iconSet(),";
out << trcall( itemText ) << ", " << itemName << " )" << endl;
trout << indent << parent << ".changeItem( " << parent << ".idAt( " << i << " ), ";
trout << trcall( itemText ) << " )" << endl;
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;
}
++i;
}
}
/*!
Creates implementation of an listbox item tag.
*/
TTQString Uic::createListBoxItemImpl( const TTQDomElement &e, const TTQString &parent,
TTQString *value )
{
TTQDomElement n = e.firstChild().toElement();
TTQString txt;
TTQString com;
TTQString pix;
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TTQString attrib = n.attribute("name");
TTQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TTQVariant() );
if ( attrib == "text" ) {
txt = v.toString();
com = getComment( n );
} else if ( attrib == "pixmap" ) {
pix = v.toString();
if (!pix.isEmpty()) {
pix.prepend("@");
}
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TTQString( externPixmaps ? "\"" : "" ) );
pix.append( TTQString( externPixmaps ? "\"" : "" ) + ")" );
}
}
}
n = n.nextSibling().toElement();
}
if ( value )
*value = trcall( txt, com );
if ( pix.isEmpty() )
return parent + ".insertItem(" + trcall( txt, com ) + ")";
return parent + ".insertItem(" + pix + ", " + trcall( txt, com ) + ")";
}
/*!
Creates implementation of an iconview item tag.
*/
TTQString Uic::createIconViewItemImpl( const TTQDomElement &e, const TTQString &parent )
{
TTQDomElement n = e.firstChild().toElement();
TTQString txt;
TTQString com;
TTQString pix;
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TTQString attrib = n.attribute("name");
TTQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TTQVariant() );
if ( attrib == "text" ) {
txt = v.toString();
com = getComment( n );
} else if ( attrib == "pixmap" ) {
pix = v.toString();
if (!pix.isEmpty()) {
pix.prepend("@");
}
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TTQString( externPixmaps ? "\"" : "" ) );
pix.append( TTQString( externPixmaps ? "\"" : "" ) + ")" );
}
}
}
n = n.nextSibling().toElement();
}
if ( pix.isEmpty() )
return "TQt::IconViewItem.new(" + parent + ", " + trcall( txt, com ) + ")";
else
return "TQt::IconViewItem.new(" + parent + ", " + trcall( txt, com ) + ", " + pix + ")";
}
/*!
Creates implementation of an listview item tag.
*/
TTQString Uic::createListViewItemImpl( const TTQDomElement &e, const TTQString &parent,
const TTQString &parentItem )
{
TTQString s;
TTQDomElement n = e.firstChild().toElement();
bool hasChildren = e.elementsByTagName( "item" ).count() > 0;
TTQString item;
if ( hasChildren ) {
item = registerObject( "item" );
s = indent + item + " = ";
} else {
if (! item_used) {
// This is here to make the ruby generated names for 'item',
// the same as the original C++
item = registerObject( "item" );
item_used = true;
}
item = "item";
s = indent + item + " = ";
}
if ( !parentItem.isEmpty() )
s += "TQt::ListViewItem.new(" + parentItem + ", " + lastItem + ")\n";
else
s += "TQt::ListViewItem.new(" + parent + ", " + lastItem + ")\n";
TTQStringList textes;
TTQStringList pixmaps;
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TTQString attrib = n.attribute("name");
TTQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TTQVariant() );
if ( attrib == "text" )
textes << v.toString();
else if ( attrib == "pixmap" ) {
TTQString pix = v.toString();
if (!pix.isEmpty()) {
pix.prepend("@");
}
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TTQString( externPixmaps ? "\"" : "" ) );
pix.append( TTQString( externPixmaps ? "\"" : "" ) + ")" );
}
pixmaps << pix;
}
} else if ( n.tagName() == "item" ) {
s += indent + item + ".setOpen(true)\n";
s += createListViewItemImpl( n, parent, item );
}
n = n.nextSibling().toElement();
}
for ( int i = 0; i < (int)textes.count(); ++i ) {
if ( !textes[ i ].isEmpty() )
s += indent + item + ".setText(" + TTQString::number( i ) + ", " + trcall( textes[ i ] ) + ")\n";
if ( !pixmaps[ i ].isEmpty() )
s += indent + item + ".setPixmap(" + TTQString::number( i ) + ", " + pixmaps[ i ] + ")\n";
}
lastItem = item;
return s;
}
/*!
Creates implementation of an listview column tag.
*/
TTQString Uic::createListViewColumnImpl( const TTQDomElement &e, const TTQString &parent,
TTQString *value )
{
TTQDomElement n = e.firstChild().toElement();
TTQString txt;
TTQString com;
TTQString pix;
bool clickable = false, resizeable = false;
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TTQString attrib = n.attribute("name");
TTQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TTQVariant() );
if ( attrib == "text" ) {
txt = v.toString();
com = getComment( n );
} else if ( attrib == "pixmap" ) {
pix = v.toString();
if (!pix.isEmpty()) {
pix.prepend("@");
}
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TTQString( externPixmaps ? "\"" : "" ) );
pix.append( TTQString( externPixmaps ? "\"" : "" ) + ")" );
}
} else if ( attrib == "clickable" )
clickable = v.toBool();
else if ( attrib == "resizable" || attrib == "resizeable" )
resizeable = v.toBool();
}
n = n.nextSibling().toElement();
}
if ( value )
*value = trcall( txt, com );
TTQString 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( false, " + parent + ".header().count() - 1 )\n";
if ( !resizeable )
s += indent + parent + ".header().setResizeEnabled( false, " + parent + ".header().count() - 1 )\n";
return s;
}
TTQString Uic::createTableRowColumnImpl( const TTQDomElement &e, const TTQString &parent,
TTQString *value )
{
TTQString objClass = getClassName( e.parentNode().toElement() );
TTQDomElement n = e.firstChild().toElement();
TTQString txt;
TTQString com;
TTQString pix;
TTQString field;
bool isRow = e.tagName() == "row";
while ( !n.isNull() ) {
if ( n.tagName() == "property" ) {
TTQString attrib = n.attribute("name");
TTQVariant v = DomTool::elementToVariant( n.firstChild().toElement(), TTQVariant() );
if ( attrib == "text" ) {
txt = v.toString();
com = getComment( n );
} else if ( attrib == "pixmap" ) {
pix = v.toString();
if ( !pix.isEmpty() && !pixmapLoaderFunction.isEmpty() ) {
pix.prepend( pixmapLoaderFunction + "(" + TTQString( externPixmaps ? "\"" : "" ) );
pix.append( TTQString( 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 TTQDataTable has an API which makes this code pretty good
TTQString 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.new(@"
+ 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.new(@"
+ 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.new(@" << pix << "))" << endl;
}
}
}
return s;
}
/*!
Creates the implementation of a layout tag. Called from createObjectImpl().
*/
TTQString Uic::createLayoutImpl( const TTQDomElement &e, const TTQString& parentClass, const TTQString& parent, const TTQString& layout )
{
TTQDomElement n;
TTQString objClass, objName;
objClass = e.tagName();
TTQString qlayout = "TQt::VBoxLayout.new";
if ( objClass == "hbox" )
qlayout = "TQt::HBoxLayout.new";
else if ( objClass == "grid" )
qlayout = "TQt::GridLayout.new";
bool isGrid = e.tagName() == "grid" ;
objName = registerObject( getLayoutName( e ) );
layoutObjects += objName;
TTQString margin = DomTool::readProperty( e, "margin", defMargin ).toString();
TTQString spacing = DomTool::readProperty( e, "spacing", defSpacing ).toString();
TTQString resizeMode = DomTool::readProperty( e, "resizeMode", TTQString::null ).toString();
TTQString 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, TQt::Vertical )" << endl;
out << indent << parent << ".layout().setSpacing(" << spacing << ")" << endl;
out << indent << parent << ".layout().setMargin(" << margin << ")" << endl;
out << indent << objName << " = " << qlayout << "(" << parent << ".layout() )" << endl;
out << indent << objName << ".setAlignment( AlignTop )" << endl;
} else {
out << indent << objName << " = " << qlayout << "(";
if ( layout.isEmpty() )
out << parent;
else {
out << "nil";
if ( !DomTool::hasProperty( e, "margin" ) )
margin = "0";
}
out << ", " << optcells << margin << ", " << spacing << ", '" << objName.mid(1) << "')" << 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" ) {
TTQString child = createSpacerImpl( n, parentClass, parent, objName );
out << indent << objName << ".addItem(" << child << ")" << endl;
} else if ( tags.contains( n.tagName() ) ) {
TTQString 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() ) {
TTQDomElement 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" ) {
TTQString 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() ) ) {
TTQString child = createObjectImpl( n, parentClass, parent, objName );
out << endl;
TTQString 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;
}
TTQString Uic::createSpacerImpl( const TTQDomElement &e, const TTQString& /*parentClass*/, const TTQString& /*parent*/, const TTQString& /*layout*/)
{
TTQDomElement n;
TTQString objClass, objName;
objClass = e.tagName();
objName = registerObject( getObjectName( e ) );
TTQSize size = DomTool::readProperty( e, "sizeHint", TTQSize( 0, 0 ) ).toSize();
TTQString 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 Qt 2.2
TTQRect geom = DomTool::readProperty( e, "geometry", TTQRect(0,0,0,0) ).toRect();
size = geom.size();
}
if ( isVspacer )
out << indent << objName << " = TQt::SpacerItem.new("
<< size.width() << ", " << size.height()
<< ", TQt::SizePolicy::Minimum, TQt::SizePolicy::" << sizeType << ")" << endl;
else
out << indent << objName << " = TQt::SpacerItem.new("
<< 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 TTQString& name, const TTQDomElement& e )
{
TTQColorGroup cg;
int r = -1;
TTQDomElement n = e.firstChild().toElement();
TTQString color;
while ( !n.isNull() ) {
if ( n.tagName() == "color" ) {
r++;
TTQColor col = DomTool::readColor( n );
color = "TQt::Color.new(%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" ) {
TTQString pixmap = n.firstChild().toText().data();
pixmap.prepend("@");
if ( !pixmapLoaderFunction.isEmpty() ) {
pixmap.prepend( pixmapLoaderFunction + "(" + TTQString( externPixmaps ? "\"" : "" ) );
pixmap.append( TTQString( externPixmaps ? "\"" : "" ) + ")" );
}
out << indent << name << ".setBrush(TQt::ColorGroup::"
<< ColorRole[r] << ", TQt::Brush.new(" << color << ", " << pixmap << "))" << endl;
}
n = n.nextSibling().toElement();
}
}
/*!
Auxiliary function to load a color group. The colorgroup must not
contain pixmaps.
*/
TTQColorGroup Uic::loadColorGroup( const TTQDomElement &e )
{
TTQColorGroup cg;
int r = -1;
TTQDomElement n = e.firstChild().toElement();
TTQColor col;
while ( !n.isNull() ) {
if ( n.tagName() == "color" ) {
r++;
cg.setColor( (TTQColorGroup::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 TTQDomElement& e, const TTQString& connection, const TTQString& table )
{
TTQString conn = getDatabaseInfo( e, "connection" );
TTQString tab = getDatabaseInfo( e, "table" );
if ( conn == connection && tab == table )
return true;
return false;
}
/*!
Registers all database connections, cursors and forms.
*/
void Uic::registerDatabases( const TTQDomElement& e )
{
TTQDomElement n;
TTQDomNodeList nl;
int i;
nl = e.parentNode().toElement().elementsByTagName( "widget" );
for ( i = 0; i < (int) nl.length(); ++i ) {
n = nl.item(i).toElement();
TTQString conn = getDatabaseInfo( n, "connection" );
TTQString tab = getDatabaseInfo( n, "table" );
TTQString 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()
*/
TTQString Uic::registerObject( const TTQString& name )
{
if ( objectNames.isEmpty() ) {
// some temporary variables we need
objectNames += "img";
objectNames += "item";
objectNames += "cg";
objectNames += "pal";
}
TTQString result("@");
result += name;
int i;
while ( ( i = result.find(' ' )) != -1 ) {
result[i] = '_';
}
if ( objectNames.contains( result ) ) {
int i = 2;
while ( objectNames.contains( result + "_" + TTQString::number(i) ) )
i++;
result += "_";
result += TTQString::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()
*/
TTQString Uic::registeredName( const TTQString& 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 TTQString& name )
{
return objectMapper.contains( name );
}
/*!
Unifies the entries in stringlist \a list. Should really be a TTQStringList feature.
*/
TTQStringList Uic::unique( const TTQStringList& list )
{
TTQStringList result;
if ( list.isEmpty() )
return result;
TTQStringList l = list;
l.sort();
result += l.first();
for ( TTQStringList::Iterator it = l.begin(); it != l.end(); ++it ) {
if ( *it != result.last() )
result += *it;
}
return result;
}
/*!
Creates an instance of class \a objClass, with parent \a parent and name \a objName
*/
TTQString Uic::createObjectInstance( const TTQString& objClass, const TTQString& parent, const TTQString& objName )
{
if ( objClass.mid( 4 ) == "ComboBox" ) {
return objClass + ".new(false, " + parent + ", \"" + objName.mid(1) + "\")";
}
return objClass + ".new(" + parent + ", \"" + objName.mid(1) + "\")";
}
bool Uic::isLayout( const TTQString& name ) const
{
return layoutObjects.contains( name );
}