Skip to content
Permalink
Browse files

timer on reload visible features (according to extend) and on filteri…

…ng features (according to filter) to avoid long reload time in case a signal (like featureAdded) is called multiple times in short period
  • Loading branch information
signedav committed May 5, 2020
1 parent 92547ed commit 0290b8c7da4f4400f20f035b00a6cdc503429b13
@@ -214,6 +214,16 @@ Emitted whenever the sort column is changed

:param column: The sort column
:param order: The sort order
%End

void featuresFiltered();
%Docstring
Emitted when the filtering of the features has been done
%End

void visibleReloaded();
%Docstring
Emitted when the the visible features on extend are reloaded (the list is created)
%End

protected:
@@ -40,6 +40,11 @@ QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsMapCanvas *canvas
setDynamicSortFilter( true );
setSortRole( QgsAttributeTableModel::SortRole );
connect( layer(), &QgsVectorLayer::selectionChanged, this, &QgsAttributeTableFilterModel::selectionChanged );

mReloadVisibleTimer.setSingleShot( true );
connect( &mReloadVisibleTimer, &QTimer::timeout, this, &QgsAttributeTableFilterModel::reloadVisible );
mFilterFeaturesTimer.setSingleShot( true );
connect( &mFilterFeaturesTimer, &QTimer::timeout, this, &QgsAttributeTableFilterModel::filterFeatures );
}

bool QgsAttributeTableFilterModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
@@ -322,42 +327,42 @@ void QgsAttributeTableFilterModel::setFilterMode( FilterMode filterMode )
switch ( mFilterMode )
{
case ShowVisible:
disconnect( mCanvas, &QgsMapCanvas::extentsChanged, this, &QgsAttributeTableFilterModel::reloadVisible );
disconnect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::reloadVisible );
disconnect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::reloadVisible );
disconnect( mTableModel, &QgsAttributeTableModel::finished, this, &QgsAttributeTableFilterModel::reloadVisible );
disconnect( mCanvas, &QgsMapCanvas::extentsChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
disconnect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
disconnect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
disconnect( mTableModel, &QgsAttributeTableModel::finished, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
break;
case ShowAll:
case ShowEdited:
case ShowSelected:
break;
case ShowFilteredList:
disconnect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::filterFeatures );
disconnect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedFilterFeatures );
disconnect( mTableModel, &QgsAttributeTableModel::finished, this, &QgsAttributeTableFilterModel::startTimedFilterFeatures );
disconnect( layer(), &QgsVectorLayer::attributeValueChanged, this, &QgsAttributeTableFilterModel::onAttributeValueChanged );
disconnect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::onGeometryChanged );
disconnect( mTableModel, &QgsAttributeTableModel::finished, this, &QgsAttributeTableFilterModel::filterFeatures );
break;
}

// setup new connections
switch ( filterMode )
{
case ShowVisible:
connect( mCanvas, &QgsMapCanvas::extentsChanged, this, &QgsAttributeTableFilterModel::reloadVisible );
connect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::reloadVisible );
connect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::reloadVisible );
connect( mTableModel, &QgsAttributeTableModel::finished, this, &QgsAttributeTableFilterModel::reloadVisible );
connect( mCanvas, &QgsMapCanvas::extentsChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
connect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
connect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
connect( mTableModel, &QgsAttributeTableModel::finished, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
generateListOfVisibleFeatures();
break;
case ShowAll:
case ShowEdited:
case ShowSelected:
break;
case ShowFilteredList:
connect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::filterFeatures );
connect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedFilterFeatures );
connect( mTableModel, &QgsAttributeTableModel::finished, this, &QgsAttributeTableFilterModel::startTimedFilterFeatures );
connect( layer(), &QgsVectorLayer::attributeValueChanged, this, &QgsAttributeTableFilterModel::onAttributeValueChanged );
connect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::onGeometryChanged );
connect( mTableModel, &QgsAttributeTableModel::finished, this, &QgsAttributeTableFilterModel::filterFeatures );
break;
}

@@ -420,6 +425,7 @@ void QgsAttributeTableFilterModel::reloadVisible()
{
generateListOfVisibleFeatures();
invalidateFilter();
emit visibleReloaded();
}

void QgsAttributeTableFilterModel::onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
@@ -429,18 +435,28 @@ void QgsAttributeTableFilterModel::onAttributeValueChanged( QgsFeatureId fid, in

if ( mFilterExpression.referencedAttributeIndexes( layer()->fields() ).contains( idx ) )
{
filterFeatures();
startTimedFilterFeatures();
}
}

void QgsAttributeTableFilterModel::onGeometryChanged()
{
if ( mFilterExpression.needsGeometry() )
{
filterFeatures();
startTimedFilterFeatures();
}
}

void QgsAttributeTableFilterModel::startTimedReloadVisible()
{
mReloadVisibleTimer.start( 10 );
}

void QgsAttributeTableFilterModel::startTimedFilterFeatures()
{
mFilterFeaturesTimer.start( 10 );
}

void QgsAttributeTableFilterModel::filterFeatures()
{
if ( !mFilterExpression.isValid() )
@@ -490,6 +506,8 @@ void QgsAttributeTableFilterModel::filterFeatures()
setFilteredFeatures( filteredFeatures );

QApplication::restoreOverrideCursor();

emit featuresFiltered();
}


@@ -238,6 +238,16 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
*/
void sortColumnChanged( int column, Qt::SortOrder order );

/**
* Emitted when the filtering of the features has been done
*/
void featuresFiltered();

/**
* Emitted when the the visible features on extend are reloaded (the list is created)
*/
void visibleReloaded();

protected:

/**
@@ -300,6 +310,10 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
int mapColumnToSource( int column ) const;
int mapColumnFromSource( int column ) const;

QTimer mReloadVisibleTimer;
QTimer mFilterFeaturesTimer;
void startTimedReloadVisible();
void startTimedFilterFeatures();
};

#endif
@@ -164,14 +164,19 @@ void TestQgsDualView::testFilterSelected()

void TestQgsDualView::testSelectAll()
{

QEventLoop loop;
connect( qobject_cast<QgsAttributeTableFilterModel *>( mDualView->mFilterModel ), &QgsAttributeTableFilterModel::visibleReloaded, &loop, &QEventLoop::quit );
mDualView->setFilterMode( QgsAttributeTableFilterModel::ShowVisible );
// Only show parts of the canvas, so only one selected feature is visible
mCanvas->setExtent( QgsRectangle( -139, 23, -100, 48 ) );
loop.exec();
mDualView->mTableView->selectAll();
QCOMPARE( mPointsLayer->selectedFeatureCount(), 10 );

mPointsLayer->selectByIds( QgsFeatureIds() );
mCanvas->setExtent( QgsRectangle( -110, 40, -100, 48 ) );
loop.exec();
mDualView->mTableView->selectAll();
QCOMPARE( mPointsLayer->selectedFeatureCount(), 1 );
}

0 comments on commit 0290b8c

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