Skip to content

Commit

Permalink
Show filter query in the indicator's tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Feb 26, 2018
1 parent f56d70f commit 8190930
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
54 changes: 48 additions & 6 deletions src/app/qgslayertreeviewfilterindicator.cpp
Expand Up @@ -26,10 +26,7 @@ QgsLayerTreeViewFilterIndicatorManager::QgsLayerTreeViewFilterIndicatorManager(
: QObject( view )
, mLayerTreeView( view )
{
mIndicator = new QgsLayerTreeViewIndicator( this );
mIndicator->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFilter2.svg" ) ) );
mIndicator->setToolTip( "Filtered" );
connect( mIndicator, &QgsLayerTreeViewIndicator::clicked, this, &QgsLayerTreeViewFilterIndicatorManager::onIndicatorClicked );
mIcon = QgsApplication::getThemeIcon( QStringLiteral( "/mActionFilter2.svg" ) );

QgsLayerTree *tree = mLayerTreeView->layerTreeModel()->rootGroup();
onAddedChildren( tree, 0, tree->children().count() - 1 );
Expand Down Expand Up @@ -154,12 +151,57 @@ void QgsLayerTreeViewFilterIndicatorManager::onIndicatorClicked( const QModelInd
vlayer->dataProvider()->setSubsetString( qb.sql() );
}

QgsLayerTreeViewIndicator *QgsLayerTreeViewFilterIndicatorManager::newIndicator( const QString &filter )
{
QgsLayerTreeViewIndicator *indicator = new QgsLayerTreeViewIndicator( this );
indicator->setIcon( mIcon );
updateIndicator( indicator, filter );
connect( indicator, &QgsLayerTreeViewIndicator::clicked, this, &QgsLayerTreeViewFilterIndicatorManager::onIndicatorClicked );
mIndicators.insert( indicator );
return indicator;
}

void QgsLayerTreeViewFilterIndicatorManager::updateIndicator( QgsLayerTreeViewIndicator *indicator, const QString &filter )
{
indicator->setToolTip( QString( "<b>%1:</b><br>%2" ).arg( tr( "Filter" ) ).arg( filter ) );
}


void QgsLayerTreeViewFilterIndicatorManager::addOrRemoveIndicator( QgsLayerTreeNode *node, QgsVectorDataProvider *provider )
{
QString filter = provider->subsetString();
if ( !filter.isEmpty() )
mLayerTreeView->addIndicator( node, mIndicator );
{
const QList<QgsLayerTreeViewIndicator *> nodeIndicators = mLayerTreeView->indicators( node );

// maybe the indicator exists already
foreach ( QgsLayerTreeViewIndicator *indicator, nodeIndicators )
{
if ( mIndicators.contains( indicator ) )
{
updateIndicator( indicator, filter );
return;
}
}

// it does not exist: need to create a new one
mLayerTreeView->addIndicator( node, newIndicator( filter ) );
}
else
mLayerTreeView->removeIndicator( node, mIndicator );
{
const QList<QgsLayerTreeViewIndicator *> nodeIndicators = mLayerTreeView->indicators( node );

// there may be existing indicator we need to get rid of
foreach ( QgsLayerTreeViewIndicator *indicator, nodeIndicators )
{
if ( mIndicators.contains( indicator ) )
{
mLayerTreeView->removeIndicator( node, indicator );
indicator->deleteLater();
return;
}
}

// no indicator was there before, nothing to do
}
}
7 changes: 6 additions & 1 deletion src/app/qgslayertreeviewfilterindicator.h
Expand Up @@ -18,6 +18,8 @@

#include "qgslayertreeviewindicator.h"

#include <QSet>

class QgsLayerTreeNode;
class QgsLayerTreeView;
class QgsVectorDataProvider;
Expand All @@ -43,11 +45,14 @@ class QgsLayerTreeViewFilterIndicatorManager : public QObject
void onIndicatorClicked( const QModelIndex &index );

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

private:
QgsLayerTreeView *mLayerTreeView;
QgsLayerTreeViewIndicator *mIndicator = nullptr;
QIcon mIcon;
QSet<QgsLayerTreeViewIndicator *> mIndicators;
};

#endif // QGSLAYERTREEVIEWFILTERINDICATOR_H

0 comments on commit 8190930

Please sign in to comment.