form.cpp
来自「qt-x11-free-3.0.3.tar.gz minigui图形界面工具」· C++ 代码 · 共 1,243 行 · 第 1/3 页
CPP
1,243 行
out << " */" << endl; out << nameOfClass << "::" << nameOfClass << "( QWidget* parent, const char* name, WFlags fl )" << endl; out << " : " << objClass << "( parent, name, fl )" << endl; isMainWindow = TRUE; } else { out << "/* " << endl; out << " * Constructs a " << nameOfClass << " which is a child of 'parent', with the " << endl; out << " * name 'name'.' " << endl; out << " */" << endl; out << nameOfClass << "::" << nameOfClass << "( QWidget* parent, const char* name )" << endl; out << " : " << objClass << "( parent, name )" << endl; } out << "{" << endl; if ( isMainWindow ) out << indent << "(void)statusBar();" << endl; // create pixmaps for all images if ( !images.isEmpty() ) { out << indent << "QImage img;" << endl; out << indent << "QPixmap "; QStringList::Iterator it; for ( it = images.begin(); it != images.fromLast(); ++it ) out << (*it) << ", "; out << (*it) << ";" << endl; for ( it = images.begin(); it != images.end(); ++it ) { out << indent << "img.loadFromData( " << (*it) << "_data, sizeof( " << (*it) << "_data ), \"PNG\" );" << endl; out << indent << (*it) << " = img;" << endl; } } // create pixmaps for all images if ( !xpmImages.isEmpty() ) { for ( it = xpmImages.begin(); it != xpmImages.end(); ++it ) { out << indent << "QPixmap " << (*it) << "( ( const char** ) " << (*it) << "_data );" << endl; } } // set the properties 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"); QDomElement n2 = n.firstChild().toElement(); QString value = setObjectProperty( objClass, QString::null, prop, n2, stdset ); if ( value.isEmpty() ) continue; if ( prop == "name" ) { out << " if ( !name )" << endl; out << "\t"; } else { out << indent; } if ( prop == "geometry" && n2.tagName() == "rect") { QDomElement n3 = n2.firstChild().toElement(); int w = 0, h = 0; while ( !n3.isNull() ) { if ( n3.tagName() == "width" ) w = n3.firstChild().toText().data().toInt(); else if ( n3.tagName() == "height" ) h = n3.firstChild().toText().data().toInt(); n3 = n3.nextSibling().toElement(); } out << "resize( " << w << ", " << h << " ); " << endl; } else { if ( stdset ) out << mkStdSet( prop ) << "( " << value << " );" << endl; else out << "setProperty( \"" << prop << "\", " << value << " );" << endl; } } } // create all children, some forms have special requirements if ( objClass == "QWizard" ) { for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( tags.contains( n.tagName() ) ) { QString page = createObjectImpl( n, objClass, "this" ); QString label = DomTool::readAttribute( n, "title", "" ).toString(); out << indent << "addPage( " << page << ", "<< trcall( label ) << " );" << endl; QVariant def( FALSE, 0 ); if ( DomTool::hasAttribute( n, "backEnabled" ) ) out << indent << "setBackEnabled( " << page << ", " << mkBool( DomTool::readAttribute( n, "backEnabled", def).toBool() ) << endl; if ( DomTool::hasAttribute( n, "nextEnabled" ) ) out << indent << "setNextEnabled( " << page << ", " << mkBool( DomTool::readAttribute( n, "nextEnabled", def).toBool() ) << endl; if ( DomTool::hasAttribute( n, "finishEnabled" ) ) out << indent << "setFinishEnabled( " << page << ", " << mkBool( DomTool::readAttribute( n, "finishEnabled", def).toBool() ) << " );" << endl; if ( DomTool::hasAttribute( n, "helpEnabled" ) ) out << indent << "setHelpEnabled( " << page << ", " << mkBool( DomTool::readAttribute( n, "helpEnabled", def).toBool() ) << endl; if ( DomTool::hasAttribute( n, "finish" ) ) out << indent << "setFinish( " << page << ", " << mkBool( DomTool::readAttribute( n, "finish", def).toBool() ) << endl; } } } else { // standard widgets for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( tags.contains( n.tagName() ) ) createObjectImpl( n, objName, "this" ); } } // database support dbConnections = unique( dbConnections ); if ( dbConnections.count() ) out << endl << indent << "// database support" << endl; for ( it = dbConnections.begin(); it != dbConnections.end(); ++it ) { if ( !(*it).isEmpty() && (*it) != "(default)") { out << indent << (*it) << "Connection = QSqlDatabase::database( \"" <<(*it) << "\" );" << endl; } } nl = e.parentNode().toElement().elementsByTagName( "widget" ); for ( i = 1; i < (int) nl.length(); i++ ) { // start at 1, 0 is the toplevel widget n = nl.item(i).toElement(); QString s = getClassName( n ); if ( s == "QDataBrowser" || s == "QDataView" ) { QString objName = getObjectName( n ); QString tab = getDatabaseInfo( n, "table" ); QString con = getDatabaseInfo( n, "connection" ); out << indent << "QSqlForm* " << objName << "Form = new QSqlForm( this, \"" << objName << "Form\" );" << endl; QDomElement n2; for ( n2 = n.firstChild().toElement(); !n2.isNull(); n2 = n2.nextSibling().toElement() ) createFormImpl( n2, objName, con, tab ); out << indent << objName << "->setForm( " << objName << "Form );" << endl; } } // actions, toolbars, menubar bool needEndl = FALSE; for ( n = e; !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "actions" ) { if ( !needEndl ) out << endl << indent << "// actions" << endl; createActionImpl( n.firstChild().toElement(), "this" ); needEndl = TRUE; } } if ( needEndl ) out << endl; needEndl = FALSE; for ( n = e; !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "toolbars" ) { if ( !needEndl ) out << endl << indent << "// toolbars" << endl; createToolbarImpl( n, objClass, objName ); needEndl = TRUE; } } if ( needEndl ) out << endl; needEndl = FALSE; for ( n = e; !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "menubar" ) { if ( !needEndl ) out << endl << indent << "// menubar" << endl; createMenuBarImpl( n, objClass, objName ); needEndl = TRUE; } } if ( needEndl ) out << endl; for ( n = e; !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "connections" ) { // setup signals and slots connections out << endl << indent << "// signals and slots connections" << endl; nl = n.elementsByTagName( "connection" ); for ( i = 0; i < (int) nl.length(); i++ ) { QString sender, receiver, signal, slot; for ( QDomElement n2 = nl.item(i).firstChild().toElement(); !n2.isNull(); n2 = n2.nextSibling().toElement() ) { if ( n2.tagName() == "sender" ) sender = n2.firstChild().toText().data(); else if ( n2.tagName() == "receiver" ) receiver = n2.firstChild().toText().data(); else if ( n2.tagName() == "signal" ) signal = n2.firstChild().toText().data(); else if ( n2.tagName() == "slot" ) slot = n2.firstChild().toText().data(); } if ( sender.isEmpty() || receiver.isEmpty() || signal.isEmpty() || slot.isEmpty() ) continue; sender = registeredName( sender ); receiver = registeredName( receiver ); // translate formwindow name to "this" if ( sender == objName ) sender = "this"; if ( receiver == objName ) receiver = "this"; out << indent << "connect( " << sender << ", SIGNAL( " << signal << " ), " << receiver << ", SLOT( " << slot << " ) );" << endl; } } else if ( n.tagName() == "tabstops" ) { // setup tab order out << endl << indent << "// tab order" << endl; QString lastName; QDomElement n2 = n.firstChild().toElement(); while ( !n2.isNull() ) { if ( n2.tagName() == "tabstop" ) { QString name = n2.firstChild().toText().data(); name = registeredName( name ); if ( !lastName.isEmpty() ) out << indent << "setTabOrder( " << lastName << ", " << name << " );" << endl; lastName = name; } n2 = n2.nextSibling().toElement(); } } } // buddies bool firstBuddy = TRUE; for ( QValueList<Buddy>::Iterator buddy = buddies.begin(); buddy != buddies.end(); ++buddy ) { if ( isObjectRegistered( (*buddy).buddy ) ) { if ( firstBuddy ) { out << endl << indent << "// buddies" << endl; } out << indent << (*buddy).key << "->setBuddy( " << registeredName( (*buddy).buddy ) << " );" << endl; firstBuddy = FALSE; } } if ( extraSlots.find( "init()" ) != extraSlots.end() ) out << indent << "init();" << endl; // end of constructor out << "}" << endl; out << endl; // destructor out << "/* " << endl; out << " * Destroys the object and frees any allocated resources" << endl; out << " */" << endl; out << nameOfClass << "::~" << nameOfClass << "()" << endl; out << "{" << endl; if ( extraSlots.find( "destroy()" ) != extraSlots.end() ) out << indent << "destroy();" << endl; out << indent << "// no need to delete child widgets, Qt does it all for us" << endl; out << "}" << endl; out << endl; // handle application events if required bool needFontEventHandler = FALSE; bool needSqlTableEventHandler = FALSE; bool needSqlDataBrowserEventHandler = FALSE; nl = e.elementsByTagName( "widget" ); for ( i = 0; i < (int) nl.length(); i++ ) { if ( !DomTool::propertiesOfType( nl.item(i).toElement() , "font" ).isEmpty() ) needFontEventHandler = TRUE; QString s = getClassName( nl.item(i).toElement() ); if ( s == "QDataTable" || s == "QDataBrowser" ) { if ( !isFrameworkCodeGenerated( nl.item(i).toElement() ) ) continue; if ( s == "QDataTable" ) needSqlTableEventHandler = TRUE; if ( s == "QDataBrowser" ) needSqlDataBrowserEventHandler = TRUE; } if ( needFontEventHandler && needSqlTableEventHandler && needSqlDataBrowserEventHandler ) break; } if ( needFontEventHandler && FALSE ) { // indent = "\t"; // increase indentation for if-clause below out << "/* " << endl; out << " * Main event handler. Reimplemented to handle" << endl; out << " * application font changes"; out << " */" << endl; out << "bool " << nameOfClass << "::event( QEvent* ev )" << endl; out << "{" << endl; out << " bool ret = " << objClass << "::event( ev ); " << endl; if ( needFontEventHandler ) { indent += "\t"; out << " if ( ev->type() == QEvent::ApplicationFontChange ) {" << endl; for ( i = 0; i < (int) nl.length(); i++ ) { n = nl.item(i).toElement(); QStringList list = DomTool::propertiesOfType( n, "font" ); for ( it = list.begin(); it != list.end(); ++it ) createExclusiveProperty( n, *it ); } out << " }" << endl; indent = " "; } out << "}" << endl; out << endl; } if ( needSqlTableEventHandler || needSqlDataBrowserEventHandler ) { out << "/* " << endl; out << " * Widget polish. Reimplemented to handle" << endl; if ( needSqlTableEventHandler ) out << " * default data table initialization" << endl; if ( needSqlDataBrowserEventHandler ) out << " * default data browser initialization" << endl; out << " */" << endl; out << "void " << nameOfClass << "::polish()" << endl; out << "{" << endl; if ( needSqlTableEventHandler ) { for ( i = 0; i < (int) nl.length(); i++ ) { QString s = getClassName( nl.item(i).toElement() ); if ( s == "QDataTable" ) { n = nl.item(i).toElement(); QString c = getObjectName( n ); QString conn = getDatabaseInfo( n, "connection" ); QString tab = getDatabaseInfo( n, "table" ); if ( !( conn.isEmpty() || tab.isEmpty() ) ) { out << indent << "if ( " << c << " ) {" << endl; out << indent << indent << "QSqlCursor* cursor = " << c << "->sqlCursor();" << endl; out << indent << indent << "if ( !cursor ) {" << endl; if ( conn == "(default)" ) out << indent << indent << indent << "cursor = new QSqlCursor( \"" << tab << "\" );" << endl; else out << indent << indent << indent << "cursor = new QSqlCursor( \"" << tab << "\", TRUE, " << conn << "Connection );" << endl; out << indent << indent << indent << c << "->setSqlCursor( cursor, FALSE, TRUE );" << endl; out << indent << indent << "}" << endl; out << indent << indent << "if ( !cursor->isActive() )" << endl; out << indent << indent << indent << c << "->refresh( QDataTable::RefreshAll );" << endl; out << indent << "}" << endl; } } } } if ( needSqlDataBrowserEventHandler ) { nl = e.elementsByTagName( "widget" ); for ( i = 0; i < (int) nl.length(); i++ ) { QString s = getClassName( nl.item(i).toElement() ); if ( s == "QDataBrowser" ) { QString obj = getObjectName( nl.item(i).toElement() ); QString tab = getDatabaseInfo( nl.item(i).toElement(), "table" ); QString conn = getDatabaseInfo( nl.item(i).toElement(), "connection" ); if ( !(tab).isEmpty() ) { out << indent << "if ( " << obj << " ) {" << endl; out << indent << indent << "if ( !" << obj << "->sqlCursor() ) {" << endl; if ( conn == "(default)" ) out << indent << indent << indent << "QSqlCursor* cursor = new QSqlCursor( \"" << tab << "\" );" << endl; else out << indent << indent << indent << "QSqlCursor* cursor = new QSqlCursor( \"" << tab << "\", TRUE, " << conn << "Connection );" << endl; out << indent << indent << indent << obj << "->setSqlCursor( cursor, TRUE );" << endl; out << indent << indent << indent << obj << "->refresh();" << endl; out << indent << indent << indent << obj << "->first();" << endl; out << indent << indent << "}" << endl; out << indent << "}" << endl; } } } } out << indent << objClass << "::polish();" << endl; out << "}" << endl; out << endl; } // create stubs for additional slots if necessary if ( !extraSlots.isEmpty() && writeSlotImpl ) { QStringList::Iterator it2; for ( it = extraSlots.begin(), it2 = extraSlotTypes.begin(); it != extraSlots.end(); ++it, ++it2 ) { QString type = *it2; if ( type.isEmpty() ) type = "void"; out << type << " " << nameOfClass << "::" << (*it) << endl; bool createWarning = TRUE; QString fname = Parser::cleanArgs( *it ); QMap<QString, QString>::Iterator fit = functionImpls.find( fname ); if ( fit != functionImpls.end() ) { int begin = (*fit).find( "{" ); QString body = (*fit).mid( begin + 1, (*fit).find( "}" ) - begin - 1 ); createWarning = body.simplifyWhiteSpace().isEmpty(); if ( !createWarning ) out << *fit << endl; } if ( createWarning ) { out << "{" << endl; if ( *it != "init()" && *it != "destroy()" ) out << " qWarning( \"" << nameOfClass << "::" << (*it) << ": Not implemented yet!\" );" << endl; out << "}" << endl; } out << endl; } }}/*! Creates form support implementation code for the widgets given in \a e. Traverses recursively over all children. */void Uic::createFormImpl( const QDomElement& e, const QString& form, const QString& connection, const QString& table ){ if ( e.tagName() == "widget" && e.attribute( "class" ) != "QDataTable" ) { QString field = getDatabaseInfo( e, "field" ); if ( !field.isEmpty() ) { if ( isWidgetInTable( e, connection, table ) ) out << indent << form << "Form->insert( " << getObjectName( e ) << ", " << fixString( field ) << " );" << endl; } } QDomElement n; for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { createFormImpl( n, form, connection, table ); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?