src/settingseditor/settingseditor.cpp

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

mercurial