Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
More renaming and add a render context
  • Loading branch information
pblottiere committed Mar 20, 2019
1 parent 0cdd80d commit 7819d19
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
31 changes: 18 additions & 13 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -71,9 +71,9 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsLayerTreeModelLegendNode::draw( cons
return im;
}

void QgsLayerTreeModelLegendNode::exportToJson( const QgsLegendSettings &settings, QJsonObject &json )
void QgsLayerTreeModelLegendNode::exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json )
{
exportSymbolToJson( settings, json );
exportSymbolToJson( settings, context, json );
exportSymbolTextToJson( settings, json );
}

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

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

void QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const
void QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const
{
const QgsSymbol *s = mItem.symbol();
if ( !s )
{
return;
}

QgsRenderContext context;
context.setScaleFactor( settings.dpi() / 25.4 );
context.setRendererScale( settings.mapScale() );
context.setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * context.scaleFactor() ) ) );
context.setForceVectorOutput( true );
QgsRenderContext ctx;
ctx.setScaleFactor( settings.dpi() / 25.4 );
ctx.setRendererScale( settings.mapScale() );
ctx.setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * ctx.scaleFactor() ) ) );
ctx.setForceVectorOutput( true );

// ensure that a minimal expression context is available
QgsExpressionContext expContext = context.expressionContext();
expContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( nullptr ) );
ctx.setExpressionContext( expContext );

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

int opacity = 255;
Expand Down Expand Up @@ -658,7 +663,7 @@ QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCo
return settings.wmsLegendSize();
}

void QgsImageLegendNode::exportSymbolToJson( const QgsLegendSettings &, QJsonObject &json ) const
void QgsImageLegendNode::exportSymbolToJson( const QgsLegendSettings &, const QgsRenderContext &, QJsonObject &json ) const
{
QByteArray byteArray;
QBuffer buffer( &byteArray );
Expand Down Expand Up @@ -723,7 +728,7 @@ QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings,
return settings.symbolSize();
}

void QgsRasterSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const
void QgsRasterSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &, QJsonObject &json ) const
{
QImage img = QImage( settings.symbolSize().toSize(), QImage::Format_ARGB32 );
img.fill( Qt::transparent );
Expand Down Expand Up @@ -829,7 +834,7 @@ QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCont
return settings.wmsLegendSize();
}

void QgsWmsLegendNode::exportSymbolToJson( const QgsLegendSettings &, QJsonObject &json ) const
void QgsWmsLegendNode::exportSymbolToJson( const QgsLegendSettings &, const QgsRenderContext &, QJsonObject &json ) const
{
QByteArray byteArray;
QBuffer buffer( &byteArray );
Expand Down
12 changes: 6 additions & 6 deletions src/core/layertree/qgslayertreemodellegendnode.h
Expand Up @@ -117,7 +117,7 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
* \param json The json object to update
* \since QGIS 3.8
*/
void exportToJson( const QgsLegendSettings &settings, QJsonObject &json );
void exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json );

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

/**
* Draws label on the right side of the item
Expand Down Expand Up @@ -200,7 +200,7 @@ class CORE_EXPORT QgsSymbolLegendNode : public QgsLayerTreeModelLegendNode

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

void exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const override;
void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;

void setEmbeddedInParent( bool embedded ) override;

Expand Down Expand Up @@ -381,7 +381,7 @@ class CORE_EXPORT QgsImageLegendNode : public QgsLayerTreeModelLegendNode

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

void exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const override;
void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;

private:
QImage mImage;
Expand Down Expand Up @@ -412,7 +412,7 @@ class CORE_EXPORT QgsRasterSymbolLegendNode : public QgsLayerTreeModelLegendNode

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

void exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const override;
void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;

private:
QColor mColor;
Expand Down Expand Up @@ -444,7 +444,7 @@ class CORE_EXPORT QgsWmsLegendNode : public QgsLayerTreeModelLegendNode

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

void exportSymbolToJson( const QgsLegendSettings &settings, QJsonObject &json ) const override;
void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const override;

void invalidateMapBasedData() override;

Expand Down
12 changes: 6 additions & 6 deletions src/core/qgslegendrenderer.cpp
Expand Up @@ -46,17 +46,17 @@ void QgsLegendRenderer::drawLegend( QPainter *painter )
paintAndDetermineSize( painter );
}

void QgsLegendRenderer::drawLegend( QJsonObject &json )
void QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, QJsonObject &json )
{
QgsLayerTreeGroup *rootGroup = mLegendModel->rootGroup();
if ( !rootGroup )
return;

json["title"] = mSettings.title();
drawLegend( rootGroup, json );
exportLegendToJson( context, rootGroup, json );
}

void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &json )
void QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup, QJsonObject &json )
{
QJsonArray nodes;
for ( auto node : nodeGroup->children() )
Expand All @@ -70,7 +70,7 @@ void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &j
QJsonObject group;
group[ "type" ] = "group";
group[ "title" ] = text;
drawLegend( nodeGroup, group );
exportLegendToJson( context, nodeGroup, group );
nodes.append( group );
}
else if ( QgsLayerTree::isLayer( node ) )
Expand All @@ -94,7 +94,7 @@ void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &j

if ( legendNodes.count() == 1 )
{
legendNodes.at( 0 )->exportToJson( mSettings, group );
legendNodes.at( 0 )->exportToJson( mSettings, context, group );
nodes.append( group );
}
else if ( legendNodes.count() > 1 )
Expand All @@ -104,7 +104,7 @@ void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &j
{
QgsLayerTreeModelLegendNode *legendNode = legendNodes.at( j );
QJsonObject symbol;
legendNode->exportToJson( mSettings, symbol );
legendNode->exportToJson( mSettings, context, symbol );
symbols.append( symbol );
}
group[ "title" ] = text;
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgslegendrenderer.h
Expand Up @@ -98,7 +98,7 @@ class CORE_EXPORT QgsLegendRenderer
*
* \since QGIS 3.8
*/
void drawLegend( QJsonObject &json );
void exportLegendToJson( const QgsRenderContext &context, QJsonObject &json );

/**
* Sets the \a style of a \a node.
Expand Down Expand Up @@ -245,7 +245,7 @@ class CORE_EXPORT QgsLegendRenderer
*
* \since QGIS 3.8
*/
void drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &json );
void exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup, QJsonObject &json );

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

0 comments on commit 7819d19

Please sign in to comment.