From bcd9c2e8f2aeeca9eb17d5cf355f1b25524b83f9 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Thu, 30 Jun 2022 18:57:07 +0200 Subject: [PATCH] QmlDesigner: Improve Has*TypeName interface Instead of putting some strings into a list let the compiler to his job and put it in a compile time tuple. This has the advantage too that it can be of different types. Change-Id: Ic4e7a9da8994d2a43ca115c1d69eab622c610437 Reviewed-by: Thomas Hartmann Reviewed-by: --- .../components/bindingeditor/actioneditor.cpp | 22 ++++++++----------- .../designercore/include/abstractproperty.h | 15 +++++++++++++ .../designercore/include/propertymetainfo.h | 7 ++++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/plugins/qmldesigner/components/bindingeditor/actioneditor.cpp b/src/plugins/qmldesigner/components/bindingeditor/actioneditor.cpp index c10c7498f99..0e12f3cb31e 100644 --- a/src/plugins/qmldesigner/components/bindingeditor/actioneditor.cpp +++ b/src/plugins/qmldesigner/components/bindingeditor/actioneditor.cpp @@ -42,6 +42,8 @@ #include #include +#include + static Q_LOGGING_CATEGORY(ceLog, "qtc.qmldesigner.connectioneditor", QtWarningMsg) namespace QmlDesigner { @@ -190,11 +192,8 @@ void ActionEditor::prepareConnections() const QmlJS::ContextPtr &context = semanticInfo.context; const QmlJS::ScopeChain &scopeChain = semanticInfo.scopeChain(path); - static QList typeWhiteList({"string", - "real", "int", "double", - "bool", - "QColor", "color", - "QtQuick.Item", "QQuickItem"}); + constexpr auto typeWhiteList = std::make_tuple( + "string", "real", "int", "double", "bool", "QColor", "color", "QtQuick.Item", "QQuickItem"); static QList methodBlackList({"toString", "destroy"}); @@ -211,19 +210,18 @@ void ActionEditor::prepareConnections() ActionEditorDialog::ConnectionOption connection(modelNode.id()); for (const auto &property : modelNode.metaInfo().properties()) { - const auto &propertyTypeName = property.propertyTypeName(); - if (!typeWhiteList.contains(propertyTypeName)) + if (!property.hasPropertyTypeName(typeWhiteList)) continue; connection.properties.append( ActionEditorDialog::PropertyOption(QString::fromUtf8(property.name()), - skipCpp(std::move(propertyTypeName)), + skipCpp(std::move(property.propertyTypeName())), property.isWritable())); } for (const VariantProperty &variantProperty : modelNode.variantProperties()) { if (variantProperty.isValid() && variantProperty.isDynamic()) { - if (!typeWhiteList.contains(variantProperty.dynamicTypeName())) + if (!variantProperty.hasDynamicTypeName(typeWhiteList)) continue; const QString name = QString::fromUtf8(variantProperty.name()); @@ -271,14 +269,12 @@ void ActionEditor::prepareConnections() if (metaInfo.isValid()) { ActionEditorDialog::SingletonOption singelton; for (const auto &property : metaInfo.properties()) { - const TypeName &typeName = property.propertyTypeName(); - - if (!typeWhiteList.contains(typeName)) + if (!property.hasPropertyTypeName(typeWhiteList)) continue; singelton.properties.append( ActionEditorDialog::PropertyOption(QString::fromUtf8(property.name()), - skipCpp(typeName), + skipCpp(property.propertyTypeName()), property.isWritable())); } diff --git a/src/plugins/qmldesigner/designercore/include/abstractproperty.h b/src/plugins/qmldesigner/designercore/include/abstractproperty.h index cb717664454..c5d83ecf34d 100644 --- a/src/plugins/qmldesigner/designercore/include/abstractproperty.h +++ b/src/plugins/qmldesigner/designercore/include/abstractproperty.h @@ -99,6 +99,21 @@ public: bool isDynamic() const; TypeName dynamicTypeName() const; + template + bool hasDynamicTypeName(const TypeName &...typeName) const + { + auto dynamicTypeName_ = dynamicTypeName(); + return ((dynamicTypeName_ == typeName) || ...); + } + + template + bool hasDynamicTypeName(const std::tuple &typeNames) const + { + auto dynamicTypeName_ = dynamicTypeName(); + return std::apply([&](auto... typeName) { return hasDynamicTypeName(typeName...); }, + typeNames); + } + Model *model() const; AbstractView *view() const; diff --git a/src/plugins/qmldesigner/designercore/include/propertymetainfo.h b/src/plugins/qmldesigner/designercore/include/propertymetainfo.h index 994ac7479ff..6463157a9e6 100644 --- a/src/plugins/qmldesigner/designercore/include/propertymetainfo.h +++ b/src/plugins/qmldesigner/designercore/include/propertymetainfo.h @@ -59,6 +59,13 @@ public: return ((propertyTypeName_ == typeName) || ...); } + template + bool hasPropertyTypeName(const std::tuple &typeNames) const + { + return std::apply([&](auto... typeName) { return hasPropertyTypeName(typeName...); }, + typeNames); + } + bool propertyTypeNameIsColor() const { return hasPropertyTypeName("QColor", "color"); } bool propertyTypeNameIsString() const { return hasPropertyTypeName("QString", "string"); } bool propertyTypeNameIsUrl() const { return hasPropertyTypeName("QUrl", "url"); }