Skip to content
Permalink
Browse files

Show filter query in the indicator's tooltip

  • Loading branch information
wonder-sk committed Feb 26, 2018
1 parent f56d70f commit 8190930bb1152fd2bcfe2b4d093c875367ff30db
Showing with 54 additions and 7 deletions.
  1. +48 −6 src/app/qgslayertreeviewfilterindicator.cpp
  2. +6 −1 src/app/qgslayertreeviewfilterindicator.h
@@ -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 );
@@ -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
}
}
@@ -18,6 +18,8 @@

#include "qgslayertreeviewindicator.h"

#include <QSet>

class QgsLayerTreeNode;
class QgsLayerTreeView;
class QgsVectorDataProvider;
@@ -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.
You can’t perform that action at this time.