Skip to content

Commit 974cc35

Browse files
committed
[FEATURE] Add color wheel and edit symbol shortcuts to style menu
...for vector layers with single symbol renderer. This completes earlier work where symbols from categorized/graduated/rule based renderers can be quickly changed through the legend item context menu.
1 parent b8bce97 commit 974cc35

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/app/qgsapplayertreeviewmenuprovider.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsvectorlayer.h"
2323
#include "qgslayertreeregistrybridge.h"
2424
#include "qgssymbolv2selectordialog.h"
25+
#include "qgssinglesymbolrendererv2.h"
2526

2627
QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView* view, QgsMapCanvas* canvas )
2728
: mView( view )
@@ -126,6 +127,30 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
126127
menuStyleManager->addSeparator();
127128
QgsMapLayerStyleGuiUtils::instance()->addStyleManagerActions( menuStyleManager, layer );
128129

130+
if ( vlayer )
131+
{
132+
QgsSingleSymbolRendererV2* singleRenderer = dynamic_cast< QgsSingleSymbolRendererV2* >( vlayer->rendererV2() );
133+
if ( singleRenderer && singleRenderer->symbol() )
134+
{
135+
//single symbol renderer, so add set color/edit symbol actions
136+
menuStyleManager->addSeparator();
137+
QgsColorWheel* colorWheel = new QgsColorWheel( menuStyleManager );
138+
colorWheel->setColor( singleRenderer->symbol()->color() );
139+
QgsColorWidgetAction* colorAction = new QgsColorWidgetAction( colorWheel, menuStyleManager, menuStyleManager );
140+
colorAction->setDismissOnColorSelection( false );
141+
connect( colorAction, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setVectorSymbolColor( const QColor& ) ) );
142+
//store the layer id in action, so we can later retrieve the corresponding layer
143+
colorAction->setProperty( "layerId", vlayer->id() );
144+
menuStyleManager->addAction( colorAction );
145+
menuStyleManager->addSeparator();
146+
QAction* editSymbolAction = new QAction( tr( "Edit Symbol..." ), menuStyleManager );
147+
//store the layer id in action, so we can later retrieve the corresponding layer
148+
editSymbolAction->setProperty( "layerId", vlayer->id() );
149+
connect( editSymbolAction, SIGNAL( triggered() ), this, SLOT( editVectorSymbol() ) );
150+
menuStyleManager->addAction( editSymbolAction );
151+
}
152+
}
153+
129154
menu->addMenu( menuStyleManager );
130155
}
131156
else
@@ -398,6 +423,54 @@ void QgsAppLayerTreeViewMenuProvider::addCustomLayerActions( QMenu* menu, QgsMap
398423
}
399424
}
400425

426+
void QgsAppLayerTreeViewMenuProvider::editVectorSymbol()
427+
{
428+
QAction* action = qobject_cast< QAction*>( sender() );
429+
if ( !action )
430+
return;
431+
432+
QString layerId = action->property( "layerId" ).toString();
433+
QgsVectorLayer* layer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
434+
if ( !layer )
435+
return;
436+
437+
QgsSingleSymbolRendererV2* singleRenderer = dynamic_cast< QgsSingleSymbolRendererV2* >( layer->rendererV2() );
438+
if ( !singleRenderer )
439+
return;
440+
441+
QScopedPointer< QgsSymbolV2 > symbol( singleRenderer->symbol() ? singleRenderer->symbol()->clone() : nullptr );
442+
QgsSymbolV2SelectorDialog dlg( symbol.data(), QgsStyleV2::defaultStyle(), layer, mView->window() );
443+
dlg.setMapCanvas( mCanvas );
444+
if ( dlg.exec() )
445+
{
446+
singleRenderer->setSymbol( symbol.take() );
447+
layer->triggerRepaint();
448+
mView->refreshLayerSymbology( layer->id() );
449+
}
450+
}
451+
452+
void QgsAppLayerTreeViewMenuProvider::setVectorSymbolColor( const QColor& color )
453+
{
454+
QAction* action = qobject_cast< QAction*>( sender() );
455+
if ( !action )
456+
return;
457+
458+
QString layerId = action->property( "layerId" ).toString();
459+
QgsVectorLayer* layer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
460+
if ( !layer )
461+
return;
462+
463+
QgsSingleSymbolRendererV2* singleRenderer = dynamic_cast< QgsSingleSymbolRendererV2* >( layer->rendererV2() );
464+
if ( !singleRenderer || !singleRenderer->symbol() )
465+
return;
466+
467+
QgsSymbolV2* newSymbol = singleRenderer->symbol()->clone();
468+
newSymbol->setColor( color );
469+
singleRenderer->setSymbol( newSymbol );
470+
layer->triggerRepaint();
471+
mView->refreshLayerSymbology( layer->id() );
472+
}
473+
401474
void QgsAppLayerTreeViewMenuProvider::editSymbolLegendNodeSymbol()
402475
{
403476
QAction* action = qobject_cast< QAction*>( sender() );

src/app/qgsapplayertreeviewmenuprovider.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class QgsAppLayerTreeViewMenuProvider : public QObject, public QgsLayerTreeViewM
4747

4848
private slots:
4949

50+
void editVectorSymbol();
51+
void setVectorSymbolColor( const QColor &color );
5052
void editSymbolLegendNodeSymbol();
5153
void setSymbolLegendNodeColor( const QColor &color );
5254
};

0 commit comments

Comments
 (0)