qwidgetfactory.cpp

来自「qt-x11-free-3.0.3.tar.gz minigui图形界面工具」· C++ 代码 · 共 1,717 行 · 第 1/4 页

CPP
1,717
字号
#endif		table->setNumCols( table->numCols() + 1 );	}	QDomElement n = e.firstChild().toElement();	QPixmap pix;	bool hasPixmap = FALSE;	QString txt;	QString field;	QValueList<Field> fieldMap;	if ( fieldMaps.find( table ) != fieldMaps.end() ) {	    fieldMap = *fieldMaps.find( table );	    fieldMaps.remove( table );	}	while ( !n.isNull() ) {	    if ( n.tagName() == "property" ) {		QString attrib = n.attribute( "name" );		QVariant v = DomTool::elementToVariant( n.firstChild().toElement(), QVariant() );		if ( attrib == "text" )		    txt = v.toString();		else if ( attrib == "pixmap" ) {		    hasPixmap = !n.firstChild().firstChild().toText().data().isEmpty();		    if ( hasPixmap )			pix = loadPixmap( n.firstChild().toElement().toElement() );		} else if ( attrib == "field" )		    field = v.toString();	    }	    n = n.nextSibling().toElement();	}	int i = isRow ? table->numRows() - 1 : table->numCols() - 1;	QHeader *h = !isRow ? table->horizontalHeader() : table->verticalHeader();	if ( hasPixmap ) {#ifndef QT_NO_SQL	    if ( isSql )		((QDataTable*)table)->addColumn( field, txt, -1, pix );	    else#endif		h->setLabel( i, pix, txt );	} else {#ifndef QT_NO_SQL	    if ( isSql ) {		((QDataTable*)table)->addColumn( field, txt );	    }	    else#endif		h->setLabel( i, txt );	}	if ( !isRow && !field.isEmpty() ) {	    fieldMap.append( Field( txt, (hasPixmap ? pix : QPixmap()), field ) );	    fieldMaps.insert( table, fieldMap );	}    }#endif}void QWidgetFactory::loadItem( const QDomElement &e, QPixmap &pix, QString &txt, bool &hasPixmap ){    QDomElement n = e;    hasPixmap = FALSE;    while ( !n.isNull() ) {	if ( n.tagName() == "property" ) {	    QString attrib = n.attribute( "name" );	    QVariant v = DomTool::elementToVariant( n.firstChild().toElement(), QVariant() );	    if ( attrib == "text" )		txt = v.toString();	    else if ( attrib == "pixmap" ) {		pix = loadPixmap( n.firstChild().toElement() );		hasPixmap = !pix.isNull();	    }	}	n = n.nextSibling().toElement();    }}void QWidgetFactory::createItem( const QDomElement &e, QWidget *widget, QListViewItem *i ){    if ( widget->inherits( "QListBox" ) || widget->inherits( "QComboBox" ) ) {	QDomElement n = e.firstChild().toElement();	QPixmap pix;	bool hasPixmap = FALSE;	QString txt;	loadItem( n, pix, txt, hasPixmap );	QListBox *lb = 0;	if ( widget->inherits( "QListBox" ) )	    lb = (QListBox*)widget;	else	    lb = ( (QComboBox*)widget)->listBox();	if ( hasPixmap ) {	    new QListBoxPixmap( lb, pix, txt );	} else {	    new QListBoxText( lb, txt );	}#ifndef QT_NO_ICONVIEW    } else if ( widget->inherits( "QIconView" ) ) {	QDomElement n = e.firstChild().toElement();	QPixmap pix;	bool hasPixmap = FALSE;	QString txt;	loadItem( n, pix, txt, hasPixmap );	QIconView *iv = (QIconView*)widget;	new QIconViewItem( iv, txt, pix );#endif    } else if ( widget->inherits( "QListView" ) ) {	QDomElement n = e.firstChild().toElement();	QPixmap pix;	QValueList<QPixmap> pixmaps;	QStringList textes;	QListViewItem *item = 0;	QListView *lv = (QListView*)widget;	if ( i )	    item = new QListViewItem( i, lastItem );	else	    item = new QListViewItem( lv, lastItem );	while ( !n.isNull() ) {	    if ( n.tagName() == "property" ) {		QString attrib = n.attribute( "name" );		QVariant v = DomTool::elementToVariant( n.firstChild().toElement(), QVariant() );		if ( attrib == "text" )		    textes << v.toString();		else if ( attrib == "pixmap" ) {		    QString s = v.toString();		    if ( s.isEmpty() ) {			pixmaps << QPixmap();		    } else {			pix = loadPixmap( n.firstChild().toElement() );			pixmaps << pix;		    }		}	    } else if ( n.tagName() == "item" ) {		item->setOpen( TRUE );		createItem( n, widget, item );	    }	    n = n.nextSibling().toElement();	}	for ( int i = 0; i < lv->columns(); ++i ) {	    item->setText( i, textes[ i ] );	    item->setPixmap( i, pixmaps[ i ] );	}	lastItem = item;    }}void QWidgetFactory::loadChildAction( QObject *parent, const QDomElement &e ){    QDomElement n = e;    QAction *a = 0;    EventFunction ef;    if ( n.tagName() == "action" ) {	a = new QAction( parent );	QDomElement n2 = n.firstChild().toElement();	while ( !n2.isNull() ) {	    if ( n2.tagName() == "property" ) {		setProperty( a, n2.attribute( "name" ), n2.firstChild().toElement() );	    } else if ( n2.tagName() == "event" ) {		ef.events.append( n2.attribute( "name" ) );		ef.functions.append( QStringList::split( ',', n2.attribute( "functions" ) ) );	    }	    n2 = n2.nextSibling().toElement();	}	if ( !parent->inherits( "QAction" ) )	    actionList.append( a );    } else if ( n.tagName() == "actiongroup" ) {	a = new QActionGroup( parent );	QDomElement n2 = n.firstChild().toElement();	while ( !n2.isNull() ) {	    if ( n2.tagName() == "property" ) {		setProperty( a, n2.attribute( "name" ), n2.firstChild().toElement() );	    } else if ( n2.tagName() == "action" ||			n2.tagName() == "actiongroup" ) {		loadChildAction( a, n2 );	    } else if ( n2.tagName() == "event" ) {		ef.events.append( n2.attribute( "name" ) );		ef.functions.append( QStringList::split( ',', n2.attribute( "functions" ) ) );	    }	    n2 = n2.nextSibling().toElement();	}	if ( !parent->inherits( "QAction" ) )	    actionList.append( a );    }    if ( a )	eventMap.insert( a, ef );}void QWidgetFactory::loadActions( const QDomElement &e ){    QDomElement n = e.firstChild().toElement();    while ( !n.isNull() ) {	if ( n.tagName() == "action" ) {	    loadChildAction( toplevel, n );	} else if ( n.tagName() == "actiongroup" ) {	    loadChildAction( toplevel, n );	}	n = n.nextSibling().toElement();    }}void QWidgetFactory::loadToolBars( const QDomElement &e ){    QDomElement n = e.firstChild().toElement();    QMainWindow *mw = ( (QMainWindow*)toplevel );    QToolBar *tb = 0;    while ( !n.isNull() ) {	if ( n.tagName() == "toolbar" ) {	    Qt::Dock dock = (Qt::Dock)n.attribute( "dock" ).toInt();	    tb = new QToolBar( QString::null, mw, dock );	    tb->setLabel( n.attribute( "label" ) );	    tb->setName( n.attribute( "name" ) );	    QDomElement n2 = n.firstChild().toElement();	    while ( !n2.isNull() ) {		if ( n2.tagName() == "action" ) {		    QAction *a = findAction( n2.attribute( "name" ) );		    if ( a )			a->addTo( tb );		} else if ( n2.tagName() == "separator" ) {		    tb->addSeparator();		} else if ( n2.tagName() == "widget" ) {		    (void)createWidgetInternal( n2, tb, 0, n2.attribute( "class", "QWidget" ) );		} else if ( n2.tagName() == "property" ) {		    setProperty( tb, n2.attribute( "name" ), n2.firstChild().toElement() );		}		n2 = n2.nextSibling().toElement();	    }	}	n = n.nextSibling().toElement();    }}void QWidgetFactory::loadMenuBar( const QDomElement &e ){    QDomElement n = e.firstChild().toElement();    QMainWindow *mw = ( (QMainWindow*)toplevel );    QMenuBar *mb = mw->menuBar();    while ( !n.isNull() ) {	if ( n.tagName() == "item" ) {	    QPopupMenu *popup = new QPopupMenu( mw );	    popup->setName( n.attribute( "name" ) );	    QDomElement n2 = n.firstChild().toElement();	    while ( !n2.isNull() ) {		if ( n2.tagName() == "action" ) {		    QAction *a = findAction( n2.attribute( "name" ) );		    if ( a )			a->addTo( popup );		} else if ( n2.tagName() == "separator" ) {		    popup->insertSeparator();		}		n2 = n2.nextSibling().toElement();	    }	    mb->insertItem( translate( n.attribute( "text" ) ), popup );	} else if ( n.tagName() == "property" ) {	    setProperty( mb, n.attribute( "name" ), n.firstChild().toElement() );	}	n = n.nextSibling().toElement();    }}// compatibility with early 3.0 betasvoid QWidgetFactory::loadFunctions( const QDomElement &e ){    QDomElement n = e.firstChild().toElement();    QMap<QString, QString> bodies;    QString s;    if ( !interpreterInterfaceManager )	interpreterInterfaceManager =	    new QPluginManager<InterpreterInterface>( IID_Interpreter, QApplication::libraryPaths(), "/designer" );    while ( !n.isNull() ) {	if ( n.tagName() == "function" ) {	    QString name = n.attribute( "name" );	    int pos = name.find( "(" );	    QString ident = name.left( pos );	    QMap<QString, QString>::Iterator it = languageSlots.find( ident );	    if ( it != languageSlots.end() ) {		InterpreterInterface *interpreterInterface = 0;		interpreterInterfaceManager->queryInterface( *it, &interpreterInterface );		Functions *funcs = 0;		QMap<QString, Functions*>::Iterator fit = languageFunctions.find( *it );		if ( fit == languageFunctions.end() ) {		    funcs = new Functions;		    languageFunctions.insert( *it, funcs );		} else {		    funcs = *fit;		}		QString body = n.firstChild().toText().data();		if ( interpreterInterface ) {		    QString s = interpreterInterface->createFunctionDeclaration( name, body );		    funcs->functions += s;		    if ( qwf_language && *qwf_language == *it ) {			if ( !qwf_functions )			    qwf_functions = new QMap<QWidget*, QString>;			if ( !qwf_forms )			    qwf_forms = new QMap<QWidget*, QString>;			(*(qwf_functions))[ toplevel ].append( s );		    }		    interpreterInterface->release();		}	    }	}	n = n.nextSibling().toElement();    }}QAction *QWidgetFactory::findAction( const QString &name ){    for ( QAction *a = actionList.first(); a; a = actionList.next() ) {	if ( QString( a->name() ) == name )	    return a;	QAction *ac = (QAction*)a->child( name.latin1(), "QAction" );	if ( ac )	    return ac;    }    return 0;}/*!    If you use a pixmap collection (which is the default for new    projects) rather than saving the pixmaps    within the .ui XML file, you must load the   pixmap collection. QWidgetFactory looks in the default   QMimeSourceFactory for the pixmaps. Either add it there   manually, or call this function and specify the directory where   the images can be found, as \a dir. This is normally the directory   called \c images in the project's directory.*/void QWidgetFactory::loadImages( const QString &dir ){    QDir d( dir );    QStringList l = d.entryList( QDir::Files );    for ( QStringList::Iterator it = l.begin(); it != l.end(); ++it )	QMimeSourceFactory::defaultFactory()->setPixmap( *it, QPixmap( d.path() + "/" + *it, "PNG" ) );}void QWidgetFactory::loadExtraSource(){    if ( !qwf_language || !languageInterfaceManager )	return;    QString lang = *qwf_language;    LanguageInterface *iface = 0;    languageInterfaceManager->queryInterface( lang, &iface );    if ( !iface )	return;    QValueList<LanguageInterface::Function> functions;    QStringList forwards;    QStringList includesImpl;    QStringList includesDecl;    QStringList vars;    QValueList<LanguageInterface::Connection> connections;    iface->loadFormCode( toplevel->name(), qwf_currFileName + iface->formCodeExtension(),			 functions, forwards, includesImpl, includesDecl, vars, connections );    for ( QValueList<LanguageInterface::Connection>::Iterator cit = connections.begin();	  cit != connections.end(); ++cit ) {	QObject *sender  = 0;	QString name = (*cit).sender;	if ( name == "this" || qstrcmp( toplevel->name(), name ) == 0 ) {	    sender = toplevel;	} else {	    if ( name == "this" )		name = toplevel->name();	    QObjectList *l = toplevel->queryList( 0, name, FALSE );	    if ( l ) {		if ( l->first() )		    sender = l->first();		delete l;	    }	}	if ( !sender )	    sender = findAction( name );	EventFunction ef = eventMap[ sender ];	ef.events.append( (*cit).signal );	ef.functions.append( QStringList::split( ',', (*cit).slot ) );	eventMap.replace( sender, ef );    }    if ( interpreterInterfaceManager ) {	InterpreterInterface *interpreterInterface = 0;	interpreterInterfaceManager->queryInterface( lang, &interpreterInterface );	if ( interpreterInterface ) {	    Functions *funcs = 0;	    QMap<QString, Functions*>::Iterator fit = languageFunctions.find( lang );	    if ( fit == languageFunctions.end() ) {		funcs = new Functions;		languageFunctions.insert( lang, funcs );	    } else {		funcs = *fit;	    }	    for ( QValueList<LanguageInterface::Function>::Iterator fit2 = functions.begin();		  fit2 != functions.end(); ++fit2 ) {		languageSlots.insert( (*fit2).name.left( (*fit2).name.find( '(' ) ), lang );		QString s;		QString comments = (*fit2).comments;		if ( !comments.isEmpty() )		    s += comments + "\n";		 s += interpreterInterface->createFunctionDeclaration( (*fit2).name, (*fit2).body );		funcs->functions += s;		if ( !qwf_functions )		    qwf_functions = new QMap<QWidget*, QString>;		if ( !qwf_forms )		    qwf_forms = new QMap<QWidget*, QString>;		(*(qwf_functions))[ toplevel ].append( s );	    }	    interpreterInterface->release();	}    }    QStringList::Iterator vit;    for ( vit = vars.begin(); vit != vars.end(); ++vit )	variables << *vit;}QString QWidgetFactory::translate( const QString& sourceText, const QString& comment ){    return qApp->translate( toplevel->name(), sourceText.utf8(),			    comment.utf8(), QApplication::UnicodeUTF8 );}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?