Skip to content

Commit

Permalink
restore addLegendLayerAction and related functions after legend redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky authored and wonder-sk committed May 23, 2014
1 parent a1948e6 commit fa5f68f
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/app/legend/qgsapplegendinterface.cpp
Expand Up @@ -20,6 +20,8 @@
#include "qgslayertreemodel.h"
#include "qgslayertreeview.h"
#include "qgsmaplayer.h"
#include "qgsproject.h"
#include "qgslayertreeregistrybridge.h"


QgsAppLegendInterface::QgsAppLegendInterface( QgsLayerTreeView * layerTreeView )
Expand Down Expand Up @@ -299,18 +301,17 @@ void QgsAppLegendInterface::onRemovedChildren()
void QgsAppLegendInterface::addLegendLayerAction( QAction* action,
QString menu, QString id, QgsMapLayer::LayerType type, bool allLayers )
{
// TODO[MD] mLegend->addLegendLayerAction( action, menu, id, type, allLayers );
QgsProject::instance()->layerTreeRegistryBridge()->addLegendLayerAction( action, menu, id, type, allLayers );
}

void QgsAppLegendInterface::addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer )
{
// TODO[MD] mLegend->addLegendLayerActionForLayer( action, layer );
QgsProject::instance()->layerTreeRegistryBridge()->addLegendLayerActionForLayer( action, layer );
}

bool QgsAppLegendInterface::removeLegendLayerAction( QAction* action )
{
// TODO[MD] return mLegend->removeLegendLayerAction( action );
return false;
return QgsProject::instance()->layerTreeRegistryBridge()->removeLegendLayerAction( action );
}

QgsMapLayer* QgsAppLegendInterface::currentLayer()
Expand Down
64 changes: 63 additions & 1 deletion src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -11,6 +11,7 @@
#include "qgsrasterlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgslayertreeregistrybridge.h"


QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView* view, QgsMapCanvas* canvas )
Expand Down Expand Up @@ -141,7 +142,68 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
duplicateLayersAction->setEnabled( false );
}

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

if ( ! lyrActions.isEmpty() )
{
menu->addSeparator();
QList<QMenu*> theMenus;
for ( int i = 0; i < lyrActions.count(); i++ )
{
if ( lyrActions[i].allLayers || lyrActions[i].layers.contains( layer ) )
{
if ( lyrActions[i].menu.isEmpty() )
{
menu->addAction( lyrActions[i].action );
}
else
{
// find or create menu for given menu name
// adapted from QgisApp::getPluginMenu( QString menuName )
QString menuName = lyrActions[i].menu;
#ifdef Q_WS_MAC
// Mac doesn't have '&' keyboard shortcuts.
menuName.remove( QChar( '&' ) );
#endif
QAction* before = 0;
QMenu* newMenu = 0;
QString dst = menuName;
dst.remove( QChar( '&' ) );
foreach ( QMenu* menu, theMenus )
{
QString src = menu->title();
src.remove( QChar( '&' ) );
int comp = dst.localeAwareCompare( src );
if ( comp < 0 )
{
// Add item before this one
before = menu->menuAction();
break;
}
else if ( comp == 0 )
{
// Plugin menu item already exists
newMenu = menu;
break;
}
}
if ( ! newMenu )
{
// It doesn't exist, so create
newMenu = new QMenu( menuName );
theMenus.append( newMenu );
// Where to put it? - we worked that out above...
menu->insertMenu( before, newMenu );
}
// QMenu* menu = getMenu( lyrActions[i].menu, &theBeforeSep, &theAfterSep, &theMenu );
newMenu->addAction( lyrActions[i].action );
}
}
}
menu->addSeparator();
}

