qwidgetfactory.cpp

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

CPP
1,717
字号
    widgetFactories.append( factory );}/*!  Creates a widget of the type \a className passing \a parent and  \a name to its constructor.  If \a className is a widget in the Qt library, it is directly  created by this function. If the widget isn't in the Qt library,  each of the installed widget plugins is asked, in turn, to create  the widget. As soon as a plugin says it can create the widget it is  asked to do so. It may occur that none of the plugins can create the  widget, in which case each installed widget factory is asked to  create the widget (see addWidgetFactory()). If the widget cannot be  created by any of these means, 0 is returned.  If you have a custom widget, and want it to be created using the  widget factory, there are two approaches you can use:  \list 1  \i Write a widget plugin. This allows you to use the  widget in \e{Qt Designer} and in this QWidgetFactory. See the widget  plugin documentation for further details. (See the \link  designer-manual-6.book#creatingplugins Creating Custom Widgets with  Plugins\endlink section of the \link designer-manual.book Qt  Designer manual\endlink for an example.  \i Subclass QWidgetFactory. Then reimplement this function to create  and return an instance of your custom widget if \a className equals  the name of your widget, otherwise return 0. Then at the beginning  of your program where you want to use the widget factory to create  widgets do a:  \code  QWidgetFactory::addWidgetFactory( new MyWidgetFactory );  \endcode  where MyWidgetFactory is your QWidgetFactory subclass.  \endlist*/QWidget *QWidgetFactory::createWidget( const QString &className, QWidget *parent, const char *name ) const{    // create widgets we know    if ( className == "QPushButton" ) {	return new QPushButton( parent, name );    } else if ( className == "QToolButton" ) {	return new QToolButton( parent, name );    } else if ( className == "QCheckBox" ) {	return new QCheckBox( parent, name );    } else if ( className == "QRadioButton" ) {	return new QRadioButton( parent, name );    } else if ( className == "QGroupBox" ) {	return new QGroupBox( parent, name );    } else if ( className == "QButtonGroup" ) {	return new QButtonGroup( parent, name );    } else if ( className == "QIconView" ) {#if !defined(QT_NO_ICONVIEW)	return new QIconView( parent, name );#endif    } else if ( className == "QTable" ) {#if !defined(QT_NO_TABLE)	return new QTable( parent, name );#endif    } else if ( className == "QListBox" ) {	return new QListBox( parent, name );    } else if ( className == "QListView" ) {	return new QListView( parent, name );    } else if ( className == "QLineEdit" ) {	return new QLineEdit( parent, name );    } else if ( className == "QSpinBox" ) {	return new QSpinBox( parent, name );    } else if ( className == "QMultiLineEdit" ) {	return new QMultiLineEdit( parent, name );    } else if ( className == "QLabel"  || className == "TextLabel" || className == "PixmapLabel" ) {	return new QLabel( parent, name );    } else if ( className == "QLayoutWidget" ) {	return new QWidget( parent, name );    } else if ( className == "QTabWidget" ) {	return new QTabWidget( parent, name );    } else if ( className == "QComboBox" ) {	return new QComboBox( FALSE, parent, name );    } else if ( className == "QWidget" ) {	if ( !qwf_stays_on_top )	    return new QWidget( parent, name );	return new QWidget( parent, name, Qt::WStyle_StaysOnTop );    } else if ( className == "QDialog" ) {	if ( !qwf_stays_on_top )	    return new QDialog( parent, name );	return new QDialog( parent, name, FALSE, Qt::WStyle_StaysOnTop );    } else if ( className == "QWizard" ) {	return  new QWizard( parent, name );    } else if ( className == "QLCDNumber" ) {	return new QLCDNumber( parent, name );    } else if ( className == "QProgressBar" ) {	return new QProgressBar( parent, name );    } else if ( className == "QTextView" ) {	return new QTextView( parent, name );    } else if ( className == "QTextBrowser" ) {	return new QTextBrowser( parent, name );    } else if ( className == "QDial" ) {	return new QDial( parent, name );    } else if ( className == "QSlider" ) {	return new QSlider( parent, name );    } else if ( className == "QFrame" ) {	return new QFrame( parent, name );    } else if ( className == "QSplitter" ) {	return new QSplitter( parent, name );    } else if ( className == "Line" ) {	QFrame *f = new QFrame( parent, name );	f->setFrameStyle( QFrame::HLine | QFrame::Sunken );	return f;    } else if ( className == "QTextEdit" ) {	return new QTextEdit( parent, name );    } else if ( className == "QDateEdit" ) {	return new QDateEdit( parent, name );    } else if ( className == "QTimeEdit" ) {	return new QTimeEdit( parent, name );    } else if ( className == "QDateTimeEdit" ) {	return new QDateTimeEdit( parent, name );    } else if ( className == "QScrollBar" ) {	return new QScrollBar( parent, name );    } else if ( className == "QPopupMenu" ) {	return new QPopupMenu( parent, name );    } else if ( className == "QWidgetStack" ) {	return new QWidgetStack( parent, name );    } else if ( className == "QMainWindow" ) {	QMainWindow *mw = 0;	if ( !qwf_stays_on_top )	    mw = new QMainWindow( parent, name );	else	    mw = new QMainWindow( parent, name, Qt::WType_TopLevel | Qt::WStyle_StaysOnTop );	mw->setCentralWidget( new QWidget( mw, "qt_central_widget" ) );	mw->centralWidget()->show();	(void)mw->statusBar();	return mw;    }#if !defined(QT_NO_SQL)    else if ( className == "QDataTable" ) {	return new QDataTable( parent, name );    } else if ( className == "QDataBrowser" ) {	return new QDesignerDataBrowser2( parent, name );    } else if ( className == "QDataView" ) {	return new QDesignerDataView2( parent, name );    }#endif    // try to create it using the loaded widget plugins    if ( !widgetInterfaceManager )	widgetInterfaceManager = new QPluginManager<WidgetInterface>( IID_Widget, QApplication::libraryPaths(), "/designer" );    QInterfacePtr<WidgetInterface> iface = 0;    widgetInterfaceManager->queryInterface( className, &iface );    if ( iface ) {	QWidget *w = iface->create( className, parent, name );	if ( w )	    return w;    }    // hope we have a factory which can do it    for ( QWidgetFactory* f = widgetFactories.first(); f; f = widgetFactories.next() ) {	QWidget *w = f->createWidget( className, parent, name );	if ( w )	    return w;    }    // no success    return 0;}QWidget *QWidgetFactory::createWidgetInternal( const QDomElement &e, QWidget *parent, QLayout* layout, const QString &classNameArg ){    lastItem = 0;    QDomElement n = e.firstChild().toElement();    QWidget *w = 0; // the widget that got created    QObject *obj = 0; // gets the properties    QString className = classNameArg;    int row = e.attribute( "row" ).toInt();    int col = e.attribute( "column" ).toInt();    int rowspan = e.attribute( "rowspan" ).toInt();    int colspan = e.attribute( "colspan" ).toInt();    if ( rowspan < 1 )	rowspan = 1;    if ( colspan < 1 )	colspan = 1;    if ( !className.isEmpty() ) {	if ( !layout && className  == "QLayoutWidget" )	    className = "QWidget";	if ( layout && className == "QLayoutWidget" ) {	    // hide layout widgets	    w = parent;	} else {	    obj = QWidgetFactory::createWidget( className, parent, 0 );	    if ( !obj )		return 0;	    w = (QWidget*)obj;	    if ( !toplevel )		toplevel = w;	    if ( w->inherits( "QMainWindow" ) )		w = ( (QMainWindow*)w )->centralWidget();	    if ( layout ) {		switch( layoutType( layout ) ) {		case HBox:		    ( (QHBoxLayout*)layout )->addWidget( w );		    break;		case VBox:		    ( (QVBoxLayout*)layout )->addWidget( w );		    break;		case Grid:		    ( (QGridLayout*)layout )->addMultiCellWidget( w, row, row + rowspan - 1,								  col, col + colspan - 1 );		    break;		default:		    break;		}	    }	    layout = 0;	}    }    while ( !n.isNull() ) {	if ( n.tagName() == "spacer" ) {	    createSpacer( n, layout );	} else if ( n.tagName() == "widget" ) {	    QMap< QString, QString> *oldDbControls = dbControls;	    createWidgetInternal( n, w, layout, n.attribute( "class", "QWidget" ) );	    dbControls = oldDbControls;	} else if ( n.tagName() == "hbox" ) {	    QLayout *parentLayout = layout;	    if ( layout && layout->inherits( "QGridLayout" ) )		layout = createLayout( 0, 0, QWidgetFactory::HBox );	    else		layout = createLayout( w, layout, QWidgetFactory::HBox );	    obj = layout;	    n = n.firstChild().toElement();	    if ( parentLayout && parentLayout->inherits( "QGridLayout" ) )		( (QGridLayout*)parentLayout )->addMultiCellLayout( layout, row, row + rowspan - 1, col, col + colspan - 1 );	    continue;	} else if ( n.tagName() == "grid" ) {	    QLayout *parentLayout = layout;	    if ( layout && layout->inherits( "QGridLayout" ) )		layout = createLayout( 0, 0, QWidgetFactory::Grid );	    else		layout = createLayout( w, layout, QWidgetFactory::Grid );	    obj = layout;	    n = n.firstChild().toElement();	    if ( parentLayout && parentLayout->inherits( "QGridLayout" ) )		( (QGridLayout*)parentLayout )->addMultiCellLayout( layout, row, row + rowspan - 1, col, col + colspan - 1 );	    continue;	} else if ( n.tagName() == "vbox" ) {	    QLayout *parentLayout = layout;	    if ( layout && layout->inherits( "QGridLayout" ) )		layout = createLayout( 0, 0, QWidgetFactory::VBox );	    else		layout = createLayout( w, layout, QWidgetFactory::VBox );	    obj = layout;	    n = n.firstChild().toElement();	    if ( parentLayout && parentLayout->inherits( "QGridLayout" ) )		( (QGridLayout*)parentLayout )->addMultiCellLayout( layout, row, row + rowspan - 1, col, col + colspan - 1 );	    continue;	} else if ( n.tagName() == "property" && obj ) {	    setProperty( obj, n.attribute( "name" ), n.firstChild().toElement() );	} else if ( n.tagName() == "attribute" && w ) {	    QString attrib = n.attribute( "name" );	    QVariant v = DomTool::elementToVariant( n.firstChild().toElement(), QVariant() );	    if ( parent->inherits( "QTabWidget" ) ) {		if ( attrib == "title" )		    ( (QTabWidget*)parent )->insertTab( w, v.toString() );	    } else if ( parent->inherits( "QWizard" ) ) {		if ( attrib == "title" )		    ( (QWizard*)parent )->addPage( w, v.toString() );	    }	} else if ( n.tagName() == "item" ) {	    createItem( n, w );	} else if ( n.tagName() == "column" || n.tagName() == "row" ) {	    createColumn( n, w );	}	n = n.nextSibling().toElement();    }    return w;}QLayout *QWidgetFactory::createLayout( QWidget *widget, QLayout*  layout, LayoutType type ){    int spacing = defSpacing;    int margin = defMargin;    if ( !layout && widget && widget->inherits( "QTabWidget" ) )	widget = ((QTabWidget*)widget)->currentPage();    if ( !layout && widget && widget->inherits( "QWizard" ) )	widget = ((QWizard*)widget)->currentPage();    if ( !layout && widget && widget->inherits( "QWidgetStack" ) )	widget = ((QWidgetStack*)widget)->visibleWidget();    if ( !layout && widget && widget->inherits( "QGroupBox" ) ) {	QGroupBox *gb = (QGroupBox*)widget;	gb->setColumnLayout( 0, Qt::Vertical );	gb->layout()->setMargin( 0 );	gb->layout()->setSpacing( 0 );	QLayout *l;	switch ( type ) {	case HBox:	    l = new QHBoxLayout( gb->layout() );	    l->setAlignment( Qt::AlignTop );	    return l;	case VBox:	    l = new QVBoxLayout( gb->layout(), spacing );	    l->setAlignment( Qt::AlignTop );	    return l;	case Grid:	    l = new QGridLayout( gb->layout() );	    l->setAlignment( Qt::AlignTop );	    return l;	default:	    return 0;	}    } else {	if ( layout ) {	    QLayout *l;	    switch ( type ) {	    case HBox:		l = new QHBoxLayout( layout );		l->setSpacing( spacing );		l->setMargin( margin );		return l;	    case VBox:		l = new QVBoxLayout( layout );		l->setSpacing( spacing );		l->setMargin( margin );		return l;	    case Grid: {		l = new QGridLayout( layout );		l->setSpacing( spacing );		l->setMargin( margin );		return l;	    }	    default:		return 0;	    }	} else {	    QLayout *l;	    switch ( type ) {	    case HBox:		l = new QHBoxLayout( widget );		if ( !widget ) {		    l->setMargin( margin );		    l->setSpacing( margin );		}		return l;	    case VBox:		l = new QVBoxLayout( widget );		if ( !widget ) {		    l->setMargin( margin );		    l->setSpacing( margin );		}		return l;	    case Grid: {		l = new QGridLayout( widget );		if ( !widget ) {		    l->setMargin( margin );		    l->setSpacing( margin );		}		return l;	    }	    default:		return 0;	    }	}    }}QWidgetFactory::LayoutType QWidgetFactory::layoutType( QLayout *layout ) const{    if ( layout->inherits( "QHBoxLayout" ) )	return HBox;    else if ( layout->inherits( "QVBoxLayout" ) )	return VBox;    else if ( layout->inherits( "QGridLayout" ) )	return Grid;    return NoLayout;}void QWidgetFactory::setProperty( QObject* obj, const QString &prop, const QDomElement &e ){    const QMetaProperty *p = obj->metaObject()->property( obj->metaObject()->findProperty( prop, TRUE ), TRUE );    QVariant defVariant;    if ( e.tagName() == "font" ) {	QFont f( qApp->font() );	if ( obj->isWidgetType() && ( (QWidget*)obj )->parentWidget() )	    f = ( (QWidget*)obj )->parentWidget()->font();	defVariant = QVariant( f );    }    QString comment;    QVariant v( DomTool::elementToVariant( e, defVariant, comment ) );    if ( e.tagName() == "pixmap" ) {	QPixmap pix = loadPixmap( e );	if ( pix.isNull() )	    return;	v = QVariant( pix );    } else if ( e.tagName() == "iconset" ) {	QPixmap pix = loadPixmap( e );	if ( pix.isNull() )	    return;	v = QVariant( QIconSet( pix ) );    } else if ( e.tagName() == "image" ) {	v = QVariant( loadFromCollection( v.toString() ) );    } else if ( e.tagName() == "string" ) {	v = QVariant( translate( v.asString(), comment ) );    }    if ( !p ) {	if ( obj->isWidgetType() ) {	    if ( prop == "toolTip" ) {		if ( !v.toString().isEmpty() )		    QToolTip::add( (QWidget*)obj, v.toString() );	    } else if ( prop == "whatsThis" ) {		if ( !v.toString().isEmpty() )		    QWhatsThis::add( (QWidget*)obj, v.toString() );	    }

⌨️ 快捷键说明

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