From b99bc1a0550b535ccce2eb05a18d38ec72bf6709 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Wed, 10 Aug 2022 12:06:00 +0200 Subject: [PATCH] QmlDesigner: Adapt PropertyMetaInfo::castedValue It tries to avoid calls to propertyTypeName() because it is slow and unreliable. Instead we use the builtin type ids. If there are more common cases we should add more builtin type ids. Change-Id: I7f6b6c4251fd275878c476526c75058a8580b4a7 Reviewed-by: Tim Jenssen Reviewed-by: Reviewed-by: Thomas Hartmann --- .../designercore/metainfo/nodemetainfo.cpp | 124 ++++++++++++------ .../projectstorage/commontypecache.h | 61 ++++++++- 2 files changed, 146 insertions(+), 39 deletions(-) diff --git a/src/plugins/qmldesigner/designercore/metainfo/nodemetainfo.cpp b/src/plugins/qmldesigner/designercore/metainfo/nodemetainfo.cpp index 160528c9ab2..0fa54c4da76 100644 --- a/src/plugins/qmldesigner/designercore/metainfo/nodemetainfo.cpp +++ b/src/plugins/qmldesigner/designercore/metainfo/nodemetainfo.cpp @@ -33,14 +33,17 @@ #include #include -#include #include +#include +#include +#include +#include -#include +#include #include #include +#include #include -#include #include #include @@ -1136,7 +1139,7 @@ QVariant::Type NodeMetaInfoPrivate::variantTypeId(const PropertyName &propertyNa if (typeName == "vector4d") return QVariant::Vector4D; - return QVariant::nameToType(typeName.data()); + return QVariant::nameToType(typeName.data()); // This is deprecated } int NodeMetaInfoPrivate::majorVersion() const @@ -1849,44 +1852,91 @@ bool PropertyMetaInfo::isPointer() const QVariant PropertyMetaInfo::castedValue(const QVariant &value) const { - const QVariant variant = value; - QVariant copyVariant = variant; - if (isEnumType() || variant.canConvert()) - return variant; + if constexpr (!useProjectStorage()) { + const QVariant variant = value; + QVariant copyVariant = variant; + if (isEnumType() || variant.canConvert()) + return variant; - const TypeName &typeName = propertyTypeName(); + const TypeName &typeName = propertyTypeName(); - QVariant::Type typeId = m_nodeMetaInfoPrivateData->variantTypeId(m_propertyName); + QVariant::Type typeId = m_nodeMetaInfoPrivateData->variantTypeId(m_propertyName); - if (variant.type() == QVariant::UserType && variant.userType() == ModelNode::variantUserType()) { - return variant; - } else if (typeId == QVariant::UserType && typeName == "QVariant") { - return variant; - } else if (typeId == QVariant::UserType && typeName == "variant") { - return variant; - } else if (typeId == QVariant::UserType && typeName == "var") { - return variant; - } else if (variant.type() == QVariant::List) { - // TODO: check the contents of the list - return variant; - } else if (typeName == "var" || typeName == "variant") { - return variant; - } else if (typeName == "alias") { - // TODO: The QML compiler resolves the alias type. We probably should do the same. - return variant; - } else if (typeName == ".double") { - return variant.toDouble(); - } else if (typeName == ".float") { - return variant.toFloat(); - } else if (typeName == ".int") { - return variant.toInt(); - } else if (typeName == ".bool") { - return variant.toBool(); - } else if (copyVariant.convert(typeId)) { - return copyVariant; + if (variant.type() == QVariant::UserType + && variant.userType() == ModelNode::variantUserType()) { + return variant; + } else if (typeId == QVariant::UserType && typeName == "QVariant") { + return variant; + } else if (typeId == QVariant::UserType && typeName == "variant") { + return variant; + } else if (typeId == QVariant::UserType && typeName == "var") { + return variant; + } else if (variant.type() == QVariant::List) { + // TODO: check the contents of the list + return variant; + } else if (typeName == "var" || typeName == "variant") { + return variant; + } else if (typeName == "alias") { + // TODO: The QML compiler resolves the alias type. We probably should do the same. + return variant; + } else if (typeName == ".double") { + return variant.toDouble(); + } else if (typeName == ".float") { + return variant.toFloat(); + } else if (typeName == ".int") { + return variant.toInt(); + } else if (typeName == ".bool") { + return variant.toBool(); + } else if (copyVariant.convert(typeId)) { + return copyVariant; + } + + } else { + if (isEnumType() || value.canConvert()) + return value; + + const TypeId &typeId = propertyData().typeId; + + if (value.type() == QVariant::UserType && value.userType() == ModelNode::variantUserType()) { + return value; + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value; + } else if (value.type() == QVariant::List) { + // TODO: check the contents of the list + return value; + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.toDouble(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.toFloat(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.toInt(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.toBool(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.toString(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.toDateTime(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.toUrl(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.value(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.value(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.value(); + } else if (typeId == m_projectStorage->builtinTypeId()) { + return value.value(); + } else { + const auto typeName = propertyTypeName(); + const auto metaType = QMetaType::fromName(typeName); + auto copy = value; + bool converted = copy.convert(metaType); + if (converted) + return copy; + } } - return Internal::PropertyParser::variantFromString(variant.toString()); + return Internal::PropertyParser::variantFromString(value.toString()); } const Storage::Info::PropertyDeclaration &PropertyMetaInfo::propertyData() const diff --git a/src/plugins/qmldesigner/designercore/projectstorage/commontypecache.h b/src/plugins/qmldesigner/designercore/projectstorage/commontypecache.h index 06307be0f84..9ce183efe17 100644 --- a/src/plugins/qmldesigner/designercore/projectstorage/commontypecache.h +++ b/src/plugins/qmldesigner/designercore/projectstorage/commontypecache.h @@ -30,13 +30,35 @@ #include #include +QT_BEGIN_NAMESPACE +class QColor; +class QDateTime; +class QString; +class QUrl; +class QVariant; +class QVector2D; +class QVector3D; +class QVector4D; +QT_END_NAMESPACE + namespace QmlDesigner::Storage::Info { inline constexpr char QtQuick[] = "QtQuick"; inline constexpr char QML[] = "QML"; +inline constexpr char QMLNative[] = "QML-cppnative"; inline constexpr char Item[] = "Item"; inline constexpr char DoubleType[] = "double"; +inline constexpr char IntType[] = "int"; +inline constexpr char BoolType[] = "bool"; +inline constexpr char FloatType[] = "float"; inline constexpr char var[] = "var"; +inline constexpr char string[] = "string"; +inline constexpr char date[] = "date"; +inline constexpr char url[] = "url"; +inline constexpr char color[] = "color"; +inline constexpr char vector2d[] = "vector2d"; +inline constexpr char vector3d[] = "vector3d"; +inline constexpr char vector4d[] = "vector4d"; template struct CacheType @@ -48,7 +70,20 @@ struct CacheType template class CommonTypeCache { - using CommonTypes = std::tuple, CacheType, CacheType>; + using CommonTypes = std::tuple, + CacheType, + CacheType, + CacheType, + CacheType, + CacheType, + CacheType, + CacheType, + CacheType, + CacheType, + CacheType, + CacheType, + CacheType, + CacheType>; public: CommonTypeCache(const ProjectStorage &projectStorage) @@ -87,7 +122,29 @@ public: auto builtinTypeId() const { if constexpr (std::is_same_v) - return builtinTypeId(); + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); + if constexpr (std::is_same_v) + return typeId(); else return TypeId{}; }