uic.cpp
来自「qt-x11-free-3.0.3.tar.gz minigui图形界面工具」· C++ 代码 · 共 985 行 · 第 1/3 页
CPP
985 行
/************************************************************************ Copyright (C) 2000 Trolltech AS. All rights reserved.**** 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.************************************************************************/#include "uic.h"#include "parser.h"#include "widgetdatabase.h"#include "domtool.h"#include <qfile.h>#include <qstringlist.h>#include <qdatetime.h>#define NO_STATIC_COLORS#include <globaldefs.h>#include <qregexp.h>#include <stdio.h>#include <stdlib.h>#include <zlib.h>bool Uic::isMainWindow = FALSE;QString Uic::getComment( const QDomNode& n ){ QDomNode child = n.firstChild(); while ( !child.isNull() ) { if ( child.toElement().tagName() == "comment" ) return child.toElement().firstChild().toText().data(); child = child.nextSibling(); } return QString::null;}QString Uic::mkBool( bool b ){ return b? "TRUE" : "FALSE";}QString Uic::mkBool( const QString& s ){ return mkBool( s == "true" || s == "1" );}bool Uic::toBool( const QString& s ){ return s == "true" || s.toInt() != 0;}QString Uic::fixString( const QString &str ){ QString s( str ); s.replace( QRegExp( "\\\\" ), "\\\\" ); s.replace( QRegExp( "\"" ), "\\\"" ); s.replace( QRegExp( "\r?\n" ), "\\n\"\n\"" ); return "\"" + s + "\"";}QString Uic::trcall( const QString& sourceText, const QString& comment ){ if ( comment.isEmpty() ) return trmacro + "( " + fixString( sourceText ) + " )"; return trmacro + "( " + fixString( sourceText ) + ", " + fixString( comment ) + " )";}QString Uic::mkStdSet( const QString& prop ){ return QString( "set" ) + prop[0].upper() + prop.mid(1);}bool Uic::isEmptyFunction( const QString& fname ){ QMap<QString, QString>::Iterator fit = functionImpls.find( Parser::cleanArgs( fname ) ); if ( fit != functionImpls.end() ) { int begin = (*fit).find( "{" ); QString body = (*fit).mid( begin + 1, (*fit).find( "}" ) - begin - 1 ); return body.simplifyWhiteSpace().isEmpty(); } return FALSE;}/*! \class Uic uic.h \brief User Interface Compiler The class Uic encapsulates the user interface compiler (uic). */Uic::Uic( const QString &fn, QTextStream &outStream, QDomDocument doc, bool decl, bool subcl, const QString &trm, const QString& subClass, bool omitForwardDecls ) : out( outStream ), trmacro( trm ), nofwd( omitForwardDecls ){ fileName = fn; writeSlotImpl = TRUE; defMargin = BOXLAYOUT_DEFAULT_MARGIN; defSpacing = BOXLAYOUT_DEFAULT_SPACING; externPixmaps = FALSE; indent = " "; // default indent 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") ); QDomElement e = doc.firstChild().firstChild().toElement(); QDomElement 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", QString::number( defSpacing ) ).toInt(); defMargin = e.attribute( "margin", QString::number( defMargin ) ).toInt(); } e = e.nextSibling().toElement(); } e = widget; if ( nameOfClass.isEmpty() ) nameOfClass = getObjectName( e ); if ( subcl ) { if ( decl ) createSubDecl( e, subClass ); else createSubImpl( e, subClass ); } else { if ( decl ) createFormDecl( e ); else createFormImpl( e ); }}/*! Extracts a pixmap loader function from \a e */QString Uic::getPixmapLoaderFunction( const QDomElement& e ){ QDomElement n; for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "pixmapfunction" ) return n.firstChild().toText().data(); } return QString::null;}/*! Extracts the forms class name from \a e */QString Uic::getFormClassName( const QDomElement& e ){ QDomElement n; QString cn; for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "class" ) { QString s = n.firstChild().toText().data(); int i; while ( ( i = s.find(' ' )) != -1 ) s[i] = '_'; cn = s; } } return cn;}/*! Extracts a class name from \a e. */QString Uic::getClassName( const QDomElement& e ){ QString s = e.attribute( "class" ); if ( s.isEmpty() && e.tagName() == "toolbar" ) s = "QToolBar"; else if ( s.isEmpty() && e.tagName() == "menubar" ) s = "QMenuBar"; return s;}/*! Returns TRUE if database framework code is generated, else FALSE.*/bool Uic::isFrameworkCodeGenerated( const QDomElement& e ){ QDomElement n = getObjectProperty( e, "frameworkCode" ); if ( n.attribute("name") == "frameworkCode" && !DomTool::elementToVariant( n.firstChild().toElement(), QVariant( TRUE, 0 ) ).toBool() ) return FALSE; return TRUE;}/*! Extracts an object name from \a e. It's stored in the 'name' property. */QString Uic::getObjectName( const QDomElement& e ){ QDomElement n = getObjectProperty( e, "name" ); if ( n.firstChild().toElement().tagName() == "cstring" ) return n.firstChild().toElement().firstChild().toText().data(); return QString::null;}/*! Extracts an layout name from \a e. It's stored in the 'name' property of the preceeding sibling (the first child of a QLayoutWidget). */QString Uic::getLayoutName( const QDomElement& e ){ QDomElement p = e.parentNode().toElement(); QString tail = QString::null; if ( getClassName(p) != "QLayoutWidget" ) tail = "Layout"; QDomElement n = getObjectProperty( p, "name" ); if ( n.firstChild().toElement().tagName() == "cstring" ) return n.firstChild().toElement().firstChild().toText().data() + tail; return e.tagName();}QString Uic::getDatabaseInfo( const QDomElement& e, const QString& tag ){ QDomElement n; QDomElement 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 QString::null; n = getObjectProperty( e, "database" ); if ( n.firstChild().toElement().tagName() == "stringlist" ) { // find correct stringlist entry QDomElement n1 = n.firstChild().firstChild().toElement(); for ( int i = 0; i < child && !n1.isNull(); ++i ) n1 = n1.nextSibling().toElement(); if ( n1.isNull() ) return QString::null; return n1.firstChild().toText().data(); } return QString::null;}void Uic::registerLayouts( const QDomElement &e ){ if ( layouts.contains(e.tagName()) ) createObjectDecl(e); QDomNodeList 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. */QString Uic::getInclude( const QString& className ){ int wid = WidgetDatabase::idFromClassName( className ); if ( wid != -1 ) return WidgetDatabase::includeFile( wid ); return QString::null;}void Uic::createActionDecl( const QDomElement& e ){ QString objClass = e.tagName() == "action" ? "QAction" : "QActionGroup"; QString objName = getObjectName( e ); if ( objName.isEmpty() ) return; out << " " << objClass << "* " << objName << ";" << endl; if ( e.tagName() == "actiongroup" ) { for ( QDomElement n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "action" || n.tagName() == "actiongroup" ) createActionDecl( n ); } }}void Uic::createToolbarDecl( const QDomElement &e ){ if ( e.tagName() == "toolbar" ) out << " " << "QToolBar *" << getObjectName( e ) << ";" << endl;}void Uic::createMenuBarDecl( const QDomElement &e ){ if ( e.tagName() == "item" ) out << " " << "QPopupMenu *" << e.attribute( "name" ) << ";" << endl;}void Uic::createActionImpl( const QDomElement &n, const QString &parent ){ for ( QDomElement ae = n; !ae.isNull(); ae = ae.nextSibling().toElement() ) { QString objName = registerObject( getObjectName( ae ) ); if ( ae.tagName() == "action" ) out << indent << objName << " = new QAction( " << parent << ", \"" << objName << "\" );" << endl; else if ( ae.tagName() == "actiongroup" ) out << indent << objName << " = new QActionGroup( " << parent << ", \"" << objName << "\" );" << endl;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?