Skip to content

Commit

Permalink
Cleanup QgsTemporalUtils::calculateTemporalRangeForProject
Browse files Browse the repository at this point in the history
Move logic to subclasses instead
  • Loading branch information
nyalldawson committed May 10, 2020
1 parent 702675c commit db7108b
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 91 deletions.
5 changes: 5 additions & 0 deletions src/core/mesh/qgsmeshlayertemporalproperties.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ void QgsMeshLayerTemporalProperties::setDefaultsFromDataProviderTemporalCapabili
mTimeExtent = temporalCapabilities->timeExtent(); mTimeExtent = temporalCapabilities->timeExtent();
} }


QgsDateTimeRange QgsMeshLayerTemporalProperties::calculateTemporalExtent( QgsMapLayer * ) const
{
return mTimeExtent;
}

QgsDateTimeRange QgsMeshLayerTemporalProperties::timeExtent() const QgsDateTimeRange QgsMeshLayerTemporalProperties::timeExtent() const
{ {
return mTimeExtent; return mTimeExtent;
Expand Down
1 change: 1 addition & 0 deletions src/core/mesh/qgsmeshlayertemporalproperties.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class CORE_EXPORT QgsMeshLayerTemporalProperties : public QgsMapLayerTemporalPro
QDomElement writeXml( QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context ) override; QDomElement writeXml( QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context ) override;
bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override; bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
void setDefaultsFromDataProviderTemporalCapabilities( const QgsDataProviderTemporalCapabilities *capabilities ) override; void setDefaultsFromDataProviderTemporalCapabilities( const QgsDataProviderTemporalCapabilities *capabilities ) override;
QgsDateTimeRange calculateTemporalExtent( QgsMapLayer *layer ) const override SIP_SKIP;


/** /**
* Returns the time extent * Returns the time extent
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaplayertemporalproperties.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ bool QgsMapLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTimeR
{ {
return true; return true;
} }

QgsDateTimeRange QgsMapLayerTemporalProperties::calculateTemporalExtent( QgsMapLayer * ) const
{
return QgsDateTimeRange();
}
13 changes: 13 additions & 0 deletions src/core/qgsmaplayertemporalproperties.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ class CORE_EXPORT QgsMapLayerTemporalProperties : public QgsTemporalProperty
*/ */
virtual void setDefaultsFromDataProviderTemporalCapabilities( const QgsDataProviderTemporalCapabilities *capabilities ) = 0; virtual void setDefaultsFromDataProviderTemporalCapabilities( const QgsDataProviderTemporalCapabilities *capabilities ) = 0;


#ifndef SIP_RUN
// sip gets confused with this, refuses to compile

/**
* Attempts to calculate the overall temporal extent for the specified \a layer, using
* the settings defined by the temporal properties object.
*
* May return an infinite range if the extent could not be calculated.
*
* \note Not available in Python bindings
*/
virtual QgsDateTimeRange calculateTemporalExtent( QgsMapLayer *layer ) const;
#endif
}; };


#endif // QGSMAPLAYERTEMPORALPROPERTIES_H #endif // QGSMAPLAYERTEMPORALPROPERTIES_H
94 changes: 3 additions & 91 deletions src/core/qgstemporalutils.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,100 +37,12 @@ QgsDateTimeRange QgsTemporalUtils::calculateTemporalRangeForProject( QgsProject


if ( !currentLayer->temporalProperties() || !currentLayer->temporalProperties()->isActive() ) if ( !currentLayer->temporalProperties() || !currentLayer->temporalProperties()->isActive() )
continue; continue;
QgsDateTimeRange layerRange; QgsDateTimeRange layerRange = currentLayer->temporalProperties()->calculateTemporalExtent( currentLayer );


switch ( currentLayer->type() ) if ( layerRange.begin().isValid() && ( !minDate.isValid() || layerRange.begin() < minDate ) )
{
case QgsMapLayerType::RasterLayer:
{
QgsRasterLayer *rasterLayer = qobject_cast< QgsRasterLayer *>( currentLayer );
const QgsRasterLayerTemporalProperties *temporalProperties = qobject_cast< const QgsRasterLayerTemporalProperties * >( rasterLayer->temporalProperties() );

switch ( temporalProperties->mode() )
{
case QgsRasterLayerTemporalProperties::ModeFixedTemporalRange:
layerRange = temporalProperties->fixedTemporalRange();
break;

case QgsRasterLayerTemporalProperties::ModeTemporalRangeFromDataProvider:
layerRange = rasterLayer->dataProvider()->temporalCapabilities()->availableTemporalRange();
break;
}
break;
}

case QgsMapLayerType::VectorLayer:
{
QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( currentLayer );

QgsVectorLayerTemporalProperties *properties = static_cast< QgsVectorLayerTemporalProperties * >( vectorLayer->temporalProperties() );
switch ( properties->mode() )
{
case QgsVectorLayerTemporalProperties::ModeFixedTemporalRange:
layerRange = properties->fixedTemporalRange();
break;

case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeInstantFromField:
{
const int fieldIndex = vectorLayer->fields().lookupField( properties->startField() );
if ( fieldIndex >= 0 )
{
layerRange = QgsDateTimeRange( vectorLayer->minimumValue( fieldIndex ).toDateTime(),
vectorLayer->maximumValue( fieldIndex ).toDateTime() );
}
break;
}

case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromFields:
{
const int startFieldIndex = vectorLayer->fields().lookupField( properties->startField() );
const int endFieldIndex = vectorLayer->fields().lookupField( properties->endField() );
if ( startFieldIndex >= 0 && endFieldIndex >= 0 )
{
layerRange = QgsDateTimeRange( std::min( vectorLayer->minimumValue( startFieldIndex ).toDateTime(),
vectorLayer->minimumValue( endFieldIndex ).toDateTime() ),
std::max( vectorLayer->maximumValue( startFieldIndex ).toDateTime(),
vectorLayer->maximumValue( endFieldIndex ).toDateTime() ) );
}
else if ( startFieldIndex >= 0 )
{
layerRange = QgsDateTimeRange( vectorLayer->minimumValue( startFieldIndex ).toDateTime(),
vectorLayer->maximumValue( startFieldIndex ).toDateTime() );
}
else if ( endFieldIndex >= 0 )
{
layerRange = QgsDateTimeRange( vectorLayer->minimumValue( endFieldIndex ).toDateTime(),
vectorLayer->maximumValue( endFieldIndex ).toDateTime() );
}
break;
}

case QgsVectorLayerTemporalProperties::ModeRedrawLayerOnly:
break;
}
break;
}

case QgsMapLayerType::MeshLayer:
{
QgsMeshLayer *meshLayer = qobject_cast<QgsMeshLayer *>( currentLayer );
const QgsMeshLayerTemporalProperties *temporalProperties = qobject_cast< const QgsMeshLayerTemporalProperties * >( meshLayer->temporalProperties() );
layerRange = temporalProperties->timeExtent();
break;
}

case QgsMapLayerType::PluginLayer:
case QgsMapLayerType::VectorTileLayer:
break;
}


if ( !minDate.isValid() || layerRange.begin() < minDate )
minDate = layerRange.begin(); minDate = layerRange.begin();
if ( !maxDate.isValid() || layerRange.end() > maxDate ) if ( layerRange.end().isValid() && ( !maxDate.isValid() || layerRange.end() > maxDate ) )
maxDate = layerRange.end(); maxDate = layerRange.end();


} }


return QgsDateTimeRange( minDate, maxDate ); return QgsDateTimeRange( minDate, maxDate );
Expand Down
53 changes: 53 additions & 0 deletions src/core/qgsvectorlayertemporalproperties.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,59 @@ bool QgsVectorLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTi
return true; return true;
} }


QgsDateTimeRange QgsVectorLayerTemporalProperties::calculateTemporalExtent( QgsMapLayer *layer ) const
{
QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !layer )
return QgsDateTimeRange();

