QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
36 #include <QDomDocument>
37 #include <QDomElement>
43 , mLegendModel( new
QgsLegendModel( layout->project()->layerTreeRoot(), this ) )
45 #if 0 //no longer required?
46 connect( &
layout->atlasComposition(), &QgsAtlasComposition::renderEnded,
this, &QgsLayoutItemLegend::onAtlasEnded );
49 mTitle = mSettings.
title();
89 if ( mFilterAskedForUpdate )
91 mFilterAskedForUpdate =
false;
92 doUpdateFilterByMap();
95 int dpi = painter->device()->logicalDpiX();
96 double dotsPerMM = dpi / 25.4;
114 QSizeF mapSizePixels = QSizeF( mMap->rect().width() * dotsPerMM, mMap->rect().height() * dotsPerMM );
124 mInitialMapScaleCalculated =
true;
127 legendRenderer.
setLegendSize( mForceResize && mSizeToContents ? QSize() : rect().size() );
130 if ( mSizeToContents )
133 QSizeF size = legendRenderer.
minimumSize( &context );
136 mForceResize =
false;
142 else if ( size.height() > rect().height() || size.width() > rect().width() )
145 QSizeF targetSize = rect().size();
146 if ( size.height() > targetSize.height() )
147 targetSize.setHeight( size.height() );
148 if ( size.width() > targetSize.width() )
149 targetSize.setWidth( size.width() );
162 if ( !mMapUuid.isEmpty() )
164 setLinkedMap( qobject_cast< QgsLayoutItemMap * >(
mLayout->itemByUuid( mMapUuid,
true ) ) );
171 clearLegendCachedData();
183 painter->setPen( QPen( QColor( 0, 0, 0 ) ) );
185 if ( !mSizeToContents )
188 QRectF thisPaintRect = QRectF( 0, 0, rect().width(), rect().height() );
189 painter->setClipRect( thisPaintRect );
210 if ( !mSizeToContents )
213 if ( !mInitialMapScaleCalculated )
224 QSizeF size = legendRenderer.
minimumSize( &context );
225 QgsDebugMsg( QStringLiteral(
"width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
226 if ( size.isValid() )
236 mSizeToContents = enabled;
241 return mSizeToContents;
244 void QgsLayoutItemLegend::setCustomLayerTree(
QgsLayerTree *rootGroup )
246 mLegendModel->setRootGroup( rootGroup ? rootGroup : (
mLayout ?
mLayout->project()->layerTreeRoot() :
nullptr ) );
248 mCustomLayerTree.reset( rootGroup );
257 setCustomLayerTree( autoUpdate ?
nullptr :
mLayout->project()->layerTreeRoot()->clone() );
262 void QgsLayoutItemLegend::nodeCustomPropertyChanged(
QgsLayerTreeNode *,
const QString &key )
264 if ( key == QLatin1String(
"cached_name" ) )
277 return !mCustomLayerTree;
282 mLegendFilterByMap = enabled;
291 if (
mLayout &&
id().isEmpty() )
294 mLayout->itemsModel()->updateItemDisplayName(
this );
314 return mSettings.
rstyle( s );
319 return mSettings.
style( s );
518 legendElem.setAttribute( QStringLiteral(
"title" ), mTitle );
519 legendElem.setAttribute( QStringLiteral(
"titleAlignment" ), QString::number(
static_cast< int >( mSettings.
titleAlignment() ) ) );
520 legendElem.setAttribute( QStringLiteral(
"columnCount" ), QString::number( mColumnCount ) );
521 legendElem.setAttribute( QStringLiteral(
"splitLayer" ), QString::number( mSettings.
splitLayer() ) );
522 legendElem.setAttribute( QStringLiteral(
"equalColumnWidth" ), QString::number( mSettings.
equalColumnWidth() ) );
524 legendElem.setAttribute( QStringLiteral(
"boxSpace" ), QString::number( mSettings.
boxSpace() ) );
525 legendElem.setAttribute( QStringLiteral(
"columnSpace" ), QString::number( mSettings.
columnSpace() ) );
527 legendElem.setAttribute( QStringLiteral(
"symbolWidth" ), QString::number( mSettings.
symbolSize().width() ) );
528 legendElem.setAttribute( QStringLiteral(
"symbolHeight" ), QString::number( mSettings.
symbolSize().height() ) );
530 legendElem.setAttribute( QStringLiteral(
"symbolAlignment" ), mSettings.
symbolAlignment() );
532 legendElem.setAttribute( QStringLiteral(
"symbolAlignment" ), mSettings.
symbolAlignment() );
533 legendElem.setAttribute( QStringLiteral(
"lineSpacing" ), QString::number( mSettings.
lineSpacing() ) );
535 legendElem.setAttribute( QStringLiteral(
"rasterBorder" ), mSettings.
drawRasterStroke() );
537 legendElem.setAttribute( QStringLiteral(
"rasterBorderWidth" ), QString::number( mSettings.
rasterStrokeWidth() ) );
539 legendElem.setAttribute( QStringLiteral(
"wmsLegendWidth" ), QString::number( mSettings.
wmsLegendSize().width() ) );
540 legendElem.setAttribute( QStringLiteral(
"wmsLegendHeight" ), QString::number( mSettings.
wmsLegendSize().height() ) );
541 legendElem.setAttribute( QStringLiteral(
"wrapChar" ), mSettings.
wrapChar() );
542 legendElem.setAttribute( QStringLiteral(
"fontColor" ), mSettings.
fontColor().name() );
544 legendElem.setAttribute( QStringLiteral(
"resizeToContents" ), mSizeToContents );
548 legendElem.setAttribute( QStringLiteral(
"map_uuid" ), mMap->
uuid() );
551 QDomElement legendStyles = doc.createElement( QStringLiteral(
"styles" ) );
552 legendElem.appendChild( legendStyles );
560 if ( mCustomLayerTree )
563 mCustomLayerTree->writeXml( legendElem, context );
566 if ( mLegendFilterByMap )
568 legendElem.setAttribute( QStringLiteral(
"legendFilterByMap" ), QStringLiteral(
"1" ) );
570 legendElem.setAttribute( QStringLiteral(
"legendFilterByAtlas" ), mFilterOutAtlas ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
578 mTitle = itemElem.attribute( QStringLiteral(
"title" ) );
580 if ( !itemElem.attribute( QStringLiteral(
"titleAlignment" ) ).isEmpty() )
582 mSettings.
setTitleAlignment(
static_cast< Qt::AlignmentFlag
>( itemElem.attribute( QStringLiteral(
"titleAlignment" ) ).toInt() ) );
584 int colCount = itemElem.attribute( QStringLiteral(
"columnCount" ), QStringLiteral(
"1" ) ).toInt();
585 if ( colCount < 1 ) colCount = 1;
586 mColumnCount = colCount;
588 mSettings.
setSplitLayer( itemElem.attribute( QStringLiteral(
"splitLayer" ), QStringLiteral(
"0" ) ).toInt() == 1 );
589 mSettings.
setEqualColumnWidth( itemElem.attribute( QStringLiteral(
"equalColumnWidth" ), QStringLiteral(
"0" ) ).toInt() == 1 );
591 QDomNodeList stylesNodeList = itemElem.elementsByTagName( QStringLiteral(
"styles" ) );
592 if ( !stylesNodeList.isEmpty() )
594 QDomNode stylesNode = stylesNodeList.at( 0 );
595 for (
int i = 0; i < stylesNode.childNodes().size(); i++ )
597 QDomElement styleElem = stylesNode.childNodes().at( i ).toElement();
600 QString name = styleElem.attribute( QStringLiteral(
"name" ) );
614 fontClr.setNamedColor( itemElem.attribute( QStringLiteral(
"fontColor" ), QStringLiteral(
"#000000" ) ) );
618 mSettings.
setBoxSpace( itemElem.attribute( QStringLiteral(
"boxSpace" ), QStringLiteral(
"2.0" ) ).toDouble() );
619 mSettings.
setColumnSpace( itemElem.attribute( QStringLiteral(
"columnSpace" ), QStringLiteral(
"2.0" ) ).toDouble() );
621 mSettings.
setSymbolSize( QSizeF( itemElem.attribute( QStringLiteral(
"symbolWidth" ), QStringLiteral(
"7.0" ) ).toDouble(), itemElem.attribute( QStringLiteral(
"symbolHeight" ), QStringLiteral(
"14.0" ) ).toDouble() ) );
622 mSettings.
setSymbolAlignment(
static_cast< Qt::AlignmentFlag
>( itemElem.attribute( QStringLiteral(
"symbolAlignment" ), QString::number( Qt::AlignLeft ) ).toInt() ) );
624 mSettings.
setWmsLegendSize( QSizeF( itemElem.attribute( QStringLiteral(
"wmsLegendWidth" ), QStringLiteral(
"50" ) ).toDouble(), itemElem.attribute( QStringLiteral(
"wmsLegendHeight" ), QStringLiteral(
"25" ) ).toDouble() ) );
625 mSettings.
setLineSpacing( itemElem.attribute( QStringLiteral(
"lineSpacing" ), QStringLiteral(
"1.0" ) ).toDouble() );
627 mSettings.
setDrawRasterStroke( itemElem.attribute( QStringLiteral(
"rasterBorder" ), QStringLiteral(
"1" ) ) != QLatin1String(
"0" ) );
629 mSettings.
setRasterStrokeWidth( itemElem.attribute( QStringLiteral(
"rasterBorderWidth" ), QStringLiteral(
"0" ) ).toDouble() );
631 mSettings.
setWrapChar( itemElem.attribute( QStringLiteral(
"wrapChar" ) ) );
633 mSizeToContents = itemElem.attribute( QStringLiteral(
"resizeToContents" ), QStringLiteral(
"1" ) ) != QLatin1String(
"0" );
636 mLegendFilterByMap = itemElem.attribute( QStringLiteral(
"legendFilterByMap" ), QStringLiteral(
"0" ) ).toInt();
639 if ( !itemElem.attribute( QStringLiteral(
"map_uuid" ) ).isEmpty() )
641 mMapUuid = itemElem.attribute( QStringLiteral(
"map_uuid" ) );
644 setupMapConnections( mMap,
false );
647 mFilterOutAtlas = itemElem.attribute( QStringLiteral(
"legendFilterByAtlas" ), QStringLiteral(
"0" ) ).toInt();
650 QDomElement layerTreeElem = itemElem.firstChildElement( QStringLiteral(
"layer-tree" ) );
651 if ( layerTreeElem.isNull() )
652 layerTreeElem = itemElem.firstChildElement( QStringLiteral(
"layer-tree-group" ) );
654 if ( !layerTreeElem.isNull() )
658 tree->resolveReferences(
mLayout->project(),
true );
659 setCustomLayerTree( tree.release() );
662 setCustomLayerTree(
nullptr );
669 if ( !
id().isEmpty() )
675 QString text = mSettings.
title();
676 if ( text.isEmpty() )
678 return tr(
"<Legend>" );
680 if ( text.length() > 25 )
682 return tr(
"%1…" ).arg( text.left( 25 ) );
691 void QgsLayoutItemLegend::setupMapConnections(
QgsLayoutItemMap *map,
bool connectSlots )
698 disconnect( map, &QObject::destroyed,
this, &QgsLayoutItemLegend::invalidateCurrentMap );
707 connect( map, &QObject::destroyed,
this, &QgsLayoutItemLegend::invalidateCurrentMap );
720 setupMapConnections( mMap,
false );
727 setupMapConnections( mMap,
true );
734 void QgsLayoutItemLegend::invalidateCurrentMap()
743 bool forceUpdate =
false;
759 if ( ok && cols >= 0 )
775 void QgsLayoutItemLegend::updateFilterByMapAndRedraw()
780 void QgsLayoutItemLegend::setModelStyleOverrides(
const QMap<QString, QString> &overrides )
782 mLegendModel->setLayerStyleOverrides( overrides );
783 const QList< QgsLayerTreeLayer * > layers = mLegendModel->rootGroup()->findLayers();
785 mLegendModel->refreshLayerLegend( nodeLayer );
789 void QgsLayoutItemLegend::clearLegendCachedData()
794 mLegendModel->clearCachedData( node );
798 const QList< QgsLayerTreeNode * > children = group->
children();
801 clearNodeCache( child );
806 clearNodeCache( mLegendModel->rootGroup() );
809 void QgsLayoutItemLegend::mapLayerStyleOverridesChanged()
815 if ( mLegendFilterByMap )
831 void QgsLayoutItemLegend::mapThemeChanged(
const QString &theme )
833 if ( mThemeName == theme )
839 if ( mLegendFilterByMap )
847 if ( mThemeName.isEmpty() )
849 setModelStyleOverrides( QMap<QString, QString>() );
854 const QMap<QString, QString> overrides =
mLayout->project()->mapThemeCollection()->mapThemeStyleOverrides( mThemeName );
855 setModelStyleOverrides( overrides );
869 mFilterAskedForUpdate =
true;
875 void QgsLayoutItemLegend::doUpdateFilterByMap()
879 if ( !mThemeName.isEmpty() )
882 const QMap<QString, QString> overrides =
mLayout->project()->mapThemeCollection()->mapThemeStyleOverrides( mThemeName );
883 mLegendModel->setLayerStyleOverrides( overrides );
891 mLegendModel->setLayerStyleOverrides( QMap<QString, QString>() );
896 if ( mMap && ( mLegendFilterByMap || filterByExpression || mInAtlas ) )
898 double dpi =
mLayout->renderContext().dpi();
902 QSizeF size( requestRectangle.
width(), requestRectangle.
height() );
910 filterPolygon =
mLayout->reportContext().currentGeometry( mMap->
crs() );
912 mLegendModel->setLegendFilter( &ms, mInAtlas || mLegendFilterByMap, filterPolygon,
true );
915 mLegendModel->setLegendFilterByMap(
nullptr );
917 clearLegendCachedData();
928 mFilterOutAtlas = doFilter;
933 return mFilterOutAtlas;
936 void QgsLayoutItemLegend::onAtlasFeature()
938 if ( !
mLayout->reportContext().feature().isValid() )
940 mInAtlas = mFilterOutAtlas;
944 void QgsLayoutItemLegend::onAtlasEnded()
984 const QList<QgsLayerTreeNode *> childNodes = group->
children();
990 if ( !visit( nodeGroup ) )
1002 const QList<QgsLayerTreeModelLegendNode *> legendNodes = mLegendModel->layerLegendNodes( nodeLayer );
1007 if ( !symbolNode->patchShape().isNull() )
1019 return visit( mLegendModel->rootGroup( ) );
1030 , mLayoutLegend( layout )
1038 , mLayoutLegend( layout )
1050 if ( nodeLayer && ( role == Qt::DisplayRole || role == Qt::EditRole ) )
1052 QString name = node->
customProperty( QStringLiteral(
"cached_name" ) ).toString();
1053 if ( !name.isEmpty() )
1059 name = nodeLayer->
customProperty( QStringLiteral(
"legend/title-label" ) ).toString();
1060 if ( name.isEmpty() )
1061 name = nodeLayer->
name();
1062 if ( name.isEmpty() )
1063 name = node->
customProperty( QStringLiteral(
"legend/title-label" ) ).toString();
1064 if ( name.isEmpty() )
1065 name = node->
name();
1066 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toInt() )
1070 name += QStringLiteral(
" [%1]" ).arg( vlayer->
featureCount() );
1076 const bool evaluate = ( vlayer && !nodeLayer->
labelExpression().isEmpty() ) || name.contains(
"[%" );
1087 if ( mLayoutLegend )
1090 const QList<QgsLayerTreeModelLegendNode *> legendnodes =
layerLegendNodes( nodeLayer,
false );
1091 if ( legendnodes.count() > 1 )
1096 symnode->evaluateLabel( expressionContext );
1099 else if (
QgsSymbolLegendNode *symnode = qobject_cast<QgsSymbolLegendNode *>( legendnodes.first() ) )
1100 name = symnode->evaluateLabel( expressionContext );
1119 if ( !
mLegend.contains( nodeLayer ) )
1120 return QList<QgsLayerTreeModelLegendNode *>();
1123 QList<QgsLayerTreeModelLegendNode *> lst(
data.activeNodes );
1124 if ( !skipNodeEmbeddedInParent &&
data.embeddedNodeInParent )
1125 lst.prepend(
data.embeddedNodeInParent );
1134 void QgsLegendModel::forceRefresh()
@ FlagUseAdvancedEffects
Enable advanced effects such as blend modes.
void setSymbolAlignment(Qt::AlignmentFlag alignment)
Sets the alignment for placement of legend symbols.
void setEqualColumnWidth(bool s)
Sets whether all columns should have equal widths.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QString labelExpression() const
Returns the expression member of the LayerTreeNode.
QString id() const
Returns the item's ID name.
const QgsLayout * layout() const
Returns the layout the object is attached to.
@ Symbol
Symbol icon (excluding label)
Qt::AlignmentFlag symbolAlignment() const
Returns the alignment for placement of legend symbols.
QString title() const
Returns the title for the legend, which will be rendered above all legend items.
Style
Component of legends which can be styled.
static QString encodeColor(const QColor &color)
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
QgsLegendPatchShape patchShape() const
Returns the symbol patch shape to use when rendering the legend node symbol.
void setLegendSize(QSizeF s)
Sets the preferred resulting legend size.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QColor rasterStrokeColor() const
Returns the stroke color for the stroke drawn around raster symbol items.
int valueAsInt(int key, const QgsExpressionContext &context, int defaultValue=0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as an integer.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
double rasterStrokeWidth() const
Returns the stroke width (in layout units) for the stroke drawn around raster symbol items.
QMap< QString, QString > layerStyleOverrides() const
Returns stored overrides of styles for layers.
QIcon icon() const override
Returns the item's icon.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
void setSymbolSize(QSizeF s)
Sets the default symbol size (in millimeters) used for legend items.
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
Q_DECL_DEPRECATED void drawLegend(QPainter *painter)
Draws the legend with given painter.
void setStyleMargin(QgsLegendStyle::Style component, double margin)
Set the margin for a legend component.
QVariant data(const QModelIndex &index, int role) const override
void setTitle(const QString &title)
Sets the legend title.
void setSplitLayer(bool s)
Sets whether layer components can be split over multiple columns.
void setFont(const QFont &font)
Sets the font used for rendering this legend component.
void extentChanged()
Emitted when the map's extent changes.
@ AllowLegendChangeState
Allow check boxes for legend nodes (if supported by layer's legend)
QHash< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer's legend nodes.
void layerStyleOverridesChanged()
Emitted when layer style overrides are changed...
int type() const override
@ LegendColumnCount
Legend column count.
void setMargin(Side side, double margin)
Sets the margin (in mm) for the specified side of the component.
QgsMapSettings mapSettings(const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings) const
Returns map settings that will be used for drawing of the map.
QgsPropertyCollection mDataDefinedProperties
Q_DECL_DEPRECATED void setMmPerMapUnit(double mmPerMapUnit)
QgsLegendStyle style(QgsLegendStyle::Style s) const
Returns the style for a legend component.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setBoxSpace(double s)
Sets the legend box space (in millimeters), which is the empty margin around the inside of the legend...
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
void writeXml(const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes the component's style definition to an XML element.
void setFontColor(const QColor &color)
Sets the legend font color.
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
QString name() const override
Returns the layer's name.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
QColor rasterStrokeColor() const
Returns the stroke color for the stroke drawn around raster symbol items.
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns reference to modifiable legend style.
@ SymbolLabel
Symbol label (excluding icon)
long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
void changed()
Emitted when the object's properties change.
static QColor decodeColor(const QString &str)
bool legendFilterOutAtlas() const
Returns whether to filter out legend elements outside of the current atlas feature.
double wmsLegendWidth() const
Returns the WMS legend width.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
Contains information relating to the style entity currently being visited.
bool drawRasterStroke() const
Returns whether a stroke will be drawn around raster symbol items.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
void refreshLegend()
Emitted to refresh the legend.
virtual void redraw()
Triggers a redraw (update) of the item.
void finalizeRestoreFromXml() override
Called after all pending items have been restored from XML.
QString displayName() const override
Gets item display name.
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
#define Q_NOWARN_DEPRECATED_POP
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Returns legend node for given index.
@ AllProperties
All properties for item.
double wmsLegendHeight() const
Returns the WMS legend height.
void setResizeToContents(bool enabled)
Sets whether the legend should automatically resize to fit its contents.
bool legendFilterByMapEnabled() const
Find out whether legend items are filtered to show just the ones visible in the associated map.
static bool hasLegendFilterExpression(const QgsLayerTreeGroup &group)
Test if one of the layers in a group has an expression filter.
static QgsLayoutItemLegend * create(QgsLayout *layout)
Returns a new legend item for the specified layout.
void clearCachedData(QgsLayerTreeNode *node) const
Clears any previously cached data for the specified node.
void setAutoUpdateModel(bool autoUpdate)
Sets whether the legend content should auto update to reflect changes in the project's layer tree.
void setLegendFilterOutAtlas(bool doFilter)
When set to true, during an atlas rendering, it will filter out legend elements where features are ou...
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
double rasterStrokeWidth() const
Returns the stroke width (in millimeters) for the stroke drawn around raster symbol items.
void setLineSpacing(double spacing)
Sets the spacing in-between multiple lines.
void setStyle(QgsLegendStyle::Style component, const QgsLegendStyle &style)
Sets the style of component to style for the legend.
QgsLayoutItem::Flags itemFlags() const override
Returns the item's flags, which indicate how the item behaves.
void setSplitLayer(bool enabled)
Sets whether the legend items from a single layer can be split over multiple columns.
QFont styleFont(QgsLegendStyle::Style component) const
Returns the font settings for a legend component.
void setTitle(const QString &t)
Sets the title for the legend, which will be rendered above all legend items.
void setRasterStrokeWidth(double width)
Sets the stroke width for the stroke drawn around raster symbol items.
void setEqualColumnWidth(bool equalize)
Sets whether column widths should be equalized.
void setRasterStrokeColor(const QColor &color)
Sets the stroke color for the stroke drawn around raster symbol items.
double boxSpace() const
Returns the legend box space (in millimeters), which is the empty margin around the inside of the leg...
@ FlagOverridesPaint
Item overrides the default layout item painting method.
void projectColorsChanged()
Emitted whenever the project's color scheme has been changed.
Structure that stores all data associated with one map layer.
void setColumnSpace(double s)
Sets the margin space between adjacent columns (in millimeters).
Qt::AlignmentFlag symbolAlignment() const
Returns the alignment for placement of legend symbols.
QSizeF wmsLegendSize() const
Returns the size (in millimeters) of WMS legend graphics shown in the legend.
QgsRectangle extent() const
Returns the current map extent.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
void setBoxSpace(double space)
Sets the legend box space.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Handles preparing a paint surface for the layout item and painting the item's content.
@ MustPlaceInOwnLayer
Item must be placed in its own individual layer.
double scale() const
Returns the calculated map scale.
bool drawRasterStroke() const
Returns whether a stroke will be drawn around raster symbol items.
Base class for graphical items within a QgsLayout.
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
void updateFilterByMap(bool redraw=true)
Updates the legend content when filtered by map.
ExportLayerBehavior exportLayerBehavior() const override
Returns the behavior of this item during exporting to layered exports (e.g.
void setLineSpacing(double s)
Sets the line spacing to use between lines of legend text.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
QgsRectangle requestedExtent() const
Calculates the extent to request and the yShift of the top-left point in case of rotation.
Q_DECL_DEPRECATED void setDpi(int dpi)
double symbolWidth() const
Returns the legend symbol width.
QgsLayoutItemLegend(QgsLayout *layout)
Constructor for QgsLayoutItemLegend, with the specified parent layout.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
double mapUnitsToLayoutUnits() const
Returns the conversion factor from map units to layout units.
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map to associate with the legend.
void setDrawRasterStroke(bool enabled)
Sets whether a stroke will be drawn around raster symbol items.
Single scope for storing variables and functions for use within a QgsExpressionContext....
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false) const
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
Q_DECL_DEPRECATED void setMapScale(double scale)
Sets the legend map scale.
QString valueAsString(int key, const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a string.
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
QColor fontColor() const
Returns the legend font color.
double symbolHeight() const
Returns the legend symbol height.
void setSymbolWidth(double width)
Sets the legend symbol width.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
QString themeName() const
Returns the name of the theme currently linked to the legend.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
Layout graphical items for displaying a map.
@ LegendTitle
Legend title.
int columnCount() const
Returns the legend column count.
double boxSpace() const
Returns the legend box space.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
void setStyle(QgsLegendStyle::Style s, const QgsLegendStyle &style)
Sets the style for a legend component.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
virtual QString uuid() const
Returns the item identification string.
QgsLegendStyle style(QgsLegendStyle::Style s) const
Returns legend style.
void themeChanged(const QString &theme)
Emitted when the map's associated theme is changed.
void setColumnCount(int count)
Sets the legend column count.
void setFontColor(const QColor &c)
Sets the font color used for legend items.
void setDrawRasterStroke(bool enabled)
Sets whether a stroke will be drawn around raster symbol items.
void setColumnCount(int c)
Sets the desired minimum number of columns to show in the legend.
void setWmsLegendHeight(double height)
Sets the WMS legend height.
QPointer< QgsLayout > mLayout
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
void setFlag(Flag f, bool on=true)
Enable or disable a model flag.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
void adjustBoxSize()
Sets the legend's item bounds to fit the whole legend content.
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 mapRotationChanged(double newRotation)
Emitted when the map's rotation changes.
QFont font() const
Returns the font used for rendering this legend component.
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
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.
Flags flags() const
Returns OR-ed combination of model flags.
bool splitLayer() const
Returns true if layer components can be split over multiple columns.
void setStyleFont(QgsLegendStyle::Style component, const QFont &font)
Sets the style font for a legend component.
double height() const
Returns the height of the rectangle.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
QSizeF symbolSize() const
Returns the default symbol size (in millimeters) used for legend items.
void symbolFeatureCountMapChanged()
Emitted when the feature count for symbols on this layer has been recalculated.
virtual QString name() const =0
Returns name of the node.
void setColumnSpace(double spacing)
Sets the legend column spacing.
QgsVectorLayerFeatureCounter * countSymbolFeatures(bool storeSymbolFids=false)
Count features for symbols.
bool splitLayer() const
Returns whether the legend items from a single layer can be split over multiple columns.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
bool equalColumnWidth() const
Returns true if all columns should have equal widths.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
void setRasterStrokeWidth(double width)
Sets the stroke width for the stroke drawn around raster symbol items.
void setWmsLegendWidth(double width)
Sets the WMS legend width.
void setWmsLegendSize(QSizeF s)
Sets the desired size (in millimeters) of WMS legend graphics shown in the legend.
The QgsLegendRenderer class handles automatic layout and rendering of legend. The content is given by...
QColor fontColor() const
Returns the font color used for legend items.
double lineSpacing() const
Returns the line spacing to use between lines of legend text.
Q_DECL_DEPRECATED void setUseAdvancedEffects(bool use)
@ LayoutMillimeters
Millimeters.
@ AllowNodeReorder
Allow reordering with drag'n'drop.
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns modifiable reference to the style for a legend component.
QString title() const
Returns the legend title.
void setRasterStrokeColor(const QColor &color)
Sets the stroke color for the stroke drawn around raster symbol items.
QSizeF minimumSize(QgsRenderContext *renderContext=nullptr)
Runs the layout algorithm and returns the minimum size required for the legend.
void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties) override
bool resizeToContents() const
Returns whether the legend should automatically resize to fit its contents.
QPainter * painter()
Returns the destination QPainter for the render operation.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void setSymbolHeight(double height)
Sets the legend symbol height.
Single variable definition for use within a QgsExpressionContextScope.
double lineSpacing() const
Returns the spacing in-between lines in layout units.
static QgsLayerTree * readXml(QDomElement &element, const QgsReadWriteContext &context)
Load the layer tree from an XML element.
void removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file.
QgsLegendModel(QgsLayerTree *rootNode, QObject *parent=nullptr, QgsLayoutItemLegend *layout=nullptr)
Construct the model based on the given layer tree.
#define Q_NOWARN_DEPRECATED_PUSH
void readXml(const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads the component's style definition from an XML element.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
@ LayoutLegend
Legend item.
double columnSpace() const
Returns the margin space between adjacent columns (in millimeters).
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
@ Group
Legend group title.
QString wrapString() const
Returns the legend text wrapping string.
@ Subgroup
Legend subgroup title.
void setSymbolAlignment(Qt::AlignmentFlag alignment)
Sets the alignment for placement of legend symbols.
double width() const
Returns the width of the rectangle.
double columnSpace() const
Returns the legend column spacing.
void setWrapChar(const QString &t)
Sets a string to use as a wrapping character.
void updateLegend()
Updates the model and all legend entries.
bool equalColumnWidth() const
Returns whether column widths should be equalized.
void setWrapString(const QString &string)
Sets the legend text wrapping string.
DataDefinedProperty
Data defined properties for different item types.
QString wrapChar() const
Returns the string used as a wrapping character.
bool autoUpdateModel() const
Returns whether the legend content should auto update to reflect changes in the project's layer tree.
void setLegendFilterByMapEnabled(bool enabled)
Set whether legend items should be filtered to show just the ones visible in the associated map.
bool isNull() const
Returns true if the patch shape is a null QgsLegendPatchShape, which indicates that the default legen...