📄 propertyeditor.cpp
字号:
if ( s == "PointArray" ) return QVariant::PointArray; if ( s == "Region" ) return QVariant::Region; if ( s == "Bitmap" ) return QVariant::Bitmap; if ( s == "Cursor" ) return QVariant::Cursor; if ( s == "SizePolicy" ) return QVariant::SizePolicy; return QVariant::Invalid; }/*! Sets up the property list by adding an item for each designableproperty of the widget which is just edited.*/void PropertyList::setupProperties(){ if ( !editor->widget() ) return; bool allProperties = !editor->widget()->inherits( "Spacer" ); QStrList lst = editor->widget()->metaObject()->propertyNames( allProperties ); PropertyItem *item = 0; QMap<QString, bool> unique; QWidget *w = editor->widget(); QStringList valueSet; bool parentHasLayout = !editor->formWindow()->isMainContainer( w ) && w->parentWidget() && WidgetFactory::layoutType( w->parentWidget() ) != WidgetFactory::NoLayout; for ( QListIterator<char> it( lst ); it.current(); ++it ) { const QMetaProperty* p = editor->widget()->metaObject()->property( it.current(), allProperties ); if ( !p ) continue; if ( unique.contains( QString::fromLatin1( it.current() ) ) ) continue; unique.insert( QString::fromLatin1( it.current() ), TRUE ); if ( editor->formWindow()->isMainContainer( editor->widget() ) ) { if ( qstrcmp( p->name(), "geometry" ) == 0 ) continue; } else { // hide some toplevel-only stuff if ( qstrcmp( p->name(), "icon" ) == 0 ) continue; if ( qstrcmp( p->name(), "iconText" ) == 0 ) continue; if ( qstrcmp( p->name(), "caption" ) == 0 ) continue; if ( qstrcmp( p->name(), "sizeIncrement" ) == 0 ) continue; if ( qstrcmp( p->name(), "baseSize" ) == 0 ) continue; if ( parentHasLayout && qstrcmp( p->name(), "geometry" ) == 0 ) continue; if ( w->inherits( "QLayoutWidget" ) || w->inherits( "Spacer" ) ) { if ( qstrcmp( p->name(), "sizePolicy" ) == 0 ) continue; } } if ( qstrcmp( p->name(), "minimumHeight" ) == 0 ) continue; if ( qstrcmp( p->name(), "minimumWidth" ) == 0 ) continue; if ( qstrcmp( p->name(), "maximumHeight" ) == 0 ) continue; if ( qstrcmp( p->name(), "maximumWidth" ) == 0 ) continue; if ( qstrcmp( p->name(), "buttonGroupId" ) == 0 ) { // #### remove this when designable in Q_PROPERTY can take a function (isInButtonGroup() in this case) if ( !editor->widget()->parentWidget() || !editor->widget()->parentWidget()->inherits( "QButtonGroup" ) ) continue; } if ( p->designable() ) { if ( p->isSetType() ) { if ( QString( p->name() ) == "alignment" ) { QStringList lst; lst << p->valueToKey( AlignLeft ) << p->valueToKey( AlignHCenter ) << p->valueToKey( AlignRight ); item = new PropertyListItem( this, item, 0, "hAlign", FALSE ); item->setValue( lst ); setPropertyValue( item ); if ( MetaDataBase::isPropertyChanged( editor->widget(), "hAlign" ) ) item->setChanged( TRUE, FALSE ); if ( !editor->widget()->inherits( "QMultiLineEdit" ) ) { lst.clear(); lst << p->valueToKey( AlignTop ) << p->valueToKey( AlignVCenter ) << p->valueToKey( AlignBottom ); item = new PropertyListItem( this, item, 0, "vAlign", FALSE ); item->setValue( lst ); setPropertyValue( item ); if ( MetaDataBase::isPropertyChanged( editor->widget(), "vAlign" ) ) item->setChanged( TRUE, FALSE ); item = new PropertyBoolItem( this, item, 0, "wordwrap" ); setPropertyValue( item ); if ( MetaDataBase::isPropertyChanged( editor->widget(), "wordwrap" ) ) item->setChanged( TRUE, FALSE ); } } else { qWarning( "Sets except 'alignment' not supported yet.... %s.", p->name() ); } } else if ( p->isEnumType() ) { QStrList l = p->enumKeys(); QStringList lst; for ( uint i = 0; i < l.count(); ++i ) { QString k = l.at( i ); // filter out enum-masks if ( k[0] == 'M' && k[1].category() == QChar::Letter_Uppercase ) continue; lst << l.at( i ); } item = new PropertyListItem( this, item, 0, p->name(), FALSE ); item->setValue( lst ); } else { QVariant::Type t = QVariant::nameToType( p->type() ); if ( !addPropertyItem( item, p->name(), t ) ) continue; } } if ( item && !p->isSetType() ) { if ( valueSet.findIndex( item->name() ) == -1 ) { setPropertyValue( item ); valueSet << item->name(); } if ( MetaDataBase::isPropertyChanged( editor->widget(), p->name() ) ) item->setChanged( TRUE, FALSE ); } } if ( WidgetFactory::layoutType( w ) != WidgetFactory::NoLayout ) { item = new PropertyIntItem( this, item, 0, "layoutSpacing", TRUE ); setPropertyValue( item ); item->setChanged( TRUE ); item = new PropertyIntItem( this, item, 0, "layoutMargin", TRUE ); setPropertyValue( item ); item->setChanged( TRUE ); } if ( !w->inherits( "Spacer" ) && !w->inherits( "QLayoutWidget" ) ) { item = new PropertyTextItem( this, item, 0, "toolTip", TRUE, FALSE ); setPropertyValue( item ); if ( MetaDataBase::isPropertyChanged( editor->widget(), "toolTip" ) ) item->setChanged( TRUE, FALSE ); item = new PropertyTextItem( this, item, 0, "whatsThis", TRUE, TRUE ); setPropertyValue( item ); if ( MetaDataBase::isPropertyChanged( editor->widget(), "whatsThis" ) ) item->setChanged( TRUE, FALSE ); } if ( w->inherits( "CustomWidget" ) ) { MetaDataBase::CustomWidget *cw = ( (CustomWidget*)w )->customWidget(); if ( cw ) { for ( QValueList<MetaDataBase::Property>::Iterator it = cw->lstProperties.begin(); it != cw->lstProperties.end(); ++it ) { if ( unique.contains( QString( (*it).property ) ) ) continue; unique.insert( QString( (*it).property ), TRUE ); addPropertyItem( item, (*it).property, type_to_variant( (*it).type ) ); setPropertyValue( item ); if ( MetaDataBase::isPropertyChanged( editor->widget(), (*it).property ) ) item->setChanged( TRUE, FALSE ); } } } setCurrentItem( firstChild() ); updateEditorSize(); updateEditorSize();}bool PropertyList::addPropertyItem( PropertyItem *&item, const QCString &name, QVariant::Type t ){ switch ( t ) { case QVariant::String: item = new PropertyTextItem( this, item, 0, name, TRUE, editor->widget()->inherits( "QLabel" ) || editor->widget()->inherits( "QTextView" ) ); break; case QVariant::CString: item = new PropertyTextItem( this, item, 0, name, FALSE, FALSE, TRUE ); break; case QVariant::Bool: item = new PropertyBoolItem( this, item, 0, name ); break; case QVariant::Font: item = new PropertyFontItem( this, item, 0, name ); break; case QVariant::Int: if ( name == "accel" ) item = new PropertyTextItem( this, item, 0, name, FALSE, FALSE, FALSE, TRUE ); else item = new PropertyIntItem( this, item, 0, name, TRUE ); break; case QVariant::UInt: item = new PropertyIntItem( this, item, 0, name, FALSE ); break; case QVariant::StringList: item = new PropertyListItem( this, item, 0, name, TRUE ); break; case QVariant::Rect: item = new PropertyCoordItem( this, item, 0, name, PropertyCoordItem::Rect ); break; case QVariant::Point: item = new PropertyCoordItem( this, item, 0, name, PropertyCoordItem::Point ); break; case QVariant::Size: item = new PropertyCoordItem( this, item, 0, name, PropertyCoordItem::Size ); break; case QVariant::Color: item = new PropertyColorItem( this, item, 0, name, TRUE ); break; case QVariant::Pixmap: item = new PropertyPixmapItem( this, item, 0, name ); break; case QVariant::SizePolicy: item = new PropertySizePolicyItem( this, item, 0, name ); break; case QVariant::Palette: item = new PropertyPaletteItem( this, item, 0, name ); break; case QVariant::Cursor: item = new PropertyCursorItem( this, item, 0, name ); break; default: return FALSE; } return TRUE;}void PropertyList::paintEmptyArea( QPainter *p, const QRect &r ){ p->fillRect( r, backColor2 );}void PropertyList::setCurrentItem( QListViewItem *i ){ if ( currentItem() ) ( (PropertyItem*)currentItem() )->hideEditor(); QListView::setCurrentItem( i ); ( (PropertyItem*)currentItem() )->showEditor();}void PropertyList::updateEditorSize(){ QSize s( header()->sectionPos(1) + header()->sectionSize(1), height() ); QResizeEvent e( s, size() ); resizeEvent( &e ); viewport()->repaint( s.width(), 0, width() - s.width(), height(), FALSE );}/*! This has to be called if the value if \a i should be set as property to the currently edited widget.*/void PropertyList::valueChanged( PropertyItem *i ){ if ( !editor->widget() ) return; QString pn( tr( "Set '%1' of '%2'" ).arg( i->name() ).arg( editor->widget()->name() ) ); SetPropertyCommand *cmd = new SetPropertyCommand( pn, editor->formWindow(), editor->widget(), editor, i->name(), WidgetFactory::property( editor->widget(), i->name() ), i->value(), i->currentItem(), i->currentItemFromObject() ); cmd->execute(); editor->formWindow()->commandHistory()->addCommand( cmd, TRUE );}void PropertyList::itemPressed( QListViewItem *i, const QPoint &p, int c ){ if ( !i ) return; PropertyItem *pi = (PropertyItem*)i; if ( !pi->hasSubItems() ) return; if ( c == 0 && viewport()->mapFromGlobal( p ).x() < 20 ) toggleOpen( i );}void PropertyList::toggleOpen( QListViewItem *i ){ if ( !i ) return; PropertyItem *pi = (PropertyItem*)i; if ( pi->hasSubItems() ) { pi->setOpen( !pi->isOpen() ); } else { pi->toggle(); }}bool PropertyList::eventFilter( QObject *o, QEvent *e ){ if ( !o || !e ) return TRUE; PropertyItem *i = (PropertyItem*)currentItem(); if ( o != this &&e->type() == QEvent::KeyPress ) { QKeyEvent *ke = (QKeyEvent*)e; if ( ( ke->key() == Key_Up || ke->key() == Key_Down ) && ( o != this || o != viewport() ) && !( ke->state() & ControlButton ) ) { QApplication::sendEvent( this, (QKeyEvent*)e ); return TRUE; } else if ( ( !o->inherits( "QLineEdit" ) || ( o->inherits( "QLineEdit" ) && ( (QLineEdit*)o )->isReadOnly() ) ) && i && i->hasSubItems() ) { if ( !i->isOpen() && ( ke->key() == Key_Plus || ke->key() == Key_Right )) i->setOpen( TRUE ); else if ( i->isOpen() && ( ke->key() == Key_Minus || ke->key() == Key_Left ) ) i->setOpen( FALSE ); } else if ( ( ke->key() == Key_Return || ke->key() == Key_Enter ) && o->inherits( "QComboBox" ) ) { QKeyEvent ke2( QEvent::KeyPress, Key_Space, 0, 0 ); QApplication::sendEvent( o, &ke2 ); return TRUE; } } else if ( e->type() == QEvent::FocusOut && o->inherits( "QLineEdit" ) ) { QTimer::singleShot( 100, editor->formWindow()->commandHistory(), SLOT( checkCompressedCommand() ) ); } return QListView::eventFilter( o, e );}/*! This method re-initializes each item of the property list.*/void PropertyList::refetchData(){ QListViewItemIterator it( this ); for ( ; it.current(); ++it ) { PropertyItem *i = (PropertyItem*)it.current(); if ( !i->propertyParent() ) setPropertyValue( i ); if ( i->hasSubItems() ) i->initChildren(); bool changed = MetaDataBase::isPropertyChanged( editor->widget(), i->name() ); if ( changed != i->isChanged() ) i->setChanged( changed, FALSE ); } updateEditorSize();}/*! This method initializes the value of the item \a i to the value of the corresponding property.*/void PropertyList::setPropertyValue( PropertyItem *i ){ const QMetaProperty *p = editor->widget()->metaObject()->property( i->name(), TRUE ); if ( !p ) { if ( i->name() == "hAlign" ) { int align = editor->widget()->property( "alignment" ).toInt(); p = editor->widget()->metaObject()->property( "alignment", TRUE ); align &= AlignLeft | AlignHCenter | AlignRight; ( (PropertyListItem*)i )->setCurrentItem( p->valueToKeys( align ).first() ); } else if ( i->name() == "vAlign" ) { int align = editor->widget()->property( "alignment" ).toInt(); p = editor->widget()->metaObject()->property( "alignment", TRUE ); align &= AlignTop | AlignVCenter | AlignBottom; ( (PropertyListItem*)i )->setCurrentItem( p->valueToKeys( align ).first() ); } else if ( i->name() == "wordwrap" ) { int align = editor->widget()->property( "alignment" ).toInt(); if ( align & WordBreak ) i->setValue( QVariant( TRUE, 0 ) ); else i->setValue( QVariant( FALSE, 0 ) ); } else if ( i->name() == "layoutSpacing" ) { ( (PropertyIntItem*)i )->setValue( MetaDataBase::spacing( WidgetFactory::containerOfWidget( editor->widget() ) ) ); } else if ( i->name() == "layoutMargin" ) { ( (PropertyIntItem*)i )->setValue( MetaDataBase::margin( WidgetFactory::containerOfWidget( editor->widget() ) ) ); } else if ( i->name() == "toolTip" || i->name() == "whatsThis" ) { i->setValue( MetaDataBase::fakeProperty( editor->widget(), i->name() ) ); } else if ( editor->widget()->inherits( "CustomWidget" ) ) { MetaDataBase::CustomWidget *cw = ( (CustomWidget*)editor->widget() )->customWidget(); if ( !cw ) return; i->setValue( MetaDataBase::fakeProperty( editor->widget(), i->name() ) ); } return; } if ( p->isSetType() ) ; else if ( p->isEnumType() ) ( (PropertyListItem*)i )->setCurrentItem( p->valueToKey( editor->widget()->property( i->name() ).toInt() ) ); else i->setValue( editor->widget()->property( i->name() ) );}void PropertyList::setCurrentProperty( const QString &n ){ if ( currentItem() && currentItem()->text( 0 ) == n || currentItem() && ( (PropertyItem*)currentItem() )->propertyParent() && ( (PropertyItem*)currentItem() )->propertyParent()->text( 0 ) == n ) return; QListViewItemIterator it( this ); for ( ; it.current(); ++it ) { if ( it.current()->text( 0 ) == n ) { setCurrentItem( it.current() ); break; } }}PropertyEditor *PropertyList::propertyEditor() const{ return editor;}void PropertyList::resetProperty(){ if ( !currentItem() ) return; PropertyItem *i = (PropertyItem*)currentItem(); if ( !MetaDataBase::isPropertyChanged( editor->widget(), i->PropertyItem::name() ) ) return; QString pn( tr( "Reset '%1' of '%2'" ).arg( i->name() ).arg( editor->widget()->name() ) ); SetPropertyCommand *cmd = new SetPropertyCommand( pn, editor->formWindow(), editor->widget(), editor, i->name(), i->value(), WidgetFactory::defaultValue( editor->widget(), i->name() ), WidgetFactory::defaultCurrentItem( editor->widget(), i->name() ), i->currentItem(), TRUE ); cmd->execute(); editor->formWindow()->commandHistory()->addCommand( cmd, FALSE ); if ( i->hasSubItems() ) i->initChildren();}// --------------------------------------------------------------/*! \class PropertyEditor propertyeditor.h \brief PropertyEdior toplevel window This is the toplevel window of the property editor which contains a listview for editing properties.*/PropertyEditor::PropertyEditor( QWidget *parent ) : QVBox( parent, 0, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_StaysOnTop | WStyle_Tool |WStyle_MinMax | WStyle_SysMenu ){ setCaption( tr( "Property Editor" ) ); setIcon( PixmapChooser::loadPixmap( "logo" ) ); wid = 0; formwindow = 0; listview = new PropertyList( this );}QWidget *PropertyEditor::widget() const{ return wid;}void PropertyEditor::setWidget( QWidget *w, FormWindow *fw ){ if ( w == wid ) { bool ret = TRUE; if ( WidgetFactory::layoutType( wid ) != WidgetFactory::NoLayout ) { QListViewItemIterator it( listview ); ret = FALSE; while ( it.current()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -