⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 widgetfactory.cpp

📁 Qt/Embedded是一个多平台的C++图形用户界面应用程序框架
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/************************************************************************ 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 <qvariant.h> // HP-UX compiler need this here#include "widgetfactory.h"#include <widgetdatabase.h>#include "metadatabase.h"#include "mainwindow.h"#include "formwindow.h"#include "pixmapchooser.h"#include "layout.h"#include "listboxeditorimpl.h"#include "listvieweditorimpl.h"#include "iconvieweditorimpl.h"#include "formwindow.h"#include "multilineeditorimpl.h"#include "../integration/kdevelop/kdewidgets.h"#include <qmodules.h>#include <qpixmap.h>#include <qgroupbox.h>#include <qiconview.h>#if defined(QT_MODULE_TABLE)#include <qtable.h>#endif#include <qlineedit.h>#include <qspinbox.h>#include <qmultilineedit.h>#include <qlabel.h>#include <qlayout.h>#include <qwidgetstack.h>#include <qcombobox.h>#include <qtabbar.h>#include <qlistbox.h>#include <qlistview.h>#include <qobjectlist.h>#include <qlcdnumber.h>#include <qslider.h>#include <qdial.h>#include <qprogressbar.h>#include <qtextview.h>#include <qtextbrowser.h>#include <qframe.h>#include <qmetaobject.h>#include <qwidgetstack.h>#include <qwizard.h>#include <qvaluelist.h>#include <qtimer.h>#include <globaldefs.h>void QLayoutWidget::paintEvent( QPaintEvent* ){    QPainter p ( this );    p.setPen( red );    p.drawRect( rect() );}int QDesignerTabWidget::currentPage() const{    return tabBar()->currentTab();}void QDesignerTabWidget::setCurrentPage( int i ){    tabBar()->setCurrentTab( i );}QString QDesignerTabWidget::pageTitle() const{    return ((QTabWidget*)this)->tabLabel( QTabWidget::currentPage() );}void QDesignerTabWidget::setPageTitle( const QString& title ){    changeTab( QTabWidget::currentPage(), title  );}void QDesignerTabWidget::setPageName( const QCString& name ){    if ( QTabWidget::currentPage() )	QTabWidget::currentPage()->setName( name );}QCString QDesignerTabWidget::pageName() const{    if ( !QTabWidget::currentPage() )	return 0;    return QTabWidget::currentPage()->name();}int QDesignerTabWidget::count() const{    return tabBar()->count();}int QDesignerWizard::currentPageNum() const{    for ( int i = 0; i < pageCount(); ++i ) {	if ( page( i ) == currentPage() )	    return i;    }    return 0;}void QDesignerWizard::setCurrentPage( int i ){    if ( i < currentPageNum() ) {	while ( i < currentPageNum() ) {	    if ( currentPageNum() == 0 )		break;	    back();	}	    } else {	while ( i > currentPageNum() ) {	    if ( currentPageNum() == pageCount() - 1 )		break;	    next();	}    }}QString QDesignerWizard::pageTitle() const{    return title( currentPage() );}void QDesignerWizard::setPageTitle( const QString& title ){    setTitle( currentPage(), title );}void QDesignerWizard::setPageName( const QCString& name ){    if ( QWizard::currentPage() )	QWizard::currentPage()->setName( name );}QCString QDesignerWizard::pageName() const{    if ( !QWizard::currentPage() )	return 0;    return QWizard::currentPage()->name();}int QDesignerWizard::pageNum( QWidget *p ){    for ( int i = 0; i < pageCount(); ++i ) {	if ( page( i ) == p )	    return i;    }    return -1;}void QDesignerWizard::addPage( QWidget *p, const QString &t ){    QWizard::addPage( p, t );    if ( removedPages.find( p ) )	removedPages.remove( p );}void QDesignerWizard::removePage( QWidget *p ){    QWizard::removePage( p );    removedPages.insert( p, p );}void QDesignerWizard::insertPage( QWidget *p, const QString &t, int index ){    if ( removedPages.find( p ) )	removedPages.remove( p );    if ( index == pageCount() ) {	addPage( p, t );	return;    }    QList<Page> pages;    pages.setAutoDelete( TRUE );    setCurrentPage( pageCount() - 1 );    while ( pageCount() ) {	QWidget *w = currentPage();	pages.insert( 0, new Page( w, title( w ) ) );	back();	removePage( w );    }    int i = 0;    for ( Page *pg = pages.first(); pg; pg = pages.next(), ++i ) {	if ( i == index ) {	    addPage( p, t );	    next();	}	addPage( pg->p, pg->t );	next();    }}QMap< int, QMap< QString, QVariant> > *defaultProperties = 0;QMap< int, QStringList > *changedProperties = 0;/*!  \class WidgetFactory widgetfactory.h  \brief Set of static functions for creating widgets, layouts and do other stuff  The widget factory offers functions to create widgets, create and  delete layouts find out other details - all based on the  WidgetDatabase's data. So the functions that use ids use the same  ids as in the WidgetDatabase.*/static void saveDefaultProperties( QWidget *w, int id ){    QMap< QString, QVariant> propMap;    QStrList lst = w->metaObject()->propertyNames( TRUE );    for ( uint i = 0; i < lst.count(); ++i )	propMap.insert( lst.at( i ), w->property( lst.at( i ) ) );    defaultProperties->insert( id, propMap );}static void saveChangedProperties( QWidget *w, int id ){    QStringList l = MetaDataBase::changedProperties( w );    changedProperties->insert( id, l );}/*!  Creates a widget of the type which is registered as \a id as  child of \a parent. The \a name is optional. If \a init is TRUE, the  widget is initialized with some defaults, else the plain widget is  created.*/QWidget *WidgetFactory::create( int id, QWidget *parent, const char *name, bool init, const QRect *r, Qt::Orientation orient ){    QString n = WidgetDatabase::className( id );    if ( n.isEmpty() )	return 0;    if ( !defaultProperties ) {	defaultProperties = new QMap< int, QMap< QString, QVariant> >();	changedProperties = new QMap< int, QStringList >();    }    QWidget *w = 0;    if ( !WidgetDatabase::isCustomWidget( id ) )	w = createWidget( n, parent, name ? name : (const char*)WidgetDatabase::createWidgetName( id ).data(), init, r, orient );    else	w = createCustomWidget( parent, name ? name : (const char*)WidgetDatabase::createWidgetName( id ).data(),				MetaDataBase::customWidget( id ) );    if ( !w )	return 0;    MetaDataBase::addEntry( w );    if ( !defaultProperties->contains( id ) )	saveDefaultProperties( w, id );    if ( !changedProperties->contains( id ) )	saveChangedProperties( w, id );    return w;}/*!  Creates a layout on the widget \a widget of the type \a type  which can be \c HBox, \c VBox or \c Grid.*/QLayout *WidgetFactory::createLayout( QWidget *widget, QLayout*  layout, LayoutType type ){    int spacing = BOXLAYOUT_DEFAULT_SPACING;    int margin = 0;    if ( widget && !widget->inherits( "QLayoutWidget" ) &&	 ( WidgetDatabase::isContainer( WidgetDatabase::idFromClassName( WidgetFactory::classNameOf( widget ) ) ) ||	   widget && widget->parentWidget() && widget->parentWidget()->inherits( "FormWindow" ) ) )	margin = BOXLAYOUT_DEFAULT_MARGIN;	    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();    MetaDataBase::addEntry( widget );    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() );	    MetaDataBase::setMargin( gb, margin );	    MetaDataBase::setSpacing( gb, spacing );	    l->setAlignment( AlignTop );	    return l;	case VBox:	    l = new QVBoxLayout( gb->layout(), spacing );	    MetaDataBase::setMargin( gb, margin );	    MetaDataBase::setSpacing( gb, spacing );	    l->setAlignment( AlignTop );	    return l;	case Grid:	    l = new QDesignerGridLayout( gb->layout() );	    MetaDataBase::setMargin( gb, margin );	    MetaDataBase::setSpacing( gb, spacing );	    l->setAlignment( 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 QDesignerGridLayout( 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 ) {		    MetaDataBase::setMargin( widget, margin );		    MetaDataBase::setSpacing( widget, spacing );		} else {		    l->setMargin( margin );		    l->setSpacing( margin );		}		return l;	    case VBox:		l = new QVBoxLayout( widget );		if ( widget ) {		    MetaDataBase::setMargin( widget, margin );		    MetaDataBase::setSpacing( widget, spacing );		} else {		    l->setMargin( margin );		    l->setSpacing( margin );		}		return l;	    case Grid: {		l = new QDesignerGridLayout( widget );		if ( widget ) {		    MetaDataBase::setMargin( widget, margin );		    MetaDataBase::setSpacing( widget, spacing );		} else {		    l->setMargin( margin );		    l->setSpacing( margin );		}		return l;	    }	    default:		return 0;	    }	}    }    return 0;}void WidgetFactory::deleteLayout( QWidget *widget ){    if ( !widget )	return;    if ( widget->inherits( "QTabWidget" ) )	widget = ((QTabWidget*)widget)->currentPage();    if ( widget->inherits( "QWizard" ) )	widget = ((QWizard*)widget)->currentPage();    if ( widget->inherits( "QWidgetStack" ) )	widget = ((QWidgetStack*)widget)->visibleWidget();    delete widget->layout();}FormWindow *find_formwindow( QWidget *w ){    if ( !w )	return 0;    while ( TRUE ) {	if ( w->inherits( "FormWindow" ) )	    return (FormWindow*)w;	if ( !w->parentWidget() )	    return 0;	w = w->parentWidget();    }    return 0;}/*!  Factory functions for creating a widget of the type \a className  as child of \a parent with the name \a name.  If \a init is TRUE, some initial default properties are set. This  has to be in sync with the initChangedProperties() function!*/QWidget *WidgetFactory::createWidget( const QString &className, QWidget *parent, const char *name, bool init,				      const QRect *r, Qt::Orientation orient ){    if ( className == "QPushButton" ) {	QPushButton *b = 0;	if ( init ) {	    b = new QDesignerPushButton( parent, name );	    b->setText( QString::fromLatin1( name ) );	} else {	    b = new QDesignerPushButton( parent, name );	}	b->setAutoDefault( TRUE );	return b;    } else if ( className == "QToolButton" ) {	if ( init ) {	    QDesignerToolButton *tb = new QDesignerToolButton( parent, name );	    tb->setText( "..." );	    return tb;	}	return new QDesignerToolButton( parent, name );    } else if ( className == "QCheckBox" ) {	if ( init ) {	    QDesignerCheckBox *cb = new QDesignerCheckBox( parent, name );	    cb->setText( QString::fromLatin1( name ) );	    return cb;	}	return new QDesignerCheckBox( parent, name );    } else if ( className == "QRadioButton" ) {	if ( init ) {	    QDesignerRadioButton *rb = new QDesignerRadioButton( parent, name );	    rb->setText( QString::fromLatin1( name ) );	    return rb;	}	return new QDesignerRadioButton( parent, name );    } else if ( className == "QGroupBox" ) {	if ( init )	    return new QGroupBox( QString::fromLatin1( name ), parent, name );	return new QGroupBox( parent, name );    } else if ( className == "QButtonGroup" ) {	if ( init )	    return new QButtonGroup( QString::fromLatin1( name ), parent, name );	return new QButtonGroup( parent, name );    } else if ( className == "QIconView" ) {#if defined(QT_MODULE_ICONVIEW)	QIconView* iv = new QIconView( parent, name );	if ( init )	    (void) new QIconViewItem( iv, MainWindow::tr( "New Item" ) );	return iv;#else	return 0;#endif    } else if ( className == "QTable" ) {#if defined(QT_MODULE_TABLE)	return new QTable( 3, 3, parent, name );#else	return 0;#endif    } else if ( className == "QListBox" ) {	QListBox* lb = new QListBox( parent, name );	if ( init ) {	    lb->insertItem( MainWindow::tr( "New Item" ) );	    lb->setCurrentItem( 0 );	}	return lb;    } else if ( className == "QListView" ) {	QListView *lv = new QListView( parent, name );	lv->setSorting( -1 );	if ( init ) {	    lv->addColumn( MainWindow::tr( "Column 1" ) );	    lv->setCurrentItem( new QListViewItem( lv, MainWindow::tr( "New Item" ) ) );	}	return lv;    } 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" ) {	QDesignerLabel *l = new QDesignerLabel( parent, name );	if ( init ) {	    l->setText( QString::fromLatin1( name ) );	    MetaDataBase::addEntry( l );	    MetaDataBase::setPropertyChanged( l, "text", TRUE );	}	return l;    } else if ( className == "PixmapLabel" ) {	QDesignerLabel *l = new QDesignerLabel( parent, name );	if ( init ) {	    l->setPixmap( PixmapChooser::loadPixmap( "qtlogo.png", PixmapChooser::NoSize ) );	    l->setScaledContents( TRUE );	    MetaDataBase::addEntry( l );	    MetaDataBase::setPropertyChanged( l, "pixmap", TRUE );	    MetaDataBase::setPropertyChanged( l, "scaledContents", TRUE );	}	return l;    } else if ( className == "QLayoutWidget" )	return new QLayoutWidget( parent, name );    else if ( className == "QTabWidget" ) {

⌨️ 快捷键说明

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