From 4e263bae987bcc343497496bcdc962c292977b6a Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Fri, 6 Dec 2013 14:29:38 -0200 Subject: Added a language preference to the Settings. When the user first opens the application the default language is selected; this can be changed to a hardcoded one by going to system preferences and choosing the one you want. Restart required. Fixes #136 [Dirk Hohndel: whitespace fixes, removed qDebug() call, rephrased the message displayed prompting the user to restart.] Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- qt-ui/models.h | 14 +++++++++++++- qt-ui/preferences.cpp | 19 +++++++++++++++++++ qt-ui/preferences.ui | 45 +++++++++++++++++++++++++++++++-------------- 4 files changed, 113 insertions(+), 16 deletions(-) (limited to 'qt-ui') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 9950ed339..985d213f6 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -16,6 +16,8 @@ #include #include +#include +#include #include #include #include @@ -318,7 +320,7 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in int CylindersModel::rowCount(const QModelIndex& parent) const { - return rows; + return rows; } void CylindersModel::add() @@ -1750,3 +1752,50 @@ QVariant GasSelectionModel::data(const QModelIndex& index, int role) const } return QStringListModel::data(index, role); } + +// Language Model, The Model to populate the list of possible Languages. + +LanguageModel* LanguageModel::instance() +{ + static LanguageModel *self = new LanguageModel(); + QLocale l; + return self; +} + +LanguageModel::LanguageModel(QObject* parent): QAbstractListModel(parent) +{ + QSettings s; + QDir d; + d.setCurrent( getSubsurfaceDataPath("translations") ); + QStringList result = d.entryList(); + Q_FOREACH(const QString& s, result){ + if ( !s.endsWith(".qm") ){ + continue; + } + languages.push_back(s); + } +} + +QVariant LanguageModel::data(const QModelIndex& index, int role) const +{ + QLocale loc; + if(!index.isValid()) + return QVariant(); + switch(role){ + case Qt::DisplayRole:{ + QString currentString = languages.at(index.row()); + QLocale l( currentString.remove("subsurface_")); + return l.countryToString(l.country()); + }break; + case Qt::UserRole:{ + QString currentString = languages.at(index.row()); + return currentString.remove("subsurface_"); + }break; + } + return QVariant(); +} + +int LanguageModel::rowCount(const QModelIndex& parent) const +{ + return languages.count(); +} diff --git a/qt-ui/models.h b/qt-ui/models.h index 9dd7b2bf6..baa7b32c4 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -234,7 +234,7 @@ private: class YearlyStatisticsModel : public TreeModel { Q_OBJECT public: - enum { YEAR,DIVES,TOTAL_TIME,AVERAGE_TIME,SHORTEST_TIME,LONGEST_TIME,AVG_DEPTH,MIN_DEPTH, + enum { YEAR,DIVES,TOTAL_TIME,AVERAGE_TIME,SHORTEST_TIME,LONGEST_TIME,AVG_DEPTH,MIN_DEPTH, MAX_DEPTH,AVG_SAC,MIN_SAC,MAX_SAC,AVG_TEMP,MIN_TEMP,MAX_TEMP,COLUMNS}; virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; @@ -312,4 +312,16 @@ public slots: void repopulate(); }; + +class LanguageModel : public QAbstractListModel { + Q_OBJECT +public: + static LanguageModel* instance(); + virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; + virtual int rowCount(const QModelIndex& parent = QModelIndex()) const; +private: + LanguageModel(QObject* parent = 0); + + QStringList languages; +}; #endif diff --git a/qt-ui/preferences.cpp b/qt-ui/preferences.cpp index f8b24da42..2155a710d 100644 --- a/qt-ui/preferences.cpp +++ b/qt-ui/preferences.cpp @@ -3,11 +3,13 @@ #include #include #include +#include PreferencesDialog* PreferencesDialog::instance() { static PreferencesDialog *dialog = new PreferencesDialog(mainWindow()); dialog->setAttribute(Qt::WA_QuitOnClose, false); + LanguageModel::instance(); return dialog; } @@ -107,6 +109,15 @@ void PreferencesDialog::setUiFromPrefs() ui.show_sac->setChecked(prefs.show_sac); ui.vertical_speed_minutes->setChecked(prefs.units.vertical_speed_time == units::MINUTES); ui.vertical_speed_seconds->setChecked(prefs.units.vertical_speed_time == units::SECONDS); + + ui.languageView->setModel( LanguageModel::instance() ); + + QSettings s; + s.beginGroup("Language"); + QAbstractItemModel *m = ui.languageView->model(); + QModelIndexList languages = m->match( m->index(0,0), Qt::DisplayRole, s.value("UiLanguage").toString()); + if (languages.count()) + ui.languageView->setCurrentIndex(languages.first()); } void PreferencesDialog::restorePrefs() @@ -174,6 +185,14 @@ void PreferencesDialog::syncSettings() s.endGroup(); s.sync(); + QLocale loc; + s.beginGroup("Language"); + if (s.value("UiLanguage").toString() != ui.languageView->currentIndex().data(Qt::UserRole)){ + QMessageBox::warning(mainWindow(), tr("Restart required"), + tr("To correctly load a new language you must restart Subsurface.")); + } + s.setValue("UiLanguage", ui.languageView->currentIndex().data(Qt::UserRole)); + emit settingsChanged(); } diff --git a/qt-ui/preferences.ui b/qt-ui/preferences.ui index 7b6f3e596..73632dc72 100644 --- a/qt-ui/preferences.ui +++ b/qt-ui/preferences.ui @@ -110,6 +110,16 @@ + + + Language + + + + :/advanced + + + @@ -121,7 +131,7 @@ - 1 + 0 @@ -301,7 +311,7 @@ - meter + meter buttonGroup @@ -311,7 +321,7 @@ - feet + feet buttonGroup @@ -328,7 +338,7 @@ - bar + bar buttonGroup_2 @@ -338,7 +348,7 @@ - psi + psi buttonGroup_2 @@ -355,7 +365,7 @@ - liter + liter buttonGroup_3 @@ -365,7 +375,7 @@ - cu ft + cu ft buttonGroup_3 @@ -382,7 +392,7 @@ - celsius + celsius buttonGroup_4 @@ -392,7 +402,7 @@ - fahrenheit + fahrenheit buttonGroup_4 @@ -409,7 +419,7 @@ - kg + kg buttonGroup_5 @@ -497,7 +507,7 @@ - pO₂ + pO₂ @@ -538,7 +548,7 @@ - pN₂ + pN₂ @@ -643,7 +653,7 @@ false - max ppO₂ + max ppO₂ @@ -817,6 +827,13 @@ + + + + + + + @@ -1098,9 +1115,9 @@ + - -- cgit v1.2.3-70-g09d2