17#include "moc_qgsmaplayerlegend.cpp"
81 const auto constOrder = order;
82 for (
const int id : constOrder )
83 orderStr << QString::number(
id );
84 const QString
str = orderStr.isEmpty() ? QStringLiteral(
"empty" ) : orderStr.join( QLatin1Char(
',' ) );
93 const int numNodes = lst.count();
102 QgsDebugError( QStringLiteral(
"Legend node order manipulation is invalid without existing legend" ) );
106 const int numNodes = _originalLegendNodeCount( nodeLayer );
109 order.reserve( numNodes );
110 for (
int i = 0; i < numNodes; ++i )
117 const QString orderStr = nodeLayer->
customProperty( QStringLiteral(
"legend/node-order" ) ).toString();
119 if ( orderStr.isEmpty() )
120 return _makeNodeOrder( nodeLayer );
122 if ( orderStr == QLatin1String(
"empty" ) )
125 const int numNodes = _originalLegendNodeCount( nodeLayer );
128 const auto constSplit = orderStr.split(
',' );
129 for (
const QString &item : constSplit )
132 const int id = item.toInt( &ok );
133 if ( !ok || id < 0 || id >= numNodes )
134 return _makeNodeOrder( nodeLayer );
144 return nodeLayer->
customProperties().contains( QStringLiteral(
"legend/node-order" ) );
149 nodeLayer->
setCustomProperty(
"legend/label-" + QString::number( originalIndex ), newLabel );
154 return nodeLayer->
customProperty(
"legend/label-" + QString::number( originalIndex ) ).toString();
159 return nodeLayer->
customProperties().contains(
"legend/label-" + QString::number( originalIndex ) );
164 QDomDocument patchDoc;
165 QDomElement patchElem = patchDoc.createElement( QStringLiteral(
"patch" ) );
167 patchDoc.appendChild( patchElem );
168 nodeLayer->
setCustomProperty(
"legend/patch-shape-" + QString::number( originalIndex ), patchDoc.toString() );
173 const QString patchDef = nodeLayer->
customProperty(
"legend/patch-shape-" + QString::number( originalIndex ) ).toString();
174 if ( patchDef.isEmpty() )
177 QDomDocument doc( QStringLiteral(
"patch" ) );
178 doc.setContent( patchDef );
186 if ( size.isValid() )
194 const QString size = nodeLayer->
customProperty(
"legend/symbol-size-" + QString::number( originalIndex ) ).toString();
195 if ( size.isEmpty() )
209 doc.appendChild( elem );
210 nodeLayer->
setCustomProperty(
"legend/custom-symbol-" + QString::number( originalIndex ), doc.toString() );
218 const QString symbolDef = nodeLayer->
customProperty(
"legend/custom-symbol-" + QString::number( originalIndex ) ).toString();
219 if ( symbolDef.isEmpty() )
223 doc.setContent( symbolDef );
224 const QDomElement elem = doc.documentElement();
239 QDomElement elem = doc.createElement( QStringLiteral(
"rampSettings" ) );
240 settings->
writeXml( doc, elem, rwContext );
241 doc.appendChild( elem );
242 nodeLayer->
setCustomProperty(
"legend/custom-ramp-settings-" + QString::number( originalIndex ), doc.toString() );
245 nodeLayer->
removeCustomProperty(
"legend/custom-ramp-settings-" + QString::number( originalIndex ) );
250 const QString settingsDef = nodeLayer->
customProperty(
"legend/custom-ramp-settings-" + QString::number( originalIndex ) ).toString();
251 if ( settingsDef.isEmpty() )
255 doc.setContent( settingsDef );
256 const QDomElement elem = doc.documentElement();
262 settings.
readXml( elem, rwContext );
268 if ( columnBreakBeforeNode )
269 nodeLayer->
setCustomProperty(
"legend/column-break-" + QString::number( originalIndex ), QStringLiteral(
"1" ) );
276 return nodeLayer->
customProperty(
"legend/column-break-" + QString::number( originalIndex ) ).toInt();
283 const auto constNodes = nodes;
287 if ( !userLabel.isNull() )
293 symbolNode->setPatchShape( shape );
302 colorRampNode->setSettings( *settings );
307 if ( userSize.isValid() )
323 QList<QgsLayerTreeModelLegendNode *> newOrder;
324 QSet<int> usedIndices;
325 const auto constOrder = order;
326 for (
const int idx : constOrder )
328 if ( usedIndices.contains( idx ) )
330 QgsDebugError( QStringLiteral(
"invalid node order. ignoring." ) );
334 newOrder << nodes[idx];
339 for (
int i = 0; i < nodes.count(); ++i )
341 if ( !usedIndices.contains( i ) )
362 QList<QgsLayerTreeModelLegendNode *> nodes;
367 if ( !placeholderImage.isEmpty() )
379 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toBool() )
388 const QList<QgsLayerTreeModelLegendNode *> rendererNodes = r->
createLegendNodes( nodeLayer );
394 if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( ruleKey ) )
396 legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( ruleKey ) );
397 legendNode->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
403 if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() && qobject_cast< QgsSymbolLegendNode * >( nodes[0] ) )
404 nodes[0]->setEmbeddedInParent(
true );
421 for (
int i = 0; i < pList.size(); ++i )
426 if ( ruleBasedLabeling && ruleBasedLabeling->
rootRule() )
436 nodes.append( node );
446 mTextOnSymbolEnabled =
false;
448 mTextOnSymbolContent.clear();
450 mShowLabelLegend = elem.attribute( QStringLiteral(
"showLabelLegend" ), QStringLiteral(
"0" ) ).compare( QStringLiteral(
"1" ), Qt::CaseInsensitive ) == 0;
452 const QDomElement tosElem = elem.firstChildElement( QStringLiteral(
"text-on-symbol" ) );
453 if ( !tosElem.isNull() )
455 mTextOnSymbolEnabled =
true;
456 const QDomElement tosFormatElem = tosElem.firstChildElement( QStringLiteral(
"text-style" ) );
457 mTextOnSymbolTextFormat.
readXml( tosFormatElem, context );
458 const QDomElement tosContentElem = tosElem.firstChildElement( QStringLiteral(
"content" ) );
459 QDomElement tosContentItemElem = tosContentElem.firstChildElement( QStringLiteral(
"item" ) );
460 while ( !tosContentItemElem.isNull() )
462 mTextOnSymbolContent.insert( tosContentItemElem.attribute( QStringLiteral(
"key" ) ), tosContentItemElem.attribute( QStringLiteral(
"value" ) ) );
463 tosContentItemElem = tosContentItemElem.nextSiblingElement( QStringLiteral(
"item" ) );
470 QDomElement elem = doc.createElement( QStringLiteral(
"legend" ) );
471 elem.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"default-vector" ) );
472 elem.setAttribute( QStringLiteral(
"showLabelLegend" ), mShowLabelLegend );
474 if ( mTextOnSymbolEnabled )
476 QDomElement tosElem = doc.createElement( QStringLiteral(
"text-on-symbol" ) );
477 const QDomElement tosFormatElem = mTextOnSymbolTextFormat.
writeXml( doc, context );
478 tosElem.appendChild( tosFormatElem );
479 QDomElement tosContentElem = doc.createElement( QStringLiteral(
"content" ) );
480 for (
auto it = mTextOnSymbolContent.constBegin(); it != mTextOnSymbolContent.constEnd(); ++it )
482 QDomElement tosContentItemElem = doc.createElement( QStringLiteral(
"item" ) );
483 tosContentItemElem.setAttribute( QStringLiteral(
"key" ), it.key() );
484 tosContentItemElem.setAttribute( QStringLiteral(
"value" ), it.value() );
485 tosContentElem.appendChild( tosContentItemElem );
487 tosElem.appendChild( tosContentElem );
488 elem.appendChild( tosElem );
506 QList<QgsLayerTreeModelLegendNode *> nodes;
515 if ( !placeholderImage.isEmpty() )
536 QList<QgsLayerTreeModelLegendNode *> nodes;
544 if ( indexScalar > -1 && indexVector > -1 && indexScalar != indexVector )
546 else if ( indexScalar > -1 )
548 else if ( indexVector > -1 )
556 if ( indexScalar > -1 )
558 const QString scalarNameKey = QStringLiteral(
"scalarName" );
560 QIcon(),
nullptr, scalarNameKey );
576 QStringLiteral(
"scalarLegend" ),
588 for (
const QPair< QString, QColor > &item : items )
591 QStringLiteral(
"scalarLegend" ) + QUuid::createUuid().toString(),
600 if ( indexVector > -1 )
603 const QString vectorNameKey = QStringLiteral(
"vectorName" );
608 const QColor arrowColor = settings.
color();
611 vectorIcon,
nullptr, vectorNameKey );
618 vectorIcon,
nullptr, vectorNameKey );
633 QStringLiteral(
"vectorLegend" ),
645 for (
const QPair< QString, QColor > &item : items )
648 QStringLiteral(
"vectorLegend" ) + QUuid::createUuid().toString(),
677 return QList<QgsLayerTreeModelLegendNode *>();
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
@ Linear
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
virtual QStringList subProviders() const
Gets list of sub-providers within the layer's labeling.
virtual QgsPalLayerSettings settings(const QString &providerId=QString()) const =0
Gets associated label settings.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
Settings for a color ramp legend node.
bool useContinuousLegend() const
Returns true if a continuous gradient legend will be used.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads settings from an XML element.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const
Writes settings to an XML element.
A legend node which renders a color ramp.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Returns legend symbology items if provided by renderer.
Qgis::ShaderInterpolationMethod colorRampType() const
Returns the color ramp interpolation method.
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom color map.
QgsColorRamp * createColorRamp() const
Creates a gradient color ramp from shader settings.
Default legend implementation for mesh layers.
QgsDefaultMeshLayerLegend(QgsMeshLayer *ml)
Creates an instance for the given mesh layer.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
Default legend implementation for point cloud layers.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
QgsDefaultPointCloudLayerLegend(QgsPointCloudLayer *layer)
Creates an instance for the given point cloud layer.
Default legend implementation for raster layers.
QgsDefaultRasterLayerLegend(QgsRasterLayer *rl)
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
Default legend implementation for vector layers.
QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Returns list of legend nodes to be used for a particular layer tree layer node.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes configuration to a DOM element, to be used later with readXml()
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads configuration from a DOM element previously written by writeXml()
QgsDefaultVectorLayerLegend(QgsVectorLayer *vl)
virtual QList< QgsLayerTreeModelLegendNode * > legendItems(QgsLayerTreeLayer *nodeLayer) const
Returns list of legend nodes for the diagram.
Abstract base class for all 2D vector feature renderers.
virtual QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer) const
Returns a list of legend nodes to be used for the legend for the renderer.
virtual QString legendClassificationAttribute() const
If supported by the renderer, return classification attribute for the use in legend.
QImage pathAsImage(const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking=false, double targetDpi=96, int frameNumber=-1, bool *isMissing=nullptr)
Returns the specified path rendered as an image.
Implementation of legend node interface for displaying arbitrary raster image.
@ ColorRamp
Render with a color ramp.
@ SingleColor
Render with a single color.
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
@ RuleKey
Rule key of the node (QString)
virtual void setColumnBreak(bool breakBeforeNode)
Sets whether a forced column break should occur before the node.
virtual void setUserPatchSize(QSizeF size)
Sets the user (overridden) size for the legend node.
virtual void setUserLabel(const QString &userLabel)
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
void removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file.
QStringList customProperties() const
Returns list of keys stored in custom properties.
Represents a patch shape for use in map legends.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Read settings from a DOM element.
void writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
static void setLegendNodeOrder(QgsLayerTreeLayer *nodeLayer, const QList< int > &order)
static void setLegendNodeColorRampSettings(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsColorRampLegendNodeSettings *settings)
Sets a custom legend color ramp settings for the legend node belonging to nodeLayer at the specified ...
static void setLegendNodeCustomSymbol(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsSymbol *symbol)
Sets a custom legend symbol for the legend node belonging to nodeLayer at the specified originalIndex...
static QString legendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex)
static QSizeF legendNodeSymbolSize(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the legend node symbol size for the legend node belonging to nodeLayer at the specified origi...
static bool hasLegendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex)
static bool legendNodeColumnBreak(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns whether a forced column break should occur before the node.
static bool hasLegendNodeOrder(QgsLayerTreeLayer *nodeLayer)
static void setLegendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QString &newLabel)
static void setLegendNodeSymbolSize(QgsLayerTreeLayer *nodeLayer, int originalIndex, QSizeF size)
Sets the legend symbol size for the legend node belonging to nodeLayer at the specified originalIndex...
static QgsColorRampLegendNodeSettings * legendNodeColorRampSettings(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the custom legend color ramp settings for the legend node belonging to nodeLayer at the speci...
static void setLegendNodeColumnBreak(QgsLayerTreeLayer *nodeLayer, int originalIndex, bool columnBreakBeforeNode)
Sets whether a forced column break should occur before the node.
static void setLegendNodePatchShape(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsLegendPatchShape &shape)
Sets the legend patch shape for the legend node belonging to nodeLayer at the specified originalIndex...
static QgsSymbol * legendNodeCustomSymbol(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the custom legend symbol for the legend node belonging to nodeLayer at the specified original...
static QList< int > legendNodeOrder(QgsLayerTreeLayer *nodeLayer)
static void applyLayerNodeProperties(QgsLayerTreeLayer *nodeLayer, QList< QgsLayerTreeModelLegendNode * > &nodes)
update according to layer node's custom properties (order of items, user labels for items)
static QgsLegendPatchShape legendNodePatchShape(QgsLayerTreeLayer *nodeLayer, int originalIndex)
Returns the legend patch shape for the legend node belonging to nodeLayer at the specified originalIn...
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads configuration from a DOM element previously written by writeXml()
static QgsMapLayerLegend * defaultMeshLegend(QgsMeshLayer *ml)
Create new legend implementation for mesh layer.
static QgsMapLayerLegend * defaultPointCloudLegend(QgsPointCloudLayer *layer)
Create new legend implementation for a point cloud layer.
QgsMapLayerLegend(QObject *parent=nullptr)
Constructor for QgsMapLayerLegend.
virtual QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Writes configuration to a DOM element, to be used later with readXml()
static QgsMapLayerLegend * defaultRasterLegend(QgsRasterLayer *rl)
Create new legend implementation for raster layer.
static QgsMapLayerLegend * defaultVectorLegend(QgsVectorLayer *vl)
Create new legend implementation for vector layer.
virtual QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer)=0
Returns list of legend nodes to be used for a particular layer tree layer node.
void itemsChanged()
Emitted when existing items/nodes got invalid and should be replaced by new ones.
QgsMapLayerLegend * legend() const
Can be nullptr.
QString legendPlaceholderImage() const
Returns path to the placeholder image or an empty string if a generated legend is shown.
void rendererChanged()
Signal emitted when renderer is changed.
void nameChanged()
Emitted when the name has been changed.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
QgsMeshRendererSettings rendererSettings() const
Returns renderer settings.
QgsMeshDatasetGroupMetadata datasetGroupMetadata(const QgsMeshDatasetIndex &index) const
Returns the dataset groups metadata.
Represents a mesh renderer settings for scalar datasets.
QgsColorRampShader colorRampShader() const
Returns color ramp shader function.
Represents all mesh renderer settings.
QgsMeshRendererScalarSettings scalarSettings(int groupIndex) const
Returns renderer settings.
int activeVectorDatasetGroup() const
Returns the active vector dataset group.
int activeScalarDatasetGroup() const
Returns the active scalar dataset group.
QgsMeshRendererVectorSettings vectorSettings(int groupIndex) const
Returns renderer settings.
Represents a renderer settings for vector datasets.
QColor color() const
Returns color used for drawing arrows.
QgsColorRampShader colorRampShader() const
Sets the color ramp shader used to render vector datasets.
QgsInterpolatedLineColor::ColoringMethod coloringMethod() const
Returns the coloring method used to render vector datasets.
Contains settings for how a map layer will be labeled.
Represents a map layer supporting display of point clouds.
QgsPointCloudRenderer * renderer()
Returns the 2D renderer for the point cloud.
Abstract base class for 2d point cloud renderers.
virtual QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer)
Creates a set of legend nodes representing the renderer.
static QgsProject * instance()
Returns the QgsProject singleton instance.
virtual bool supportsLegendGraphic() const
Returns whether the provider supplies a legend graphic.
Represents a raster layer.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
virtual QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer)
Creates a set of legend nodes representing the renderer.
double maximumValue() const
Returns the minimum value for the raster shader.
double minimumValue() const
Returns the maximum value for the raster shader.
Implementation of legend node interface for displaying raster legend entries.
The class is used as a container of context for various read/write operations on other objects.
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
A child rule for QgsRuleBasedLabeling.
const QgsRuleBasedLabeling::Rule * findRuleByKey(const QString &key) const
Try to find a rule given its unique key.
QString description() const
A human readable description for this rule.
Rule based labeling for a vector layer.
QgsRuleBasedLabeling::Rule * rootRule()
This class is a composition of two QSettings instances:
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.
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 QgsDebugError(str)
QList< QPair< QString, QColor > > QgsLegendColorList