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 <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2013-12-06 14:29:38 -02:00 committed by Dirk Hohndel
parent bfe5ccda1c
commit 4e263bae98
5 changed files with 118 additions and 18 deletions

View file

@ -87,8 +87,12 @@ void init_ui(int *argcp, char ***argvp)
QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath()); QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath());
xslt_path = strdup(getSubsurfaceDataPath("xslt").toAscii().data()); xslt_path = strdup(getSubsurfaceDataPath("xslt").toAscii().data());
QLocale loc; QSettings s;
s.beginGroup("Language");
QLocale loc(s.value("UiLanguage", QLocale().uiLanguages().first()).toString());
QString uiLang = loc.uiLanguages().first(); QString uiLang = loc.uiLanguages().first();
s.endGroup();
// there's a stupid Qt bug on MacOS where uiLanguages doesn't give us the country info // there's a stupid Qt bug on MacOS where uiLanguages doesn't give us the country info
if (!uiLang.contains('-') && uiLang != loc.bcp47Name()) { if (!uiLang.contains('-') && uiLang != loc.bcp47Name()) {
QLocale loc2(loc.bcp47Name()); QLocale loc2(loc.bcp47Name());
@ -117,7 +121,6 @@ void init_ui(int *argcp, char ***argvp)
} }
} }
QSettings s;
s.beginGroup("DiveComputer"); s.beginGroup("DiveComputer");
default_dive_computer_vendor = getSetting(s, "dive_computer_vendor"); default_dive_computer_vendor = getSetting(s, "dive_computer_vendor");
default_dive_computer_product = getSetting(s,"dive_computer_product"); default_dive_computer_product = getSetting(s,"dive_computer_product");

View file

@ -16,6 +16,8 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QDir>
#include <QSettings>
#include <QColor> #include <QColor>
#include <QBrush> #include <QBrush>
#include <QFont> #include <QFont>
@ -318,7 +320,7 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in
int CylindersModel::rowCount(const QModelIndex& parent) const int CylindersModel::rowCount(const QModelIndex& parent) const
{ {
return rows; return rows;
} }
void CylindersModel::add() void CylindersModel::add()
@ -1750,3 +1752,50 @@ QVariant GasSelectionModel::data(const QModelIndex& index, int role) const
} }
return QStringListModel::data(index, role); 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();
}

View file

@ -234,7 +234,7 @@ private:
class YearlyStatisticsModel : public TreeModel { class YearlyStatisticsModel : public TreeModel {
Q_OBJECT Q_OBJECT
public: 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}; 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; virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
@ -312,4 +312,16 @@ public slots:
void repopulate(); 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 #endif

View file

@ -3,11 +3,13 @@
#include <QSettings> #include <QSettings>
#include <QDebug> #include <QDebug>
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox>
PreferencesDialog* PreferencesDialog::instance() PreferencesDialog* PreferencesDialog::instance()
{ {
static PreferencesDialog *dialog = new PreferencesDialog(mainWindow()); static PreferencesDialog *dialog = new PreferencesDialog(mainWindow());
dialog->setAttribute(Qt::WA_QuitOnClose, false); dialog->setAttribute(Qt::WA_QuitOnClose, false);
LanguageModel::instance();
return dialog; return dialog;
} }
@ -107,6 +109,15 @@ void PreferencesDialog::setUiFromPrefs()
ui.show_sac->setChecked(prefs.show_sac); ui.show_sac->setChecked(prefs.show_sac);
ui.vertical_speed_minutes->setChecked(prefs.units.vertical_speed_time == units::MINUTES); 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.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() void PreferencesDialog::restorePrefs()
@ -174,6 +185,14 @@ void PreferencesDialog::syncSettings()
s.endGroup(); s.endGroup();
s.sync(); 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(); emit settingsChanged();
} }

View file

