Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update exportSymbolToJson
  • Loading branch information
pblottiere committed May 13, 2020
1 parent 555d886 commit 91a6c56
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
41 changes: 25 additions & 16 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -86,9 +86,9 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsLayerTreeModelLegendNode::draw( cons

QJsonObject QgsLayerTreeModelLegendNode::exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context )
{
QJsonObject json;
exportSymbolToJson( settings, context, json );
exportSymbolTextToJson( settings, json );
QJsonObject json = exportSymbolToJson( settings, context );
const QString text = data( Qt::DisplayRole ).toString();
json[ QStringLiteral( "title" ) ] = text;
return json;
}

Expand Down Expand Up @@ -132,18 +132,21 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &setting
return size;
}

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

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

QJsonObject json;
json[ "icon" ] = base64;
return json;
}

QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const
Expand Down Expand Up @@ -227,12 +230,6 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &set
return labelSize;
}

void QgsLayerTreeModelLegendNode::exportSymbolTextToJson( const QgsLegendSettings &, QJsonObject &json ) const
{
const QString text = data( Qt::DisplayRole ).toString();
json[ "title" ] = text;
}

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

QgsSymbolLegendNode::QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent )
Expand Down Expand Up @@ -663,12 +660,12 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
std::max( height + 2 * heightOffset, static_cast< double >( desiredHeight ) ) );
}

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


Expand Down Expand Up @@ -706,7 +703,10 @@ void QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings,
QBuffer buffer( &byteArray );
img.save( &buffer, "PNG" );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );

QJsonObject json;
json[ "icon" ] = base64;
return json;
}

void QgsSymbolLegendNode::setEmbeddedInParent( bool embedded )
Expand Down Expand Up @@ -864,13 +864,16 @@ QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCo
return settings.wmsLegendSize();
}

void QgsImageLegendNode::exportSymbolToJson( const QgsLegendSettings &, const QgsRenderContext &, QJsonObject &json ) const
QJsonObject QgsImageLegendNode::exportSymbolToJson( const QgsLegendSettings &, const QgsRenderContext & ) const
{
QByteArray byteArray;
QBuffer buffer( &byteArray );
mImage.save( &buffer, "PNG" );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );

QJsonObject json;
json[ "icon" ] = base64;
return json;
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -956,7 +959,7 @@ QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings,
return size;
}

void QgsRasterSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &, QJsonObject &json ) const
QJsonObject QgsRasterSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext & ) const
{
QImage img = QImage( settings.symbolSize().toSize(), QImage::Format_ARGB32 );
img.fill( Qt::transparent );
Expand Down Expand Up @@ -991,7 +994,10 @@ void QgsRasterSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &set
QBuffer buffer( &byteArray );
img.save( &buffer, "PNG" );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );

QJsonObject json;
json[ "icon" ] = base64;
return json;
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -1080,13 +1086,16 @@ QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemCont
return settings.wmsLegendSize();
}

void QgsWmsLegendNode::exportSymbolToJson( const QgsLegendSettings &, const QgsRenderContext &, QJsonObject &json ) const
QJsonObject QgsWmsLegendNode::exportSymbolToJson( const QgsLegendSettings &, const QgsRenderContext & ) const
{
QByteArray byteArray;
QBuffer buffer( &byteArray );
mImage.save( &buffer, "PNG" );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );

QJsonObject json;
json[ "icon" ] = base64;
return json;
}

/* private */
Expand Down
18 changes: 5 additions & 13 deletions src/core/layertree/qgslayertreemodellegendnode.h
Expand Up @@ -241,7 +241,7 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
* \param json The json object to update
* \since QGIS 3.8
*/
virtual void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const;
virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const;

/**
* Draws label on the right side of the item
Expand All @@ -252,14 +252,6 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
*/
virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;

/**
* Adds a label in a JSON object with the key "title".
* \param settings Legend layout configuration
* \param json The json object to update
* \since QGIS 3.8
*/
void exportSymbolTextToJson( const QgsLegendSettings &settings, QJsonObject &json ) const;

signals:
//! Emitted on internal data change so the layer tree model can forward the signal to views
void dataChanged();
Expand Down Expand Up @@ -311,7 +303,7 @@ class CORE_EXPORT QgsSymbolLegendNode : public QgsLayerTreeModelLegendNode

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

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

void setEmbeddedInParent( bool embedded ) override;

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

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

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

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

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

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

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

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

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

void invalidateMapBasedData() override;

Expand Down

0 comments on commit 91a6c56

Please sign in to comment.