From 5507f0a17501767db2e4a814b62db2af2e34b2ea Mon Sep 17 00:00:00 2001 From: Elias Steurer Date: Sat, 25 Jan 2020 20:31:35 +0100 Subject: [PATCH] Fix #44 Remove hardcoded langauge keys Remove currentIndex and ListModel comboBoxListModel --- ScreenPlay/qml/Settings/Settings.qml | 59 +++++++++----------- ScreenPlay/qml/Settings/SettingsComboBox.qml | 6 +- ScreenPlay/src/settings.cpp | 2 + ScreenPlay/src/settings.h | 19 ++++++- 4 files changed, 46 insertions(+), 40 deletions(-) diff --git a/ScreenPlay/qml/Settings/Settings.qml b/ScreenPlay/qml/Settings/Settings.qml index 0268c9a3..ca155012 100644 --- a/ScreenPlay/qml/Settings/Settings.qml +++ b/ScreenPlay/qml/Settings/Settings.qml @@ -1,5 +1,5 @@ -import QtQuick 2.12 -import QtQuick.Controls 2.3 +import QtQuick 2.14 +import QtQuick.Controls 2.14 import QtQuick.Dialogs 1.2 import QtQuick.Layouts 1.3 import QtGraphicalEffects 1.0 @@ -162,58 +162,52 @@ Item { } SettingsHorizontalSeperator {} + SettingsComboBox { id: settingsLanguage headline: qsTr("Language") description: qsTr("Set the ScreenPlay UI Language") - onCurrentIndexChanged: { - var key = settingsLanguage.comboBoxListModel.get( - settingsLanguage.currentIndex).text.toString() + comboBox.currentIndex: indexOfValue(ScreenPlay.settings.language) - var languageKey - switch (key) { - case "German": - languageKey = "de" - break - case "English": - languageKey = "en" - break - case "Russian": - languageKey = "ru" - break - case "French": - languageKey = "fr" - break - case "Spanish": - languageKey = "es" - break - default: - languageKey = "en" - break + function indexOfValue(value) { + for (var i = 0; i < liLanguage.count; i++) { + let ourValue = liLanguage.get(i).value + if (value === ourValue) + return i } - - ScreenPlay.settings.setqSetting("language", - languageKey) - + return -1 + } + comboBox.onActivated: { + ScreenPlay.settings.setqSetting( + "language", + liLanguage.get(comboBox.currentIndex).value) ScreenPlay.settings.setupLanguage() ScreenPlay.mainWindowEngine.retranslate() } - comboBoxListModel: ListModel { + comboBox.textRole: "text" + comboBox.valueRole: "value" + comboBox.model: ListModel { + id: liLanguage ListElement { text: "English" + value: "en" } ListElement { text: "German" + value: "de" } ListElement { text: "Russian" + value: "ru" } ListElement { text: "French" + value: "fr" } ListElement { text: "Spanish" + value: "es" } } } @@ -286,10 +280,7 @@ Item { id: settingsFillModeComboBox headline: qsTr("Default Fill Mode") description: qsTr("Set this property to define how the video is scaled to fit the target area.") - onCurrentIndexChanged: { - - } - comboBoxListModel: ListModel { + comboBox.model: ListModel { ListElement { text: "Stretch" } diff --git a/ScreenPlay/qml/Settings/SettingsComboBox.qml b/ScreenPlay/qml/Settings/SettingsComboBox.qml index 16d32db0..c341fb0a 100644 --- a/ScreenPlay/qml/Settings/SettingsComboBox.qml +++ b/ScreenPlay/qml/Settings/SettingsComboBox.qml @@ -6,10 +6,9 @@ Item { id: settingsComboBox property string headline: "Headline" property string description: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit." - property int currentIndex:0 property bool isChecked: false property bool enabled: true - property ListModel comboBoxListModel + property alias comboBox: comboBox onEnabledChanged: { @@ -69,8 +68,5 @@ Item { rightMargin: 20 verticalCenter: parent.verticalCenter } - - model: comboBoxListModel - onActivated:settingsComboBox.currentIndex = currentIndex } } diff --git a/ScreenPlay/src/settings.cpp b/ScreenPlay/src/settings.cpp index 26a86f6e..d16f46c2 100644 --- a/ScreenPlay/src/settings.cpp +++ b/ScreenPlay/src/settings.cpp @@ -302,6 +302,7 @@ void Settings::setupLanguage() if (tsFile.exists(":/translations/ScreenPlay_" + localeSplits.at(0) + ".qm")) { m_translator.load(":/translations/ScreenPlay_" + localeSplits.at(0) + ".qm"); m_qSettings.setValue("language", QVariant(localeSplits.at(0))); + setLanguage(QVariant(localeSplits.at(0)).toString()); m_qSettings.sync(); app->installTranslator(&m_translator); } @@ -309,6 +310,7 @@ void Settings::setupLanguage() QFile tsFile; if (tsFile.exists(":/translations/ScreenPlay_" + m_qSettings.value("language").toString() + ".qm")) { m_translator.load(":/translations/ScreenPlay_" + m_qSettings.value("language").toString() + ".qm"); + setLanguage(m_qSettings.value("language").toString()); app->installTranslator(&m_translator); } } diff --git a/ScreenPlay/src/settings.h b/ScreenPlay/src/settings.h index 10722027..98776391 100644 --- a/ScreenPlay/src/settings.h +++ b/ScreenPlay/src/settings.h @@ -54,6 +54,7 @@ class Settings : public QObject { Q_PROPERTY(bool offlineMode READ offlineMode WRITE setOfflineMode NOTIFY offlineModeChanged) Q_PROPERTY(QString decoder READ decoder WRITE setDecoder NOTIFY decoderChanged) Q_PROPERTY(QString gitBuildHash READ gitBuildHash WRITE setGitBuildHash NOTIFY gitBuildHashChanged) + Q_PROPERTY(QString language READ language WRITE setLanguage NOTIFY languageChanged) public: explicit Settings( @@ -110,6 +111,11 @@ public: return m_anonymousTelemetry; } + QString language() const + { + return m_language; + } + signals: void autostartChanged(bool autostart); void highPriorityStartChanged(bool highPriorityStart); @@ -124,6 +130,8 @@ signals: void anonymousTelemetryChanged(bool anonymousTelemetry); + void languageChanged(QString language); + public slots: bool writeSingleSettingConfig(QString name, QVariant value); void writeJsonFileFromResource(const QString& filename); @@ -171,7 +179,6 @@ public slots: void setLocalStoragePath(QUrl localStoragePath) { - //Remove: "file:///" QJsonValue cleanedPath = QJsonValue(localStoragePath.toString()); @@ -236,6 +243,15 @@ public slots: emit anonymousTelemetryChanged(m_anonymousTelemetry); } + void setLanguage(QString language) + { + if (m_language == language) + return; + + m_language = language; + emit languageChanged(m_language); + } + private: void restoreDefault(const QString& appConfigLocation, const QString& settingsFileType); @@ -255,5 +271,6 @@ private: QString m_gitBuildHash; bool m_silentStart { false }; bool m_anonymousTelemetry { true }; + QString m_language; }; }