Skip to content
Permalink
Browse files

fix date/time widget for field with time zones

this adds a new format of date/time to use Qt::ISODate format
the issue is that QDate::fromString does not recognize the t letter for the timezone in format
this a hack since any field_format needs to be tested if it corresponds to QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT and in such case replace by Qt::ISODate instead of a format string
  • Loading branch information
3nids committed Dec 28, 2017
1 parent df547e1 commit 1710dc0587765beb494a8c21212f0946642481df
@@ -24,6 +24,7 @@ the field configuration.
static const QString DEFAULT_DATE_FORMAT;
static const QString DEFAULT_TIME_FORMAT;
static const QString DEFAULT_DATETIME_FORMAT;
static const QString DEFAULT_ISO_FORMAT;

QgsDateTimeFieldFormatter();
%Docstring
@@ -22,6 +22,7 @@
const QString QgsDateTimeFieldFormatter::DEFAULT_DATE_FORMAT = QStringLiteral( "yyyy-MM-dd" );
const QString QgsDateTimeFieldFormatter::DEFAULT_TIME_FORMAT = QStringLiteral( "HH:mm:ss" );
const QString QgsDateTimeFieldFormatter::DEFAULT_DATETIME_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss" );
const QString QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss+t" );


QString QgsDateTimeFieldFormatter::id() const
@@ -37,15 +38,23 @@ QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fi

if ( value.isNull() )
{
QgsSettings settings;
return QgsApplication::nullRepresentation();
}

const QgsField field = layer->fields().at( fieldIndex );
const QString displayFormat = config.value( QStringLiteral( "display_format" ), defaultFormat( field.type() ) ).toString();
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), defaultFormat( field.type() ) ).toString();

QDateTime date = QDateTime::fromString( value.toString(), fieldFormat );
QDateTime date;
if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
{
date = QDateTime::fromString( value.toString(), Qt::ISODate );
}
else
{
date = QDateTime::fromString( value.toString(), fieldFormat );
}


if ( date.isValid() )
{
@@ -33,6 +33,7 @@ class CORE_EXPORT QgsDateTimeFieldFormatter : public QgsFieldFormatter
static const QString DEFAULT_DATE_FORMAT;
static const QString DEFAULT_TIME_FORMAT;
static const QString DEFAULT_DATETIME_FORMAT;
static const QString DEFAULT_ISO_FORMAT;

/**
* Default constructor of field formatter for a date time field.
@@ -67,9 +67,13 @@ void QgsDateTimeEditConfig::updateFieldFormat( int idx )
{
mFieldFormatEdit->setText( QgsDateTimeFieldFormatter::DEFAULT_DATETIME_FORMAT );
}
else if ( idx == 3 )
{
mFieldFormatEdit->setText( QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT );
}

mFieldFormatEdit->setVisible( idx == 3 );
mFieldHelpToolButton->setVisible( idx == 3 );
mFieldFormatEdit->setEnabled( idx == 4 );
mFieldHelpToolButton->setVisible( idx == 4 );
if ( mFieldHelpToolButton->isHidden() && mDisplayHelpToolButton->isHidden() )
{
mHelpScrollArea->setVisible( false );
@@ -132,8 +136,10 @@ void QgsDateTimeEditConfig::setConfig( const QVariantMap &config )
mFieldFormatComboBox->setCurrentIndex( 1 );
else if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_DATETIME_FORMAT )
mFieldFormatComboBox->setCurrentIndex( 2 );
else
else if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
mFieldFormatComboBox->setCurrentIndex( 3 );
else
mFieldFormatComboBox->setCurrentIndex( 4 );

QString displayFormat = config.value( QStringLiteral( "display_format" ), QgsDateTimeFieldFormatter::defaultFormat( fieldDef.type() ) ).toString();
mDisplayFormatEdit->setText( displayFormat );
@@ -115,7 +115,14 @@ void QgsDateTimeEditWrapper::showIndeterminateState()
void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime &dateTime )
{
const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
emit valueChanged( dateTime.toString( fieldFormat ) );
if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
{
emit valueChanged( dateTime.toString( Qt::ISODate ) );
}
else
{
emit valueChanged( dateTime.toString( fieldFormat ) );
}
}

QVariant QgsDateTimeEditWrapper::value() const
@@ -137,13 +144,22 @@ QVariant QgsDateTimeEditWrapper::value() const

const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();

QDateTime date;
if ( mQgsDateTimeEdit )
{
return mQgsDateTimeEdit->dateTime().toString( fieldFormat );
date = mQgsDateTimeEdit->dateTime();
}
else
{
return mQDateTimeEdit->dateTime().toString( fieldFormat );
date = mQDateTimeEdit->dateTime();
}
if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
{
return date.toString( Qt::ISODate );
}
else
{
return date.toString( fieldFormat );
}
}

@@ -153,7 +169,22 @@ void QgsDateTimeEditWrapper::setValue( const QVariant &value )
return;

const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
const QDateTime date = field().type() == QVariant::DateTime ? value.toDateTime() : QDateTime::fromString( value.toString(), fieldFormat );
QDateTime date;
if ( field().type() == QVariant::DateTime )
{
date = value.toDateTime();
}
else
{
if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
{
date = QDateTime::fromString( value.toString(), Qt::ISODate );
}
else
{
date = QDateTime::fromString( value.toString(), fieldFormat );
}
}

if ( mQgsDateTimeEdit )
{
@@ -47,7 +47,14 @@ QVariant QgsDateTimeSearchWidgetWrapper::value() const
return QDateTime();

const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( layer()->fields().at( mFieldIdx ).type() ) ).toString();
return mDateTimeEdit->dateTime().toString( fieldFormat );
if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
{
return mDateTimeEdit->dateTime().toString( fieldFormat );
}
else
{
return mDateTimeEdit->dateTime().toString( Qt::ISODate );
}
}

QgsSearchWidgetWrapper::FilterFlags QgsDateTimeSearchWidgetWrapper::supportedFlags() const
@@ -24,8 +24,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>468</width>
<height>1033</height>
<width>478</width>
<height>971</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_5">
@@ -95,6 +95,11 @@
<string>Date &amp; time</string>
</property>
</item>
<item>
<property name="text">
<string>ISO date &amp; time</string>
</property>
</item>
<item>
<property name="text">
<string>Custom format</string>
@@ -272,6 +277,32 @@
</tabstops>
<resources>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit 1710dc0

Please sign in to comment.
You can’t perform that action at this time.