Skip to content

Commit fa5f68f

Browse files
etienneskywonder-sk
authored andcommitted
restore addLegendLayerAction and related functions after legend redesign
1 parent a1948e6 commit fa5f68f

File tree

4 files changed

+175
-9
lines changed

4 files changed

+175
-9
lines changed

src/app/legend/qgsapplegendinterface.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "qgslayertreemodel.h"
2121
#include "qgslayertreeview.h"
2222
#include "qgsmaplayer.h"
23+
#include "qgsproject.h"
24+
#include "qgslayertreeregistrybridge.h"
2325

2426

2527
QgsAppLegendInterface::QgsAppLegendInterface( QgsLayerTreeView * layerTreeView )
@@ -299,18 +301,17 @@ void QgsAppLegendInterface::onRemovedChildren()
299301
void QgsAppLegendInterface::addLegendLayerAction( QAction* action,
300302
QString menu, QString id, QgsMapLayer::LayerType type, bool allLayers )
301303
{
302-
// TODO[MD] mLegend->addLegendLayerAction( action, menu, id, type, allLayers );
304+
QgsProject::instance()->layerTreeRegistryBridge()->addLegendLayerAction( action, menu, id, type, allLayers );
303305
}
304306

305307
void QgsAppLegendInterface::addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer )
306308
{
307-
// TODO[MD] mLegend->addLegendLayerActionForLayer( action, layer );
309+
QgsProject::instance()->layerTreeRegistryBridge()->addLegendLayerActionForLayer( action, layer );
308310
}
309311

310312
bool QgsAppLegendInterface::removeLegendLayerAction( QAction* action )
311313
{
312-
// TODO[MD] return mLegend->removeLegendLayerAction( action );
313-
return false;
314+
return QgsProject::instance()->layerTreeRegistryBridge()->removeLegendLayerAction( action );
314315
}
315316

316317
QgsMapLayer* QgsAppLegendInterface::currentLayer()

src/app/qgsapplayertreeviewmenuprovider.cpp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "qgsrasterlayer.h"
1212
#include "qgsvectordataprovider.h"
1313
#include "qgsvectorlayer.h"
14+
#include "qgslayertreeregistrybridge.h"
1415

1516

1617
QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView* view, QgsMapCanvas* canvas )
@@ -141,7 +142,68 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
141142
duplicateLayersAction->setEnabled( false );
142143
}
143144

144-
// TODO: custom actions
145+
// add custom layer actions - should this go at end?
146+
QList< LegendLayerAction > lyrActions =
147+
QgsProject::instance()->layerTreeRegistryBridge()->legendLayerActions( layer->type() );
148+
149+
if ( ! lyrActions.isEmpty() )
150+
{
151+
menu->addSeparator();
152+
QList<QMenu*> theMenus;
153+
for ( int i = 0; i < lyrActions.count(); i++ )
154+
{
155+
if ( lyrActions[i].allLayers || lyrActions[i].layers.contains( layer ) )
156+
{
157+
if ( lyrActions[i].menu.isEmpty() )
158+
{
159+
menu->addAction( lyrActions[i].action );
160+
}
161+
else
162+
{
163+
// find or create menu for given menu name
164+
// adapted from QgisApp::getPluginMenu( QString menuName )
165+
QString menuName = lyrActions[i].menu;
166+
#ifdef Q_WS_MAC
167+
// Mac doesn't have '&' keyboard shortcuts.
168+
menuName.remove( QChar( '&' ) );
169+
#endif
170+
QAction* before = 0;
171+
QMenu* newMenu = 0;
172+
QString dst = menuName;
173+
dst.remove( QChar( '&' ) );
174+
foreach ( QMenu* menu, theMenus )
175+
{
176+
QString src = menu->title();
177+
src.remove( QChar( '&' ) );
178+
int comp = dst.localeAwareCompare( src );
179+
if ( comp < 0 )
180+
{
181+
// Add item before this one
182+
before = menu->menuAction();
183+
break;
184+
}
185+
else if ( comp == 0 )
186+
{
187+
// Plugin menu item already exists
188+
newMenu = menu;
189+
break;
190+
}
191+
}
192+
if ( ! newMenu )
193+
{
194+
// It doesn't exist, so create
195+
newMenu = new QMenu( menuName );
196+
theMenus.append( newMenu );
197+
// Where to put it? - we worked that out above...
198+
menu->insertMenu( before, newMenu );
199+
}
200+
// QMenu* menu = getMenu( lyrActions[i].menu, &theBeforeSep, &theAfterSep, &theMenu );
201+
newMenu->addAction( lyrActions[i].action );
202+
}
203+
}
204+
}
205+
menu->addSeparator();
206+
}
145207

146208
if ( layer && QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
147209
menu->addAction( tr( "&Properties" ), QgisApp::instance(), SLOT( layerProperties() ) );

src/core/layertree/qgslayertreeregistrybridge.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "qgslayertree.h"
2121

2222
#include "qgsproject.h"
23+
#include "qgslogger.h"
24+
#include <QAction>
2325

2426
QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge( QgsLayerTreeGroup *root, QObject *parent )
2527
: QObject( parent )
@@ -63,11 +65,17 @@ void QgsLayerTreeRegistryBridge::layersAdded( QList<QgsMapLayer*> layers )
6365

6466
void QgsLayerTreeRegistryBridge::layersWillBeRemoved( QStringList layerIds )
6567
{
68+
QgsDebugMsg( QString( "%1 layers will be removed, enabled:%2" ).arg( layerIds.count() ).arg( mEnabled ) );
69+
6670
if ( !mEnabled )
6771
return;
6872

6973
foreach ( QString layerId, layerIds )
7074
{
75+
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
76+
if ( mapLayer )
77+
removeLegendLayerActionsForLayer( mapLayer );
78+
7179
QgsLayerTreeLayer* nodeLayer = mRoot->findLayer( layerId );
7280
if ( nodeLayer )
7381
qobject_cast<QgsLayerTreeGroup*>( nodeLayer->parent() )->removeChildNode( nodeLayer );
@@ -111,5 +119,79 @@ void QgsLayerTreeRegistryBridge::groupRemovedChildren()
111119
toRemove << layerId;
112120
mLayerIdsForRemoval.clear();
113121

122+
QgsDebugMsg( QString( "%1 layers will be removed" ).arg( toRemove.count() ) );
123+
114124
QgsMapLayerRegistry::instance()->removeMapLayers( toRemove );
115125
}
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: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,23 @@
1919
#include <QObject>
2020
#include <QStringList>
2121

22+
class QAction;
23+
2224
class QgsLayerTreeGroup;
2325
class QgsLayerTreeNode;
2426

25-
class QgsMapLayer;
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+
};
2639

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

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+
5273
signals:
5374

5475
protected slots:
@@ -58,15 +79,15 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
5879
void groupWillRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo );
5980
void groupRemovedChildren();
6081

61-
protected:
62-
6382
protected:
6483
QgsLayerTreeGroup* mRoot;
6584
QStringList mLayerIdsForRemoval;
6685
bool mEnabled;
6786

6887
QgsLayerTreeGroup* mInsertionPointGroup;
69-
int mInsertionPointIndex;
88+
int mInsertionPointIndex;
89+
90+
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > > mLegendLayerActionMap;
7091
};
7192

7293
#endif // QGSLAYERTREEREGISTRYBRIDGE_H

0 commit comments

Comments
 (0)