Sun, 09 Apr 2023 16:30:33 +0300
Also connect up the "Delete" action
#include <QSettings> #include <QMdiArea> #include <QFileDialog> #include <ui_settingseditor.h> #include "src/settings.h" #include "src/gl/common.h" #include "src/settingseditor/keyboardshortcutseditor.h" #include "src/settingseditor/settingseditor.h" static QVariantMap storeSettings() { QVariantMap result; QSettings settingsObject; for (const QString& key : settingsObject.allKeys()) { result[key] = settingsObject.value(key); } return result; } static void restoreSettings(const QVariantMap& storedValues) { QSettings settingsObject; settingsObject.clear(); for (const QString& key : storedValues.keys()) { settingsObject.setValue(key, storedValues[key]); } } SettingsEditor::SettingsEditor( const uiutilities::KeySequenceMap& defaultKeyboardShortcuts, QWidget* parent ) : QMdiSubWindow{parent}, ui{*new Ui_SettingsEditor}, libraries{this}, librariesEditor{this}, defaultKeyboardShortcuts{defaultKeyboardShortcuts} { QWidget* centralWidget = new QWidget{this}; this->ui.setupUi(centralWidget); this->setWidget(centralWidget); this->ui.keyboardShortcutsView->setModel(new KeyboardShortcutsEditor{parent, this}); this->ui.viewModeButtonGroup->setId(this->ui.viewModeTabs, int{QMdiArea::TabbedView}); this->ui.viewModeButtonGroup->setId(this->ui.viewModeSubWindows, int{QMdiArea::SubWindowView}); this->ui.toolButtonStyle->addItem(tr("Icons only"), Qt::ToolButtonIconOnly); this->ui.toolButtonStyle->addItem(tr("Text only"), Qt::ToolButtonTextOnly); this->ui.toolButtonStyle->addItem(tr("Text beside icon"), Qt::ToolButtonTextBesideIcon); this->ui.toolButtonStyle->addItem(tr("Text under icon"), Qt::ToolButtonTextUnderIcon); this->ui.toolButtonStyle->addItem(tr("Style default"), Qt::ToolButtonFollowStyle); connect( this->ui.codeEditorSystemFont, &QCheckBox::stateChanged, [this](int state){ const bool checked = (state == Qt::Checked); this->ui.codeEditorFontFamily->setEnabled(not checked); this->ui.codeEditorFontSize->setEnabled(not checked); } ); this->loadSettings(); this->setWindowTitle(tr("Settings")); this->setWindowIcon(QIcon{":/icons/settings-outline.png"}); this->librariesEditor.setModel(&libraries); QVBoxLayout* layout = new QVBoxLayout{this}; layout->addWidget(&librariesEditor); this->ui.tabLdrawLibraries->setLayout(layout); QSettings settingsObject; connect( this->ui.buttonBox, &QDialogButtonBox::clicked, [this, previousSettings = storeSettings()](QAbstractButton* button) { const auto role = this->ui.buttonBox->buttonRole(button); switch (role) { case QDialogButtonBox::AcceptRole: this->close(); break; case QDialogButtonBox::RejectRole: restoreSettings(previousSettings); Q_EMIT this->settingsChanged(); this->close(); break; case QDialogButtonBox::ResetRole: restoreSettings(previousSettings); Q_EMIT this->settingsChanged(); break; default: break; } } ); connect( this->ui.exportSettingsButton, &QAbstractButton::clicked, [&] { const QString defaultName = QStringLiteral(CMAKE_PROJECT_NAME ".conf"); QString filename = QFileDialog::getSaveFileName(this, "", defaultName, "*.conf"); if (not filename.isEmpty()) { QSettings settings; QSettings exportSettings{filename, QSettings::IniFormat}; for (const QString& key : settings.allKeys()) { exportSettings.setValue(key, settings.value(key)); } exportSettings.sync(); } } ); connect( this->ui.importSettingsButton, &QAbstractButton::clicked, [&] { QString filename = QFileDialog::getOpenFileName(this, "", "", "*.ini"); if (not filename.isEmpty()) { QSettings settings; const QSettings importSettings{filename}; settings.clear(); for (const QString& key : importSettings.allKeys()) { settings.setValue(key, importSettings.value(key)); } settings.sync(); this->loadSettings(); } } ); for (auto* widget : this->findChildren<QAbstractButton*>()) { connect(widget, &QAbstractButton::clicked, this, &SettingsEditor::saveSettings); } for (auto* widget : this->findChildren<QSpinBox*>()) { connect(widget, qOverload<int>(&QSpinBox::valueChanged), this, &SettingsEditor::saveSettings); } for (auto* widget : this->findChildren<ColorEdit*>()) { connect(widget, &ColorEdit::colorChanged, this, &SettingsEditor::saveSettings); } for (auto* widget : this->findChildren<QComboBox*>()) { connect(widget, qOverload<int>(&QComboBox::currentIndexChanged), this, &SettingsEditor::saveSettings); } } SettingsEditor::~SettingsEditor() { delete &this->ui; } void SettingsEditor::saveSettings() { if (not disableSettingsAutoSave) { setSetting<Setting::MainColor>(this->ui.mainColorButton->color()); setSetting<Setting::BackgroundColor>(this->ui.backgroundColorButton->color()); setSetting<Setting::SelectedColor>(this->ui.selectedColorButton->color()); setSetting<Setting::LineThickness>(static_cast<GLfloat>(this->ui.lineThickness->value())); setSetting<Setting::LineAntiAliasing>(this->ui.lineAntiAliasing->isChecked()); setSetting<Setting::LogOpenGLDebugMessages>(this->ui.logOpenGLDebugMessages->isChecked()); setSetting<Setting::CodeEditorUseSystemFont>(this->ui.codeEditorSystemFont->isChecked()); setSetting<Setting::CodeEditorFontFamily>(this->ui.codeEditorFontFamily->currentText()); setSetting<Setting::CodeEditorFontSize>(this->ui.codeEditorFontSize->value()); const int viewMode = this->ui.viewModeButtonGroup->checkedId(); if (viewMode != -1) { setSetting<Setting::ViewMode>(static_cast<QMdiArea::ViewMode>(viewMode)); } const QVariant toolButtonStyle = this->ui.toolButtonStyle->currentData(); if (toolButtonStyle.isValid()) { setSetting<Setting::ToolButtonStyle>( static_cast<Qt::ToolButtonStyle>(toolButtonStyle.toInt())); } this->librariesEditor.saveSettings(); Q_EMIT this->settingsChanged(); } } void SettingsEditor::loadSettings() { this->disableSettingsAutoSave = true; this->libraries.restoreFromSettings(); this->ui.mainColorButton->setColor(setting<Setting::MainColor>()); this->ui.backgroundColorButton->setColor(setting<Setting::BackgroundColor>()); this->ui.selectedColorButton->setColor(setting<Setting::SelectedColor>()); this->ui.lineThickness->setValue(double_cast(setting<Setting::LineThickness>())); this->ui.lineAntiAliasing->setChecked(setting<Setting::LineAntiAliasing>()); this->ui.logOpenGLDebugMessages->setChecked(setting<Setting::LogOpenGLDebugMessages>()); this->ui.codeEditorSystemFont->setChecked(setting<Setting::CodeEditorUseSystemFont>()); this->ui.codeEditorFontFamily->setCurrentText(setting<Setting::CodeEditorFontFamily>()); this->ui.codeEditorFontSize->setValue(setting<Setting::CodeEditorFontSize>()); auto* const viewModeButton = this->ui.viewModeButtonGroup->button(setting<Setting::ViewMode>()); if (viewModeButton != nullptr) { viewModeButton->setChecked(true); } int toolButtonStyleIndex = this->ui.toolButtonStyle->findData( setting<Setting::ToolButtonStyle>()); if (toolButtonStyleIndex != -1) { this->ui.toolButtonStyle->setCurrentIndex(toolButtonStyleIndex); } this->disableSettingsAutoSave = false; }