Skip to content

Commit

Permalink
Ensure that project colors show correctly in legend and symbol previews
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 12, 2019
1 parent ac10769 commit 0f42c8e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
context.setForceVectorOutput( true );
context.setPainter( ctx ? ctx->painter : nullptr );

// use a minimal expression context for now
QgsExpressionContext expContext;
expContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( nullptr ) );
context.setExpressionContext( expContext );

//Consider symbol size for point markers
double height = settings.symbolSize().height();
double width = settings.symbolSize().width();
Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayoutitemlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ QgsLayoutItemLegend::QgsLayoutItemLegend( QgsLayout *layout )
// Connect to the main layertreeroot.
// It serves in "auto update mode" as a medium between the main app legend and this one
connect( mLayout->project()->layerTreeRoot(), &QgsLayerTreeNode::customPropertyChanged, this, &QgsLayoutItemLegend::nodeCustomPropertyChanged );

// If project colors change, we need to redraw legend, as legend symbols may rely on project colors
connect( mLayout->project(), &QgsProject::projectColorsChanged, this, [ = ]
{
invalidateCache();
update();
} );
}

QgsLayoutItemLegend *QgsLayoutItemLegend::create( QgsLayout *layout )
Expand Down
8 changes: 8 additions & 0 deletions src/core/symbology/qgssymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ void QgsSymbol::drawPreviewIcon( QPainter *painter, QSize size, QgsRenderContext
QgsSymbolRenderContext symbolContext( context, outputUnit(), mOpacity, false, mRenderHints, nullptr, QgsFields(), mapUnitScale() );
symbolContext.setOriginalGeometryType( mType == Fill ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::UnknownGeometry );

if ( !customContext )
{
// if no render context was passed, build a minimal expression context
QgsExpressionContext expContext;
expContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( nullptr ) );
context.setExpressionContext( expContext );
}

Q_FOREACH ( QgsSymbolLayer *layer, mLayers )
{
if ( !layer->enabled() )
Expand Down
10 changes: 8 additions & 2 deletions src/core/symbology/qgssymbollayerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ QPixmap QgsSymbolLayerUtils::symbolPreviewPixmap( QgsSymbol *symbol, QSize size,
}

// If the context has no feature and there are DD properties,
// use a clone and clear all DDs: see issue #19096
// use a clone and clear some DDs: see issue #19096
// Applying a data defined size to a categorized layer hides its category symbol in the layers panel and legend
if ( symbol->hasDataDefinedProperties() &&
!( customContext
Expand All @@ -682,7 +682,13 @@ QPixmap QgsSymbolLayerUtils::symbolPreviewPixmap( QgsSymbol *symbol, QSize size,
const QgsSymbolLayerList layers( symbol_noDD->symbolLayers() );
for ( const auto &layer : layers )
{
layer->dataDefinedProperties().clear();
for ( int i = 0; i < layer->dataDefinedProperties().count(); ++i )
{
QgsProperty &prop = layer->dataDefinedProperties().property( i );
// don't clear project color properties -- we want to show them in symbol previews
if ( prop.isActive() && !prop.isProjectColor() )
prop.setActive( false );
}
}
symbol_noDD->drawPreviewIcon( &painter, size, customContext );
}
Expand Down

0 comments on commit 0f42c8e

Please sign in to comment.