Skip to content
Permalink
Browse files

Use subsetStringChange signal for layer tree view filter indicator

  • Loading branch information
nirvn committed May 30, 2018
1 parent 0a3bef1 commit 377f393c6707746656668f33a7117f3a7ed973c7
Showing with 21 additions and 21 deletions.
  1. +18 −18 src/app/qgslayertreeviewfilterindicator.cpp
  2. +3 −3 src/app/qgslayertreeviewfilterindicator.h
@@ -54,11 +54,11 @@ void QgsLayerTreeViewFilterIndicatorProvider::onAddedChildren( QgsLayerTreeNode
QgsLayerTreeLayer *childLayerNode = QgsLayerTree::toLayer( childNode );
if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( childLayerNode->layer() ) )
{
if ( vlayer->dataProvider() )
if ( vlayer )
{
connect( vlayer->dataProvider(), &QgsDataProvider::dataChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onProviderDataChanged );
connect( vlayer, &QgsVectorLayer::subsetStringChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onSubsetStringChanged );

addOrRemoveIndicator( childLayerNode, vlayer->dataProvider() );
addOrRemoveIndicator( childLayerNode, vlayer );
}
}
else if ( !childLayerNode->layer() )
@@ -89,8 +89,8 @@ void QgsLayerTreeViewFilterIndicatorProvider::onWillRemoveChildren( QgsLayerTree
QgsLayerTreeLayer *childLayerNode = QgsLayerTree::toLayer( childNode );
if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( childLayerNode->layer() ) )
{
if ( vlayer->dataProvider() )
disconnect( vlayer->dataProvider(), &QgsDataProvider::dataChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onProviderDataChanged );
if ( vlayer )
disconnect( vlayer, &QgsVectorLayer::subsetStringChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onSubsetStringChanged );
}
}
}
@@ -105,29 +105,29 @@ void QgsLayerTreeViewFilterIndicatorProvider::onLayerLoaded()

if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( nodeLayer->layer() ) )
{
if ( vlayer->dataProvider() )
if ( vlayer )
{
connect( vlayer->dataProvider(), &QgsDataProvider::dataChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onProviderDataChanged );
connect( vlayer, &QgsVectorLayer::subsetStringChanged, this, &QgsLayerTreeViewFilterIndicatorProvider::onSubsetStringChanged );

addOrRemoveIndicator( nodeLayer, vlayer->dataProvider() );
addOrRemoveIndicator( nodeLayer, vlayer );
}
}
}


void QgsLayerTreeViewFilterIndicatorProvider::onProviderDataChanged()
void QgsLayerTreeViewFilterIndicatorProvider::onSubsetStringChanged()
{
QgsVectorDataProvider *provider = qobject_cast<QgsVectorDataProvider *>( sender() );
if ( !provider )
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( sender() );
if ( !vlayer )
return;

// walk the tree and find layer node that needs to be updated
const QList<QgsLayerTreeLayer *> layerNodes = mLayerTreeView->layerTreeModel()->rootGroup()->findLayers();
for ( QgsLayerTreeLayer *node : layerNodes )
{
if ( node->layer() && node->layer()->dataProvider() == provider )
if ( node->layer() && node->layer() == vlayer )
{
addOrRemoveIndicator( node, provider );
addOrRemoveIndicator( node, vlayer );
break;
}
}
@@ -141,14 +141,14 @@ void QgsLayerTreeViewFilterIndicatorProvider::onIndicatorClicked( const QModelIn
return;

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( QgsLayerTree::toLayer( node )->layer() );
if ( !vlayer || !vlayer->dataProvider() )
if ( !vlayer )
return;

// launch the query builder
QgsQueryBuilder qb( vlayer );
qb.setSql( vlayer->dataProvider()->subsetString() );
qb.setSql( vlayer->subsetString() );
if ( qb.exec() )
vlayer->dataProvider()->setSubsetString( qb.sql() );
vlayer->setSubsetString( qb.sql() );
}

QgsLayerTreeViewIndicator *QgsLayerTreeViewFilterIndicatorProvider::newIndicator( const QString &filter )
@@ -167,9 +167,9 @@ void QgsLayerTreeViewFilterIndicatorProvider::updateIndicator( QgsLayerTreeViewI
}


void QgsLayerTreeViewFilterIndicatorProvider::addOrRemoveIndicator( QgsLayerTreeNode *node, QgsVectorDataProvider *provider )
void QgsLayerTreeViewFilterIndicatorProvider::addOrRemoveIndicator( QgsLayerTreeNode *node, QgsVectorLayer *vlayer )
{
QString filter = provider->subsetString();
QString filter = vlayer->subsetString();
if ( !filter.isEmpty() )
{
const QList<QgsLayerTreeViewIndicator *> nodeIndicators = mLayerTreeView->indicators( node );
@@ -22,7 +22,7 @@

class QgsLayerTreeNode;
class QgsLayerTreeView;
class QgsVectorDataProvider;
class QgsVectorLayer;


//! Adds indicators showing whether vector layers have a filter applied.
@@ -40,14 +40,14 @@ class QgsLayerTreeViewFilterIndicatorProvider : public QObject
//! Starts listening to layer provider's dataChanged signal
void onLayerLoaded();
//! Adds/removes indicator of a layer
void onProviderDataChanged();
void onSubsetStringChanged();

void onIndicatorClicked( const QModelIndex &index );

private:
QgsLayerTreeViewIndicator *newIndicator( const QString &filter );
void updateIndicator( QgsLayerTreeViewIndicator *indicator, const QString &filter );
void addOrRemoveIndicator( QgsLayerTreeNode *node, QgsVectorDataProvider *provider );
void addOrRemoveIndicator( QgsLayerTreeNode *node, QgsVectorLayer *vlayer );

private:
QgsLayerTreeView *mLayerTreeView;

0 comments on commit 377f393

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