Merge pull request #46419 from Joonalai/improve-format-datetimes

This commit is contained in:
Matthias Kuhn 2021-12-09 13:41:20 +01:00 committed by GitHub
commit 28fac71bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 22 deletions

View File

@ -39,11 +39,6 @@ Default constructor of field formatter for a date time field.
virtual QString representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const; virtual QString representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const;
static void applyLocaleChange();
%Docstring
Adjusts the date time formats according to locale.
%End
static QString defaultFormat( QVariant::Type type ); static QString defaultFormat( QVariant::Type type );
%Docstring %Docstring
Gets the default format in function of the type. Gets the default format in function of the type.
@ -53,6 +48,7 @@ The type is expected to be one of
- QVariant.Date - QVariant.Date
- QVariant.Time - QVariant.Time
%End %End
}; };
/************************************************************************ /************************************************************************

View File

@ -441,6 +441,14 @@ Returns the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "ex
Returns the QGIS locale. Returns the QGIS locale.
.. versionadded:: 3.0 .. versionadded:: 3.0
%End
static void setLocale( const QLocale &locale );
%Docstring
Sets the QGIS locale - used mainly by 3rd party apps and tests.
In QGIS this is internally triggered by the application in startup.
.. versionadded:: 3.22.2
%End %End
static QString userThemesFolder(); static QString userThemesFolder();
@ -1080,6 +1088,15 @@ In order to register translatable strings, connect to this signal and register t
.. versionadded:: 3.4 .. versionadded:: 3.4
%End %End
void localeChanged();
%Docstring
Emitted when project locale has been changed.
.. versionadded:: 3.22.2
%End
}; };

View File

@ -113,7 +113,6 @@ typedef SInt32 SRefCon;
#include "qgsuserprofilemanager.h" #include "qgsuserprofilemanager.h"
#include "qgsuserprofile.h" #include "qgsuserprofile.h"
#include "qgsdatetimefieldformatter.h"
#ifdef HAVE_OPENCL #ifdef HAVE_OPENCL
#include "qgsopenclutils.h" #include "qgsopenclutils.h"
@ -1032,14 +1031,14 @@ int main( int argc, char *argv[] )
} }
QLocale::setDefault( currentLocale ); QLocale::setDefault( currentLocale );
// Date time settings
QgsDateTimeFieldFormatter::applyLocaleChange();
QgsApplication::setTranslation( translationCode ); QgsApplication::setTranslation( translationCode );
} }
QgsApplication myApp( argc, argv, myUseGuiFlag, QString(), QStringLiteral( "desktop" ) ); QgsApplication myApp( argc, argv, myUseGuiFlag, QString(), QStringLiteral( "desktop" ) );
// Set locale to emit QgsApplication's localeChanged signal
myApp.setLocale( QLocale() );
//write the log messages written before creating QgsApplication //write the log messages written before creating QgsApplication
for ( const QString &preApplicationLogMessage : std::as_const( preApplicationLogMessages ) ) for ( const QString &preApplicationLogMessage : std::as_const( preApplicationLogMessages ) )
QgsMessageLog::logMessage( preApplicationLogMessage ); QgsMessageLog::logMessage( preApplicationLogMessage );

View File

@ -98,13 +98,6 @@ QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fi
return result; return result;
} }
void QgsDateTimeFieldFormatter::applyLocaleChange()
{
QString dateFormat = QLocale().dateFormat( QLocale::FormatType::ShortFormat );
QgsDateTimeFieldFormatter::DATETIME_FORMAT = QString( "%1 %2" ).arg( dateFormat, QgsDateTimeFieldFormatter::TIME_FORMAT );
QgsDateTimeFieldFormatter::DATE_FORMAT = dateFormat;
}
QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type ) QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type )
{ {
switch ( type ) switch ( type )
@ -117,3 +110,10 @@ QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type )
return QgsDateTimeFieldFormatter::DATE_FORMAT; return QgsDateTimeFieldFormatter::DATE_FORMAT;
} }
} }
void QgsDateTimeFieldFormatter::applyLocaleChange()
{
QString dateFormat = QLocale().dateFormat( QLocale::FormatType::ShortFormat );
QgsDateTimeFieldFormatter::DATETIME_FORMAT = QString( "%1 %2" ).arg( dateFormat, QgsDateTimeFieldFormatter::TIME_FORMAT );
QgsDateTimeFieldFormatter::DATE_FORMAT = dateFormat;
}

View File

@ -46,9 +46,6 @@ class CORE_EXPORT QgsDateTimeFieldFormatter : public QgsFieldFormatter
QString representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const override; QString representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const override;
//! Adjusts the date time formats according to locale.
static void applyLocaleChange();
/** /**
* Gets the default format in function of the type. * Gets the default format in function of the type.
* The type is expected to be one of * The type is expected to be one of
@ -58,6 +55,13 @@ class CORE_EXPORT QgsDateTimeFieldFormatter : public QgsFieldFormatter
* - QVariant::Time * - QVariant::Time
*/ */
static QString defaultFormat( QVariant::Type type ); static QString defaultFormat( QVariant::Type type );
/**
* Adjusts the date time formats according to locale.
*
* \since QGIS 3.22.2
*/
static void applyLocaleChange(); SIP_SKIP;
}; };
#endif // QGSDATETIMEFIELDKIT_H #endif // QGSDATETIMEFIELDKIT_H

