Skip to content

Commit

Permalink
add a bool in config to determine if it uses Qt::ISODate for field fo…
Browse files Browse the repository at this point in the history
…rmat

also fixes the fact that the display format of QDateTimeEdit only accepts string formats, so in case of Qt::ISODate use a default date/time with zmne zone
  • Loading branch information
3nids committed Jan 3, 2018
1 parent a3588e0 commit 919cbec
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 61 deletions.
1 change: 1 addition & 0 deletions python/core/fieldformatter/qgsdatetimefieldformatter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ the field configuration.
static const QString DEFAULT_TIME_FORMAT;
static const QString DEFAULT_DATETIME_FORMAT;
static const QString DEFAULT_ISO_FORMAT;
static const QString DEFAULT_ISO_DISPLAY_FORMAT;

QgsDateTimeFieldFormatter();
%Docstring
Expand Down
11 changes: 7 additions & 4 deletions src/core/fieldformatter/qgsdatetimefieldformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
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" );
// we need to use Qt::ISODate rather than a string format definition in QDate::fromString
const QString QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT = QStringLiteral( "Qt ISO Date" );
// but QDateTimeEdit::setDisplayFormat only accepts string formats, so use with time zone by default
const QString QgsDateTimeFieldFormatter::DEFAULT_ISO_DISPLAY_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss+t" );


QString QgsDateTimeFieldFormatter::id() const
Expand All @@ -42,11 +45,12 @@ QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fi
}

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

QDateTime date;
if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
if ( fieldIsoFormat )
{
date = QDateTime::fromString( value.toString(), Qt::ISODate );
}
Expand All @@ -55,7 +59,6 @@ QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fi
date = QDateTime::fromString( value.toString(), fieldFormat );
}


