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 <thomas.hartmann@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Marco Bubke
2022-06-30 18:57:07 +02:00
parent fe50af151d
commit bcd9c2e8f2
3 changed files with 31 additions and 13 deletions

View File

@@ -42,6 +42,8 @@
#include <qmljs/qmljsscopechain.h> #include <qmljs/qmljsscopechain.h>
#include <qmljs/qmljsvalueowner.h> #include <qmljs/qmljsvalueowner.h>
#include <tuple>
static Q_LOGGING_CATEGORY(ceLog, "qtc.qmldesigner.connectioneditor", QtWarningMsg) static Q_LOGGING_CATEGORY(ceLog, "qtc.qmldesigner.connectioneditor", QtWarningMsg)
namespace QmlDesigner { namespace QmlDesigner {
@@ -190,11 +192,8 @@ void ActionEditor::prepareConnections()
const QmlJS::ContextPtr &context = semanticInfo.context; const QmlJS::ContextPtr &context = semanticInfo.context;
const QmlJS::ScopeChain &scopeChain = semanticInfo.scopeChain(path); const QmlJS::ScopeChain &scopeChain = semanticInfo.scopeChain(path);
static QList<TypeName> typeWhiteList({"string", constexpr auto typeWhiteList = std::make_tuple(
"real", "int", "double", "string", "real", "int", "double", "bool", "QColor", "color", "QtQuick.Item", "QQuickItem");
"bool",
"QColor", "color",
"QtQuick.Item", "QQuickItem"});
static QList<PropertyName> methodBlackList({"toString", "destroy"}); static QList<PropertyName> methodBlackList({"toString", "destroy"});
@@ -211,19 +210,18 @@ void ActionEditor::prepareConnections()
ActionEditorDialog::ConnectionOption connection(modelNode.id()); ActionEditorDialog::ConnectionOption connection(modelNode.id());
for (const auto &property : modelNode.metaInfo().properties()) { for (const auto &property : modelNode.metaInfo().properties()) {
const auto &propertyTypeName = property.propertyTypeName(); if (!property.hasPropertyTypeName(typeWhiteList))
if (!typeWhiteList.contains(propertyTypeName))
continue; continue;
connection.properties.append( connection.properties.append(
ActionEditorDialog::PropertyOption(QString::fromUtf8(property.name()), ActionEditorDialog::PropertyOption(QString::fromUtf8(property.name()),
skipCpp(std::move(propertyTypeName)), skipCpp(std::move(property.propertyTypeName())),
property.isWritable())); property.isWritable()));
} }
for (const VariantProperty &variantProperty : modelNode.variantProperties()) { for (const VariantProperty &variantProperty : modelNode.variantProperties()) {
if (variantProperty.isValid() && variantProperty.isDynamic()) { if (variantProperty.isValid() && variantProperty.isDynamic()) {
if (!typeWhiteList.contains(variantProperty.dynamicTypeName())) if (!variantProperty.hasDynamicTypeName(typeWhiteList))
continue; continue;
const QString name = QString::fromUtf8(variantProperty.name()); const QString name = QString::fromUtf8(variantProperty.name());
@@ -271,14 +269,12 @@ void ActionEditor::prepareConnections()
if (metaInfo.isValid()) { if (metaInfo.isValid()) {
ActionEditorDialog::SingletonOption singelton; ActionEditorDialog::SingletonOption singelton;
for (const auto &property : metaInfo.properties()) { for (const auto &property : metaInfo.properties()) {
const TypeName &typeName = property.propertyTypeName(); if (!property.hasPropertyTypeName(typeWhiteList))
if (!typeWhiteList.contains(typeName))
continue; continue;
singelton.properties.append( singelton.properties.append(
ActionEditorDialog::PropertyOption(QString::fromUtf8(property.name()), ActionEditorDialog::PropertyOption(QString::fromUtf8(property.name()),
skipCpp(typeName), skipCpp(property.propertyTypeName()),
property.isWritable())); property.isWritable()));
} }

View File

@@ -99,6 +99,21 @@ public:
bool isDynamic() const; bool isDynamic() const;
TypeName dynamicTypeName() const; TypeName dynamicTypeName() const;
template<typename... TypeName>
bool hasDynamicTypeName(const TypeName &...typeName) const
{
auto dynamicTypeName_ = dynamicTypeName();
return ((dynamicTypeName_ == typeName) || ...);
}
template<typename... TypeName>
bool hasDynamicTypeName(const std::tuple<TypeName...> &typeNames) const
{
auto dynamicTypeName_ = dynamicTypeName();
return std::apply([&](auto... typeName) { return hasDynamicTypeName(typeName...); },
typeNames);
}
Model *model() const; Model *model() const;
AbstractView *view() const; AbstractView *view() const;

View File

@@ -59,6 +59,13 @@ public:
return ((propertyTypeName_ == typeName) || ...); return ((propertyTypeName_ == typeName) || ...);
} }
template<typename... TypeName>
bool hasPropertyTypeName(const std::tuple<TypeName...> &typeNames) const
{
return std::apply([&](auto... typeName) { return hasPropertyTypeName(typeName...); },
typeNames);
}
bool propertyTypeNameIsColor() const { return hasPropertyTypeName("QColor", "color"); } bool propertyTypeNameIsColor() const { return hasPropertyTypeName("QColor", "color"); }
bool propertyTypeNameIsString() const { return hasPropertyTypeName("QString", "string"); } bool propertyTypeNameIsString() const { return hasPropertyTypeName("QString", "string"); }
bool propertyTypeNameIsUrl() const { return hasPropertyTypeName("QUrl", "url"); } bool propertyTypeNameIsUrl() const { return hasPropertyTypeName("QUrl", "url"); }