View File

@ -85,6 +85,7 @@
#include "layout/qgspagesizeregistry.h" #include "layout/qgspagesizeregistry.h"
#include "qgsrecentstylehandler.h" #include "qgsrecentstylehandler.h"
#include "qgsdatetimefieldformatter.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
#include <QDesktopWidget> #include <QDesktopWidget>
@ -230,6 +231,8 @@ QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const Q
mApplicationMembers = new ApplicationMembers(); mApplicationMembers = new ApplicationMembers();
*sProfilePath() = profileFolder; *sProfilePath() = profileFolder;
connect( instance(), &QgsApplication::localeChanged, &QgsDateTimeFieldFormatter::applyLocaleChange );
} }
void QgsApplication::init( QString profileFolder ) void QgsApplication::init( QString profileFolder )
@ -1311,6 +1314,12 @@ QString QgsApplication::locale()
} }
} }
void QgsApplication::setLocale( const QLocale &locale )
{
QLocale::setDefault( locale );
emit instance()->localeChanged();
}
QString QgsApplication::userThemesFolder() QString QgsApplication::userThemesFolder()
{ {
return qgisSettingsDirPath() + QStringLiteral( "/themes" ); return qgisSettingsDirPath() + QStringLiteral( "/themes" );

View File

@ -468,6 +468,14 @@ class CORE_EXPORT QgsApplication : public QApplication
*/ */
static QString locale(); static QString locale();
/**
* Sets the QGIS locale - used mainly by 3rd party apps and tests.
* In QGIS this is internally triggered by the application in startup.
*
* \since QGIS 3.22.2
*/
static void setLocale( const QLocale &locale );
//! Returns the path to user's themes folder //! Returns the path to user's themes folder
static QString userThemesFolder(); static QString userThemesFolder();
@ -1043,6 +1051,15 @@ class CORE_EXPORT QgsApplication : public QApplication
*/ */
void requestForTranslatableObjects( QgsTranslationContext *translationContext ); void requestForTranslatableObjects( QgsTranslationContext *translationContext );
/**
* Emitted when project locale has been changed.
*
* \since QGIS 3.22.2
*/
void localeChanged();
private: private:
static void copyPath( const QString &src, const QString &dst ); static void copyPath( const QString &src, const QString &dst );

View File

@ -24,6 +24,7 @@ from qgis.PyQt.QtCore import (
Qt Qt
) )
from qgis.core import ( from qgis.core import (
QgsApplication,
QgsFeature, QgsFeature,
QgsProject, QgsProject,
QgsRelation, QgsRelation,
@ -679,7 +680,7 @@ class TestQgsDateTimeFieldFormatter(unittest.TestCase):
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
"""Reset locale""" """Reset locale"""
QLocale.setDefault(QLocale(QLocale.English)) QgsApplication.setLocale(QLocale(QLocale.English))
def test_representValue(self): def test_representValue(self):
layer = QgsVectorLayer("point?field=datetime:datetime&field=date:date&field=time:time", layer = QgsVectorLayer("point?field=datetime:datetime&field=date:date&field=time:time",
@ -716,8 +717,7 @@ class TestQgsDateTimeFieldFormatter(unittest.TestCase):
} }
for locale, assertions in locale_assertions.items(): for locale, assertions in locale_assertions.items():
QLocale().setDefault(locale) QgsApplication.setLocale(locale)
QgsDateTimeFieldFormatter.applyLocaleChange()
field_formatter = QgsDateTimeFieldFormatter() field_formatter = QgsDateTimeFieldFormatter()
self.assertEqual(field_formatter.defaultFormat(QVariant.Date), assertions["date_format"], locale.name()) self.assertEqual(field_formatter.defaultFormat(QVariant.Date), assertions["date_format"], locale.name())