mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-06 00:03:16 -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;
|
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
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/************************************************************************
|
/************************************************************************
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -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 );
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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" );
|
||||||
|
|||||||
@ -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 );
|
||||||
|
|||||||
@ -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())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user