Skip to content
Permalink
Browse files

[FEATURE] Directly set class symbol color from context menu in legend

Shows a color wheel widget in the menu, which allows you to
interactively edit the color for a classes' symbol.
  • Loading branch information
nyalldawson committed Dec 9, 2015
1 parent 2377688 commit 2026fce135d379605b0d5b2777dd20ebbf852707
Showing with 42 additions and 0 deletions.
  1. +41 −0 src/app/qgsapplayertreeviewmenuprovider.cpp
  2. +1 −0 src/app/qgsapplayertreeviewmenuprovider.h
@@ -4,6 +4,9 @@
#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsclipboard.h"
#include "qgscolorwidgets.h"
#include "qgscolorschemeregistry.h"
#include "qgscolorswatchgrid.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreemodellegendnode.h"
@@ -205,6 +208,22 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
menu->addAction( QgsApplication::getThemeIcon( "/mActionHideAllLayers.png" ), tr( "&Hide All Items" ),
symbolNode, SLOT( uncheckAllItems() ) );
menu->addSeparator();

if ( symbolNode->symbol() )
{
QgsColorWheel* colorWheel = new QgsColorWheel( menu );
colorWheel->setColor( symbolNode->symbol()->color() );
QgsColorWidgetAction* colorAction = new QgsColorWidgetAction( colorWheel, menu, menu );
colorAction->setDismissOnColorSelection( false );
connect( colorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setSymbolLegendNodeColor( const QColor& ) ) );
//store the layer id and rule key in action, so we can later retrieve the corresponding
//legend node, if it still exists
colorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() );
colorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() );
menu->addAction( colorAction );
menu->addSeparator();
}

QAction* editSymbolAction = new QAction( tr( "Edit Symbol..." ), menu );
//store the layer id and rule key in action, so we can later retrieve the corresponding
//legend node, if it still exists
@@ -391,3 +410,25 @@ void QgsAppLayerTreeViewMenuProvider::editSymbolLegendNodeSymbol()
node->setSymbol( symbol.take() );
}
}

void QgsAppLayerTreeViewMenuProvider::setSymbolLegendNodeColor( const QColor &color )
{
QAction* action = qobject_cast< QAction*>( sender() );
if ( !action )
return;

QString layerId = action->property( "layerId" ).toString();
QString ruleKey = action->property( "ruleKey" ).toString();

QgsSymbolV2LegendNode* node = dynamic_cast<QgsSymbolV2LegendNode*>( mView->layerTreeModel()->findLegendNode( layerId, ruleKey ) );
if ( !node )
return;

const QgsSymbolV2* originalSymbol = node->symbol();
if ( !originalSymbol )
return;

QgsSymbolV2* newSymbol = originalSymbol->clone();
newSymbol->setColor( color );
node->setSymbol( newSymbol );
}
@@ -48,6 +48,7 @@ class QgsAppLayerTreeViewMenuProvider : public QObject, public QgsLayerTreeViewM
private slots:

void editSymbolLegendNodeSymbol();
void setSymbolLegendNodeColor( const QColor &color );
};

#endif // QGSAPPLAYERTREEVIEWMENUPROVIDER_H

1 comment on commit 2026fce

@nirvn

This comment has been minimized.

Copy link
Contributor

@nirvn nirvn commented on 2026fce Dec 9, 2015

Nice, very nice. Two thoughts:

  • it would be nice to have project / system colors in there if possible
  • could we find a way to insert this useful shortcut for childless layers (i.e. simple symbology)? maybe the color well could be added within the Styles > sub menu?
Please sign in to comment.
You can’t perform that action at this time.