QGIS API Documentation 3.35.0-Master (274c3da7543)
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 auto constLegendSymbolItems = r->legendSymbolItems();
388 for ( const QgsLegendSymbolItem &i : constLegendSymbolItems )
389 {
390 if ( auto *lDataDefinedSizeLegendSettings = i.dataDefinedSizeLegendSettings() )
391 nodes << new QgsDataDefinedSizeLegendNode( nodeLayer, *lDataDefinedSizeLegendSettings );
392 else
393 {
394 QgsSymbolLegendNode *legendNode = new QgsSymbolLegendNode( nodeLayer, i );
395 if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( i.ruleKey() ) )
396 {
397 legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( i.ruleKey() ) );
398 legendNode->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
399 }
400 nodes << legendNode;
401 }
402 }
403
404 if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() )
405 nodes[0]->setEmbeddedInParent( true );
406
407
408 if ( mLayer->diagramsEnabled() )
409 {
410 const auto constLegendItems = mLayer->diagramRenderer()->legendItems( nodeLayer );
411 for ( QgsLayerTreeModelLegendNode *i : constLegendItems )
412 {
413 nodes.append( i );
414 }
415 }
416
417 if ( mLayer->labelsEnabled() && mShowLabelLegend )
418 {
419 const QgsAbstractVectorLayerLabeling *labeling = mLayer->labeling();
420 if ( labeling )
421 {
422 const QStringList pList = labeling->subProviders();
423 for ( int i = 0; i < pList.size(); ++i )
424 {
425 const QgsPalLayerSettings s = labeling->settings( pList.at( i ) );
426 QString description;
427 const QgsRuleBasedLabeling *ruleBasedLabeling = dynamic_cast<const QgsRuleBasedLabeling *>( labeling );
428 if ( ruleBasedLabeling && ruleBasedLabeling->rootRule() )
429 {
430 const QgsRuleBasedLabeling::Rule *rule = ruleBasedLabeling->rootRule()->findRuleByKey( pList.at( i ) );
431 if ( rule )
432 {
433 description = rule->description();
434 }
435 }
436 QgsVectorLabelLegendNode *node = new QgsVectorLabelLegendNode( nodeLayer, s );
437 node->setUserLabel( description );
438 nodes.append( node );
439 }
440 }
441 }
442
443
444 return nodes;
445}
446
447void QgsDefaultVectorLayerLegend::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
448{
449 mTextOnSymbolEnabled = false;
450 mTextOnSymbolTextFormat = QgsTextFormat();
451 mTextOnSymbolContent.clear();
452
453 mShowLabelLegend = elem.attribute( QStringLiteral( "showLabelLegend" ), QStringLiteral( "0" ) ).compare( QStringLiteral( "1" ), Qt::CaseInsensitive ) == 0;
454
455 const QDomElement tosElem = elem.firstChildElement( QStringLiteral( "text-on-symbol" ) );
456 if ( !tosElem.isNull() )
457 {
458 mTextOnSymbolEnabled = true;
459 const QDomElement tosFormatElem = tosElem.firstChildElement( QStringLiteral( "text-style" ) );
460 mTextOnSymbolTextFormat.readXml( tosFormatElem, context );
461 const QDomElement tosContentElem = tosElem.firstChildElement( QStringLiteral( "content" ) );
462 QDomElement tosContentItemElem = tosContentElem.firstChildElement( QStringLiteral( "item" ) );
463 while ( !tosContentItemElem.isNull() )
464 {
465 mTextOnSymbolContent.insert( tosContentItemElem.attribute( QStringLiteral( "key" ) ), tosContentItemElem.attribute( QStringLiteral( "value" ) ) );
466 tosContentItemElem = tosContentItemElem.nextSiblingElement( QStringLiteral( "item" ) );
467 }
468 }
469}
470
471QDomElement QgsDefaultVectorLayerLegend::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
472{
473 QDomElement elem = doc.createElement( QStringLiteral( "legend" ) );
474 elem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "default-vector" ) );
475 elem.setAttribute( QStringLiteral( "showLabelLegend" ), mShowLabelLegend );
476
477 if ( mTextOnSymbolEnabled )
478 {
479 QDomElement tosElem = doc.createElement( QStringLiteral( "text-on-symbol" ) );
480 const QDomElement tosFormatElem = mTextOnSymbolTextFormat.writeXml( doc, context );
481 tosElem.appendChild( tosFormatElem );
482 QDomElement tosContentElem = doc.createElement( QStringLiteral( "content" ) );
483 for ( auto it = mTextOnSymbolContent.constBegin(); it != mTextOnSymbolContent.constEnd(); ++it )
484 {
485 QDomElement tosContentItemElem = doc.createElement( QStringLiteral( "item" ) );
486 tosContentItemElem.setAttribute( QStringLiteral( "key" ), it.key() );
487 tosContentItemElem.setAttribute( QStringLiteral( "value" ), it.value() );
488 tosContentElem.appendChild( tosContentItemElem );
489 }
490 tosElem.appendChild( tosContentElem );
491 elem.appendChild( tosElem );
492 }
493
494 return elem;
495}
496
497
498// -------------------------------------------------------------------------
499
500
506
508{
509 QList<QgsLayerTreeModelLegendNode *> nodes;
510
511 // temporary solution for WMS. Ideally should be done with a delegate.
512 if ( mLayer->dataProvider() && mLayer->dataProvider()->supportsLegendGraphic() )
513 {
514 nodes << new QgsWmsLegendNode( nodeLayer );
515 }
516
517 const QString placeholderImage = mLayer->legendPlaceholderImage();
518 if ( !placeholderImage.isEmpty() )
519 {
520 bool fitsInCache;
521 const QImage img = QgsApplication::imageCache()->pathAsImage( placeholderImage, QSize(), false, 1.0, fitsInCache );
522 nodes << new QgsImageLegendNode( nodeLayer, img );
523 }
524 else if ( mLayer->renderer() )
525 nodes.append( mLayer->renderer()->createLegendNodes( nodeLayer ) );
526 return nodes;
527}
528
529// -------------------------------------------------------------------------
530
536
538{
539 QList<QgsLayerTreeModelLegendNode *> nodes;
540
541 const QgsMeshRendererSettings rendererSettings = mLayer->rendererSettings();
542
543 const int indexScalar = rendererSettings.activeScalarDatasetGroup();
544 const int indexVector = rendererSettings.activeVectorDatasetGroup();
545
546 QString name;
547 if ( indexScalar > -1 && indexVector > -1 && indexScalar != indexVector )
548 name = QString( "%1 / %2" ).arg( mLayer->datasetGroupMetadata( indexScalar ).name(), mLayer->datasetGroupMetadata( indexVector ).name() );
549 else if ( indexScalar > -1 )
550 name = mLayer->datasetGroupMetadata( indexScalar ).name();
551 else if ( indexVector > -1 )
552 name = mLayer->datasetGroupMetadata( indexVector ).name();
553 else
554 {
555 // neither contours nor vectors get rendered - no legend needed
556 return nodes;
557 }
558
559 nodes << new QgsSimpleLegendNode( nodeLayer, name );
560
561 if ( indexScalar > -1 )
562 {
563 const QgsMeshRendererScalarSettings settings = rendererSettings.scalarSettings( indexScalar );
564 const QgsColorRampShader shader = settings.colorRampShader();
565 switch ( shader.colorRampType() )
566 {
568 if ( ! shader.legendSettings() || shader.legendSettings()->useContinuousLegend() )
569 {
570 // for interpolated shaders we use a ramp legend node
571 if ( !shader.colorRampItemList().isEmpty() )
572 {
573 nodes << new QgsColorRampLegendNode( nodeLayer, shader.createColorRamp(),
575 shader.minimumValue(),
576 shader.maximumValue() );
577 }
578 break;
579 }
580 [[fallthrough]];
583 {
584 // for all others we use itemised lists
585 QgsLegendColorList items;
586 settings.colorRampShader().legendSymbologyItems( items );
587 for ( const QPair< QString, QColor > &item : items )
588 {
589 nodes << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
590 }
591 break;
592 }
593 }
594 }
595
596 return nodes;
597}
598
599//
600// QgsDefaultPointCloudLayerLegend
601//
602
608
610{
611 QgsPointCloudRenderer *renderer = mLayer->renderer();
612 if ( !renderer )
613 return QList<QgsLayerTreeModelLegendNode *>();
614
615 return renderer->createLegendNodes( nodeLayer );
616}
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
virtual QStringList subProviders() const
Gets list of sub-providers within the layer's labeling.
virtual QgsPalLayerSettings settings(const QString &providerId=QString()) const =0
Gets associated label settings.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
Settings for a color ramp legend node.
bool useContinuousLegend() const
Returns true if a continuous gradient legend will be used.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads settings from an XML element.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const
Writes settings to an XML element.
A legend node which renders a color ramp.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Returns legend symbology items if provided by renderer.
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
Type colorRampType() const
Returns the color ramp type.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
QgsColorRamp * createColorRamp() const
Creates a gradient color ramp from shader settings.
@ Interpolated
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
Produces legend node with a marker symbol.
Default legend implementation for mesh layers.
QgsDefaultMeshLayerLegend(QgsMeshLayer *ml)
Creates an instance for the given mesh layer.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
Default legend implementation for point cloud layers.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
QgsDefaultPointCloudLayerLegend(QgsPointCloudLayer *layer)
Creates an instance for the given point cloud layer.
Default legend implementation for raster layers.
QgsDefaultRasterLayerLegend(QgsRasterLayer *rl)
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
Default legend implementation for vector layers.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes configuration to a DOM element, to be used later with readXml()
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads configuration from a DOM element previously written by writeXml()
QgsDefaultVectorLayerLegend(QgsVectorLayer *vl)
virtual QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
virtual QString legendClassificationAttribute() const
If supported by the renderer, return classification attribute for the use in legend.
QImage pathAsImage(const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking=false, double targetDpi=96, int frameNumber=-1, bool *isMissing=nullptr)
Returns the specified path rendered as an image.
Implementation of legend node interface for displaying arbitrary raster image.
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
virtual void setColumnBreak(bool breakBeforeNode)
Sets whether a forced column break should occur before the node.
virtual void setUserPatchSize(QSizeF size)
Sets the user (overridden) size for the legend node.
virtual void setEmbeddedInParent(bool embedded)
virtual void setUserLabel(const QString &userLabel)
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
void removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file.
QStringList customProperties() const
Returns list of keys stored in custom properties.
Represents a patch shape for use in map legends.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read settings from a DOM element.
void writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
static void setLegendNodeOrder(QgsLayerTreeLayer *nodeLayer, const QList< int > &order)
static void setLegendNodeColorRampSettings(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsColorRampLegendNodeSettings *settings)
Sets a custom legend color ramp settings for the legend node belonging to nodeLayer at the specified ...
static void setLegendNodeCustomSymbol(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsSymbol *symbol)
Sets a custom legend symbol for the legend node belonging to nodeLayer at the specified originalIndex...
static QString legendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex)
static QSizeF legendNodeSymbolSize(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the legend node symbol size for the legend node belonging to nodeLayer at the specified origi...
static bool hasLegendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex)
static bool legendNodeColumnBreak(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns whether a forced column break should occur before the node.
static bool hasLegendNodeOrder(QgsLayerTreeLayer *nodeLayer)
static void setLegendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QString &newLabel)
static void setLegendNodeSymbolSize(QgsLayerTreeLayer *nodeLayer, int originalIndex, QSizeF size)
Sets the legend symbol size for the legend node belonging to nodeLayer at the specified originalIndex...
static QgsColorRampLegendNodeSettings * legendNodeColorRampSettings(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the custom legend color ramp settings for the legend node belonging to nodeLayer at the speci...
static void setLegendNodeColumnBreak(QgsLayerTreeLayer *nodeLayer, int originalIndex, bool columnBreakBeforeNode)
Sets whether a forced column break should occur before the node.
static void setLegendNodePatchShape(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsLegendPatchShape &shape)
Sets the legend patch shape for the legend node belonging to nodeLayer at the specified originalIndex...
static QgsSymbol * legendNodeCustomSymbol(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the custom legend symbol for the legend node belonging to nodeLayer at the specified original...
static QList< int > legendNodeOrder(QgsLayerTreeLayer *nodeLayer)
static void applyLayerNodeProperties(QgsLayerTreeLayer *nodeLayer, QList< QgsLayerTreeModelLegendNode * > &nodes)
update according to layer node's custom properties (order of items, user labels for items)
static QgsLegendPatchShape legendNodePatchShape(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the legend patch shape for the legend node belonging to nodeLayer at the specified originalIn...
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads configuration from a DOM element previously written by writeXml()
static QgsMapLayerLegend * defaultMeshLegend(QgsMeshLayer *ml)
Create new legend implementation for mesh layer.
static QgsMapLayerLegend * defaultPointCloudLegend(QgsPointCloudLayer *layer)
Create new legend implementation for a point cloud layer.
QgsMapLayerLegend(QObject *parent=nullptr)
Constructor for QgsMapLayerLegend.
virtual QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Writes configuration to a DOM element, to be used later with readXml()
static QgsMapLayerLegend * defaultRasterLegend(QgsRasterLayer *rl)
Create new legend implementation for raster layer.
static QgsMapLayerLegend * defaultVectorLegend(QgsVectorLayer *vl)
Create new legend implementation for vector layer.
virtual QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer)=0
Returns list of legend nodes to be used for a particular layer tree layer node.
void itemsChanged()
Emitted when existing items/nodes got invalid and should be replaced by new ones.
QgsMapLayerLegend * legend() const
Can be nullptr.
QString legendPlaceholderImage() const
Returns path to the placeholder image or an empty string if a generated legend is shown.
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:65
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