src/settingseditor/settingseditor.cpp

Tue, 07 Jun 2022 20:44:19 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 07 Jun 2022 20:44:19 +0300
changeset 202
b05af0bab735
parent 48
3c10f0e2fbe0
child 218
63125c36de73
permissions
-rw-r--r--

Replaced the tab widget with an MDI area

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>
40
30cb5e836736 added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents: 39
diff changeset
3 #include "gl/common.h"
16
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
4 #include "keyboardshortcutseditor.h"
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 #include "settingseditor.h"
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 #include "ui_settingseditor.h"
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7
16
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
8 SettingsEditor::SettingsEditor(
41
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
9 Configuration* settings,
16
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},
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 settings{settings},
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 libraries{settings, this},
16
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
17 librariesEditor{settings, this},
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
18 defaultKeyboardShortcuts{defaultKeyboardShortcuts}
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 this->ui.setupUi(this);
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
21 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
22 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
23 this->ui.viewModeButtonGroup->setId(this->ui.viewModeSubWindows, int{QMdiArea::SubWindowView});
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 this->loadLocales();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 this->setDefaults();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 QVBoxLayout* layout = new QVBoxLayout{this};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 layout->addWidget(&librariesEditor);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 this->ui.tabLdrawLibraries->setLayout(layout);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 connect(
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 this,
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 &SettingsEditor::accepted,
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 this,
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 &SettingsEditor::handleAccepted);
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
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 SettingsEditor::~SettingsEditor()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 delete &this->ui;
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
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 void SettingsEditor::handleAccepted()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 {
41
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
43 this->settings->setLocale(this->ui.language->currentData().toString());
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
44 this->settings->setMainColor(this->ui.mainColorButton->selectedColor());
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
45 this->settings->setBackgroundColor(this->ui.backgroundColorButton->selectedColor());
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
46 this->settings->setSelectedColor(this->ui.selectedColorButton->selectedColor());
44
c6114b3af3a6 added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents: 41
diff changeset
47 this->settings->setLineThickness(static_cast<GLfloat>(this->ui.lineThickness->value()));
45
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
48 this->settings->setLineAntiAliasing(this->ui.lineAntiAliasing->isChecked());
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
49 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
50 if (viewMode != -1) {
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
51 this->settings->setViewMode(viewMode);
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
52 }
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 this->librariesEditor.saveSettings(this->settings);
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
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 void SettingsEditor::loadLocales()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 this->ui.language->clear();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 QDir dir{":/locale"};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 // Collect translation files in built-in resources
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 QVector<QString> localeCodes = {"en"}; // English is the default locale
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 for (const QFileInfo& file : dir.entryInfoList(QDir::Files))
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 localeCodes.append(file.baseName());
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 std::sort(localeCodes.begin(), localeCodes.end());
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 this->ui.language->addItem(tr("System language"), "system");
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 for (const QString& localeCode : localeCodes)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 const QLocale locale{localeCode};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 const QString languageName = QLocale::languageToString(locale.language());
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 const QIcon flag{":/flags/" + localeCode + ".png"};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 this->ui.language->addItem(languageName, localeCode);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 this->ui.language->setItemIcon(this->ui.language->count() - 1, flag);
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
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 void SettingsEditor::setDefaults()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 {
41
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
80 this->setCurrentLanguage(this->settings->locale());
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
81 this->ui.mainColorButton->setSelectedColor(this->settings->mainColor());
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents: 40
diff changeset
82 this->ui.backgroundColorButton->setSelectedColor(this->settings->backgroundColor());
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
83 this->ui.selectedColorButton->setSelectedColor(this->settings->selectedColor());
44
c6114b3af3a6 added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents: 41
diff changeset
84 this->ui.lineThickness->setValue(static_cast<double>(this->settings->lineThickness()));
45
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
85 this->ui.lineAntiAliasing->setChecked(this->settings->lineAntiAliasing());
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
86 auto* const viewModeButton = this->ui.viewModeButtonGroup->button(this->settings->viewMode());
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
87 if (viewModeButton != nullptr) {
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
88 viewModeButton->setChecked(true);
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
89 }
7
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
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 void SettingsEditor::setCurrentLanguage(const QString& localeCode)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 for (int i = 0; i < this->ui.language->count(); i += 1)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 if (this->ui.language->itemData(i) == localeCode)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 this->ui.language->setCurrentIndex(i);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 break;
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 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 }

mercurial