@@ -71,9 +71,9 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsLayerTreeModelLegendNode::draw( cons
71
71
return im;
72
72
}
73
73
74
- void QgsLayerTreeModelLegendNode::exportToJson ( const QgsLegendSettings &settings, QJsonObject &json )
74
+ void QgsLayerTreeModelLegendNode::exportToJson ( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json )
75
75
{
76
- exportSymbolToJson ( settings, json );
76
+ exportSymbolToJson ( settings, context, json );
77
77
exportSymbolTextToJson ( settings, json );
78
78
}
79
79
@@ -89,7 +89,7 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &setting
89
89
return settings.symbolSize ();
90
90
}
91
91
92
- void QgsLayerTreeModelLegendNode::exportSymbolToJson ( const QgsLegendSettings &settings, QJsonObject &json ) const
92
+ void QgsLayerTreeModelLegendNode::exportSymbolToJson ( const QgsLegendSettings &settings, const QgsRenderContext &, QJsonObject &json ) const
93
93
{
94
94
const QIcon icon = data ( Qt::DecorationRole ).value <QIcon>();
95
95
if ( icon.isNull () )
@@ -514,21 +514,26 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
514
514
std::max ( height + 2 * heightOffset, static_cast < double >( settings.symbolSize ().height () ) ) );
515
515
}
516
516
517
- void QgsSymbolLegendNode::exportSymbolToJson ( const QgsLegendSettings &settings, QJsonObject &json ) const
517
+ void QgsSymbolLegendNode::exportSymbolToJson ( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const
518
518
{
519
519
const QgsSymbol *s = mItem .symbol ();
520
520
if ( !s )
521
521
{
522
522
return ;
523
523
}
524
524
525
- QgsRenderContext context;
526
- context.setScaleFactor ( settings.dpi () / 25.4 );
527
- context.setRendererScale ( settings.mapScale () );
528
- context.setMapToPixel ( QgsMapToPixel ( 1 / ( settings.mmPerMapUnit () * context.scaleFactor () ) ) );
529
- context.setForceVectorOutput ( true );
525
+ QgsRenderContext ctx;
526
+ ctx.setScaleFactor ( settings.dpi () / 25.4 );
527
+ ctx.setRendererScale ( settings.mapScale () );
528
+ ctx.setMapToPixel ( QgsMapToPixel ( 1 / ( settings.mmPerMapUnit () * ctx.scaleFactor () ) ) );
529
+ ctx.setForceVectorOutput ( true );
530
+
531
+ // ensure that a minimal expression context is available
532
+ QgsExpressionContext expContext = context.expressionContext ();
533
+ expContext.appendScopes ( QgsExpressionContextUtils::globalProjectLayerScopes ( nullptr ) );
534
+ ctx.setExpressionContext ( expContext );
530
535
531
- const QPixmap pix = QgsSymbolLayerUtils::symbolPreviewPixmap ( mItem .symbol (), minimumIconSize (), 0 , &context );
536
+ const QPixmap pix = QgsSymbolLayerUtils::symbolPreviewPixmap ( mItem .symbol (), minimumIconSize (), 0 , &ctx );
532
537
QImage img ( pix.toImage ().convertToFormat ( QImage::Format_ARGB32_Premultiplied ) );
533
538
534
539
int opacity = 255 ;
@@ -658,7 +663,7 @@ QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCo
658
663
return settings.wmsLegendSize ();
659
664
}
660
665
661
- void QgsImageLegendNode::exportSymbolToJson ( const QgsLegendSettings &, QJsonObject &json ) const
666
+ void QgsImageLegendNode::exportSymbolToJson ( const QgsLegendSettings &, const QgsRenderContext &, QJsonObject &json ) const
662
667
{
663
668
QByteArray byteArray;
664
669
QBuffer buffer ( &byteArray );
@@ -723,7 +728,7 @@ QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings,
723
728
return settings.symbolSize ();
724
729
}
725
730
726
- void QgsRasterSymbolLegendNode::exportSymbolToJson ( const QgsLegendSettings &settings, QJsonObject &json ) const
731
+ void QgsRasterSymbolLegendNode::exportSymbolToJson ( const QgsLegendSettings &settings, const QgsRenderContext &, QJsonObject &json ) const
727
732
{
728
733
QImage img = QImage ( settings.symbolSize ().toSize (), QImage::Format_ARGB32 );
729
734
img.fill ( Qt::transparent );
@@ -829,7 +834,7 @@ QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCont
829
834
return settings.wmsLegendSize ();
830
835
}
831
836
832
- void QgsWmsLegendNode::exportSymbolToJson ( const QgsLegendSettings &, QJsonObject &json ) const
837
+ void QgsWmsLegendNode::exportSymbolToJson ( const QgsLegendSettings &, const QgsRenderContext &, QJsonObject &json ) const
833
838
{
834
839
QByteArray byteArray;
835
840
QBuffer buffer ( &byteArray );
0 commit comments