src/settingseditor/settingseditor.cpp

Tue, 14 Jun 2022 20:35:11 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Tue, 14 Jun 2022 20:35:11 +0300
changeset 219
571e85c0d811
parent 218
63125c36de73
child 237
10a6298f636f
permissions
-rw-r--r--

fix comment

7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include <QSettings>
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
2 #include <QMdiArea>
218
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
3 #include "settings.h"
40
30cb5e836736 added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents: 39
diff changeset
4 #include "gl/common.h"
16
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
5 #include "keyboardshortcutseditor.h"
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 #include "settingseditor.h"
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 #include "ui_settingseditor.h"
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8
16
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
9 SettingsEditor::SettingsEditor(
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
10 const uiutilities::KeySequenceMap& defaultKeyboardShortcuts,
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
11 QWidget* parent
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
12 ) :
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 QDialog{parent},
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 ui{*new Ui_SettingsEditor},
218
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
15 libraries{this},
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
16 librariesEditor{this},
16
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
17 defaultKeyboardShortcuts{defaultKeyboardShortcuts}
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 this->ui.setupUi(this);
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
20 this->ui.keyboardShortcutsView->setModel(new KeyboardShortcutsEditor{parent, this});
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
21 this->ui.viewModeButtonGroup->setId(this->ui.viewModeTabs, int{QMdiArea::TabbedView});
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
22 this->ui.viewModeButtonGroup->setId(this->ui.viewModeSubWindows, int{QMdiArea::SubWindowView});
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 this->loadLocales();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 this->setDefaults();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 QVBoxLayout* layout = new QVBoxLayout{this};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 layout->addWidget(&librariesEditor);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 this->ui.tabLdrawLibraries->setLayout(layout);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 connect(
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 this,
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 &SettingsEditor::accepted,
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 this,
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 &SettingsEditor::handleAccepted);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 SettingsEditor::~SettingsEditor()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 delete &this->ui;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 void SettingsEditor::handleAccepted()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 {
218
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
42 setSetting<Setting::Locale>(this->ui.language->currentData().toString());
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
43 setSetting<Setting::MainColor>(this->ui.mainColorButton->selectedColor());
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
44 setSetting<Setting::BackgroundColor>(this->ui.backgroundColorButton->selectedColor());
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
45 setSetting<Setting::SelectedColor>(this->ui.selectedColorButton->selectedColor());
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
46 setSetting<Setting::LineThickness>(static_cast<GLfloat>(this->ui.lineThickness->value()));
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
47 setSetting<Setting::LineAntiAliasing>(this->ui.lineAntiAliasing->isChecked());
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
48 const int viewMode = this->ui.viewModeButtonGroup->checkedId();
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
49 if (viewMode != -1) {
218
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
50 setSetting<Setting::ViewMode>(static_cast<QMdiArea::ViewMode>(viewMode));
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
51 }
218
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
52 this->librariesEditor.saveSettings();
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 void SettingsEditor::loadLocales()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 this->ui.language->clear();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 QDir dir{":/locale"};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 // Collect translation files in built-in resources
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 QVector<QString> localeCodes = {"en"}; // English is the default locale
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 for (const QFileInfo& file : dir.entryInfoList(QDir::Files))
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 localeCodes.append(file.baseName());
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 std::sort(localeCodes.begin(), localeCodes.end());
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 this->ui.language->addItem(tr("System language"), "system");
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 for (const QString& localeCode : localeCodes)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 const QLocale locale{localeCode};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 const QString languageName = QLocale::languageToString(locale.language());
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 const QIcon flag{":/flags/" + localeCode + ".png"};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 this->ui.language->addItem(languageName, localeCode);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 this->ui.language->setItemIcon(this->ui.language->count() - 1, flag);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 void SettingsEditor::setDefaults()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 {
218
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
79 this->setCurrentLanguage(setting<Setting::Locale>());
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
80 this->ui.mainColorButton->setSelectedColor(setting<Setting::MainColor>());
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
81 this->ui.backgroundColorButton->setSelectedColor(setting<Setting::BackgroundColor>());
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
82 this->ui.selectedColorButton->setSelectedColor(setting<Setting::SelectedColor>());
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
83 this->ui.lineThickness->setValue(double_cast(setting<Setting::LineThickness>()));
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
84 this->ui.lineAntiAliasing->setChecked(setting<Setting::LineAntiAliasing>());
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 202
diff changeset
85 auto* const viewModeButton = this->ui.viewModeButtonGroup->button(setting<Setting::ViewMode>());
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
86 if (viewModeButton != nullptr) {
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
87 viewModeButton->setChecked(true);
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
88 }
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 void SettingsEditor::setCurrentLanguage(const QString& localeCode)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 for (int i = 0; i < this->ui.language->count(); i += 1)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 if (this->ui.language->itemData(i) == localeCode)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 this->ui.language->setCurrentIndex(i);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 break;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 }

mercurial