Skip to content

Commit

Permalink
Merge pull request #46419 from Joonalai/improve-format-datetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 9, 2021
2 parents e622068 + 8ba472b commit 28fac71
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 22 deletions.
Expand Up @@ -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.
Expand All @@ -53,6 +48,7 @@ The type is expected to be one of
- QVariant.Date
- QVariant.Time
%End

};

/************************************************************************
Expand Down
17 changes: 17 additions & 0 deletions python/core/auto_generated/qgsapplication.sip.in
Expand Up @@ -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();
Expand Down Expand Up @@ -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


};


Expand Down
7 changes: 3 additions & 4 deletions src/app/main.cpp
Expand Up @@ -113,7 +113,6 @@ typedef SInt32 SRefCon;

#include "qgsuserprofilemanager.h"
#include "qgsuserprofile.h"
#include "qgsdatetimefieldformatter.h"

#ifdef HAVE_OPENCL
#include "qgsopenclutils.h"
Expand Down Expand Up @@ -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 );
Expand Down
14 changes: 7 additions & 7 deletions src/core/fieldformatter/qgsdatetimefieldformatter.cpp
Expand Up @@ -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 )
Expand All @@ -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;
}
10 changes: 7 additions & 3 deletions src/core/fieldformatter/qgsdatetimefieldformatter.h
Expand Up @@ -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
Expand All @@ -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
9 changes: 9 additions & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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" );
Expand Down
17 changes: 17 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -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();

Expand Down Expand Up @@ -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 );
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_qgsfieldformatters.py
Expand Up @@ -24,6 +24,7 @@
Qt
)
from qgis.core import (
QgsApplication,
QgsFeature,
QgsProject,
QgsRelation,
Expand Down Expand Up @@ -679,7 +680,7 @@ def setUpClass(cls):
@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",
Expand Down Expand Up @@ -716,8 +717,7 @@ def test_representValue(self):
}

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())
Expand Down

0 comments on commit 28fac71

Please sign in to comment.