QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
68 const auto constOrder = order;
69 for (
int id : constOrder )
70 orderStr << QString::number(
id );
71 QString str = orderStr.isEmpty() ? QStringLiteral(
"empty" ) : orderStr.join( QLatin1Char(
',' ) );
80 int numNodes = lst.count();
89 QgsDebugMsg( QStringLiteral(
"Legend node order manipulation is invalid without existing legend" ) );
93 int numNodes = _originalLegendNodeCount( nodeLayer );
96 order.reserve( numNodes );
97 for (
int i = 0; i < numNodes; ++i )
104 QString orderStr = nodeLayer->
customProperty( QStringLiteral(
"legend/node-order" ) ).toString();
106 if ( orderStr.isEmpty() )
107 return _makeNodeOrder( nodeLayer );
109 if ( orderStr == QLatin1String(
"empty" ) )
112 int numNodes = _originalLegendNodeCount( nodeLayer );
115 const auto constSplit = orderStr.split(
',' );
116 for (
const QString &item : constSplit )
119 int id = item.toInt( &ok );
120 if ( !ok || id < 0 || id >= numNodes )
121 return _makeNodeOrder( nodeLayer );
131 return nodeLayer->
customProperties().contains( QStringLiteral(
"legend/node-order" ) );
136 nodeLayer->
setCustomProperty(
"legend/label-" + QString::number( originalIndex ), newLabel );
141 return nodeLayer->
customProperty(
"legend/label-" + QString::number( originalIndex ) ).toString();
146 return nodeLayer->
customProperties().contains(
"legend/label-" + QString::number( originalIndex ) );
151 QDomDocument patchDoc;
152 QDomElement patchElem = patchDoc.createElement( QStringLiteral(
"patch" ) );
154 patchDoc.appendChild( patchElem );
155 nodeLayer->
setCustomProperty(
"legend/patch-shape-" + QString::number( originalIndex ), patchDoc.toString() );
160 QString patchDef = nodeLayer->
customProperty(
"legend/patch-shape-" + QString::number( originalIndex ) ).toString();
161 if ( patchDef.isEmpty() )
164 QDomDocument doc( QStringLiteral(
"patch" ) );
165 doc.setContent( patchDef );
173 if ( size.isValid() )
181 const QString size = nodeLayer->
customProperty(
"legend/symbol-size-" + QString::number( originalIndex ) ).toString();
182 if ( size.isEmpty() )
196 doc.appendChild( elem );
197 nodeLayer->
setCustomProperty(
"legend/custom-symbol-" + QString::number( originalIndex ), doc.toString() );
205 const QString symbolDef = nodeLayer->
customProperty(
"legend/custom-symbol-" + QString::number( originalIndex ) ).toString();
206 if ( symbolDef.isEmpty() )
210 doc.setContent( symbolDef );
211 const QDomElement elem = doc.documentElement();
221 if ( columnBreakBeforeNode )
222 nodeLayer->
setCustomProperty(
"legend/column-break-" + QString::number( originalIndex ), QStringLiteral(
"1" ) );
229 return nodeLayer->
customProperty(
"legend/column-break-" + QString::number( originalIndex ) ).toInt();
236 const auto constNodes = nodes;
240 if ( !userLabel.isNull() )
246 symbolNode->setPatchShape( shape );
252 if ( userSize.isValid() )
268 QList<QgsLayerTreeModelLegendNode *> newOrder;
269 QSet<int> usedIndices;
270 const auto constOrder = order;
271 for (
int idx : constOrder )
273 if ( usedIndices.contains( idx ) )
275 QgsDebugMsg( QStringLiteral(
"invalid node order. ignoring." ) );
279 newOrder << nodes[idx];
284 for (
int i = 0; i < nodes.count(); ++i )
286 if ( !usedIndices.contains( i ) )
306 QList<QgsLayerTreeModelLegendNode *> nodes;
312 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toBool() )
324 if (
auto *lDataDefinedSizeLegendSettings = i.dataDefinedSizeLegendSettings() )
329 if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( i.ruleKey() ) )
331 legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( i.ruleKey() ) );
332 legendNode->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
338 if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() )
357 mTextOnSymbolEnabled =
false;
359 mTextOnSymbolContent.clear();
361 QDomElement tosElem = elem.firstChildElement( QStringLiteral(
"text-on-symbol" ) );
362 if ( !tosElem.isNull() )
364 mTextOnSymbolEnabled =
true;
365 QDomElement tosFormatElem = tosElem.firstChildElement( QStringLiteral(
"text-style" ) );
366 mTextOnSymbolTextFormat.
readXml( tosFormatElem, context );
367 QDomElement tosContentElem = tosElem.firstChildElement( QStringLiteral(
"content" ) );
368 QDomElement tosContentItemElem = tosContentElem.firstChildElement( QStringLiteral(
"item" ) );
369 while ( !tosContentItemElem.isNull() )
371 mTextOnSymbolContent.insert( tosContentItemElem.attribute( QStringLiteral(
"key" ) ), tosContentItemElem.attribute( QStringLiteral(
"value" ) ) );
372 tosContentItemElem = tosContentItemElem.nextSiblingElement( QStringLiteral(
"item" ) );
379 QDomElement elem = doc.createElement( QStringLiteral(
"legend" ) );
380 elem.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"default-vector" ) );
382 if ( mTextOnSymbolEnabled )
384 QDomElement tosElem = doc.createElement( QStringLiteral(
"text-on-symbol" ) );
385 QDomElement tosFormatElem = mTextOnSymbolTextFormat.
writeXml( doc, context );
386 tosElem.appendChild( tosFormatElem );
387 QDomElement tosContentElem = doc.createElement( QStringLiteral(
"content" ) );
388 for (
auto it = mTextOnSymbolContent.constBegin(); it != mTextOnSymbolContent.constEnd(); ++it )
390 QDomElement tosContentItemElem = doc.createElement( QStringLiteral(
"item" ) );
391 tosContentItemElem.setAttribute( QStringLiteral(
"key" ), it.key() );
392 tosContentItemElem.setAttribute( QStringLiteral(
"value" ), it.value() );
393 tosContentElem.appendChild( tosContentItemElem );
395 tosElem.appendChild( tosContentElem );
396 elem.appendChild( tosElem );
414 QList<QgsLayerTreeModelLegendNode *> nodes;
423 if ( rasterItemList.isEmpty() )
429 int max_count = 1000;
431 for ( QgsLegendColorList::const_iterator itemIt = rasterItemList.constBegin();
432 itemIt != rasterItemList.constEnd(); ++itemIt, ++count )
436 if ( count == max_count )
438 QString label = tr(
"following %1 items\nnot displayed" ).arg( rasterItemList.size() - max_count );
457 QList<QgsLayerTreeModelLegendNode *> nodes;
469 if ( indexScalar > -1 && indexVector > -1 && indexScalar != indexVector )
471 else if ( indexScalar > -1 )
473 else if ( indexVector > -1 )
483 if ( indexScalar > -1 )
488 for (
const QPair< QString, QColor > &item : qgis::as_const( items ) )
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)
Default legend implementation for vector layers.
Represents all mesh renderer settings.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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.
static QgsMapLayerLegend * defaultMeshLegend(QgsMeshLayer *ml)
Create new legend implementation for mesh layer.
Implementation of legend node interface for displaying arbitrary label with icon.
virtual void setUserPatchSize(QSizeF size)
Sets the user (overridden) size for the legend node.
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer.
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.
Default legend implementation for raster layers.
QgsMapLayerLegend * legend() const
Can be nullptr.
QgsLegendColorList legendSymbologyItems() const
Returns a list with classification items (Text and color)
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.
virtual bool supportsLegendGraphic() const
Returns whether the provider supplies a legend graphic.
QgsMeshRendererSettings rendererSettings() const
Returns renderer settings.
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...
QgsMeshDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
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.
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.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes configuration to a DOM element, to be used later with readXml()
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
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.
Base class for providing data for QgsMeshLayer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
static void setLegendNodeCustomSymbol(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsSymbol *symbol)
Sets a custom legend symbol size for the legend node belonging to nodeLayer at the specified original...
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.
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.
static QString encodeSize(QSizeF size)
Encodes a QSizeF to a string.
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.
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...
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.
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.
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...
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.
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 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.
virtual QgsMeshDatasetGroupMetadata datasetGroupMetadata(int groupIndex) const =0
Returns dataset group metadata.
QgsDefaultVectorLayerLegend(QgsVectorLayer *vl)
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read settings from a DOM element.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads configuration from a DOM element previously written by writeXml()
QgsFeatureRenderer * renderer()
Returns renderer.
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...