diff --git a/python/core/auto_generated/qgsmaplayertemporalproperties.sip.in b/python/core/auto_generated/qgsmaplayertemporalproperties.sip.in index 1412528fa83d..6140e6a4aad8 100644 --- a/python/core/auto_generated/qgsmaplayertemporalproperties.sip.in +++ b/python/core/auto_generated/qgsmaplayertemporalproperties.sip.in @@ -66,6 +66,11 @@ Returns the temporal properties temporal range source, can be layer or project. Sets the temporal properties temporal range ``source``. .. seealso:: :py:func:`temporalSource` +%End + + virtual bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const; +%Docstring +Returns ``True`` if the layer should be visible and rendered for the specified time ``range``. %End }; diff --git a/python/core/auto_generated/raster/qgsrasterlayertemporalproperties.sip.in b/python/core/auto_generated/raster/qgsrasterlayertemporalproperties.sip.in index 7343806a1f06..5b90d3fb0c0a 100644 --- a/python/core/auto_generated/raster/qgsrasterlayertemporalproperties.sip.in +++ b/python/core/auto_generated/raster/qgsrasterlayertemporalproperties.sip.in @@ -30,6 +30,9 @@ Constructor for QgsRasterLayerTemporalProperties, with the specified ``parent`` The ``enabled`` argument specifies whether the temporal properties are initially enabled or not (see isActive()). %End + virtual bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const; + + enum TemporalMode { ModeFixedTemporalRange, diff --git a/src/core/qgsmaplayertemporalproperties.cpp b/src/core/qgsmaplayertemporalproperties.cpp index de8e9e7d9345..79daa6836b9d 100644 --- a/src/core/qgsmaplayertemporalproperties.cpp +++ b/src/core/qgsmaplayertemporalproperties.cpp @@ -31,6 +31,11 @@ void QgsMapLayerTemporalProperties::setTemporalSource( QgsMapLayerTemporalProper } } +bool QgsMapLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTimeRange & ) const +{ + return true; +} + QgsMapLayerTemporalProperties::TemporalSource QgsMapLayerTemporalProperties::temporalSource() const { return mSource; diff --git a/src/core/qgsmaplayertemporalproperties.h b/src/core/qgsmaplayertemporalproperties.h index cb99a8da1063..0a5feb68d930 100644 --- a/src/core/qgsmaplayertemporalproperties.h +++ b/src/core/qgsmaplayertemporalproperties.h @@ -23,6 +23,7 @@ #include "qgis_sip.h" #include "qgstemporalproperty.h" #include "qgsreadwritecontext.h" +#include "qgsrange.h" #include @@ -86,6 +87,11 @@ class CORE_EXPORT QgsMapLayerTemporalProperties : public QgsTemporalProperty **/ void setTemporalSource( TemporalSource source ); + /** + * Returns TRUE if the layer should be visible and rendered for the specified time \a range. + */ + virtual bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const; + private: //! Source of the properties temporal range diff --git a/src/core/raster/qgsrasterlayertemporalproperties.cpp b/src/core/raster/qgsrasterlayertemporalproperties.cpp index e7a8822bc54b..fb6070c3d9be 100644 --- a/src/core/raster/qgsrasterlayertemporalproperties.cpp +++ b/src/core/raster/qgsrasterlayertemporalproperties.cpp @@ -23,6 +23,22 @@ QgsRasterLayerTemporalProperties::QgsRasterLayerTemporalProperties( QObject *par { } +bool QgsRasterLayerTemporalProperties::isVisibleInTemporalRange( const QgsDateTimeRange &range ) const +{ + if ( !isActive() ) + return true; + + switch ( mMode ) + { + case ModeFixedTemporalRange: + return range.isInfinite() || mFixedRange.isInfinite() || mFixedRange.overlaps( range ); + + case ModeTemporalRangeFromDataProvider: + return true; + } + return true; +} + QgsRasterLayerTemporalProperties::TemporalMode QgsRasterLayerTemporalProperties::mode() const { return mMode; diff --git a/src/core/raster/qgsrasterlayertemporalproperties.h b/src/core/raster/qgsrasterlayertemporalproperties.h index 116ae72d7e6e..66d1c011e170 100644 --- a/src/core/raster/qgsrasterlayertemporalproperties.h +++ b/src/core/raster/qgsrasterlayertemporalproperties.h @@ -45,6 +45,8 @@ class CORE_EXPORT QgsRasterLayerTemporalProperties : public QgsMapLayerTemporalP */ QgsRasterLayerTemporalProperties( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enabled = false ); + bool isVisibleInTemporalRange( const QgsDateTimeRange &range ) const override; + /** * Mode of the raster temporal properties **/ diff --git a/tests/src/core/testqgsrasterlayertemporalproperties.cpp b/tests/src/core/testqgsrasterlayertemporalproperties.cpp index b420128f5698..10b221984f84 100644 --- a/tests/src/core/testqgsrasterlayertemporalproperties.cpp +++ b/tests/src/core/testqgsrasterlayertemporalproperties.cpp @@ -40,6 +40,7 @@ class TestQgsRasterLayerTemporalProperties : public QObject void checkSettingTemporalRange(); void testChangedSignal(); + void testVisibleInTimeRange(); private: QgsRasterLayerTemporalProperties *temporalProperties = nullptr; @@ -97,5 +98,50 @@ void TestQgsRasterLayerTemporalProperties::testChangedSignal() QCOMPARE( spy.count(), 2 ); } +void TestQgsRasterLayerTemporalProperties::testVisibleInTimeRange() +{ + QgsRasterLayerTemporalProperties props; + // by default, should be visible regardless of time range + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange() ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ) ), + QDateTime( QDate( 2020, 1, 1 ) ) ) ) ); + + // when in data provider time handling mode, we also should always render regardless of time range + props.setIsActive( true ); + props.setMode( QgsRasterLayerTemporalProperties::ModeTemporalRangeFromDataProvider ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange() ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ) ), + QDateTime( QDate( 2020, 1, 1 ) ) ) ) ); + // fix temporal range should be ignored while in ModeTemporalRangeFromDataProvider + props.setFixedTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ) ), + QDateTime( QDate( 2020, 1, 5 ) ) ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange() ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2019, 1, 1 ) ), + QDateTime( QDate( 2019, 1, 2 ) ) ) ) ); + + // switch to fixed time mode + props.setMode( QgsRasterLayerTemporalProperties::ModeFixedTemporalRange ); + // should be visible in infinite time ranges + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange() ) ); + // should not be visible -- outside of fixed time range + QVERIFY( !props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2019, 1, 1 ) ), + QDateTime( QDate( 2019, 1, 2 ) ) ) ) ); + // should be visible -- intersects fixed time range + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 2 ) ), + QDateTime( QDate( 2020, 1, 3 ) ) ) ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 2 ) ), + QDateTime( ) ) ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime(), + QDateTime( QDate( 2020, 1, 3 ) ) ) ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2019, 1, 2 ) ), + QDateTime( QDate( 2020, 1, 3 ) ) ) ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2019, 1, 2 ) ), + QDateTime( QDate( 2021, 1, 3 ) ) ) ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ) ), + QDateTime( QDate( 2020, 1, 1 ) ) ) ) ); + QVERIFY( props.isVisibleInTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 5 ) ), + QDateTime( QDate( 2020, 1, 5 ) ) ) ) ); +} + QGSTEST_MAIN( TestQgsRasterLayerTemporalProperties ) #include "testqgsrasterlayertemporalproperties.moc"