Skip to content

Commit

Permalink
Add drawLegend method for json
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Mar 19, 2019
1 parent 1e8aec4 commit 9651efa
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 )
Expand Down
4 changes: 4 additions & 0 deletions src/core/layertree/qgslayertreemodellegendnode.h
Expand Up @@ -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
Expand All @@ -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();
Expand Down
74 changes: 74 additions & 0 deletions src/core/qgslegendrenderer.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsvectorlayer.h"
#include "qgsexpressioncontextutils.h"

#include <QJsonObject>
#include <QPainter>


Expand All @@ -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 );
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgslegendrenderer.h
Expand Up @@ -21,6 +21,7 @@

class QRectF;
class QStandardItem;
class QJsonObject;

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

void drawLegend( QJsonObject &json );

/**
* Sets the \a style of a \a node.
*
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 9651efa

Please sign in to comment.