24#include <QApplication>
26#include <QDialogButtonBox>
31#include <QKeySequenceEdit>
37#include <QTemporaryFile>
49void copySettings(QSettings& source, QSettings& target)
51 for (
const QString& key :
source.allKeys())
52 target.setValue(key,
source.value(key));
55void applySettingHelp(QWidget* widget,
const SILICON::ui::settings::Definition& setting)
57 if (!setting.
help.has_value())
60 widget->setToolTip(*setting.
help);
61 widget->setStatusTip(*setting.
help);
64void addSettingRow(QFormLayout* layout,
const QString& label, QWidget* editor,
65 const SILICON::ui::settings::Definition& setting)
67 const auto labelWidget =
new QLabel(label, layout->parentWidget());
68 labelWidget->setBuddy(editor);
70 applySettingHelp(labelWidget, setting);
71 applySettingHelp(editor, setting);
73 layout->addRow(labelWidget, editor);
81 setWindowTitle(tr(
"Settings"));
94 const auto generalGroup =
new QGroupBox(tr(
"General"),
this);
95 const auto generalLayout =
new QFormLayout(generalGroup);
99 addSettingRow(generalLayout, tr(
"Max transitions per delta cycle"),
107 const auto keybindingsContents =
new QWidget(
this);
110 const auto keybindingsScrollArea =
new QScrollArea(
this);
111 keybindingsScrollArea->setWidgetResizable(
true);
112 keybindingsScrollArea->setFrameShape(QFrame::NoFrame);
113 keybindingsScrollArea->setWidget(keybindingsContents);
115 const auto keybindingsGroup =
new QGroupBox(tr(
"Keybindings"),
this);
116 const auto keybindingsBoxLay =
new QVBoxLayout(keybindingsGroup);
117 keybindingsBoxLay->addWidget(keybindingsScrollArea);
120 new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel,
this);
122 buttonBox->addButton(tr(
"Restore Defaults"), QDialogButtonBox::ResetRole);
127 const auto transferLayout =
new QHBoxLayout();
130 transferLayout->addStretch();
132 const auto mainLayout =
new QVBoxLayout(
this);
133 mainLayout->addWidget(generalGroup);
134 mainLayout->addWidget(keybindingsGroup, 1);
135 mainLayout->addLayout(transferLayout);
143 [
this] { reject(); });
144 callbacks.connect(
buttonBox->button(QDialogButtonBox::Save), &QPushButton::clicked, [
this] {
159 themeIndex >= 0 ? themeIndex
165 for (
int i = 0; i <
shortcuts.size(); ++i) {
176 themeIndex >= 0 ? themeIndex
181 for (
int i = 0; i <
shortcuts.size(); ++i)
190 if (
settings.status() != QSettings::NoError) {
192 tr(
"Could not write the settings file."));
202 importedSettings.sync();
203 if (importedSettings.status() != QSettings::NoError) {
205 tr(
"Could not read the selected settings file."));
210 copySettings(importedSettings,
settings);
212 if (
settings.status() != QSettings::NoError) {
214 tr(
"Could not save the imported settings."));
225 QTemporaryFile exportedFile(
this);
226 if (!exportedFile.open()) {
228 tr(
"Could not write the settings file."));
232 const QString exportedFileName = exportedFile.fileName();
233 exportedFile.close();
236 exportedSettings.clear();
237 copySettings(
settings, exportedSettings);
239 exportedSettings.sync();
241 if (exportedSettings.status() != QSettings::NoError) {
243 tr(
"Could not write the settings file."));
247 QFile file(exportedFileName);
248 if (!file.open(QIODevice::ReadOnly)) {
250 tr(
"Could not read back the exported settings file."));
253 return file.readAll();
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()) {
265 tr(
"Could not read the selected settings file."));
268 importedFile.flush();
269 const QString importedFileName = importedFile.fileName();
270 importedFile.close();
281 if (!content.isEmpty())
283 this, tr(
"Export Settings"), QStringLiteral(
"silicon-settings.toml"),
284 tr(
"TOML Settings (*.toml);;All Files (*)"), content);
289 const auto editor =
new QKeySequenceEdit(
this);
303 for (
int i = 0; i <
shortcuts.size(); ++i) {
317 for (
int i = 0; i <
shortcuts.size(); ++i)
318 target.setValue(
shortcuts[i].setting.name,
static void setMaxSimulationSteps(uint64_t value)
static void setMaxTransitionsPerDeltaCycle(int value)
QVector< ShortcutSetting > shortcuts
void loadSettings()
Loads persisted settings into the visible editors.
QPushButton * restoreDefaultsButton
QFormLayout * keybindingsLayout
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
QDialogButtonBox * buttonBox
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.
QPushButton * importButton
QPushButton * exportButton
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.
QSpinBox * maxSimulationStepsSpinBox
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)
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)
QVariant value(const QSettings &settings, const Definition &setting)
Reads a setting value, falling back to its declared default when unset.
constexpr auto LIGHT_THEME
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.
int maxTransitionsPerDeltaCycle
Shortcut row shown in the settings dialog.
std::optional< QString > help