mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-05 00:04:40 -05:00
Merge pull request #46419 from Joonalai/improve-format-datetimes
This commit is contained in:
commit
28fac71bb4
@ -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
|
||||
|
||||
};
|
||||
|
||||
/************************************************************************
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -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 );
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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" );
|
||||
|
||||
@ -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 );
|
||||
|
||||
@ -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())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user