From 306065e5c47e7f9a3b4d87c12448ed314048f8d3 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Fri, 8 May 2020 23:36:11 +0200 Subject: [PATCH 01/11] Update API --- src/core/qgslegendrenderer.cpp | 7 +++++-- src/core/qgslegendrenderer.h | 2 +- src/server/services/wms/qgswmsrenderer.cpp | 5 +---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/core/qgslegendrenderer.cpp b/src/core/qgslegendrenderer.cpp index 78637fcff39..954c6a096fe 100644 --- a/src/core/qgslegendrenderer.cpp +++ b/src/core/qgslegendrenderer.cpp @@ -68,14 +68,17 @@ void QgsLegendRenderer::drawLegend( QPainter *painter ) paintAndDetermineSize( context ); } -void QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, QJsonObject &json ) +QJsonObject QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context ) { + QJsonObject json; + QgsLayerTreeGroup *rootGroup = mLegendModel->rootGroup(); if ( !rootGroup ) - return; + return json; json[QStringLiteral( "title" )] = mSettings.title(); exportLegendToJson( context, rootGroup, json ); + return json; } void QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup, QJsonObject &json ) diff --git a/src/core/qgslegendrenderer.h b/src/core/qgslegendrenderer.h index ed02ca61123..12fdbd7f55c 100644 --- a/src/core/qgslegendrenderer.h +++ b/src/core/qgslegendrenderer.h @@ -100,7 +100,7 @@ class CORE_EXPORT QgsLegendRenderer * * \since QGIS 3.8 */ - void exportLegendToJson( const QgsRenderContext &context, QJsonObject &json ); + QJsonObject exportLegendToJson( const QgsRenderContext &context ); /** * Sets the \a style of a \a node. diff --git a/src/server/services/wms/qgswmsrenderer.cpp b/src/server/services/wms/qgswmsrenderer.cpp index ba004e9913b..10a4b65c6b3 100644 --- a/src/server/services/wms/qgswmsrenderer.cpp +++ b/src/server/services/wms/qgswmsrenderer.cpp @@ -194,11 +194,8 @@ namespace QgsWms QgsLegendRenderer renderer( &model, settings ); // rendering - QJsonObject json; QgsRenderContext renderContext; - renderer.exportLegendToJson( renderContext, json ); - - return json; + return renderer.exportLegendToJson( renderContext ); } void QgsRenderer::runHitTest( const QgsMapSettings &mapSettings, HitTest &hitTest ) const From 0772591784337416721b5f37281d28c286c4ebb2 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Fri, 8 May 2020 23:36:36 +0200 Subject: [PATCH 02/11] Update unit tests --- tests/src/core/testqgslegendrenderer.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/src/core/testqgslegendrenderer.cpp b/tests/src/core/testqgslegendrenderer.cpp index 696659ed38c..bccf8bf2cf1 100644 --- a/tests/src/core/testqgslegendrenderer.cpp +++ b/tests/src/core/testqgslegendrenderer.cpp @@ -101,10 +101,8 @@ static QJsonObject _renderJsonLegend( QgsLayerTreeModel *legendModel, const QgsL { QgsLegendRenderer legendRenderer( legendModel, settings ); - QJsonObject json; QgsRenderContext context; - legendRenderer.exportLegendToJson( context, json ); - return json; + return legendRenderer.exportLegendToJson( context ); } static bool _verifyImage( const QString &testName, QString &report, int diff = 30 ) From 6550dad44987a7623b601b125e12297d8d70d7ea Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Fri, 8 May 2020 23:36:47 +0200 Subject: [PATCH 03/11] Update sip --- python/core/auto_generated/qgslegendrenderer.sip.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/core/auto_generated/qgslegendrenderer.sip.in b/python/core/auto_generated/qgslegendrenderer.sip.in index 64b0ce9188a..575cbe4f65b 100644 --- a/python/core/auto_generated/qgslegendrenderer.sip.in +++ b/python/core/auto_generated/qgslegendrenderer.sip.in @@ -81,7 +81,7 @@ Draws the legend using a given render ``context``. The legend will occupy the ar .. versionadded:: 3.6 %End - void exportLegendToJson( const QgsRenderContext &context, QJsonObject &json ); + QJsonObject exportLegendToJson( const QgsRenderContext &context ); %Docstring Renders the legend in a ``json`` object. From 2cbf4a12ea68dad9321e9380cebcc9cf983d1296 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Mon, 11 May 2020 11:32:45 +0200 Subject: [PATCH 04/11] Update another method --- src/core/qgslegendrenderer.cpp | 9 +++++---- src/core/qgslegendrenderer.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/core/qgslegendrenderer.cpp b/src/core/qgslegendrenderer.cpp index 954c6a096fe..452eeb6a050 100644 --- a/src/core/qgslegendrenderer.cpp +++ b/src/core/qgslegendrenderer.cpp @@ -76,13 +76,14 @@ QJsonObject QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &conte if ( !rootGroup ) return json; + json = exportLegendToJson( context, rootGroup ); json[QStringLiteral( "title" )] = mSettings.title(); - exportLegendToJson( context, rootGroup, json ); return json; } -void QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup, QJsonObject &json ) +QJsonObject QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup ) { + QJsonObject json; QJsonArray nodes; const QList childNodes = nodeGroup->children(); for ( QgsLayerTreeNode *node : childNodes ) @@ -93,10 +94,9 @@ void QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, Qgs const QModelIndex idx = mLegendModel->node2index( nodeGroup ); const QString text = mLegendModel->data( idx, Qt::DisplayRole ).toString(); - QJsonObject group; + QJsonObject group = exportLegendToJson( context, nodeGroup ); group[ QStringLiteral( "type" ) ] = QStringLiteral( "group" ); group[ QStringLiteral( "title" ) ] = text; - exportLegendToJson( context, nodeGroup, group ); nodes.append( group ); } else if ( QgsLayerTree::isLayer( node ) ) @@ -141,6 +141,7 @@ void QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &context, Qgs } json[QStringLiteral( "nodes" )] = nodes; + return json; } QSizeF QgsLegendRenderer::paintAndDetermineSize( QgsRenderContext &context ) diff --git a/src/core/qgslegendrenderer.h b/src/core/qgslegendrenderer.h index 12fdbd7f55c..e8b74cc3484 100644 --- a/src/core/qgslegendrenderer.h +++ b/src/core/qgslegendrenderer.h @@ -234,7 +234,7 @@ class CORE_EXPORT QgsLegendRenderer * * \since QGIS 3.8 */ - void exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup, QJsonObject &json ); + QJsonObject exportLegendToJson( const QgsRenderContext &context, QgsLayerTreeGroup *nodeGroup ); /** * Draws the legend using the specified render \a context, and returns the actual size of the legend. From 236db3903b139e3460eb5334f10eaa0948dd17c2 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Mon, 11 May 2020 11:47:32 +0200 Subject: [PATCH 05/11] Update api of exportToJson --- .../layertree/qgslayertreemodellegendnode.cpp | 4 +++- src/core/layertree/qgslayertreemodellegendnode.h | 2 +- src/core/qgslegendrenderer.cpp | 15 ++++++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/core/layertree/qgslayertreemodellegendnode.cpp b/src/core/layertree/qgslayertreemodellegendnode.cpp index f714eae01e3..2713f1b3f57 100644 --- a/src/core/layertree/qgslayertreemodellegendnode.cpp +++ b/src/core/layertree/qgslayertreemodellegendnode.cpp @@ -84,10 +84,12 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsLayerTreeModelLegendNode::draw( cons return im; } -void QgsLayerTreeModelLegendNode::exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) +QJsonObject QgsLayerTreeModelLegendNode::exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) { + QJsonObject json; exportSymbolToJson( settings, context, json ); exportSymbolTextToJson( settings, json ); + return json; } QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const diff --git a/src/core/layertree/qgslayertreemodellegendnode.h b/src/core/layertree/qgslayertreemodellegendnode.h index 662e39773ed..4b7a52582c0 100644 --- a/src/core/layertree/qgslayertreemodellegendnode.h +++ b/src/core/layertree/qgslayertreemodellegendnode.h @@ -223,7 +223,7 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject * \param json The json object to update * \since QGIS 3.8 */ - void exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ); + QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ); /** * Draws symbol on the left side of the item diff --git a/src/core/qgslegendrenderer.cpp b/src/core/qgslegendrenderer.cpp index 452eeb6a050..a2776c83ce5 100644 --- a/src/core/qgslegendrenderer.cpp +++ b/src/core/qgslegendrenderer.cpp @@ -101,9 +101,6 @@ QJsonObject QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &conte } else if ( QgsLayerTree::isLayer( node ) ) { - QJsonObject group; - group[ QStringLiteral( "type" ) ] = QStringLiteral( "layer" ); - QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node ); QString text; @@ -120,21 +117,25 @@ QJsonObject QgsLegendRenderer::exportLegendToJson( const QgsRenderContext &conte if ( legendNodes.count() == 1 ) { - legendNodes.at( 0 )->exportToJson( mSettings, context, group ); + QJsonObject group = legendNodes.at( 0 )->exportToJson( mSettings, context ); + group[ QStringLiteral( "type" ) ] = QStringLiteral( "layer" ); nodes.append( group ); } else if ( legendNodes.count() > 1 ) { + QJsonObject group; + group[ QStringLiteral( "type" ) ] = QStringLiteral( "layer" ); + group[ QStringLiteral( "title" ) ] = text; + QJsonArray symbols; for ( int j = 0; j < legendNodes.count(); j++ ) { QgsLayerTreeModelLegendNode *legendNode = legendNodes.at( j ); - QJsonObject symbol; - legendNode->exportToJson( mSettings, context, symbol ); + QJsonObject symbol = legendNode->exportToJson( mSettings, context ); symbols.append( symbol ); } - group[ QStringLiteral( "title" ) ] = text; group[ QStringLiteral( "symbols" ) ] = symbols; + nodes.append( group ); } } From 555d8863faf06a113102e6c7b228365caa843f2d Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Mon, 11 May 2020 11:48:03 +0200 Subject: [PATCH 06/11] Update sip binding --- .../auto_generated/layertree/qgslayertreemodellegendnode.sip.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in b/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in index 9df218b7b89..20d954c3b26 100644 --- a/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in +++ b/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in @@ -160,7 +160,7 @@ Default implementation calls drawSymbol() and drawSymbolText() methods. If ctx is ``None``, this is just first stage when preparing layout - without actual rendering. %End - void exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ); + QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ); %Docstring Entry point called from QgsLegendRenderer to do the rendering in a JSON object. From 91a6c5601e5448ce2bf672ca8ec58f623abe2d20 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Mon, 11 May 2020 13:55:36 +0200 Subject: [PATCH 07/11] Update exportSymbolToJson --- .../layertree/qgslayertreemodellegendnode.cpp | 41 +++++++++++-------- .../layertree/qgslayertreemodellegendnode.h | 18 +++----- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/core/layertree/qgslayertreemodellegendnode.cpp b/src/core/layertree/qgslayertreemodellegendnode.cpp index 2713f1b3f57..c6af2c7296e 100644 --- a/src/core/layertree/qgslayertreemodellegendnode.cpp +++ b/src/core/layertree/qgslayertreemodellegendnode.cpp @@ -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; } @@ -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(); 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 @@ -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 ) @@ -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(); } @@ -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 ) @@ -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; } // ------------------------------------------------------------------------- @@ -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 ); @@ -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; } // ------------------------------------------------------------------------- @@ -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 */ diff --git a/src/core/layertree/qgslayertreemodellegendnode.h b/src/core/layertree/qgslayertreemodellegendnode.h index 4b7a52582c0..fac8b070689 100644 --- a/src/core/layertree/qgslayertreemodellegendnode.h +++ b/src/core/layertree/qgslayertreemodellegendnode.h @@ -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 @@ -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(); @@ -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; @@ -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; @@ -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; @@ -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; From 1c28b6904441bcad983830a194d7896d9e6236d9 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Mon, 11 May 2020 13:55:59 +0200 Subject: [PATCH 08/11] Update sip binding --- .../qgslayertreemodellegendnode.sip.in | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in b/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in index 20d954c3b26..0a48977adcd 100644 --- a/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in +++ b/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in @@ -183,7 +183,7 @@ Draws symbol on the left side of the item :return: Real size of the symbol (may be bigger than "normal" symbol size from settings) %End - virtual void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const; + virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const; %Docstring Adds a symbol in base64 string within a JSON object with the key "icon". @@ -203,16 +203,6 @@ Draws label on the right side of the item :param symbolSize: Real size of the associated symbol - used for correct positioning when rendering :return: Size of the label (may span multiple lines) -%End - - void exportSymbolTextToJson( const QgsLegendSettings &settings, QJsonObject &json ) const; -%Docstring -Adds a label in a JSON object with the key "title". - -:param settings: Legend layout configuration -:param json: The json object to update - -.. versionadded:: 3.8 %End signals: @@ -269,7 +259,7 @@ Constructor for QgsSymbolLegendNode. virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const; - virtual void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const; + virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const; virtual void setEmbeddedInParent( bool embedded ); @@ -519,7 +509,7 @@ Constructor for QgsImageLegendNode. virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const; - virtual void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const; + virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const; }; @@ -553,7 +543,7 @@ Constructor for QgsRasterSymbolLegendNode. virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const; - virtual void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const; + virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const; }; @@ -586,7 +576,7 @@ Constructor for QgsWmsLegendNode. virtual QSizeF drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const; - virtual void exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context, QJsonObject &json ) const; + virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const; virtual void invalidateMapBasedData(); From c10169e2628c75cc3f6237c6cd28b0244697b152 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Tue, 12 May 2020 08:21:55 +0200 Subject: [PATCH 09/11] Use QStringLiteral --- src/core/layertree/qgslayertreemodellegendnode.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/layertree/qgslayertreemodellegendnode.cpp b/src/core/layertree/qgslayertreemodellegendnode.cpp index c6af2c7296e..19e463962b4 100644 --- a/src/core/layertree/qgslayertreemodellegendnode.cpp +++ b/src/core/layertree/qgslayertreemodellegendnode.cpp @@ -145,7 +145,7 @@ QJsonObject QgsLayerTreeModelLegendNode::exportSymbolToJson( const QgsLegendSett const QString base64 = QString::fromLatin1( byteArray.toBase64().data() ); QJsonObject json; - json[ "icon" ] = base64; + json[ QStringLiteral( "icon" ) ] = base64; return json; } @@ -705,7 +705,7 @@ QJsonObject QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &se const QString base64 = QString::fromLatin1( byteArray.toBase64().data() ); QJsonObject json; - json[ "icon" ] = base64; + json[ QStringLiteral( "icon" ) ] = base64; return json; } @@ -872,7 +872,7 @@ QJsonObject QgsImageLegendNode::exportSymbolToJson( const QgsLegendSettings &, c const QString base64 = QString::fromLatin1( byteArray.toBase64().data() ); QJsonObject json; - json[ "icon" ] = base64; + json[ QStringLiteral( "icon" ) ] = base64; return json; } @@ -996,7 +996,7 @@ QJsonObject QgsRasterSymbolLegendNode::exportSymbolToJson( const QgsLegendSettin const QString base64 = QString::fromLatin1( byteArray.toBase64().data() ); QJsonObject json; - json[ "icon" ] = base64; + json[ QStringLiteral( "icon" ) ] = base64; return json; } @@ -1094,7 +1094,7 @@ QJsonObject QgsWmsLegendNode::exportSymbolToJson( const QgsLegendSettings &, con const QString base64 = QString::fromLatin1( byteArray.toBase64().data() ); QJsonObject json; - json[ "icon" ] = base64; + json[ QStringLiteral( "icon" ) ] = base64; return json; } From 86503e53fa6369adb2e2dd85bb08ffda65780508 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Tue, 12 May 2020 13:16:54 +0200 Subject: [PATCH 10/11] Update doc --- .../auto_generated/layertree/qgslayertreemodellegendnode.sip.in | 2 -- src/core/layertree/qgslayertreemodellegendnode.h | 2 -- 2 files changed, 4 deletions(-) diff --git a/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in b/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in index 0a48977adcd..3b28e1c679a 100644 --- a/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in +++ b/python/core/auto_generated/layertree/qgslayertreemodellegendnode.sip.in @@ -167,7 +167,6 @@ JSON object. :param settings: Legend layout configuration :param context: Rendering context -:param json: The json object to update .. versionadded:: 3.8 %End @@ -189,7 +188,6 @@ Adds a symbol in base64 string within a JSON object with the key "icon". :param settings: Legend layout configuration :param context: Rendering context -:param json: The json object to update .. versionadded:: 3.8 %End diff --git a/src/core/layertree/qgslayertreemodellegendnode.h b/src/core/layertree/qgslayertreemodellegendnode.h index fac8b070689..5eb35fa6169 100644 --- a/src/core/layertree/qgslayertreemodellegendnode.h +++ b/src/core/layertree/qgslayertreemodellegendnode.h @@ -220,7 +220,6 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject * JSON object. * \param settings Legend layout configuration * \param context Rendering context - * \param json The json object to update * \since QGIS 3.8 */ QJsonObject exportToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ); @@ -238,7 +237,6 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject * Adds a symbol in base64 string within a JSON object with the key "icon". * \param settings Legend layout configuration * \param context Rendering context - * \param json The json object to update * \since QGIS 3.8 */ virtual QJsonObject exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const; From 3cfe500f35de59f2592a4097f0898e64551d1446 Mon Sep 17 00:00:00 2001 From: Blottiere Paul Date: Tue, 12 May 2020 21:07:20 +0200 Subject: [PATCH 11/11] Add Python unit test --- tests/src/python/CMakeLists.txt | 1 + tests/src/python/test_qgslegendrenderer.py | 50 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/src/python/test_qgslegendrenderer.py diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index 757cd04900a..7dd2be7f880 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -138,6 +138,7 @@ ADD_PYTHON_TEST(PyQgsLayoutUnitsComboBox test_qgslayoutunitscombobox.py) ADD_PYTHON_TEST(PyQgsLegendPatchShape test_qgslegendpatchshape.py) ADD_PYTHON_TEST(PyQgsLegendPatchShapeButton test_qgslegendpatchshapebutton.py) ADD_PYTHON_TEST(PyQgsLegendPatchShapeWidget test_qgslegendpatchshapewidget.py) +ADD_PYTHON_TEST(PyQgsLegendRenderer test_qgslegendrenderer.py) ADD_PYTHON_TEST(PyQgsLineSegment test_qgslinesegment.py) ADD_PYTHON_TEST(PyQgsLineSymbolLayers test_qgslinesymbollayers.py) ADD_PYTHON_TEST(PyQgsLocalDefaultSettings test_qgslocaldefaultsettings.py) diff --git a/tests/src/python/test_qgslegendrenderer.py b/tests/src/python/test_qgslegendrenderer.py new file mode 100644 index 00000000000..0f85018772f --- /dev/null +++ b/tests/src/python/test_qgslegendrenderer.py @@ -0,0 +1,50 @@ +# coding=utf-8 +""""Test QgsLegendRenderer JSON export + +.. note:: This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + +Run with ctest -V -R PyQgsLegendRenderer + +""" + +__author__ = 'elpaso@itopen.it' +__date__ = '2020-04-29' +__copyright__ = 'Copyright 2020, ItOpen' + +import os + +from qgis.core import ( + QgsProject, + QgsLegendModel, + QgsLegendSettings, + QgsLegendRenderer, + QgsRenderContext, +) +from qgis.testing import start_app, unittest +from utilities import unitTestDataPath + +QGISAPP = start_app() +TEST_DATA_DIR = unitTestDataPath() + + +class TestPyQgsLegendRenderer(unittest.TestCase): + + def test_json_export(self): + + project = QgsProject() + self.assertTrue(project.read(os.path.join(unitTestDataPath('qgis_server'), 'test_project.qgs'))) + model = QgsLegendModel(project.layerTreeRoot()) + ctx = QgsRenderContext() + settings = QgsLegendSettings() + renderer = QgsLegendRenderer(model, settings) + nodes = renderer.exportLegendToJson(ctx)['nodes'].toVariant() + self.assertEqual(len(nodes), 7) + self.assertEqual(nodes[0]['type'], 'layer') + self.assertEqual(nodes[0]['title'], 'testlayer') + + +if __name__ == '__main__': + unittest.main()