Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Mar 19, 2019
1 parent 2ccc3f1 commit c98a2ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
24 changes: 9 additions & 15 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -91,15 +91,15 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &setting

void QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &settings, QJsonObject &json ) const
{
QIcon icon = data( Qt::DecorationRole ).value<QIcon>();
const QIcon icon = data( Qt::DecorationRole ).value<QIcon>();
if ( icon.isNull() )
return;

QImage image( icon.pixmap( settings.symbolSize().width(), settings.symbolSize().height() ).toImage() );
const QImage image( icon.pixmap( settings.symbolSize().width(), settings.symbolSize().height() ).toImage() );
QByteArray byteArray;
QBuffer buffer( &byteArray );
image.save( &buffer, "PNG" );
QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
json[ "icon" ] = base64;
}

Expand Down Expand Up @@ -149,7 +149,6 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &set

void QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &, QJsonObject &json ) const
{
QgsExpressionContext tempContext;
const QString text = data( Qt::DisplayRole ).toString();
json[ "title" ] = text;
}
Expand Down Expand Up @@ -517,7 +516,7 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC

void QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, QJsonObject &json ) const
{
QgsSymbol *s = mItem.symbol();
const QgsSymbol *s = mItem.symbol();
if ( !s )
{
return;
Expand All @@ -529,12 +528,7 @@ void QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, QJsonOb
context.setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * context.scaleFactor() ) ) );
context.setForceVectorOutput( true );

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

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

int opacity = 255;
Expand All @@ -553,7 +547,7 @@ void QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, QJsonOb
QByteArray byteArray;
QBuffer buffer( &byteArray );
img.save( &buffer, "PNG" );
QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
json[ "icon" ] = base64;
}

Expand Down Expand Up @@ -669,7 +663,7 @@ void QgsImageLegendNode::drawSymbol( const QgsLegendSettings &, QJsonObject &jso
QByteArray byteArray;
QBuffer buffer( &byteArray );
mImage.save( &buffer, "PNG" );
QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
json[ "icon" ] = base64;
}

Expand Down Expand Up @@ -763,7 +757,7 @@ void QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, Q
QByteArray byteArray;
QBuffer buffer( &byteArray );
img.save( &buffer, "PNG" );
QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
json[ "icon" ] = base64;
}

Expand Down Expand Up @@ -840,7 +834,7 @@ void QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &, QJsonObject &json
QByteArray byteArray;
QBuffer buffer( &byteArray );
mImage.save( &buffer, "PNG" );
QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
json[ "icon" ] = base64;
}

Expand Down
8 changes: 3 additions & 5 deletions src/core/qgslegendrenderer.cpp
Expand Up @@ -59,14 +59,12 @@ void QgsLegendRenderer::drawLegend( QJsonObject &json )
void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &json )
{
QJsonArray nodes;
Q_FOREACH ( QgsLayerTreeNode *node, nodeGroup->children() )
for ( auto node : nodeGroup->children() )
{
if ( QgsLayerTree::isGroup( node ) )
{
QgsLayerTreeGroup *nodeGroup = QgsLayerTree::toGroup( node );

QModelIndex idx = mLegendModel->node2index( nodeGroup );
QgsExpressionContext tempContext;
const QModelIndex idx = mLegendModel->node2index( nodeGroup );
const QString text = mLegendModel->data( idx, Qt::DisplayRole ).toString();

QJsonObject group;
Expand All @@ -85,7 +83,7 @@ void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &j
QString text;
if ( nodeLegendStyle( nodeLayer ) != QgsLegendStyle::Hidden )
{
QModelIndex idx = mLegendModel->node2index( nodeLayer );
const QModelIndex idx = mLegendModel->node2index( nodeLayer );
text = mLegendModel->data( idx, Qt::DisplayRole ).toString();
}

Expand Down

0 comments on commit c98a2ec

Please sign in to comment.