📄 object.cpp
字号:
/************************************************************************ 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 "domtool.h"#include <qregexp.h>#include <qsizepolicy.h>#include <qstringlist.h>#define NO_STATIC_COLORS#include <globaldefs.h>/*! Creates a declaration for the object given in \a e. Children are not traversed recursively. \sa createObjectImpl() */void Uic::createObjectDecl( const QDomElement& e ){ if ( e.tagName() == "vbox" ) { out << " QVBoxLayout* " << registerObject(getLayoutName(e) ) << ";" << endl; } else if ( e.tagName() == "hbox" ) { out << " QHBoxLayout* " << registerObject(getLayoutName(e) ) << ";" << endl; } else if ( e.tagName() == "grid" ) { out << " QGridLayout* " << registerObject(getLayoutName(e) ) << ";" << endl; } else { QString objClass = getClassName( e ); if ( objClass.isEmpty() ) return; QString objName = getObjectName( e ); if ( objName.isEmpty() ) return; // ignore QLayoutWidgets if ( objClass == "QLayoutWidget" ) return; // register the object and unify its name objName = registerObject( objName ); if ( objClass == "Line" ) objClass = "QFrame"; out << " " << objClass << "* " << objName << ";" << endl; }}/*! Creates an implementation for the object given in \a e. Traverses recursively over all children. Returns the name of the generated child object. \sa createObjectDecl() */static bool createdCentralWidget = FALSE;QString Uic::createObjectImpl( const QDomElement &e, const QString& parentClass, const QString& par, const QString& layout ){ QString parent( par ); if ( parent == "this" && isMainWindow ) { if ( !createdCentralWidget ) out << indent << "setCentralWidget( new QWidget( this, \"qt_central_widget\" ) );" << endl; createdCentralWidget = TRUE; parent = "centralWidget()"; } QDomElement n; QString objClass, objName; int numItems = 0; int numColumns = 0; int numRows = 0; if ( layouts.contains( e.tagName() ) ) return createLayoutImpl( e, parentClass, parent, layout ); objClass = getClassName( e ); if ( objClass.isEmpty() ) return objName; objName = getObjectName( e ); QString definedName = objName; bool isTmpObject = objName.isEmpty() || objClass == "QLayoutWidget"; if ( isTmpObject ) { if ( objClass[0] == 'Q' ) objName = objClass.mid(1); else objName = objClass.lower(); objName.prepend( "private" ); } bool isLine = objClass == "Line"; if ( isLine ) objClass = "QFrame"; out << endl; if ( objClass == "QLayoutWidget" ) { if ( layout.isEmpty() ) { // register the object and unify its name objName = registerObject( objName ); out << " QWidget* " << objName << " = new QWidget( " << parent << ", \"" << definedName << "\" );" << endl; } else { // the layout widget is not necessary, hide it by creating its child in the parent QString result; for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if (tags.contains( n.tagName() ) ) result = createObjectImpl( n, parentClass, parent, layout ); } return result; } } else if ( objClass != "QToolBar" && objClass != "QMenuBar" ) { // register the object and unify its name objName = registerObject( objName ); out << " "; if ( isTmpObject ) out << objClass << "* "; out << objName << " = new " << createObjectInstance( objClass, parent, objName ) << ";" << endl; } lastItem = "0"; // set the properties and insert items bool hadFrameShadow = FALSE; for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "property" ) { bool stdset = stdsetdef; if ( n.hasAttribute( "stdset" ) ) stdset = toBool( n.attribute( "stdset" ) ); QString prop = n.attribute( "name" ); if ( prop == "database" ) continue; QString value = setObjectProperty( objClass, objName, prop, n.firstChild().toElement(), stdset ); if ( value.isEmpty() ) continue; if ( prop == "name" ) continue; if ( isLine && prop == "frameShadow" ) hadFrameShadow = TRUE; if ( prop == "buddy" && value.startsWith("\"") && value.endsWith("\"") ) { buddies << Buddy( objName, value.mid(1, value.length() - 2 ) ); continue; } if ( isLine && prop == "orientation" ) { prop = "frameShape"; if ( value.right(10) == "Horizontal" ) value = "QFrame::HLine"; else value = "QFrame::VLine"; if ( !hadFrameShadow ) { prop = "frameStyle"; value += " | QFrame::Sunken"; } } if ( prop == "buttonGroupId" ) { if ( parentClass == "QButtonGroup" ) out << indent << parent << "->insert( " << objName << ", " << value << " );" << endl; continue; } if ( prop == "frameworkCode" ) continue; if ( objClass == "QMultiLineEdit" && QRegExp("echoMode|hMargin|maxLength|maxLines|undoEnabled").exactMatch(prop) ) continue; QString call = objName + "->"; if ( stdset ) { call += mkStdSet( prop ) + "( "; } else { call += "setProperty( \"" + prop + "\", "; } if ( prop == "accel" ) call += "QKeySequence( " + value + " ) );"; else call += value + " );"; if ( n.firstChild().toElement().tagName() == "string" || prop == "currentItem" ) { trout << indent << call << endl; } else { out << indent << call << endl; } } else if ( n.tagName() == "item" ) { QString call; QString value; if ( objClass.mid( 1 ) == "ListBox" ) { call = createListBoxItemImpl( n, objName ); if ( !call.isEmpty() ) { if ( numItems == 0 ) trout << indent << objName << "->clear();" << endl; trout << indent << call << endl; } } else if ( objClass.mid( 1 ) == "ComboBox" ) { call = createListBoxItemImpl( n, objName, &value ); if ( !call.isEmpty() ) { if ( numItems == 0 ) trout << indent << objName << "->clear();" << endl; trout << indent << call << endl; } } else if ( objClass.mid( 1 ) == "IconView" ) { call = createIconViewItemImpl( n, objName ); if ( !call.isEmpty() ) { if ( numItems == 0 ) trout << indent << objName << "->clear();" << endl; trout << indent << call << endl; } } else if ( objClass.mid( 1 ) == "ListView" ) { call = createListViewItemImpl( n, objName, QString::null ); if ( !call.isEmpty() ) { if ( numItems == 0 ) trout << indent << objName << "->clear();" << endl; trout << call << endl; } } if ( !call.isEmpty() ) numItems++; } else if ( n.tagName() == "column" || n.tagName() == "row" ) { QString call; QString value; if ( objClass.mid( 1 ) == "ListView" ) { call = createListViewColumnImpl( n, objName, &value ); if ( !call.isEmpty() ) { out << call; trout << indent << objName << "->header()->setLabel( " << numColumns++ << ", " << value << " );\n"; } } else if ( objClass == "QTable" || objClass == "QDataTable" ) { bool isCols = ( n.tagName() == "column" ); call = createTableRowColumnImpl( n, objName, &value ); if ( !call.isEmpty() ) { out << call; trout << indent << objName << "->" << ( isCols ? "horizontalHeader" : "verticalHeader" ) << "()->setLabel( " << ( isCols ? numColumns++ : numRows++ ) << ", " << value << " );\n"; } } } } // create all children, some widgets have special requirements if ( objClass == "QTabWidget" ) { for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( tags.contains( n.tagName() ) ) { QString page = createObjectImpl( n, objClass, objName ); QString comment; QString label = DomTool::readAttribute( n, "title", "", comment ).toString(); out << indent << objName << "->insertTab( " << page << ", \"\" );" << endl; trout << indent << objName << "->changeTab( " << page << ", " << trcall( label, comment ) << " );" << endl; } } } else if ( objClass == "QWidgetStack" ) { for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( tags.contains( n.tagName() ) ) { QString page = createObjectImpl( n, objClass, objName ); int id = DomTool::readAttribute( n, "id", "" ).toInt(); out << indent << objName << "->addWidget( " << page << ", " << id << " );" << endl; } } } else if ( objClass != "QToolBar" && objClass != "QMenuBar" ) { // standard widgets for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( tags.contains( n.tagName() ) ) createObjectImpl( n, objClass, objName ); } } return objName;}/*! Creates a set-call for property \a exclusiveProp of the object given in \a e. If the object does not have this property, the function does nothing. Exclusive properties are used to generate the implementation of application font or palette change handlers in createFormImpl(). */void Uic::createExclusiveProperty( const QDomElement & e, const QString& exclusiveProp ){ QDomElement n; QString objClass = getClassName( e ); if ( objClass.isEmpty() ) return; QString objName = getObjectName( e ); if ( objClass.isEmpty() ) return; for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "property" ) { bool stdset = stdsetdef; if ( n.hasAttribute( "stdset" ) ) stdset = toBool( n.attribute( "stdset" ) ); QString prop = n.attribute( "name" ); if ( prop != exclusiveProp ) continue; QString value = setObjectProperty( objClass, objName, prop, n.firstChild().toElement(), stdset ); if ( value.isEmpty() ) continue; // we assume the property isn't of type 'string' out << '\t' << objName << "->setProperty( \"" << prop << "\", " << value << " );" << endl; } }}/*! Attention: this function has to be in sync with Resource::saveProperty() and DomTool::elementToVariant. If you change one, change all. */QString Uic::setObjectProperty( const QString& objClass, const QString& obj, const QString &prop, const QDomElement &e, bool stdset ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -