Skip to content

Commit 189707f

Browse files
committed
[layertree] move implementation of legend actions to QgsAppLayerTreeViewMenuProvider
1 parent fa5f68f commit 189707f

5 files changed

Lines changed: 116 additions & 108 deletions

File tree

‎src/app/legend/qgsapplegendinterface.cpp‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "qgsapplegendinterface.h"
1818

19+
#include "qgsapplayertreeviewmenuprovider.h"
1920
#include "qgslayertree.h"
2021
#include "qgslayertreemodel.h"
2122
#include "qgslayertreeview.h"
@@ -301,17 +302,29 @@ void QgsAppLegendInterface::onRemovedChildren()
301302
void QgsAppLegendInterface::addLegendLayerAction( QAction* action,
302303
QString menu, QString id, QgsMapLayer::LayerType type, bool allLayers )
303304
{
304-
QgsProject::instance()->layerTreeRegistryBridge()->addLegendLayerAction( action, menu, id, type, allLayers );
305+
QgsAppLayerTreeViewMenuProvider* menuProvider = dynamic_cast<QgsAppLayerTreeViewMenuProvider*>(mLayerTreeView->menuProvider());
306+
if (!menuProvider)
307+
return;
308+
309+
menuProvider->addLegendLayerAction( action, menu, id, type, allLayers );
305310
}
306311

307312
void QgsAppLegendInterface::addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer )
308313
{
309-
QgsProject::instance()->layerTreeRegistryBridge()->addLegendLayerActionForLayer( action, layer );
314+
QgsAppLayerTreeViewMenuProvider* menuProvider = dynamic_cast<QgsAppLayerTreeViewMenuProvider*>(mLayerTreeView->menuProvider());
315+
if (!menuProvider)
316+
return;
317+
318+
menuProvider->addLegendLayerActionForLayer( action, layer );
310319
}
311320

312321
bool QgsAppLegendInterface::removeLegendLayerAction( QAction* action )
313322
{
314-
return QgsProject::instance()->layerTreeRegistryBridge()->removeLegendLayerAction( action );
323+
QgsAppLayerTreeViewMenuProvider* menuProvider = dynamic_cast<QgsAppLayerTreeViewMenuProvider*>(mLayerTreeView->menuProvider());
324+
if (!menuProvider)
325+
return false;
326+
327+
menuProvider->removeLegendLayerAction( action );
315328
}
316329

317330
QgsMapLayer* QgsAppLegendInterface::currentLayer()

‎src/app/qgsapplayertreeviewmenuprovider.cpp‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
143143
}
144144

145145
// add custom layer actions - should this go at end?
146-
QList< LegendLayerAction > lyrActions =
147-
QgsProject::instance()->layerTreeRegistryBridge()->legendLayerActions( layer->type() );
146+
QList< LegendLayerAction > lyrActions = legendLayerActions( layer->type() );
148147

149148
if ( ! lyrActions.isEmpty() )
150149
{
@@ -236,3 +235,76 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
236235
return menu;
237236
}
238237