if ( date.isValid() )
{
result = date.toString( displayFormat );
Expand Down
1 change: 1 addition & 0 deletions src/core/fieldformatter/qgsdatetimefieldformatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CORE_EXPORT QgsDateTimeFieldFormatter : public QgsFieldFormatter
static const QString DEFAULT_TIME_FORMAT;
static const QString DEFAULT_DATETIME_FORMAT;
static const QString DEFAULT_ISO_FORMAT;
static const QString DEFAULT_ISO_DISPLAY_FORMAT;

/**
* Default constructor of field formatter for a date time field.
Expand Down
63 changes: 35 additions & 28 deletions src/gui/editorwidgets/qgsdatetimeeditconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ QgsDateTimeEditConfig::QgsDateTimeEditConfig( QgsVectorLayer *vl, int fieldIdx,
{
setupUi( this );

mFieldFormatComboBox->clear();
mFieldFormatComboBox->addItem( tr( "Date" ), QgsDateTimeFieldFormatter::DEFAULT_DATE_FORMAT );
mFieldFormatComboBox->addItem( tr( "Time" ), QgsDateTimeFieldFormatter::DEFAULT_TIME_FORMAT );
mFieldFormatComboBox->addItem( tr( "Date time" ), QgsDateTimeFieldFormatter::DEFAULT_DATETIME_FORMAT );
mFieldFormatComboBox->addItem( tr( "ISO date time" ), QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT );
mFieldFormatComboBox->addItem( tr( "Custom" ), "" );

mDemoDateTimeEdit->setDateTime( QDateTime::currentDateTime() );

connect( mDisplayFormatEdit, &QLineEdit::textChanged, this, &QgsDateTimeEditConfig::updateDemoWidget );
Expand Down Expand Up @@ -55,25 +62,16 @@ void QgsDateTimeEditConfig::updateDemoWidget()

void QgsDateTimeEditConfig::updateFieldFormat( int idx )
{
if ( idx == 0 )
{
mFieldFormatEdit->setText( QgsDateTimeFieldFormatter::DEFAULT_DATE_FORMAT );
}
else if ( idx == 1 )
Q_UNUSED( idx );
const QString format = mFieldFormatComboBox->currentData().toString();
bool custom = format.isEmpty();
if ( !custom )
{
mFieldFormatEdit->setText( QgsDateTimeFieldFormatter::DEFAULT_TIME_FORMAT );
}
else if ( idx == 2 )
{
mFieldFormatEdit->setText( QgsDateTimeFieldFormatter::DEFAULT_DATETIME_FORMAT );
}
else if ( idx == 3 )
{
mFieldFormatEdit->setText( QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT );
mFieldFormatEdit->setText( format );
}

mFieldFormatEdit->setEnabled( idx == 4 );
mFieldHelpToolButton->setVisible( idx == 4 );
mFieldFormatEdit->setEnabled( custom );
mFieldHelpToolButton->setVisible( custom );
if ( mFieldHelpToolButton->isHidden() && mDisplayHelpToolButton->isHidden() )
{
mHelpScrollArea->setVisible( false );
Expand All @@ -85,20 +83,29 @@ void QgsDateTimeEditConfig::updateDisplayFormat( const QString &fieldFormat )
{
if ( mDisplayFormatComboBox->currentIndex() == 0 )
{
mDisplayFormatEdit->setText( fieldFormat );
// i.e. display format is default
if ( mFieldFormatComboBox->currentData() == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
{
mDisplayFormatEdit->setText( QgsDateTimeFieldFormatter::DEFAULT_ISO_DISPLAY_FORMAT );
}
else
{
mDisplayFormatEdit->setText( fieldFormat );
}
}
}


void QgsDateTimeEditConfig::displayFormatChanged( int idx )
{
mDisplayFormatEdit->setEnabled( idx == 1 );
mDisplayHelpToolButton->setVisible( idx == 1 );
const bool custom = idx == 1;
mDisplayFormatEdit->setEnabled( custom );
mDisplayHelpToolButton->setVisible( custom );
if ( mFieldHelpToolButton->isHidden() && mDisplayHelpToolButton->isHidden() )
{
mHelpScrollArea->setVisible( false );
}
if ( idx == 0 )
if ( !custom )
{
mDisplayFormatEdit->setText( mFieldFormatEdit->text() );
}
Expand All @@ -116,6 +123,7 @@ QVariantMap QgsDateTimeEditConfig::config()
{
QVariantMap myConfig;

myConfig.insert( QStringLiteral( "field_iso_format" ), mFieldFormatEdit->text() == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT );
myConfig.insert( QStringLiteral( "field_format" ), mFieldFormatEdit->text() );
myConfig.insert( QStringLiteral( "display_format" ), mDisplayFormatEdit->text() );
myConfig.insert( QStringLiteral( "calendar_popup" ), mCalendarPopupCheckBox->isChecked() );
Expand All @@ -130,16 +138,15 @@ void QgsDateTimeEditConfig::setConfig( const QVariantMap &config )
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( fieldDef.type() ) ).toString();
mFieldFormatEdit->setText( fieldFormat );

if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_DATE_FORMAT )
mFieldFormatComboBox->setCurrentIndex( 0 );
else if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_TIME_FORMAT )
mFieldFormatComboBox->setCurrentIndex( 1 );
else if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_DATETIME_FORMAT )
mFieldFormatComboBox->setCurrentIndex( 2 );
else if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
mFieldFormatComboBox->setCurrentIndex( 3 );
const int idx = mFieldFormatComboBox->findData( fieldFormat );
if ( idx >= 0 )
{
mFieldFormatComboBox->setCurrentIndex( idx );
}
else
{
mFieldFormatComboBox->setCurrentIndex( 4 );
}

QString displayFormat = config.value( QStringLiteral( "display_format" ), QgsDateTimeFieldFormatter::defaultFormat( fieldDef.type() ) ).toString();
mDisplayFormatEdit->setText( displayFormat );
Expand Down
10 changes: 7 additions & 3 deletions src/gui/editorwidgets/qgsdatetimeeditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ void QgsDateTimeEditWrapper::showIndeterminateState()

void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime &dateTime )
{
const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
if ( fieldIsoFormat )
{
emit valueChanged( dateTime.toString( Qt::ISODate ) );
}
Expand All @@ -142,6 +143,7 @@ QVariant QgsDateTimeEditWrapper::value() const
}
}

const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();

QDateTime date;
Expand All @@ -153,7 +155,7 @@ QVariant QgsDateTimeEditWrapper::value() const
{
date = mQDateTimeEdit->dateTime();
}
if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
if ( fieldIsoFormat )
{
return date.toString( Qt::ISODate );
}
Expand All @@ -168,15 +170,17 @@ void QgsDateTimeEditWrapper::setValue( const QVariant &value )
if ( !mQDateTimeEdit )
return;

const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();

QDateTime date;
if ( field().type() == QVariant::DateTime )
{
date = value.toDateTime();
}
else
{
if ( fieldFormat == QgsDateTimeFieldFormatter::DEFAULT_ISO_FORMAT )
if ( fieldIsoFormat )
{
date = QDateTime::fromString( value.toString(), Qt::ISODate );
}
Expand Down
40 changes: 14 additions & 26 deletions src/ui/editorwidgets/qgsdatetimeeditconfig.ui

Large diffs are not rendered by default.

0 comments on commit 919cbec

Please sign in to comment.