QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
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(
',' ) );
92 const int numNodes = lst.count();
101 QgsDebugMsg( QStringLiteral(
"Legend node order manipulation is invalid without existing legend" ) );
105 const int numNodes = _originalLegendNodeCount( nodeLayer );
108 order.reserve( numNodes );
109 for (
int i = 0; i < numNodes; ++i )
116 const QString orderStr = nodeLayer->
customProperty( QStringLiteral(
"legend/node-order" ) ).toString();
118 if ( orderStr.isEmpty() )
119 return _makeNodeOrder( nodeLayer );
121 if ( orderStr == QLatin1String(
"empty" ) )
124 const int numNodes = _originalLegendNodeCount( nodeLayer );
127 const auto constSplit = orderStr.split(
',' );
128 for (
const QString &item : constSplit )
131 const int id = item.toInt( &ok );
132 if ( !ok || id < 0 || id >= numNodes )
133 return _makeNodeOrder( nodeLayer );
143 return nodeLayer->
customProperties().contains( QStringLiteral(
"legend/node-order" ) );
148 nodeLayer->
setCustomProperty(
"legend/label-" + QString::number( originalIndex ), newLabel );
153 return nodeLayer->
customProperty(
"legend/label-" + QString::number( originalIndex ) ).toString();
158 return nodeLayer->
customProperties().contains(
"legend/label-" + QString::number( originalIndex ) );
163 QDomDocument patchDoc;
164 QDomElement patchElem = patchDoc.createElement( QStringLiteral(
"patch" ) );
166 patchDoc.appendChild( patchElem );
167 nodeLayer->
setCustomProperty(
"legend/patch-shape-" + QString::number( originalIndex ), patchDoc.toString() );
172 const QString patchDef = nodeLayer->
customProperty(
"legend/patch-shape-" + QString::number( originalIndex ) ).toString();
173 if ( patchDef.isEmpty() )
176 QDomDocument doc( QStringLiteral(
"patch" ) );
177 doc.setContent( patchDef );
185 if ( size.isValid() )
193 const QString size = nodeLayer->
customProperty(
"legend/symbol-size-" + QString::number( originalIndex ) ).toString();
194 if ( size.isEmpty() )
208 doc.appendChild( elem );
209 nodeLayer->
setCustomProperty(
"legend/custom-symbol-" + QString::number( originalIndex ), doc.toString() );
217 const QString symbolDef = nodeLayer->
customProperty(
"legend/custom-symbol-" + QString::number( originalIndex ) ).toString();
218 if ( symbolDef.isEmpty() )
222 doc.setContent( symbolDef );
223 const QDomElement elem = doc.documentElement();
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() );
244 nodeLayer->
removeCustomProperty(
"legend/custom-ramp-settings-" + QString::number( originalIndex ) );
249 const QString settingsDef = nodeLayer->
customProperty(
"legend/custom-ramp-settings-" + QString::number( originalIndex ) ).toString();
250 if ( settingsDef.isEmpty() )
254 doc.setContent( settingsDef );
255 const QDomElement elem = doc.documentElement();
261 settings.
readXml( elem, rwContext );
267 if ( columnBreakBeforeNode )
268 nodeLayer->
setCustomProperty(
"legend/column-break-" + QString::number( originalIndex ), QStringLiteral(
"1" ) );
275 return nodeLayer->
customProperty(
"legend/column-break-" + QString::number( originalIndex ) ).toInt();
282 const auto constNodes = nodes;
286 if ( !userLabel.isNull() )
292 symbolNode->setPatchShape( shape );
301 colorRampNode->setSettings( *settings );
306 if ( userSize.isValid() )
322 QList<QgsLayerTreeModelLegendNode *> newOrder;
323 QSet<int> usedIndices;
324 const auto constOrder = order;
325 for (
const int idx : constOrder )
327 if ( usedIndices.contains( idx ) )
329 QgsDebugMsg( QStringLiteral(
"invalid node order. ignoring." ) );
333 newOrder << nodes[idx];
338 for (
int i = 0; i < nodes.count(); ++i )
340 if ( !usedIndices.contains( i ) )
361 QList<QgsLayerTreeModelLegendNode *> nodes;
366 if ( !placeholderImage.isEmpty() )
379 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toBool() )
391 if (
auto *lDataDefinedSizeLegendSettings = i.dataDefinedSizeLegendSettings() )
396 if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( i.ruleKey() ) )
398 legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( i.ruleKey() ) );
399 legendNode->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
405 if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() )
424 for (
int i = 0; i < pList.size(); ++i )
429 if ( ruleBasedLabeling && ruleBasedLabeling->
rootRule() )
439 nodes.append( node );
450 mTextOnSymbolEnabled =
false;
452 mTextOnSymbolContent.clear();
454 mShowLabelLegend = elem.attribute( QStringLiteral(
"showLabelLegend" ), QStringLiteral(
"0" ) ).compare( QStringLiteral(
"1" ), Qt::CaseInsensitive ) == 0;
456 const QDomElement tosElem = elem.firstChildElement( QStringLiteral(
"text-on-symbol" ) );
457 if ( !tosElem.isNull() )
459 mTextOnSymbolEnabled =
true;
460 const QDomElement tosFormatElem = tosElem.firstChildElement( QStringLiteral(
"text-style" ) );
461 mTextOnSymbolTextFormat.
readXml( tosFormatElem, context );
462 const QDomElement tosContentElem = tosElem.firstChildElement( QStringLiteral(
"content" ) );
463 QDomElement tosContentItemElem = tosContentElem.firstChildElement( QStringLiteral(
"item" ) );
464 while ( !tosContentItemElem.isNull() )
466 mTextOnSymbolContent.insert( tosContentItemElem.attribute( QStringLiteral(
"key" ) ), tosContentItemElem.attribute( QStringLiteral(
"value" ) ) );
467 tosContentItemElem = tosContentItemElem.nextSiblingElement( QStringLiteral(
"item" ) );
474 QDomElement elem = doc.createElement( QStringLiteral(
"legend" ) );
475 elem.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"default-vector" ) );
476 elem.setAttribute( QStringLiteral(
"showLabelLegend" ), mShowLabelLegend );
478 if ( mTextOnSymbolEnabled )
480 QDomElement tosElem = doc.createElement( QStringLiteral(
"text-on-symbol" ) );
481 const QDomElement tosFormatElem = mTextOnSymbolTextFormat.
writeXml( doc, context );
482 tosElem.appendChild( tosFormatElem );
483 QDomElement tosContentElem = doc.createElement( QStringLiteral(
"content" ) );
484 for (
auto it = mTextOnSymbolContent.constBegin(); it != mTextOnSymbolContent.constEnd(); ++it )
486 QDomElement tosContentItemElem = doc.createElement( QStringLiteral(
"item" ) );
487 tosContentItemElem.setAttribute( QStringLiteral(
"key" ), it.key() );
488 tosContentItemElem.setAttribute( QStringLiteral(
"value" ), it.value() );
489 tosContentElem.appendChild( tosContentItemElem );
491 tosElem.appendChild( tosContentElem );
492 elem.appendChild( tosElem );
510 QList<QgsLayerTreeModelLegendNode *> nodes;
519 if ( !placeholderImage.isEmpty() )
540 QList<QgsLayerTreeModelLegendNode *> nodes;
548 if ( indexScalar > -1 && indexVector > -1 && indexScalar != indexVector )
550 else if ( indexScalar > -1 )
552 else if ( indexVector > -1 )
562 if ( indexScalar > -1 )
588 for (
const QPair< QString, QColor > &item : items )
614 return QList<QgsLayerTreeModelLegendNode *>();
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...
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads configuration from a DOM element previously written by writeXml()
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
bool diagramsEnabled() const
Returns whether the layer contains diagrams which are enabled and should be drawn.
static void applyLayerNodeProperties(QgsLayerTreeLayer *nodeLayer, QList< QgsLayerTreeModelLegendNode * > &nodes)
update according to layer node's custom properties (order of items, user labels for items)
static QgsColorRampLegendNodeSettings * legendNodeColorRampSettings(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the custom legend color ramp settings for the legend node belonging to nodeLayer at the speci...
Default legend implementation for vector layers.
QgsRuleBasedLabeling::Rule * rootRule()
A child rule for QgsRuleBasedLabeling.
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.
Represents all mesh renderer settings.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsDefaultPointCloudLayerLegend(QgsPointCloudLayer *layer)
Creates an instance for the given point cloud layer.
The class is used as a container of context for various read/write operations on other objects.
static bool legendNodeColumnBreak(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns whether a forced column break should occur before the node.
Contains settings for how a map layer will be labeled.
Represents a map layer supporting display of point clouds.
static QgsMapLayerLegend * defaultMeshLegend(QgsMeshLayer *ml)
Create new legend implementation for mesh layer.
Implementation of legend node interface for displaying arbitrary label with icon.
A legend node which renders a color ramp.
virtual void setUserPatchSize(QSizeF size)
Sets the user (overridden) size for the legend node.
virtual QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer)
Creates a set of legend nodes representing the renderer.
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer.
double maximumValue() const
Returns the minimum value for the raster shader.
const QgsRuleBasedLabeling::Rule * findRuleByKey(const QString &key) const
Try to find a rule given its unique key.
virtual QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Writes configuration to a DOM element, to be used later with readXml()
static QgsProject * instance()
Returns the QgsProject singleton instance.
static void setLegendNodeColumnBreak(QgsLayerTreeLayer *nodeLayer, int originalIndex, bool columnBreakBeforeNode)
Sets whether a forced column break should occur before the node.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
This class is a composition of two QSettings instances:
virtual QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer)=0
Returns list of legend nodes to be used for a particular layer tree layer node.
Abstract base class for all rendered symbols.
const QgsDiagramRenderer * diagramRenderer() const
Produces legend node with a marker symbol.
QgsPointCloudRenderer * renderer()
Returns the 2D renderer for the point cloud.
Default legend implementation for raster layers.
QgsMapLayerLegend * legend() const
Can be nullptr.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
Rule based labeling for a vector layer.
virtual QString legendClassificationAttribute() const
If supported by the renderer, return classification attribute for the use in legend.
Represents a patch shape for use in map legends.
Produces legend node for a labeling text symbol.
virtual bool supportsLegendGraphic() const
Returns whether the provider supplies a legend graphic.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
QgsMeshRendererSettings rendererSettings() const
Returns renderer settings.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
Container for all settings relating to text rendering.
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
QgsColorRamp * createColorRamp() const
Creates a gradient color ramp from shader settings.
static void setLegendNodeOrder(QgsLayerTreeLayer *nodeLayer, const QList< int > &order)
int activeScalarDatasetGroup() const
Returns the active scalar dataset group.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Returns legend symbology items if provided by renderer.
QgsMeshRendererScalarSettings scalarSettings(int groupIndex) const
Returns renderer settings.
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
QgsDefaultMeshLayerLegend(QgsMeshLayer *ml)
Creates an instance for the given mesh layer.
virtual QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
QgsColorRampShader colorRampShader() const
Returns color ramp shader function.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const
Writes settings to an XML element.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes configuration to a DOM element, to be used later with readXml()
virtual QStringList subProviders() const
Gets list of sub-providers within the layer's labeling.
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
const QgsAbstractVectorLayerLabeling * labeling() const
Access to const labeling configuration.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
static QgsMapLayerLegend * defaultVectorLegend(QgsVectorLayer *vl)
Create new legend implementation for vector layer.
Layer tree node points to a map layer.
void nameChanged()
Emitted when the name has been changed.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
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...
Implementation of legend node interface for displaying WMS legend entries.
static QgsSymbol * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
QgsDefaultRasterLayerLegend(QgsRasterLayer *rl)
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
QString legendPlaceholderImage() const
Returns path to the placeholder image or an empty string if a generated legend is shown.
Represents a raster layer.
static bool hasLegendNodeOrder(QgsLayerTreeLayer *nodeLayer)
QgsMapLayerLegend(QObject *parent=nullptr)
Constructor for QgsMapLayerLegend.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
virtual void setEmbeddedInParent(bool embedded)
void rendererChanged()
Signal emitted when renderer is changed.
bool useContinuousLegend() const
Returns true if a continuous gradient legend will be used.
static QString encodeSize(QSizeF size)
Encodes a QSizeF to a string.
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
double minimumValue() const
Returns the maximum value for the raster shader.
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
static QList< int > legendNodeOrder(QgsLayerTreeLayer *nodeLayer)
QStringList customProperties() const
Returns list of keys stored in custom properties.
static QgsMapLayerLegend * defaultRasterLegend(QgsRasterLayer *rl)
Create new legend implementation for raster layer.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom colormap.
Type colorRampType() const
Returns the color ramp type.
static void setLegendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QString &newLabel)
Represents a mesh renderer settings for scalar datasets.
Default legend implementation for mesh layers.
virtual void setUserLabel(const QString &userLabel)
static QgsLegendPatchShape legendNodePatchShape(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the legend patch shape for the legend node belonging to nodeLayer at the specified originalIn...
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 ...
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.
Represents a vector layer which manages a vector based data sets.
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.
void itemsChanged()
Emitted when existing items/nodes got invalid and should be replaced by new ones.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
QString description() const
A human readable description for this rule.
Implementation of legend node interface for displaying arbitrary raster image.
Abstract base class for 2d point cloud renderers.
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...
QList< QPair< QString, QColor > > QgsLegendColorList
static QString legendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex)
QgsVectorLayerFeatureCounter * countSymbolFeatures(bool storeSymbolFids=false)
Count features for symbols.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
QgsMeshDatasetGroupMetadata datasetGroupMetadata(const QgsMeshDatasetIndex &index) const
Returns the dataset groups metadata.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads settings from an XML element.
Implementation of legend node interface for displaying raster legend entries.
static QgsSymbol * legendNodeCustomSymbol(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the custom legend symbol for the legend node belonging to nodeLayer at the specified original...
Default legend implementation for point cloud layers.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
static bool hasLegendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex)
void removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file.
@ Interpolated
Interpolates the color between two class breaks linearly.
void writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
static QSizeF legendNodeSymbolSize(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the legend node symbol size for the legend node belonging to nodeLayer at the specified origi...
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
static QgsMapLayerLegend * defaultPointCloudLegend(QgsPointCloudLayer *layer)
Create new legend implementation for a point cloud layer.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
virtual QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer)
Creates a set of legend nodes representing the renderer.
static QSizeF decodeSize(const QString &string)
Decodes a QSizeF from a string.
QgsDefaultVectorLayerLegend(QgsVectorLayer *vl)
virtual QgsPalLayerSettings settings(const QString &providerId=QString()) const =0
Gets associated label settings.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read settings from a DOM element.
Settings for a color ramp legend node.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
bool labelsEnabled() const
Returns whether the layer contains labels which are enabled and should be drawn.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads configuration from a DOM element previously written by writeXml()
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
virtual void setColumnBreak(bool breakBeforeNode)
Sets whether a forced column break should occur before the node.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...