From fd112fc16e16858672d35a4b44141528e6f847ea Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Mon, 7 Feb 2011 12:30:13 +0200 Subject: [PATCH] GUI: Store selected language as language code. GUI was storing selected language as index to the languages list. This is fragile since the order and count of items can change. This commit changes to using ISO language code (e.g. "en" for "English"). Fixes ticket #2446 (GUI: Don't use index number for language selection) --- gui/mainwindow.cpp | 12 ++++----- gui/mainwindow.h | 4 +-- gui/settingsdialog.cpp | 19 ++++++++++----- gui/settingsdialog.h | 1 + gui/translationhandler.cpp | 50 ++++++++++++++++++++------------------ gui/translationhandler.h | 14 ++++++++--- 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index f0cda060c..fa473f30c 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -154,7 +154,7 @@ void MainWindow::LoadSettings() mApplications->LoadSettings(mSettings); - SetLanguage(mSettings->value(SETTINGS_LANGUAGE, mTranslation->SuggestLanguage()).toInt()); + SetLanguage(mSettings->value(SETTINGS_LANGUAGE, mTranslation->SuggestLanguage()).toString()); } void MainWindow::SaveSettings() @@ -410,8 +410,8 @@ void MainWindow::ProgramSettings() dialog.SaveFullPath(), dialog.SaveAllErrors(), dialog.ShowNoErrorsMessage()); - const int currentLang = mTranslation->GetCurrentLanguage(); - const int newLang = mSettings->value(SETTINGS_LANGUAGE, 0).toInt(); + const QString currentLang = mTranslation->GetCurrentLanguage(); + const QString newLang = mSettings->value(SETTINGS_LANGUAGE, "en").toString(); if (currentLang != newLang) SetLanguage(newLang); } @@ -651,15 +651,15 @@ void MainWindow::FormatAndSetTitle(const QString &text) } -void MainWindow::SetLanguage(int index) +void MainWindow::SetLanguage(const QString &code) { - if (mTranslation->GetCurrentLanguage() == index) + if (mTranslation->GetCurrentLanguage() == code) { return; } QString error; - if (!mTranslation->SetLanguage(index, error)) + if (!mTranslation->SetLanguage(code, error)) { QMessageBox msg(QMessageBox::Critical, tr("Cppcheck"), diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 1455773aa..ea844a287 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -255,9 +255,9 @@ protected: /** * @brief Set current language - * @param index Index of the language to set + * @param code Language code of the language to set (e.g. "en"). */ - void SetLanguage(const int index); + void SetLanguage(const QString &code); /** * @brief Event coming when application is about to close. diff --git a/gui/settingsdialog.cpp b/gui/settingsdialog.cpp index 28fcf4773..2301950fa 100644 --- a/gui/settingsdialog.cpp +++ b/gui/settingsdialog.cpp @@ -91,13 +91,17 @@ SettingsDialog::~SettingsDialog() void SettingsDialog::InitTranslationsList() { - QStringList languages = mTranslator->GetNames(); - foreach(const QString lang, languages) + const QString current = mTranslator->GetCurrentLanguage(); + QList translations = mTranslator->GetTranslations(); + foreach(TranslationInfo translation, translations) { - mUI.mListLanguages->addItem(lang); + QListWidgetItem *item = new QListWidgetItem; + item->setText(translation.mName); + item->setData(LangCodeRole, QVariant(translation.mCode)); + mUI.mListLanguages->addItem(item); + if (translation.mCode == current) + mUI.mListLanguages->setCurrentItem(item); } - const int current = mTranslator->GetCurrentLanguage(); - mUI.mListLanguages->setCurrentRow(current); } Qt::CheckState SettingsDialog::BoolToCheckState(bool yes) const @@ -148,7 +152,10 @@ void SettingsDialog::SaveSettingValues() SaveCheckboxValue(mUI.mShowDebugWarnings, SETTINGS_SHOW_DEBUG_WARNINGS); SaveCheckboxValue(mUI.mInlineSuppressions, SETTINGS_INLINE_SUPPRESSIONS); mSettings->setValue(SETTINGS_GLOBAL_INCLUDE_PATHS, mUI.mEditIncludePaths->text()); - mSettings->setValue(SETTINGS_LANGUAGE, mUI.mListLanguages->currentRow()); + + QListWidgetItem *currentLang = mUI.mListLanguages->currentItem(); + const QString langcode = currentLang->data(LangCodeRole).toString(); + mSettings->setValue(SETTINGS_LANGUAGE, langcode); } void SettingsDialog::SaveCheckboxValue(QCheckBox *box, const QString &name) diff --git a/gui/settingsdialog.h b/gui/settingsdialog.h index b99b5c294..d2a9e11ad 100644 --- a/gui/settingsdialog.h +++ b/gui/settingsdialog.h @@ -201,6 +201,7 @@ protected: */ Ui::Settings mUI; private: + static const int LangCodeRole = Qt::UserRole; }; /// @} #endif // SETTINGSDIALOG_H diff --git a/gui/translationhandler.cpp b/gui/translationhandler.cpp index cb8712b66..7f7e56bed 100644 --- a/gui/translationhandler.cpp +++ b/gui/translationhandler.cpp @@ -24,7 +24,6 @@ TranslationHandler::TranslationHandler(QObject *parent) : QObject(parent), - mCurrentLanguage(-1), mTranslator(new QTranslator(this)) { // Add our available languages @@ -66,10 +65,10 @@ const QStringList TranslationHandler::GetNames() const return names; } -bool TranslationHandler::SetLanguage(const int index, QString &error) +bool TranslationHandler::SetLanguage(const QString &code, QString &error) { //If English is the language - if (index == 2) + if (code == "en") { //Just remove all extra translators if (mTranslator) @@ -77,12 +76,13 @@ bool TranslationHandler::SetLanguage(const int index, QString &error) qApp->removeTranslator(mTranslator); } - mCurrentLanguage = index; + mCurrentLanguage = code; return true; } //Make sure the translator is otherwise valid - if (index >= mTranslations.size()) + int index = GetLanguageIndexByCode(code); + if (index == -1) { error = QObject::tr("Incorrect language specified!"); return false; @@ -108,17 +108,17 @@ bool TranslationHandler::SetLanguage(const int index, QString &error) qApp->installTranslator(mTranslator); - mCurrentLanguage = index; + mCurrentLanguage = code; return true; } -int TranslationHandler::GetCurrentLanguage() const +QString TranslationHandler::GetCurrentLanguage() const { return mCurrentLanguage; } -int TranslationHandler::SuggestLanguage() const +QString TranslationHandler::SuggestLanguage() const { /* Get language from system locale's name @@ -129,29 +129,16 @@ int TranslationHandler::SuggestLanguage() const QString language = QLocale::system().name().left(2); //qDebug()<<"Your language is"< GetTranslations() const + { + return mTranslations; + } + bool SetLanguage(const QString &code, QString &error); + QString GetCurrentLanguage() const; + QString SuggestLanguage() const; protected: void AddTranslation(const char *name, const char *filename); + int GetLanguageIndexByCode(const QString &code) const; - int mCurrentLanguage; + QString mCurrentLanguage; QList mTranslations; QTranslator *mTranslator; private: