Skip to content
Permalink
Browse files

fix date/time widget does not handle time zones

issue #16657
manually picked from QGIS3 bdf744e
  • Loading branch information
3nids committed Jan 3, 2018
1 parent d4365a1 commit ff70d1392f480941bb3687869904aeded4516786
@@ -21,6 +21,13 @@ QgsDateTimeEditConfig::QgsDateTimeEditConfig( QgsVectorLayer* vl, int fieldIdx,
{
setupUi( this );

mFieldFormatComboBox->clear();
mFieldFormatComboBox->addItem( tr( "Date" ), QGSDATETIMEEDIT_DATEFORMAT );
mFieldFormatComboBox->addItem( tr( "Time" ), QGSDATETIMEEDIT_TIMEFORMAT );
mFieldFormatComboBox->addItem( tr( "Date time" ), QGSDATETIMEEDIT_DATETIMEFORMAT );
mFieldFormatComboBox->addItem( tr( "ISO date time" ), QGSDATETIMEEDIT_ISODATETIMEFORMAT );
mFieldFormatComboBox->addItem( tr( "Custom" ), QString() );

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

connect( mDisplayFormatEdit, SIGNAL( textChanged( QString ) ), this, SLOT( updateDemoWidget() ) );
@@ -53,46 +60,50 @@ void QgsDateTimeEditConfig::updateDemoWidget()

void QgsDateTimeEditConfig::updateFieldFormat( int idx )
{
if ( idx == 0 )
{
mFieldFormatEdit->setText( QGSDATETIMEEDIT_DATEFORMAT );
}
else if ( idx == 1 )
{
mFieldFormatEdit->setText( QGSDATETIMEEDIT_TIMEFORMAT );
}
else if ( idx == 2 )
const QString format = mFieldFormatComboBox->itemData( idx ).toString();
bool custom = format.isEmpty();
if ( !custom )
{
mFieldFormatEdit->setText( QGSDATETIMEEDIT_DATETIMEFORMAT );
mFieldFormatEdit->setText( format );
}

mFieldFormatEdit->setVisible( idx == 3 );
mFieldHelpToolButton->setVisible( idx == 3 );
mFieldFormatEdit->setEnabled( custom );
mFieldHelpToolButton->setVisible( custom );
if ( mFieldHelpToolButton->isHidden() && mDisplayHelpToolButton->isHidden() )
{
mHelpScrollArea->setVisible( false );
}
}



void QgsDateTimeEditConfig::updateDisplayFormat( const QString& fieldFormat )
{
if ( mDisplayFormatComboBox->currentIndex() == 0 )
{
mDisplayFormatEdit->setText( fieldFormat );
// i.e. display format is default
if ( mFieldFormatComboBox->itemData( mFieldFormatComboBox->currentIndex() ) == QGSDATETIMEEDIT_ISODATETIMEFORMAT )
{
mDisplayFormatEdit->setText( QGSDATETIMEEDIT_ISODISPLAYFORMAT );
}
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() );
}
@@ -110,6 +121,7 @@ QgsEditorWidgetConfig QgsDateTimeEditConfig::config()
{
QgsEditorWidgetConfig myConfig;

myConfig.insert( "field_iso_format", mFieldFormatEdit->text() == QGSDATETIMEEDIT_ISODATETIMEFORMAT );
myConfig.insert( "field_format", mFieldFormatEdit->text() );
myConfig.insert( "display_format", mDisplayFormatEdit->text() );
myConfig.insert( "calendar_popup", mCalendarPopupCheckBox->isChecked() );
@@ -126,14 +138,15 @@ void QgsDateTimeEditConfig::setConfig( const QgsEditorWidgetConfig &config )
const QString fieldFormat = config[ "field_format" ].toString();
mFieldFormatEdit->setText( fieldFormat );

if ( fieldFormat == QGSDATETIMEEDIT_DATEFORMAT )
mFieldFormatComboBox->setCurrentIndex( 0 );
else if ( fieldFormat == QGSDATETIMEEDIT_TIMEFORMAT )
mFieldFormatComboBox->setCurrentIndex( 1 );
else if ( fieldFormat == QGSDATETIMEEDIT_DATETIMEFORMAT )
mFieldFormatComboBox->setCurrentIndex( 2 );
const int idx = mFieldFormatComboBox->findData( fieldFormat );
if ( idx >= 0 )
{
mFieldFormatComboBox->setCurrentIndex( idx );
}
else
mFieldFormatComboBox->setCurrentIndex( 3 );
{
mFieldFormatComboBox->setCurrentIndex( 4 );
}
}

if ( config.contains( "display_format" ) )
@@ -21,6 +21,9 @@
#define QGSDATETIMEEDIT_DATEFORMAT "yyyy-MM-dd"
#define QGSDATETIMEEDIT_TIMEFORMAT "HH:mm:ss"
#define QGSDATETIMEEDIT_DATETIMEFORMAT "yyyy-MM-dd HH:mm:ss"
#define QGSDATETIMEEDIT_ISODATETIMEFORMAT "Qt ISO Date"
#define QGSDATETIMEEDIT_ISODISPLAYFORMAT "yyyy-MM-dd HH:mm:ss+t"


/** \ingroup gui
* \class QgsDateTimeEditFactory
@@ -113,8 +113,16 @@ void QgsDateTimeEditWrapper::showIndeterminateState()

void QgsDateTimeEditWrapper::dateTimeChanged( const QDateTime& dateTime )
{
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
emit valueChanged( dateTime.toString( fieldFormat ) );
const bool fieldIsoFormat = config( "field_iso_format" , false ).toBool();
const QString fieldFormat = config( "field_format" , QGSDATETIMEEDIT_DATEFORMAT ).toString();
if ( fieldIsoFormat )
{
emit valueChanged( dateTime.toString( Qt::ISODate ) );
}
else
{
emit valueChanged( dateTime.toString( fieldFormat ) );
}
}

QVariant QgsDateTimeEditWrapper::value() const
@@ -134,15 +142,25 @@ QVariant QgsDateTimeEditWrapper::value() const
}
}

const bool fieldIsoFormat = config( "field_iso_format", false ).toBool();
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();

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

@@ -151,8 +169,25 @@ void QgsDateTimeEditWrapper::setValue( const QVariant &value )
if ( !mQDateTimeEdit )
return;

const bool fieldIsoFormat = config( "field_iso_format", false ).toBool();
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).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 ( fieldIsoFormat )
{
date = QDateTime::fromString( value.toString(), Qt::ISODate );
}
else
{
date = QDateTime::fromString( value.toString(), fieldFormat );
}
}

if ( mQgsDateTimeEdit )
{
@@ -45,8 +45,17 @@ QVariant QgsDateTimeSearchWidgetWrapper::value() const
if ( ! mDateTimeEdit )
return QDateTime();

const bool fieldIsoFormat = config( "field_iso_format", false ).toBool();
const QString fieldFormat = config( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
return mDateTimeEdit->dateTime().toString( fieldFormat );

if ( fieldIsoFormat )
{
return mDateTimeEdit->dateTime().toString( Qt::ISODate );
}
else
{
return mDateTimeEdit->dateTime().toString( fieldFormat );
}
}

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">
@@ -80,26 +80,6 @@
<height>0</height>
</size>
</property>
<item>
<property name="text">
<string>Date</string>
</property>
</item>
<item>
<property name="text">
<string>Time</string>
</property>
</item>
<item>
<property name="text">
<string>Date &amp; time</string>
</property>
</item>
<item>
<property name="text">
<string>Custom format</string>
</property>
</item>
</widget>
</item>
<item row="0" column="1">
@@ -250,6 +230,9 @@
</widget>
<resources>
<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 ff70d13

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