if ( layer && QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
menu->addAction( tr( "&Properties" ), QgisApp::instance(), SLOT( layerProperties() ) );
Expand Down
82 changes: 82 additions & 0 deletions src/core/layertree/qgslayertreeregistrybridge.cpp
Expand Up @@ -20,6 +20,8 @@
#include "qgslayertree.h"

#include "qgsproject.h"
#include "qgslogger.h"
#include <QAction>

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

void QgsLayerTreeRegistryBridge::layersWillBeRemoved( QStringList layerIds )
{
QgsDebugMsg( QString( "%1 layers will be removed, enabled:%2" ).arg( layerIds.count() ).arg( mEnabled ) );

if ( !mEnabled )
return;

foreach ( QString layerId, layerIds )
{
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
if ( mapLayer )
removeLegendLayerActionsForLayer( mapLayer );

QgsLayerTreeLayer* nodeLayer = mRoot->findLayer( layerId );
if ( nodeLayer )
qobject_cast<QgsLayerTreeGroup*>( nodeLayer->parent() )->removeChildNode( nodeLayer );
Expand Down Expand Up @@ -111,5 +119,79 @@ void QgsLayerTreeRegistryBridge::groupRemovedChildren()
toRemove << layerId;
mLayerIdsForRemoval.clear();

QgsDebugMsg( QString( "%1 layers will be removed" ).arg( toRemove.count() ) );

QgsMapLayerRegistry::instance()->removeMapLayers( toRemove );
}

void QgsLayerTreeRegistryBridge::addLegendLayerAction( QAction* action, QString menu, QString id,
QgsMapLayer::LayerType type, bool allLayers )
{
mLegendLayerActionMap[type].append( LegendLayerAction( action, menu, id, allLayers ) );
}

bool QgsLayerTreeRegistryBridge::removeLegendLayerAction( QAction* action )
{
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it;
for ( it = mLegendLayerActionMap.begin();
it != mLegendLayerActionMap.end(); ++it )
{
for ( int i = 0; i < it->count(); i++ )
{
if (( *it )[i].action == action )
{
( *it ).removeAt( i );
return true;
}
}
}
return false;
}

void QgsLayerTreeRegistryBridge::addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer )
{
legendLayerActions( layer->type() );
if ( !action || !layer || ! mLegendLayerActionMap.contains( layer->type() ) )
return;

QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it
= mLegendLayerActionMap.find( layer->type() );
for ( int i = 0; i < it->count(); i++ )
{
if ( ( *it )[i].action == action )
{
( *it )[i].layers.append( layer );
return;
}
}
}

void QgsLayerTreeRegistryBridge::removeLegendLayerActionsForLayer( QgsMapLayer* layer )
{
if ( ! layer || ! mLegendLayerActionMap.contains( layer->type() ) )
return;

QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it
= mLegendLayerActionMap.find( layer->type() );
for ( int i = 0; i < it->count(); i++ )
{
( *it )[i].layers.removeAll( layer );
}
}

QList< LegendLayerAction > QgsLayerTreeRegistryBridge::legendLayerActions( QgsMapLayer::LayerType type ) const
{
#ifdef QGISDEBUG
if ( mLegendLayerActionMap.contains( type ) )
{
QgsDebugMsg( QString("legendLayerActions for layers of type %1:").arg( type ) );

foreach ( LegendLayerAction lyrAction, mLegendLayerActionMap[ type ] )
{
QgsDebugMsg( QString("%1/%2 - %3 layers").arg( lyrAction.menu ).arg( lyrAction.action->text() ).arg( lyrAction.layers.count()) );
}
}
#endif

return mLegendLayerActionMap.contains( type ) ? mLegendLayerActionMap.value( type ) : QList< LegendLayerAction >() ;
}
29 changes: 25 additions & 4 deletions src/core/layertree/qgslayertreeregistrybridge.h
Expand Up @@ -19,10 +19,23 @@
#include <QObject>
#include <QStringList>

class QAction;

class QgsLayerTreeGroup;
class QgsLayerTreeNode;

class QgsMapLayer;
#include "qgsmaplayer.h"

struct LegendLayerAction
{
LegendLayerAction( QAction* a, QString m, QString i, bool all )
: action( a ), menu( m ), id( i ), allLayers( all ) {}
QAction* action;
QString menu;
QString id;
bool allLayers;
QList<QgsMapLayer*> layers;
};

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

void addLegendLayerAction( QAction* action, QString menu, QString id,
QgsMapLayer::LayerType type, bool allLayers );
bool removeLegendLayerAction( QAction* action );
void addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer );
void removeLegendLayerActionsForLayer( QgsMapLayer* layer );
QList< LegendLayerAction > legendLayerActions( QgsMapLayer::LayerType type ) const;


signals:

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

protected:

protected:
QgsLayerTreeGroup* mRoot;
QStringList mLayerIdsForRemoval;
bool mEnabled;

QgsLayerTreeGroup* mInsertionPointGroup;
int mInsertionPointIndex;
int mInsertionPointIndex;

QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > > mLegendLayerActionMap;
};

#endif // QGSLAYERTREEREGISTRYBRIDGE_H

0 comments on commit fa5f68f

Please sign in to comment.