|
22 | 22 | #include "qgsvectorlayer.h"
|
23 | 23 | #include "qgslayertreeregistrybridge.h"
|
24 | 24 | #include "qgssymbolv2selectordialog.h"
|
| 25 | +#include "qgssinglesymbolrendererv2.h" |
25 | 26 |
|
26 | 27 | QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView* view, QgsMapCanvas* canvas )
|
27 | 28 | : mView( view )
|
@@ -126,6 +127,30 @@ QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu()
|
126 | 127 | menuStyleManager->addSeparator();
|
127 | 128 | QgsMapLayerStyleGuiUtils::instance()->addStyleManagerActions( menuStyleManager, layer );
|
128 | 129 |
|
| 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 | + |
129 | 154 | menu->addMenu( menuStyleManager );
|
130 | 155 | }
|
131 | 156 | else
|
@@ -398,6 +423,54 @@ void QgsAppLayerTreeViewMenuProvider::addCustomLayerActions( QMenu* menu, QgsMap
|
398 | 423 | }
|
399 | 424 | }
|
400 | 425 |
|
| 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 | + |
401 | 474 | void QgsAppLayerTreeViewMenuProvider::editSymbolLegendNodeSymbol()
|
402 | 475 | {
|
403 | 476 | QAction* action = qobject_cast< QAction*>( sender() );
|
|
0 commit comments