Skip to content
Permalink
Browse files

If a postgres raster layer is set to take the date from a field,

show a combo box for the default date choice instead of a free-form
date widget

This makes it easier for users to pick a valid default date for
the layer, corresponding to a value where data actually exists...
  • Loading branch information
nyalldawson committed Mar 28, 2021
1 parent c7da1f7 commit 07673fb375471ab0037ed75f286e9280107053d5
@@ -48,6 +48,7 @@ QgsPostgresRasterTemporalSettingsWidget::QgsPostgresRasterTemporalSettingsWidget
} );
mPostgresRasterDefaultTime->setAllowNull( true );
mPostgresRasterDefaultTime->setEmpty();
mDefaultTimeStackedWidget->setCurrentIndex( 0 );

syncToLayer( mRasterLayer );
}
@@ -58,6 +59,9 @@ void QgsPostgresRasterTemporalSettingsWidget::syncToLayer( QgsMapLayer *layer )
const QgsFields fields { mRasterLayer->dataProvider()->fields() };
mPostgresRasterTemporalFieldComboBox->setFields( fields );

mDefaultTimeStackedWidget->setCurrentIndex( 0 );
mDefaultTimeComboBox->clear();

if ( mRasterLayer->dataProvider()->uri().hasParam( QStringLiteral( "temporalFieldIndex" ) ) )
{
bool ok;
@@ -66,12 +70,30 @@ void QgsPostgresRasterTemporalSettingsWidget::syncToLayer( QgsMapLayer *layer )
{
mPostgresRasterTemporalGroup->setChecked( true );
mPostgresRasterTemporalFieldComboBox->setField( fields.field( fieldIdx ).name() );

const QList< QgsDateTimeRange > allRanges = mRasterLayer->dataProvider()->temporalCapabilities()->allAvailableTemporalRanges();
if ( !allRanges.empty() && allRanges.size() < 50 )
{
// if an appropriate number of unique ranges is known, show a combo box with these options instead of the free-form
// date picker widget
mDefaultTimeStackedWidget->setCurrentIndex( 1 );
for ( const QgsDateTimeRange &range : allRanges )
{
mDefaultTimeComboBox->addItem( range.begin().toString( Qt::ISODate ), QVariant::fromValue( range.begin() ) );
}
}

if ( mRasterLayer->dataProvider()->uri().hasParam( QStringLiteral( "temporalDefaultTime" ) ) )
{
const QDateTime defaultDateTime { QDateTime::fromString( mRasterLayer->dataProvider()->uri().param( QStringLiteral( "temporalDefaultTime" ) ), Qt::DateFormat::ISODate ) };
if ( defaultDateTime.isValid() )
{
mPostgresRasterDefaultTime->setDateTime( defaultDateTime );

if ( const int index = mDefaultTimeComboBox->findData( QVariant::fromValue( defaultDateTime ) ); index >= 0 )
mDefaultTimeComboBox->setCurrentIndex( index );
else if ( mDefaultTimeComboBox->count() > 0 )
mDefaultTimeComboBox->setCurrentIndex( 0 );
}
}
}
@@ -92,14 +114,31 @@ void QgsPostgresRasterTemporalSettingsWidget::apply()
if ( fieldIdx >= 0 )
{
uri.setParam( QStringLiteral( "temporalFieldIndex" ), QString::number( fieldIdx ) );
if ( mPostgresRasterDefaultTime->dateTime().isValid() )

QDateTime defaultDateTime;
if ( mDefaultTimeStackedWidget->currentIndex() == 0 )
{
if ( mPostgresRasterDefaultTime->dateTime().isValid() )
{
defaultDateTime = mPostgresRasterDefaultTime->dateTime();
}
}
else
{
if ( mDefaultTimeComboBox->currentData().isValid() )
{
defaultDateTime = mDefaultTimeComboBox->currentData().value< QDateTime >();
}
}

if ( defaultDateTime.isValid() )
{
QDateTime defaultDateTime { mPostgresRasterDefaultTime->dateTime() };
const QTime defaultTime { defaultDateTime.time() };
// Set secs to 0
defaultDateTime.setTime( { defaultTime.hour(), defaultTime.minute(), 0 } );
uri.setParam( QStringLiteral( "temporalDefaultTime" ), defaultDateTime.toString( Qt::DateFormat::ISODate ) );
}

if ( uri.uri( ) != originaUri )
mRasterLayer->setDataSource( uri.uri(), mRasterLayer->name(), mRasterLayer->providerType(), QgsDataProvider::ProviderOptions() );
}
@@ -45,13 +45,6 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_12">
<property name="text">
<string>Default time</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QgsFieldComboBox" name="mPostgresRasterTemporalFieldComboBox">
<property name="sizePolicy">
@@ -62,23 +55,72 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<item row="2" column="0" colspan="2">
<widget class="QLabel" name="label_14">
<property name="text">
<string>Temporal field</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;A default value can be specified and it will be used when the temporal controller is not requesting any particular time.&lt;/p&gt;&lt;p&gt;It is recommended to set a default temporal value here instead of using a provider filter because the provider filter will still be active when using the temporal controller while this default value will be ignored.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QgsDateTimeEdit" name="mPostgresRasterDefaultTime"/>
<widget class="QStackedWidget" name="mDefaultTimeStackedWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="widget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QgsDateTimeEdit" name="mPostgresRasterDefaultTime"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QComboBox" name="mDefaultTimeComboBox"/>
</item>
</layout>
</widget>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QLabel" name="label_14">
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;A default value can be specified and it will be used when the temporal controller is not requesting any particular time.&lt;/p&gt;&lt;p&gt;It is recommended to set a default temporal value here instead of using a provider filter because the provider filter will still be active when using the temporal controller while this default value will be ignored.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>Temporal field</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_12">
<property name="text">
<string>Default time</string>
</property>
</widget>
</item>

0 comments on commit 07673fb

Please sign in to comment.