238+
239+
240+
void QgsAppLayerTreeViewMenuProvider::addLegendLayerAction( QAction* action, QString menu, QString id,
241+
QgsMapLayer::LayerType type, bool allLayers )
242+
{
243+
mLegendLayerActionMap[type].append( LegendLayerAction( action, menu, id, allLayers ) );
244+
}
245+
246+
bool QgsAppLayerTreeViewMenuProvider::removeLegendLayerAction( QAction* action )
247+
{
248+
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it;
249+
for ( it = mLegendLayerActionMap.begin();
250+
it != mLegendLayerActionMap.end(); ++it )
251+
{
252+
for ( int i = 0; i < it->count(); i++ )
253+
{
254+
if (( *it )[i].action == action )
255+
{
256+
( *it ).removeAt( i );
257+
return true;
258+
}
259+
}
260+
}
261+
return false;
262+
}
263+
264+
void QgsAppLayerTreeViewMenuProvider::addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer )
265+
{
266+
legendLayerActions( layer->type() );
267+
if ( !action || !layer || ! mLegendLayerActionMap.contains( layer->type() ) )
268+
return;
269+
270+
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it
271+
= mLegendLayerActionMap.find( layer->type() );
272+
for ( int i = 0; i < it->count(); i++ )
273+
{
274+
if ( ( *it )[i].action == action )
275+
{
276+
( *it )[i].layers.append( layer );
277+
return;
278+
}
279+
}
280+
}
281+
282+
void QgsAppLayerTreeViewMenuProvider::removeLegendLayerActionsForLayer( QgsMapLayer* layer )
283+
{
284+
if ( ! layer || ! mLegendLayerActionMap.contains( layer->type() ) )
285+
return;
286+
287+
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it
288+
= mLegendLayerActionMap.find( layer->type() );
289+
for ( int i = 0; i < it->count(); i++ )
290+
{
291+
( *it )[i].layers.removeAll( layer );
292+
}
293+
}
294+
295+
QList< LegendLayerAction > QgsAppLayerTreeViewMenuProvider::legendLayerActions( QgsMapLayer::LayerType type ) const
296+
{
297+
#ifdef QGISDEBUG
298+
if ( mLegendLayerActionMap.contains( type ) )
299+
{
300+
QgsDebugMsg( QString("legendLayerActions for layers of type %1:").arg( type ) );
301+
302+
foreach ( LegendLayerAction lyrAction, mLegendLayerActionMap[ type ] )
303+
{
304+
QgsDebugMsg( QString("%1/%2 - %3 layers").arg( lyrAction.menu ).arg( lyrAction.action->text() ).arg( lyrAction.layers.count()) );
305+
}
306+
}
307+
#endif
308+
309+
return mLegendLayerActionMap.contains( type ) ? mLegendLayerActionMap.value( type ) : QList< LegendLayerAction >() ;
310+
}

‎src/app/qgsapplayertreeviewmenuprovider.h‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@
44
#include <QObject>
55

66
#include "qgslayertreeview.h"
7+
#include "qgsmaplayer.h"
78

8-
class QgsMapCanvas;
9+
class QAction;
10+
11+
struct LegendLayerAction
12+
{
13+
LegendLayerAction( QAction* a, QString m, QString i, bool all )
14+
: action( a ), menu( m ), id( i ), allLayers( all ) {}
15+
QAction* action;
16+
QString menu;
17+
QString id;
18+
bool allLayers;
19+
QList<QgsMapLayer*> layers;
20+
};
921

22+
class QgsMapCanvas;
1023

1124
class QgsAppLayerTreeViewMenuProvider : public QObject, public QgsLayerTreeViewMenuProvider
1225
{
@@ -15,9 +28,19 @@ class QgsAppLayerTreeViewMenuProvider : public QObject, public QgsLayerTreeViewM
1528

1629
QMenu* createContextMenu();
1730

31+
void addLegendLayerAction( QAction* action, QString menu, QString id,
32+
QgsMapLayer::LayerType type, bool allLayers );
33+
bool removeLegendLayerAction( QAction* action );
34+
void addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer );
35+
void removeLegendLayerActionsForLayer( QgsMapLayer* layer );
36+
QList< LegendLayerAction > legendLayerActions( QgsMapLayer::LayerType type ) const;
37+
38+
1839
protected:
1940
QgsLayerTreeView* mView;
2041
QgsMapCanvas* mCanvas;
42+
43+
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > > mLegendLayerActionMap;
2144
};
2245

2346

‎src/core/layertree/qgslayertreeregistrybridge.cpp‎

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "qgsproject.h"
2323
#include "qgslogger.h"
24-
#include <QAction>
2524

2625
QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge( QgsLayerTreeGroup *root, QObject *parent )
2726
: QObject( parent )
@@ -72,10 +71,6 @@ void QgsLayerTreeRegistryBridge::layersWillBeRemoved( QStringList layerIds )
7271

