QmlDesigner: NodeMetaInfo can now get exported type names

You can get all exported names. That can the C++ names too for C++
types. If you only want the types name which are imported in a document
you can provide the source id for the document and get only that. If
there are different version exports you get them too.

Task-number: QDS-10274
Change-Id: Iefc0b68ebfd65998cf0481961f8c7643cdf9e5b9
Reviewed-by: Vikas Pachdha <vikas.pachdha@qt.io>
This commit is contained in:
Marco Bubke
2023-07-12 16:43:38 +02:00
parent f7f65b9ea4
commit 3d5ceb795d
6 changed files with 111 additions and 10 deletions

View File

@@ -8,6 +8,7 @@
#include <documentmessage.h> #include <documentmessage.h>
#include <model/modelresourcemanagementinterface.h> #include <model/modelresourcemanagementinterface.h>
#include <projectstorage/projectstoragefwd.h> #include <projectstorage/projectstoragefwd.h>
#include <projectstorageids.h>
#include <QMimeData> #include <QMimeData>
#include <QObject> #include <QObject>
@@ -112,6 +113,7 @@ public:
} }
QUrl fileUrl() const; QUrl fileUrl() const;
SourceId fileUrlSourceId() const;
void setFileUrl(const QUrl &url); void setFileUrl(const QUrl &url);
const MetaInfo metaInfo() const; const MetaInfo metaInfo() const;

View File

@@ -76,6 +76,9 @@ public:
int majorVersion() const; int majorVersion() const;
int minorVersion() const; int minorVersion() const;
Storage::Info::ExportedTypeNames allExportedTypeNames() const;
Storage::Info::ExportedTypeNames exportedTypeNamesForSourceId(SourceId sourceId) const;
SourceId sourceId() const; SourceId sourceId() const;
QString componentFileName() const; QString componentFileName() const;

View File

