propertyeditor.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 635 行 · 第 1/2 页

CPP
635
字号
                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 KeySequenceProperty(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:                // ### qDebug() << "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);            const 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);            m_indexToProperty[i] = p;        }    }    foreach (Group g, groups) {        root->addProperty(new SeparatorProperty(QString(), g.name));        foreach (IProperty *p, g.properties) {            root->addProperty(p);        }    }}void PropertyEditor::updatePropertySheet(){    if (!m_prop_sheet)        return;    const int count = m_prop_sheet->count();    for (int i = 0; i < count; ++i) {        IndexToPropertyMap::const_iterator it = m_indexToProperty.constFind(i);        if (it !=  m_indexToProperty.constEnd()) {            IProperty *p = it.value();            p->setValue(m_prop_sheet->property(i));            m_editor->editorModel()->refresh(p);        }    }}PropertyEditor::PropertyEditor(QDesignerFormEditorInterface *core,            QWidget *parent, Qt::WindowFlags flags)    : QDesignerPropertyEditor(parent, flags),      m_core(core),      m_editor(new QPropertyEditor(this)),      m_properties(0),      m_prop_sheet(0){    connect(m_editor, SIGNAL(editorOpened()), this, SIGNAL(editorOpened()));    connect(m_editor, SIGNAL(editorClosed()), this, SIGNAL(editorClosed()));    QVBoxLayout *lay = new QVBoxLayout(this);    lay->setMargin(0);    lay->addWidget(m_editor);    connect(m_editor, SIGNAL(propertyChanged(IProperty*)),        this, SLOT(slotFirePropertyChanged(IProperty*)));    connect(m_editor->editorModel(), SIGNAL(resetProperty(QString)),                this, SLOT(slotResetProperty(QString)));    connect(m_editor, SIGNAL(customContextMenuRequested(QPoint)),            this, SLOT(slotCustomContextMenuRequested(QPoint)));}PropertyEditor::~PropertyEditor(){     delete m_properties;}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;        IProperty *p = propertyByName(m_editor->initialInput(), name);    if (!p)        return;    if (p->value() != value)         p->setValue(value);        p->setChanged(changed);    p->setDirty(false);        m_editor->editorModel()->refresh(p);}void PropertyEditor::setPropertyComment(const QString &name, const QString &value){    if (isReadOnly())        return;    IProperty *parent = propertyByName(m_editor->initialInput(), name);    if (!parent || parent->kind() != IProperty::Property_Group)        return;        AbstractPropertyGroup *parentGroup = static_cast<AbstractPropertyGroup *>(parent);        if (parentGroup->propertyCount() != 1)        return;        IProperty *commentProperty = parentGroup->propertyAt(0);    if (commentProperty->value().toString() != value)        commentProperty->setValue(value);        commentProperty->setDirty(false);    m_editor->editorModel()->refresh(commentProperty);    }void PropertyEditor::slotFirePropertyChanged(IProperty *p){    if (isReadOnly() || !object())        return;    // Comment or property    if (p->parent() && p->propertyName() == QLatin1String("comment")) {        const QString parentProperty = p->parent()->propertyName();        emit propertyCommentChanged(parentProperty, p->value().toString());    } else {        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;    IPropertyGroup *old_properties = m_properties;    m_properties = 0;    m_prop_sheet = 0;    m_indexToProperty.clear();    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::slotResetProperty(const QString &prop_name){    QDesignerFormWindowInterface *form = m_core->formWindowManager()->activeFormWindow();    if (form == 0) {        qDebug("PropertyEditor::resetProperty(): widget does not belong to any form");        return;    }    emit resetProperty(prop_name);}QString PropertyEditor::currentPropertyName() const{    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();}void PropertyEditor::slotCustomContextMenuRequested(const QPoint &pos){        const QModelIndex idx = m_editor->indexAt(pos);    if (!idx.isValid())        return;        QPropertyEditorModel *model = m_editor->editorModel();    IProperty *nonfake = model->privateData(idx);    while (nonfake != 0 && nonfake->isFake())        nonfake = nonfake->parent();    const QDesignerPropertySheetExtension *sheet = m_prop_sheet;    const QDesignerDynamicPropertySheetExtension *dynamicSheet = qt_extension<QDesignerDynamicPropertySheetExtension*>(m_core->extensionManager(), m_object);;    if (!sheet || !dynamicSheet)        return;    int index = -1;    const bool addEnabled = dynamicSheet->dynamicPropertiesAllowed();    bool insertRemoveEnabled = false;    if (addEnabled) {        if (nonfake) {            const int idx = sheet->indexOf(nonfake->propertyName());            if (dynamicSheet->isDynamicProperty(idx)) {                insertRemoveEnabled = true;                index = idx;            }        }    }    QMenu menu(this);    QAction *addAction = menu.addAction(tr("Add Dynamic Property..."));    addAction->setEnabled(addEnabled);    QAction *removeAction = menu.addAction(tr("Remove Dynamic Property"));    removeAction->setEnabled(insertRemoveEnabled);    const QAction *result = menu.exec(mapToGlobal(pos));    if (result == removeAction && nonfake) {        emit removeDynamicProperty(nonfake->propertyName());    } else if (result == addAction) {        NewDynamicPropertyDialog dlg(this);        QStringList reservedNames;        for (int i = 0; i < sheet->count(); i++) {            if (!dynamicSheet->isDynamicProperty(i) || sheet->isVisible(i))                reservedNames.append(sheet->propertyName(i));        }        dlg.setReservedNames(reservedNames);        if (dlg.exec() == QDialog::Accepted) {            const QString newName = dlg.propertyName();            const QVariant newValue = dlg.propertyValue();            emit addDynamicProperty(newName, newValue);        }    }}}

⌨️ 快捷键说明

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