7372
foreach ( QString layerId, layerIds )
7473
{
75-
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
76-
if ( mapLayer )
77-
removeLegendLayerActionsForLayer( mapLayer );
78-
7974
QgsLayerTreeLayer* nodeLayer = mRoot->findLayer( layerId );
8075
if ( nodeLayer )
8176
qobject_cast<QgsLayerTreeGroup*>( nodeLayer->parent() )->removeChildNode( nodeLayer );
@@ -123,75 +118,3 @@ void QgsLayerTreeRegistryBridge::groupRemovedChildren()
123118

124119
QgsMapLayerRegistry::instance()->removeMapLayers( toRemove );
125120
}
126-
127-
void QgsLayerTreeRegistryBridge::addLegendLayerAction( QAction* action, QString menu, QString id,
128-
QgsMapLayer::LayerType type, bool allLayers )
129-
{
130-
mLegendLayerActionMap[type].append( LegendLayerAction( action, menu, id, allLayers ) );
131-
}
132-
133-
bool QgsLayerTreeRegistryBridge::removeLegendLayerAction( QAction* action )
134-
{
135-
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it;
136-
for ( it = mLegendLayerActionMap.begin();
137-
it != mLegendLayerActionMap.end(); ++it )
138-
{
139-
for ( int i = 0; i < it->count(); i++ )
140-
{
141-
if (( *it )[i].action == action )
142-
{
143-
( *it ).removeAt( i );
144-
return true;
145-
}
146-
}
147-
}
148-
return false;
149-
}
150-
151-
void QgsLayerTreeRegistryBridge::addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer )
152-
{
153-
legendLayerActions( layer->type() );
154-
if ( !action || !layer || ! mLegendLayerActionMap.contains( layer->type() ) )
155-
return;
156-
157-
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it
158-
= mLegendLayerActionMap.find( layer->type() );
159-
for ( int i = 0; i < it->count(); i++ )
160-
{
161-
if ( ( *it )[i].action == action )
162-
{
163-
( *it )[i].layers.append( layer );
164-
return;
165-
}
166-
}
167-
}
168-
169-
void QgsLayerTreeRegistryBridge::removeLegendLayerActionsForLayer( QgsMapLayer* layer )
170-
{
171-
if ( ! layer || ! mLegendLayerActionMap.contains( layer->type() ) )
172-
return;
173-
174-
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it
175-
= mLegendLayerActionMap.find( layer->type() );
176-
for ( int i = 0; i < it->count(); i++ )
177-
{
178-
( *it )[i].layers.removeAll( layer );
179-
}
180-
}
181-
182-
QList< LegendLayerAction > QgsLayerTreeRegistryBridge::legendLayerActions( QgsMapLayer::LayerType type ) const
183-
{
184-
#ifdef QGISDEBUG
185-
if ( mLegendLayerActionMap.contains( type ) )
186-
{
187-
QgsDebugMsg( QString("legendLayerActions for layers of type %1:").arg( type ) );
188-
189-
foreach ( LegendLayerAction lyrAction, mLegendLayerActionMap[ type ] )
190-
{
191-
QgsDebugMsg( QString("%1/%2 - %3 layers").arg( lyrAction.menu ).arg( lyrAction.action->text() ).arg( lyrAction.layers.count()) );
192-
}
193-
}
194-
#endif
195-
196-
return mLegendLayerActionMap.contains( type ) ? mLegendLayerActionMap.value( type ) : QList< LegendLayerAction >() ;
197-
}

‎src/core/layertree/qgslayertreeregistrybridge.h‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,10 @@
1919
#include <QObject>
2020
#include <QStringList>
2121

22-
class QAction;
23-
2422
class QgsLayerTreeGroup;
2523
class QgsLayerTreeNode;
24+
class QgsMapLayer;
2625

27-
#include "qgsmaplayer.h"
28-
29-
struct LegendLayerAction
30-
{
31-
LegendLayerAction( QAction* a, QString m, QString i, bool all )
32-
: action( a ), menu( m ), id( i ), allLayers( all ) {}
33-
QAction* action;
34-
QString menu;
35-
QString id;
36-
bool allLayers;
37-
QList<QgsMapLayer*> layers;
38-
};
3926

4027
/**
4128
* Listens to the updates in map layer registry and does changes in layer tree.
@@ -62,14 +49,6 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
6249
//! By default it is root group with zero index.
6350
void setLayerInsertionPoint( QgsLayerTreeGroup* parentGroup, int index );
6451

65-
void addLegendLayerAction( QAction* action, QString menu, QString id,
66-
QgsMapLayer::LayerType type, bool allLayers );
67-
bool removeLegendLayerAction( QAction* action );
68-
void addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer );
69-
void removeLegendLayerActionsForLayer( QgsMapLayer* layer );
70-
QList< LegendLayerAction > legendLayerActions( QgsMapLayer::LayerType type ) const;
71-
72-
7352
signals:
7453

7554
protected slots:
@@ -85,9 +64,7 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
8564
bool mEnabled;
8665

8766
QgsLayerTreeGroup* mInsertionPointGroup;
88-
int mInsertionPointIndex;
89-
90-
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > > mLegendLayerActionMap;
67+
int mInsertionPointIndex;
9168
};
9269

9370
#endif // QGSLAYERTREEREGISTRYBRIDGE_H

0 commit comments

Comments
 (0)