35 , mEmbeddedInParent( false )
46 return Qt::ItemIsEnabled;
65 double itemHeight = std::max( static_cast< double >( settings.
symbolSize().height() ), textHeight );
76 QIcon symbolIcon =
data( Qt::DecorationRole ).value<QIcon>();
77 if ( symbolIcon.isNull() )
89 QSizeF labelSize( 0, 0 );
97 labelSize.rheight() = lines.count() * textHeight + ( lines.count() - 1 ) * ( settings.
lineSpacing() + textDescent );
99 double labelX = 0.0, labelY = 0.0;
104 labelX = ctx->
point.x() + std::max( static_cast< double >( symbolSize.width() ), ctx->
labelXOffset );
105 labelY = ctx->
point.y();
108 if ( labelSize.height() < symbolSize.height() )
109 labelY += symbolSize.height() / 2 - labelSize.height() / 2;
111 labelY += textHeight;
114 for ( QStringList::Iterator itemPart = lines.begin(); itemPart != lines.end(); ++itemPart )
116 labelSize.rwidth() = std::max( settings.
textWidthMillimeters( symbolLabelFont, *itemPart ), double( labelSize.width() ) );
120 settings.
drawText( ctx->
painter, labelX, labelY, *itemPart, symbolLabelFont );
121 if ( itemPart != ( lines.end() - 1 ) )
122 labelY += textDescent + settings.
lineSpacing() + textHeight;
134 , mSymbolUsesMapUnits( false )
137 mIconSize = QSize( iconSize, iconSize );
141 connect( nodeLayer, &QObject::destroyed,
this, [ = ]() {
mLayerNode =
nullptr; } );
150 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
152 return Qt::ItemIsEnabled;
166 QSize minSz( iconSize, iconSize );
184 if ( !mTextOnSymbolLabel.isEmpty() && context )
188 int wInt = ceil( w ), hInt = ceil( h );
189 if ( wInt > minSz.width() ) minSz.setWidth( wInt );
190 if ( hInt > minSz.height() ) minSz.setHeight( hInt );
194 minSz.setWidth( mItem.
level() * INDENT_SIZE + minSz.width() );
210 if ( !vlayer || !vlayer->
renderer() )
245 context->setScaleFactor( dpi / 25.4 );
246 context->setRendererScale( scale );
248 return context.release();
251 void QgsSymbolLegendNode::checkAll(
bool state )
254 if ( !vlayer || !vlayer->
renderer() )
269 if ( role == Qt::DisplayRole )
273 else if ( role == Qt::EditRole )
277 else if ( role == Qt::DecorationRole )
279 if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
287 if ( !mTextOnSymbolLabel.isEmpty() && context )
289 QPainter painter( &pix );
290 painter.setRenderHint( QPainter::Antialiasing );
291 context->setPainter( &painter );
292 QFontMetricsF fm( mTextOnSymbolTextFormat.
scaledFont( *context ) );
293 qreal yBaselineVCenter = ( mIconSize.height() + fm.ascent() - fm.descent() ) / 2;
295 QStringList() << mTextOnSymbolLabel, *context, mTextOnSymbolTextFormat );
300 pix = QPixmap( mIconSize );
301 pix.fill( Qt::transparent );
309 QPixmap pix2( pix.width() + mItem.
level() * INDENT_SIZE, pix.height() );
310 pix2.fill( Qt::transparent );
312 p.drawPixmap( mItem.
level() * INDENT_SIZE, 0, pix );
319 else if ( role == Qt::CheckStateRole )
325 if ( !vlayer || !vlayer->
renderer() )
344 if ( role != Qt::CheckStateRole )
351 if ( !vlayer || !vlayer->
renderer() )
384 double height = settings.
symbolSize().height();
388 double widthOffset = 0;
389 double heightOffset = 0;
391 if (
QgsMarkerSymbol *markerSymbol = dynamic_cast<QgsMarkerSymbol *>( s ) )
394 double size = markerSymbol->size( context ) / context.
scaleFactor();
399 widthOffset = ( settings.
symbolSize().width() - width ) / 2.0;
401 if ( height < settings.
symbolSize().height() )
403 heightOffset = ( settings.
symbolSize().height() - height ) / 2.0;
409 double currentXPosition = ctx->
point.x();
410 double currentYCoord = ctx->
point.y() + ( itemHeight - settings.
symbolSize().height() ) / 2;
418 opacity = ( 255 * vectorLayer->opacity() );
421 p->setRenderHint( QPainter::Antialiasing );
422 p->translate( currentXPosition + widthOffset, currentYCoord + heightOffset );
423 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
428 QSize tempImageSize( width * dotsPerMM, height * dotsPerMM );
429 QImage tempImage = QImage( tempImageSize, QImage::Format_ARGB32 );
430 tempImage.fill( Qt::transparent );
431 QPainter imagePainter( &tempImage );
432 imagePainter.setRenderHint( QPainter::Antialiasing );
437 imagePainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
438 imagePainter.fillRect( tempImage.rect(), QColor( 0, 0, 0, opacity ) );
441 p->drawImage( 0, 0, tempImage );
445 s->
drawPreviewIcon( p, QSize( width * dotsPerMM, height * dotsPerMM ), &context );
448 if ( !mTextOnSymbolLabel.isEmpty() )
450 QFontMetricsF fm( mTextOnSymbolTextFormat.
scaledFont( context ) );
451 qreal yBaselineVCenter = ( height * dotsPerMM + fm.ascent() - fm.descent() ) / 2;
453 QStringList() << mTextOnSymbolLabel, context, mTextOnSymbolTextFormat );
459 return QSizeF( std::max( width + 2 * widthOffset, static_cast< double >( settings.
symbolSize().width() ) ),
460 std::max( height + 2 * heightOffset, static_cast< double >( settings.
symbolSize().height() ) ) );
473 if ( mSymbolUsesMapUnits )
481 void QgsSymbolLegendNode::updateLabel()
496 if ( showFeatureCount && vl && vl->
featureCount() >= 0 )
497 mLabel += QStringLiteral(
" [%1]" ).arg( vl->
featureCount() );
502 if ( showFeatureCount && vl )
505 mLabel += QStringLiteral(
" [%1]" ).arg( count != -1 ? QLocale().toString( count ) : tr(
"N/A" ) );
527 if ( role == Qt::DisplayRole || role == Qt::EditRole )
529 else if ( role == Qt::DecorationRole )
548 if ( role == Qt::DecorationRole )
550 return QPixmap::fromImage( mImage );
552 else if ( role == Qt::SizeHintRole )
554 return mImage.size();
561 Q_UNUSED( itemHeight );
566 mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
582 if ( role == Qt::DecorationRole )
585 QPixmap pix( iconSize, iconSize );
589 else if ( role == Qt::DisplayRole || role == Qt::EditRole )
600 QColor itemColor = mColor;
604 itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
606 ctx->
painter->setBrush( itemColor );
613 pen.setJoinStyle( Qt::MiterJoin );
618 ctx->
painter->setPen( Qt::NoPen );
635 QImage QgsWmsLegendNode::getLegendGraphic()
const 637 if ( ! mValid && ! mFetcher )
644 if ( ! mod )
return mImage;
649 Q_ASSERT( ! mFetcher );
668 if ( role == Qt::DecorationRole )
670 return QPixmap::fromImage( getLegendGraphic() );
672 else if ( role == Qt::SizeHintRole )
674 return getLegendGraphic().size();
681 Q_UNUSED( itemHeight );
687 QRectF( QPointF( 0, 0 ), mImage.size() ) );
693 QImage QgsWmsLegendNode::renderMessage(
const QString &msg )
const 695 const int fontHeight = 10;
696 const int margin = fontHeight / 2;
697 const int nlines = 1;
699 const int w = 512, h = fontHeight * nlines + margin * ( nlines + 1 );
700 QImage image( w, h, QImage::Format_ARGB32_Premultiplied );
702 painter.begin( &image );
703 painter.setPen( QColor( 255, 0, 0 ) );
704 painter.setFont( QFont( QStringLiteral(
"Chicago" ), fontHeight ) );
705 painter.fillRect( 0, 0, w, h, QColor( 255, 255, 255 ) );
706 painter.drawText( 0, margin + fontHeight, msg );
713 void QgsWmsLegendNode::getLegendGraphicProgress( qint64 cur, qint64 tot )
715 QString msg = QStringLiteral(
"Downloading... %1/%2" ).arg( cur ).arg( tot );
717 mImage = renderMessage( msg );
721 void QgsWmsLegendNode::getLegendGraphicErrored(
const QString &msg )
723 if ( ! mFetcher )
return;
725 mImage = renderMessage( msg );
737 void QgsWmsLegendNode::getLegendGraphicFinished(
const QImage &image )
739 if ( ! mFetcher )
return;
742 if ( ! image.isNull() )
744 if ( image != mImage )
778 if ( role == Qt::DecorationRole )
781 return QPixmap::fromImage( mImage );
783 else if ( role == Qt::SizeHintRole )
786 return mImage.size();
804 ctx->
painter->setRenderHint( QPainter::Antialiasing );
827 void QgsDataDefinedSizeLegendNode::cacheImage()
const 829 if ( mImage.isNull() )
835 context->setScaleFactor( 96 / 25.4 );
void setForceVectorOutput(bool force)
Sets whether rendering operations should use vector operations instead of any faster raster shortcuts...
double fontHeightCharacterMM(const QFont &font, QChar c) const
Returns the font height of a character in millimeters.
static QPixmap symbolPreviewPixmap(QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr)
Returns a pixmap preview for a color ramp.
void drawCollapsedLegend(QgsRenderContext &context, QSize *outputSize SIP_OUT=nullptr, int *labelXOffset SIP_OUT=nullptr) const
Draw the legend if using LegendOneNodeForAll and optionally output size of the legend and x offset of...
QgsDataDefinedSizeLegendNode(QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent=nullptr)
Construct the node using QgsDataDefinedSizeLegend as definition of the node's appearance.
QFont scaledFont(const QgsRenderContext &context) const
Returns a font with the size scaled to match the format's size settings (including units and map unit...
~QgsDataDefinedSizeLegendNode() override
double lineSpacing() const
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Text at point of origin draw mode.
Abstract base class for all rendered symbols.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
void setFont(const QFont &font)
Sets font used for rendering of labels - only valid for collapsed legend.
Use antialiasing while drawing.
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...
QgsMapLayer * layer() const
Returns the map layer associated with this node.
QgsImageLegendNode(QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent=nullptr)
Constructor for QgsImageLegendNode.
virtual bool setData(const QVariant &value, int role)
Sets some data associated with the item. Default implementation does nothing and returns false...
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QgsSymbolLegendNode(QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent=nullptr)
Constructor for QgsSymbolLegendNode.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
void emitStyleChanged()
Triggers an emission of the styleChanged() signal.
void setRendererScale(double scale)
Sets the renderer map scale.
const QgsSymbol * symbol() const
Returns the symbol used by the legend node.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
virtual QgsImageFetcher * getLegendGraphicFetcher(const QgsMapSettings *mapSettings)
Returns a new image downloader for the raster legend.
void invalidateMapBasedData() override
Notification from model that information from associated map view has changed.
void invalidateMapBasedData() override
Notification from model that information from associated map view has changed.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
virtual Qt::ItemFlags flags() const
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
double mmPerMapUnit() const
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
QString label() const
Returns text label.
QgsLayerTreeModelLegendNode(QgsLayerTreeLayer *nodeL, QObject *parent=nullptr)
Construct the node with pointer to its parent layer node.
QPainter * painter
Painter.
QgsWmsLegendNode(QgsLayerTreeLayer *nodeLayer, QObject *parent=nullptr)
Constructor for QgsWmsLegendNode.
int level() const
Indentation level that tells how deep the item is in a hierarchy of items. For flat lists level is 0...
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
A marker symbol type, for rendering Point and MultiPoint geometries.
The QgsMapSettings class contains configuration for rendering of the map.
Perform transforms between map coordinates and device coordinates.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2...
QgsRasterDataProvider * dataProvider() override
Returns the layer's data provider.
QString name() const override
Returns the layer's name.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted...
static int scaleIconSize(int standardSize)
Scales an layer tree model icon size to compensate for display pixel density, making the icon size hi...
Qt::ItemFlags flags() const override
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
Rule key of the node (QString)
QSize minimumIconSize() const
Calculates the minimum icon size to prevent cropping.
void setSymbol(QgsSymbol *s)
Sets symbol of the item. Takes ownership of symbol.
virtual QSizeF drawSymbolText(const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize) const
Draws label on the right side of the item.
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
QgsRasterSymbolLegendNode(QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent=nullptr)
Constructor for QgsRasterSymbolLegendNode.
void checkAllItems()
Checks all items belonging to the same layer as this node.
virtual QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const
Draws symbol on the left side of the item.
void error(const QString &msg)
Emitted when an error occurs.
QgsLayerTreeLayer * mLayerNode
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
ItemMetrics draw(const QgsLegendSettings &settings, ItemContext *ctx) override
Entry point called from QgsLegendRenderer to do the rendering.
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
QStringList splitStringForWrapping(const QString &stringToSplt) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
void drawPreviewIcon(QPainter *painter, QSize size, QgsRenderContext *customContext=nullptr)
Draws an icon of the symbol that occupies an area given by size using the specified painter...
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
QString parentRuleKey() const
Key of the parent legend node.
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
QgsFeatureRenderer * renderer()
Returns renderer.
void dataChanged()
Emitted on internal data change so the layer tree model can forward the signal to views...
void symbolFeatureCountMapChanged()
Emitted when the feature count for symbols on this layer has been recalculated.
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
item in symbology was checked
virtual void setLegendSymbolItem(const QString &key, QgsSymbol *symbol)
Sets the symbol to be used for a legend symbol item.
QgsLegendStyle style(QgsLegendStyle::Style s) const
Returns style.
bool testFlag(Flag f) const
Check whether a flag is enabled.
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
QImage collapsedLegendImage(QgsRenderContext &context, const QColor &backgroundColor=Qt::transparent, double paddingMM=1) const
Returns output image that would be shown in the legend. Returns invalid image if legend is not config...
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true)
Draws text within a rectangle using the specified settings.
virtual ItemMetrics draw(const QgsLegendSettings &settings, ItemContext *ctx)
Entry point called from QgsLegendRenderer to do the rendering.
void setEmbeddedInParent(bool embedded) override
double mapScale() const
Returns the legend map scale.
void setSymbol(QgsSymbol *symbol)
Sets the symbol to be used by the legend node.
bool isCheckable() const
Returns whether the item is user-checkable - whether renderer supports enabling/disabling it...
QgsSimpleLegendNode(QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon=QIcon(), QObject *parent=nullptr, const QString &key=QString())
Constructor for QgsSimpleLegendNode.
bool drawRasterStroke() const
Returns whether a stroke will be drawn around raster symbol items.
virtual void setEmbeddedInParent(bool embedded)
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
Contains information about the context of a rendering operation.
double rasterStrokeWidth() const
Returns the stroke width (in millimeters) for the stroke drawn around raster symbol items...
void legendMapViewData(double *mapUnitsPerPixel, int *dpi, double *scale) const
Gets hints about map view - to be used in legend nodes.
bool useAdvancedEffects() const
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
QPointF point
Top-left corner of the legend item.
bool setData(const QVariant &value, int role) override
Sets some data associated with the item. Default implementation does nothing and returns false...
QgsRenderContext * createTemporaryRenderContext() const
Returns a temporary context or null if legendMapViewData are not valid.
QgsLayerTreeModel * model() const
Returns pointer to model owning this legend node.
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
SymbolType type() const
Returns the symbol's type.
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
static QRect nonTransparentImageRect(const QImage &image, QSize minSize=QSize(), bool center=false)
Calculates the non-transparent region of an image.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
void setFlag(Flag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
QgsSymbol * symbol() const
Returns associated symbol. May be null.
void uncheckAllItems()
Unchecks all items belonging to the same layer as this node.
QFont font() const
The font for this style.
void setTextColor(const QColor &color)
Sets text color for rendering of labels - only valid for collapsed legend.
QString ruleKey() const
Returns unique identifier of the rule for identification of the item within renderer.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
QSizeF symbolSize() const
QSizeF wmsLegendSize() const
For legends that support it, will show them in a tree instead of a list (needs also ShowLegend)...
static double textWidth(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, QFontMetricsF *fontMetrics=nullptr)
Returns the width of a text based on a given format.
Represents a vector layer which manages a vector based data sets.
double labelXOffset
offset from the left side where label should start
void finish(const QImage &legend)
Emitted when the download completes.
Object that keeps configuration of appearance of marker symbol's data-defined size in legend...
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
Raster renderer pipe that applies colors to a raster.
void progress(qint64 received, qint64 total)
Emitted to report progress.
Layer tree node points to a map layer.
QColor rasterStrokeColor() const
Returns the stroke color for the stroke drawn around raster symbol items.
Base class for raster data providers.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode, QFontMetricsF *fontMetrics=nullptr)
Returns the height of a text based on a given format.
QgsUnitTypes::RenderUnit outputUnit() const
Returns the units to use for sizes and widths within the symbol.
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...