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}
DEPENDS
Qt::Core Qt::Widgets QmlDesignerCore TextEditorSupport
PUBLIC_DEFINES
$<$<BOOL:${USE_PROJECTSTORAGE}>:QDS_USE_PROJECTSTORAGE>
SOURCES
dsconstants.h
dsstore.h dsstore.cpp

View File

@@ -5,6 +5,7 @@
#include "dsthememanager.h"
#include <generatedcomponentutils.h>
#include <import.h>
#include <plaintexteditmodifier.h>
#include <qmljs/parser/qmldirparser_p.h>
#include <qmljs/qmljsreformatter.h>
@@ -49,17 +50,28 @@ static QByteArray reformatQml(const QString &content)
return content.toUtf8();
}
std::optional<QString> modelSerializeHelper(QmlDesigner::ExternalDependenciesInterface &ed,
std::function<void(QmlDesigner::Model *)> callback,
const Utils::FilePath &targetDir,
const QString &typeName,
bool isSingelton = false)
std::optional<QString> modelSerializeHelper(
QmlDesigner::ProjectStorageDependencies &projectStorageDependencies,
QmlDesigner::ExternalDependenciesInterface &ed,
std::function<void(QmlDesigner::Model *)> 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<QString> 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<QString> DSStore::load(const Utils::FilePath &dsModuleDirPath)
return {};
}
std::optional<QString> DSStore::save(bool mcuCompatible) const
std::optional<QString> DSStore::save(bool mcuCompatible)
{
if (auto moduleDir = dsModuleDir(m_ed))
return save(*moduleDir, mcuCompatible);
@@ -155,7 +169,7 @@ std::optional<QString> DSStore::save(bool mcuCompatible) const
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()))
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))
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<QString> DSStore::loadCollection(const QString &typeName,
std::optional<QString> 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<QString> 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<QString> 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 {};

View File

@@ -14,10 +14,10 @@ using DSCollections = std::map<QString, DSThemeManager>;
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<QString> load();
std::optional<QString> load(const Utils::FilePath &dsModuleDirPath);
std::optional<QString> save(bool mcuCompatible = false) const;
std::optional<QString> save(const Utils::FilePath &moduleDirPath, bool mcuCompatible = false) const;
std::optional<QString> save(bool mcuCompatible = false);
std::optional<QString> save(const Utils::FilePath &moduleDirPath, bool mcuCompatible = false);
size_t collectionCount() const { return m_collections.size(); }
@@ -38,10 +38,11 @@ private:
std::optional<QString> 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<DSThemeManager *, const QString &> m_collectionTypeNames;
};