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)
This commit is contained in:
parent
64a70fa51c
commit
fd112fc16e
|
@ -154,7 +154,7 @@ void MainWindow::LoadSettings()
|
||||||
|
|
||||||
mApplications->LoadSettings(mSettings);
|
mApplications->LoadSettings(mSettings);
|
||||||
|
|
||||||
SetLanguage(mSettings->value(SETTINGS_LANGUAGE, mTranslation->SuggestLanguage()).toInt());
|
SetLanguage(mSettings->value(SETTINGS_LANGUAGE, mTranslation->SuggestLanguage()).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::SaveSettings()
|
void MainWindow::SaveSettings()
|
||||||
|
@ -410,8 +410,8 @@ void MainWindow::ProgramSettings()
|
||||||
dialog.SaveFullPath(),
|
dialog.SaveFullPath(),
|
||||||
dialog.SaveAllErrors(),
|
dialog.SaveAllErrors(),
|
||||||
dialog.ShowNoErrorsMessage());
|
dialog.ShowNoErrorsMessage());
|
||||||
const int currentLang = mTranslation->GetCurrentLanguage();
|
const QString currentLang = mTranslation->GetCurrentLanguage();
|
||||||
const int newLang = mSettings->value(SETTINGS_LANGUAGE, 0).toInt();
|
const QString newLang = mSettings->value(SETTINGS_LANGUAGE, "en").toString();
|
||||||
if (currentLang != newLang)
|
if (currentLang != newLang)
|
||||||
SetLanguage(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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString error;
|
QString error;
|
||||||
if (!mTranslation->SetLanguage(index, error))
|
if (!mTranslation->SetLanguage(code, error))
|
||||||
{
|
{
|
||||||
QMessageBox msg(QMessageBox::Critical,
|
QMessageBox msg(QMessageBox::Critical,
|
||||||
tr("Cppcheck"),
|
tr("Cppcheck"),
|
||||||
|
|
|
@ -255,9 +255,9 @@ protected:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set current language
|
* @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.
|
* @brief Event coming when application is about to close.
|
||||||
|
|
|
@ -91,13 +91,17 @@ SettingsDialog::~SettingsDialog()
|
||||||
|
|
||||||
void SettingsDialog::InitTranslationsList()
|
void SettingsDialog::InitTranslationsList()
|
||||||
{
|
{
|
||||||
QStringList languages = mTranslator->GetNames();
|
const QString current = mTranslator->GetCurrentLanguage();
|
||||||
foreach(const QString lang, languages)
|
QList<TranslationInfo> 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
|
Qt::CheckState SettingsDialog::BoolToCheckState(bool yes) const
|
||||||
|
@ -148,7 +152,10 @@ void SettingsDialog::SaveSettingValues()
|
||||||
SaveCheckboxValue(mUI.mShowDebugWarnings, SETTINGS_SHOW_DEBUG_WARNINGS);
|
SaveCheckboxValue(mUI.mShowDebugWarnings, SETTINGS_SHOW_DEBUG_WARNINGS);
|
||||||
SaveCheckboxValue(mUI.mInlineSuppressions, SETTINGS_INLINE_SUPPRESSIONS);
|
SaveCheckboxValue(mUI.mInlineSuppressions, SETTINGS_INLINE_SUPPRESSIONS);
|
||||||
mSettings->setValue(SETTINGS_GLOBAL_INCLUDE_PATHS, mUI.mEditIncludePaths->text());
|
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)
|
void SettingsDialog::SaveCheckboxValue(QCheckBox *box, const QString &name)
|
||||||
|
|
|
@ -201,6 +201,7 @@ protected:
|
||||||
*/
|
*/
|
||||||
Ui::Settings mUI;
|
Ui::Settings mUI;
|
||||||
private:
|
private:
|
||||||
|
static const int LangCodeRole = Qt::UserRole;
|
||||||
};
|
};
|
||||||
/// @}
|
/// @}
|
||||||
#endif // SETTINGSDIALOG_H
|
#endif // SETTINGSDIALOG_H
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
|
|
||||||
TranslationHandler::TranslationHandler(QObject *parent) :
|
TranslationHandler::TranslationHandler(QObject *parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
mCurrentLanguage(-1),
|
|
||||||
mTranslator(new QTranslator(this))
|
mTranslator(new QTranslator(this))
|
||||||
{
|
{
|
||||||
// Add our available languages
|
// Add our available languages
|
||||||
|
@ -66,10 +65,10 @@ const QStringList TranslationHandler::GetNames() const
|
||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TranslationHandler::SetLanguage(const int index, QString &error)
|
bool TranslationHandler::SetLanguage(const QString &code, QString &error)
|
||||||
{
|
{
|
||||||
//If English is the language
|
//If English is the language
|
||||||
if (index == 2)
|
if (code == "en")
|
||||||
{
|
{
|
||||||
//Just remove all extra translators
|
//Just remove all extra translators
|
||||||
if (mTranslator)
|
if (mTranslator)
|
||||||
|
@ -77,12 +76,13 @@ bool TranslationHandler::SetLanguage(const int index, QString &error)
|
||||||
qApp->removeTranslator(mTranslator);
|
qApp->removeTranslator(mTranslator);
|
||||||
}
|
}
|
||||||
|
|
||||||
mCurrentLanguage = index;
|
mCurrentLanguage = code;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Make sure the translator is otherwise valid
|
//Make sure the translator is otherwise valid
|
||||||
if (index >= mTranslations.size())
|
int index = GetLanguageIndexByCode(code);
|
||||||
|
if (index == -1)
|
||||||
{
|
{
|
||||||
error = QObject::tr("Incorrect language specified!");
|
error = QObject::tr("Incorrect language specified!");
|
||||||
return false;
|
return false;
|
||||||
|
@ -108,17 +108,17 @@ bool TranslationHandler::SetLanguage(const int index, QString &error)
|
||||||
|
|
||||||
qApp->installTranslator(mTranslator);
|
qApp->installTranslator(mTranslator);
|
||||||
|
|
||||||
mCurrentLanguage = index;
|
mCurrentLanguage = code;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int TranslationHandler::GetCurrentLanguage() const
|
QString TranslationHandler::GetCurrentLanguage() const
|
||||||
{
|
{
|
||||||
return mCurrentLanguage;
|
return mCurrentLanguage;
|
||||||
}
|
}
|
||||||
|
|
||||||
int TranslationHandler::SuggestLanguage() const
|
QString TranslationHandler::SuggestLanguage() const
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
Get language from system locale's name
|
Get language from system locale's name
|
||||||
|
@ -129,29 +129,16 @@ int TranslationHandler::SuggestLanguage() const
|
||||||
QString language = QLocale::system().name().left(2);
|
QString language = QLocale::system().name().left(2);
|
||||||
//qDebug()<<"Your language is"<<language;
|
//qDebug()<<"Your language is"<<language;
|
||||||
|
|
||||||
//catenate that to the default language filename
|
|
||||||
QString file = QString("cppcheck_%1").arg(language);
|
|
||||||
//qDebug()<<"Language file could be"<<file;
|
|
||||||
|
|
||||||
|
|
||||||
//And see if we can find it from our list of language files
|
//And see if we can find it from our list of language files
|
||||||
int index = 0;
|
int index = GetLanguageIndexByCode(language);
|
||||||
for (int i = 0; i < mTranslations.size(); i++)
|
|
||||||
{
|
|
||||||
if (mTranslations[i].mFilename == file)
|
|
||||||
{
|
|
||||||
index = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//If nothing found, return English
|
//If nothing found, return English
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
{
|
{
|
||||||
return 0;
|
return "en";
|
||||||
}
|
}
|
||||||
|
|
||||||
return index;
|
return language;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TranslationHandler::AddTranslation(const char *name, const char *filename)
|
void TranslationHandler::AddTranslation(const char *name, const char *filename)
|
||||||
|
@ -159,5 +146,20 @@ void TranslationHandler::AddTranslation(const char *name, const char *filename)
|
||||||
TranslationInfo info;
|
TranslationInfo info;
|
||||||
info.mName = name;
|
info.mName = name;
|
||||||
info.mFilename = filename;
|
info.mFilename = filename;
|
||||||
|
info.mCode = QString(filename).right(2);
|
||||||
mTranslations.append(info);
|
mTranslations.append(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int TranslationHandler::GetLanguageIndexByCode(const QString &code) const
|
||||||
|
{
|
||||||
|
int index = -1;
|
||||||
|
for (int i = 0; i < mTranslations.size(); i++)
|
||||||
|
{
|
||||||
|
if (mTranslations[i].mCode == code)
|
||||||
|
{
|
||||||
|
index = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ struct TranslationInfo
|
||||||
{
|
{
|
||||||
QString mName;
|
QString mName;
|
||||||
QString mFilename;
|
QString mFilename;
|
||||||
|
QString mCode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,13 +42,18 @@ public:
|
||||||
TranslationHandler(QObject *parent);
|
TranslationHandler(QObject *parent);
|
||||||
virtual ~TranslationHandler();
|
virtual ~TranslationHandler();
|
||||||
const QStringList GetNames() const;
|
const QStringList GetNames() const;
|
||||||
bool SetLanguage(const int index, QString &error);
|
QList<TranslationInfo> GetTranslations() const
|
||||||
int GetCurrentLanguage() const;
|
{
|
||||||
int SuggestLanguage() const;
|
return mTranslations;
|
||||||
|
}
|
||||||
|
bool SetLanguage(const QString &code, QString &error);
|
||||||
|
QString GetCurrentLanguage() const;
|
||||||
|
QString SuggestLanguage() const;
|
||||||
protected:
|
protected:
|
||||||
void AddTranslation(const char *name, const char *filename);
|
void AddTranslation(const char *name, const char *filename);
|
||||||
|
int GetLanguageIndexByCode(const QString &code) const;
|
||||||
|
|
||||||
int mCurrentLanguage;
|
QString mCurrentLanguage;
|
||||||
QList<TranslationInfo> mTranslations;
|
QList<TranslationInfo> mTranslations;
|
||||||
QTranslator *mTranslator;
|
QTranslator *mTranslator;
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue