QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
18 #include "qgssettings.h"
19 #include "qgslayertree.h"
21 #include "qgsmeshlayer.h"
22 #include "qgspluginlayer.h"
23 #include "qgsrasterlayer.h"
24 #include "qgsrenderer.h"
25 #include "qgsvectorlayer.h"
26 #include "qgspointcloudlayer.h"
27 #include "qgsdiagramrenderer.h"
28 #include "qgssymbollayerutils.h"
29 #include "qgspointcloudrenderer.h"
30 #include "qgsrasterrenderer.h"
31 #include "qgscolorramplegendnode.h"
32 #include "qgsvectorlayerlabeling.h"
33 #include "qgsrulebasedlabeling.h"
34 
36  : QObject( parent )
37 {
38 }
39 
40 void QgsMapLayerLegend::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
41 {
42  Q_UNUSED( elem )
43  Q_UNUSED( context )
44 }
45 
46 QDomElement QgsMapLayerLegend::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
47 {
48  Q_UNUSED( doc )
49  Q_UNUSED( context )
50  return QDomElement();
51 }
52 
54 {
55  return new QgsDefaultVectorLayerLegend( vl );
56 }
57 
59 {
60  return new QgsDefaultRasterLayerLegend( rl );
61 }
62 
64 {
65  return new QgsDefaultMeshLayerLegend( ml );
66 }
67 
69 {
70  return new QgsDefaultPointCloudLayerLegend( layer );
71 }
72 
73 // -------------------------------------------------------------------------
74 
75 
76 void QgsMapLayerLegendUtils::setLegendNodeOrder( QgsLayerTreeLayer *nodeLayer, const QList<int> &order )
77 {
78  QStringList orderStr;
79  const auto constOrder = order;
80  for ( int id : constOrder )
81  orderStr << QString::number( id );
82  QString str = orderStr.isEmpty() ? QStringLiteral( "empty" ) : orderStr.join( QLatin1Char( ',' ) );
83 
84  nodeLayer->setCustomProperty( QStringLiteral( "legend/node-order" ), str );
85 }
86 
87 static int _originalLegendNodeCount( QgsLayerTreeLayer *nodeLayer )
88 {
89  // this is not particularly efficient way of finding out number of legend nodes
90  QList<QgsLayerTreeModelLegendNode *> lst = nodeLayer->layer()->legend()->createLayerTreeModelLegendNodes( nodeLayer );
91  int numNodes = lst.count();
92  qDeleteAll( lst );
93  return numNodes;
94 }
95 
96 static QList<int> _makeNodeOrder( QgsLayerTreeLayer *nodeLayer )
97 {
98  if ( !nodeLayer->layer() || !nodeLayer->layer()->legend() )
99  {
100  QgsDebugMsg( QStringLiteral( "Legend node order manipulation is invalid without existing legend" ) );
101  return QList<int>();
102  }
103 
104  int numNodes = _originalLegendNodeCount( nodeLayer );
105 
106  QList<int> order;
107  order.reserve( numNodes );
108  for ( int i = 0; i < numNodes; ++i )
109  order << i;
110  return order;
111 }
112 
114 {
115  QString orderStr = nodeLayer->customProperty( QStringLiteral( "legend/node-order" ) ).toString();
116 
117  if ( orderStr.isEmpty() )
118  return _makeNodeOrder( nodeLayer );
119 
120  if ( orderStr == QLatin1String( "empty" ) )
121  return QList<int>();
122 
123  int numNodes = _originalLegendNodeCount( nodeLayer );
124 
125  QList<int> lst;
126  const auto constSplit = orderStr.split( ',' );
127  for ( const QString &item : constSplit )
128  {
129  bool ok;
130  int id = item.toInt( &ok );
131  if ( !ok || id < 0 || id >= numNodes )
132  return _makeNodeOrder( nodeLayer );
133 
134  lst << id;
135  }
136 
137  return lst;
138 }
139 
141 {
142  return nodeLayer->customProperties().contains( QStringLiteral( "legend/node-order" ) );
143 }
144 
145 void QgsMapLayerLegendUtils::setLegendNodeUserLabel( QgsLayerTreeLayer *nodeLayer, int originalIndex, const QString &newLabel )
146 {
147  nodeLayer->setCustomProperty( "legend/label-" + QString::number( originalIndex ), newLabel );
148 }
149 
150 QString QgsMapLayerLegendUtils::legendNodeUserLabel( QgsLayerTreeLayer *nodeLayer, int originalIndex )
151 {
152  return nodeLayer->customProperty( "legend/label-" + QString::number( originalIndex ) ).toString();
153 }
154 
156 {
157  return nodeLayer->customProperties().contains( "legend/label-" + QString::number( originalIndex ) );
158 }
159 
161 {
162  QDomDocument patchDoc;
163  QDomElement patchElem = patchDoc.createElement( QStringLiteral( "patch" ) );
164  shape.writeXml( patchElem, patchDoc, QgsReadWriteContext() );
165  patchDoc.appendChild( patchElem );
166  nodeLayer->setCustomProperty( "legend/patch-shape-" + QString::number( originalIndex ), patchDoc.toString() );
167 }
168 
170 {
171  QString patchDef = nodeLayer->customProperty( "legend/patch-shape-" + QString::number( originalIndex ) ).toString();
172  if ( patchDef.isEmpty() )
173  return QgsLegendPatchShape();
174 
175  QDomDocument doc( QStringLiteral( "patch" ) );
176  doc.setContent( patchDef );
177  QgsLegendPatchShape shape;
178  shape.readXml( doc.documentElement(), QgsReadWriteContext() );
179  return shape;
180 }
181 
182 void QgsMapLayerLegendUtils::setLegendNodeSymbolSize( QgsLayerTreeLayer *nodeLayer, int originalIndex, QSizeF size )
183 {
184  if ( size.isValid() )
185  nodeLayer->setCustomProperty( "legend/symbol-size-" + QString::number( originalIndex ), QgsSymbolLayerUtils::encodeSize( size ) );
186  else
187  nodeLayer->removeCustomProperty( "legend/symbol-size-" + QString::number( originalIndex ) );
188 }
189 
191 {
192  const QString size = nodeLayer->customProperty( "legend/symbol-size-" + QString::number( originalIndex ) ).toString();
193  if ( size.isEmpty() )
194  return QSizeF();
195  else
196  return QgsSymbolLayerUtils::decodeSize( size );
197 }
198 
199 void QgsMapLayerLegendUtils::setLegendNodeCustomSymbol( QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsSymbol *symbol )
200 {
201  if ( symbol )
202  {
203  QDomDocument doc;
204  QgsReadWriteContext rwContext;
205  rwContext.setPathResolver( QgsProject::instance()->pathResolver() );
206  QDomElement elem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "custom symbol" ), symbol, doc, rwContext );
207  doc.appendChild( elem );
208  nodeLayer->setCustomProperty( "legend/custom-symbol-" + QString::number( originalIndex ), doc.toString() );
209  }
210  else
211  nodeLayer->removeCustomProperty( "legend/custom-symbol-" + QString::number( originalIndex ) );
212 }
213 
215 {
216  const QString symbolDef = nodeLayer->customProperty( "legend/custom-symbol-" + QString::number( originalIndex ) ).toString();
217  if ( symbolDef.isEmpty() )
218  return nullptr;
219 
220  QDomDocument doc;
221  doc.setContent( symbolDef );
222  const QDomElement elem = doc.documentElement();
223 
224  QgsReadWriteContext rwContext;
225  rwContext.setPathResolver( QgsProject::instance()->pathResolver() );
226 
227  return QgsSymbolLayerUtils::loadSymbol( elem, rwContext );
228 }
229 
231 {
232  if ( settings )
233  {
234  QDomDocument doc;
235  QgsReadWriteContext rwContext;
236  rwContext.setPathResolver( QgsProject::instance()->pathResolver() );
237  QDomElement elem = doc.createElement( QStringLiteral( "rampSettings" ) );
238  settings->writeXml( doc, elem, rwContext );
239  doc.appendChild( elem );
240  nodeLayer->setCustomProperty( "legend/custom-ramp-settings-" + QString::number( originalIndex ), doc.toString() );
241  }
242  else
243  nodeLayer->removeCustomProperty( "legend/custom-ramp-settings-" + QString::number( originalIndex ) );
244 }
245 
247 {
248  const QString settingsDef = nodeLayer->customProperty( "legend/custom-ramp-settings-" + QString::number( originalIndex ) ).toString();
249  if ( settingsDef.isEmpty() )
250  return nullptr;
251 
252  QDomDocument doc;
253  doc.setContent( settingsDef );
254  const QDomElement elem = doc.documentElement();
255 
256  QgsReadWriteContext rwContext;
257  rwContext.setPathResolver( QgsProject::instance()->pathResolver() );
258 
260  settings.readXml( elem, rwContext );
261  return new QgsColorRampLegendNodeSettings( settings );
262 }
263 
264 void QgsMapLayerLegendUtils::setLegendNodeColumnBreak( QgsLayerTreeLayer *nodeLayer, int originalIndex, bool columnBreakBeforeNode )
265 {
266  if ( columnBreakBeforeNode )
267  nodeLayer->setCustomProperty( "legend/column-break-" + QString::number( originalIndex ), QStringLiteral( "1" ) );
268  else
269  nodeLayer->removeCustomProperty( "legend/column-break-" + QString::number( originalIndex ) );
270 }
271 
273 {
274  return nodeLayer->customProperty( "legend/column-break-" + QString::number( originalIndex ) ).toInt();
275 }
276 
277 void QgsMapLayerLegendUtils::applyLayerNodeProperties( QgsLayerTreeLayer *nodeLayer, QList<QgsLayerTreeModelLegendNode *> &nodes )
278 {
279  // handle user labels
280  int i = 0;
281  const auto constNodes = nodes;
282  for ( QgsLayerTreeModelLegendNode *legendNode : constNodes )
283  {
284  QString userLabel = QgsMapLayerLegendUtils::legendNodeUserLabel( nodeLayer, i );
285  if ( !userLabel.isNull() )
286  legendNode->setUserLabel( userLabel );
287 
288  if ( QgsSymbolLegendNode *symbolNode = dynamic_cast< QgsSymbolLegendNode * >( legendNode ) )
289  {
291  symbolNode->setPatchShape( shape );
292 
293  symbolNode->setCustomSymbol( QgsMapLayerLegendUtils::legendNodeCustomSymbol( nodeLayer, i ) );
294  }
295  else if ( QgsColorRampLegendNode *colorRampNode = dynamic_cast< QgsColorRampLegendNode * >( legendNode ) )
296  {
297  std::unique_ptr< QgsColorRampLegendNodeSettings > settings( QgsMapLayerLegendUtils::legendNodeColorRampSettings( nodeLayer, i ) );
298  if ( settings )
299  {
300  colorRampNode->setSettings( *settings );
301  }
302  }
303 
304  const QSizeF userSize = QgsMapLayerLegendUtils::legendNodeSymbolSize( nodeLayer, i );
305  if ( userSize.isValid() )
306  {
307  legendNode->setUserPatchSize( userSize );
308  }
309 
310  if ( legendNodeColumnBreak( nodeLayer, i ) )
311  legendNode->setColumnBreak( true );
312 
313  i++;
314  }
315 
316  // handle user order of nodes
318  {
319  QList<int> order = QgsMapLayerLegendUtils::legendNodeOrder( nodeLayer );
320 
321  QList<QgsLayerTreeModelLegendNode *> newOrder;
322  QSet<int> usedIndices;
323  const auto constOrder = order;
324  for ( int idx : constOrder )
325  {
326  if ( usedIndices.contains( idx ) )
327  {
328  QgsDebugMsg( QStringLiteral( "invalid node order. ignoring." ) );
329  return;
330  }
331 
332  newOrder << nodes[idx];
333  usedIndices << idx;
334  }
335 
336  // delete unused nodes
337  for ( int i = 0; i < nodes.count(); ++i )
338  {
339  if ( !usedIndices.contains( i ) )
340  delete nodes[i];
341  }
342 
343  nodes = newOrder;
344  }
345 
346 }
347 
348 // -------------------------------------------------------------------------
349 
350 
352  : mLayer( vl )
353 {
355  connect( mLayer, &QgsMapLayer::nameChanged, this, &QgsMapLayerLegend::itemsChanged );
356 }
357 
358 QList<QgsLayerTreeModelLegendNode *> QgsDefaultVectorLayerLegend::createLayerTreeModelLegendNodes( QgsLayerTreeLayer *nodeLayer )
359 {
360  QList<QgsLayerTreeModelLegendNode *> nodes;
361 
362  QgsFeatureRenderer *r = mLayer->renderer();
363  if ( !r )
364  return nodes;
365 
366  if ( nodeLayer->customProperty( QStringLiteral( "showFeatureCount" ), 0 ).toBool() )
367  mLayer->countSymbolFeatures();
368 
369  QgsSettings settings;
370  if ( settings.value( QStringLiteral( "qgis/showLegendClassifiers" ), false ).toBool() && !r->legendClassificationAttribute().isEmpty() )
371  {
372  nodes.append( new QgsSimpleLegendNode( nodeLayer, r->legendClassificationAttribute() ) );
373  }
374 
375  const auto constLegendSymbolItems = r->legendSymbolItems();
376  for ( const QgsLegendSymbolItem &i : constLegendSymbolItems )
377  {
378  if ( auto *lDataDefinedSizeLegendSettings = i.dataDefinedSizeLegendSettings() )
379  nodes << new QgsDataDefinedSizeLegendNode( nodeLayer, *lDataDefinedSizeLegendSettings );
380  else
381  {
382  QgsSymbolLegendNode *legendNode = new QgsSymbolLegendNode( nodeLayer, i );
383  if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( i.ruleKey() ) )
384  {
385  legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( i.ruleKey() ) );
386  legendNode->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
387  }
388  nodes << legendNode;
389  }
390  }
391 
392  if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() )
393  nodes[0]->setEmbeddedInParent( true );
394 
395 
396  if ( mLayer->diagramsEnabled() )
397  {
398  const auto constLegendItems = mLayer->diagramRenderer()->legendItems( nodeLayer );
399  for ( QgsLayerTreeModelLegendNode *i : constLegendItems )
400  {
401  nodes.append( i );
402  }
403  }
404 
405  if ( mLayer->labelsEnabled() && mShowLabelLegend )
406  {
407  const QgsAbstractVectorLayerLabeling *labeling = mLayer->labeling();
408  if ( labeling )
409  {
410  QStringList pList = labeling->subProviders();
411  for ( int i = 0; i < pList.size(); ++i )
412  {
413  const QgsPalLayerSettings s = labeling->settings( pList.at( i ) );
414  QString description;
415  const QgsRuleBasedLabeling *ruleBasedLabeling = dynamic_cast<const QgsRuleBasedLabeling *>( labeling );
416  if ( ruleBasedLabeling && ruleBasedLabeling->rootRule() )
417  {
418  const QgsRuleBasedLabeling::Rule *rule = ruleBasedLabeling->rootRule()->findRuleByKey( pList.at( i ) );
419  if ( rule )
420  {
421  description = rule->description();
422  }
423  }
424  QgsVectorLabelLegendNode *node = new QgsVectorLabelLegendNode( nodeLayer, s );
425  node->setUserLabel( description );
426  nodes.append( node );
427  }
428  }
429  }
430 
431 
432  return nodes;
433 }
434 
435 void QgsDefaultVectorLayerLegend::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
436 {
437  mTextOnSymbolEnabled = false;
438  mTextOnSymbolTextFormat = QgsTextFormat();
439  mTextOnSymbolContent.clear();
440 
441  mShowLabelLegend = elem.attribute( QStringLiteral( "showLabelLegend" ), QStringLiteral( "0" ) ).compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0;
442 
443  QDomElement tosElem = elem.firstChildElement( QStringLiteral( "text-on-symbol" ) );
444  if ( !tosElem.isNull() )
445  {
446  mTextOnSymbolEnabled = true;
447  QDomElement tosFormatElem = tosElem.firstChildElement( QStringLiteral( "text-style" ) );
448  mTextOnSymbolTextFormat.readXml( tosFormatElem, context );
449  QDomElement tosContentElem = tosElem.firstChildElement( QStringLiteral( "content" ) );
450  QDomElement tosContentItemElem = tosContentElem.firstChildElement( QStringLiteral( "item" ) );
451  while ( !tosContentItemElem.isNull() )
452  {
453  mTextOnSymbolContent.insert( tosContentItemElem.attribute( QStringLiteral( "key" ) ), tosContentItemElem.attribute( QStringLiteral( "value" ) ) );
454  tosContentItemElem = tosContentItemElem.nextSiblingElement( QStringLiteral( "item" ) );
455  }
456  }
457 }
458 
459 QDomElement QgsDefaultVectorLayerLegend::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
460 {
461  QDomElement elem = doc.createElement( QStringLiteral( "legend" ) );
462  elem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "default-vector" ) );
463  elem.setAttribute( QStringLiteral( "showLabelLegend" ), mShowLabelLegend );
464 
465  if ( mTextOnSymbolEnabled )
466  {
467  QDomElement tosElem = doc.createElement( QStringLiteral( "text-on-symbol" ) );
468  QDomElement tosFormatElem = mTextOnSymbolTextFormat.writeXml( doc, context );
469  tosElem.appendChild( tosFormatElem );
470  QDomElement tosContentElem = doc.createElement( QStringLiteral( "content" ) );
471  for ( auto it = mTextOnSymbolContent.constBegin(); it != mTextOnSymbolContent.constEnd(); ++it )
472  {
473  QDomElement tosContentItemElem = doc.createElement( QStringLiteral( "item" ) );
474  tosContentItemElem.setAttribute( QStringLiteral( "key" ), it.key() );
475  tosContentItemElem.setAttribute( QStringLiteral( "value" ), it.value() );
476  tosContentElem.appendChild( tosContentItemElem );
477  }
478  tosElem.appendChild( tosContentElem );
479  elem.appendChild( tosElem );
480  }
481 
482  return elem;
483 }
484 
485 
486 // -------------------------------------------------------------------------
487 
488 
490  : mLayer( rl )
491 {
493 }
494 
495 QList<QgsLayerTreeModelLegendNode *> QgsDefaultRasterLayerLegend::createLayerTreeModelLegendNodes( QgsLayerTreeLayer *nodeLayer )
496 {
497  QList<QgsLayerTreeModelLegendNode *> nodes;
498 
499  // temporary solution for WMS. Ideally should be done with a delegate.
500  if ( mLayer->dataProvider() && mLayer->dataProvider()->supportsLegendGraphic() )
501  {
502  nodes << new QgsWmsLegendNode( nodeLayer );
503  }
504 
505  if ( mLayer->renderer() )
506  nodes.append( mLayer->renderer()->createLegendNodes( nodeLayer ) );
507  return nodes;
508 }
509 
510 // -------------------------------------------------------------------------
511 
513  : mLayer( ml )
514 {
516 }
517 
518 QList<QgsLayerTreeModelLegendNode *> QgsDefaultMeshLayerLegend::createLayerTreeModelLegendNodes( QgsLayerTreeLayer *nodeLayer )
519 {
520  QList<QgsLayerTreeModelLegendNode *> nodes;
521 
522  QgsMeshDataProvider *provider = mLayer->dataProvider();
523  if ( !provider )
524  return nodes;
525 
526  QgsMeshRendererSettings rendererSettings = mLayer->rendererSettings();
527 
528  int indexScalar = rendererSettings.activeScalarDatasetGroup();
529  int indexVector = rendererSettings.activeVectorDatasetGroup();
530 
531  QString name;
532  if ( indexScalar > -1 && indexVector > -1 && indexScalar != indexVector )
533  name = QString( "%1 / %2" ).arg( provider->datasetGroupMetadata( indexScalar ).name(), provider->datasetGroupMetadata( indexVector ).name() );
534  else if ( indexScalar > -1 )
535  name = provider->datasetGroupMetadata( indexScalar ).name();
536  else if ( indexVector > -1 )
537  name = provider->datasetGroupMetadata( indexVector ).name();
538  else
539  {
540  // neither contours nor vectors get rendered - no legend needed
541  return nodes;
542  }
543 
544  nodes << new QgsSimpleLegendNode( nodeLayer, name );
545 
546  if ( indexScalar > -1 )
547  {
548  QgsMeshRendererScalarSettings settings = rendererSettings.scalarSettings( indexScalar );
549  const QgsColorRampShader shader = settings.colorRampShader();
550  switch ( shader.colorRampType() )
551  {
553  if ( ! shader.legendSettings() || shader.legendSettings()->useContinuousLegend() )
554  {
555  // for interpolated shaders we use a ramp legend node
556  if ( !shader.colorRampItemList().isEmpty() )
557  {
558  nodes << new QgsColorRampLegendNode( nodeLayer, shader.createColorRamp(),
560  shader.minimumValue(),
561  shader.maximumValue() );
562  }
563  break;
564  }
565  Q_FALLTHROUGH();
568  {
569  // for all others we use itemised lists
570  QgsLegendColorList items;
571  settings.colorRampShader().legendSymbologyItems( items );
572  for ( const QPair< QString, QColor > &item : items )
573  {
574  nodes << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
575  }
576  break;
577  }
578  }
579  }
580 
581  return nodes;
582 }
583 
584 //
585 // QgsDefaultPointCloudLayerLegend
586 //
587 
589  : mLayer( layer )
590 {
592 }
593 
595 {
596  QgsPointCloudRenderer *renderer = mLayer->renderer();
597  if ( !renderer )
598  return QList<QgsLayerTreeModelLegendNode *>();
599 
600  return renderer->createLegendNodes( nodeLayer );
601 }
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.
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.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
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.
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:362
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.
virtual QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer)=0
Returns list of legend nodes to be used for a particular layer tree layer node.
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.
void itemsChanged()
Emitted when existing items/nodes got invalid and should be replaced by new ones.
QgsMapLayerLegend * legend() const
Can be nullptr.
void rendererChanged()
Signal emitted when renderer is changed.
void nameChanged()
Emitted when the name has been changed.
Base class for providing data for QgsMeshLayer.
QString name() const
Returns name of the dataset group.
virtual QgsMeshDatasetGroupMetadata datasetGroupMetadata(int groupIndex) const =0
Returns dataset group metadata.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:95
QgsMeshRendererSettings rendererSettings() const
Returns renderer settings.
QgsMeshDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
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:467
virtual bool supportsLegendGraphic() const
Returns whether the provider supplies a legend graphic.
Represents a raster layer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
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()
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:38
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.
const QgsDiagramRenderer * diagramRenderer() const
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
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