Skip to content

Commit 9651efa

Browse files
committed
Add drawLegend method for json
1 parent 1e8aec4 commit 9651efa

4 files changed

+95
-0
lines changed

src/core/layertree/qgslayertreemodellegendnode.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ QgsLayerTreeModelLegendNode::ItemMetrics QgsLayerTreeModelLegendNode::draw( cons
7171
return im;
7272
}
7373

74+
void QgsLayerTreeModelLegendNode::draw( const QgsLegendSettings &settings, QJsonObject &json )
75+
{
76+
drawSymbolText( settings, json );
77+
json[ "symbol" ] = "TODO";
78+
}
7479

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

137+
void QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &settings, QJsonObject &json ) const
138+
{
139+
QgsExpressionContext tempContext;
140+
const QString text = data( Qt::DisplayRole ).toString();
141+
json[ "title" ] = text;
142+
}
143+
132144
// -------------------------------------------------------------------------
133145

134146
QgsSymbolLegendNode::QgsSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent )

src/core/layertree/qgslayertreemodellegendnode.h

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
110110
*/
111111
virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );
112112

113+
void draw( const QgsLegendSettings &settings, QJsonObject &json );
114+
113115
/**
114116
* Draws symbol on the left side of the item
115117
* \param settings Legend layout configuration
@@ -128,6 +130,8 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
128130
*/
129131
virtual QSizeF drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const;
130132

133+
void drawSymbolText( const QgsLegendSettings &settings, QJsonObject &json ) const;
134+
131135
signals:
132136
//! Emitted on internal data change so the layer tree model can forward the signal to views
133137
void dataChanged();

src/core/qgslegendrenderer.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgsvectorlayer.h"
2626
#include "qgsexpressioncontextutils.h"
2727

28+
#include <QJsonObject>
2829
#include <QPainter>
2930

3031

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

49+
void QgsLegendRenderer::drawLegend( QJsonObject &json )
50+
{
51+
QgsLayerTreeGroup *rootGroup = mLegendModel->rootGroup();
52+
if ( !rootGroup )
53+
return;
54+
55+
json["title"] = mSettings.title();
56+
drawLegend( rootGroup, json );
57+
}
58+
59+
void QgsLegendRenderer::drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &json )
60+
{
61+
QJsonArray nodes;
62+
Q_FOREACH ( QgsLayerTreeNode *node, nodeGroup->children() )
63+
{
64+
if ( QgsLayerTree::isGroup( node ) )
65+
{
66+
QgsLayerTreeGroup *nodeGroup = QgsLayerTree::toGroup( node );
67+
68+
QModelIndex idx = mLegendModel->node2index( nodeGroup );
69+
QgsExpressionContext tempContext;
70+
const QString text = mLegendModel->data( idx, Qt::DisplayRole ).toString();
71+
72+
QJsonObject group;
73+
group[ "type" ] = "group";
74+
group[ "title" ] = text;
75+
drawLegend( nodeGroup, group );
76+
nodes.append( group );
77+
}
78+
else if ( QgsLayerTree::isLayer( node ) )
79+
{
80+
QJsonObject group;
81+
group[ "type" ] = "layer";
82+
83+
QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
84+
85+
QString text;
86+
if ( nodeLegendStyle( nodeLayer ) != QgsLegendStyle::Hidden )
87+
{
88+
QModelIndex idx = mLegendModel->node2index( nodeLayer );
89+
text = mLegendModel->data( idx, Qt::DisplayRole ).toString();
90+
}
91+
92+
QList<QgsLayerTreeModelLegendNode *> legendNodes = mLegendModel->layerLegendNodes( nodeLayer );
93+
94+
if ( legendNodes.isEmpty() && mLegendModel->legendFilterMapSettings() )
95+
continue;
96+
97+
if ( legendNodes.count() == 1 )
98+
{
99+
legendNodes.at( 0 )->draw( mSettings, group );
100+
nodes.append( group );
101+
}
102+
else if ( legendNodes.count() > 1 )
103+
{
104+
QJsonArray symbols;
105+
for ( int j = 0; j < legendNodes.count(); j++ )
106+
{
107+
QgsLayerTreeModelLegendNode *legendNode = legendNodes.at( j );
108+
QJsonObject symbol;
109+
legendNode->draw( mSettings, symbol );
110+
symbols.append( symbol );
111+
}
112+
group[ "title" ] = text;
113+
group[ "symbols" ] = symbols;
114+
nodes.append( group );
115+
}
116+
}
117+
}
118+
119+
json["nodes"] = nodes;
120+
}
121+
48122
QSizeF QgsLegendRenderer::paintAndDetermineSize( QPainter *painter )
49123
{
50124
return paintAndDetermineSizeInternal( nullptr, painter );

src/core/qgslegendrenderer.h

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
class QRectF;
2323
class QStandardItem;
24+
class QJsonObject;
2425

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

96+
void drawLegend( QJsonObject &json );
97+
9598
/**
9699
* Sets the \a style of a \a node.
97100
*
@@ -232,6 +235,8 @@ class CORE_EXPORT QgsLegendRenderer
232235
*/
233236
QSizeF drawGroupTitle( QgsLayerTreeGroup *nodeGroup, QPainter *painter = nullptr, QPointF point = QPointF() );
234237

238+
void drawLegend( QgsLayerTreeGroup *nodeGroup, QJsonObject &json );
239+
235240
/**
236241
* Draws the legend using the specified render \a context, and returns the actual size of the legend.
237242
*

0 commit comments

Comments
 (0)