From 6d6544043daa68edc8b24b5223d508ea63c49ee0 Mon Sep 17 00:00:00 2001 From: Samir Aguiar Date: Sun, 18 Jan 2015 01:57:42 +0100 Subject: [PATCH] preferencesdialog: New combobox to change application language The behavior remains basically as before: when first launched the application will try to load a translation for the user's locale, and if one cannot be found it defaults to English. The difference is that now this is remembered so that upon further launches the program will go straight to the matching locale. See issue #182. --- src/Application.cpp | 10 ++- src/PreferencesDialog.cpp | 58 ++++++++++++++ src/PreferencesDialog.h | 2 + src/PreferencesDialog.ui | 161 +++++++++++++++++++++++--------------- 4 files changed, 169 insertions(+), 62 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index b9069c07..791e3efb 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -8,6 +8,7 @@ #include "Application.h" #include "MainWindow.h" +#include "PreferencesDialog.h" Application::Application(int& argc, char** argv) : QApplication(argc, argv) @@ -25,12 +26,13 @@ Application::Application(int& argc, char** argv) : // Load translations bool ok; - QString name = QLocale::system().name(); + QString name = PreferencesDialog::getSettingsValue("General", "language").toString(); // First of all try to load the application translation file. m_translatorApp = new QTranslator(this); ok = m_translatorApp->load("sqlb_" + name, "translations"); if (ok == true) { + PreferencesDialog::setSettingsValue("General", "language", name); installTranslator(m_translatorApp); // The application translation file has been found and loaded. @@ -47,6 +49,12 @@ Application::Application(int& argc, char** argv) : if (ok == true) installTranslator(m_translatorQt); } + else { + // Set the correct locale so that the program won't erroneously detect + // a language change when the user toggles settings for the first time. + // (it also prevents the program from always looking for a translation on launch) + PreferencesDialog::setSettingsValue("General", "language", "en_US"); + } // Parse command line QString fileToOpen; diff --git a/src/PreferencesDialog.cpp b/src/PreferencesDialog.cpp index 2b9624fb..3cd8cca5 100644 --- a/src/PreferencesDialog.cpp +++ b/src/PreferencesDialog.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include QHash PreferencesDialog::m_hCache; @@ -16,6 +18,15 @@ PreferencesDialog::PreferencesDialog(QWidget* parent) ui->setupUi(this); ui->treeSyntaxHighlighting->setColumnHidden(0, true); + // Model to sort the languages in the language combo box + QSortFilterProxyModel *proxy = new QSortFilterProxyModel(ui->languageComboBox); + proxy->setSourceModel(ui->languageComboBox->model()); + + // Prevent setModel() from deleting the current model (now source of the proxy model) + ui->languageComboBox->model()->setParent(proxy); + + ui->languageComboBox->setModel(proxy); + loadSettings(); } @@ -63,6 +74,7 @@ void PreferencesDialog::loadSettings() ui->spinLogFontSize->setValue(getSettingsValue("log", "fontsize").toInt()); ui->listExtensions->addItems(getSettingsValue("extensions", "list").toStringList()); + fillLanguageBox(); } void PreferencesDialog::saveSettings() @@ -89,6 +101,14 @@ void PreferencesDialog::saveSettings() extList.append(item->text()); setSettingsValue("extensions", "list", extList); + // Warn about restarting to change language + QVariant newLanguage = ui->languageComboBox->itemData(ui->languageComboBox->currentIndex()); + if (newLanguage != getSettingsValue("General", "language")) + QMessageBox::information(this, QApplication::applicationName(), + tr("The language will change after you restart the application.")); + + setSettingsValue("General", "language", newLanguage); + accept(); } @@ -160,6 +180,10 @@ QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const if(group == "General" && name == "recentFileList") return QStringList(); + // General/language? + if(group == "General" && name == "language") + return QLocale::system().name(); + // syntaxhighlighter? if(group == "syntaxhighlighter") { @@ -238,3 +262,37 @@ void PreferencesDialog::removeExtension() if(ui->listExtensions->currentIndex().isValid()) ui->listExtensions->takeItem(ui->listExtensions->currentIndex().row()); } + +void PreferencesDialog::fillLanguageBox() +{ + QDir translationsDir("translations", "sqlb_*.qm"); + + // Add default language + ui->languageComboBox->addItem("English (United States)", "en_US"); + + foreach(const QFileInfo &file, translationsDir.entryInfoList()) + { + QLocale locale(file.baseName().remove("sqlb_")); + + // Prevent invalid locales from being added to the box + if(locale.name() == "C") + continue; + + QString language = QLocale::languageToString(locale.language()) + " (" + + QLocale::countryToString(locale.country()) + ")"; + + ui->languageComboBox->addItem(language, locale.name()); + } + + ui->languageComboBox->model()->sort(0); + + // Try to select the language for the stored locale + int index = ui->languageComboBox->findData(getSettingsValue("General", "language"), + Qt::UserRole, Qt::MatchExactly); + + // If there's no translation for the current locale, default to English + if(index < 0) + index = ui->languageComboBox->findData("en_US", Qt::UserRole, Qt::MatchExactly); + + ui->languageComboBox->setCurrentIndex(index); +} diff --git a/src/PreferencesDialog.h b/src/PreferencesDialog.h index 3b7e4cc1..dab9517a 100644 --- a/src/PreferencesDialog.h +++ b/src/PreferencesDialog.h @@ -38,6 +38,8 @@ private: // This works similar to getSettingsValue but returns the default value instead of the value set by the user static QVariant getSettingsDefaultValue(const QString& group, const QString& name); + void fillLanguageBox(); + // Cache for storing the settings to avoid repeatedly reading the settings file all the time static QHash m_hCache; }; diff --git a/src/PreferencesDialog.ui b/src/PreferencesDialog.ui index 556cc456..1a756f03 100644 --- a/src/PreferencesDialog.ui +++ b/src/PreferencesDialog.ui @@ -22,6 +22,46 @@ 0 + + + General + + + + + 140 + 10 + 61 + 20 + + + + Language + + + + + + 210 + 10 + 211 + 26 + + + + + 0 + 0 + + + + QComboBox::AdjustToMinimumContentsLength + + + 20 + + + &Database @@ -74,33 +114,18 @@ - - + + - Default &location + Remove line breaks in schema view - locationEdit + checkHideSchemaLinebreaks - - - - - - false - - - - - - - ... - - - - + + @@ -122,19 +147,34 @@ - - + + + + + + false + + + + + + + ... + + + + + + + - Remove line breaks in schema view + Default &location - checkHideSchemaLinebreaks + locationEdit - - - @@ -420,7 +460,6 @@ - tabWidget encodingComboBox foreignKeysCheckBox locationEdit @@ -470,38 +509,6 @@ - - setLocationButton - clicked() - PreferencesDialog - chooseLocation() - - - 568 - 119 - - - 459 - 62 - - - - - treeSyntaxHighlighting - itemDoubleClicked(QTreeWidgetItem*,int) - PreferencesDialog - showColourDialog(QTreeWidgetItem*,int) - - - 103 - 53 - - - 395 - -1 - - - buttonAddExtension clicked() @@ -534,6 +541,38 @@ + + treeSyntaxHighlighting + itemDoubleClicked(QTreeWidgetItem*,int) + PreferencesDialog + showColourDialog(QTreeWidgetItem*,int) + + + 103 + 53 + + + 395 + -1 + + + + + setLocationButton + clicked() + PreferencesDialog + chooseLocation() + + + 568 + 119 + + + 459 + 62 + + + saveSettings()