From a79d252f185c43e9e696056936d430d6c6ef5f49 Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Mon, 16 Jul 2018 13:09:03 +0200 Subject: [PATCH] WIP settings code gen --- .../mvvmcore/settingsgenerator/settings.xml | 52 ++++++++++++++++- .../settingsgenerator/settingsgenerator.pro | 6 +- .../settingsgenerator/testbackend.cpp | 29 ++++++++++ .../mvvmcore/settingsgenerator/testbackend.h | 30 ++++++++++ tools/settingsgenerator/qpmx.json | 2 +- .../settingsgenerator/qsettingsgenerator.xsd | 3 +- tools/settingsgenerator/settingsgenerator.cpp | 58 ++++++++++++++----- tools/settingsgenerator/settingsgenerator.h | 6 +- 8 files changed, 163 insertions(+), 23 deletions(-) create mode 100644 tests/auto/mvvmcore/settingsgenerator/testbackend.cpp create mode 100644 tests/auto/mvvmcore/settingsgenerator/testbackend.h diff --git a/tests/auto/mvvmcore/settingsgenerator/settings.xml b/tests/auto/mvvmcore/settingsgenerator/settings.xml index d5c7acd..04414e7 100644 --- a/tests/auto/mvvmcore/settingsgenerator/settings.xml +++ b/tests/auto/mvvmcore/settingsgenerator/settings.xml @@ -1,3 +1,53 @@ - + + QtCore/QDateTime + QtCore/QUrl + testbackend.h + + + Test Backend + 42 + + + + + + + + + QUrl::fromLocalFile(QStringLiteral("/path/to/something")) + + + + + + + + + + + + + qRound(42.5) + + + + + diff --git a/tests/auto/mvvmcore/settingsgenerator/settingsgenerator.pro b/tests/auto/mvvmcore/settingsgenerator/settingsgenerator.pro index cb51453..6862b82 100644 --- a/tests/auto/mvvmcore/settingsgenerator/settingsgenerator.pro +++ b/tests/auto/mvvmcore/settingsgenerator/settingsgenerator.pro @@ -8,7 +8,11 @@ CONFIG -= app_bundle TARGET = tst_settingsgenerator SOURCES += \ - tst_settingsgenerator.cpp + tst_settingsgenerator.cpp \ + testbackend.cpp SETTINGS_DEFINITIONS += \ settings.xml + +HEADERS += \ + testbackend.h diff --git a/tests/auto/mvvmcore/settingsgenerator/testbackend.cpp b/tests/auto/mvvmcore/settingsgenerator/testbackend.cpp new file mode 100644 index 0000000..6404b82 --- /dev/null +++ b/tests/auto/mvvmcore/settingsgenerator/testbackend.cpp @@ -0,0 +1,29 @@ +#include "testbackend.h" + +TestBackend::TestBackend(QString name, int code, QObject *parent) : + ISettingsAccessor{parent}, + _name{std::move(name)}, + _code{code} +{} + +bool TestBackend::contains(const QString &key) const +{ + return false; +} + +QVariant TestBackend::load(const QString &key, const QVariant &defaultValue) const +{ + return {}; +} + +void TestBackend::save(const QString &key, const QVariant &value) +{ +} + +void TestBackend::remove(const QString &key) +{ +} + +void TestBackend::sync() +{ +} diff --git a/tests/auto/mvvmcore/settingsgenerator/testbackend.h b/tests/auto/mvvmcore/settingsgenerator/testbackend.h new file mode 100644 index 0000000..f42bfa3 --- /dev/null +++ b/tests/auto/mvvmcore/settingsgenerator/testbackend.h @@ -0,0 +1,30 @@ +#ifndef TESTBACKEND_H +#define TESTBACKEND_H + +#include + +class TestBackend : public QtMvvm::ISettingsAccessor +{ + Q_OBJECT + +public: + explicit TestBackend(QString name, int code, QObject *parent = nullptr); + + // ISettingsAccessor interface +public: + bool contains(const QString &key) const override; + QVariant load(const QString &key, const QVariant &defaultValue) const override; + void save(const QString &key, const QVariant &value) override; + void remove(const QString &key) override; + +public slots: + void sync() override; + +public: + QString _name; + int _code; +}; + +#define SOME_EXPORT + +#endif // TESTBACKEND_H diff --git a/tools/settingsgenerator/qpmx.json b/tools/settingsgenerator/qpmx.json index 78c041a..a47c6de 100644 --- a/tools/settingsgenerator/qpmx.json +++ b/tools/settingsgenerator/qpmx.json @@ -3,7 +3,7 @@ { "package": "de.skycoder42.qxmlcodegen", "provider": "qpm", - "version": "1.1.5" + "version": "1.1.6" } ], "license": { diff --git a/tools/settingsgenerator/qsettingsgenerator.xsd b/tools/settingsgenerator/qsettingsgenerator.xsd index 7ae6038..0cf9c48 100644 --- a/tools/settingsgenerator/qsettingsgenerator.xsd +++ b/tools/settingsgenerator/qsettingsgenerator.xsd @@ -42,7 +42,6 @@ - @@ -80,7 +79,7 @@ - + diff --git a/tools/settingsgenerator/settingsgenerator.cpp b/tools/settingsgenerator/settingsgenerator.cpp index 54af0a6..afb5bdf 100644 --- a/tools/settingsgenerator/settingsgenerator.cpp +++ b/tools/settingsgenerator/settingsgenerator.cpp @@ -257,7 +257,7 @@ void SettingsGenerator::writeHeader(const SettingsType &settings) << "\tstatic " << settings.name.value() << " *instance();\n\n" << "\tQtMvvm::ISettingsAccessor *accessor() const;\n\n"; - writeNodeElements(settings); + writeNodeElements(settings, settings.typeMappings); _hdr << "\nprivate:\n" << "\tQtMvvm::ISettingsAccessor *_accessor;\n" @@ -265,39 +265,67 @@ void SettingsGenerator::writeHeader(const SettingsType &settings) << "#endif //" << incGuard << '\n'; } -void SettingsGenerator::writeNodeElements(const NodeContentGroup &node, int intendent) +void SettingsGenerator::writeNodeElements(const NodeContentGroup &node, const QHash &typeMappings, int intendent) { for(const auto &cNode : node.contentNodes) { if(nonstd::holds_alternative(cNode)) - writeNode(nonstd::get(cNode), intendent); + writeNode(nonstd::get(cNode), typeMappings, intendent); else if(nonstd::holds_alternative(cNode)) - writeEntry(nonstd::get(cNode), intendent); + writeEntry(nonstd::get(cNode), typeMappings, intendent); else if(nonstd::holds_alternative(cNode)) - writeNodeElements(nonstd::get(cNode), intendent); + writeNodeElements(nonstd::get(cNode), typeMappings, intendent); } } -void SettingsGenerator::writeNode(const NodeType &node, int intendent) +void SettingsGenerator::writeNode(const NodeType &node, const QHash &typeMappings, int intendent) { - _hdr << TABS << "struct {\n"; - writeNodeElements(node, intendent + 1); + _hdr << TABS << "struct { //" << node.key << "\n"; + writeNodeElements(node, typeMappings, intendent + 1); _hdr << TABS << "} " << node.key << ";\n"; } -void SettingsGenerator::writeEntry(const EntryType &entry, int intendent) +void SettingsGenerator::writeEntry(const EntryType &entry, const QHash &typeMappings, int intendent) { - _hdr << TABS << "struct : QtMvvm::SettingsEntry<" << entry.type << "> {\n"; - writeNodeElements(entry, intendent + 1); - _hdr << TABS << "} " << entry.key << ";\n"; + if(entry.contentNodes.isEmpty()) + _hdr << TABS << "QtMvvm::SettingsEntry<" << typeMappings.value(entry.type, entry.type) << "> " << entry.key << ";\n"; + else { + _hdr << TABS << "struct : QtMvvm::SettingsEntry<" << typeMappings.value(entry.type, entry.type) << "> { //" << entry.key << "\n"; + writeNodeElements(entry, typeMappings, intendent + 1); + _hdr << TABS << "} " << entry.key << ";\n"; + } } void SettingsGenerator::writeSource(const SettingsType &settings) { - _src << "#include \"" << _hdrFile.fileName() << "\"\n\n"; + _src << "#include \"" << _hdrFile.fileName() << "\"\n"; + if(!settings.backend) + _src << "#include \n"; + _src << "\n"; + auto backend = settings.backend.value_or(BackendType{QStringLiteral("QtMvvm::QSettingsAccessor"), {}}); _src << settings.name.value() << "::" << settings.name.value() << "(QObject *parent) : \n" - << "\t" << settings.name.value() << "{nullptr, parent}\n" - << "{}\n\n"; + << "\t" << settings.name.value() << "{new " << backend.className << "{"; + if(!backend.param.isEmpty()) { + _src << "\n"; + auto first = true; + for(const auto ¶m : qAsConst(backend.param)) { + if(first) + first = false; + else + _src << ",\n"; + _src << "\t\tQVariant{"; + if(param.asStr) + _src << "QStringLiteral(\"" << param.value << "\")"; + else + _src << param.value; + _src << "}.value<" << param.type << ">()"; + } + _src << "\n\t"; + } + _src << "}, parent}\n" + << "{\n" + << "\t_accessor->setParent(this);\n" + << "}\n\n"; _src << settings.name.value() << "::" << settings.name.value() << "(QtMvvm::ISettingsAccessor *accessor, QObject *parent) : \n" << "\tQObject{parent},\n" diff --git a/tools/settingsgenerator/settingsgenerator.h b/tools/settingsgenerator/settingsgenerator.h index 6a83354..cae6fcc 100644 --- a/tools/settingsgenerator/settingsgenerator.h +++ b/tools/settingsgenerator/settingsgenerator.h @@ -36,9 +36,9 @@ private: NodeContentGroup *replaceNodeByEntry(NodeContentGroup *cGrp, NodeContentGroup *node, EntryType &&entry); void writeHeader(const SettingsType &settings); - void writeNodeElements(const NodeContentGroup &node, int intendent = 1); - void writeNode(const NodeType &node, int intendent = 1); - void writeEntry(const EntryType &entry, int intendent = 1); + void writeNodeElements(const NodeContentGroup &node, const QHash &typeMappings, int intendent = 1); + void writeNode(const NodeType &node, const QHash &typeMappings, int intendent = 1); + void writeEntry(const EntryType &entry, const QHash &typeMappings, int intendent = 1); void writeSource(const SettingsType &settings); };