diff --git a/src/plugins/qmldesigner/libs/designsystem/CMakeLists.txt b/src/plugins/qmldesigner/libs/designsystem/CMakeLists.txt index b85e4a0dc22..880fe74e02f 100644 --- a/src/plugins/qmldesigner/libs/designsystem/CMakeLists.txt +++ b/src/plugins/qmldesigner/libs/designsystem/CMakeLists.txt @@ -2,6 +2,8 @@ add_qtc_library(DesignSystem STATIC PUBLIC_INCLUDES ${CMAKE_CURRENT_LIST_DIR} DEPENDS Qt::Core Qt::Widgets QmlDesignerCore TextEditorSupport + PUBLIC_DEFINES + $<$:QDS_USE_PROJECTSTORAGE> SOURCES dsconstants.h dsstore.h dsstore.cpp diff --git a/src/plugins/qmldesigner/libs/designsystem/dsstore.cpp b/src/plugins/qmldesigner/libs/designsystem/dsstore.cpp index 26ab0f9001c..10ed7d11de4 100644 --- a/src/plugins/qmldesigner/libs/designsystem/dsstore.cpp +++ b/src/plugins/qmldesigner/libs/designsystem/dsstore.cpp @@ -5,6 +5,7 @@ #include "dsthememanager.h" #include +#include #include #include #include @@ -49,17 +50,28 @@ static QByteArray reformatQml(const QString &content) return content.toUtf8(); } -std::optional modelSerializeHelper(QmlDesigner::ExternalDependenciesInterface &ed, - std::function callback, - const Utils::FilePath &targetDir, - const QString &typeName, - bool isSingelton = false) +std::optional modelSerializeHelper( + QmlDesigner::ProjectStorageDependencies &projectStorageDependencies, + QmlDesigner::ExternalDependenciesInterface &ed, + std::function callback, + const Utils::FilePath &targetDir, + const QString &typeName, + bool isSingelton = false) { QString qmlText{"import QtQuick\nQtObject {}\n"}; if (isSingelton) qmlText.prepend("pragma Singleton\n"); - QmlDesigner::ModelPointer model(QmlDesigner::Model::create("QtObject")); +#ifdef QDS_USE_PROJECTSTORAGE + auto model = QmlDesigner::Model::create(projectStorageDependencies, + "QtObject", + {QmlDesigner::Import::createLibraryImport("QtQtuick")}, + QUrl::fromLocalFile( + "/path/dummy.qml")); // the dummy file will most probably not work +#else + auto model = QmlDesigner::Model::create("QtObject"); +#endif + QPlainTextEdit editor; editor.setPlainText(qmlText); QmlDesigner::NotIndentingTextEditModifier modifier(&editor); @@ -87,8 +99,10 @@ std::optional modelSerializeHelper(QmlDesigner::ExternalDependenciesInt namespace QmlDesigner { -DSStore::DSStore(ExternalDependenciesInterface &ed) +DSStore::DSStore(ExternalDependenciesInterface &ed, + ProjectStorageDependencies projectStorageDependencies) : m_ed(ed) + , m_projectStorageDependencies(projectStorageDependencies) {} DSStore::~DSStore() {} @@ -147,7 +161,7 @@ std::optional DSStore::load(const Utils::FilePath &dsModuleDirPath) return {}; } -std::optional DSStore::save(bool mcuCompatible) const +std::optional DSStore::save(bool mcuCompatible) { if (auto moduleDir = dsModuleDir(m_ed)) return save(*moduleDir, mcuCompatible); @@ -155,7 +169,7 @@ std::optional DSStore::save(bool mcuCompatible) const return tr("Can not locate design system module"); } -std::optional DSStore::save(const Utils::FilePath &moduleDirPath, bool mcuCompatible) const +std::optional DSStore::save(const Utils::FilePath &moduleDirPath, bool mcuCompatible) { if (!QDir().mkpath(moduleDirPath.absoluteFilePath().toString())) return tr("Can not create design system module directory %1.").arg(moduleDirPath.toString()); @@ -215,8 +229,15 @@ std::optional DSStore::loadCollection(const QString &typeName, if (!reader.fetch(qmlFilePath, QFile::Text)) return reader.errorString(); - ModelPointer model(QmlDesigner::Model::create("QtObject")); - +#ifdef QDS_USE_PROJECTSTORAGE + auto model = QmlDesigner::Model::create(m_projectStorageDependencies, + "QtObject", + {QmlDesigner::Import::createLibraryImport("QtQtuick")}, + QUrl::fromLocalFile( + "/path/dummy.qml")); // the dummy file will most probably not work +#else + auto model = QmlDesigner::Model::create("QtObject"); +#endif QPlainTextEdit editor; QString qmlContent = QString::fromUtf8(reader.data()); editor.setPlainText(qmlContent); @@ -238,7 +259,7 @@ std::optional DSStore::loadCollection(const QString &typeName, std::optional DSStore::writeQml(const DSThemeManager &mgr, const QString &typeName, const Utils::FilePath &targetDir, - bool mcuCompatible) const + bool mcuCompatible) { if (mgr.themeCount() == 0) return {}; @@ -249,7 +270,8 @@ std::optional DSStore::writeQml(const DSThemeManager &mgr, mgr.decorateThemeInterface(interfaceModel->rootModelNode()); }; - if (auto error = modelSerializeHelper(m_ed, decorateInterface, targetDir, themeInterfaceType)) + if (auto error = modelSerializeHelper( + m_projectStorageDependencies, m_ed, decorateInterface, targetDir, themeInterfaceType)) return tr("Can not write theme interface %1.\n%2").arg(themeInterfaceType, *error); } @@ -257,7 +279,8 @@ std::optional DSStore::writeQml(const DSThemeManager &mgr, mgr.decorate(collectionModel->rootModelNode(), themeInterfaceType.toUtf8(), mcuCompatible); }; - if (auto error = modelSerializeHelper(m_ed, decorateCollection, targetDir, typeName, true)) + if (auto error = modelSerializeHelper( + m_projectStorageDependencies, m_ed, decorateCollection, targetDir, typeName, true)) return tr("Can not write collection %1.\n%2").arg(typeName, *error); return {}; diff --git a/src/plugins/qmldesigner/libs/designsystem/dsstore.h b/src/plugins/qmldesigner/libs/designsystem/dsstore.h index 585a38c39e0..03e8e1ddc21 100644 --- a/src/plugins/qmldesigner/libs/designsystem/dsstore.h +++ b/src/plugins/qmldesigner/libs/designsystem/dsstore.h @@ -14,10 +14,10 @@ using DSCollections = std::map; class DSStore { - Q_DECLARE_TR_FUNCTIONS(DSStore); + Q_DECLARE_TR_FUNCTIONS(DSStore) public: - DSStore(ExternalDependenciesInterface &ed); + DSStore(ExternalDependenciesInterface &ed, ProjectStorageDependencies projectStorageDependencies); ~DSStore(); QString moduleImportStr() const; @@ -25,8 +25,8 @@ public: std::optional load(); std::optional load(const Utils::FilePath &dsModuleDirPath); - std::optional save(bool mcuCompatible = false) const; - std::optional save(const Utils::FilePath &moduleDirPath, bool mcuCompatible = false) const; + std::optional save(bool mcuCompatible = false); + std::optional save(const Utils::FilePath &moduleDirPath, bool mcuCompatible = false); size_t collectionCount() const { return m_collections.size(); } @@ -38,10 +38,11 @@ private: std::optional writeQml(const DSThemeManager &mgr, const QString &typeName, const Utils::FilePath &targetDir, - bool mcuCompatible) const; + bool mcuCompatible); private: ExternalDependenciesInterface &m_ed; + ProjectStorageDependencies m_projectStorageDependencies; DSCollections m_collections; std::map m_collectionTypeNames; };