propertyeditor.cpp
来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 928 行 · 第 1/2 页
CPP
928 行
if (GraphicsPropertyEditor *ed = qobject_cast<GraphicsPropertyEditor*>(editor)) { QPixmap newValue = ed->pixmap(); if (newValue.serialNumber() != m_value.serialNumber()) { m_value = newValue; setChanged(true); } }}// -------------------------------------------------------------------------PaletteProperty::PaletteProperty(QDesignerFormEditorInterface *core, const QPalette &value, QWidget *selectedWidget, const QString &name) : AbstractProperty<QPalette>(value, name){ m_selectedWidget = selectedWidget; m_core = core;}void PaletteProperty::setValue(const QVariant &value){ m_value = qvariant_cast<QPalette>(value); QPalette parentPalette = QPalette(); if (m_selectedWidget) { if (m_selectedWidget->isWindow()) parentPalette = QApplication::palette(m_selectedWidget); else { if (m_selectedWidget->parentWidget()) parentPalette = m_selectedWidget->parentWidget()->palette(); } } uint mask = m_value.resolve(); m_value = m_value.resolve(parentPalette); m_value.resolve(mask);}QString PaletteProperty::toString() const{ return QString(); // ### implement me}QWidget *PaletteProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{ PaletteEditorButton *btn = new PaletteEditorButton(m_core, m_value, m_selectedWidget, parent); QObject::connect(btn, SIGNAL(changed()), target, receiver); return btn;}void PaletteProperty::updateEditorContents(QWidget *editor){ if (PaletteEditorButton *btn = qobject_cast<PaletteEditorButton*>(editor)) { btn->setPalette(m_value); }}void PaletteProperty::updateValue(QWidget *editor){ if (PaletteEditorButton *btn = qobject_cast<PaletteEditorButton*>(editor)) { QPalette newValue = btn->palette(); if (newValue.resolve() != m_value.resolve() || newValue != m_value) { m_value = newValue; setChanged(true); } }}// -------------------------------------------------------------------------------------struct Group{ QString name; QList<IProperty*> properties; inline Group() {} inline Group(const QString &n): name(n) {} inline bool operator == (const Group &other) const { return name == other.name; }};MetaDataBaseItem* PropertyEditor::metaDataBaseItem() const { QObject *o = object(); if (!o) return 0; if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(o)) o = promoted->child(); MetaDataBase* db = qobject_cast<MetaDataBase*>(core()->metaDataBase()); if (!db) return 0; return static_cast<MetaDataBaseItem*>(db->item(o));}void PropertyEditor::createPropertySheet(PropertyCollection *root, QObject *object){ QList<Group> groups; QExtensionManager *m = m_core->extensionManager(); QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*> (m, m_core); bool isMainContainer = false; if (QWidget *widget = qobject_cast<QWidget*>(object)) { if (QDesignerFormWindowInterface *fw = QDesignerFormWindowInterface::findFormWindow(widget)) { isMainContainer = (fw->mainContainer() == widget); } } m_prop_sheet = qobject_cast<QDesignerPropertySheetExtension*>(m->extension(object, Q_TYPEID(QDesignerPropertySheetExtension))); for (int i=0; i<m_prop_sheet->count(); ++i) { if (!m_prop_sheet->isVisible(i)) continue; QString pname = m_prop_sheet->propertyName(i); QVariant value = m_prop_sheet->property(i); IProperty *p = 0; if (qVariantCanConvert<FlagType>(value)) { FlagType f = qvariant_cast<FlagType>(value); if (pname == QLatin1String("alignment")) { // ### fixme!!! if (qobject_cast<QLineEdit *>(object)) { QStringList align_keys = QStringList() << QString::fromUtf8("Qt::AlignLeft") << QString::fromUtf8("Qt::AlignHCenter") << QString::fromUtf8("Qt::AlignRight"); QMap<QString, QVariant> align_map; foreach (QString align, align_keys) { align_map.insert(align, f.items.value(align)); } p = new MapProperty(align_map, uint(f.value.toInt() & Qt::AlignHorizontal_Mask), QLatin1String("alignment")); } else { p = new AlignmentProperty(f.items, Qt::Alignment(f.value.toInt()), pname); } } else { if (lang) { QMap<QString, QVariant> items; QMapIterator<QString, QVariant> it (f.items); while (it.hasNext()) { it.next(); QString id = lang->enumerator(it.key()); items.insert(id, it.value()); } f.items = items; } p = new FlagsProperty(f.items, f.value.toInt(), pname); } } else if (qVariantCanConvert<EnumType>(value)) { EnumType e = qvariant_cast<EnumType>(value); if (lang) { QMap<QString, QVariant> items; QMapIterator<QString, QVariant> it (e.items); e.names.clear(); while (it.hasNext()) { it.next(); QString id = lang->enumerator(it.key()); items.insert(id, it.value()); e.names.append(id); } e.items = items; } p = new MapProperty(e.items, e.value, pname, e.names); } if (!p) { switch (value.type()) { case 0: p = createSpecialProperty(value, pname); break; case QVariant::Int: p = new IntProperty(value.toInt(), pname); break; case QVariant::UInt: p = new IntProperty(value.toUInt(), pname); break; case QVariant::LongLong: p = new LongLongProperty(value.toLongLong(), pname); break; case QVariant::ULongLong: p = new LongLongProperty(value.toULongLong(), pname); break; case QVariant::Double: p = new DoubleProperty(value.toDouble(), pname); break; case QVariant::Char: p = new CharProperty(value.toChar(), pname); break; case QVariant::Bool: p = new BoolProperty(value.toBool(), pname); break; case QVariant::ByteArray: p = new StringProperty(QString::fromUtf8(value.toByteArray()), pname); break; case QVariant::String: { MetaDataBaseItem *item = metaDataBaseItem(); if (item && pname != QLatin1String("objectName")) { p = new StringProperty(value.toString(), pname, true, item->propertyComment(pname)); } else { StringProperty *sprop = new StringProperty(value.toString(), pname); p = sprop; if (pname == QLatin1String("objectName")) { sprop->setCheckValidObjectName(true); sprop->setAllowScope(isMainContainer); } } } break; case QVariant::Size: p = new SizeProperty(value.toSize(), pname); break; case QVariant::SizeF: p = new SizeFProperty(value.toSizeF(), pname); break; case QVariant::Point: p = new PointProperty(value.toPoint(), pname); break; case QVariant::PointF: p = new PointFProperty(value.toPointF(), pname); break; case QVariant::Rect: p = new RectProperty(value.toRect(), pname); break; case QVariant::RectF: p = new RectFProperty(value.toRectF(), pname); break; case QVariant::Icon: p = new IconProperty(m_core, qvariant_cast<QIcon>(value), pname); break; case QVariant::Pixmap: p = new PixmapProperty(m_core, qvariant_cast<QPixmap>(value), pname); break; case QVariant::Font: p = new FontProperty(qvariant_cast<QFont>(value), pname, qobject_cast<QWidget *>(object)); break; case QVariant::Color: p = new ColorProperty(qvariant_cast<QColor>(value), pname); break; case QVariant::SizePolicy: p = new SizePolicyProperty(qvariant_cast<QSizePolicy>(value), pname); break; case QVariant::DateTime: p = new DateTimeProperty(value.toDateTime(), pname); break; case QVariant::Date: p = new DateProperty(value.toDate(), pname); break; case QVariant::Time: p = new TimeProperty(value.toTime(), pname); break; case QVariant::Cursor: p = new CursorProperty(qvariant_cast<QCursor>(value), pname); break; case QVariant::KeySequence: p = new StringProperty(qvariant_cast<QKeySequence>(value), pname); break; case QVariant::Palette: p = new PaletteProperty(m_core, qvariant_cast<QPalette>(value), qobject_cast<QWidget *>(object), pname); break; case QVariant::Url: p = new UrlProperty(value.toUrl(), pname); break; case QVariant::StringList: p = new StringListProperty(qvariant_cast<QStringList>(value), pname); break; default: // ### qWarning() << "property" << pname << "with type" << value.type() << "not supported yet!"; break; } // end switch } if (p != 0) { p->setHasReset(m_prop_sheet->hasReset(i)); p->setChanged(m_prop_sheet->isChanged(i)); p->setDirty(false); QString pgroup = m_prop_sheet->propertyGroup(i); int groupIndex = groups.indexOf(pgroup); if (groupIndex == -1) { groupIndex = groups.count(); groups.append(Group(pgroup)); } QList<IProperty*> &groupProperties = groups[groupIndex].properties; groupProperties.append(p); } } foreach (Group g, groups) { root->addProperty(new SeparatorProperty(QString(), g.name)); foreach (IProperty *p, g.properties) { root->addProperty(p); } }}PropertyEditor::PropertyEditor(QDesignerFormEditorInterface *core, QWidget *parent, Qt::WindowFlags flags) : QDesignerPropertyEditorInterface(parent, flags), m_core(core), m_properties(0){ QVBoxLayout *lay = new QVBoxLayout(this); lay->setMargin(0); m_editor = new QPropertyEditor(this); lay->addWidget(m_editor); m_prop_sheet = 0; connect(m_editor, SIGNAL(propertyChanged(IProperty*)), this, SLOT(firePropertyChanged(IProperty*))); connect(m_editor->editorModel(), SIGNAL(resetProperty(QString)), this, SLOT(resetProperty(QString)));}PropertyEditor::~PropertyEditor(){}bool PropertyEditor::isReadOnly() const{ return m_editor->isReadOnly();}void PropertyEditor::setReadOnly(bool readOnly){ m_editor->setReadOnly(readOnly);}QDesignerFormEditorInterface *PropertyEditor::core() const{ return m_core;}IProperty *PropertyEditor::propertyByName(IProperty *p, const QString &name){ if (p->propertyName() == name) return p; if (p->kind() == IProperty::Property_Group) { IPropertyGroup *g = static_cast<IPropertyGroup*>(p); for (int i=0; i<g->propertyCount(); ++i) if (IProperty *c = propertyByName(g->propertyAt(i), name)) return c; } return 0;}void PropertyEditor::setPropertyValue(const QString &name, const QVariant &value, bool changed){ if (isReadOnly()) return; if (IProperty *p = propertyByName(m_editor->initialInput(), name)) { if (p->value() != value) p->setValue(value); p->setChanged(changed); p->setDirty(false); m_editor->editorModel()->refresh(p); }}void PropertyEditor::firePropertyChanged(IProperty *p){ if (isReadOnly()) return; if (object()) { if (p->parent() && p->propertyName() == QLatin1String("comment")) { QString parentProperty = p->parent()->propertyName(); if (MetaDataBaseItem *item = metaDataBaseItem()) { item->setPropertyComment(parentProperty, p->value().toString()); emit propertyChanged(parentProperty, p->parent()->value()); } return; } } emit propertyChanged(p->propertyName(), p->value());}void PropertyEditor::clearDirty(IProperty *p){ p->setDirty(false); if (p->kind() == IProperty::Property_Normal) return; IPropertyGroup *g = static_cast<IPropertyGroup*>(p); for (int i=0; i<g->propertyCount(); ++i) clearDirty(g->propertyAt(i));}void PropertyEditor::setObject(QObject *object){ if (m_editor->initialInput()) clearDirty(m_editor->initialInput()); m_object = object; if (QAction *action = qobject_cast<QAction*>(m_object)) { if (action->menu()) m_object = action->menu(); } IPropertyGroup *old_properties = m_properties; m_properties = 0; m_prop_sheet = 0; if (m_object) { PropertyCollection *collection = new PropertyCollection(QLatin1String("<root>")); createPropertySheet(collection, object); m_properties = collection; } m_editor->setInitialInput(m_properties); delete old_properties;}void PropertyEditor::resetProperty(const QString &prop_name){ int idx = m_prop_sheet->indexOf(prop_name); if (idx == -1) { qWarning("PropertyEditor::resetProperty(): no property \"%s\"", prop_name.toUtf8().constData()); return; } QDesignerFormWindowInterface *form = m_core->formWindowManager()->activeFormWindow(); if (form == 0) { qWarning("PropertyEditor::resetProperty(): widget does not belong to any form"); return; } ResetPropertyCommand *cmd = new ResetPropertyCommand(form); cmd->init(m_object, prop_name); form->commandHistory()->push(cmd);}QString PropertyEditor::currentPropertyName() const{ QModelIndex index = m_editor->selectionModel()->currentIndex(); if (index.isValid()) { IProperty *property = static_cast<IProperty*>(index.internalPointer()); while (property && property->isFake()) property = property->parent(); if (property) return property->propertyName(); } return QString();}#include "propertyeditor.moc"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?