From 563e055c5aad741e945adbdefef273812ff979fe Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Wed, 31 Aug 2022 10:26:28 +0200 Subject: [PATCH] QmlDesigner: Use new validation approach in property editor Many validation tests can now be skipped. Task-number: QDS-7454 Change-Id: I3fe3ee1fcf97b965872cb6f76f00034caeb72ba0 Reviewed-by: Thomas Hartmann --- .../propertyeditor/gradientmodel.cpp | 2 +- .../propertyeditorqmlbackend.cpp | 2 +- .../propertyeditor/propertyeditorvalue.cpp | 65 +++++++------------ .../propertyeditor/propertyeditorview.cpp | 14 ++-- 4 files changed, 31 insertions(+), 52 deletions(-) diff --git a/src/plugins/qmldesigner/components/propertyeditor/gradientmodel.cpp b/src/plugins/qmldesigner/components/propertyeditor/gradientmodel.cpp index dd93f7f9003..84e7b8284e0 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/gradientmodel.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/gradientmodel.cpp @@ -34,7 +34,7 @@ int GradientModel::rowCount(const QModelIndex & /*parent*/) const QmlDesigner::ModelNode gradientNode = m_itemNode.modelNode().nodeProperty(gradientPropertyName().toUtf8()).modelNode(); - if (gradientNode.isValid() && gradientNode.hasNodeListProperty("stops")) + if (gradientNode.hasNodeListProperty("stops")) return gradientNode.nodeListProperty("stops").toModelNodeList().count(); } } diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp index ad52e4aa52f..6c41274a51a 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp @@ -808,7 +808,7 @@ NodeMetaInfo PropertyEditorQmlBackend::findCommonAncestor(const ModelNode &node) if (!node.isValid()) return node.metaInfo(); - if (auto metaInfo = node.metaInfo(); metaInfo.isValid()) + if (auto metaInfo = node.metaInfo()) return metaInfo; AbstractView *view = node.view(); diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorvalue.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorvalue.cpp index d3fcbb644d2..be62ce4684c 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorvalue.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorvalue.cpp @@ -38,13 +38,10 @@ PropertyEditorValue::PropertyEditorValue(QObject *parent) QVariant PropertyEditorValue::value() const { QVariant returnValue = m_value; - if (modelNode().isValid()) { - if (auto metaInfo = modelNode().metaInfo(); - metaInfo.isValid() && metaInfo.hasProperty(name()) - && metaInfo.property(name()).propertyType().isUrl()) { - returnValue = returnValue.toUrl().toString(); - } + if (auto metaInfo = modelNode().metaInfo(); metaInfo.property(name()).propertyType().isUrl()) { + returnValue = returnValue.toUrl().toString(); } + return returnValue; } @@ -82,30 +79,24 @@ static void fixAmbigousColorNames(const QmlDesigner::ModelNode &modelNode, const QmlDesigner::PropertyName &name, QVariant *value) { - if (modelNode.isValid()) { - if (auto metaInfo = modelNode.metaInfo(); - metaInfo.isValid() && metaInfo.property(name).propertyType().isColor()) { - if ((value->type() == QVariant::Color)) { - QColor color = value->value(); - int alpha = color.alpha(); - color = QColor(color.name()); - color.setAlpha(alpha); - *value = color; - } else if (value->toString() != QStringLiteral("transparent")) { - *value = QColor(value->toString()).name(QColor::HexArgb); - } + if (auto metaInfo = modelNode.metaInfo(); metaInfo.property(name).propertyType().isColor()) { + if ((value->type() == QVariant::Color)) { + QColor color = value->value(); + int alpha = color.alpha(); + color = QColor(color.name()); + color.setAlpha(alpha); + *value = color; + } else if (value->toString() != QStringLiteral("transparent")) { + *value = QColor(value->toString()).name(QColor::HexArgb); } } } static void fixUrl(const QmlDesigner::ModelNode &modelNode, const QmlDesigner::PropertyName &name, QVariant *value) { - if (modelNode.isValid()) { - if (auto metaInfo = modelNode.metaInfo(); - metaInfo.isValid() && metaInfo.property(name).propertyType().isUrl()) - - if (!value->isValid()) - *value = QStringLiteral(""); + if (auto metaInfo = modelNode.metaInfo(); metaInfo.property(name).propertyType().isUrl()) { + if (!value->isValid()) + *value = QStringLiteral(""); } } @@ -124,12 +115,8 @@ void PropertyEditorValue::setValueWithEmit(const QVariant &value) { if (!compareVariants(value, m_value ) || isBound()) { QVariant newValue = value; - if (modelNode().isValid()) { - if (auto metaInfo = modelNode().metaInfo(); - metaInfo.isValid() && metaInfo.hasProperty(name()) - && metaInfo.property(name()).propertyType().isUrl()) { - newValue = QUrl(newValue.toString()); - } + if (auto metaInfo = modelNode().metaInfo(); metaInfo.property(name()).propertyType().isUrl()) { + newValue = QUrl(newValue.toString()); } if (cleverDoubleCompare(newValue, m_value)) @@ -212,7 +199,7 @@ bool PropertyEditorValue::isBound() const bool PropertyEditorValue::isInModel() const { - return modelNode().isValid() && modelNode().hasProperty(name()); + return modelNode().hasProperty(name()); } QmlDesigner::PropertyName PropertyEditorValue::name() const @@ -248,7 +235,7 @@ bool PropertyEditorValue::isTranslated() const metaInfo.isValid() && metaInfo.hasProperty(name()) && metaInfo.property(name()).propertyType().isString()) { const QmlDesigner::QmlObjectNode objectNode(modelNode()); - if (objectNode.isValid() && objectNode.hasBindingProperty(name())) { + if (objectNode.hasBindingProperty(name())) { const QRegularExpression rx( QRegularExpression::anchoredPattern("qsTr(|Id|anslate)\\(\".*\"\\)")); //qsTr() @@ -422,7 +409,7 @@ QString PropertyEditorValue::getTranslationContext() const metaInfo.isValid() && metaInfo.hasProperty(name()) && metaInfo.property(name()).propertyType().isString()) { const QmlDesigner::QmlObjectNode objectNode(modelNode()); - if (objectNode.isValid() && objectNode.hasBindingProperty(name())) { + if (objectNode.hasBindingProperty(name())) { const QRegularExpression rx(QRegularExpression::anchoredPattern( "qsTranslate\\(\"(.*)\"\\s*,\\s*\".*\"\\s*\\)")); const QRegularExpressionMatch match = rx.match(expression()); @@ -438,7 +425,7 @@ bool PropertyEditorValue::isIdList() const { if (modelNode().isValid() && modelNode().metaInfo().isValid() && modelNode().metaInfo().hasProperty(name())) { const QmlDesigner::QmlObjectNode objectNode(modelNode()); - if (objectNode.isValid() && objectNode.hasBindingProperty(name())) { + if (objectNode.hasBindingProperty(name())) { static const QRegularExpression rx(QRegularExpression::anchoredPattern( "^[a-z_]\\w*|^[A-Z]\\w*\\.{1}([a-z_]\\w*\\.?)+")); const QString exp = objectNode.propertyAffectedByCurrentState(name()) ? expression() : modelNode().bindingProperty(name()).expression(); @@ -586,11 +573,7 @@ bool PropertyEditorNodeWrapper::exists() QString PropertyEditorNodeWrapper::type() { - if (!(m_modelNode.isValid())) - return QString(); - return m_modelNode.simplifiedTypeName(); - } QmlDesigner::ModelNode PropertyEditorNodeWrapper::parentModelNode() const @@ -635,8 +618,7 @@ void PropertyEditorNodeWrapper::add(const QString &type) void PropertyEditorNodeWrapper::remove() { if ((m_editorValue && m_editorValue->modelNode().isValid())) { - if (QmlDesigner::QmlObjectNode(m_modelNode).isValid()) - QmlDesigner::QmlObjectNode(m_modelNode).destroy(); + QmlDesigner::QmlObjectNode(m_modelNode).destroy(); m_editorValue->modelNode().removeProperty(m_editorValue->name()); } else { qWarning("PropertyEditorNodeWrapper::remove failed - node invalid"); @@ -675,13 +657,12 @@ void PropertyEditorNodeWrapper::setup() Q_ASSERT(m_editorValue); Q_ASSERT(m_editorValue->modelNode().isValid()); if ((m_editorValue->modelNode().isValid() && m_modelNode.isValid())) { - QmlDesigner::QmlObjectNode qmlObjectNode(m_modelNode); const QStringList propertyNames = m_valuesPropertyMap.keys(); for (const QString &propertyName : propertyNames) m_valuesPropertyMap.clear(propertyName); qDeleteAll(m_valuesPropertyMap.children()); - if (qmlObjectNode.isValid()) { + if (QmlDesigner::QmlObjectNode qmlObjectNode = m_modelNode) { for (const auto &property : m_modelNode.metaInfo().properties()) { const auto &propertyName = property.name(); auto valueObject = new PropertyEditorValue(&m_valuesPropertyMap); diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp index 183197a452d..66036118aa9 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp @@ -161,8 +161,8 @@ void PropertyEditorView::changeValue(const QString &name) QVariant castedValue; - if (metaInfo.isValid() && metaInfo.hasProperty(propertyName)) { - castedValue = metaInfo.property(propertyName).castedValue(value->value()); + if (auto property = metaInfo.property(propertyName)) { + castedValue = property.castedValue(value->value()); } else if (propertyIsAttachedLayoutProperty(propertyName)) { castedValue = value->value(); } else { @@ -177,8 +177,7 @@ void PropertyEditorView::changeValue(const QString &name) bool propertyTypeUrl = false; - if (metaInfo.isValid() && metaInfo.hasProperty(propertyName) - && metaInfo.property(propertyName).propertyType().isUrl()) { + if (metaInfo.property(propertyName).propertyType().isUrl()) { // turn absolute local file paths into relative paths propertyTypeUrl = true; QString filePath = castedValue.toUrl().toString(); @@ -247,9 +246,8 @@ void PropertyEditorView::changeExpression(const QString &propertyName) return; } - if (auto metaInfo = qmlObjectNode->modelNode().metaInfo(); - metaInfo.isValid() && metaInfo.hasProperty(name)) { - const auto &propertType = metaInfo.property(name).propertyType(); + if (auto property = qmlObjectNode->modelNode().metaInfo().property(name)) { + const auto &propertType = property.propertyType(); if (propertType.isColor()) { if (QColor(value->expression().remove('"')).isValid()) { qmlObjectNode->setVariantProperty(name, QColor(value->expression().remove('"'))); @@ -642,7 +640,7 @@ void PropertyEditorView::propertiesRemoved(const QList& proper if ("width" == property.name() || "height" == property.name()) { const QmlItemNode qmlItemNode = m_selectedNode; - if (qmlItemNode.isValid() && qmlItemNode.isInLayout()) + if (qmlItemNode.isInLayout()) resetPuppet(); }