Skip to content

Commit 2026fce

Browse files
committed
[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.
1 parent 2377688 commit 2026fce

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/app/qgsapplayertreeviewmenuprovider.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "qgisapp.h"
55
#include "qgsapplication.h"
66
#include "qgsclipboard.h"
7+
#include "qgscolorwidgets.h"
8+
#include "qgscolorschemeregistry.h"
9+
#include "qgscolorswatchgrid.h"
710
#include "qgslayertree.h"
811
#include "qgslayertreemodel.h"
912
#include "qgslayertreemodellegendnode.h"
@@ -205,6 +208,22 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
205208
menu->addAction( QgsApplication::getThemeIcon( "/mActionHideAllLayers.png" ), tr( "&Hide All Items" ),
206209
symbolNode, SLOT( uncheckAllItems() ) );
207210
menu->addSeparator();
211+
212+
if ( symbolNode->symbol() )
213+
{
214+
QgsColorWheel* colorWheel = new QgsColorWheel( menu );
215+
colorWheel->setColor( symbolNode->symbol()->color() );
216+
QgsColorWidgetAction* colorAction = new QgsColorWidgetAction( colorWheel, menu, menu );
217+
colorAction->setDismissOnColorSelection( false );
218+
connect( colorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setSymbolLegendNodeColor( const QColor& ) ) );
219+
//store the layer id and rule key in action, so we can later retrieve the corresponding
220+
//legend node, if it still exists
221+
colorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() );
222+
colorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() );
223+
menu->addAction( colorAction );
224+
menu->addSeparator();
225+
}
226+
208227
QAction* editSymbolAction = new QAction( tr( "Edit Symbol..." ), menu );
209228
//store the layer id and rule key in action, so we can later retrieve the corresponding
210229
//legend node, if it still exists
@@ -391,3 +410,25 @@ void QgsAppLayerTreeViewMenuProvider::editSymbolLegendNodeSymbol()
391410
node->setSymbol( symbol.take() );
392411
}
393412
}
413+
414+
void QgsAppLayerTreeViewMenuProvider::setSymbolLegendNodeColor( const QColor &color )
415+
{
416+
QAction* action = qobject_cast< QAction*>( sender() );
417+
if ( !action )
418+
return;
419+
420+
QString layerId = action->property( "layerId" ).toString();
421+
QString ruleKey = action->property( "ruleKey" ).toString();
422+
423+
QgsSymbolV2LegendNode* node = dynamic_cast<QgsSymbolV2LegendNode*>( mView->layerTreeModel()->findLegendNode( layerId, ruleKey ) );
424+
if ( !node )
425+
return;
426+
427+
const QgsSymbolV2* originalSymbol = node->symbol();
428+
if ( !originalSymbol )
429+
return;
430+
431+
QgsSymbolV2* newSymbol = originalSymbol->clone();
432+
newSymbol->setColor( color );
433+
node->setSymbol( newSymbol );
434+
}

src/app/qgsapplayertreeviewmenuprovider.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class QgsAppLayerTreeViewMenuProvider : public QObject, public QgsLayerTreeViewM
4848
private slots:
4949

5050
void editSymbolLegendNodeSymbol();
51+
void setSymbolLegendNodeColor( const QColor &color );
5152
};
5253

5354
#endif // QGSAPPLAYERTREEVIEWMENUPROVIDER_H

0 commit comments

Comments
 (0)