@ -110,6 +110,16 @@
</iconset> </iconset>
</property> </property>
</item> </item>
<item>
<property name="text">
<string>Language</string>
</property>
<property name="icon">
<iconset>
<normalon>:/advanced</normalon>
</iconset>
</property>
</item>
</widget> </widget>
</item> </item>
<item> <item>
@ -121,7 +131,7 @@
</sizepolicy> </sizepolicy>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>1</number> <number>0</number>
</property> </property>
<widget class="QWidget" name="page_2"> <widget class="QWidget" name="page_2">
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
@ -301,7 +311,7 @@
<item row="0" column="1"> <item row="0" column="1">
<widget class="QRadioButton" name="meter"> <widget class="QRadioButton" name="meter">
<property name="text"> <property name="text">
<string>meter</string> <string>meter</string>
</property> </property>
<attribute name="buttonGroup"> <attribute name="buttonGroup">
<string notr="true">buttonGroup</string> <string notr="true">buttonGroup</string>
@ -311,7 +321,7 @@
<item row="0" column="2"> <item row="0" column="2">
<widget class="QRadioButton" name="feet"> <widget class="QRadioButton" name="feet">
<property name="text"> <property name="text">
<string>feet</string> <string>feet</string>
</property> </property>
<attribute name="buttonGroup"> <attribute name="buttonGroup">
<string notr="true">buttonGroup</string> <string notr="true">buttonGroup</string>
@ -328,7 +338,7 @@
<item row="1" column="1"> <item row="1" column="1">
<widget class="QRadioButton" name="bar"> <widget class="QRadioButton" name="bar">
<property name="text"> <property name="text">
<string>bar</string> <string>bar</string>
</property> </property>
<attribute name="buttonGroup"> <attribute name="buttonGroup">
<string notr="true">buttonGroup_2</string> <string notr="true">buttonGroup_2</string>
@ -338,7 +348,7 @@
<item row="1" column="2"> <item row="1" column="2">
<widget class="QRadioButton" name="psi"> <widget class="QRadioButton" name="psi">
<property name="text"> <property name="text">
<string>psi</string> <string>psi</string>
</property> </property>
<attribute name="buttonGroup"> <attribute name="buttonGroup">
<string notr="true">buttonGroup_2</string> <string notr="true">buttonGroup_2</string>
@ -355,7 +365,7 @@
<item row="2" column="1"> <item row="2" column="1">
<widget class="QRadioButton" name="liter"> <widget class="QRadioButton" name="liter">
<property name="text"> <property name="text">
<string>liter</string> <string>liter</string>
</property> </property>
<attribute name="buttonGroup"> <attribute name="buttonGroup">
<string notr="true">buttonGroup_3</string> <string notr="true">buttonGroup_3</string>
@ -365,7 +375,7 @@
<item row="2" column="2"> <item row="2" column="2">
<widget class="QRadioButton" name="cuft"> <widget class="QRadioButton" name="cuft">
<property name="text"> <property name="text">
<string>cu ft</string> <string>cu ft</string>
</property> </property>
<attribute name="buttonGroup"> <attribute name="buttonGroup">
<string notr="true">buttonGroup_3</string> <string notr="true">buttonGroup_3</string>
@ -382,7 +392,7 @@
<item row="3" column="1"> <item row="3" column="1">
<widget class="QRadioButton" name="celsius"> <widget class="QRadioButton" name="celsius">
<property name="text"> <property name="text">
<string>celsius</string> <string>celsius</string>
</property> </property>
<attribute name="buttonGroup"> <attribute name="buttonGroup">
<string notr="true">buttonGroup_4</string> <string notr="true">buttonGroup_4</string>
@ -392,7 +402,7 @@
<item row="3" column="2"> <item row="3" column="2">
<widget class="QRadioButton" name="fahrenheit"> <widget class="QRadioButton" name="fahrenheit">
<property name="text"> <property name="text">
<string>fahrenheit</string> <string>fahrenheit</string>
</property> </property>
<attribute name="buttonGroup"> <attribute name="buttonGroup">
<string notr="true">buttonGroup_4</string> <string notr="true">buttonGroup_4</string>
@ -409,7 +419,7 @@
<item row="4" column="1"> <item row="4" column="1">
<widget class="QRadioButton" name="kgs"> <widget class="QRadioButton" name="kgs">
<property name="text"> <property name="text">
<string>kg</string> <string>kg</string>
</property> </property>
<attribute name="buttonGroup"> <attribute name="buttonGroup">
<string notr="true">buttonGroup_5</string> <string notr="true">buttonGroup_5</string>
@ -497,7 +507,7 @@
<item> <item>
<widget class="QCheckBox" name="po2"> <widget class="QCheckBox" name="po2">
<property name="text"> <property name="text">
<string>pO₂</string> <string>pO₂</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -538,7 +548,7 @@
<item> <item>
<widget class="QCheckBox" name="pn2"> <widget class="QCheckBox" name="pn2">
<property name="text"> <property name="text">
<string>pN₂</string> <string>pN₂</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -643,7 +653,7 @@
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="text"> <property name="text">
<string>max ppO₂</string> <string>max ppO₂</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -817,6 +827,13 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="page_4">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QListView" name="languageView"/>
</item>
</layout>
</widget>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -1098,9 +1115,9 @@
<buttongroup name="verticalSpeed"/> <buttongroup name="verticalSpeed"/>
<buttongroup name="buttonGroup_2"/> <buttongroup name="buttonGroup_2"/>
<buttongroup name="buttonGroup_3"/> <buttongroup name="buttonGroup_3"/>
<buttongroup name="buttonGroup"/>
<buttongroup name="buttonGroup_4"/> <buttongroup name="buttonGroup_4"/>
<buttongroup name="buttonGroup_5"/> <buttongroup name="buttonGroup_5"/>
<buttongroup name="buttonGroup_6"/> <buttongroup name="buttonGroup_6"/>
<buttongroup name="buttonGroup"/>
</buttongroups> </buttongroups>
</ui> </ui>