Browse Source

WIP settings code gen

pull/2/head
Skycoder42 6 years ago
parent
commit
a79d252f18
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 52
      tests/auto/mvvmcore/settingsgenerator/settings.xml
  2. 6
      tests/auto/mvvmcore/settingsgenerator/settingsgenerator.pro
  3. 29
      tests/auto/mvvmcore/settingsgenerator/testbackend.cpp
  4. 30
      tests/auto/mvvmcore/settingsgenerator/testbackend.h
  5. 2
      tools/settingsgenerator/qpmx.json
  6. 3
      tools/settingsgenerator/qsettingsgenerator.xsd
  7. 58
      tools/settingsgenerator/settingsgenerator.cpp
  8. 6
      tools/settingsgenerator/settingsgenerator.h

52
tests/auto/mvvmcore/settingsgenerator/settings.xml

@ -1,3 +1,53 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Settings name="TestSettings">
<Settings name="TestSettings"
prefix="SOME_EXPORT"
baseKey="tests">
<Include>QtCore/QDateTime</Include>
<Include local="false">QtCore/QUrl</Include>
<Include local="true">testbackend.h</Include>
<Backend class="TestBackend">
<Param type="QString" asStr="true">Test Backend</Param>
<Param type="int">42</Param>
</Backend>
<TypeMapping key="range" type="int"/>
<Node key="emptyNode"/>
<Entry key="emptyEntry"
type="bool"/>
<Entry key="advancedEntry"
type="QString"
qmlGroupKey="qmlAdvancedEntry"
default="Hello World"
tr="true"
trContext="some_context"/>
<Entry key="codeEntry"
type="QUrl">
<Code>QUrl::fromLocalFile(QStringLiteral("/path/to/something"))</Code>
</Entry>
<Node key="parentNode">
<Node key="emptyChildNode"/>
<Node key="fullChildNode">
<Entry key="replaceEntry"
type="range"
default="42"/>
</Node>
<Entry key="parentEntry"
type="bool">
<Node key="subNode"/>
<Entry key="nodeWithCodeEntry"
type="int">
<Node key="someNode"/>
<Code>
qRound(42.5)
</Code>
</Entry>
<Entry key="leafEntry"
type="QString"
default="translate me"
tr="true"/>
</Entry>
</Node>
</Settings>

6
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

29
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()
{
}

30
tests/auto/mvvmcore/settingsgenerator/testbackend.h

@ -0,0 +1,30 @@
#ifndef TESTBACKEND_H
#define TESTBACKEND_H
#include <QtMvvmCore/ISettingsAccessor>
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

2
tools/settingsgenerator/qpmx.json

@ -3,7 +3,7 @@
{
"package": "de.skycoder42.qxmlcodegen",
"provider": "qpm",
"version": "1.1.5"
"version": "1.1.6"
}
],
"license": {

3
tools/settingsgenerator/qsettingsgenerator.xsd

@ -42,7 +42,6 @@
<xs:complexType name="ParamType">
<xs:simpleContent>
<xs:extension base="xs:string" qxg:member="value">
<xs:attribute name="key" type="xs:string" use="required"/>
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute default="false" name="asStr" type="xs:boolean" use="optional"/>
</xs:extension>
@ -80,7 +79,7 @@
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="qmlGroupKey" type="xs:string" use="optional"/>
<xs:attribute name="default" type="xs:string" use="optional" qxg:member="defaultValue"/>
<xs:attribute default="false" name="tr" type="xs:boolean" use="optional"/>
<xs:attribute name="tr" type="xs:boolean" default="false" use="optional"/>
<xs:attribute name="trContext" type="xs:string" use="optional"/>
</xs:extension>
</xs:complexContent>

58
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<QString, QString> &typeMappings, int intendent)
{
for(const auto &cNode : node.contentNodes) {
if(nonstd::holds_alternative<NodeType>(cNode))
writeNode(nonstd::get<NodeType>(cNode), intendent);
writeNode(nonstd::get<NodeType>(cNode), typeMappings, intendent);
else if(nonstd::holds_alternative<EntryType>(cNode))
writeEntry(nonstd::get<EntryType>(cNode), intendent);
writeEntry(nonstd::get<EntryType>(cNode), typeMappings, intendent);
else if(nonstd::holds_alternative<NodeContentGroup>(cNode))
writeNodeElements(nonstd::get<NodeContentGroup>(cNode), intendent);
writeNodeElements(nonstd::get<NodeContentGroup>(cNode), typeMappings, intendent);
}
}
void SettingsGenerator::writeNode(const NodeType &node, int intendent)
void SettingsGenerator::writeNode(const NodeType &node, const QHash<QString, QString> &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<QString, QString> &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 <QtMvvmCore/QSettingsAccessor>\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 &param : 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"

6
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<QString, QString> &typeMappings, int intendent = 1);
void writeNode(const NodeType &node, const QHash<QString, QString> &typeMappings, int intendent = 1);
void writeEntry(const EntryType &entry, const QHash<QString, QString> &typeMappings, int intendent = 1);
void writeSource(const SettingsType &settings);
};

Loading…
Cancel
Save