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"
2018-01-01 19:23:40 +00:00
# include "ui_preferences_language.h"
2018-06-03 20:15:19 +00:00
# include "core/qthelper.h"
2018-08-15 09:50:53 +00:00
# include "core/settings/qPrefLanguage.h"
2015-09-17 18:58:26 +00:00
2020-12-28 13:30:57 +00:00
# include <QDir>
2015-09-17 18:58:26 +00:00
# include <QMessageBox>
2015-09-17 21:08:24 +00:00
# include <QSortFilterProxyModel>
2023-05-23 06:45:32 +00:00
# include <QCompleter>
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
2019-12-09 18:58:20 +00:00
PreferencesLanguage : : PreferencesLanguage ( ) : AbstractPreferencesWidget ( tr ( " Language " ) , QIcon ( " :preferences-desktop-locale-icon " ) , 1 )
2015-09-17 18:58:26 +00:00
{
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 ) ;
2023-05-23 06:45:32 +00:00
ui - > dateFormatEntry - > completer ( ) - > setCaseSensitivity ( Qt : : CaseSensitive ) ;
2024-03-09 08:22:34 +00:00
connect ( ui - > dateFormatEntry , & QComboBox : : currentTextChanged ,
this , & PreferencesLanguage : : dateFormatChanged ) ;
2017-11-10 15:28:09 +00:00
ui - > timeFormatEntry - > addItem ( " hh:mm " ) ;
ui - > timeFormatEntry - > addItem ( " h:mm AP " ) ;
ui - > timeFormatEntry - > addItem ( " hh:mm AP " ) ;
2023-05-23 06:45:32 +00:00
ui - > timeFormatEntry - > completer ( ) - > setCaseSensitivity ( Qt : : CaseSensitive ) ;
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 ) ;
2017-11-10 15:28:09 +00:00
ui - > timeFormatEntry - > setCurrentText ( 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
}
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 ( ) ;
2018-08-15 09:50:53 +00:00
qPrefLanguage : : set_language ( currentText ) ;
qPrefLanguage : : set_lang_locale ( currentLocale ) ;
qPrefLanguage : : set_use_system_language ( ui - > languageSystemDefault - > isChecked ( ) ) ;
qPrefLanguage : : set_time_format_override ( ! ui - > timeFormatSystemDefault - > isChecked ( ) ) ;
qPrefLanguage : : set_date_format_override ( ! ui - > dateFormatSystemDefault - > isChecked ( ) ) ;
qPrefLanguage : : set_time_format ( ui - > timeFormatEntry - > currentText ( ) ) ;
qPrefLanguage : : set_date_format ( ui - > dateFormatEntry - > currentText ( ) ) ;
qPrefLanguage : : set_date_format_short ( ui - > shortDateFormatEntry - > text ( ) ) ;
2020-03-22 15:43:21 +00:00
initUiLanguage ( ) ;
2016-10-22 21:38:23 +00:00
2020-05-22 07:39:48 +00:00
// When switching to system defaults, initUiLanguage() will reset the date and time formats.
// Therefore, refresh the UI fields to give the user a visual feedback of the new formats.
refreshSettings ( ) ;
2023-05-23 06:45:32 +00:00
QString qDateTimeWeb = tr ( " These will be used as is. This might not be what you intended. To avoid this warning wrap the literal parts in quotes ('). \n See https://doc.qt.io/archives/qt-4.8/qdatetime.html#fromString " ) ;
QRegularExpression timeStandardFormat ( " ^([hHmszaApP \\ s: \\ .]|'[^']*') * $ " ) ;
if ( ! timeStandardFormat . match ( ui - > timeFormatEntry - > currentText ( ) ) . hasMatch ( ) )
2016-10-22 21:38:23 +00:00
QMessageBox : : warning ( this , tr ( " Literal characters " ) ,
2023-05-23 06:45:32 +00:00
tr ( " Non-standard character(s) in time format. \n " ) + qDateTimeWeb ) ;
2016-10-22 21:38:23 +00:00
2023-05-23 06:45:32 +00:00
QRegularExpression dateStandardFormat ( " ^([dMy \\ s/ \\ ., \\ -]|'[^']*') * $ " ) ;
if ( ! dateStandardFormat . match ( ui - > dateFormatEntry - > currentText ( ) ) . hasMatch ( ) | |
! dateStandardFormat . match ( ui - > shortDateFormatEntry - > text ( ) ) . hasMatch ( ) )
2016-10-22 21:38:23 +00:00
QMessageBox : : warning ( this , tr ( " Literal characters " ) ,
2023-05-23 06:45:32 +00:00
tr ( " Non-standard character(s) in date format. \n " ) + qDateTimeWeb ) ;
2015-10-30 20:14:06 +00:00
}