Skip to content
Permalink
Browse files

More renaming and add a render context

  • Loading branch information
pblottiere committed Mar 20, 2019
1 parent 0cdd80d commit 7819d19af30475b6380c6860db85a45b9e09d349
@@ -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 );
}

@@ -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() )
@@ -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;
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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
@@ -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
@@ -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;

@@ -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;
@@ -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;
@@ -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;

@@ -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() )
@@ -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 ) )
@@ -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 )
@@ -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;
@@ -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.
@@ -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.

0 comments on commit 7819d19

Please sign in to comment.
You can’t perform that action at this time.