Skip to content

Commit

Permalink
[temporal] Update the attribute table's show feature visible on map t…
Browse files Browse the repository at this point in the history
…o take temporal range context into account
  • Loading branch information
nirvn authored and nyalldawson committed Nov 20, 2020
1 parent 3446c4f commit 6b638aa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/gui/attributetable/qgsattributetablefiltermodel.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgsattributetablemodel.h"
#include "qgsfeatureiterator.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayertemporalproperties.h"
#include "qgsfeature.h"
#include "qgsmapcanvas.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -343,6 +344,7 @@ void QgsAttributeTableFilterModel::disconnectFilterModeConnections()
{
case ShowVisible:
disconnect( mCanvas, &QgsMapCanvas::extentsChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
disconnect( mCanvas, &QgsMapCanvas::temporalRangeChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
disconnect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
disconnect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
break;
Expand All @@ -365,6 +367,7 @@ void QgsAttributeTableFilterModel::connectFilterModeConnections( QgsAttributeTab
{
case ShowVisible:
connect( mCanvas, &QgsMapCanvas::extentsChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
connect( mCanvas, &QgsMapCanvas::temporalRangeChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
connect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
connect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
generateListOfVisibleFeatures();
Expand Down Expand Up @@ -607,6 +610,19 @@ void QgsAttributeTableFilterModel::generateListOfVisibleFeatures()
{
r.setFilterRect( rect );
}

if ( mCanvas->mapSettings().isTemporal() )
{
if ( !layer()->temporalProperties()->isVisibleInTemporalRange( mCanvas->mapSettings().temporalRange() ) )
return;

QgsVectorLayerTemporalContext temporalContext;
temporalContext.setLayer( layer() );
const QString temporalFilter = qobject_cast< const QgsVectorLayerTemporalProperties * >( layer()->temporalProperties() )->createFilterString( temporalContext, mCanvas->mapSettings().temporalRange() );
if ( !temporalFilter.isEmpty() )
r.setFilterExpression( temporalFilter );
}

QgsFeatureIterator features = masterModel()->layerCache()->getFeatures( r );

QgsFeature f;
Expand Down
43 changes: 43 additions & 0 deletions tests/src/app/testqgsattributetable.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgsfeature.h"
#include "qgsgeometry.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayertemporalproperties.h"
#include "qgsattributetabledialog.h"
#include "qgsproject.h"
#include "qgsmapcanvas.h"
Expand Down Expand Up @@ -54,6 +55,7 @@ class TestQgsAttributeTable : public QObject
void testSortByDisplayExpression();
void testOrderColumn();
void testFilteredFeatures();
void testVisibleTemporal();

private:
QgisApp *mQgisApp = nullptr;
Expand Down Expand Up @@ -210,6 +212,47 @@ void TestQgsAttributeTable::testNoGeom()

}

void TestQgsAttributeTable::testVisibleTemporal()
{
// test attribute table opening in show feature visible mode
std::unique_ptr< QgsVectorLayer> tempLayer( new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:4326&field=pk:int&field=col1:date" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
QVERIFY( tempLayer->isValid() );

QgsPolylineXY line;
line << QgsPointXY( 0, 0 ) << QgsPointXY( 1, 1 );
QgsGeometry geometry = QgsGeometry::fromPolylineXY( line ) ;
QgsFeature f1( tempLayer->dataProvider()->fields(), 1 );
f1.setGeometry( geometry );
f1.setAttributes( QgsAttributes() << 1 << QDate( 2020, 1, 1 ) );
QgsFeature f2( tempLayer->dataProvider()->fields(), 2 );
f2.setGeometry( geometry );
f2.setAttributes( QgsAttributes() << 2 << QDate( 2020, 3, 1 ) );
QgsFeature f3( tempLayer->dataProvider()->fields(), 3 );
line.clear();
line << QgsPointXY( -3, -3 ) << QgsPointXY( -2, -2 );
geometry = QgsGeometry::fromPolylineXY( line );
f3.setGeometry( geometry );
f3.setAttributes( QgsAttributes() << 3 << QDate( 2020, 1, 1 ) );
QVERIFY( tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 << f3 ) );

QgsVectorLayerTemporalProperties *temporalProperties = qobject_cast< QgsVectorLayerTemporalProperties *>( tempLayer->temporalProperties() );
temporalProperties->setIsActive( true );
temporalProperties->setMode( QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromFields );
temporalProperties->setStartField( QStringLiteral( "col1" ) );

mQgisApp->mapCanvas()->setDestinationCrs( QgsCoordinateReferenceSystem( "EPSG:4326" ) );
mQgisApp->mapCanvas()->resize( 500, 500 );
mQgisApp->mapCanvas()->setLayers( QList< QgsMapLayer *>() << tempLayer.get() );
mQgisApp->mapCanvas()->setExtent( QgsRectangle( -1, -1, 1, 1 ) );
mQgisApp->mapCanvas()->setTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2020, 1, 1 ) ), QDateTime( QDate( 2020, 2, 1 ) ) ) );

std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( tempLayer.get(), QgsAttributeTableFilterModel::ShowVisible ) );

// feature id 2 is filtered out due to being out of temporal range
// feature id 3 is filtered out due to being out of visible extent
QCOMPARE( dlg->mMainView->filteredFeatures(), QgsFeatureIds() << 1 );
}

void TestQgsAttributeTable::testSelected()
{
// test attribute table opening in show selected mode
Expand Down

0 comments on commit 6b638aa

Please sign in to comment.