📄 widgetfactory.cpp
字号:
QTabWidget *tw = new QDesignerTabWidget( parent, name ); if ( init ) { FormWindow *fw = find_formwindow( parent ); QWidget *w = fw ? new QDesignerWidget( fw, tw, "tab" ) : new QWidget( tw, "tab" ); tw->addTab( w, MainWindow::tr("Tab 1") ); w = fw ? new QDesignerWidget( fw, tw, "tab" ) : new QWidget( tw, "tab" ); MetaDataBase::addEntry( tw ); tw->addTab( w, MainWindow::tr("Tab 2") ); MetaDataBase::addEntry( tw ); MetaDataBase::addEntry( w ); } return tw; } else if ( className == "QComboBox" ) { return new QComboBox( FALSE, parent, name ); } else if ( className == "QWidget" ) { if ( parent && ( parent->inherits( "FormWindow" ) || parent->inherits( "QWizard" ) || parent->inherits( "QTabWidget" ) ) ) { FormWindow *fw = find_formwindow( parent ); if ( fw ) { QDesignerWidget *dw = new QDesignerWidget( fw, parent, name ); MetaDataBase::addEntry( dw ); return dw; } } return new QWidget( parent, name ); } else if ( className == "QDialog" ) { QDialog *dia = 0; if ( parent && parent->inherits( "FormWindow" ) ) dia = new QDesignerDialog( (FormWindow*)parent, parent, name ); else dia = new QDialog( parent, name ); if ( parent ) dia->reparent( parent, QPoint( 0, 0 ), TRUE ); return dia; } else if ( className == "QWizard" ) { QWizard *wiz = new QDesignerWizard( parent, name ); if ( parent ) wiz->reparent( parent, QPoint( 0, 0 ), TRUE ); if ( init && parent && parent->inherits( "FormWindow" ) ) { QDesignerWidget *dw = new QDesignerWidget( (FormWindow*)parent, wiz, "page" ); MetaDataBase::addEntry( dw ); wiz->addPage( dw, FormWindow::tr( "Page" ) ); QTimer::singleShot( 0, wiz, SLOT( next() ) ); } return wiz; } else if ( className == "Spacer" ) { Spacer *s = new Spacer( parent, name ); MetaDataBase::addEntry( s ); MetaDataBase::setPropertyChanged( s, "orientation", TRUE ); MetaDataBase::setPropertyChanged( s, "sizeType", TRUE ); if ( !r ) return s; if ( !r->isValid() || r->width() < 2 && r->height() < 2 ) s->setOrientation( orient ); else if ( r->width() < r->height() ) s->setOrientation( Qt::Vertical ); else s->setOrientation( Qt::Horizontal ); return s; } 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" ) { QSlider *s = new QSlider( parent, name ); if ( !r ) return s; if ( !r->isValid() || r->width() < 2 && r->height() < 2 ) s->setOrientation( orient ); else if ( r->width() > r->height() ) s->setOrientation( Qt::Horizontal ); MetaDataBase::addEntry( s ); MetaDataBase::setPropertyChanged( s, "orientation", TRUE ); return s; } else if ( className == "QFrame" ) { if ( !init ) return new QFrame( parent, name ); QFrame *f = new QFrame( parent, name ); f->setFrameStyle( QFrame::StyledPanel | QFrame::Raised ); return f; } else if ( className == "Line" ) { Line *l = new Line( parent, name ); MetaDataBase::addEntry( l ); MetaDataBase::setPropertyChanged( l, "orientation", TRUE ); if ( !r ) return l; if ( !r->isValid() || r->width() < 2 && r->height() < 2 ) l->setOrientation( orient ); else if ( r->width() < r->height() ) l->setOrientation( Qt::Vertical ); return l; } QWidget *w = qt_create_kde_widget( className, parent, name, init ); if ( w ) return w; return 0;}/*! Find out which type the layout of the widget is. Returns \c HBox, \c VBox, \c Grid or \c NoLayout. \a layout points to this QWidget::layout() of \a w or to 0 after the function call.*/WidgetFactory::LayoutType WidgetFactory::layoutType( QWidget *w, QLayout *&layout ){ layout = 0; if ( w && w->inherits( "QTabWidget" ) ) w = ((QTabWidget*)w)->currentPage(); if ( w && w->inherits( "QWizard" ) ) w = ((QWizard*)w)->currentPage(); if ( w && w->inherits( "QWidgetStack" ) ) w = ((QWidgetStack*)w)->visibleWidget(); if ( !w || !w->layout() ) return NoLayout; QLayout *lay = w->layout(); if ( w->inherits( "QGroupBox" ) ) { QObjectList *l = lay->queryList( "QLayout" ); if ( l && l->first() ) lay = (QLayout*)l->first(); delete l; } layout = lay; if ( lay->inherits( "QHBoxLayout" ) ) return HBox; else if ( lay->inherits( "QVBoxLayout" ) ) return VBox; else if ( lay->inherits( "QGridLayout" ) ) return Grid; return NoLayout;}/*! \overload*/WidgetFactory::LayoutType WidgetFactory::layoutType( QLayout *layout ){ if ( layout->inherits( "QHBoxLayout" ) ) return HBox; else if ( layout->inherits( "QVBoxLayout" ) ) return VBox; else if ( layout->inherits( "QGridLayout" ) ) return Grid; return NoLayout;}/*! \overload*/WidgetFactory::LayoutType WidgetFactory::layoutType( QWidget *w ){ QLayout *l = 0; return layoutType( w, l );}QWidget *WidgetFactory::layoutParent( QLayout *layout ){ QObject *o = layout; while ( o ) { if ( o->isWidgetType() ) return (QWidget*)o; o = o->parent(); } return 0;}/*! Returns the widget into which children should be inserted when \a w is a container known to the designer. Usually that is \a w itself, sometimes it is different (e.g. a tabwidget is known to the designer as a container but the child widgets should be inserted into the current page of the tabwidget. So in this case this function returns the current page of the tabwidget.) */QWidget* WidgetFactory::containerOfWidget( QWidget *w ){ if ( !w ) return w; if ( w->inherits( "QTabWidget" ) ) return ((QTabWidget*)w)->currentPage(); if ( w->inherits( "QWizard" ) ) return ((QWizard*)w)->currentPage(); if ( w->inherits( "QWidgetStack" ) ) return ((QWidgetStack*)w)->visibleWidget(); return w;}/*! Returns the actual designer widget of the container \a w. This is normally \a w itself, but might be a parent or grand parent of \a w (e.g. when working with a tabwidget and \a w is the container which contains and layouts childs, but the actual widget known to the designer is the tabwidget which is the parent of \a w. So this function returns the tabwidget then.)*/QWidget* WidgetFactory::widgetOfContainer( QWidget *w ){ if ( w->parentWidget() && w->parentWidget()->inherits( "QWidgetStack" ) ) w = w->parentWidget(); while ( w ) { if ( WidgetDatabase::isContainer( WidgetDatabase::idFromClassName( WidgetFactory::classNameOf( w ) ) ) || w && w->parentWidget() && w->parentWidget()->inherits( "FormWindow" ) ) return w; w = w->parentWidget(); } return w;}/*! Returns whether \a o is a passive interactor or not. */bool WidgetFactory::isPassiveInteractor( QObject* o ){ if ( o->inherits( "QTabBar" ) ) return TRUE; else if ( o->inherits( "QSizeGrip" ) ) return TRUE; else if ( o->inherits( "QToolButton" ) && o->parent() && o->parent()->inherits( "QTabBar" ) ) return TRUE; else if ( o->parent() && o->parent()->inherits( "QWizard" ) && o->inherits( "QPushButton" ) ) return TRUE; return FALSE;}/*! Returns the class name of object \a o that should be used for externally (i.e. for saving) */const char* WidgetFactory::classNameOf( QObject* o ){ if ( o->inherits( "QDesignerTabWidget" ) ) return "QTabWidget"; else if ( o->inherits( "QDesignerDialog" ) ) return "QDialog"; else if ( o->inherits( "QDesignerWidget" ) ) return "QWidget"; else if ( o->inherits( "CustomWidget" ) ) return ( (CustomWidget*)o )->realClassName().latin1(); else if ( o->inherits( "QDesignerLabel" ) ) return "QLabel"; else if ( o->inherits( "QDesignerWizard" ) ) return "QWizard"; else if ( o->inherits( "QDesignerPushButton" ) ) return "QPushButton"; else if ( o->inherits( "QDesignerToolButton" ) ) return "QToolButton"; else if ( o->inherits( "QDesignerRadioButton" ) ) return "QRadioButton"; else if ( o->inherits( "QDesignerCheckBox" ) ) return "QCheckBox"; return o->className();}/*! As some properties are set by default when creating a widget this functions markes this properties as changed. Has to be in sync with createWidget()!*/void WidgetFactory::initChangedProperties( QObject *o ){ MetaDataBase::setPropertyChanged( o, "name", TRUE ); MetaDataBase::setPropertyChanged( o, "geometry", TRUE ); if ( o->inherits( "QPushButton" ) || o->inherits("QRadioButton") || o->inherits( "QCheckBox" ) || o->inherits( "QToolButton" ) ) MetaDataBase::setPropertyChanged( o, "text", TRUE ); else if ( o->inherits( "QGroupBox" ) ) MetaDataBase::setPropertyChanged( o, "title", TRUE ); else if ( o->isA( "QFrame" ) ) { MetaDataBase::setPropertyChanged( o, "frameShadow", TRUE ); MetaDataBase::setPropertyChanged( o, "frameShape", TRUE ); } else if ( o->inherits( "QTabWidget" ) || o->inherits( "QWizard" ) ) { MetaDataBase::setPropertyChanged( o, "pageTitle", TRUE ); MetaDataBase::setPropertyChanged( o, "pageName", TRUE ); }}bool WidgetFactory::hasSpecialEditor( int id ){ QString className = WidgetDatabase::className( id ); if ( className.mid( 1 ) == "ListBox" ) return TRUE; if ( className.mid( 1 ) == "ComboBox" ) return TRUE; if ( className.mid( 1 ) == "ListView" ) return TRUE; if ( className.mid( 1 ) == "IconView" ) return TRUE; if ( className == "QMultiLineEdit" ) return TRUE; return FALSE;}bool WidgetFactory::hasItems( int id ){ QString className = WidgetDatabase::className( id ); if ( className.mid( 1 ) == "ListBox" || className.mid( 1 ) == "ListView" || className.mid( 1 ) == "IconView" || className.mid( 1 ) == "ComboBox" ) return TRUE; return FALSE;}void WidgetFactory::editWidget( int id, QWidget *parent, QWidget *editWidget, FormWindow *fw ){ QString className = WidgetDatabase::className( id ); if ( className.mid( 1 ) == "ListBox" ) { if ( !editWidget->inherits( "QListBox" ) ) return; ListBoxEditor *e = new ListBoxEditor( parent, editWidget, fw ); e->exec(); delete e; return; } if ( className.mid( 1 ) == "ComboBox" ) { if ( !editWidget->inherits( "QComboBox" ) ) return; QComboBox *cb = (QComboBox*)editWidget; ListBoxEditor *e = new ListBoxEditor( parent, cb->listBox(), fw ); e->exec(); delete e; cb->update(); return; } if ( className.mid( 1 ) == "ListView" ) { if ( !editWidget->inherits( "QListView" ) ) return; QListView *lv = (QListView*)editWidget; ListViewEditor *e = new ListViewEditor( parent, lv, fw ); e->exec(); delete e; return; } if ( className.mid( 1 ) == "IconView" ) { if ( !editWidget->inherits( "QIconView" ) ) return; IconViewEditor *e = new IconViewEditor( parent, editWidget, fw ); e->exec(); delete e; return; } if ( className == "QMultiLineEdit" ) { MultiLineEditor *e = new MultiLineEditor( parent, editWidget, fw ); e->exec(); delete e; return; }}bool WidgetFactory::canResetProperty( QWidget *w, const QString &propName ){ if ( propName == "name" || propName == "geometry" ) return FALSE; QStringList l = *changedProperties->find( WidgetDatabase::idFromClassName( WidgetFactory::classNameOf( w ) ) ); return l.findIndex( propName ) == -1;}bool WidgetFactory::resetProperty( QWidget *w, const QString &propName ){ const QMetaProperty *p = w->metaObject()->property( propName, TRUE ); if (!p || ( p->reset == 0 ) ) return FALSE; typedef void (QObject::*ProtoVoid)() const; ProtoVoid m = (ProtoVoid)p->reset; (w->*m)(); return TRUE;}QVariant WidgetFactory::defaultValue( QWidget *w, const QString &propName ){ if ( propName == "wordwrap" ) { int v = defaultValue( w, "alignment" ).toInt(); return QVariant( ( v & WordBreak ) == WordBreak, 0 ); } else if ( propName == "toolTip" || propName == "whatsThis" ) { return QVariant( QString::fromLatin1( "" ) ); } else if ( w->inherits( "CustomWidget" ) ) { return QVariant(); } return *( *defaultProperties->find( WidgetDatabase::idFromClassName( classNameOf( w ) ) ) ).find( propName );}QString WidgetFactory::defaultCurrentItem( QWidget *w, const QString &propName ){ const QMetaProperty *p = w->metaObject()->property( propName, TRUE ); if ( !p ) { int v = defaultValue( w, "alignment" ).toInt(); if ( propName == "hAlign" ) { if ( ( v & AlignLeft ) == AlignLeft ) return "AlignLeft"; if ( ( v & AlignCenter ) == AlignCenter || ( v & AlignHCenter ) == AlignHCenter ) return "AlignHCenter"; if ( ( v & AlignRight ) == AlignRight ) return "AlignRight"; } else if ( propName == "vAlign" ) { if ( ( v & AlignTop ) == AlignTop ) return "AlignTop"; if ( ( v & AlignCenter ) == AlignCenter || ( v & AlignVCenter ) == AlignVCenter ) return "AlignVCenter"; if ( ( v & AlignBottom ) == AlignBottom ) return "AlignBottom"; } return QString::null; } return p->valueToKey( defaultValue( w, propName ).toInt() );}QWidget *WidgetFactory::createCustomWidget( QWidget *parent, const char *name, MetaDataBase::CustomWidget *w ){ if ( !w ) return 0; return new CustomWidget( parent, name, w );}QVariant WidgetFactory::property( QWidget *w, const char *name ){ QVariant v = w->property( name ); if ( v.isValid() ) return v; return MetaDataBase::fakeProperty( w, name );}void QDesignerLabel::updateBuddy(){ if ( myBuddy.isEmpty() ) return; QObjectList *l = topLevelWidget()->queryList( "QWidget", myBuddy, FALSE, TRUE ); if ( !l || !l->first() ) { delete l; return; } QLabel::setBuddy( (QWidget*)l->first() ); delete l;}void QDesignerWidget::paintEvent( QPaintEvent *e ){ formwindow->paintGrid( this, e );}void QDesignerDialog::paintEvent( QPaintEvent *e ){ formwindow->paintGrid( this, e );}QSizePolicy QLayoutWidget::sizePolicy() const{ return sp;}bool QLayoutWidget::event( QEvent *e ){ if ( e && ( e->type() == QEvent::ChildInserted || e->type() == QEvent::ChildRemoved || e->type() == QEvent::LayoutHint ) ) updateSizePolicy(); return QWidget::event( e );}void QLayoutWidget::updateSizePolicy(){ if ( !children() || children()->count() == 0 ) { sp = QWidget::sizePolicy(); return; } QObjectListIt it( *children() ); QObject *o; QSizePolicy::SizeType vt = QSizePolicy::Preferred; QSizePolicy::SizeType ht = QSizePolicy::Preferred; while ( ( o = it.current() ) ) { ++it; if ( !o->inherits( "QWidget" ) || ( (QWidget*)o )->testWState( WState_ForceHide ) ) continue; QWidget *w = (QWidget*)o; if ( w->sizePolicy().horData() == QSizePolicy::Expanding || w->sizePolicy().horData() == QSizePolicy::MinimumExpanding ) ht = QSizePolicy::Expanding; else if ( w->sizePolicy().horData() == QSizePolicy::Fixed && ht != QSizePolicy::Expanding ) ht = QSizePolicy::Fixed; if ( w->sizePolicy().verData() == QSizePolicy::Expanding || w->sizePolicy().verData() == QSizePolicy::MinimumExpanding ) vt = QSizePolicy::Expanding; else if ( w->sizePolicy().verData() == QSizePolicy::Fixed && vt != QSizePolicy::Expanding ) vt = QSizePolicy::Fixed; } sp = QSizePolicy( ht, vt ); if ( layout() ) layout()->invalidate(); updateGeometry();}void CustomWidget::paintEvent( QPaintEvent *e ){ if ( parentWidget() && parentWidget()->inherits( "FormWindow" ) ) { ( (FormWindow*)parentWidget() )->paintGrid( this, e ); } else { QPainter p( this ); p.fillRect( rect(), colorGroup().dark() ); p.drawPixmap( ( width() - cusw->pixmap->width() ) / 2, ( height() - cusw->pixmap->height() ) / 2, *cusw->pixmap ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -