@@ -26,10 +26,7 @@ QgsLayerTreeViewFilterIndicatorManager::QgsLayerTreeViewFilterIndicatorManager(
26
26
: QObject( view )
27
27
, mLayerTreeView( view )
28
28
{
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" ) );
33
30
34
31
QgsLayerTree *tree = mLayerTreeView ->layerTreeModel ()->rootGroup ();
35
32
onAddedChildren ( tree, 0 , tree->children ().count () - 1 );
@@ -154,12 +151,57 @@ void QgsLayerTreeViewFilterIndicatorManager::onIndicatorClicked( const QModelInd
154
151
vlayer->dataProvider ()->setSubsetString ( qb.sql () );
155
152
}
156
153
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
+
157
169
158
170
void QgsLayerTreeViewFilterIndicatorManager::addOrRemoveIndicator ( QgsLayerTreeNode *node, QgsVectorDataProvider *provider )
159
171
{
160
172
QString filter = provider->subsetString ();
161
173
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
+ }
163
190
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
+ }
165
207
}
0 commit comments