SILICON
Loading...
Searching...
No Matches
settingsWindow.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 "settingsWindow.hpp"
19
20#include <limits>
21#include <utility>
22
23#include <QAction>
24#include <QApplication>
25#include <QComboBox>
26#include <QDialogButtonBox>
27#include <QFile>
28#include <QFormLayout>
29#include <QGroupBox>
30#include <QHBoxLayout>
31#include <QKeySequenceEdit>
32#include <QLabel>
33#include <QPushButton>
34#include <QScrollArea>
35#include <QSettings>
36#include <QSpinBox>
37#include <QTemporaryFile>
38#include <QVBoxLayout>
39
40#include <core/simulator.hpp>
43#include <ui/shell/theme.hpp>
44
45
46namespace SILICON::ui {
47
48namespace {
49void copySettings(QSettings& source, QSettings& target)
50{
51 for (const QString& key : source.allKeys())
52 target.setValue(key, source.value(key));
53}
54
55void applySettingHelp(QWidget* widget, const SILICON::ui::settings::Definition& setting)
56{
57 if (!setting.help.has_value())
58 return;
59
60 widget->setToolTip(*setting.help);
61 widget->setStatusTip(*setting.help);
62}
63
64void addSettingRow(QFormLayout* layout, const QString& label, QWidget* editor,
65 const SILICON::ui::settings::Definition& setting)
66{
67 const auto labelWidget = new QLabel(label, layout->parentWidget());
68 labelWidget->setBuddy(editor);
69
70 applySettingHelp(labelWidget, setting);
71 applySettingHelp(editor, setting);
72
73 layout->addRow(labelWidget, editor);
74}
75} // namespace
76
77SettingsWindow::SettingsWindow(const QString& appName, QVector<ShortcutSetting> shortcuts,
78 QWidget* parent)
79 : QDialog(parent), settings(appName, this), shortcuts(std::move(shortcuts))
80{
81 setWindowTitle(tr("Settings"));
82 resize(560, 520);
83
84 themeCombo = new QComboBox(this);
87
88 maxSimulationStepsSpinBox = new QSpinBox(this);
89 maxSimulationStepsSpinBox->setRange(1, std::numeric_limits<int>::max());
90
91 maxTransitionsPerDeltaCycleSpinBox = new QSpinBox(this);
92 maxTransitionsPerDeltaCycleSpinBox->setRange(1, std::numeric_limits<int>::max());
93
94 const auto generalGroup = new QGroupBox(tr("General"), this);
95 const auto generalLayout = new QFormLayout(generalGroup);
96 addSettingRow(generalLayout, tr("Theme"), themeCombo, SILICON::ui::settings::Theme);
97 addSettingRow(generalLayout, tr("Max simulation steps"), maxSimulationStepsSpinBox,
99 addSettingRow(generalLayout, tr("Max transitions per delta cycle"),
102
103 keybindingsLayout = new QFormLayout();
104 for (const ShortcutSetting& shortcut : this->shortcuts)
105 addShortcutEditor(shortcut);
106
107 const auto keybindingsContents = new QWidget(this);
108 keybindingsContents->setLayout(keybindingsLayout);
109
110 const auto keybindingsScrollArea = new QScrollArea(this);
111 keybindingsScrollArea->setWidgetResizable(true);
112 keybindingsScrollArea->setFrameShape(QFrame::NoFrame);
113 keybindingsScrollArea->setWidget(keybindingsContents);
114
115 const auto keybindingsGroup = new QGroupBox(tr("Keybindings"), this);
116 const auto keybindingsBoxLay = new QVBoxLayout(keybindingsGroup);
117 keybindingsBoxLay->addWidget(keybindingsScrollArea);
118
119 buttonBox =
120 new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel, this);
122 buttonBox->addButton(tr("Restore Defaults"), QDialogButtonBox::ResetRole);
123
124 importButton = new QPushButton(tr("Import..."), this);
125 exportButton = new QPushButton(tr("Export..."), this);
126
127 const auto transferLayout = new QHBoxLayout();
128 transferLayout->addWidget(importButton);
129 transferLayout->addWidget(exportButton);
130 transferLayout->addStretch();
131
132 const auto mainLayout = new QVBoxLayout(this);
133 mainLayout->addWidget(generalGroup);
134 mainLayout->addWidget(keybindingsGroup, 1);
135 mainLayout->addLayout(transferLayout);
136 mainLayout->addWidget(buttonBox);
137
138 callbacks.connect(importButton, &QPushButton::clicked, [this] { importSettings(); });
139 callbacks.connect(exportButton, &QPushButton::clicked, [this] { exportSettings(); });
140 callbacks.connect(restoreDefaultsButton, &QPushButton::clicked,
141 [this] { restoreDefaults(); });
142 callbacks.connect(buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked,
143 [this] { reject(); });
144 callbacks.connect(buttonBox->button(QDialogButtonBox::Save), &QPushButton::clicked, [this] {
145 if (saveSettings())
146 accept();
147 });
148
149 loadSettings();
150}
151
153{
154 settings.sync();
155
157 const int themeIndex = themeCombo->findData(values.theme);
158 themeCombo->setCurrentIndex(
159 themeIndex >= 0 ? themeIndex
160 : themeCombo->findData(SILICON::ui::settings::Theme.defaultValue));
161
164
165 for (int i = 0; i < shortcuts.size(); ++i) {
166 shortcutEditors[i]->setKeySequence(
167 SILICON::ui::settings::value(settings, shortcuts[i].setting).value<QKeySequence>());
168 }
169}
170
172{
173 const CommonSettingsValues defaults;
174 const int themeIndex = themeCombo->findData(defaults.theme);
175 themeCombo->setCurrentIndex(
176 themeIndex >= 0 ? themeIndex
177 : themeCombo->findData(SILICON::ui::settings::Theme.defaultValue));
180
181 for (int i = 0; i < shortcuts.size(); ++i)
182 shortcutEditors[i]->setKeySequence(
183 shortcuts[i].setting.defaultValue.value<QKeySequence>());
184}
185
187{
189 settings.sync();
190 if (settings.status() != QSettings::NoError) {
191 inputDialog::warning(this, tr("Settings Error"),
192 tr("Could not write the settings file."));
193 return false;
194 }
195
197 return true;
198}
199
200bool SettingsWindow::importSettingsStore(QSettings& importedSettings)
201{
202 importedSettings.sync();
203 if (importedSettings.status() != QSettings::NoError) {
204 inputDialog::warning(this, tr("Import Settings"),
205 tr("Could not read the selected settings file."));
206 return false;
207 }
208
209 settings.clear();
210 copySettings(importedSettings, settings);
211 settings.sync();
212 if (settings.status() != QSettings::NoError) {
213 inputDialog::warning(this, tr("Import Settings"),
214 tr("Could not save the imported settings."));
215 return false;
216 }
217
218 loadSettings();
220 return true;
221}
222
224{
225 QTemporaryFile exportedFile(this);
226 if (!exportedFile.open()) {
227 inputDialog::warning(this, tr("Export Settings"),
228 tr("Could not write the settings file."));
229 return {};
230 }
231
232 const QString exportedFileName = exportedFile.fileName();
233 exportedFile.close();
234
235 QSettings exportedSettings(exportedFileName, SiliconSettings::format());
236 exportedSettings.clear();
237 copySettings(settings, exportedSettings);
238 writeVisibleSettings(exportedSettings);
239 exportedSettings.sync();
240
241 if (exportedSettings.status() != QSettings::NoError) {
242 inputDialog::warning(this, tr("Export Settings"),
243 tr("Could not write the settings file."));
244 return {};
245 }
246
247 QFile file(exportedFileName);
248 if (!file.open(QIODevice::ReadOnly)) {
249 inputDialog::warning(this, tr("Export Settings"),
250 tr("Could not read back the exported settings file."));
251 return {};
252 }
253 return file.readAll();
254}
255
257{
259 this, tr("Import Settings"), tr("TOML Settings (*.toml);;All Files (*)"),
260 [this](const QString& fileName, const QByteArray& fileContent) {
261 QTemporaryFile importedFile(this);
262 if (!importedFile.open()
263 || importedFile.write(fileContent) != fileContent.size()) {
264 inputDialog::warning(this, tr("Import Settings"),
265 tr("Could not read the selected settings file."));
266 return;
267 }
268 importedFile.flush();
269 const QString importedFileName = importedFile.fileName();
270 importedFile.close();
271
272 QSettings importedSettings(importedFileName, SiliconSettings::format());
273 importSettingsStore(importedSettings);
274 },
275 callbacks.context());
276}
277
279{
280 const QByteArray content = exportSettingsContent();
281 if (!content.isEmpty())
283 this, tr("Export Settings"), QStringLiteral("silicon-settings.toml"),
284 tr("TOML Settings (*.toml);;All Files (*)"), content);
285}
286
288{
289 const auto editor = new QKeySequenceEdit(this);
290 shortcutEditors.append(editor);
291 keybindingsLayout->addRow(shortcut.label, editor);
292}
293
295{
297 static_cast<uint64_t>(maxSimulationStepsSpinBox->value()));
300
301 ThemeEngine::apply(*qApp, themeModeFromText(themeCombo->currentData().toString()));
302
303 for (int i = 0; i < shortcuts.size(); ++i) {
304 if (shortcuts[i].action)
305 shortcuts[i].action->setShortcut(shortcutEditors[i]->keySequence());
306 }
307}
308
309void SettingsWindow::writeVisibleSettings(QSettings& target) const
310{
311 target.setValue(SILICON::ui::settings::Theme.name, themeCombo->currentData().toString());
316
317 for (int i = 0; i < shortcuts.size(); ++i)
318 target.setValue(shortcuts[i].setting.name,
319 QVariant::fromValue(shortcutEditors[i]->keySequence()));
320}
321
322} // namespace SILICON::ui
static void setMaxSimulationSteps(uint64_t value)
static void setMaxTransitionsPerDeltaCycle(int value)
QVector< ShortcutSetting > shortcuts
void loadSettings()
Loads persisted settings into the visible editors.
bool saveSettings()
Writes the current editor state to disk and applies it to the app.
void restoreDefaults()
Resets the visible editors to their declared default values.
void importSettings()
Imports settings from an external TOML file into the active store.
QSpinBox * maxTransitionsPerDeltaCycleSpinBox
void writeVisibleSettings(QSettings &target) const
Writes the currently visible values to a target settings store.
void addShortcutEditor(const ShortcutSetting &shortcut)
Adds one key sequence editor row for a shortcut setting.
QByteArray exportSettingsContent()
Serializes the visible settings to TOML bytes.
QVector< QKeySequenceEdit * > shortcutEditors
bool importSettingsStore(QSettings &importedSettings)
Applies settings from another settings store.
SettingsWindow(const QString &appName, QVector< ShortcutSetting > shortcuts, QWidget *parent=nullptr)
Builds the settings dialog for one application namespace.
void exportSettings()
Exports the current settings to an external TOML file.
void applySettings()
Applies the visible settings to the running application state.
static QSettings::Format format()
Returns the registered TOML-backed settings format.
static void apply(QApplication &app, theme::Mode mode)
Definition theme.cpp:169
std::optional< QString > saveFileContent(QWidget *parent, const QString &caption, const QString &suggestedFileName, const QString &filter, const QByteArray &content)
void openFileContent(QWidget *parent, const QString &caption, const QString &filter, OpenContentCallback callback, QObject *context)
void warning(QWidget *parent, const QString &title, const QString &text)
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.
Shortcut row shown in the settings dialog.