Skip to content

Commit 8190930

Browse files
committedFeb 26, 2018
Show filter query in the indicator's tooltip
1 parent f56d70f commit 8190930

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed
 

‎src/app/qgslayertreeviewfilterindicator.cpp

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ QgsLayerTreeViewFilterIndicatorManager::QgsLayerTreeViewFilterIndicatorManager(
2626
: QObject( view )
2727
, mLayerTreeView( view )
2828
{
29-
mIndicator = new QgsLayerTreeViewIndicator( this );
30-
mIndicator->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFilter2.svg" ) ) );
31-
mIndicator->setToolTip( "Filtered" );
32-
connect( mIndicator, &QgsLayerTreeViewIndicator::clicked, this, &QgsLayerTreeViewFilterIndicatorManager::onIndicatorClicked );
29+
mIcon = QgsApplication::getThemeIcon( QStringLiteral( "/mActionFilter2.svg" ) );
3330

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

154+
QgsLayerTreeViewIndicator *QgsLayerTreeViewFilterIndicatorManager::newIndicator( const QString &filter )
155+
{
156+
QgsLayerTreeViewIndicator *indicator = new QgsLayerTreeViewIndicator( this );
157+
indicator->setIcon( mIcon );
158+
updateIndicator( indicator, filter );
159+
connect( indicator, &QgsLayerTreeViewIndicator::clicked, this, &QgsLayerTreeViewFilterIndicatorManager::onIndicatorClicked );
160+
mIndicators.insert( indicator );
161+
return indicator;
162+
}
163+
164+
void QgsLayerTreeViewFilterIndicatorManager::updateIndicator( QgsLayerTreeViewIndicator *indicator, const QString &filter )
165+
{
166+
indicator->setToolTip( QString( "<b>%1:</b><br>%2" ).arg( tr( "Filter" ) ).arg( filter ) );
167+
}
168+
157169

158170
void QgsLayerTreeViewFilterIndicatorManager::addOrRemoveIndicator( QgsLayerTreeNode *node, QgsVectorDataProvider *provider )
159171
{
160172
QString filter = provider->subsetString();
161173
if ( !filter.isEmpty() )
162-
mLayerTreeView->addIndicator( node, mIndicator );
174+
{
175+
const QList<QgsLayerTreeViewIndicator *> nodeIndicators = mLayerTreeView->indicators( node );
176+
177+
// maybe the indicator exists already
178+
foreach ( QgsLayerTreeViewIndicator *indicator, nodeIndicators )
179+
{
180+
if ( mIndicators.contains( indicator ) )
181+
{
182+
updateIndicator( indicator, filter );
183+
return;
184+
}
185+
}
186+
187+
// it does not exist: need to create a new one
188+
mLayerTreeView->addIndicator( node, newIndicator( filter ) );
189+
}
163190
else
164-
mLayerTreeView->removeIndicator( node, mIndicator );
191+
{
192+
const QList<QgsLayerTreeViewIndicator *> nodeIndicators = mLayerTreeView->indicators( node );
193+
194+
// there may be existing indicator we need to get rid of
195+
foreach ( QgsLayerTreeViewIndicator *indicator, nodeIndicators )
196+
{
197+
if ( mIndicators.contains( indicator ) )
198+
{
199+
mLayerTreeView->removeIndicator( node, indicator );
200+
indicator->deleteLater();
201+
return;
202+
}
203+
}
204+
205+
// no indicator was there before, nothing to do
206+
}
165207
}

‎src/app/qgslayertreeviewfilterindicator.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "qgslayertreeviewindicator.h"
2020

21+
#include <QSet>
22+
2123
class QgsLayerTreeNode;
2224
class QgsLayerTreeView;
2325
class QgsVectorDataProvider;
@@ -43,11 +45,14 @@ class QgsLayerTreeViewFilterIndicatorManager : public QObject
4345
void onIndicatorClicked( const QModelIndex &index );
4446

4547
private:
48+
QgsLayerTreeViewIndicator *newIndicator( const QString &filter );
49+
void updateIndicator( QgsLayerTreeViewIndicator *indicator, const QString &filter );
4650
void addOrRemoveIndicator( QgsLayerTreeNode *node, QgsVectorDataProvider *provider );
4751

4852
private:
4953
QgsLayerTreeView *mLayerTreeView;
50-
QgsLayerTreeViewIndicator *mIndicator = nullptr;
54+
QIcon mIcon;
55+
QSet<QgsLayerTreeViewIndicator *> mIndicators;
5156
};
5257

5358
#endif // QGSLAYERTREEVIEWFILTERINDICATOR_H

0 commit comments

Comments
 (0)
Please sign in to comment.