QGIS API Documentation 3.37.0-Master (684a802617f)
Loading...
Searching...
No Matches
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
58
63
68
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 QgsDebugError( 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 QgsDebugError( 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
358
360{
361 QList<QgsLayerTreeModelLegendNode *> nodes;
362 if ( !mLayer )
363 return nodes;
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 QgsFeatureRenderer *r = mLayer->renderer();
375 if ( !r )
376 return nodes;
377
378 if ( nodeLayer->customProperty( QStringLiteral( "showFeatureCount" ), 0 ).toBool() )
379 mLayer->countSymbolFeatures();
380
381 const QgsSettings settings;
382 if ( settings.value( QStringLiteral( "qgis/showLegendClassifiers" ), false ).toBool() && !r->legendClassificationAttribute().isEmpty() )
383 {
384 nodes.append( new QgsSimpleLegendNode( nodeLayer, r->legendClassificationAttribute() ) );
385 }
386
387 const QList<QgsLayerTreeModelLegendNode *> rendererNodes = r->createLegendNodes( nodeLayer );
388 for ( QgsLayerTreeModelLegendNode *node : rendererNodes )
389 {
390 if ( QgsSymbolLegendNode *legendNode = qobject_cast< QgsSymbolLegendNode *>( node ) )
391 {
392 const QString ruleKey = legendNode->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString();
393 if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( ruleKey ) )
394 {
395 legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( ruleKey ) );
396 legendNode->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
397 }
398 }
399 nodes << node;
400 }
401
402 if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() && qobject_cast< QgsSymbolLegendNode * >( nodes[0] ) )
403 nodes[0]->setEmbeddedInParent( true );
404
405 if ( mLayer->diagramsEnabled() )
406 {
407 const auto constLegendItems = mLayer->diagramRenderer()->legendItems( nodeLayer );
408 for ( QgsLayerTreeModelLegendNode *i : constLegendItems )
409 {
410 nodes.append( i );
411 }
412 }
413
414 if ( mLayer->labelsEnabled() && mShowLabelLegend )
415 {
416 const QgsAbstractVectorLayerLabeling *labeling = mLayer->labeling();
417 if ( labeling )
418 {
419 const QStringList pList = labeling->subProviders();
420 for ( int i = 0; i < pList.size(); ++i )
421 {
422 const QgsPalLayerSettings s = labeling->settings( pList.at( i ) );
423 QString description;
424 const QgsRuleBasedLabeling *ruleBasedLabeling = dynamic_cast<const QgsRuleBasedLabeling *>( labeling );
425 if ( ruleBasedLabeling && ruleBasedLabeling->rootRule() )
426 {
427 const QgsRuleBasedLabeling::Rule *rule = ruleBasedLabeling->rootRule()->findRuleByKey( pList.at( i ) );
428 if ( rule )
429 {
430 description = rule->description();
431 }
432 }
433 QgsVectorLabelLegendNode *node = new QgsVectorLabelLegendNode( nodeLayer, s );
434 node->setUserLabel( description );
435 nodes.append( node );
436 }
437 }
438 }
439
440 return nodes;
441}
442
443void QgsDefaultVectorLayerLegend::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
444{
445 mTextOnSymbolEnabled = false;
446 mTextOnSymbolTextFormat = QgsTextFormat();
447 mTextOnSymbolContent.clear();
448
449 mShowLabelLegend = elem.attribute( QStringLiteral( "showLabelLegend" ), QStringLiteral( "0" ) ).compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0;
450
451 const QDomElement tosElem = elem.firstChildElement( QStringLiteral( "text-on-symbol" ) );
452 if ( !tosElem.isNull() )
453 {
454 mTextOnSymbolEnabled = true;
455 const QDomElement tosFormatElem = tosElem.firstChildElement( QStringLiteral( "text-style" ) );
456 mTextOnSymbolTextFormat.readXml( tosFormatElem, context );
457 const QDomElement tosContentElem = tosElem.firstChildElement( QStringLiteral( "content" ) );
458 QDomElement tosContentItemElem = tosContentElem.firstChildElement( QStringLiteral( "item" ) );
459 while ( !tosContentItemElem.isNull() )
460 {
461 mTextOnSymbolContent.insert( tosContentItemElem.attribute( QStringLiteral( "key" ) ), tosContentItemElem.attribute( QStringLiteral( "value" ) ) );
462 tosContentItemElem = tosContentItemElem.nextSiblingElement( QStringLiteral( "item" ) );
463 }
464 }
465}
466
467QDomElement QgsDefaultVectorLayerLegend::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
468{
469 QDomElement elem = doc.createElement( QStringLiteral( "legend" ) );
470 elem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "default-vector" ) );
471 elem.setAttribute( QStringLiteral( "showLabelLegend" ), mShowLabelLegend );
472
473 if ( mTextOnSymbolEnabled )
474 {
475 QDomElement tosElem = doc.createElement( QStringLiteral( "text-on-symbol" ) );
476 const QDomElement tosFormatElem = mTextOnSymbolTextFormat.writeXml( doc, context );
477 tosElem.appendChild( tosFormatElem );
478 QDomElement tosContentElem = doc.createElement( QStringLiteral( "content" ) );
479 for ( auto it = mTextOnSymbolContent.constBegin(); it != mTextOnSymbolContent.constEnd(); ++it )
480 {
481 QDomElement tosContentItemElem = doc.createElement( QStringLiteral( "item" ) );
482 tosContentItemElem.setAttribute( QStringLiteral( "key" ), it.key() );
483 tosContentItemElem.setAttribute( QStringLiteral( "value" ), it.value() );
484 tosContentElem.appendChild( tosContentItemElem );
485 }
486 tosElem.appendChild( tosContentElem );
487 elem.appendChild( tosElem );
488 }
489
490 return elem;
491}
492
493
494// -------------------------------------------------------------------------
495
496
502
504{
505 QList<QgsLayerTreeModelLegendNode *> nodes;
506
507 // temporary solution for WMS. Ideally should be done with a delegate.
508 if ( mLayer->dataProvider() && mLayer->dataProvider()->supportsLegendGraphic() )
509 {
510 nodes << new QgsWmsLegendNode( nodeLayer );
511 }
512
513 const QString placeholderImage = mLayer->legendPlaceholderImage();
514 if ( !placeholderImage.isEmpty() )
515 {
516 bool fitsInCache;
517 const QImage img = QgsApplication::imageCache()->pathAsImage( placeholderImage, QSize(), false, 1.0, fitsInCache );
518 nodes << new QgsImageLegendNode( nodeLayer, img );
519 }
520 else if ( mLayer->renderer() )
521 nodes.append( mLayer->renderer()->createLegendNodes( nodeLayer ) );
522 return nodes;
523}
524
525// -------------------------------------------------------------------------
526
532
534{
535 QList<QgsLayerTreeModelLegendNode *> nodes;
536
537 const QgsMeshRendererSettings rendererSettings = mLayer->rendererSettings();
538
539 const int indexScalar = rendererSettings.activeScalarDatasetGroup();
540 const int indexVector = rendererSettings.activeVectorDatasetGroup();
541
542 QString name;
543 if ( indexScalar > -1 && indexVector > -1 && indexScalar != indexVector )
544 name = QString( "%1 / %2" ).arg( mLayer->datasetGroupMetadata( indexScalar ).name(), mLayer->datasetGroupMetadata( indexVector ).name() );
545 else if ( indexScalar > -1 )
546 name = mLayer->datasetGroupMetadata( indexScalar ).name();
547 else if ( indexVector > -1 )
548 name = mLayer->datasetGroupMetadata( indexVector ).name();
549 else
550 {
551 // neither contours nor vectors get rendered - no legend needed
552 return nodes;
553 }
554
555 nodes << new QgsSimpleLegendNode( nodeLayer, name );
556
557 if ( indexScalar > -1 )
558 {
559 const QgsMeshRendererScalarSettings settings = rendererSettings.scalarSettings( indexScalar );
560 const QgsColorRampShader shader = settings.colorRampShader();
561 switch ( shader.colorRampType() )
562 {
564 if ( ! shader.legendSettings() || shader.legendSettings()->useContinuousLegend() )
565 {
566 // for interpolated shaders we use a ramp legend node
567 if ( !shader.colorRampItemList().isEmpty() )
568 {
569 nodes << new QgsColorRampLegendNode( nodeLayer, shader.createColorRamp(),
571 shader.minimumValue(),
572 shader.maximumValue() );
573 }
574 break;
575 }
576 [[fallthrough]];
579 {
580 // for all others we use itemised lists
581 QgsLegendColorList items;
582 settings.colorRampShader().legendSymbologyItems( items );
583 for ( const QPair< QString, QColor > &item : items )
584 {
585 nodes << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
586 }
587 break;
588 }
589 }
590 }
591
592 return nodes;
593}
594
595//
596// QgsDefaultPointCloudLayerLegend
597//
598
604
606{
607 QgsPointCloudRenderer *renderer = mLayer->renderer();
608 if ( !renderer )
609 return QList<QgsLayerTreeModelLegendNode *>();
610
611 return renderer->createLegendNodes( nodeLayer );
612}
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
@ Linear
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
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.
Qgis::ShaderInterpolationMethod colorRampType() const
Returns the color ramp interpolation method.
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom color map.
QgsColorRamp * createColorRamp() const
Creates a gradient color ramp from shader settings.
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 QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer) const
Returns a list of legend nodes to be used for the legend for the renderer.
virtual QString legendClassificationAttribute() const
If supported by the renderer, return classification attribute for the use in legend.
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 QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
@ RuleKey
Rule key of the node (QString)
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 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.
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.
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.
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.
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:64
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:94
Container for all settings relating to text rendering.
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.
#define str(x)
Definition qgis.cpp:38
#define QgsDebugError(str)
Definition qgslogger.h:38
QList< QPair< QString, QColor > > QgsLegendColorList