@@ -1688,6 +1688,28 @@ int NodeMetaInfo::minorVersion() const
return -1; return -1;
} }
Storage::Info::ExportedTypeNames NodeMetaInfo::allExportedTypeNames() const
{
if constexpr (useProjectStorage()) {
if (isValid()) {
return m_projectStorage->exportedTypeNames(m_typeId);
}
}
return {};
}
Storage::Info::ExportedTypeNames NodeMetaInfo::exportedTypeNamesForSourceId(SourceId sourceId) const
{
if constexpr (useProjectStorage()) {
if (isValid()) {
return m_projectStorage->exportedTypeNames(m_typeId, sourceId);
}
}
return {};
}
SourceId NodeMetaInfo::sourceId() const SourceId NodeMetaInfo::sourceId() const
{ {
if constexpr (useProjectStorage()) { if constexpr (useProjectStorage()) {

View File

@@ -1970,6 +1970,11 @@ QUrl Model::fileUrl() const
return d->fileUrl(); return d->fileUrl();
} }
SourceId Model::fileUrlSourceId() const
{
return d->m_sourceId;
}
/*! /*!
\brief Sets the URL against which relative URLs within the model should be resolved. \brief Sets the URL against which relative URLs within the model should be resolved.
\param url the base URL, i.e. the qml file path. \param url the base URL, i.e. the qml file path.

View File

@@ -133,20 +133,20 @@ namespace QmlDesigner::Storage::Info {
class ExportedTypeName class ExportedTypeName
{ {
public: public:
explicit ExportedTypeName() = default; ExportedTypeName() = default;
explicit ExportedTypeName(ModuleId moduleId, ExportedTypeName(ModuleId moduleId,
::Utils::SmallStringView name, ::Utils::SmallStringView name,
Storage::Version version = Storage::Version{}) Storage::Version version = Storage::Version{})
: name{name} : name{name}
, version{version} , version{version}
, moduleId{moduleId} , moduleId{moduleId}
{} {}
explicit ExportedTypeName(ModuleId moduleId, ExportedTypeName(ModuleId moduleId,
::Utils::SmallStringView name, ::Utils::SmallStringView name,
int majorVersion, int majorVersion,
int minorVersion) int minorVersion)
: name{name} : name{name}
, version{majorVersion, minorVersion} , version{majorVersion, minorVersion}
, moduleId{moduleId} , moduleId{moduleId}

View File

@@ -3,8 +3,9 @@
#include "../utils/googletest.h" #include "../utils/googletest.h"
#include "../mocks/projectstoragemock.h" #include <matchers/info_exportedtypenames-matcher.h>
#include "../mocks/sourcepathcachemock.h" #include <mocks/projectstoragemock.h>
#include <mocks/sourcepathcachemock.h>
#include <designercore/include/model.h> #include <designercore/include/model.h>
#include <designercore/include/modelnode.h> #include <designercore/include/modelnode.h>
@@ -2231,4 +2232,72 @@ TEST_F(NodeMetaInfo, default_is_not_enumeration)
ASSERT_THAT(isType, IsFalse()); ASSERT_THAT(isType, IsFalse());
} }
TEST_F(NodeMetaInfo, all_external_type_names)
{
QmlDesigner::Storage::Info::ExportedTypeNames names{{qmlModuleId, "Object", 2, -1},
{qmlModuleId, "Obj", 2, 1}};
auto metaInfo = createMetaInfo("QML", "Foo");
ON_CALL(projectStorageMock, exportedTypeNames(metaInfo.id())).WillByDefault(Return(names));
auto exportedTypeNames = metaInfo.allExportedTypeNames();
ASSERT_THAT(exportedTypeNames,
UnorderedElementsAre(IsInfoExportTypeNames(qmlModuleId, "Object", 2, -1),
IsInfoExportTypeNames(qmlModuleId, "Obj", 2, 1)));
}
TEST_F(NodeMetaInfo, default_has_no_external_type_names)
{
QmlDesigner::Storage::Info::ExportedTypeNames names{{qmlModuleId, "Object", 2, -1},
{qmlModuleId, "Obj", 2, 1}};
QmlDesigner::NodeMetaInfo metaInfo;
ON_CALL(projectStorageMock, exportedTypeNames(_)).WillByDefault(Return(names));
auto exportedTypeNames = metaInfo.allExportedTypeNames();
ASSERT_THAT(exportedTypeNames, IsEmpty());
}
TEST_F(NodeMetaInfo, external_type_names_for_source_id)
{
QmlDesigner::Storage::Info::ExportedTypeNames names{{qmlModuleId, "Object", 2, -1},
{qmlModuleId, "Obj", 2, 1}};
auto metaInfo = createMetaInfo("QML", "Foo");
ON_CALL(projectStorageMock, exportedTypeNames(metaInfo.id(), model.fileUrlSourceId()))
.WillByDefault(Return(names));
auto exportedTypeNames = metaInfo.exportedTypeNamesForSourceId(model.fileUrlSourceId());
ASSERT_THAT(exportedTypeNames,
UnorderedElementsAre(IsInfoExportTypeNames(qmlModuleId, "Object", 2, -1),
IsInfoExportTypeNames(qmlModuleId, "Obj", 2, 1)));
}
TEST_F(NodeMetaInfo, default_has_no_external_type_names_for_source_id)
{
QmlDesigner::Storage::Info::ExportedTypeNames names{{qmlModuleId, "Object", 2, -1},
{qmlModuleId, "Obj", 2, 1}};
QmlDesigner::NodeMetaInfo metaInfo;
ON_CALL(projectStorageMock, exportedTypeNames(metaInfo.id(), model.fileUrlSourceId()))
.WillByDefault(Return(names));
auto exportedTypeNames = metaInfo.exportedTypeNamesForSourceId(model.fileUrlSourceId());
ASSERT_THAT(exportedTypeNames, IsEmpty());
}
TEST_F(NodeMetaInfo, invalid_source_id_has_no_external_type_names_for_source_id)
{
QmlDesigner::Storage::Info::ExportedTypeNames names{{qmlModuleId, "Object", 2, -1},
{qmlModuleId, "Obj", 2, 1}};
auto metaInfo = createMetaInfo("QML", "Foo");
ON_CALL(projectStorageMock, exportedTypeNames(metaInfo.id(), model.fileUrlSourceId()))
.WillByDefault(Return(names));
QmlDesigner::SourceId sourceId;
auto exportedTypeNames = metaInfo.exportedTypeNamesForSourceId(sourceId);
ASSERT_THAT(exportedTypeNames, IsEmpty());
}
} // namespace } // namespace