Skip to content
Permalink
Browse files

Add drawLegend method for json

  • Loading branch information
pblottiere committed Feb 27, 2019
1 parent 1e8aec4 commit 9651efa0f6f5aeaa807977bccff9df85495cbecb
@@ -71,6 +71,11 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsLayerTreeModelLegendNode::draw( cons
return im;
}

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

QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
{
@@ -129,6 +134,13 @@ QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &set
return labelSize;
}

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

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

QgsSymbolLegendNode::QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent )
@@ -110,6 +110,8 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
*/
virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );

void draw( const QgsLegendSettings &settings, QJsonObject &json );

/**
* Draws symbol on the left side of the item
* \param settings Legend layout configuration
@@ -128,6 +130,8 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
*/
virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;

void drawSymbolText( 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();
@@ -25,6 +25,7 @@
#include "qgsvectorlayer.h"
#include "qgsexpressioncontextutils.h"

#include <QJsonObject>
#include <QPainter>


@@ -45,6 +46,79 @@ void QgsLegendRenderer::drawLegend( QPainter *painter )
paintAndDetermineSize( painter );
}

void QgsLegendRenderer::drawLegend( QJsonObject &json )
{
QgsLayerTreeGroup *rootGroup = mLegendModel->rootGroup();
if ( !rootGroup )
return;

json["title"] = mSettings.title();
drawLegend( rootGroup, json );
}

void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &json )
{
QJsonArray nodes;
Q_FOREACH ( QgsLayerTreeNode *node, nodeGroup->children() )
{
if ( QgsLayerTree::isGroup( node ) )
{
QgsLayerTreeGroup *nodeGroup = QgsLayerTree::toGroup( node );

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

QJsonObject group;
group[ "type" ] = "group";
group[ "title" ] = text;
drawLegend( nodeGroup, group );
nodes.append( group );
}
else if ( QgsLayerTree::isLayer( node ) )
{
QJsonObject group;
group[ "type" ] = "layer";

QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );

QString text;
if ( nodeLegendStyle( nodeLayer ) != QgsLegendStyle::Hidden )
{
QModelIndex idx = mLegendModel->node2index( nodeLayer );
text = mLegendModel->data( idx, Qt::DisplayRole ).toString();
}

QList<QgsLayerTreeModelLegendNode *> legendNodes = mLegendModel->layerLegendNodes( nodeLayer );

if ( legendNodes.isEmpty() && mLegendModel->legendFilterMapSettings() )
continue;

if ( legendNodes.count() == 1 )
{
legendNodes.at( 0 )->draw( mSettings, group );
nodes.append( group );
}
else if ( legendNodes.count() > 1 )
{
QJsonArray symbols;
for ( int j = 0; j < legendNodes.count(); j++ )
{
QgsLayerTreeModelLegendNode *legendNode = legendNodes.at( j );
QJsonObject symbol;
legendNode->draw( mSettings, symbol );
symbols.append( symbol );
}
group[ "title" ] = text;
group[ "symbols" ] = symbols;
nodes.append( group );
}
}
}

json["nodes"] = nodes;
}

QSizeF QgsLegendRenderer::paintAndDetermineSize( QPainter *painter )
{
return paintAndDetermineSizeInternal( nullptr, painter );
@@ -21,6 +21,7 @@

class QRectF;
class QStandardItem;
class QJsonObject;

class QgsLayerTreeGroup;
class QgsLayerTreeLayer;
@@ -92,6 +93,8 @@ class CORE_EXPORT QgsLegendRenderer
*/
void drawLegend( QgsRenderContext &context );

void drawLegend( QJsonObject &json );

/**
* Sets the \a style of a \a node.
*
@@ -232,6 +235,8 @@ class CORE_EXPORT QgsLegendRenderer
*/
QSizeF drawGroupTitle( QgsLayerTreeGroup *nodeGroup, QPainter *painter = nullptr, QPointF point = QPointF() );

void drawLegend( 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 9651efa

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