Skip to content
Permalink
Browse files
[layertree] move implementation of legend actions to QgsAppLayerTreeV…
…iewMenuProvider
  • Loading branch information
wonder-sk committed May 23, 2014
1 parent fa5f68f commit 189707fc947a58dfce7755815b9311bbd585c223
@@ -16,6 +16,7 @@

#include "qgsapplegendinterface.h"

#include "qgsapplayertreeviewmenuprovider.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreeview.h"
@@ -301,17 +302,29 @@ void QgsAppLegendInterface::onRemovedChildren()
void QgsAppLegendInterface::addLegendLayerAction( QAction* action,
QString menu, QString id, QgsMapLayer::LayerType type, bool allLayers )
{
QgsProject::instance()->layerTreeRegistryBridge()->addLegendLayerAction( action, menu, id, type, allLayers );
QgsAppLayerTreeViewMenuProvider* menuProvider = dynamic_cast<QgsAppLayerTreeViewMenuProvider*>(mLayerTreeView->menuProvider());
if (!menuProvider)
return;

menuProvider->addLegendLayerAction( action, menu, id, type, allLayers );
}

void QgsAppLegendInterface::addLegendLayerActionForLayer( QAction* action, QgsMapLayer* layer )
{
QgsProject::instance()->layerTreeRegistryBridge()->addLegendLayerActionForLayer( action, layer );
QgsAppLayerTreeViewMenuProvider* menuProvider = dynamic_cast<QgsAppLayerTreeViewMenuProvider*>(mLayerTreeView->menuProvider());
if (!menuProvider)
return;

menuProvider->addLegendLayerActionForLayer( action, layer );
}

bool QgsAppLegendInterface::removeLegendLayerAction( QAction* action )
{
return QgsProject::instance()->layerTreeRegistryBridge()->removeLegendLayerAction( action );
QgsAppLayerTreeViewMenuProvider* menuProvider = dynamic_cast<QgsAppLayerTreeViewMenuProvider*>(mLayerTreeView->menuProvider());
if (!menuProvider)
return false;

menuProvider->removeLegendLayerAction( action );
}

QgsMapLayer* QgsAppLegendInterface::currentLayer()
@@ -143,8 +143,7 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
}

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

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



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

bool QgsAppLayerTreeViewMenuProvider::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 QgsAppLayerTreeViewMenuProvider::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 QgsAppLayerTreeViewMenuProvider::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 > QgsAppLayerTreeViewMenuProvider::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 >() ;
}
@@ -4,9 +4,22 @@
#include <QObject>

#include "qgslayertreeview.h"
#include "qgsmaplayer.h"

class QgsMapCanvas;
class QAction;

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;
};

class QgsMapCanvas;

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

QMenu* createContextMenu();

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;


protected:
QgsLayerTreeView* mView;
QgsMapCanvas* mCanvas;

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


@@ -21,7 +21,6 @@

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

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

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 );
@@ -123,75 +118,3 @@ void QgsLayerTreeRegistryBridge::groupRemovedChildren()

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 >() ;
}
@@ -19,23 +19,10 @@
#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.
@@ -62,14 +49,6 @@ 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:
@@ -85,9 +64,7 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
bool mEnabled;

QgsLayerTreeGroup* mInsertionPointGroup;
int mInsertionPointIndex;

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

#endif // QGSLAYERTREEREGISTRYBRIDGE_H

0 comments on commit 189707f

Please sign in to comment.