From fe50af151d8c6e7e06e176b0bd5d1dd851126688 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Thu, 30 Jun 2022 18:04:32 +0200 Subject: [PATCH] QmlDesigner: Simply property type name check Move some repetitive pattern to PropertyMetaInfo Change-Id: Iba8722d3d95881c1db85c12cbc2b2b8c4b629f2c Reviewed-by: Thomas Hartmann Reviewed-by: Qt CI Bot --- .../components/materialeditor/materialeditorview.cpp | 2 +- .../propertyeditor/propertyeditorvalue.cpp | 12 ++++++------ .../components/propertyeditor/propertyeditorview.cpp | 2 +- .../qmldesigner/components/sourcetool/sourcetool.cpp | 2 +- .../designercore/include/propertymetainfo.h | 4 ++++ .../qmldesigner/designercore/model/qmlobjectnode.cpp | 2 +- .../designercore/include/propertymetainfo.h | 2 ++ 7 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/plugins/qmldesigner/components/materialeditor/materialeditorview.cpp b/src/plugins/qmldesigner/components/materialeditor/materialeditorview.cpp index bd86d1b0430..c9c5dd4ed1d 100644 --- a/src/plugins/qmldesigner/components/materialeditor/materialeditorview.cpp +++ b/src/plugins/qmldesigner/components/materialeditor/materialeditorview.cpp @@ -139,7 +139,7 @@ void MaterialEditorView::changeValue(const QString &name) bool propertyTypeUrl = false; if (metaInfo.isValid() && metaInfo.hasProperty(propertyName)) { - if (metaInfo.property(propertyName).hasPropertyTypeName("QUrl", "url")) { + if (metaInfo.property(propertyName).propertyTypeNameIsUrl()) { // turn absolute local file paths into relative paths propertyTypeUrl = true; QString filePath = castedValue.toUrl().toString(); diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorvalue.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorvalue.cpp index 6384dcb24df..59e8af563d4 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorvalue.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorvalue.cpp @@ -63,7 +63,7 @@ QVariant PropertyEditorValue::value() const if (modelNode().isValid()) { if (auto metaInfo = modelNode().metaInfo(); metaInfo.isValid() && metaInfo.hasProperty(name()) - && metaInfo.property(name()).hasPropertyTypeName("QUrl", "url")) { + && metaInfo.property(name()).propertyTypeNameIsUrl()) { returnValue = returnValue.toUrl().toString(); } } @@ -106,7 +106,7 @@ static void fixAmbigousColorNames(const QmlDesigner::ModelNode &modelNode, { if (modelNode.isValid()) { if (auto metaInfo = modelNode.metaInfo(); - metaInfo.isValid() && metaInfo.property(name).hasPropertyTypeName("QColor", "color")) { + metaInfo.isValid() && metaInfo.property(name).propertyTypeNameIsColor()) { if ((value->type() == QVariant::Color)) { QColor color = value->value(); int alpha = color.alpha(); @@ -124,7 +124,7 @@ static void fixUrl(const QmlDesigner::ModelNode &modelNode, const QmlDesigner::P { if (modelNode.isValid()) { if (auto metaInfo = modelNode.metaInfo(); - metaInfo.isValid() && metaInfo.property(name).hasPropertyTypeName("QUrl", "url")) + metaInfo.isValid() && metaInfo.property(name).propertyTypeNameIsUrl()) if (!value->isValid()) *value = QStringLiteral(""); @@ -149,7 +149,7 @@ void PropertyEditorValue::setValueWithEmit(const QVariant &value) if (modelNode().isValid()) { if (auto metaInfo = modelNode().metaInfo(); metaInfo.isValid() && metaInfo.hasProperty(name()) - && metaInfo.property(name()).hasPropertyTypeName("QUrl")) { + && metaInfo.property(name()).propertyTypeNameIsUrl()) { newValue = QUrl(newValue.toString()); } } @@ -268,7 +268,7 @@ bool PropertyEditorValue::isTranslated() const if (modelNode().isValid()) { if (auto metaInfo = modelNode().metaInfo(); metaInfo.isValid() && metaInfo.hasProperty(name()) - && metaInfo.property(name()).hasPropertyTypeName("QString", "string")) { + && metaInfo.property(name()).propertyTypeNameIsString()) { const QmlDesigner::QmlObjectNode objectNode(modelNode()); if (objectNode.isValid() && objectNode.hasBindingProperty(name())) { const QRegularExpression rx( @@ -438,7 +438,7 @@ QString PropertyEditorValue::getTranslationContext() const if (modelNode().isValid()) { if (auto metaInfo = modelNode().metaInfo(); metaInfo.isValid() && metaInfo.hasProperty(name()) - && metaInfo.property(name()).hasPropertyTypeName("QString", "string")) { + && metaInfo.property(name()).propertyTypeNameIsString()) { const QmlDesigner::QmlObjectNode objectNode(modelNode()); if (objectNode.isValid() && objectNode.hasBindingProperty(name())) { const QRegularExpression rx(QRegularExpression::anchoredPattern( diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp index 21cbfef72cd..6d00c56d3ac 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp @@ -201,7 +201,7 @@ void PropertyEditorView::changeValue(const QString &name) bool propertyTypeUrl = false; if (metaInfo.isValid() && metaInfo.hasProperty(propertyName) - && metaInfo.property(propertyName).hasPropertyTypeName("QUrl", "url")) { + && metaInfo.property(propertyName).propertyTypeNameIsUrl()) { // turn absolute local file paths into relative paths propertyTypeUrl = true; QString filePath = castedValue.toUrl().toString(); diff --git a/src/plugins/qmldesigner/components/sourcetool/sourcetool.cpp b/src/plugins/qmldesigner/components/sourcetool/sourcetool.cpp index 751624e9758..6ce5c488511 100644 --- a/src/plugins/qmldesigner/components/sourcetool/sourcetool.cpp +++ b/src/plugins/qmldesigner/components/sourcetool/sourcetool.cpp @@ -54,7 +54,7 @@ bool modelNodeHasUrlSource(const QmlDesigner::ModelNode &modelNode) { QmlDesigner::NodeMetaInfo metaInfo = modelNode.metaInfo(); return metaInfo.isValid() && metaInfo.hasProperty("source") - && metaInfo.property("source").hasPropertyTypeName("QUrl", "url"); + && metaInfo.property("source").propertyTypeNameIsUrl(); } } //namespace diff --git a/src/plugins/qmldesigner/designercore/include/propertymetainfo.h b/src/plugins/qmldesigner/designercore/include/propertymetainfo.h index 7502b789236..994ac7479ff 100644 --- a/src/plugins/qmldesigner/designercore/include/propertymetainfo.h +++ b/src/plugins/qmldesigner/designercore/include/propertymetainfo.h @@ -59,6 +59,10 @@ public: return ((propertyTypeName_ == typeName) || ...); } + bool propertyTypeNameIsColor() const { return hasPropertyTypeName("QColor", "color"); } + bool propertyTypeNameIsString() const { return hasPropertyTypeName("QString", "string"); } + bool propertyTypeNameIsUrl() const { return hasPropertyTypeName("QUrl", "url"); } + private: QSharedPointer m_nodeMetaInfoPrivateData; PropertyName m_propertyName; diff --git a/src/plugins/qmldesigner/designercore/model/qmlobjectnode.cpp b/src/plugins/qmldesigner/designercore/model/qmlobjectnode.cpp index 88bf0a426d4..3804aaa1042 100644 --- a/src/plugins/qmldesigner/designercore/model/qmlobjectnode.cpp +++ b/src/plugins/qmldesigner/designercore/model/qmlobjectnode.cpp @@ -247,7 +247,7 @@ QVariant QmlObjectNode::modelValue(const PropertyName &name) const bool QmlObjectNode::isTranslatableText(const PropertyName &name) const { if (modelNode().metaInfo().isValid() && modelNode().metaInfo().hasProperty(name) - && modelNode().metaInfo().property(name).hasPropertyTypeName("QString", "string")) { + && modelNode().metaInfo().property(name).propertyTypeNameIsString()) { if (modelNode().hasBindingProperty(name)) { static QRegularExpression regularExpressionPattern( QLatin1String("^qsTr(|Id|anslate)\\(\".*\"\\)$")); diff --git a/tests/unit/mockup/qmldesigner/designercore/include/propertymetainfo.h b/tests/unit/mockup/qmldesigner/designercore/include/propertymetainfo.h index 82b0a6ed92b..db7ea0bad9e 100644 --- a/tests/unit/mockup/qmldesigner/designercore/include/propertymetainfo.h +++ b/tests/unit/mockup/qmldesigner/designercore/include/propertymetainfo.h @@ -62,6 +62,8 @@ public: auto propertyTypeName_ = propertyTypeName(); return ((propertyTypeName_ == typeName) && ...); } + + bool propertyTypeNameIsUrl() const { return hasPropertyTypeName("QUrl", "url"); } }; using PropertyMetaInfos = std::vector;