21#include <QKeySequence>
29#include <toml++/toml.hpp>
34 constexpr std::string_view TYPED_VALUE_TYPE_KEY =
"__type";
35 constexpr std::string_view TYPED_VALUE_VALUE_KEY =
"value";
36 constexpr std::string_view PATH_TYPE_NAME =
"path";
37 constexpr std::string_view SHORTCUT_TYPE_NAME =
"key_sequence";
39 QString keyToQString(
const toml::key& key)
41 const std::string_view keyName = key.str();
42 return QString::fromUtf8(keyName.data(),
static_cast<qsizetype
>(keyName.size()));
45 bool isTypedValueTable(
const toml::table& table)
47 const auto* typeNode = table.get(TYPED_VALUE_TYPE_KEY);
48 const auto* valueNode = table.get(TYPED_VALUE_VALUE_KEY);
50 return table.size() == 2 && typeNode !=
nullptr && valueNode !=
nullptr
51 && typeNode->is_string();
54 std::optional<QVariant> decodeTypedValueTable(
const toml::table& table)
56 const auto* typeNode = table.get_as<std::string>(TYPED_VALUE_TYPE_KEY);
57 const auto* valueNode = table.get_as<std::string>(TYPED_VALUE_VALUE_KEY);
58 if (typeNode ==
nullptr || valueNode ==
nullptr) {
62 if (*typeNode == PATH_TYPE_NAME) {
63 return QVariant::fromValue(std::filesystem::path(valueNode->get()));
66 if (*typeNode == SHORTCUT_TYPE_NAME) {
67 return QVariant::fromValue(QKeySequence(QString::fromStdString(valueNode->get()),
68 QKeySequence::PortableText));
74 std::optional<QVariant> decodeTomlValue(
const toml::node& node)
76 if (
const auto* stringNode = node.as_string()) {
77 return QString::fromStdString(stringNode->get());
80 if (
const auto* boolNode = node.as_boolean()) {
81 return boolNode->get();
84 if (
const auto* intNode = node.as_integer()) {
85 return QVariant::fromValue(
static_cast<int>(intNode->get()));
88 if (
const auto* tableNode = node.as_table()) {
89 if (isTypedValueTable(*tableNode)) {
90 return decodeTypedValueTable(*tableNode);
99 bool populateSettingsMap(
const toml::table& table, QSettings::SettingsMap& map,
100 const QString& prefix = {})
102 for (
const auto& [key, node] : table) {
103 const QString keyName = keyToQString(key);
104 const QString currentKey =
105 prefix.isEmpty() ? keyName : QString(
"%1/%2").arg(prefix, keyName);
107 if (
const auto* childTable = node.as_table();
108 childTable !=
nullptr && !isTypedValueTable(*childTable)) {
109 if (!populateSettingsMap(*childTable, map, currentKey)) {
115 const auto value = decodeTomlValue(node);
116 if (!
value.has_value()) {
120 map.insert(currentKey, *value);
126 toml::table encodeTypedStringValue(std::string_view typeName,
const QString& value)
129 table.insert_or_assign(TYPED_VALUE_TYPE_KEY, std::string(typeName));
130 table.insert_or_assign(TYPED_VALUE_VALUE_KEY,
value.toStdString());
134 bool insertVariantNode(toml::table& table,
const QString& key,
const QVariant& value)
136 const std::string tomlKey = key.toStdString();
137 const int metaTypeId =
value.metaType().id();
139 if (metaTypeId == QMetaType::QString) {
140 table.insert_or_assign(tomlKey,
value.toString().toStdString());
144 if (metaTypeId == QMetaType::Bool) {
145 table.insert_or_assign(tomlKey,
value.toBool());
149 if (metaTypeId == QMetaType::Int) {
150 table.insert_or_assign(tomlKey,
value.toInt());
156 if (metaTypeId == qMetaTypeId<std::filesystem::path>()) {
157 const auto typedValue = encodeTypedStringValue(
159 QString::fromStdString(
value.value<std::filesystem::path>().generic_string()));
160 table.insert_or_assign(tomlKey, typedValue);
164 if (metaTypeId == qMetaTypeId<QKeySequence>()) {
165 const auto typedValue = encodeTypedStringValue(
167 value.value<QKeySequence>().toString(QKeySequence::PortableText));
168 table.insert_or_assign(tomlKey, typedValue);
175 bool writeSettingsEntry(toml::table& root,
const QString& key,
const QVariant& value)
177 const QStringList path = key.split(
'/', Qt::SkipEmptyParts);
178 if (path.isEmpty()) {
182 toml::table* currentTable = &root;
183 for (qsizetype i = 0; i < path.size() - 1; ++i) {
184 const std::string section = path[i].toStdString();
185 auto* childTable = currentTable->get_as<toml::table>(section);
186 if (childTable ==
nullptr) {
187 auto [it, inserted] = currentTable->insert(section, toml::table{});
192 childTable = it->second.as_table();
195 if (childTable ==
nullptr) {
199 currentTable = childTable;
202 return insertVariantNode(*currentTable, path.back(), value);
205 bool readTomlFile(QIODevice& device, QSettings::SettingsMap& map)
208 const QByteArray contents = device.readAll();
209 if (contents.trimmed().isEmpty()) {
213 const toml::table table = toml::parse(contents.toStdString());
214 return populateSettingsMap(table, map);
215 }
catch (
const toml::parse_error&) {
220 bool writeTomlFile(QIODevice& device,
const QSettings::SettingsMap& map)
223 for (
auto it = map.cbegin(); it != map.cend(); ++it) {
224 if (!writeSettingsEntry(table, it.key(), it.value())) {
229 std::ostringstream stream;
231 const std::string serialized = stream.str();
232 return device.write(serialized.c_str(),
static_cast<qint64
>(serialized.size()))
233 ==
static_cast<qint64
>(serialized.size());
236 const QSettings::Format tomlFormat =
237 QSettings::registerFormat(
"toml", readTomlFile, writeTomlFile);
251 : QSettings(tomlFormat, QSettings::UserScope,
"SILICON", appName, parent)
268 .maxTransitionsPerDeltaCycle =
SiliconSettings(const QString &appName, QObject *parent=nullptr)
Opens the user-scoped settings store for a SILICON application.
static QSettings::Format format()
Returns the registered TOML-backed settings format.
QVariant value(const QSettings &settings, const Definition &setting)
Reads a setting value, falling back to its declared default when unset.
constexpr auto DARK_THEME
const Definition MaxSimulationSteps
const Definition MaxTransitionsPerDeltaCycle
SILICON::ui::theme::Mode themeModeFromText(const QString &value)
Converts a persisted theme string into a theme engine mode.
CommonSettingsValues readCommonSettings(const QSettings &settings)
Reads the common UI and simulation settings into a typed snapshot.
Snapshot of the common application settings used at runtime.
Describes a persisted setting entry.
QVariant getResolvedDefault() const