Skip to content

Commit 7819d19

Browse files
committedMar 20, 2019
More renaming and add a render context
1 parent 0cdd80d commit 7819d19

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed
 

‎src/core/layertree/qgslayertreemodellegendnode.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsLayerTreeModelLegendNode::draw( cons
7171
return im;
7272
}
7373

74-
void QgsLayerTreeModelLegendNode::exportToJson( const QgsLegendSettings &settings, QJsonObject &json )
74+
void QgsLayerTreeModelLegendNode::exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json )
7575
{
76-
exportSymbolToJson( settings, json );
76+
exportSymbolToJson( settings, context, json );
7777
exportSymbolTextToJson( settings, json );
7878
}
7979

@@ -89,7 +89,7 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &setting
8989
return settings.symbolSize();
9090
}
9191

92-
void QgsLayerTreeModelLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const
92+
void QgsLayerTreeModelLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &, QJsonObject &json ) const
9393
{
9494
const QIcon icon = data( Qt::DecorationRole ).value<QIcon>();
9595
if ( icon.isNull() )
@@ -514,21 +514,26 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
514514
std::max( height + 2 * heightOffset, static_cast< double >( settings.symbolSize().height() ) ) );
515515
}
516516

517-
void QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const
517+
void QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const
518518
{
519519
const QgsSymbol *s = mItem.symbol();
520520
if ( !s )
521521
{
522522
return;
523523
}
524524

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 );
530535

531-
const QPixmap pix = QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), minimumIconSize(), 0, &context );
536+
const QPixmap pix = QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), minimumIconSize(), 0, &ctx );
532537
QImage img( pix.toImage().convertToFormat( QImage::Format_ARGB32_Premultiplied ) );
533538

534539
int opacity = 255;
@@ -658,7 +663,7 @@ QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCo
658663
return settings.wmsLegendSize();
659664
}
660665

661-
void QgsImageLegendNode::exportSymbolToJson( const QgsLegendSettings &, QJsonObject &json ) const
666+
void QgsImageLegendNode::exportSymbolToJson( const QgsLegendSettings &, const QgsRenderContext &, QJsonObject &json ) const
662667
{
663668
QByteArray byteArray;
664669
QBuffer buffer( &byteArray );
@@ -723,7 +728,7 @@ QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings,
723728
return settings.symbolSize();
724729
}
725730

726-
void QgsRasterSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const
731+
void QgsRasterSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &, QJsonObject &json ) const
727732
{
728733
QImage img = QImage( settings.symbolSize().toSize(), QImage::Format_ARGB32 );
729734
img.fill( Qt::transparent );
@@ -829,7 +834,7 @@ QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCont
829834
return settings.wmsLegendSize();
830835
}
831836

832-
void QgsWmsLegendNode::exportSymbolToJson( const QgsLegendSettings &, QJsonObject &json ) const
837+
void QgsWmsLegendNode::exportSymbolToJson( const QgsLegendSettings &, const QgsRenderContext &, QJsonObject &json ) const
833838
{
834839
QByteArray byteArray;
835840
QBuffer buffer( &byteArray );

‎src/core/layertree/qgslayertreemodellegendnode.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
117117
* \param json The json object to update
118118
* \since QGIS 3.8
119119
*/
120-
void exportToJson( const QgsLegendSettings &settings, QJsonObject &json );
120+
void exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json );
121121

122122
/**
123123
* Draws symbol on the left side of the item
@@ -134,7 +134,7 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
134134
* \param json The json object to update
135135
* \since QGIS 3.8
136136
*/
137-
virtual void exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const;
137+
virtual void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const;
138138

139139
/**
140140
* Draws label on the right side of the item
@@ -200,7 +200,7 @@ class CORE_EXPORT QgsSymbolLegendNode : public QgsLayerTreeModelLegendNode
200200

201201
QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
202202

203-
void exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const override;
203+
void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
204204

205205
void setEmbeddedInParent( bool embedded ) override;
206206

@@ -381,7 +381,7 @@ class CORE_EXPORT QgsImageLegendNode : public QgsLayerTreeModelLegendNode
381381

382382
QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
383383

384-
void exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const override;
384+
void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
385385

386386
private:
387387
QImage mImage;
@@ -412,7 +412,7 @@ class CORE_EXPORT QgsRasterSymbolLegendNode : public QgsLayerTreeModelLegendNode
412412

413413
QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
414414

415-
void exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const override;
415+
void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
416416

417417
private:
418418
QColor mColor;
@@ -444,7 +444,7 @@ class CORE_EXPORT QgsWmsLegendNode : public QgsLayerTreeModelLegendNode
444444

445445
QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const override;
446446

447-
void exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const override;
447+
void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;
448448

449449
void invalidateMapBasedData() override;
450450

‎src/core/qgslegendrenderer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ void QgsLegendRenderer::drawLegend( QPainter *painter )
4646
paintAndDetermineSize( painter );
4747
}
4848

49-
void QgsLegendRenderer::drawLegend( QJsonObject &json )
49+
void QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, QJsonObject &json )
5050
{
5151
QgsLayerTreeGroup *rootGroup = mLegendModel->rootGroup();
5252
if ( !rootGroup )
5353
return;
5454

5555
json["title"] = mSettings.title();
56-
drawLegend( rootGroup, json );
56+
exportLegendToJson( context, rootGroup, json );
5757
}
5858

59-
void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &json )
59+
void QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup, QJsonObject &json )
6060
{
6161
QJsonArray nodes;
6262
for ( auto node : nodeGroup->children() )
@@ -70,7 +70,7 @@ void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &j
7070
QJsonObject group;
7171
group[ "type" ] = "group";
7272
group[ "title" ] = text;
73-
drawLegend( nodeGroup, group );
73+
exportLegendToJson( context, nodeGroup, group );
7474
nodes.append( group );
7575
}
7676
else if ( QgsLayerTree::isLayer( node ) )
@@ -94,7 +94,7 @@ void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &j
9494

9595
if ( legendNodes.count() == 1 )
9696
{
97-
legendNodes.at( 0 )->exportToJson( mSettings, group );
97+
legendNodes.at( 0 )->exportToJson( mSettings, context, group );
9898
nodes.append( group );
9999
}
100100
else if ( legendNodes.count() > 1 )
@@ -104,7 +104,7 @@ void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &j
104104
{
105105
QgsLayerTreeModelLegendNode *legendNode = legendNodes.at( j );
106106
QJsonObject symbol;
107-
legendNode->exportToJson( mSettings, symbol );
107+
legendNode->exportToJson( mSettings, context, symbol );
108108
symbols.append( symbol );
109109
}
110110
group[ "title" ] = text;

‎src/core/qgslegendrenderer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class CORE_EXPORT QgsLegendRenderer
9898
*
9999
* \since QGIS 3.8
100100
*/
101-
void drawLegend( QJsonObject &json );
101+
void exportLegendToJson( const QgsRenderContext &context, QJsonObject &json );
102102

103103
/**
104104
* Sets the \a style of a \a node.
@@ -245,7 +245,7 @@ class CORE_EXPORT QgsLegendRenderer
245245
*
246246
* \since QGIS 3.8
247247
*/
248-
void drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &json );
248+
void exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup, QJsonObject &json );
249249

250250
/**
251251
* Draws the legend using the specified render \a context, and returns the actual size of the legend.

0 commit comments

Comments
 (0)
Please sign in to comment.