Wed, 01 Jan 2020 17:45:56 +0200
things
7 | 1 | #include <QSettings> |
16 | 2 | #include "keyboardshortcutseditor.h" |
7 | 3 | #include "settingseditor.h" |
4 | #include "ui_settingseditor.h" | |
5 | ||
16 | 6 | SettingsEditor::SettingsEditor( |
7 | QSettings* settings, | |
8 | const uiutilities::KeySequenceMap& defaultKeyboardShortcuts, | |
9 | QWidget* parent | |
10 | ) : | |
7 | 11 | QDialog{parent}, |
12 | ui{*new Ui_SettingsEditor}, | |
13 | settings{settings}, | |
14 | libraries{settings, this}, | |
16 | 15 | librariesEditor{settings, this}, |
16 | defaultKeyboardShortcuts{defaultKeyboardShortcuts} | |
7 | 17 | { |
18 | this->ui.setupUi(this); | |
19 | this->loadLocales(); | |
20 | this->setDefaults(); | |
21 | QVBoxLayout* layout = new QVBoxLayout{this}; | |
22 | layout->addWidget(&librariesEditor); | |
23 | this->ui.tabLdrawLibraries->setLayout(layout); | |
24 | connect( | |
25 | this, | |
26 | &SettingsEditor::accepted, | |
27 | this, | |
28 | &SettingsEditor::handleAccepted); | |
16 | 29 | this->ui.keyboardShortcutsView->setModel(new KeyboardShortcutsEditor{parent, this}); |
7 | 30 | } |
31 | ||
32 | SettingsEditor::~SettingsEditor() | |
33 | { | |
34 | delete &this->ui; | |
35 | } | |
36 | ||
37 | void SettingsEditor::handleAccepted() | |
38 | { | |
39 | this->settings->setValue("locale", this->ui.language->currentData().toString()); | |
40 | this->librariesEditor.saveSettings(this->settings); | |
41 | } | |
42 | ||
43 | void SettingsEditor::loadLocales() | |
44 | { | |
45 | this->ui.language->clear(); | |
46 | QDir dir{":/locale"}; | |
47 | // Collect translation files in built-in resources | |
48 | QVector<QString> localeCodes = {"en"}; // English is the default locale | |
49 | for (const QFileInfo& file : dir.entryInfoList(QDir::Files)) | |
50 | { | |
51 | localeCodes.append(file.baseName()); | |
52 | } | |
53 | std::sort(localeCodes.begin(), localeCodes.end()); | |
54 | this->ui.language->addItem(tr("System language"), "system"); | |
55 | for (const QString& localeCode : localeCodes) | |
56 | { | |
57 | const QLocale locale{localeCode}; | |
58 | const QString languageName = QLocale::languageToString(locale.language()); | |
59 | const QIcon flag{":/flags/" + localeCode + ".png"}; | |
60 | this->ui.language->addItem(languageName, localeCode); | |
61 | this->ui.language->setItemIcon(this->ui.language->count() - 1, flag); | |
62 | } | |
63 | } | |
64 | ||
65 | void SettingsEditor::setDefaults() | |
66 | { | |
67 | this->setCurrentLanguage(this->settings->value("locale", QLocale::system().name()).toString()); | |
68 | } | |
69 | ||
70 | void SettingsEditor::setCurrentLanguage(const QString& localeCode) | |
71 | { | |
72 | for (int i = 0; i < this->ui.language->count(); i += 1) | |
73 | { | |
74 | if (this->ui.language->itemData(i) == localeCode) | |
75 | { | |
76 | this->ui.language->setCurrentIndex(i); | |
77 | break; | |
78 | } | |
79 | } | |
80 | } |