switch ( mMode )
{
case QgsVectorLayerTemporalProperties::ModeFixedTemporalRange:
return mFixedRange;

case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeInstantFromField:
{
const int fieldIndex = vectorLayer->fields().lookupField( mStartFieldName );
if ( fieldIndex >= 0 )
{
return QgsDateTimeRange( vectorLayer->minimumValue( fieldIndex ).toDateTime(),
vectorLayer->maximumValue( fieldIndex ).toDateTime() );
}
break;
}

case QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromFields:
{
const int startFieldIndex = vectorLayer->fields().lookupField( mStartFieldName );
const int endFieldIndex = vectorLayer->fields().lookupField( mEndFieldName );
if ( startFieldIndex >= 0 && endFieldIndex >= 0 )
{
return QgsDateTimeRange( std::min( vectorLayer->minimumValue( startFieldIndex ).toDateTime(),
vectorLayer->minimumValue( endFieldIndex ).toDateTime() ),
std::max( vectorLayer->maximumValue( startFieldIndex ).toDateTime(),
vectorLayer->maximumValue( endFieldIndex ).toDateTime() ) );
}
else if ( startFieldIndex >= 0 )
{
return QgsDateTimeRange( vectorLayer->minimumValue( startFieldIndex ).toDateTime(),
vectorLayer->maximumValue( startFieldIndex ).toDateTime() );
}
else if ( endFieldIndex >= 0 )
{
return QgsDateTimeRange( vectorLayer->minimumValue( endFieldIndex ).toDateTime(),
vectorLayer->maximumValue( endFieldIndex ).toDateTime() );
}
break;
}

case QgsVectorLayerTemporalProperties::ModeRedrawLayerOnly:
break;
}

return QgsDateTimeRange();
}

QgsVectorLayerTemporalProperties::TemporalMode QgsVectorLayerTemporalProperties::mode() const QgsVectorLayerTemporalProperties::TemporalMode QgsVectorLayerTemporalProperties::mode() const
{ {
return mMode; return mMode;
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayertemporalproperties.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CORE_EXPORT QgsVectorLayerTemporalProperties : public QgsMapLayerTemporalP
QgsVectorLayerTemporalProperties( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enabled = false ); QgsVectorLayerTemporalProperties( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enabled = false );


bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const override; bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const override;
QgsDateTimeRange calculateTemporalExtent( QgsMapLayer *layer ) const override SIP_SKIP;


/** /**
* Mode of the vector temporal properties * Mode of the vector temporal properties
Expand Down
18 changes: 18 additions & 0 deletions src/core/raster/qgsrasterlayertemporalproperties.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ bool QgsRasterLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTi
return true; return true;
} }


QgsDateTimeRange QgsRasterLayerTemporalProperties::calculateTemporalExtent( QgsMapLayer *layer ) const
{
QgsRasterLayer *rasterLayer = qobject_cast< QgsRasterLayer *>( layer );
if ( !rasterLayer )
return QgsDateTimeRange();

switch ( mMode )
{
case QgsRasterLayerTemporalProperties::ModeFixedTemporalRange:
return mFixedRange;

case QgsRasterLayerTemporalProperties::ModeTemporalRangeFromDataProvider:
return rasterLayer->dataProvider()->temporalCapabilities()->availableTemporalRange();
}

return QgsDateTimeRange();
}

QgsRasterLayerTemporalProperties::TemporalMode QgsRasterLayerTemporalProperties::mode() const QgsRasterLayerTemporalProperties::TemporalMode QgsRasterLayerTemporalProperties::mode() const
{ {
return mMode; return mMode;
Expand Down
1 change: 1 addition & 0 deletions src/core/raster/qgsrasterlayertemporalproperties.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CORE_EXPORT QgsRasterLayerTemporalProperties : public QgsMapLayerTemporalP
QgsRasterLayerTemporalProperties( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enabled = false ); QgsRasterLayerTemporalProperties( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enabled = false );


bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const override; bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const override;
QgsDateTimeRange calculateTemporalExtent( QgsMapLayer *layer ) const override SIP_SKIP;


/** /**
* Mode of the raster temporal properties * Mode of the raster temporal properties
Expand Down

0 comments on commit db7108b

Please sign in to comment.