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;
static void applyLocaleChange();
%Docstring
Adjusts the date time formats according to locale.
%End
static QString defaultFormat( QVariant::Type type );
%Docstring
Gets the default format in function of the type.
@ -53,6 +48,7 @@ The type is expected to be one of
- QVariant.Date
- QVariant.Time
%End
};
/************************************************************************

View File

@ -441,6 +441,14 @@ Returns the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "ex
Returns the QGIS locale.
.. 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
static QString userThemesFolder();
@ -1080,6 +1088,15 @@ In order to register translatable strings, connect to this signal and register t
.. versionadded:: 3.4
%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 "qgsuserprofile.h"
#include "qgsdatetimefieldformatter.h"
#ifdef HAVE_OPENCL
#include "qgsopenclutils.h"
@ -1032,14 +1031,14 @@ int main( int argc, char *argv[] )
}
QLocale::setDefault( currentLocale );
// Date time settings
QgsDateTimeFieldFormatter::applyLocaleChange();
QgsApplication::setTranslation( translationCode );
}
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
for ( const QString &preApplicationLogMessage : std::as_const( preApplicationLogMessages ) )
QgsMessageLog::logMessage( preApplicationLogMessage );

View File

@ -98,13 +98,6 @@ QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fi
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 )
{
switch ( type )
@ -117,3 +110,10 @@ QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type )
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;
//! Adjusts the date time formats according to locale.
static void applyLocaleChange();
/**
* Gets the default format in function of the type.
* The type is expected to be one of
@ -58,6 +55,13 @@ class CORE_EXPORT QgsDateTimeFieldFormatter : public QgsFieldFormatter
* - QVariant::Time
*/
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

View File

@ -85,6 +85,7 @@
#include "layout/qgspagesizeregistry.h"
#include "qgsrecentstylehandler.h"
#include "qgsdatetimefieldformatter.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
#include <QDesktopWidget>
@ -230,6 +231,8 @@ QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const Q
mApplicationMembers = new ApplicationMembers();
*sProfilePath() = profileFolder;
connect( instance(), &QgsApplication::localeChanged, &QgsDateTimeFieldFormatter::applyLocaleChange );
}
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()
{
return qgisSettingsDirPath() + QStringLiteral( "/themes" );

View File

@ -468,6 +468,14 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
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
static QString userThemesFolder();
@ -1043,6 +1051,15 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
void requestForTranslatableObjects( QgsTranslationContext *translationContext );
/**
* Emitted when project locale has been changed.
*
* \since QGIS 3.22.2
*/
void localeChanged();
private:
static void copyPath( const QString &src, const QString &dst );

View File

@ -24,6 +24,7 @@ from qgis.PyQt.QtCore import (
Qt
)
from qgis.core import (
QgsApplication,
QgsFeature,
QgsProject,
QgsRelation,
@ -679,7 +680,7 @@ class TestQgsDateTimeFieldFormatter(unittest.TestCase):
@classmethod
def tearDownClass(cls):
"""Reset locale"""
QLocale.setDefault(QLocale(QLocale.English))
QgsApplication.setLocale(QLocale(QLocale.English))
def test_representValue(self):
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():
QLocale().setDefault(locale)
QgsDateTimeFieldFormatter.applyLocaleChange()
QgsApplication.setLocale(locale)
field_formatter = QgsDateTimeFieldFormatter()
self.assertEqual(field_formatter.defaultFormat(QVariant.Date), assertions["date_format"], locale.name())