src/settingseditor/settingseditor.cpp

changeset 341
71c8cea3c205
parent 335
c5830bce1c23
child 347
5c655cc006de
equal deleted inserted replaced
340:e17e07661f4c 341:71c8cea3c205
3 #include <ui_settingseditor.h> 3 #include <ui_settingseditor.h>
4 #include "src/settings.h" 4 #include "src/settings.h"
5 #include "src/gl/common.h" 5 #include "src/gl/common.h"
6 #include "src/settingseditor/keyboardshortcutseditor.h" 6 #include "src/settingseditor/keyboardshortcutseditor.h"
7 #include "src/settingseditor/settingseditor.h" 7 #include "src/settingseditor/settingseditor.h"
8
9 static QVariantMap storeSettings()
10 {
11 QVariantMap result;
12 QSettings settingsObject;
13 for (const QString& key : settingsObject.allKeys()) {
14 result[key] = settingsObject.value(key);
15 }
16 return result;
17 }
18
19 static void restoreSettings(const QVariantMap& storedValues)
20 {
21 QSettings settingsObject;
22 settingsObject.clear();
23 for (const QString& key : storedValues.keys()) {
24 settingsObject.setValue(key, storedValues[key]);
25 }
26 }
8 27
9 SettingsEditor::SettingsEditor( 28 SettingsEditor::SettingsEditor(
10 const uiutilities::KeySequenceMap& defaultKeyboardShortcuts, 29 const uiutilities::KeySequenceMap& defaultKeyboardShortcuts,
11 QWidget* parent 30 QWidget* parent
12 ) : 31 ) :
14 ui{*new Ui_SettingsEditor}, 33 ui{*new Ui_SettingsEditor},
15 libraries{this}, 34 libraries{this},
16 librariesEditor{this}, 35 librariesEditor{this},
17 defaultKeyboardShortcuts{defaultKeyboardShortcuts} 36 defaultKeyboardShortcuts{defaultKeyboardShortcuts}
18 { 37 {
19 QWidget* widget = new QWidget{this}; 38 QWidget* centralWidget = new QWidget{this};
20 this->ui.setupUi(widget); 39 this->ui.setupUi(centralWidget);
21 this->setWidget(widget); 40 this->setWidget(centralWidget);
22 this->ui.keyboardShortcutsView->setModel(new KeyboardShortcutsEditor{parent, this}); 41 this->ui.keyboardShortcutsView->setModel(new KeyboardShortcutsEditor{parent, this});
23 this->ui.viewModeButtonGroup->setId(this->ui.viewModeTabs, int{QMdiArea::TabbedView}); 42 this->ui.viewModeButtonGroup->setId(this->ui.viewModeTabs, int{QMdiArea::TabbedView});
24 this->ui.viewModeButtonGroup->setId(this->ui.viewModeSubWindows, int{QMdiArea::SubWindowView}); 43 this->ui.viewModeButtonGroup->setId(this->ui.viewModeSubWindows, int{QMdiArea::SubWindowView});
25 this->ui.toolButtonStyle->addItem(tr("Icons only"), Qt::ToolButtonIconOnly); 44 this->ui.toolButtonStyle->addItem(tr("Icons only"), Qt::ToolButtonIconOnly);
26 this->ui.toolButtonStyle->addItem(tr("Text only"), Qt::ToolButtonTextOnly); 45 this->ui.toolButtonStyle->addItem(tr("Text only"), Qt::ToolButtonTextOnly);
32 this->setWindowIcon(QIcon{":/icons/settings-outline.png"}); 51 this->setWindowIcon(QIcon{":/icons/settings-outline.png"});
33 this->librariesEditor.setModel(&libraries); 52 this->librariesEditor.setModel(&libraries);
34 QVBoxLayout* layout = new QVBoxLayout{this}; 53 QVBoxLayout* layout = new QVBoxLayout{this};
35 layout->addWidget(&librariesEditor); 54 layout->addWidget(&librariesEditor);
36 this->ui.tabLdrawLibraries->setLayout(layout); 55 this->ui.tabLdrawLibraries->setLayout(layout);
37 connect(this->ui.buttonBox, &QDialogButtonBox::clicked, 56 QSettings settingsObject;
38 [&](QAbstractButton* button) { 57 connect(
58 this->ui.buttonBox, &QDialogButtonBox::clicked,
59 [this, previousSettings = storeSettings()](QAbstractButton* button)
60 {
39 const auto role = this->ui.buttonBox->buttonRole(button); 61 const auto role = this->ui.buttonBox->buttonRole(button);
40 if (role == QDialogButtonBox::ApplyRole) { 62 switch (role)
41 this->saveSettings(); 63 {
64 case QDialogButtonBox::AcceptRole:
65 this->close();
66 break;
67 case QDialogButtonBox::RejectRole:
68 restoreSettings(previousSettings);
69 Q_EMIT this->settingsChanged();
70 this->close();
71 break;
72 case QDialogButtonBox::ResetRole:
73 restoreSettings(previousSettings);
74 Q_EMIT this->settingsChanged();
75 break;
76 default:
77 break;
42 } 78 }
43 }); 79 }
80 );
81 for (auto* widget : this->findChildren<QAbstractButton*>()) {
82 connect(widget, &QAbstractButton::clicked, this, &SettingsEditor::saveSettings);
83 }
84 for (auto* widget : this->findChildren<QSpinBox*>()) {
85 connect(widget, qOverload<int>(&QSpinBox::valueChanged), this, &SettingsEditor::saveSettings);
86 }
87 for (auto* widget : this->findChildren<ColorButton*>()) {
88 connect(widget, &ColorButton::colorChanged, this, &SettingsEditor::saveSettings);
89 }
90 for (auto* widget : this->findChildren<QComboBox*>()) {
91 connect(widget, qOverload<int>(&QComboBox::currentIndexChanged), this, &SettingsEditor::saveSettings);
92 }
44 } 93 }
45 94
46 SettingsEditor::~SettingsEditor() 95 SettingsEditor::~SettingsEditor()
47 { 96 {
48 delete &this->ui; 97 delete &this->ui;

mercurial