SILICON
Loading...
Searching...
No Matches
siliconSettings.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "siliconSettings.hpp"
19
20#include <QIODevice>
21#include <QKeySequence>
22#include <QMetaType>
23
24#include <filesystem>
25#include <optional>
26#include <sstream>
27#include <string_view>
28
29#include <toml++/toml.hpp>
30
31namespace SILICON::ui {
32
33namespace {
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";
38
39 QString keyToQString(const toml::key& key)
40 {
41 const std::string_view keyName = key.str();
42 return QString::fromUtf8(keyName.data(), static_cast<qsizetype>(keyName.size()));
43 }
44
45 bool isTypedValueTable(const toml::table& table)
46 {
47 const auto* typeNode = table.get(TYPED_VALUE_TYPE_KEY);
48 const auto* valueNode = table.get(TYPED_VALUE_VALUE_KEY);
49
50 return table.size() == 2 && typeNode != nullptr && valueNode != nullptr
51 && typeNode->is_string();
52 }
53
54 std::optional<QVariant> decodeTypedValueTable(const toml::table& table)
55 {
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) {
59 return std::nullopt;
60 }
61
62 if (*typeNode == PATH_TYPE_NAME) {
63 return QVariant::fromValue(std::filesystem::path(valueNode->get()));
64 }
65
66 if (*typeNode == SHORTCUT_TYPE_NAME) {
67 return QVariant::fromValue(QKeySequence(QString::fromStdString(valueNode->get()),
68 QKeySequence::PortableText));
69 }
70
71 return std::nullopt;
72 }
73
74 std::optional<QVariant> decodeTomlValue(const toml::node& node)
75 {
76 if (const auto* stringNode = node.as_string()) {
77 return QString::fromStdString(stringNode->get());
78 }
79
80 if (const auto* boolNode = node.as_boolean()) {
81 return boolNode->get();
82 }
83
84 if (const auto* intNode = node.as_integer()) {
85 return QVariant::fromValue(static_cast<int>(intNode->get()));
86 }
87
88 if (const auto* tableNode = node.as_table()) {
89 if (isTypedValueTable(*tableNode)) {
90 return decodeTypedValueTable(*tableNode);
91 }
92 }
93
94 return std::nullopt;
95 }
96
97 // QSettings stores groups as slash-delimited flat keys, while TOML stores them as
98 // nested tables. Reading expands TOML back into the flat key space QSettings expects.
99 bool populateSettingsMap(const toml::table& table, QSettings::SettingsMap& map,
100 const QString& prefix = {})
101 {
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);
106
107 if (const auto* childTable = node.as_table();
108 childTable != nullptr && !isTypedValueTable(*childTable)) {
109 if (!populateSettingsMap(*childTable, map, currentKey)) {
110 return false;
111 }
112 continue;
113 }
114
115 const auto value = decodeTomlValue(node);
116 if (!value.has_value()) {
117 return false;
118 }
119
120 map.insert(currentKey, *value);
121 }
122
123 return true;
124 }
125
126 toml::table encodeTypedStringValue(std::string_view typeName, const QString& value)
127 {
128 toml::table table;
129 table.insert_or_assign(TYPED_VALUE_TYPE_KEY, std::string(typeName));
130 table.insert_or_assign(TYPED_VALUE_VALUE_KEY, value.toStdString());
131 return table;
132 }
133
134 bool insertVariantNode(toml::table& table, const QString& key, const QVariant& value)
135 {
136 const std::string tomlKey = key.toStdString();
137 const int metaTypeId = value.metaType().id();
138
139 if (metaTypeId == QMetaType::QString) {
140 table.insert_or_assign(tomlKey, value.toString().toStdString());
141 return true;
142 }
143
144 if (metaTypeId == QMetaType::Bool) {
145 table.insert_or_assign(tomlKey, value.toBool());
146 return true;
147 }
148
149 if (metaTypeId == QMetaType::Int) {
150 table.insert_or_assign(tomlKey, value.toInt());
151 return true;
152 }
153
154 // TOML has no native path or shortcut type, so these are stored as tagged tables
155 // to preserve their QVariant type on readback.
156 if (metaTypeId == qMetaTypeId<std::filesystem::path>()) {
157 const auto typedValue = encodeTypedStringValue(
158 PATH_TYPE_NAME,
159 QString::fromStdString(value.value<std::filesystem::path>().generic_string()));
160 table.insert_or_assign(tomlKey, typedValue);
161 return true;
162 }
163
164 if (metaTypeId == qMetaTypeId<QKeySequence>()) {
165 const auto typedValue = encodeTypedStringValue(
166 SHORTCUT_TYPE_NAME,
167 value.value<QKeySequence>().toString(QKeySequence::PortableText));
168 table.insert_or_assign(tomlKey, typedValue);
169 return true;
170 }
171
172 return false;
173 }
174
175 bool writeSettingsEntry(toml::table& root, const QString& key, const QVariant& value)
176 {
177 const QStringList path = key.split('/', Qt::SkipEmptyParts);
178 if (path.isEmpty()) {
179 return false;
180 }
181
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{});
188 if (!inserted) {
189 return false;
190 }
191
192 childTable = it->second.as_table();
193 }
194
195 if (childTable == nullptr) {
196 return false;
197 }
198
199 currentTable = childTable;
200 }
201
202 return insertVariantNode(*currentTable, path.back(), value);
203 }
204
205 bool readTomlFile(QIODevice& device, QSettings::SettingsMap& map)
206 {
207 try {
208 const QByteArray contents = device.readAll();
209 if (contents.trimmed().isEmpty()) {
210 return true;
211 }
212
213 const toml::table table = toml::parse(contents.toStdString());
214 return populateSettingsMap(table, map);
215 } catch (const toml::parse_error&) {
216 return false;
217 }
218 }
219
220 bool writeTomlFile(QIODevice& device, const QSettings::SettingsMap& map)
221 {
222 toml::table table;
223 for (auto it = map.cbegin(); it != map.cend(); ++it) {
224 if (!writeSettingsEntry(table, it.key(), it.value())) {
225 return false;
226 }
227 }
228
229 std::ostringstream stream;
230 stream << table;
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());
234 }
235
236 const QSettings::Format tomlFormat =
237 QSettings::registerFormat("toml", readTomlFile, writeTomlFile);
238
239} // namespace
240
241namespace settings {
242
243 QVariant value(const QSettings& settings, const Definition& setting)
244 {
245 return settings.value(setting.name, setting.getResolvedDefault());
246 }
247
248} // namespace settings
249
250SiliconSettings::SiliconSettings(const QString& appName, QObject* parent)
251 : QSettings(tomlFormat, QSettings::UserScope, "SILICON", appName, parent)
252{
253}
254
255QSettings::Format SiliconSettings::format()
256{
257 return tomlFormat;
258}
259
261{
262 return {
263 .theme =
265 .maxSimulationSteps = SILICON::ui::settings::value(
267 .toInt(),
268 .maxTransitionsPerDeltaCycle =
271 .toInt(),
272 };
273}
274
280
281} // namespace SILICON::ui
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.
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.