50#include "moc_qgslayertreemodellegendnode.cpp"
52using namespace Qt::StringLiterals;
61 return qobject_cast<QgsLayerTreeModel *>( parent() );
66 return Qt::ItemIsEnabled;
102 std::optional< QgsScopedRenderContextScaleToPixels > scaleToPx( *ctx.
context );
125 const double itemHeight = std::max(
static_cast< double >( ctx.
patchSize.height() > 0 ? ctx.
patchSize.height() : settings.
symbolSize().height() ), textHeight );
139 const QString text =
data( Qt::DisplayRole ).toString();
140 json[u
"title"_s] = text;
146 const QIcon symbolIcon =
data( Qt::DecorationRole ).value<QIcon>();
147 if ( symbolIcon.isNull() )
156 size.setHeight( ctx->
patchSize.height() );
163 const int width =
static_cast<int>( size.width() * scaleFactor );
164 const int height =
static_cast<int>( size.height() * scaleFactor );
165 const int y =
static_cast<int>( ( ctx->
top + ( itemHeight - size.height() ) / 2 ) * scaleFactor );
172 x =
static_cast<int>( ctx->
columnLeft * scaleFactor );
175 x =
static_cast<int>( ( ctx->
columnRight - size.width() ) * scaleFactor );
178 symbolIcon.paint( ctx->
painter, x, y, width, height );
185 const QIcon icon =
data( Qt::DecorationRole ).value<QIcon>();
187 return QJsonObject();
189 const QImage image( icon.pixmap( settings.
symbolSize().width(), settings.
symbolSize().height() ).toImage() );
190 QByteArray byteArray;
191 QBuffer buffer( &byteArray );
192 image.save( &buffer,
"PNG" );
193 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
196 json[u
"icon"_s] = base64;
203 std::unique_ptr< QgsRenderContext > tempContext;
208 context = tempContext.get();
214 std::optional< QgsTextDocument > tempDocument;
220 document = &tempDocument.value();
223 std::optional< QgsTextDocumentMetrics > tempMetrics;
228 metrics = &tempMetrics.value();
235 const QSizeF labelSizeMM( documentSize / dotsPerMM );
237 double labelXMin = 0.0;
238 double labelXMax = 0.0;
239 double labelYMM = 0.0;
240 if ( ctx && context->
painter() )
268 if ( labelSizeMM.height() < symbolSizeMM.height() )
269 labelYMM += ( symbolSizeMM.height() - labelSizeMM.height() ) / 2;
280 QRectF( labelXMin * dotsPerMM, std::round( labelYMM * dotsPerMM ), ( labelXMax - labelXMin ) * dotsPerMM, std::max( symbolSizeMM.height(), labelSizeMM.height() ) * dotsPerMM ),
309 if ( !vlayer->renderer() )
313 for (
const auto &item : symbolList )
315 vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), !vlayer->renderer()->legendSymbolItemChecked( item.ruleKey() ) );
319 vlayer->emitStyleChanged();
320 vlayer->triggerRepaint();
324 if ( !pclayer->renderer() )
327 const QStringList ruleKeys = pclayer->renderer()->legendRuleKeys();
328 for (
const QString &rule : ruleKeys )
330 pclayer->renderer()->checkLegendItem( rule, !pclayer->renderer()->legendItemChecked( rule ) );
334 pclayer->emitStyleChanged();
335 pclayer->triggerRepaint();
367 connect( nodeLayer, &QObject::destroyed,
this, [
this]() {
mLayerNode =
nullptr; } );
371 mSymbolUsesMapUnits =
symbol->usesMapUnits();
379 if ( mItem.isCheckable() )
380 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
382 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
399 int maxSize = largeIconSize;
407 if ( !ok && context )
424 if ( !mTextOnSymbolLabel.isEmpty() && context )
428 int wInt = ceil( w ), hInt = ceil( h );
429 if ( wInt > minSz.width() )
430 minSz.setWidth( wInt );
431 if ( hInt > minSz.height() )
432 minSz.setHeight( hInt );
440 return mItem.symbol();
448 const QVariant legendlabel =
mLayerNode->customProperty( u
"legend/title-label"_s );
476 return mCustomSymbol.get();
481 mCustomSymbol.reset(
symbol );
489 std::unique_ptr< QgsSymbol > s(
symbol );
491 if ( !vlayer || !vlayer->
renderer() )
494 mItem.setSymbol( s.get() );
508 if (
auto *lModel =
model() )
509 lModel->legendMapViewData( &mupp, &dpi, &scale );
515 auto context = std::make_unique<QgsRenderContext>();
516 context->setScaleFactor( dpi / 25.4 );
517 context->setRendererScale( scale );
523 if (
model() && !
model()->targetScreenProperties().isEmpty() )
530 context->setExpressionContext( expContext );
532 return context.release();
535void QgsLayerTreeModelLegendNode::checkAll(
bool state )
539 if ( !vlayer->renderer() )
543 for (
const auto &item : symbolList )
545 vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), state );
549 vlayer->emitStyleChanged();
550 vlayer->triggerRepaint();
552 else if ( QgsPointCloudLayer *pclayer = qobject_cast<QgsPointCloudLayer *>(
mLayerNode->layer() ) )
554 if ( !pclayer->renderer() )
557 const QStringList ruleKeys = pclayer->renderer()->legendRuleKeys();
558 for (
const QString &rule : ruleKeys )
560 pclayer->renderer()->checkLegendItem( rule, state );
564 pclayer->emitStyleChanged();
565 pclayer->triggerRepaint();
571 if ( role == Qt::DisplayRole )
575 else if ( role == Qt::EditRole )
579 else if ( role == Qt::DecorationRole )
581 if ( mPixmap.isNull() )
585 if ( mItem.symbol() )
595 if ( !mTextOnSymbolLabel.isEmpty() && context )
597 QPainter painter( &mPixmap );
598 painter.setRenderHint( QPainter::Antialiasing );
599 context->setPainter( &painter );
600 bool isNullSize =
false;
601 const QFontMetricsF fm( mTextOnSymbolTextFormat.scaledFont( *context, 1.0, &isNullSize ) );
604 const qreal yBaselineVCenter = ( mIconSize.height() + fm.ascent() - fm.descent() ) / 2;
612 mPixmap.fill( Qt::transparent );
617 else if ( role == Qt::CheckStateRole )
619 if ( !mItem.isCheckable() )
624 if ( !vlayer->renderer() )
627 return vlayer->renderer()->legendSymbolItemChecked( mItem.ruleKey() ) ? Qt::Checked : Qt::Unchecked;
632 return mItem.ruleKey();
636 return mItem.parentRuleKey();
648 if ( role != Qt::CheckStateRole )
651 if ( !mItem.isCheckable() )
655 if ( !vlayer || !vlayer->
renderer() )
661 project->setDirty(
true );
673 QgsSymbol *s = mCustomSymbol ? mCustomSymbol.get() : mItem.symbol();
681 std::unique_ptr< QgsRenderContext > tempRenderContext;
687 tempRenderContext = std::make_unique< QgsRenderContext >();
690 tempRenderContext->setScaleFactor( settings.
dpi() / 25.4 );
691 tempRenderContext->setRendererScale( settings.
mapScale() );
696 tempRenderContext->setPainter( ctx ? ctx->
painter :
nullptr );
701 tempRenderContext->setExpressionContext( expContext );
702 context = tempRenderContext.get();
706 const bool hasFixedWidth = ctx && ctx->
patchSize.width() > 0;
707 const bool hasFixedHeight = ctx && ctx->
patchSize.height() > 0;
708 const double desiredHeight = hasFixedHeight ? ctx->
patchSize.height() : settings.
symbolSize().height();
709 const double desiredWidth = hasFixedWidth ? ctx->
patchSize.width() : settings.
symbolSize().width();
710 double height = desiredHeight;
711 double width = desiredWidth;
714 double widthOffset = 0;
715 double heightOffset = 0;
722 const double size = markerSymbol->size( *context ) / context->
scaleFactor();
725 if ( !hasFixedHeight )
727 if ( !hasFixedWidth )
732 bool restrictedSizeSymbolOK;
733 double restrictedSymbolWidth = width;
734 double restrictedSymbolHeight = height;
735 const std::unique_ptr<QgsSymbol> minMaxSizeSymbol(
738 if ( minMaxSizeSymbol )
740 s = minMaxSizeSymbol.get();
741 if ( !hasFixedHeight )
742 height = restrictedSymbolHeight;
743 if ( !hasFixedWidth )
744 width = restrictedSymbolWidth;
749 if ( width < desiredWidth )
751 widthOffset = ( desiredWidth - width ) / 2.0;
753 if ( height < desiredHeight )
755 heightOffset = ( desiredHeight - height ) / 2.0;
760 const double currentYCoord = ctx->
top + ( itemHeight - desiredHeight ) / 2;
768 opacity =
static_cast<int >( std::round( 255 * layer->opacity() ) );
777 p->translate( ctx->
columnLeft + widthOffset, currentYCoord + heightOffset );
780 p->translate( ctx->
columnRight - widthOffset - width, currentYCoord + heightOffset );
784 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
790 if ( opacity != 255 && !forceVector )
797 const QSize symbolSize(
static_cast< int >( std::round( width * dotsPerMM ) ),
static_cast<int >( std::round( height * dotsPerMM ) ) );
798 const QSize tempImageSize( symbolSize.width() + maxBleed * 2, symbolSize.height() + maxBleed * 2 );
799 QImage tempImage = QImage( tempImageSize, QImage::Format_ARGB32 );
800 tempImage.fill( Qt::transparent );
801 QPainter imagePainter( &tempImage );
805 imagePainter.translate( maxBleed, maxBleed );
807 imagePainter.translate( -maxBleed, -maxBleed );
810 imagePainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
811 imagePainter.fillRect( tempImage.rect(), QColor( 0, 0, 0, opacity ) );
814 p->drawImage( -maxBleed, -maxBleed, tempImage );
816 else if ( !restrictedSizeSymbolOK )
821 const QSize symbolSize(
static_cast< int >( std::round( width * dotsPerMM ) ),
static_cast<int >( std::round( height * dotsPerMM ) ) );
822 const QSize maxSize( symbolSize.width() + maxBleed * 2, symbolSize.height() + maxBleed * 2 );
824 p->setClipRect( -maxBleed, -maxBleed, maxSize.width(), maxSize.height(), Qt::IntersectClip );
830 s->
drawPreviewIcon( p, QSize(
static_cast< int >( std::round( width * dotsPerMM ) ),
static_cast< int >( std::round( height * dotsPerMM ) ) ), context,
false,
nullptr, &
patchShape, ctx->
screenProperties );
833 if ( !mTextOnSymbolLabel.isEmpty() )
835 bool isNullSize =
false;
836 const QFontMetricsF fm( mTextOnSymbolTextFormat.scaledFont( *context, 1.0, &isNullSize ) );
839 const qreal yBaselineVCenter = ( height * dotsPerMM + fm.ascent() - fm.descent() ) / 2;
845 return QSizeF( std::max( width + 2 * widthOffset,
static_cast< double >( desiredWidth ) ), std::max( height + 2 * heightOffset,
static_cast< double >( desiredHeight ) ) );
851 if ( mItem.scaleMaxDenom() > 0 )
853 json[u
"scaleMaxDenom"_s] = mItem.scaleMaxDenom();
855 if ( mItem.scaleMinDenom() > 0 )
857 json[u
"scaleMinDenom"_s] = mItem.scaleMinDenom();
860 const QgsSymbol *s = mCustomSymbol ? mCustomSymbol.get() : mItem.symbol();
885 QImage img( pix.toImage().convertToFormat( QImage::Format_ARGB32_Premultiplied ) );
889 opacity = ( 255 * layer->opacity() );
891 if ( opacity != 255 )
894 painter.begin( &img );
895 painter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
896 painter.fillRect( pix.rect(), QColor( 0, 0, 0, opacity ) );
900 QByteArray byteArray;
901 QBuffer buffer( &byteArray );
902 img.save( &buffer,
"PNG" );
903 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
905 json[u
"icon"_s] = base64;
918 if ( mSymbolUsesMapUnits )
927 if ( mIconSize == sz )
935void QgsSymbolLegendNode::updateLabel()
947 if ( showFeatureCount && vl )
953 mLabel += u
" [%1%2]"_s.arg( estimatedCount ? u
"≈"_s : QString(), count != -1 ? QLocale().toString( count ) : tr(
"N/A" ) );
973 if ( label.isEmpty() )
976 if ( !
mLayerNode->labelExpression().isEmpty() )
978 else if ( symLabel.contains(
"[%" ) )
985 if ( !
mLayerNode->labelExpression().isEmpty() )
987 else if ( label.contains(
"[%" ) )
1021 if ( role == Qt::DisplayRole || role == Qt::EditRole )
1023 else if ( role == Qt::DecorationRole )
1043 if ( role == Qt::DecorationRole )
1045 return QPixmap::fromImage( mImage );
1047 else if ( role == Qt::SizeHintRole )
1049 return mImage.size();
1060 Q_UNUSED( itemHeight )
1066 const double imgWidth = settings.
wmsLegendSize().width() * scaleFactor;
1067 const double imgHeight = settings.
wmsLegendSize().height() * scaleFactor;
1069 const QImage scaledImg = mImage.scaled( QSizeF( imgWidth, imgHeight ).toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation );
1074 ctx->
painter->drawImage( QPointF( ctx->
columnLeft * scaleFactor, ctx->
top * scaleFactor ), scaledImg );
1077 case Qt::AlignRight:
1078 ctx->
painter->drawImage( QPointF( ctx->
columnRight * scaleFactor - imgWidth, ctx->
top * scaleFactor ), scaledImg );
1087 QByteArray byteArray;
1088 QBuffer buffer( &byteArray );
1089 mImage.save( &buffer,
"PNG" );
1090 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1093 json[u
"icon"_s] = base64;
1107 , mParentRuleKey( parentRuleKey )
1113 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
1115 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
1122 case Qt::DecorationRole:
1125 QPixmap pix( iconSize, iconSize );
1127 return QIcon( pix );
1130 case Qt::DisplayRole:
1141 return mParentRuleKey;
1143 case Qt::CheckStateRole:
1150 if ( !pclayer->renderer() )
1153 return pclayer->renderer()->legendItemChecked( mRuleKey ) ? Qt::Checked : Qt::Unchecked;
1166 if ( role != Qt::CheckStateRole )
1174 if ( !pclayer->renderer() )
1177 pclayer->renderer()->checkLegendItem( mRuleKey, value == Qt::Checked );
1180 pclayer->emitStyleChanged();
1182 pclayer->triggerRepaint();
1183 if ( pclayer->sync3DRendererTo2DRenderer() )
1184 pclayer->convertRenderer3DFromRenderer2D();
1203 if ( ctx->
patchSize.width() < size.width() )
1204 offsetX = ( size.width() - ctx->
patchSize.width() ) / 2.0;
1205 size.setWidth( ctx->
patchSize.width() );
1209 size.setHeight( ctx->
patchSize.height() );
1215 QColor itemColor = mColor;
1219 itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
1221 ctx->
painter->setBrush( itemColor );
1228 pen.setJoinStyle( Qt::MiterJoin );
1233 ctx->
painter->setPen( Qt::NoPen );
1240 ctx->
painter->drawRect( QRectF( ctx->
columnLeft + offsetX, ctx->
top + ( itemHeight - size.height() ) / 2, size.width(), size.height() ) );
1243 case Qt::AlignRight:
1244 ctx->
painter->drawRect( QRectF( ctx->
columnRight - size.width() - offsetX, ctx->
top + ( itemHeight - size.height() ) / 2, size.width(), size.height() ) );
1253 QImage img = QImage( settings.
symbolSize().toSize(), QImage::Format_ARGB32 );
1254 img.fill( Qt::transparent );
1256 QPainter painter( &img );
1257 painter.setRenderHint( QPainter::Antialiasing );
1259 QColor itemColor = mColor;
1263 itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
1265 painter.setBrush( itemColor );
1272 pen.setJoinStyle( Qt::MiterJoin );
1273 painter.setPen( pen );
1277 painter.setPen( Qt::NoPen );
1280 painter.drawRect( QRectF( 0, 0, settings.
symbolSize().width(), settings.
symbolSize().height() ) );
1282 QByteArray byteArray;
1283 QBuffer buffer( &byteArray );
1284 img.save( &buffer,
"PNG" );
1285 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1288 json[u
"icon"_s] = base64;
1300QImage QgsWmsLegendNode::getLegendGraphic(
bool synchronous )
const
1302 if ( !mValid && !mFetcher )
1309 if ( layer && layer->
isValid() )
1320 Q_ASSERT( !mFetcher );
1332 connect( mFetcher.get(), &QObject::destroyed, &loop, &QEventLoop::quit );
1339 QgsDebugError( u
"Failed to download legend graphics: layer is not valid."_s );
1348 if ( role == Qt::DecorationRole )
1350 return QPixmap::fromImage( getLegendGraphic() );
1352 else if ( role == Qt::SizeHintRole )
1354 return getLegendGraphic().size();
1365 Q_UNUSED( itemHeight )
1369 double px2mm = 1000. / image.dotsPerMeterX();
1370 double mmWidth = image.width() * px2mm;
1371 double mmHeight = image.height() * px2mm;
1373 QSize targetSize = QSize( mmWidth, mmHeight );
1376 double targetHeight = mmHeight * settings.
wmsLegendSize().width() / mmWidth;
1377 targetSize = QSize( settings.
wmsLegendSize().width(), targetHeight );
1381 double targetWidth = mmWidth * settings.
wmsLegendSize().height() / mmHeight;
1382 targetSize = QSize( targetWidth, settings.
wmsLegendSize().height() );
1387 QImage smoothImage = image.scaled( targetSize / px2mm, Qt::KeepAspectRatio, Qt::SmoothTransformation );
1393 ctx->
painter->drawImage( QRectF( ctx->
columnLeft, ctx->
top, targetSize.width(), targetSize.height() ), smoothImage, QRectF( QPointF( 0, 0 ), smoothImage.size() ) );
1396 case Qt::AlignRight:
1397 ctx->
painter->drawImage( QRectF( ctx->
columnRight - settings.
wmsLegendSize().width(), ctx->
top, targetSize.width(), targetSize.height() ), smoothImage, QRectF( QPointF( 0, 0 ), smoothImage.size() ) );
1406 QByteArray byteArray;
1407 QBuffer buffer( &byteArray );
1408 mImage.save( &buffer,
"PNG" );
1409 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1412 json[u
"icon"_s] = base64;
1416QImage QgsWmsLegendNode::renderMessage(
const QString &msg )
const
1418 const int fontHeight = 10;
1419 const int margin = fontHeight / 2;
1420 const int nlines = 1;
1422 const int w = 512, h = fontHeight * nlines + margin * ( nlines + 1 );
1423 QImage image( w, h, QImage::Format_ARGB32_Premultiplied );
1425 painter.begin( &image );
1426 painter.setPen( QColor( 255, 0, 0 ) );
1427 painter.setFont( QFont( u
"Chicago"_s, fontHeight ) );
1428 painter.fillRect( 0, 0, w, h, QColor( 255, 255, 255 ) );
1429 painter.drawText( 0, margin + fontHeight, msg );
1436void QgsWmsLegendNode::getLegendGraphicProgress( qint64 cur, qint64 tot )
1438 const QString msg = tot > 0 ? tr(
"Downloading: %1% (%2)" ).arg(
static_cast< int >( std::round( 100 * cur / tot ) ) ).arg(
QgsFileUtils::representFileSize( tot ) )
1440 mImage = renderMessage( msg );
1444void QgsWmsLegendNode::getLegendGraphicErrored(
const QString & )
1457void QgsWmsLegendNode::getLegendGraphicFinished(
const QImage &image )
1462 if ( !image.isNull() )
1464 if ( image != mImage )
1484 return getLegendGraphic(
true );
1499 if ( role == Qt::DecorationRole )
1502 return QPixmap::fromImage( mImage );
1504 else if ( role == Qt::SizeHintRole )
1507 return mImage.size();
1520 std::unique_ptr< QgsRenderContext > tempRenderContext;
1525 tempRenderContext = std::make_unique< QgsRenderContext >();
1528 tempRenderContext->setScaleFactor( settings.
dpi() / 25.4 );
1529 tempRenderContext->setRendererScale( settings.
mapScale() );
1531 tempRenderContext->setMapToPixel(
QgsMapToPixel( 1 / ( settings.
mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
1532 tempRenderContext->setForceVectorOutput(
true );
1533 tempRenderContext->setPainter( ctx.
painter );
1540 tempRenderContext->setExpressionContext( expContext );
1541 context = tempRenderContext.get();
1558 double labelXOffset;
1562 context->
painter()->restore();
1571void QgsDataDefinedSizeLegendNode::cacheImage()
const
1573 if ( mImage.isNull() )
1578 context = std::make_unique<QgsRenderContext>();
1579 Q_ASSERT( context );
1580 context->setScaleFactor( 96 / 25.4 );
1582 mImage = mSettings->collapsedLegendImage( *context );
1588 , mLabelSettings( labelSettings )
1596 if ( role == Qt::DisplayRole )
1600 if ( role == Qt::DecorationRole )
1610 Q_UNUSED( itemHeight );
1613 return QSizeF( 0, 0 );
1617 if ( renderContext )
1622 return QSizeF( 0, 0 );
1627 const QStringList textLines( mLabelSettings.
legendString() );
1630 double textWidth, textHeight;
1631 textWidthHeight( textWidth, textHeight, ctx, textFormat, textLines );
1635 textPos( renderContext.
scaleFactor() * ( xOffset + settings.
symbolSize().width() / 2.0 - textWidth / 2.0 ), renderContext.
scaleFactor() * ( yOffset + settings.
symbolSize().height() / 2.0 + textHeight / 2.0 ) );
1640 const double symbolWidth = std::max( textWidth, settings.
symbolSize().width() );
1641 const double symbolHeight = std::max( textHeight, settings.
symbolSize().height() );
1642 return QSizeF( symbolWidth, symbolHeight );
1647 Q_UNUSED( settings );
1649 const double mmToPixel = 96.0 / 25.4;
1651 const QStringList textLines( mLabelSettings.legendString() );
1656 double textWidth, textHeight;
1657 textWidthHeight( textWidth, textHeight, ctx, textFormat, textLines );
1660 QByteArray byteArray;
1661 QBuffer buffer( &byteArray );
1662 previewPixmap.save( &buffer,
"PNG" );
1663 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1666 json[u
"icon"_s] = base64;
1670void QgsVectorLabelLegendNode::textWidthHeight(
double &width,
double &height,
QgsRenderContext &ctx,
const QgsTextFormat &textFormat,
const QStringList &textLines )
const
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
@ Symbol
Symbol icon (excluding label).
@ SymbolLabel
Symbol label (excluding icon).
@ Point
Text at point of origin layout mode.
@ RectangleCapHeightBased
Similar to Rectangle mode, but uses cap height only when calculating font heights for the first line ...
@ Horizontal
Horizontally oriented text.
@ Millimeters
Millimeters.
@ RenderSymbolPreview
The render is for a symbol preview only and map based properties may not be available,...
@ RenderLayerTree
The render is for a layer tree display where map based properties are not available and where avoidan...
@ LosslessImageRendering
Render images losslessly whenever possible, instead of the default lossy jpeg rendering used for some...
@ Antialiasing
Use antialiasing while drawing.
TextHorizontalAlignment
Text horizontal alignment.
@ WrapLines
Automatically wrap long lines of text.
QgsDataDefinedSizeLegendNode(QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent=nullptr)
Construct the node using QgsDataDefinedSizeLegend as definition of the node's appearance.
ItemMetrics draw(const QgsLegendSettings &settings, ItemContext &ctx) override
Entry point called from QgsLegendRenderer to do the rendering.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
~QgsDataDefinedSizeLegendNode() override
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
void setFont(const QFont &font)
Sets font used for rendering of labels - only valid for collapsed legend.
void setTextColor(const QColor &color)
Sets text color for rendering of labels - only valid for collapsed legend.
void drawCollapsedLegend(QgsRenderContext &context, QSizeF *outputSize SIP_OUT=nullptr, double *labelXOffset SIP_OUT=nullptr) const
Draw the legend if using LegendOneNodeForAll and optionally output size of the legend and x offset of...
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
Stores the component parts of a data source URI (e.g.
bool useEstimatedMetadata() const
Returns true if estimated metadata should be used for the connection.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
static QString replaceExpressionText(const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr)
This function replaces each expression between [% and %] in the string with the result of its evaluat...
virtual void setLegendSymbolItem(const QString &key, QgsSymbol *symbol)
Sets the symbol to be used for a legend symbol item.
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
Sets whether the legend symbology item with the specified ley should be checked.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
void progress(qint64 received, qint64 total)
Emitted to report progress.
void error(const QString &msg)
Emitted when an error occurs.
void finish(const QImage &legend)
Emitted when the download completes.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
QgsImageLegendNode(QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent=nullptr)
Constructor for QgsImageLegendNode.
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
Adds a symbol in base64 string within a JSON object with the key "icon".
static QRect nonTransparentImageRect(const QImage &image, QSize minSize=QSize(), bool center=false)
Calculates the non-transparent region of an image.
Layer tree node points to a map layer.
QString labelExpression() const
Returns the expression member of the LayerTreeNode.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
QJsonObject exportToJson(const QgsLegendSettings &settings, const QgsRenderContext &context)
Entry point called from QgsLegendRenderer to do the rendering in a JSON object.
@ SimpleLegend
Simple label with icon legend node type.
@ RasterSymbolLegend
Raster symbol legend node type.
@ ImageLegend
Raster image legend node type.
@ DataDefinedSizeLegend
Marker symbol legend node type.
@ WmsLegend
WMS legend node type.
@ SymbolLegend
Vector symbol legend node type.
void checkAllItems()
Checks all checkable items belonging to the same layer as this node.
@ ParentRuleKey
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2....
@ NodeType
Type of node. Added in 3.16.
@ RuleKey
Rule key of the node (QString).
void uncheckAllItems()
Unchecks all checkable items belonging to the same layer as this node.
QgsLayerTreeLayer * mLayerNode
QgsLayerTreeModelLegendNode(QgsLayerTreeLayer *nodeL, QObject *parent=nullptr)
Construct the node with pointer to its parent layer node.
virtual void setUserPatchSize(QSizeF size)
Sets the user (overridden) size for the legend node.
void sizeChanged()
Emitted when the size of this node changes.
virtual ItemMetrics draw(const QgsLegendSettings &settings, ItemContext &ctx)
Entry point called from QgsLegendRenderer to do the rendering.
void dataChanged()
Emitted on internal data change so the layer tree model can forward the signal to views.
QgsRenderContext * createTemporaryRenderContext() const
Returns a temporary context or nullptr if legendMapViewData are not valid.
virtual QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const
Adds a symbol in base64 string within a JSON object with the key "icon".
QgsLayerTreeModel * model() const
Returns pointer to model owning this legend node.
void toggleAllItems()
Toggle all checkable items belonging to the same layer as this node.
QgsLegendPatchShape mPatchShape
virtual QSizeF userPatchSize() const
Returns the user (overridden) size for the legend node.
virtual Qt::ItemFlags flags() const
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
virtual QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const
Draws symbol on the left side of the item.
virtual void setEmbeddedInParent(bool embedded)
virtual bool setData(const QVariant &value, int role)
Sets some data associated with the item. Default implementation does nothing and returns false.
virtual QSizeF drawSymbolText(const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize) const
Draws label on the right side of the item.
A model representing the layer tree, including layers and groups of layers.
QSet< QgsScreenProperties > targetScreenProperties() const
Returns the target screen properties to use when generating icons.
static int scaleIconSize(int standardSize)
Scales an layer tree model icon size to compensate for display pixel density, making the icon size hi...
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or nullptr if none is enabled).
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.
Represents a patch shape for use in map legends.
Stores the appearance and layout settings for legend drawing with QgsLegendRenderer.
bool drawRasterStroke() const
Returns whether a stroke will be drawn around raster symbol items.
QSizeF wmsLegendSize() const
Returns the size (in millimeters) of WMS legend graphics shown in the legend.
double minimumSymbolSize() const
Returns the minimum symbol size (in mm).
double autoWrapLinesAfter() const
Returns the maximum line length (in millimeters) allowed before lines of text in the legend will be a...
double rasterStrokeWidth() const
Returns the stroke width (in millimeters) for the stroke drawn around raster symbol items.
QSizeF symbolSize() const
Returns the default symbol size (in millimeters) used for legend items.
double maximumSymbolSize() const
Returns the maximum symbol size (in mm).
QColor rasterStrokeColor() const
Returns the stroke color for the stroke drawn around raster symbol items.
Q_DECL_DEPRECATED bool useAdvancedEffects() const
QgsLegendStyle style(Qgis::LegendComponent s) const
Returns the style for a legend component.
Q_DECL_DEPRECATED int dpi() const
bool synchronousLegendRequests() const
Returns whether to request legend graphics synchronously.
Q_DECL_DEPRECATED double mmPerMapUnit() const
QStringList evaluateItemText(const QString &text, const QgsExpressionContext &context) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
Qt::AlignmentFlag symbolAlignment() const
Returns the alignment for placement of legend symbols.
Q_DECL_DEPRECATED double mapScale() const
Returns the legend map scale.
Qt::Alignment alignment() const
Returns the alignment for the legend component.
QgsTextFormat & textFormat()
Returns the text format used for rendering this legend component.
double margin(Side side) const
Returns the margin (in mm) for the specified side of the component.
Stores information about one class/rule of a vector layer renderer in a unified way that can be used ...
QString ruleKey() const
Returns unique identifier of the rule for identification of the item within renderer.
Base class for all map layer types.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
void emitStyleChanged()
Triggers an emission of the styleChanged() signal.
QgsProject * project() const
Returns the parent project if this map layer is added to a project.
Contains configuration for rendering maps.
Perform transforms between map coordinates and device coordinates.
A marker symbol type, for rendering Point and MultiPoint geometries.
Contains settings for how a map layer will be labeled.
QString legendString() const
legendString
static QPixmap labelSettingsPreviewPixmap(const QgsPalLayerSettings &settings, QSize size, const QString &previewText=QString(), int padding=0, const QgsScreenProperties &screen=QgsScreenProperties())
Returns a pixmap preview for label settings.
const QgsTextFormat & format() const
Returns the label text formatting settings, e.g., font settings, buffer settings, etc.
Represents a map layer supporting display of point clouds.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Base class for raster data providers.
virtual QgsImageFetcher * getLegendGraphicFetcher(const QgsMapSettings *mapSettings)
Returns a new image downloader for the raster legend.
Represents a raster layer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
Raster renderer pipe that applies colors to a raster.
Qt::ItemFlags flags() const override
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
bool setData(const QVariant &value, int role) override
Sets some data associated with the item. Default implementation does nothing and returns false.
QString ruleKey() const
Returns the unique identifier of node for identification of the item within renderer.
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
Adds a symbol in base64 string within a JSON object with the key "icon".
QgsRasterSymbolLegendNode(QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent=nullptr, bool isCheckable=false, const QString &ruleKey=QString(), const QString &parentRuleKey=QString())
Constructor for QgsRasterSymbolLegendNode.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
bool isCheckable() const
Returns whether the item is user-checkable - whether renderer supports enabling/disabling it.
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
Contains information about the context of a rendering operation.
Q_DECL_DEPRECATED void setForceVectorOutput(bool force)
Sets whether rendering operations should use vector operations instead of any faster raster shortcuts...
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
QgsExpressionContext & expressionContext()
Gets the expression context.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
Qgis::RasterizedRenderingPolicy rasterizedRenderingPolicy() const
Returns the policy controlling when rasterisation of content during renders is permitted.
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
static QgsRenderContext fromQPainter(QPainter *painter)
Creates a default render context given a pixel based QPainter destination.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void setRendererScale(double scale)
Sets the renderer map scale.
Qgis::RenderContextFlags flags() const
Returns combination of flags used for rendering.
Scoped object for saving and restoring a QPainter object's state.
Scoped object for temporary scaling of a QgsRenderContext for pixel based rendering.
Stores properties relating to a screen.
double devicePixelRatio() const
Returns the ratio between physical pixels and device-independent pixels for the screen.
static QgsSettingsTreeNode * sTreeLayerTree
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
QgsSimpleLegendNode(QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon=QIcon(), QObject *parent=nullptr, const QString &key=QString())
Constructor for QgsSimpleLegendNode.
static std::unique_ptr< QgsSymbol > restrictedSizeSymbol(const QgsSymbol *s, double minSize, double maxSize, QgsRenderContext *context, double &width, double &height, bool *ok=nullptr)
Creates a new symbol with size restricted to min/max size if original size is out of min/max range.
static QPixmap symbolPreviewPixmap(const QgsSymbol *symbol, QSize size, int padding=0, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr, const QgsLegendPatchShape *shape=nullptr, const QgsScreenProperties &screen=QgsScreenProperties())
Returns a pixmap preview for a color ramp.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
static double MAXIMUM_SIZE
QString evaluateLabel(const QgsExpressionContext &context=QgsExpressionContext(), const QString &label=QString())
Evaluates and returns the text label of the current node.
const QgsSymbol * symbol() const
Returns the symbol used by the legend node.
void setPatchShape(const QgsLegendPatchShape &shape)
Sets the symbol patch shape to use when rendering the legend node symbol.
QgsSymbolLegendNode(QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent=nullptr)
Constructor for QgsSymbolLegendNode.
void setIconSize(QSize sz)
Set the icon size.
static const QgsSettingsEntryDouble * settingsLegendSymbolMaximumSize
QgsExpressionContextScope * createSymbolScope() const
Create an expression context scope containing symbol related variables.
QgsLegendPatchShape patchShape() const
Returns the symbol patch shape to use when rendering the legend node symbol.
QSize minimumIconSize() const
Calculates the minimum icon size to prevent cropping.
~QgsSymbolLegendNode() override
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
void invalidateMapBasedData() override
Notification from model that information from associated map view has changed.
bool setData(const QVariant &value, int role) override
Sets some data associated with the item. Default implementation does nothing and returns false.
Qt::ItemFlags flags() const override
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
void setCustomSymbol(QgsSymbol *symbol)
Sets the node's custom symbol.
void setEmbeddedInParent(bool embedded) override
QgsSymbol * customSymbol() const
Returns the node's custom symbol.
QString symbolLabel() const
Label of the symbol, user defined label will be used, otherwise will default to the label made by QGI...
void setSymbol(QgsSymbol *symbol)
Sets the symbol to be used by the legend node.
static double MINIMUM_SIZE
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
static const QgsSettingsEntryDouble * settingsLegendSymbolMinimumSize
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
Adds a symbol in base64 string within a JSON object with the key "icon".
Abstract base class for all rendered symbols.
void drawPreviewIcon(QPainter *painter, QSize size, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr, const QgsLegendPatchShape *patchShape=nullptr, const QgsScreenProperties &screen=QgsScreenProperties())
Draws an icon of the symbol that occupies an area given by size using the specified painter.
Qgis::SymbolType type() const
Returns the symbol's type.
Contains pre-calculated metrics of a QgsTextDocument.
QSizeF documentSize(Qgis::TextLayoutMode mode, Qgis::TextOrientation orientation) const
Returns the overall size of the document.
double firstLineCapHeight() const
Returns the cap height for the first line of text.
static QgsTextDocumentMetrics calculateMetrics(const QgsTextDocument &document, const QgsTextFormat &format, const QgsRenderContext &context, double scaleFactor=1.0, const QgsTextDocumentRenderContext &documentContext=QgsTextDocumentRenderContext())
Returns precalculated text metrics for a text document, when rendered using the given base format and...
const QgsTextDocument & document() const
Returns the document associated with the calculated metrics.
Encapsulates the context in which a text document is to be rendered.
void setFlags(Qgis::TextRendererFlags flags)
Sets associated text renderer flags.
void setMaximumWidth(double width)
Sets the maximum width (in painter units) for rendered text.
Represents a document consisting of one or more QgsTextBlock objects.
static QgsTextDocument fromTextAndFormat(const QStringList &lines, const QgsTextFormat &format)
Constructor for QgsTextDocument consisting of a set of lines, respecting settings from a text format.
Container for all settings relating to text rendering.
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
QColor color() const
Returns the color that text will be rendered in.
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.
static void drawDocument(const QRectF &rect, const QgsTextFormat &format, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics, QgsRenderContext &context, Qgis::TextHorizontalAlignment horizontalAlignment=Qgis::TextHorizontalAlignment::Left, Qgis::TextVerticalAlignment verticalAlignment=Qgis::TextVerticalAlignment::Top, double rotation=0, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags())
Draws a text document within a rectangle using the specified settings.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor=1.0)
Returns the font metrics for the given text format, when rendered in the specified render context.
static double calculateScaleFactorForFormat(const QgsRenderContext &context, const QgsTextFormat &format)
Returns the scale factor used for upscaling font sizes and downscaling destination painter devices.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Point, QFontMetricsF *fontMetrics=nullptr, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), double maxLineWidth=0)
Returns the height of a text based on a given format.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
QgsVectorLabelLegendNode(QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent=nullptr)
QgsVectorLabelLegendNode.
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
drawSymbol
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
exportSymbolToJson
~QgsVectorLabelLegendNode() override
QVariant data(int role) const override
data Returns data associated with the item
Represents a vector layer which manages a vector based dataset.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
void symbolFeatureCountMapChanged()
Emitted when the feature count for symbols on this layer has been recalculated.
QgsExpressionContextScope * createExpressionContextScope() const final
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
QImage getLegendGraphicBlocking() const
Fetches the image from the server and returns it.
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
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.
~QgsWmsLegendNode() override
QgsWmsLegendNode(QgsLayerTreeLayer *nodeLayer, QObject *parent=nullptr)
Constructor for QgsWmsLegendNode.
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
Adds a symbol in base64 string within a JSON object with the key "icon".
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
QList< QgsLegendSymbolItem > QgsLegendSymbolList
#define QgsDebugError(str)
Single variable definition for use within a QgsExpressionContextScope.
QPainter * painter
Painter.
double top
Top y-position of legend item.
QgsLegendPatchShape patchShape
The patch shape to render for the node.
QgsScreenProperties screenProperties
Destination screen properties.
double maxSiblingSymbolWidth
Largest symbol width, considering all other sibling legend components associated with the current com...
QSizeF patchSize
Symbol patch size to render for the node.
const QgsTextDocument * textDocument
Optional text document.
double columnLeft
Left side of current legend column.
const QgsTextDocumentMetrics * textDocumentMetrics
Optional text document metrics.
double columnRight
Right side of current legend column.
Q_NOWARN_DEPRECATED_POP QgsRenderContext * context
Render context, if available.