QmlDesigner: Add project storage to dsstore

Change-Id: I8717986b6943970ba4df9b30f72fb9fb1626d666
Reviewed-by: Vikas Pachdha <vikas.pachdha@qt.io>
This commit is contained in:
Marco Bubke
2024-10-23 20:30:31 +02:00
parent 44fcb093dc
commit 3eb89de645
3 changed files with 45 additions and 19 deletions

View File

@@ -2,6 +2,8 @@ add_qtc_library(DesignSystem STATIC
PUBLIC_INCLUDES ${CMAKE_CURRENT_LIST_DIR} PUBLIC_INCLUDES ${CMAKE_CURRENT_LIST_DIR}
DEPENDS DEPENDS
Qt::Core Qt::Widgets QmlDesignerCore TextEditorSupport Qt::Core Qt::Widgets QmlDesignerCore TextEditorSupport
PUBLIC_DEFINES
$<$<BOOL:${USE_PROJECTSTORAGE}>:QDS_USE_PROJECTSTORAGE>
SOURCES SOURCES
dsconstants.h dsconstants.h
dsstore.h dsstore.cpp dsstore.h dsstore.cpp

View File

@@ -5,6 +5,7 @@
#include "dsthememanager.h" #include "dsthememanager.h"
#include <generatedcomponentutils.h> #include <generatedcomponentutils.h>
#include <import.h>
#include <plaintexteditmodifier.h> #include <plaintexteditmodifier.h>
#include <qmljs/parser/qmldirparser_p.h> #include <qmljs/parser/qmldirparser_p.h>
#include <qmljs/qmljsreformatter.h> #include <qmljs/qmljsreformatter.h>
@@ -49,17 +50,28 @@ static QByteArray reformatQml(const QString &content)
return content.toUtf8(); return content.toUtf8();
} }
std::optional<QString> modelSerializeHelper(QmlDesigner::ExternalDependenciesInterface &ed, std::optional<QString> modelSerializeHelper(
std::function<void(QmlDesigner::Model *)> callback, QmlDesigner::ProjectStorageDependencies &projectStorageDependencies,
const Utils::FilePath &targetDir, QmlDesigner::ExternalDependenciesInterface &ed,
const QString &typeName, std::function<void(QmlDesigner::Model *)> callback,
bool isSingelton = false) const Utils::FilePath &targetDir,
const QString &typeName,
bool isSingelton = false)
{ {
QString qmlText{"import QtQuick\nQtObject {}\n"}; QString qmlText{"import QtQuick\nQtObject {}\n"};
if (isSingelton) if (isSingelton)
qmlText.prepend("pragma Singleton\n"); 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; QPlainTextEdit editor;
editor.setPlainText(qmlText); editor.setPlainText(qmlText);
QmlDesigner::NotIndentingTextEditModifier modifier(&editor); QmlDesigner::NotIndentingTextEditModifier modifier(&editor);
@@ -87,8 +99,10 @@ std::optional<QString> modelSerializeHelper(QmlDesigner::ExternalDependenciesInt
namespace QmlDesigner { namespace QmlDesigner {
DSStore::DSStore(ExternalDependenciesInterface &ed) DSStore::DSStore(ExternalDependenciesInterface &ed,
ProjectStorageDependencies projectStorageDependencies)
: m_ed(ed) : m_ed(ed)
, m_projectStorageDependencies(projectStorageDependencies)
{} {}
DSStore::~DSStore() {} DSStore::~DSStore() {}
@@ -147,7 +161,7 @@ std::optional<QString> DSStore::load(const Utils::FilePath &dsModuleDirPath)
return {}; return {};
} }
std::optional<QString> DSStore::save(bool mcuCompatible) const std::optional<QString> DSStore::save(bool mcuCompatible)
{ {
if (auto moduleDir = dsModuleDir(m_ed)) if (auto moduleDir = dsModuleDir(m_ed))
return save(*moduleDir, mcuCompatible); return save(*moduleDir, mcuCompatible);
@@ -155,7 +169,7 @@ std::optional<QString> DSStore::save(bool mcuCompatible) const
return tr("Can not locate design system module"); return tr("Can not locate design system module");
} }
std::optional<QString> DSStore::save(const Utils::FilePath &moduleDirPath, bool mcuCompatible) const std::optional<QString> DSStore::save(const Utils::FilePath &moduleDirPath, bool mcuCompatible)
{ {
if (!QDir().mkpath(moduleDirPath.absoluteFilePath().toString())) if (!QDir().mkpath(moduleDirPath.absoluteFilePath().toString()))
return tr("Can not create design system module directory %1.").arg(moduleDirPath.toString()); return tr("Can not create design system module directory %1.").arg(moduleDirPath.toString());
@@ -215,8 +229,15 @@ std::optional<QString> DSStore::loadCollection(const QString &typeName,
if (!reader.fetch(qmlFilePath, QFile::Text)) if (!reader.fetch(qmlFilePath, QFile::Text))
return reader.errorString(); 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; QPlainTextEdit editor;
QString qmlContent = QString::fromUtf8(reader.data()); QString qmlContent = QString::fromUtf8(reader.data());
editor.setPlainText(qmlContent); editor.setPlainText(qmlContent);
@@ -238,7 +259,7 @@ std::optional<QString> DSStore::loadCollection(const QString &typeName,
std::optional<QString> DSStore::writeQml(const DSThemeManager &mgr, std::optional<QString> DSStore::writeQml(const DSThemeManager &mgr,
const QString &typeName, const QString &typeName,
const Utils::FilePath &targetDir, const Utils::FilePath &targetDir,
bool mcuCompatible) const bool mcuCompatible)
{ {
if (mgr.themeCount() == 0) if (mgr.themeCount() == 0)
return {}; return {};
@@ -249,7 +270,8 @@ std::optional<QString> DSStore::writeQml(const DSThemeManager &mgr,
mgr.decorateThemeInterface(interfaceModel->rootModelNode()); 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); return tr("Can not write theme interface %1.\n%2").arg(themeInterfaceType, *error);
} }
@@ -257,7 +279,8 @@ std::optional<QString> DSStore::writeQml(const DSThemeManager &mgr,
mgr.decorate(collectionModel->rootModelNode(), themeInterfaceType.toUtf8(), mcuCompatible); 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 tr("Can not write collection %1.\n%2").arg(typeName, *error);
return {}; return {};

View File

@@ -14,10 +14,10 @@ using DSCollections = std::map<QString, DSThemeManager>;
class DSStore class DSStore
{ {
Q_DECLARE_TR_FUNCTIONS(DSStore); Q_DECLARE_TR_FUNCTIONS(DSStore)
public: public:
DSStore(ExternalDependenciesInterface &ed); DSStore(ExternalDependenciesInterface &ed, ProjectStorageDependencies projectStorageDependencies);
~DSStore(); ~DSStore();
QString moduleImportStr() const; QString moduleImportStr() const;
@@ -25,8 +25,8 @@ public:
std::optional<QString> load(); std::optional<QString> load();
std::optional<QString> load(const Utils::FilePath &dsModuleDirPath); std::optional<QString> load(const Utils::FilePath &dsModuleDirPath);
std::optional<QString> save(bool mcuCompatible = false) const; std::optional<QString> save(bool mcuCompatible = false);
std::optional<QString> save(const Utils::FilePath &moduleDirPath, bool mcuCompatible = false) const; std::optional<QString> save(const Utils::FilePath &moduleDirPath, bool mcuCompatible = false);
size_t collectionCount() const { return m_collections.size(); } size_t collectionCount() const { return m_collections.size(); }
@@ -38,10 +38,11 @@ private:
std::optional<QString> writeQml(const DSThemeManager &mgr, std::optional<QString> writeQml(const DSThemeManager &mgr,
const QString &typeName, const QString &typeName,
const Utils::FilePath &targetDir, const Utils::FilePath &targetDir,
bool mcuCompatible) const; bool mcuCompatible);
private: private:
ExternalDependenciesInterface &m_ed; ExternalDependenciesInterface &m_ed;
ProjectStorageDependencies m_projectStorageDependencies;
DSCollections m_collections; DSCollections m_collections;
std::map<DSThemeManager *, const QString &> m_collectionTypeNames; std::map<DSThemeManager *, const QString &> m_collectionTypeNames;
}; };