QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsmaplayerlegend.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayerlegend.cpp
3 --------------------------------------
4 Date : July 2014
5 Copyright : (C) 2014 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsmaplayerlegend.h"
17#include "qgsiconutils.h"
18#include "qgsimagecache.h"
19#include "qgssettings.h"
20#include "qgslayertree.h"
22#include "qgsmeshlayer.h"
23#include "qgspluginlayer.h"
24#include "qgsrasterlayer.h"
25#include "qgsrenderer.h"
26#include "qgsvectorlayer.h"
27#include "qgspointcloudlayer.h"
28#include "qgsdiagramrenderer.h"
29#include "qgssymbollayerutils.h"
31#include "qgsrasterrenderer.h"
35
37 : QObject( parent )
38{
39}
40
41void QgsMapLayerLegend::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
42{
43 Q_UNUSED( elem )
44 Q_UNUSED( context )
45}
46
47QDomElement QgsMapLayerLegend::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
48{
49 Q_UNUSED( doc )
50 Q_UNUSED( context )
51 return QDomElement();
52}
53
55{
56 return new QgsDefaultVectorLayerLegend( vl );
57}
58
60{
61 return new QgsDefaultRasterLayerLegend( rl );
62}
63
65{
66 return new QgsDefaultMeshLayerLegend( ml );
67}
68
70{
71 return new QgsDefaultPointCloudLayerLegend( layer );
72}
73
74// -------------------------------------------------------------------------
75
76
77void QgsMapLayerLegendUtils::setLegendNodeOrder( QgsLayerTreeLayer *nodeLayer, const QList<int> &order )
78{
79 QStringList orderStr;
80 const auto constOrder = order;
81 for ( const int id : constOrder )
82 orderStr << QString::number( id );
83 const QString str = orderStr.isEmpty() ? QStringLiteral( "empty" ) : orderStr.join( QLatin1Char( ',' ) );
84
85 nodeLayer->setCustomProperty( QStringLiteral( "legend/node-order" ), str );
86}
87
88static int _originalLegendNodeCount( QgsLayerTreeLayer *nodeLayer )
89{
90 // this is not particularly efficient way of finding out number of legend nodes
91 const QList<QgsLayerTreeModelLegendNode *> lst = nodeLayer->layer()->legend()->createLayerTreeModelLegendNodes( nodeLayer );
92 const int numNodes = lst.count();
93 qDeleteAll( lst );
94 return numNodes;
95}
96
97static QList<int> _makeNodeOrder( QgsLayerTreeLayer *nodeLayer )
98{
99 if ( !nodeLayer->layer() || !nodeLayer->layer()->legend() )
100 {
101 QgsDebugMsg( QStringLiteral( "Legend node order manipulation is invalid without existing legend" ) );
102 return QList<int>();
103 }
104
105 const int numNodes = _originalLegendNodeCount( nodeLayer );
106
107 QList<int> order;
108 order.reserve( numNodes );
109 for ( int i = 0; i < numNodes; ++i )
110 order << i;
111 return order;
112}
113
115{
116 const QString orderStr = nodeLayer->customProperty( QStringLiteral( "legend/node-order" ) ).toString();
117
118 if ( orderStr.isEmpty() )
119 return _makeNodeOrder( nodeLayer );
120
121 if ( orderStr == QLatin1String( "empty" ) )
122 return QList<int>();
123
124 const int numNodes = _originalLegendNodeCount( nodeLayer );
125
126 QList<int> lst;
127 const auto constSplit = orderStr.split( ',' );
128 for ( const QString &item : constSplit )
129 {
130 bool ok;
131 const int id = item.toInt( &ok );
132 if ( !ok || id < 0 || id >= numNodes )
133 return _makeNodeOrder( nodeLayer );
134
135 lst << id;
136 }
137
138 return lst;
139}
140
142{
143 return nodeLayer->customProperties().contains( QStringLiteral( "legend/node-order" ) );
144}
145
146void QgsMapLayerLegendUtils::setLegendNodeUserLabel( QgsLayerTreeLayer *nodeLayer, int originalIndex, const QString &newLabel )
147{
148 nodeLayer->setCustomProperty( "legend/label-" + QString::number( originalIndex ), newLabel );
149}
150
152{
153 return nodeLayer->customProperty( "legend/label-" + QString::number( originalIndex ) ).toString();
154}
155
157{
158 return nodeLayer->customProperties().contains( "legend/label-" + QString::number( originalIndex ) );
159}
160
162{
163 QDomDocument patchDoc;
164 QDomElement patchElem = patchDoc.createElement( QStringLiteral( "patch" ) );
165 shape.writeXml( patchElem, patchDoc, QgsReadWriteContext() );
166 patchDoc.appendChild( patchElem );
167 nodeLayer->setCustomProperty( "legend/patch-shape-" + QString::number( originalIndex ), patchDoc.toString() );
168}
169
171{
172 const QString patchDef = nodeLayer->customProperty( "legend/patch-shape-" + QString::number( originalIndex ) ).toString();
173 if ( patchDef.isEmpty() )
174 return QgsLegendPatchShape();
175
176 QDomDocument doc( QStringLiteral( "patch" ) );
177 doc.setContent( patchDef );
179 shape.readXml( doc.documentElement(), QgsReadWriteContext() );
180 return shape;
181}
182
183void QgsMapLayerLegendUtils::setLegendNodeSymbolSize( QgsLayerTreeLayer *nodeLayer, int originalIndex, QSizeF size )
184{
185 if ( size.isValid() )
186 nodeLayer->setCustomProperty( "legend/symbol-size-" + QString::number( originalIndex ), QgsSymbolLayerUtils::encodeSize( size ) );
187 else
188 nodeLayer->removeCustomProperty( "legend/symbol-size-" + QString::number( originalIndex ) );
189}
190
192{
193 const QString size = nodeLayer->customProperty( "legend/symbol-size-" + QString::number( originalIndex ) ).toString();
194 if ( size.isEmpty() )
195 return QSizeF();
196 else
197 return QgsSymbolLayerUtils::decodeSize( size );
198}
199
200void QgsMapLayerLegendUtils::setLegendNodeCustomSymbol( QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsSymbol *symbol )
201{
202 if ( symbol )
203 {
204 QDomDocument doc;
205 QgsReadWriteContext rwContext;
206 rwContext.setPathResolver( QgsProject::instance()->pathResolver() );
207 const QDomElement elem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "custom symbol" ), symbol, doc, rwContext );
208 doc.appendChild( elem );
209 nodeLayer->setCustomProperty( "legend/custom-symbol-" + QString::number( originalIndex ), doc.toString() );
210 }
211 else
212 nodeLayer->removeCustomProperty( "legend/custom-symbol-" + QString::number( originalIndex ) );
213}
214
216{
217 const QString symbolDef = nodeLayer->customProperty( "legend/custom-symbol-" + QString::number( originalIndex ) ).toString();
218 if ( symbolDef.isEmpty() )
219 return nullptr;
220
221 QDomDocument doc;
222 doc.setContent( symbolDef );
223 const QDomElement elem = doc.documentElement();
224
225 QgsReadWriteContext rwContext;
226 rwContext.setPathResolver( QgsProject::instance()->pathResolver() );
227
228 return QgsSymbolLayerUtils::loadSymbol( elem, rwContext );
229}
230
232{
233 if ( settings )
234 {
235 QDomDocument doc;
236 QgsReadWriteContext rwContext;
237 rwContext.setPathResolver( QgsProject::instance()->pathResolver() );
238 QDomElement elem = doc.createElement( QStringLiteral( "rampSettings" ) );
239 settings->writeXml( doc, elem, rwContext );
240 doc.appendChild( elem );
241 nodeLayer->setCustomProperty( "legend/custom-ramp-settings-" + QString::number( originalIndex ), doc.toString() );
242 }
243 else
244 nodeLayer->removeCustomProperty( "legend/custom-ramp-settings-" + QString::number( originalIndex ) );
245}
246
248{
249 const QString settingsDef = nodeLayer->customProperty( "legend/custom-ramp-settings-" + QString::number( originalIndex ) ).toString();
250 if ( settingsDef.isEmpty() )
251 return nullptr;
252
253 QDomDocument doc;
254 doc.setContent( settingsDef );
255 const QDomElement elem = doc.documentElement();
256
257 QgsReadWriteContext rwContext;
258 rwContext.setPathResolver( QgsProject::instance()->pathResolver() );
259
261 settings.readXml( elem, rwContext );
262 return new QgsColorRampLegendNodeSettings( settings );
263}
264
265void QgsMapLayerLegendUtils::setLegendNodeColumnBreak( QgsLayerTreeLayer *nodeLayer, int originalIndex, bool columnBreakBeforeNode )
266{
267 if ( columnBreakBeforeNode )
268 nodeLayer->setCustomProperty( "legend/column-break-" + QString::number( originalIndex ), QStringLiteral( "1" ) );
269 else
270 nodeLayer->removeCustomProperty( "legend/column-break-" + QString::number( originalIndex ) );
271}
272
274{
275 return nodeLayer->customProperty( "legend/column-break-" + QString::number( originalIndex ) ).toInt();
276}
277
278void QgsMapLayerLegendUtils::applyLayerNodeProperties( QgsLayerTreeLayer *nodeLayer, QList<QgsLayerTreeModelLegendNode *> &nodes )
279{
280 // handle user labels
281 int i = 0;
282 const auto constNodes = nodes;
283 for ( QgsLayerTreeModelLegendNode *legendNode : constNodes )
284 {
285 const QString userLabel = QgsMapLayerLegendUtils::legendNodeUserLabel( nodeLayer, i );
286 if ( !userLabel.isNull() )
287 legendNode->setUserLabel( userLabel );
288
289 if ( QgsSymbolLegendNode *symbolNode = dynamic_cast< QgsSymbolLegendNode * >( legendNode ) )
290 {
292 symbolNode->setPatchShape( shape );
293
294 symbolNode->setCustomSymbol( QgsMapLayerLegendUtils::legendNodeCustomSymbol( nodeLayer, i ) );
295 }
296 else if ( QgsColorRampLegendNode *colorRampNode = dynamic_cast< QgsColorRampLegendNode * >( legendNode ) )
297 {
298 const std::unique_ptr< QgsColorRampLegendNodeSettings > settings( QgsMapLayerLegendUtils::legendNodeColorRampSettings( nodeLayer, i ) );
299 if ( settings )
300 {
301 colorRampNode->setSettings( *settings );
302 }
303 }
304
305 const QSizeF userSize = QgsMapLayerLegendUtils::legendNodeSymbolSize( nodeLayer, i );
306 if ( userSize.isValid() )
307 {
308 legendNode->setUserPatchSize( userSize );
309 }
310
311 if ( legendNodeColumnBreak( nodeLayer, i ) )
312 legendNode->setColumnBreak( true );
313
314 i++;
315 }
316
317 // handle user order of nodes
319 {
320 const QList<int> order = QgsMapLayerLegendUtils::legendNodeOrder( nodeLayer );
321
322 QList<QgsLayerTreeModelLegendNode *> newOrder;
323 QSet<int> usedIndices;
324 const auto constOrder = order;
325 for ( const int idx : constOrder )
326 {
327 if ( usedIndices.contains( idx ) )
328 {
329 QgsDebugMsg( QStringLiteral( "invalid node order. ignoring." ) );
330 return;
331 }
332
333 newOrder << nodes[idx];
334 usedIndices << idx;
335 }
336
337 // delete unused nodes
338 for ( int i = 0; i < nodes.count(); ++i )
339 {
340 if ( !usedIndices.contains( i ) )
341 delete nodes[i];
342 }
343
344 nodes = newOrder;
345 }
346
347}
348
349// -------------------------------------------------------------------------
350
351
353 : mLayer( vl )
354{
357}
358
360{
361 QList<QgsLayerTreeModelLegendNode *> nodes;
362
363 if ( mLayer )
364 {
365 const QString placeholderImage = mLayer->legendPlaceholderImage();
366 if ( !placeholderImage.isEmpty() )
367 {
368 bool fitsInCache;
369 const QImage img = QgsApplication::imageCache()->pathAsImage( placeholderImage, QSize(), false, 1.0, fitsInCache );
370 nodes << new QgsImageLegendNode( nodeLayer, img );
371 return nodes;
372 }
373 }
374
375 QgsFeatureRenderer *r = mLayer->renderer();
376 if ( !r )
377 return nodes;
378
379 if ( nodeLayer->customProperty( QStringLiteral( "showFeatureCount" ), 0 ).toBool() )
380 mLayer->countSymbolFeatures();
381
382 const QgsSettings settings;
383 if ( settings.value( QStringLiteral( "qgis/showLegendClassifiers" ), false ).toBool() && !r->legendClassificationAttribute().isEmpty() )
384 {
385 nodes.append( new QgsSimpleLegendNode( nodeLayer, r->legendClassificationAttribute() ) );
386 }
387
388 const auto constLegendSymbolItems = r->legendSymbolItems();
389 for ( const QgsLegendSymbolItem &i : constLegendSymbolItems )
390 {
391 if ( auto *lDataDefinedSizeLegendSettings = i.dataDefinedSizeLegendSettings() )
392 nodes << new QgsDataDefinedSizeLegendNode( nodeLayer, *lDataDefinedSizeLegendSettings );
393 else
394 {
396 if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( i.ruleKey() ) )
397 {
398 legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( i.ruleKey() ) );
399 legendNode->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
400 }
401 nodes << legendNode;
402 }
403 }
404
405 if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() )
406 nodes[0]->setEmbeddedInParent( true );
407
408
409 if ( mLayer->diagramsEnabled() )
410 {
411 const auto constLegendItems = mLayer->diagramRenderer()->legendItems( nodeLayer );
412 for ( QgsLayerTreeModelLegendNode *i : constLegendItems )
413 {
414 nodes.append( i );
415 }
416 }
417
418 if ( mLayer->labelsEnabled() && mShowLabelLegend )
419 {
420 const QgsAbstractVectorLayerLabeling *labeling = mLayer->labeling();
421 if ( labeling )
422 {
423 const QStringList pList = labeling->subProviders();
424 for ( int i = 0; i < pList.size(); ++i )
425 {
426 const QgsPalLayerSettings s = labeling->settings( pList.at( i ) );
427 QString description;
428 const QgsRuleBasedLabeling *ruleBasedLabeling = dynamic_cast<const QgsRuleBasedLabeling *>( labeling );
429 if ( ruleBasedLabeling && ruleBasedLabeling->rootRule() )
430 {
431 const QgsRuleBasedLabeling::Rule *rule = ruleBasedLabeling->rootRule()->findRuleByKey( pList.at( i ) );
432 if ( rule )
433 {
434 description = rule->description();
435 }
436 }
437 QgsVectorLabelLegendNode *node = new QgsVectorLabelLegendNode( nodeLayer, s );
438 node->setUserLabel( description );
439 nodes.append( node );
440 }
441 }
442 }
443
444
445 return nodes;
446}
447
448void QgsDefaultVectorLayerLegend::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
449{
450 mTextOnSymbolEnabled = false;
451 mTextOnSymbolTextFormat = QgsTextFormat();
452 mTextOnSymbolContent.clear();
453
454 mShowLabelLegend = elem.attribute( QStringLiteral( "showLabelLegend" ), QStringLiteral( "0" ) ).compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0;
455
456 const QDomElement tosElem = elem.firstChildElement( QStringLiteral( "text-on-symbol" ) );
457 if ( !tosElem.isNull() )
458 {
459 mTextOnSymbolEnabled = true;
460 const QDomElement tosFormatElem = tosElem.firstChildElement( QStringLiteral( "text-style" ) );
461 mTextOnSymbolTextFormat.readXml( tosFormatElem, context );
462 const QDomElement tosContentElem = tosElem.firstChildElement( QStringLiteral( "content" ) );
463 QDomElement tosContentItemElem = tosContentElem.firstChildElement( QStringLiteral( "item" ) );
464 while ( !tosContentItemElem.isNull() )
465 {
466 mTextOnSymbolContent.insert( tosContentItemElem.attribute( QStringLiteral( "key" ) ), tosContentItemElem.attribute( QStringLiteral( "value" ) ) );
467 tosContentItemElem = tosContentItemElem.nextSiblingElement( QStringLiteral( "item" ) );
468 }
469 }
470}
471
472QDomElement QgsDefaultVectorLayerLegend::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
473{
474 QDomElement elem = doc.createElement( QStringLiteral( "legend" ) );
475 elem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "default-vector" ) );
476 elem.setAttribute( QStringLiteral( "showLabelLegend" ), mShowLabelLegend );
477
478 if ( mTextOnSymbolEnabled )
479 {
480 QDomElement tosElem = doc.createElement( QStringLiteral( "text-on-symbol" ) );
481 const QDomElement tosFormatElem = mTextOnSymbolTextFormat.writeXml( doc, context );
482 tosElem.appendChild( tosFormatElem );
483 QDomElement tosContentElem = doc.createElement( QStringLiteral( "content" ) );
484 for ( auto it = mTextOnSymbolContent.constBegin(); it != mTextOnSymbolContent.constEnd(); ++it )
485 {
486 QDomElement tosContentItemElem = doc.createElement( QStringLiteral( "item" ) );
487 tosContentItemElem.setAttribute( QStringLiteral( "key" ), it.key() );
488 tosContentItemElem.setAttribute( QStringLiteral( "value" ), it.value() );
489 tosContentElem.appendChild( tosContentItemElem );
490 }
491 tosElem.appendChild( tosContentElem );
492 elem.appendChild( tosElem );
493 }
494
495 return elem;
496}
497
498
499// -------------------------------------------------------------------------
500
501
503 : mLayer( rl )
504{
506}
507
509{
510 QList<QgsLayerTreeModelLegendNode *> nodes;
511
512 // temporary solution for WMS. Ideally should be done with a delegate.
513 if ( mLayer->dataProvider() && mLayer->dataProvider()->supportsLegendGraphic() )
514 {
515 nodes << new QgsWmsLegendNode( nodeLayer );
516 }
517
518 const QString placeholderImage = mLayer->legendPlaceholderImage();
519 if ( !placeholderImage.isEmpty() )
520 {
521 bool fitsInCache;
522 const QImage img = QgsApplication::imageCache()->pathAsImage( placeholderImage, QSize(), false, 1.0, fitsInCache );
523 nodes << new QgsImageLegendNode( nodeLayer, img );
524 }
525 else if ( mLayer->renderer() )
526 nodes.append( mLayer->renderer()->createLegendNodes( nodeLayer ) );
527 return nodes;
528}
529
530// -------------------------------------------------------------------------
531
533 : mLayer( ml )
534{
536}
537
539{
540 QList<QgsLayerTreeModelLegendNode *> nodes;
541
542 const QgsMeshRendererSettings rendererSettings = mLayer->rendererSettings();
543
544 const int indexScalar = rendererSettings.activeScalarDatasetGroup();
545 const int indexVector = rendererSettings.activeVectorDatasetGroup();
546
547 QString name;
548 if ( indexScalar > -1 && indexVector > -1 && indexScalar != indexVector )
549 name = QString( "%1 / %2" ).arg( mLayer->datasetGroupMetadata( indexScalar ).name(), mLayer->datasetGroupMetadata( indexVector ).name() );
550 else if ( indexScalar > -1 )
551 name = mLayer->datasetGroupMetadata( indexScalar ).name();
552 else if ( indexVector > -1 )
553 name = mLayer->datasetGroupMetadata( indexVector ).name();
554 else
555 {
556 // neither contours nor vectors get rendered - no legend needed
557 return nodes;
558 }
559
560 nodes << new QgsSimpleLegendNode( nodeLayer, name );
561
562 if ( indexScalar > -1 )
563 {
564 const QgsMeshRendererScalarSettings settings = rendererSettings.scalarSettings( indexScalar );
565 const QgsColorRampShader shader = settings.colorRampShader();
566 switch ( shader.colorRampType() )
567 {
569 if ( ! shader.legendSettings() || shader.legendSettings()->useContinuousLegend() )
570 {
571 // for interpolated shaders we use a ramp legend node
572 if ( !shader.colorRampItemList().isEmpty() )
573 {
574 nodes << new QgsColorRampLegendNode( nodeLayer, shader.createColorRamp(),
576 shader.minimumValue(),
577 shader.maximumValue() );
578 }
579 break;
580 }
581 Q_FALLTHROUGH();
584 {
585 // for all others we use itemised lists
586 QgsLegendColorList items;
587 settings.colorRampShader().legendSymbologyItems( items );
588 for ( const QPair< QString, QColor > &item : items )
589 {
590 nodes << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
591 }
592 break;
593 }
594 }
595 }
596
597 return nodes;
598}
599
600//
601// QgsDefaultPointCloudLayerLegend
602//
603
605 : mLayer( layer )
606{
608}
609
611{
612 QgsPointCloudRenderer *renderer = mLayer->renderer();
613 if ( !renderer )
614 return QList<QgsLayerTreeModelLegendNode *>();
615
616 return renderer->createLegendNodes( nodeLayer );
617}
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
virtual QStringList subProviders() const
Gets list of sub-providers within the layer's labeling.
virtual QgsPalLayerSettings settings(const QString &providerId=QString()) const =0
Gets associated label settings.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
Settings for a color ramp legend node.
bool useContinuousLegend() const
Returns true if a continuous gradient legend will be used.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads settings from an XML element.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const
Writes settings to an XML element.
A legend node which renders a color ramp.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Returns legend symbology items if provided by renderer.
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
Type colorRampType() const
Returns the color ramp type.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
QgsColorRamp * createColorRamp() const
Creates a gradient color ramp from shader settings.
@ Interpolated
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
Produces legend node with a marker symbol.
Default legend implementation for mesh layers.
QgsDefaultMeshLayerLegend(QgsMeshLayer *ml)
Creates an instance for the given mesh layer.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
Default legend implementation for point cloud layers.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
QgsDefaultPointCloudLayerLegend(QgsPointCloudLayer *layer)
Creates an instance for the given point cloud layer.
Default legend implementation for raster layers.
QgsDefaultRasterLayerLegend(QgsRasterLayer *rl)
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
Default legend implementation for vector layers.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes configuration to a DOM element, to be used later with readXml()
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads configuration from a DOM element previously written by writeXml()
QgsDefaultVectorLayerLegend(QgsVectorLayer *vl)
virtual QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
virtual QString legendClassificationAttribute() const
If supported by the renderer, return classification attribute for the use in legend.
Definition: qgsrenderer.h:392
QImage pathAsImage(const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking=false, double targetDpi=96, int frameNumber=-1, bool *isMissing=nullptr)
Returns the specified path rendered as an image.
Implementation of legend node interface for displaying arbitrary raster image.
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
virtual void setColumnBreak(bool breakBeforeNode)
Sets whether a forced column break should occur before the node.
virtual void setUserPatchSize(QSizeF size)
Sets the user (overridden) size for the legend node.
virtual void setEmbeddedInParent(bool embedded)
virtual void setUserLabel(const QString &userLabel)
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
void removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file.
QStringList customProperties() const
Returns list of keys stored in custom properties.
Represents a patch shape for use in map legends.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read settings from a DOM element.
void writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
static void setLegendNodeOrder(QgsLayerTreeLayer *nodeLayer, const QList< int > &order)
static void setLegendNodeColorRampSettings(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsColorRampLegendNodeSettings *settings)
Sets a custom legend color ramp settings for the legend node belonging to nodeLayer at the specified ...
static void setLegendNodeCustomSymbol(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsSymbol *symbol)
Sets a custom legend symbol for the legend node belonging to nodeLayer at the specified originalIndex...
static QString legendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex)
static QSizeF legendNodeSymbolSize(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the legend node symbol size for the legend node belonging to nodeLayer at the specified origi...
static bool hasLegendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex)
static bool legendNodeColumnBreak(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns whether a forced column break should occur before the node.
static bool hasLegendNodeOrder(QgsLayerTreeLayer *nodeLayer)
static void setLegendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QString &newLabel)
static void setLegendNodeSymbolSize(QgsLayerTreeLayer *nodeLayer, int originalIndex, QSizeF size)
Sets the legend symbol size for the legend node belonging to nodeLayer at the specified originalIndex...
static QgsColorRampLegendNodeSettings * legendNodeColorRampSettings(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the custom legend color ramp settings for the legend node belonging to nodeLayer at the speci...
static void setLegendNodeColumnBreak(QgsLayerTreeLayer *nodeLayer, int originalIndex, bool columnBreakBeforeNode)
Sets whether a forced column break should occur before the node.
static void setLegendNodePatchShape(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsLegendPatchShape &shape)
Sets the legend patch shape for the legend node belonging to nodeLayer at the specified originalIndex...
static QgsSymbol * legendNodeCustomSymbol(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the custom legend symbol for the legend node belonging to nodeLayer at the specified original...
static QList< int > legendNodeOrder(QgsLayerTreeLayer *nodeLayer)
static void applyLayerNodeProperties(QgsLayerTreeLayer *nodeLayer, QList< QgsLayerTreeModelLegendNode * > &nodes)
update according to layer node's custom properties (order of items, user labels for items)
static QgsLegendPatchShape legendNodePatchShape(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the legend patch shape for the legend node belonging to nodeLayer at the specified originalIn...
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads configuration from a DOM element previously written by writeXml()
static QgsMapLayerLegend * defaultMeshLegend(QgsMeshLayer *ml)
Create new legend implementation for mesh layer.
static QgsMapLayerLegend * defaultPointCloudLegend(QgsPointCloudLayer *layer)
Create new legend implementation for a point cloud layer.
QgsMapLayerLegend(QObject *parent=nullptr)
Constructor for QgsMapLayerLegend.
virtual QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Writes configuration to a DOM element, to be used later with readXml()
static QgsMapLayerLegend * defaultRasterLegend(QgsRasterLayer *rl)
Create new legend implementation for raster layer.
static QgsMapLayerLegend * defaultVectorLegend(QgsVectorLayer *vl)
Create new legend implementation for vector layer.
virtual QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer)=0
Returns list of legend nodes to be used for a particular layer tree layer node.
void itemsChanged()
Emitted when existing items/nodes got invalid and should be replaced by new ones.
QgsMapLayerLegend * legend() const
Can be nullptr.
QString legendPlaceholderImage() const
Returns path to the placeholder image or an empty string if a generated legend is shown.
Definition: qgsmaplayer.h:1537
void rendererChanged()
Signal emitted when renderer is changed.
void nameChanged()
Emitted when the name has been changed.
QString name() const
Returns name of the dataset group.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:100
QgsMeshRendererSettings rendererSettings() const
Returns renderer settings.
QgsMeshDatasetGroupMetadata datasetGroupMetadata(const QgsMeshDatasetIndex &index) const
Returns the dataset groups metadata.
Represents a mesh renderer settings for scalar datasets.
QgsColorRampShader colorRampShader() const
Returns color ramp shader function.
Represents all mesh renderer settings.
QgsMeshRendererScalarSettings scalarSettings(int groupIndex) const
Returns renderer settings.
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
int activeScalarDatasetGroup() const
Returns the active scalar dataset group.
Contains settings for how a map layer will be labeled.
Represents a map layer supporting display of point clouds.
QgsPointCloudRenderer * renderer()
Returns the 2D renderer for the point cloud.
Abstract base class for 2d point cloud renderers.
virtual QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer)
Creates a set of legend nodes representing the renderer.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:477
virtual bool supportsLegendGraphic() const
Returns whether the provider supplies a legend graphic.
Represents a raster layer.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
virtual QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer)
Creates a set of legend nodes representing the renderer.
double maximumValue() const
Returns the minimum value for the raster shader.
double minimumValue() const
Returns the maximum value for the raster shader.
Implementation of legend node interface for displaying raster legend entries.
The class is used as a container of context for various read/write operations on other objects.
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
A child rule for QgsRuleBasedLabeling.
const QgsRuleBasedLabeling::Rule * findRuleByKey(const QString &key) const
Try to find a rule given its unique key.
QString description() const
A human readable description for this rule.
Rule based labeling for a vector layer.
QgsRuleBasedLabeling::Rule * rootRule()
This class is a composition of two QSettings instances:
Definition: qgssettings.h:63
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Implementation of legend node interface for displaying arbitrary label with icon.
static QString encodeSize(QSizeF size)
Encodes a QSizeF to a string.
static QgsSymbol * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static QSizeF decodeSize(const QString &string)
Decodes a QSizeF from a string.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:93
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
Produces legend node for a labeling text symbol.
Represents a vector layer which manages a vector based data sets.
bool labelsEnabled() const
Returns whether the layer contains labels which are enabled and should be drawn.
QgsVectorLayerFeatureCounter * countSymbolFeatures(bool storeSymbolFids=false)
Count features for symbols.
bool diagramsEnabled() const
Returns whether the layer contains diagrams which are enabled and should be drawn.
const QgsAbstractVectorLayerLabeling * labeling() const
Access to const labeling configuration.
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
const QgsDiagramRenderer * diagramRenderer() const
Implementation of legend node interface for displaying WMS legend entries.
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
#define str(x)
Definition: qgis.cpp:37
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QList< QPair< QString, QColor > > QgsLegendColorList