2017-04-27 18:26:05 +00:00
// SPDX-License-Identifier: GPL-2.0
2015-09-17 18:58:26 +00:00
# include "preferences_language.h"
# include "ui_prefs_language.h"
2016-04-05 05:02:03 +00:00
# include "core/helpers.h"
2016-08-10 15:48:19 +00:00
# include "core/subsurface-qt/SettingsObjectWrapper.h"
2015-09-17 18:58:26 +00:00
# include <QApplication>
# include <QMessageBox>
2015-09-17 21:08:24 +00:00
# include <QSortFilterProxyModel>
2015-09-17 18:58:26 +00:00
2015-09-17 21:08:24 +00:00
# include "qt-models/models.h"
2015-09-17 18:58:26 +00:00
PreferencesLanguage : : PreferencesLanguage ( ) : AbstractPreferencesWidget ( tr ( " Language " ) , QIcon ( " :/language " ) , 4 )
{
ui = new Ui : : PreferencesLanguage ( ) ;
ui - > setupUi ( this ) ;
2015-09-17 21:08:24 +00:00
QSortFilterProxyModel * filterModel = new QSortFilterProxyModel ( ) ;
filterModel - > setSourceModel ( LanguageModel : : instance ( ) ) ;
filterModel - > setFilterCaseSensitivity ( Qt : : CaseInsensitive ) ;
2015-10-30 20:14:06 +00:00
ui - > languageDropdown - > setModel ( filterModel ) ;
2015-09-17 21:08:24 +00:00
filterModel - > sort ( 0 ) ;
connect ( ui - > languageFilter , & QLineEdit : : textChanged ,
filterModel , & QSortFilterProxyModel : : setFilterFixedString ) ;
2017-11-10 14:52:12 +00:00
dateFormatShortMap . insert ( " MM/dd/yyyy " , " M/d/yy " ) ;
dateFormatShortMap . insert ( " dd.MM.yyyy " , " d.M.yy " ) ;
dateFormatShortMap . insert ( " yyyy-MM-dd " , " yy-M-d " ) ;
foreach ( QString format , dateFormatShortMap . keys ( ) )
ui - > dateFormatEntry - > addItem ( format ) ;
connect ( ui - > dateFormatEntry , SIGNAL ( currentIndexChanged ( const QString & ) ) ,
this , SLOT ( dateFormatChanged ( const QString & ) ) ) ;
2015-09-17 18:58:26 +00:00
}
PreferencesLanguage : : ~ PreferencesLanguage ( )
{
delete ui ;
}
2017-11-10 14:52:12 +00:00
void PreferencesLanguage : : dateFormatChanged ( const QString & text )
{
ui - > shortDateFormatEntry - > setText ( dateFormatShortMap . value ( text ) ) ;
}
2015-09-17 18:58:26 +00:00
void PreferencesLanguage : : refreshSettings ( )
{
2016-08-10 15:42:18 +00:00
ui - > languageSystemDefault - > setChecked ( prefs . locale . use_system_language ) ;
ui - > timeFormatSystemDefault - > setChecked ( ! prefs . time_format_override ) ;
ui - > dateFormatSystemDefault - > setChecked ( ! prefs . date_format_override ) ;
ui - > timeFormatEntry - > setText ( prefs . time_format ) ;
2017-11-10 14:52:12 +00:00
ui - > dateFormatEntry - > setCurrentText ( prefs . date_format ) ;
2016-08-10 15:42:18 +00:00
ui - > shortDateFormatEntry - > setText ( prefs . date_format_short ) ;
2015-10-30 20:14:06 +00:00
QAbstractItemModel * m = ui - > languageDropdown - > model ( ) ;
2016-09-20 19:42:59 +00:00
QModelIndexList languages = m - > match ( m - > index ( 0 , 0 ) , Qt : : UserRole , QString ( prefs . locale . lang_locale ) . replace ( " - " , " _ " ) ) ;
2015-09-17 18:58:26 +00:00
if ( languages . count ( ) )
2015-10-30 20:14:06 +00:00
ui - > languageDropdown - > setCurrentIndex ( languages . first ( ) . row ( ) ) ;
2015-09-17 18:58:26 +00:00
}
2016-08-28 01:13:33 +00:00
# include <QDebug>
2015-09-17 18:58:26 +00:00
void PreferencesLanguage : : syncSettings ( )
{
2016-08-10 15:48:19 +00:00
bool useSystemLang = prefs . locale . use_system_language ;
2016-08-28 01:13:33 +00:00
QString currentText = ui - > languageDropdown - > currentText ( ) ;
2016-08-10 15:48:19 +00:00
2015-09-17 18:58:26 +00:00
if ( useSystemLang ! = ui - > languageSystemDefault - > isChecked ( ) | |
2016-08-10 15:48:19 +00:00
( ! useSystemLang & & currentText ! = prefs . locale . language ) ) {
2017-09-16 08:38:21 +00:00
// remove the googlemaps cache folder on language change
2017-09-02 12:28:40 +00:00
QDir googlecachedir ( QString ( system_default_directory ( ) ) . append ( " /googlemaps " ) ) ;
googlecachedir . removeRecursively ( ) ;
2015-09-17 18:58:26 +00:00
QMessageBox : : warning ( this , tr ( " Restart required " ) ,
tr ( " To correctly load a new language you must restart Subsurface. " ) ) ;
}
2016-09-20 19:44:26 +00:00
QAbstractItemModel * m = ui - > languageDropdown - > model ( ) ;
QModelIndexList languages = m - > match ( m - > index ( 0 , 0 ) , Qt : : DisplayRole , currentText ) ;
QString currentLocale ;
if ( languages . count ( ) )
currentLocale = m - > data ( languages . first ( ) , Qt : : UserRole ) . toString ( ) ;
2016-08-28 01:13:33 +00:00
auto lang = SettingsObjectWrapper : : instance ( ) - > language_settings ;
2016-08-10 15:48:19 +00:00
lang - > setLanguage ( currentText ) ;
2016-09-20 19:44:26 +00:00
lang - > setLangLocale ( currentLocale ) ;
2016-08-10 15:48:19 +00:00
lang - > setUseSystemLanguage ( ui - > languageSystemDefault - > isChecked ( ) ) ;
lang - > setTimeFormatOverride ( ! ui - > timeFormatSystemDefault - > isChecked ( ) ) ;
lang - > setDateFormatOverride ( ! ui - > dateFormatSystemDefault - > isChecked ( ) ) ;
lang - > setTimeFormat ( ui - > timeFormatEntry - > text ( ) ) ;
2017-11-10 14:52:12 +00:00
lang - > setDateFormat ( ui - > dateFormatEntry - > currentText ( ) ) ;
2016-08-10 15:48:19 +00:00
lang - > setDateFormatShort ( ui - > shortDateFormatEntry - > text ( ) ) ;
2015-11-02 20:50:17 +00:00
uiLanguage ( NULL ) ;
2016-10-22 21:38:23 +00:00
QRegExp tfillegalchars ( " [^hHmszaApPt \\ s:; \\ .,] " ) ;
if ( tfillegalchars . indexIn ( ui - > timeFormatEntry - > text ( ) ) > = 0 )
QMessageBox : : warning ( this , tr ( " Literal characters " ) ,
tr ( " Non-special character(s) in time format. \n These will be used as is. This might not be what you intended. \n See http://doc.qt.io/qt-5/qdatetime.html#toString " ) ) ;
2017-11-10 14:52:12 +00:00
QRegExp dfillegalchars ( " [^dMy/ \\ s:; \\ ., \\ -] " ) ;
if ( dfillegalchars . indexIn ( ui - > dateFormatEntry - > currentText ( ) ) > = 0 | |
2016-10-22 21:38:23 +00:00
dfillegalchars . indexIn ( ui - > shortDateFormatEntry - > text ( ) ) > = 0 )
QMessageBox : : warning ( this , tr ( " Literal characters " ) ,
tr ( " Non-special character(s) in time format. \n These will be used as is. This might not be what you intended. \n See http://doc.qt.io/qt-5/qdatetime.html#toString " ) ) ;
2015-10-30 20:14:06 +00:00
}