Skip to content
Permalink
Browse files

Add icons in json for markers

  • Loading branch information
pblottiere committed Mar 1, 2019
1 parent 9651efa commit 5410774ab1a5a847500b520f0c7651f44d9f239b
@@ -73,8 +73,8 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsLayerTreeModelLegendNode::draw( cons

void QgsLayerTreeModelLegendNode::draw( const QgsLegendSettings &settings, QJsonObject &json )
{
drawSymbol( settings, json );
drawSymbolText( settings, json );
json[ "symbol" ] = "TODO";
}

QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
@@ -89,6 +89,19 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &setting
return settings.symbolSize();
}

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

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() );
json[ "symbol" ] = base64;
}

QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const
{
@@ -134,7 +147,7 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &set
return labelSize;
}

void QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &settings, QJsonObject &json ) const
void QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &, QJsonObject &json ) const
{
QgsExpressionContext tempContext;
const QString text = data( Qt::DisplayRole ).toString();
@@ -502,6 +515,40 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
std::max( height + 2 * heightOffset, static_cast< double >( settings.symbolSize().height() ) ) );
}

void QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, QJsonObject &json ) 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 );

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

QImage tempImage = QImage( minimumIconSize(), QImage::Format_ARGB32 );
tempImage.fill( Qt::transparent );
QPainter imagePainter( &tempImage );
imagePainter.setRenderHint( QPainter::Antialiasing );
context.setPainter( &imagePainter );

QPixmap pix = QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), minimumIconSize(), 0, &context );

QByteArray byteArray;
QBuffer buffer( &byteArray );
QImage img( pix.toImage().convertToFormat( QImage::Format_ARGB32_Premultiplied ) );
img.save( &buffer, "PNG" );
QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
json[ "icon" ] = base64;
}

void QgsSymbolLegendNode::setEmbeddedInParent( bool embedded )
{
@@ -610,6 +657,10 @@ QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCo
return settings.wmsLegendSize();
}

void QgsImageLegendNode::drawSymbol( const QgsLegendSettings &, QJsonObject & ) const
{
}

// -------------------------------------------------------------------------

QgsRasterSymbolLegendNode::QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent )
@@ -666,6 +717,10 @@ QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings,
return settings.symbolSize();
}

void QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &, QJsonObject & ) const
{
}

// -------------------------------------------------------------------------

QgsWmsLegendNode::QgsWmsLegendNode( QgsLayerTreeLayer *nodeLayer, QObject *parent )
@@ -734,6 +789,10 @@ QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCont
return settings.wmsLegendSize();
}

void QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &, QJsonObject & ) const
{
}

/* private */
QImage QgsWmsLegendNode::renderMessage( const QString &msg ) const
{
@@ -121,6 +121,8 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
*/
virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const;

virtual void drawSymbol( const QgsLegendSettings &settings, QJsonObject &json ) const;

/**
* Draws label on the right side of the item
* \param settings Legend layout configuration
@@ -179,6 +181,8 @@ class CORE_EXPORT QgsSymbolLegendNode : public QgsLayerTreeModelLegendNode

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

void drawSymbol( const QgsLegendSettings &settings, QJsonObject &json ) const override;

void setEmbeddedInParent( bool embedded ) override;

void setUserLabel( const QString &userLabel ) override { mUserLabel = userLabel; updateLabel(); }
@@ -358,6 +362,8 @@ class CORE_EXPORT QgsImageLegendNode : public QgsLayerTreeModelLegendNode

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

void drawSymbol( const QgsLegendSettings &settings, QJsonObject &json ) const override;

private:
QImage mImage;
};
@@ -387,6 +393,8 @@ class CORE_EXPORT QgsRasterSymbolLegendNode : public QgsLayerTreeModelLegendNode

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

void drawSymbol( const QgsLegendSettings &settings, QJsonObject &json ) const override;

private:
QColor mColor;
QString mLabel;
@@ -417,6 +425,8 @@ class CORE_EXPORT QgsWmsLegendNode : public QgsLayerTreeModelLegendNode

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

void drawSymbol( const QgsLegendSettings &settings, QJsonObject &json ) const override;

void invalidateMapBasedData() override;

private slots:

0 comments on commit 5410774

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