Skip to content

Commit

Permalink
use appropriate field type in date time edit widget wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Jan 4, 2018
1 parent ab05bfb commit 227dc9c
Showing 1 changed file with 72 additions and 55 deletions.
127 changes: 72 additions & 55 deletions src/gui/editorwidgets/qgsdatetimeeditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,28 @@ 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 ( fieldIsoFormat )
{
emit valueChanged( dateTime.toString( Qt::ISODate ) );
}
else
{
emit valueChanged( dateTime.toString( fieldFormat ) );
switch ( field().type() )
{
case QVariant::DateTime:
emit valueChanged( dateTime );
break;
case QVariant::Date:
emit valueChanged( dateTime.date() );
break;
case QVariant::Time:
emit valueChanged( dateTime.time() );
default:
const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
if ( fieldIsoFormat )
{
emit valueChanged( dateTime.toString( Qt::ISODate ) );
}
else
{
emit valueChanged( dateTime.toString( fieldFormat ) );
}
break;
}
}

Expand All @@ -131,37 +144,39 @@ QVariant QgsDateTimeEditWrapper::value() const
if ( !mQDateTimeEdit )
return QVariant( field().type() );

if ( field().type() == QVariant::DateTime )
{
if ( mQgsDateTimeEdit )
{
return mQgsDateTimeEdit->dateTime();
}
else
{
return mQDateTimeEdit->dateTime();
}
}

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

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

switch ( field().type() )
{
case QVariant::DateTime:
return dateTime;
break;
case QVariant::Date:
return dateTime.date();
break;
case QVariant::Time:
return dateTime.time();
break;
default:
const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
if ( fieldIsoFormat )
{
return dateTime.toString( Qt::ISODate );
}
else
{
return dateTime.toString( fieldFormat );
}
break;
}
}

Expand All @@ -170,33 +185,35 @@ 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 ( fieldIsoFormat )
{
date = QDateTime::fromString( value.toString(), Qt::ISODate );
}
else
{
date = QDateTime::fromString( value.toString(), fieldFormat );
}
QDateTime dateTime;
switch ( field().type() )
{
case QVariant::DateTime:
case QVariant::Date:
case QVariant::Time:
dateTime = value.toDateTime();
break;
default:
const bool fieldIsoFormat = config( QStringLiteral( "field_iso_format" ), false ).toBool();
const QString fieldFormat = config( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field().type() ) ).toString();
if ( fieldIsoFormat )
{
dateTime = QDateTime::fromString( value.toString(), Qt::ISODate );
}
else
{
dateTime = QDateTime::fromString( value.toString(), fieldFormat );
}
break;
}

if ( mQgsDateTimeEdit )
{
mQgsDateTimeEdit->setDateTime( date );
mQgsDateTimeEdit->setDateTime( dateTime );
}
else
{
mQDateTimeEdit->setDateTime( date );
mQDateTimeEdit->setDateTime( dateTime );
}
}

Expand Down

0 comments on commit 227dc9c

Please sign in to comment.