QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 "moc_qgsmaplayerlegend.cpp"
18#include "qgsiconutils.h"
19#include "qgsimagecache.h"
20#include "qgssettings.h"
21#include "qgslayertree.h"
23#include "qgsmeshlayer.h"
24#include "qgspluginlayer.h"
25#include "qgsrasterlayer.h"
26#include "qgsrenderer.h"
27#include "qgsvectorlayer.h"
28#include "qgspointcloudlayer.h"
29#include "qgsdiagramrenderer.h"
30#include "qgssymbollayerutils.h"
32#include "qgsrasterrenderer.h"
36
38 : QObject( parent )
39{
40}
41
42void QgsMapLayerLegend::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
43{
44 Q_UNUSED( elem )
45 Q_UNUSED( context )
46}
47
48QDomElement QgsMapLayerLegend::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
49{
50 Q_UNUSED( doc )
51 Q_UNUSED( context )
52 return QDomElement();
53}
54
59
64
69
74
75// -------------------------------------------------------------------------
76
77
78void QgsMapLayerLegendUtils::setLegendNodeOrder( QgsLayerTreeLayer *nodeLayer, const QList<int> &order )
79{
80 QStringList orderStr;
81 const auto constOrder = order;
82 for ( const int id : constOrder )
83 orderStr << QString::number( id );
84 const QString str = orderStr.isEmpty() ? QStringLiteral( "empty" ) : orderStr.join( QLatin1Char( ',' ) );
85
86 nodeLayer->setCustomProperty( QStringLiteral( "legend/node-order" ), str );
87}
88
89static int _originalLegendNodeCount( QgsLayerTreeLayer *nodeLayer )
90{
91 // this is not particularly efficient way of finding out number of legend nodes
92 const QList<QgsLayerTreeModelLegendNode *> lst = nodeLayer->layer()->legend()->createLayerTreeModelLegendNodes( nodeLayer );
93 const int numNodes = lst.count();
94 qDeleteAll( lst );
95 return numNodes;
96}
97
98static QList<int> _makeNodeOrder( QgsLayerTreeLayer *nodeLayer )
99{
100 if ( !nodeLayer->layer() || !nodeLayer->layer()->legend() )
101 {
102 QgsDebugError( QStringLiteral( "Legend node order manipulation is invalid without existing legend" ) );
103 return QList<int>();
104 }
105
106 const int numNodes = _originalLegendNodeCount( nodeLayer );
107
108 QList<int> order;
109 order.reserve( numNodes );
110 for ( int i = 0; i < numNodes; ++i )
111 order << i;
112 return order;
113}
114
116{
117 const QString orderStr = nodeLayer->customProperty( QStringLiteral( "legend/node-order" ) ).toString();
118
119 if ( orderStr.isEmpty() )
120 return _makeNodeOrder( nodeLayer );
121
122 if ( orderStr == QLatin1String( "empty" ) )
123 return QList<int>();
124
125 const int numNodes = _originalLegendNodeCount( nodeLayer );
126
127 QList<int> lst;
128 const auto constSplit = orderStr.split( ',' );
129 for ( const QString &item : constSplit )
130 {
131 bool ok;
132 const int id = item.toInt( &ok );
133 if ( !ok || id < 0 || id >= numNodes )
134 return _makeNodeOrder( nodeLayer );
135
136 lst << id;
137 }
138
139 return lst;
140}
141
143{
144 return nodeLayer->customProperties().contains( QStringLiteral( "legend/node-order" ) );
145}
146
147void QgsMapLayerLegendUtils::setLegendNodeUserLabel( QgsLayerTreeLayer *nodeLayer, int originalIndex, const QString &newLabel )
148{
149 nodeLayer->setCustomProperty( "legend/label-" + QString::number( originalIndex ), newLabel );
150}
151
153{
154 return nodeLayer->customProperty( "legend/label-" + QString::number( originalIndex ) ).toString();
155}
156
158{
159 return nodeLayer->customProperties().contains( "legend/label-" + QString::number( originalIndex ) );
160}
161
163{
164 QDomDocument patchDoc;
165 QDomElement patchElem = patchDoc.createElement( QStringLiteral( "patch" ) );
166 shape.writeXml( patchElem, patchDoc, QgsReadWriteContext() );
167 patchDoc.appendChild( patchElem );
168 nodeLayer->setCustomProperty( "legend/patch-shape-" + QString::number( originalIndex ), patchDoc.toString() );
169}
170
172{
173 const QString patchDef = nodeLayer->customProperty( "legend/patch-shape-" + QString::number( originalIndex ) ).toString();
174 if ( patchDef.isEmpty() )
175 return QgsLegendPatchShape();
176
177 QDomDocument doc( QStringLiteral( "patch" ) );
178 doc.setContent( patchDef );
180 shape.readXml( doc.documentElement(), QgsReadWriteContext() );
181 return shape;
182}
183
184void QgsMapLayerLegendUtils::setLegendNodeSymbolSize( QgsLayerTreeLayer *nodeLayer, int originalIndex, QSizeF size )
185{
186 if ( size.isValid() )
187 nodeLayer->setCustomProperty( "legend/symbol-size-" + QString::number( originalIndex ), QgsSymbolLayerUtils::encodeSize( size ) );
188 else
189 nodeLayer->removeCustomProperty( "legend/symbol-size-" + QString::number( originalIndex ) );
190}
191
193{
194 const QString size = nodeLayer->customProperty( "legend/symbol-size-" + QString::number( originalIndex ) ).toString();
195 if ( size.isEmpty() )
196 return QSizeF();
197 else
198 return QgsSymbolLayerUtils::decodeSize( size );
199}
200
201void QgsMapLayerLegendUtils::setLegendNodeCustomSymbol( QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsSymbol *symbol )
202{
203 if ( symbol )
204 {
205 QDomDocument doc;
206 QgsReadWriteContext rwContext;
207 rwContext.setPathResolver( QgsProject::instance()->pathResolver() ); // skip-keyword-check
208 const QDomElement elem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "custom symbol" ), symbol, doc, rwContext );
209 doc.appendChild( elem );
210 nodeLayer->setCustomProperty( "legend/custom-symbol-" + QString::number( originalIndex ), doc.toString() );
211 }
212 else
213 nodeLayer->removeCustomProperty( "legend/custom-symbol-" + QString::number( originalIndex ) );
214}
215
217{
218 const QString symbolDef = nodeLayer->customProperty( "legend/custom-symbol-" + QString::number( originalIndex ) ).toString();
219 if ( symbolDef.isEmpty() )
220 return nullptr;
221
222 QDomDocument doc;
223 doc.setContent( symbolDef );
224 const QDomElement elem = doc.documentElement();
225
226 QgsReadWriteContext rwContext;
227 rwContext.setPathResolver( QgsProject::instance()->pathResolver() ); // skip-keyword-check
228
229 return QgsSymbolLayerUtils::loadSymbol( elem, rwContext );
230}
231
233{
234 if ( settings )
235 {
236 QDomDocument doc;
237 QgsReadWriteContext rwContext;
238 rwContext.setPathResolver( QgsProject::instance()->pathResolver() ); // skip-keyword-check
239 QDomElement elem = doc.createElement( QStringLiteral( "rampSettings" ) );
240 settings->writeXml( doc, elem, rwContext );
241 doc.appendChild( elem );
242 nodeLayer->setCustomProperty( "legend/custom-ramp-settings-" + QString::number( originalIndex ), doc.toString() );
243 }
244 else
245 nodeLayer->removeCustomProperty( "legend/custom-ramp-settings-" + QString::number( originalIndex ) );
246}
247
249{
250 const QString settingsDef = nodeLayer->customProperty( "legend/custom-ramp-settings-" + QString::number( originalIndex ) ).toString();
251 if ( settingsDef.isEmpty() )
252 return nullptr;
253
254 QDomDocument doc;
255 doc.setContent( settingsDef );
256 const QDomElement elem = doc.documentElement();
257
258 QgsReadWriteContext rwContext;
259 rwContext.setPathResolver( QgsProject::instance()->pathResolver() ); // skip-keyword-check
260
262 settings.readXml( elem, rwContext );
263 return new QgsColorRampLegendNodeSettings( settings );
264}
265
266void QgsMapLayerLegendUtils::setLegendNodeColumnBreak( QgsLayerTreeLayer *nodeLayer, int originalIndex, bool columnBreakBeforeNode )
267{
268 if ( columnBreakBeforeNode )
269 nodeLayer->setCustomProperty( "legend/column-break-" + QString::number( originalIndex ), QStringLiteral( "1" ) );
270 else
271 nodeLayer->removeCustomProperty( "legend/column-break-" + QString::number( originalIndex ) );
272}
273
275{
276 return nodeLayer->customProperty( "legend/column-break-" + QString::number( originalIndex ) ).toInt();
277}
278
279void QgsMapLayerLegendUtils::applyLayerNodeProperties( QgsLayerTreeLayer *nodeLayer, QList<QgsLayerTreeModelLegendNode *> &nodes )
280{
281 // handle user labels
282 int i = 0;
283 const auto constNodes = nodes;
284 for ( QgsLayerTreeModelLegendNode *legendNode : constNodes )
285 {
286 const QString userLabel = QgsMapLayerLegendUtils::legendNodeUserLabel( nodeLayer, i );
287 if ( !userLabel.isNull() )
288 legendNode->setUserLabel( userLabel );
289
290 if ( QgsSymbolLegendNode *symbolNode = dynamic_cast< QgsSymbolLegendNode * >( legendNode ) )
291 {
293 symbolNode->setPatchShape( shape );
294
295 symbolNode->setCustomSymbol( QgsMapLayerLegendUtils::legendNodeCustomSymbol( nodeLayer, i ) );
296 }
297 else if ( QgsColorRampLegendNode *colorRampNode = dynamic_cast< QgsColorRampLegendNode * >( legendNode ) )
298 {
299 const std::unique_ptr< QgsColorRampLegendNodeSettings > settings( QgsMapLayerLegendUtils::legendNodeColorRampSettings( nodeLayer, i ) );
300 if ( settings )
301 {
302 colorRampNode->setSettings( *settings );
303 }
304 }
305
306 const QSizeF userSize = QgsMapLayerLegendUtils::legendNodeSymbolSize( nodeLayer, i );
307 if ( userSize.isValid() )
308 {
309 legendNode->setUserPatchSize( userSize );
310 }
311
312 if ( legendNodeColumnBreak( nodeLayer, i ) )
313 legendNode->setColumnBreak( true );
314
315 i++;
316 }
317
318 // handle user order of nodes
320 {
321 const QList<int> order = QgsMapLayerLegendUtils::legendNodeOrder( nodeLayer );
322
323 QList<QgsLayerTreeModelLegendNode *> newOrder;
324 QSet<int> usedIndices;
325 const auto constOrder = order;
326 for ( const int idx : constOrder )
327 {
328 if ( usedIndices.contains( idx ) )
329 {
330 QgsDebugError( QStringLiteral( "invalid node order. ignoring." ) );
331 return;
332 }
333
334 newOrder << nodes[idx];
335 usedIndices << idx;
336 }
337
338 // delete unused nodes
339 for ( int i = 0; i < nodes.count(); ++i )
340 {
341 if ( !usedIndices.contains( i ) )
342 delete nodes[i];
343 }
344
345 nodes = newOrder;
346 }
347
348}
349
350// -------------------------------------------------------------------------
351
352
359
361{
362 QList<QgsLayerTreeModelLegendNode *> nodes;
363 if ( !mLayer )
364 return nodes;
365
366 const QString placeholderImage = mLayer->legendPlaceholderImage();
367 if ( !placeholderImage.isEmpty() )
368 {
369 bool fitsInCache;
370 const QImage img = QgsApplication::imageCache()->pathAsImage( placeholderImage, QSize(), false, 1.0, fitsInCache );
371 nodes << new QgsImageLegendNode( nodeLayer, img );
372 return nodes;
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 QList<QgsLayerTreeModelLegendNode *> rendererNodes = r->createLegendNodes( nodeLayer );
389 for ( QgsLayerTreeModelLegendNode *node : rendererNodes )
390 {
391 if ( QgsSymbolLegendNode *legendNode = qobject_cast< QgsSymbolLegendNode *>( node ) )
392 {
393 const QString ruleKey = legendNode->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString();
394 if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( ruleKey ) )
395 {
396 legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( ruleKey ) );
397 legendNode->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
398 }
399 }
400 nodes << node;
401 }
402
403 if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() && qobject_cast< QgsSymbolLegendNode * >( nodes[0] ) )
404 nodes[0]->setEmbeddedInParent( true );
405
406 if ( mLayer->diagramsEnabled() )
407 {
408 const auto constLegendItems = mLayer->diagramRenderer()->legendItems( nodeLayer );
409 for ( QgsLayerTreeModelLegendNode *i : constLegendItems )
410 {
411 nodes.append( i );
412 }
413 }
414
415 if ( mLayer->labelsEnabled() && mShowLabelLegend )
416 {
417 const QgsAbstractVectorLayerLabeling *labeling = mLayer->labeling();
418 if ( labeling )
419 {
420 const QStringList pList = labeling->subProviders();
421 for ( int i = 0; i < pList.size(); ++i )
422 {
423 const QgsPalLayerSettings s = labeling->settings( pList.at( i ) );
424 QString description;
425 const QgsRuleBasedLabeling *ruleBasedLabeling = dynamic_cast<const QgsRuleBasedLabeling *>( labeling );
426 if ( ruleBasedLabeling && ruleBasedLabeling->rootRule() )
427 {
428 const QgsRuleBasedLabeling::Rule *rule = ruleBasedLabeling->rootRule()->findRuleByKey( pList.at( i ) );
429 if ( rule )
430 {
431 description = rule->description();
432 }
433 }
434 QgsVectorLabelLegendNode *node = new QgsVectorLabelLegendNode( nodeLayer, s );
435 node->setUserLabel( description );
436 nodes.append( node );
437 }
438 }
439 }
440
441 return nodes;
442}
443
444void QgsDefaultVectorLayerLegend::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
445{
446 mTextOnSymbolEnabled = false;
447 mTextOnSymbolTextFormat = QgsTextFormat();
448 mTextOnSymbolContent.clear();
449
450 mShowLabelLegend = elem.attribute( QStringLiteral( "showLabelLegend" ), QStringLiteral( "0" ) ).compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0;
451
452 const QDomElement tosElem = elem.firstChildElement( QStringLiteral( "text-on-symbol" ) );
453 if ( !tosElem.isNull() )
454 {
455 mTextOnSymbolEnabled = true;
456 const QDomElement tosFormatElem = tosElem.firstChildElement( QStringLiteral( "text-style" ) );
457 mTextOnSymbolTextFormat.readXml( tosFormatElem, context );
458 const QDomElement tosContentElem = tosElem.firstChildElement( QStringLiteral( "content" ) );
459 QDomElement tosContentItemElem = tosContentElem.firstChildElement( QStringLiteral( "item" ) );
460 while ( !tosContentItemElem.isNull() )
461 {
462 mTextOnSymbolContent.insert( tosContentItemElem.attribute( QStringLiteral( "key" ) ), tosContentItemElem.attribute( QStringLiteral( "value" ) ) );
463 tosContentItemElem = tosContentItemElem.nextSiblingElement( QStringLiteral( "item" ) );
464 }
465 }
466}
467
468QDomElement QgsDefaultVectorLayerLegend::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
469{
470 QDomElement elem = doc.createElement( QStringLiteral( "legend" ) );
471 elem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "default-vector" ) );
472 elem.setAttribute( QStringLiteral( "showLabelLegend" ), mShowLabelLegend );
473
474 if ( mTextOnSymbolEnabled )
475 {
476 QDomElement tosElem = doc.createElement( QStringLiteral( "text-on-symbol" ) );
477 const QDomElement tosFormatElem = mTextOnSymbolTextFormat.writeXml( doc, context );
478 tosElem.appendChild( tosFormatElem );
479 QDomElement tosContentElem = doc.createElement( QStringLiteral( "content" ) );
480 for ( auto it = mTextOnSymbolContent.constBegin(); it != mTextOnSymbolContent.constEnd(); ++it )
481 {
482 QDomElement tosContentItemElem = doc.createElement( QStringLiteral( "item" ) );
483 tosContentItemElem.setAttribute( QStringLiteral( "key" ), it.key() );
484 tosContentItemElem.setAttribute( QStringLiteral( "value" ), it.value() );
485 tosContentElem.appendChild( tosContentItemElem );
486 }
487 tosElem.appendChild( tosContentElem );
488 elem.appendChild( tosElem );
489 }
490
491 return elem;
492}
493
494
495// -------------------------------------------------------------------------
496
497
503
505{
506 QList<QgsLayerTreeModelLegendNode *> nodes;
507
508 // temporary solution for WMS. Ideally should be done with a delegate.
509 if ( mLayer->dataProvider() && mLayer->dataProvider()->supportsLegendGraphic() )
510 {
511 nodes << new QgsWmsLegendNode( nodeLayer );
512 }
513
514 const QString placeholderImage = mLayer->legendPlaceholderImage();
515 if ( !placeholderImage.isEmpty() )
516 {
517 bool fitsInCache;
518 const QImage img = QgsApplication::imageCache()->pathAsImage( placeholderImage, QSize(), false, 1.0, fitsInCache );
519 nodes << new QgsImageLegendNode( nodeLayer, img );
520 }
521 else if ( mLayer->renderer() )
522 nodes.append( mLayer->renderer()->createLegendNodes( nodeLayer ) );
523 return nodes;
524}
525
526// -------------------------------------------------------------------------
527
533
535{
536 QList<QgsLayerTreeModelLegendNode *> nodes;
537
538 const QgsMeshRendererSettings rendererSettings = mLayer->rendererSettings();
539
540 const int indexScalar = rendererSettings.activeScalarDatasetGroup();
541 const int indexVector = rendererSettings.activeVectorDatasetGroup();
542
543 QString name;
544 if ( indexScalar > -1 && indexVector > -1 && indexScalar != indexVector )
545 name = QString( "%1 / %2" ).arg( mLayer->datasetGroupMetadata( indexScalar ).name(), mLayer->datasetGroupMetadata( indexVector ).name() );
546 else if ( indexScalar > -1 )
547 name = mLayer->datasetGroupMetadata( indexScalar ).name();
548 else if ( indexVector > -1 )
549 name = mLayer->datasetGroupMetadata( indexVector ).name();
550 else
551 {
552 // neither contours nor vectors get rendered - no legend needed
553 return nodes;
554 }
555
556 if ( indexScalar > -1 )
557 {
558 const QString scalarNameKey = QStringLiteral( "scalarName" );
559 nodes << new QgsSimpleLegendNode( nodeLayer, mLayer->datasetGroupMetadata( indexScalar ).name(),
560 QIcon(), nullptr, scalarNameKey );
561 const QgsMeshRendererScalarSettings settings = rendererSettings.scalarSettings( indexScalar );
562 const QgsColorRampShader shader = settings.colorRampShader();
563 switch ( shader.colorRampType() )
564 {
566 if ( !shader.legendSettings() || shader.legendSettings()->useContinuousLegend() )
567 {
568 // for interpolated shaders we use a ramp legend node
569 if ( !shader.colorRampItemList().isEmpty() )
570 {
571 nodes << new QgsColorRampLegendNode( nodeLayer, shader.createColorRamp(),
573 shader.minimumValue(),
574 shader.maximumValue(),
575 nullptr,
576 QStringLiteral( "scalarLegend" ),
577 scalarNameKey );
578 }
579 break;
580 }
581 [[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, nullptr, false,
591 QStringLiteral( "scalarLegend" ) + QUuid::createUuid().toString(),
592 scalarNameKey
593 );
594 }
595 break;
596 }
597 }
598 }
599
600 if ( indexVector > -1 )
601 {
602 const QgsMeshRendererVectorSettings settings = rendererSettings.vectorSettings( indexVector );
603 const QString vectorNameKey = QStringLiteral( "vectorName" );
604 switch ( settings.coloringMethod() )
605 {
607 {
608 const QColor arrowColor = settings.color();
609 const QIcon vectorIcon = QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/meshvectors.svg" ), arrowColor, arrowColor );
610 nodes << new QgsSimpleLegendNode( nodeLayer, mLayer->datasetGroupMetadata( indexVector ).name(),
611 vectorIcon, nullptr, vectorNameKey );
612 break;
613 }
615 {
616 const QIcon vectorIcon = QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/meshvectors.svg" ) );
617 nodes << new QgsSimpleLegendNode( nodeLayer, mLayer->datasetGroupMetadata( indexVector ).name(),
618 vectorIcon, nullptr, vectorNameKey );
619 const QgsColorRampShader shader = settings.colorRampShader();
620 switch ( shader.colorRampType() )
621 {
623 if ( !shader.legendSettings() || shader.legendSettings()->useContinuousLegend() )
624 {
625 // for interpolated shaders we use a ramp legend node
626 if ( !shader.colorRampItemList().isEmpty() )
627 {
628 nodes << new QgsColorRampLegendNode( nodeLayer, shader.createColorRamp(),
630 shader.minimumValue(),
631 shader.maximumValue(),
632 nullptr,
633 QStringLiteral( "vectorLegend" ),
634 vectorNameKey );
635 }
636 break;
637 }
638 [[fallthrough]];
641 {
642 // for all others we use itemised lists
643 QgsLegendColorList items;
644 settings.colorRampShader().legendSymbologyItems( items );
645 for ( const QPair< QString, QColor > &item : items )
646 {
647 nodes << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first, nullptr, false,
648 QStringLiteral( "vectorLegend" ) + QUuid::createUuid().toString(),
649 vectorNameKey
650 );
651 }
652 break;
653 }
654 break;
655 }
656 }
657 }
658 }
659
660 return nodes;
661}
662
663//
664// QgsDefaultPointCloudLayerLegend
665//
666
672
674{
675 QgsPointCloudRenderer *renderer = mLayer->renderer();
676 if ( !renderer )
677 return QList<QgsLayerTreeModelLegendNode *>();
678
679 return renderer->createLegendNodes( nodeLayer );
680}
@ 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 QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
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.
Abstract base class for all 2D vector feature renderers.
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.
@ ColorRamp
Render with a color ramp.
@ SingleColor
Render with a single color.
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.
QgsMeshRendererVectorSettings vectorSettings(int groupIndex) const
Returns renderer settings.
Represents a renderer settings for vector datasets.
QColor color() const
Returns color used for drawing arrows.
QgsColorRampShader colorRampShader() const
Sets the color ramp shader used to render vector datasets.
QgsInterpolatedLineColor::ColoringMethod coloringMethod() const
Returns the coloring method used to render vector datasets.
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:231
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:39
#define QgsDebugError(str)
Definition qgslogger.h:38
QList< QPair< QString, QColor > > QgsLegendColorList