Skip to content

Commit

Permalink
Remove assumptions that QgsSymbolLegendNode is ALWAYS associated with…
Browse files Browse the repository at this point in the history
… a vector layer
  • Loading branch information
nyalldawson committed Dec 7, 2020
1 parent c66d36c commit 2e83bcf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
41 changes: 24 additions & 17 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -632,7 +632,8 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
if ( QgsSymbolLegendNode *symbolNode = qobject_cast< QgsSymbolLegendNode * >( node ) )
{
// symbology item
if ( symbolNode->symbol() )
QgsMapLayer *layer = QgsLayerTree::toLayer( node->layerNode() )->layer();
if ( layer && layer->type() == QgsMapLayerType::VectorLayer && symbolNode->symbol() )
{
QgsColorWheel *colorWheel = new QgsColorWheel( menu );
colorWheel->setColor( symbolNode->symbol()->color() );
Expand Down Expand Up @@ -664,12 +665,15 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
const QString layerId = symbolNode->layerNode()->layerId();
const QString ruleKey = symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();

QAction *editSymbolAction = new QAction( tr( "Edit Symbol…" ), menu );
connect( editSymbolAction, &QAction::triggered, this, [this, layerId, ruleKey ]
if ( layer && layer->type() == QgsMapLayerType::VectorLayer )
{
editSymbolLegendNodeSymbol( layerId, ruleKey );
} );
menu->addAction( editSymbolAction );
QAction *editSymbolAction = new QAction( tr( "Edit Symbol…" ), menu );
connect( editSymbolAction, &QAction::triggered, this, [this, layerId, ruleKey ]
{
editSymbolLegendNodeSymbol( layerId, ruleKey );
} );
menu->addAction( editSymbolAction );
}

QAction *copySymbolAction = new QAction( tr( "Copy Symbol" ), menu );
connect( copySymbolAction, &QAction::triggered, this, [this, layerId, ruleKey ]
Expand All @@ -678,18 +682,21 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
} );
menu->addAction( copySymbolAction );

bool enablePaste = false;
std::unique_ptr< QgsSymbol > tempSymbol( QgsSymbolLayerUtils::symbolFromMimeData( QApplication::clipboard()->mimeData() ) );
if ( tempSymbol )
enablePaste = true;

QAction *pasteSymbolAction = new QAction( tr( "Paste Symbol" ), menu );
connect( pasteSymbolAction, &QAction::triggered, this, [this, layerId, ruleKey]
if ( layer && layer->type() == QgsMapLayerType::VectorLayer )
{
pasteSymbolLegendNodeSymbol( layerId, ruleKey );
} );
pasteSymbolAction->setEnabled( enablePaste );
menu->addAction( pasteSymbolAction );
bool enablePaste = false;
std::unique_ptr< QgsSymbol > tempSymbol( QgsSymbolLayerUtils::symbolFromMimeData( QApplication::clipboard()->mimeData() ) );
if ( tempSymbol )
enablePaste = true;

QAction *pasteSymbolAction = new QAction( tr( "Paste Symbol" ), menu );
connect( pasteSymbolAction, &QAction::triggered, this, [this, layerId, ruleKey]
{
pasteSymbolLegendNodeSymbol( layerId, ruleKey );
} );
pasteSymbolAction->setEnabled( enablePaste );
menu->addAction( pasteSymbolAction );
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -300,7 +300,9 @@ QgsSymbolLegendNode::QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const Qg
mSymbolMaximumSize = settings.value( "/qgis/legendsymbolMaximumSize", 20.0 ).toDouble();

updateLabel();
connect( qobject_cast<QgsVectorLayer *>( nodeLayer->layer() ), &QgsVectorLayer::symbolFeatureCountMapChanged, this, &QgsSymbolLegendNode::updateLabel );
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( nodeLayer->layer() ) )
connect( vl, &QgsVectorLayer::symbolFeatureCountMapChanged, this, &QgsSymbolLegendNode::updateLabel );

connect( nodeLayer, &QObject::destroyed, this, [ = ]() { mLayerNode = nullptr; } );

if ( auto *lSymbol = mItem.symbol() )
Expand Down

0 comments on commit 2e83bcf

Please sign in to comment.