QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgslayertreemodellegendnode.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreemodellegendnode.cpp
3 --------------------------------------
4 Date : August 2014
5 Copyright : (C) 2014 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7
8 QgsWMSLegendNode : Sandro Santilli < strk at keybit dot net >
9
10 ***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
20#include "moc_qgslayertreemodellegendnode.cpp"
21
23#include "qgslayertreemodel.h"
24#include "qgslegendsettings.h"
25#include "qgsrasterlayer.h"
26#include "qgsrenderer.h"
27#include "qgssymbollayerutils.h"
28#include "qgsimageoperation.h"
29#include "qgsvectorlayer.h"
30#include "qgspointcloudlayer.h"
32#include "qgsrasterrenderer.h"
34#include "qgsexpression.h"
35#include "qgstextrenderer.h"
36#include "qgssettings.h"
37#include "qgsfileutils.h"
38#include "qgsmarkersymbol.h"
39#include "qgsvariantutils.h"
40#include "qgslayertreelayer.h"
41#include "qgstextdocument.h"
43
44#include <QBuffer>
45#include <optional>
46
48 : QObject( parent )
49 , mLayerNode( nodeL )
50 , mEmbeddedInParent( false )
51{
52}
53
55{
56 return qobject_cast<QgsLayerTreeModel *>( parent() );
57}
58
60{
61 return Qt::ItemIsEnabled;
62}
63
64bool QgsLayerTreeModelLegendNode::setData( const QVariant &value, int role )
65{
66 Q_UNUSED( value )
67 Q_UNUSED( role )
68 return false;
69}
70
72{
74 return mLayerNode->patchSize();
75
76 return mUserSize;
77}
78
80{
81 if ( mUserSize == size )
82 return;
83
84 mUserSize = size;
85 emit sizeChanged();
86}
87
89{
91
92 const QStringList lines = settings.evaluateItemText( data( Qt::DisplayRole ).toString(), ctx->context->expressionContext() );
93
94 const QgsTextDocument textDocument = QgsTextDocument::fromTextAndFormat( lines, f );
95 // cppcheck-suppress autoVariables
96 ctx->textDocument = &textDocument;
97
98 std::optional< QgsScopedRenderContextScaleToPixels > scaleToPx( *ctx->context );
99 const double textScaleFactor = QgsTextRenderer::calculateScaleFactorForFormat( *ctx->context, f );
100
101 const QgsTextDocumentMetrics textDocumentMetrics = QgsTextDocumentMetrics::calculateMetrics( textDocument, f, *ctx->context, textScaleFactor );
102 // cppcheck-suppress autoVariables
103 ctx->textDocumentMetrics = &textDocumentMetrics;
104 scaleToPx.reset();
105
106 // itemHeight here is not really item height, it is only for symbol
107 // vertical alignment purpose, i.e. OK take single line height
108 // if there are more lines, those run under the symbol
109 // also note that we explicitly use the first line cap height here, in order to match the Qgis::TextLayoutMode::RectangleCapHeightBased mode
110 // used when rendering the symbol text
111 const double textHeight = textDocumentMetrics.firstLineCapHeight() / ctx->context->scaleFactor();
112 const double itemHeight = std::max( static_cast< double >( ctx && ctx->patchSize.height() > 0 ? ctx->patchSize.height() : settings.symbolSize().height() ), textHeight );
113
114 ItemMetrics im;
115 im.symbolSize = drawSymbol( settings, ctx, itemHeight );
116 im.labelSize = drawSymbolText( settings, ctx, im.symbolSize );
117
118 ctx->textDocument = nullptr;
119 ctx->textDocumentMetrics = nullptr;
120 return im;
121}
122
124{
125 QJsonObject json = exportSymbolToJson( settings, context );
126 const QString text = data( Qt::DisplayRole ).toString();
127 json[ QStringLiteral( "title" ) ] = text;
128 return json;
129}
130
131QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
132{
133 const QIcon symbolIcon = data( Qt::DecorationRole ).value<QIcon>();
134 if ( symbolIcon.isNull() )
135 return QSizeF();
136
137 QSizeF size = settings.symbolSize();
138 if ( ctx )
139 {
140 if ( ctx->patchSize.width() > 0 )
141 size.setWidth( ctx->patchSize.width( ) );
142 if ( ctx->patchSize.height() > 0 )
143 size.setHeight( ctx->patchSize.height( ) );
144 }
145
146 if ( ctx && ctx->painter && ctx->context )
147 {
148 const QgsScopedRenderContextScaleToPixels scopedScaleToPixels( *( ctx->context ) );
149 const double scaleFactor = ctx->context->scaleFactor();
150 const int width = static_cast<int>( size.width() * scaleFactor );
151 const int height = static_cast<int>( size.height() * scaleFactor );
152 const int y = static_cast<int>( ( ctx->top + ( itemHeight - size.height() ) / 2 ) * scaleFactor );
153 int x = 0;
154
155 switch ( settings.symbolAlignment() )
156 {
157 case Qt::AlignLeft:
158 default:
159 x = static_cast<int>( ctx->columnLeft * scaleFactor );
160 break;
161 case Qt::AlignRight:
162 x = static_cast<int>( ( ctx->columnRight - size.width() ) * scaleFactor );
163 break;
164 }
165 symbolIcon.paint( ctx->painter, x, y, width, height );
166 }
167 return size;
168}
169
171{
172 const QIcon icon = data( Qt::DecorationRole ).value<QIcon>();
173 if ( icon.isNull() )
174 return QJsonObject();
175
176 const QImage image( icon.pixmap( settings.symbolSize().width(), settings.symbolSize().height() ).toImage() );
177 QByteArray byteArray;
178 QBuffer buffer( &byteArray );
179 image.save( &buffer, "PNG" );
180 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
181
182 QJsonObject json;
183 json[ QStringLiteral( "icon" ) ] = base64;
184 return json;
185}
186
187QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSizeMM ) const
188{
189 // we need a full render context here, so make one if we don't already have one
190 std::unique_ptr< QgsRenderContext > tempContext;
191 QgsRenderContext *context = ctx ? ctx->context : nullptr;
192 if ( !context )
193 {
194 tempContext.reset( new QgsRenderContext( QgsRenderContext::fromQPainter( ctx ? ctx->painter : nullptr ) ) );
195 context = tempContext.get();
196 }
197
198 const QgsTextFormat format = settings.style( QgsLegendStyle::SymbolLabel ).textFormat();
199
200 // TODO QGIS 4.0 -- make these all mandatory
201 std::optional< QgsTextDocument > tempDocument;
202 const QgsTextDocument *document = ctx ? ctx->textDocument : nullptr;
203 if ( !document )
204 {
205 const QStringList lines = settings.evaluateItemText( data( Qt::DisplayRole ).toString(), context->expressionContext() );
206 tempDocument.emplace( QgsTextDocument::fromTextAndFormat( lines, format ) );
207 document = &tempDocument.value();
208 }
209
210 std::optional< QgsTextDocumentMetrics > tempMetrics;
211 const QgsTextDocumentMetrics *metrics = ctx ? ctx->textDocumentMetrics : nullptr;
212 if ( !metrics )
213 {
214 tempMetrics.emplace( QgsTextDocumentMetrics::calculateMetrics( *document, format, *context ) );
215 metrics = &tempMetrics.value();
216 }
217
218 const double dotsPerMM = context->scaleFactor();
219 QgsScopedRenderContextScaleToPixels scaleToPx( *context );
220
222 const QSizeF labelSizeMM( documentSize / dotsPerMM );
223
224 double labelXMin = 0.0;
225 double labelXMax = 0.0;
226 double labelYMM = 0.0;
227 if ( ctx && context->painter() )
228 {
229 switch ( settings.symbolAlignment() )
230 {
231 case Qt::AlignLeft:
232 default:
233 labelXMin = ctx->columnLeft + std::max( static_cast< double >( symbolSizeMM.width() ), ctx->maxSiblingSymbolWidth )
236 labelXMax = ctx->columnRight;
237 break;
238
239 case Qt::AlignRight:
240 labelXMin = ctx->columnLeft;
241 // NOTE -- while the below calculations use the flipped margins from the style, that's only done because
242 // those are the only margins we expose and use for now! (and we expose them as generic margins, not side-specific
243 // ones) TODO when/if we expose other margin settings, these should be reversed...
244 labelXMax = ctx->columnRight - std::max( static_cast< double >( symbolSizeMM.width() ), ctx->maxSiblingSymbolWidth )
247 break;
248 }
249
250 labelYMM = ctx->top;
251
252 // Vertical alignment of label with symbol
253 if ( labelSizeMM.height() < symbolSizeMM.height() )
254 labelYMM += ( symbolSizeMM.height() - labelSizeMM.height() ) / 2; // label centered with symbol
255 }
256
257 if ( context->painter() )
258 {
261
262
263 QgsTextRenderer::drawDocument( QRectF( labelXMin * dotsPerMM, std::round( labelYMM * dotsPerMM ),
264 ( labelXMax - labelXMin )* dotsPerMM,
265 std::max( symbolSizeMM.height(), labelSizeMM.height() ) * dotsPerMM ),
266 format, *document, *metrics, *context, halign, Qgis::TextVerticalAlignment::Top,
268 }
269
270 return labelSizeMM;
271}
272
274{
275 checkAll( true );
276}
277
279{
280 checkAll( false );
281}
282
284{
285 if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() ) )
286 {
287 if ( !vlayer->renderer() )
288 return;
289
290 const QgsLegendSymbolList symbolList = vlayer->renderer()->legendSymbolItems();
291 for ( const auto &item : symbolList )
292 {
293 vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), ! vlayer->renderer()->legendSymbolItemChecked( item.ruleKey() ) );
294 }
295
296 emit dataChanged();
297 vlayer->emitStyleChanged();
298 vlayer->triggerRepaint();
299 }
300 else if ( QgsPointCloudLayer *pclayer = qobject_cast<QgsPointCloudLayer *>( mLayerNode->layer() ) )
301 {
302 if ( !pclayer->renderer() )
303 return;
304
305 const QStringList ruleKeys = pclayer->renderer()->legendRuleKeys();
306 for ( const QString &rule : ruleKeys )
307 {
308 pclayer->renderer()->checkLegendItem( rule, !pclayer->renderer()->legendItemChecked( rule ) );
309 }
310
311 emit dataChanged();
312 pclayer->emitStyleChanged();
313 pclayer->triggerRepaint();
314 }
315}
316
317// -------------------------------------------------------------------------
318
321
323 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
324 , mItem( item )
325 , mSymbolUsesMapUnits( false )
326{
328 mIconSize = QSize( iconSize, iconSize );
329
330 if ( MINIMUM_SIZE < 0 )
331 {
332 // it's FAR too expensive to construct a QgsSettings object for every symbol node, especially for complex
333 // projects. So only read the valid size ranges once, and store them for subsequent use
334 const QgsSettings settings;
335 MINIMUM_SIZE = settings.value( "/qgis/legendsymbolMinimumSize", 0.5 ).toDouble();
336 MAXIMUM_SIZE = settings.value( "/qgis/legendsymbolMaximumSize", 20.0 ).toDouble();
337 }
338
339 updateLabel();
340 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( nodeLayer->layer() ) )
341 connect( vl, &QgsVectorLayer::symbolFeatureCountMapChanged, this, &QgsSymbolLegendNode::updateLabel );
342
343 connect( nodeLayer, &QObject::destroyed, this, [this]() { mLayerNode = nullptr; } );
344
345 if ( const QgsSymbol *symbol = mItem.symbol() )
346 {
347 mSymbolUsesMapUnits = symbol->usesMapUnits();
348 }
349}
350
352
353Qt::ItemFlags QgsSymbolLegendNode::flags() const
354{
355 if ( mItem.isCheckable() )
356 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
357 else
358 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
359}
360
361
363{
364 const std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
365 return minimumIconSize( context.get() );
366}
367
369{
371 const int largeIconSize = QgsLayerTreeModel::scaleIconSize( 512 );
372 QSize minSz( iconSize, iconSize );
373 if ( mItem.symbol() && ( mItem.symbol()->type() == Qgis::SymbolType::Marker
374 || mItem.symbol()->type() == Qgis::SymbolType::Line ) )
375 {
376 int maxSize = largeIconSize;
377
378 // unusued width, height variables
379 double width = 0.0;
380 double height = 0.0;
381 bool ok;
382 std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), MINIMUM_SIZE, MAXIMUM_SIZE, context, width, height, &ok ) );
383
384 if ( !ok && context )
385 {
386 // It's not possible to get a restricted size symbol, so we restrict
387 // pixmap target size to be sure it would fit MAXIMUM_SIZE
388 maxSize = static_cast<int>( std::round( MAXIMUM_SIZE * context->scaleFactor() ) );
389 }
390
391 const QSize size( mItem.symbol()->type() == Qgis::SymbolType::Marker ? maxSize : minSz.width(),
392 maxSize );
393
394 QgsScreenProperties targetScreen = model() && !model()->targetScreenProperties().isEmpty()
396
399 context, false, nullptr, nullptr, targetScreen ).toImage(),
400 minSz,
401 true ).size() / targetScreen.devicePixelRatio();
402 }
403
404 if ( !mTextOnSymbolLabel.isEmpty() && context )
405 {
406 const double w = QgsTextRenderer::textWidth( *context, mTextOnSymbolTextFormat, QStringList() << mTextOnSymbolLabel );
407 const double h = QgsTextRenderer::textHeight( *context, mTextOnSymbolTextFormat, QStringList() << mTextOnSymbolLabel, Qgis::TextLayoutMode::Point );
408 int wInt = ceil( w ), hInt = ceil( h );
409 if ( wInt > minSz.width() ) minSz.setWidth( wInt );
410 if ( hInt > minSz.height() ) minSz.setHeight( hInt );
411 }
412
413 return minSz;
414}
415
417{
418 return mItem.symbol();
419}
420
422{
423 QString label;
424 if ( mEmbeddedInParent )
425 {
426 const QVariant legendlabel = mLayerNode->customProperty( QStringLiteral( "legend/title-label" ) );
427 const QString layerName = QgsVariantUtils::isNull( legendlabel ) ? mLayerNode->name() : legendlabel.toString();
428 label = mUserLabel.isEmpty() ? layerName : mUserLabel;
429 }
430 else
431 label = mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
432 return label;
433}
434
436{
437 if ( mEmbeddedInParent )
438 {
439 return mLayerNode->patchShape();
440 }
441 else
442 {
443 return mPatchShape;
444 }
445}
446
448{
449 mPatchShape = shape;
450}
451
453{
454 return mCustomSymbol.get();
455}
456
458{
459 mCustomSymbol.reset( symbol );
460}
461
463{
464 if ( !symbol )
465 return;
466
467 std::unique_ptr< QgsSymbol > s( symbol ); // this method takes ownership of symbol
468 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
469 if ( !vlayer || !vlayer->renderer() )
470 return;
471
472 mItem.setSymbol( s.get() ); // doesn't transfer ownership
473 vlayer->renderer()->setLegendSymbolItem( mItem.ruleKey(), s.release() ); // DOES transfer ownership!
474
475 mPixmap = QPixmap();
476
477 emit dataChanged();
478 vlayer->triggerRepaint();
479}
480
482{
483 double scale = 0.0;
484 double mupp = 0.0;
485 int dpi = 0;
486 if ( auto *lModel = model() )
487 lModel->legendMapViewData( &mupp, &dpi, &scale );
488
489 if ( qgsDoubleNear( mupp, 0.0 ) || dpi == 0 || qgsDoubleNear( scale, 0.0 ) )
490 return nullptr;
491
492 // setup temporary render context
493 std::unique_ptr<QgsRenderContext> context = std::make_unique<QgsRenderContext>( );
494 context->setScaleFactor( dpi / 25.4 );
495 context->setRendererScale( scale );
496 context->setMapToPixel( QgsMapToPixel( mupp ) );
497 context->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
498 context->setFlag( Qgis::RenderContextFlag::RenderSymbolPreview, true );
499
500 if ( model() && !model()->targetScreenProperties().isEmpty() )
501 {
502 model()->targetScreenProperties().begin()->updateRenderContextForScreen( *context );
503 }
504
505 QgsExpressionContext expContext;
507 context->setExpressionContext( expContext );
508
509 return context.release();
510}
511
512void QgsLayerTreeModelLegendNode::checkAll( bool state )
513{
514 if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() ) )
515 {
516 if ( !vlayer->renderer() )
517 return;
518
519 const QgsLegendSymbolList symbolList = vlayer->renderer()->legendSymbolItems();
520 for ( const auto &item : symbolList )
521 {
522 vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), state );
523 }
524
525 emit dataChanged();
526 vlayer->emitStyleChanged();
527 vlayer->triggerRepaint();
528 }
529 else if ( QgsPointCloudLayer *pclayer = qobject_cast<QgsPointCloudLayer *>( mLayerNode->layer() ) )
530 {
531 if ( !pclayer->renderer() )
532 return;
533
534 const QStringList ruleKeys = pclayer->renderer()->legendRuleKeys();
535 for ( const QString &rule : ruleKeys )
536 {
537 pclayer->renderer()->checkLegendItem( rule, state );
538 }
539
540 emit dataChanged();
541 pclayer->emitStyleChanged();
542 pclayer->triggerRepaint();
543 }
544}
545
546QVariant QgsSymbolLegendNode::data( int role ) const
547{
548 if ( role == Qt::DisplayRole )
549 {
550 return mLabel;
551 }
552 else if ( role == Qt::EditRole )
553 {
554 return mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
555 }
556 else if ( role == Qt::DecorationRole )
557 {
558 if ( mPixmap.isNull() )
559 {
560 QgsScreenProperties targetScreen = model() && !model()->targetScreenProperties().isEmpty()
562
563 if ( mItem.symbol() )
564 {
565 std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
566
567 // unusued width, height variables
568 double width = 0.0;
569 double height = 0.0;
570 const std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), MINIMUM_SIZE, MAXIMUM_SIZE, context.get(), width, height ) );
571 mPixmap = QgsSymbolLayerUtils::symbolPreviewPixmap( symbol ? symbol.get() : mItem.symbol(), mIconSize, 0, context.get(), false, nullptr, nullptr, targetScreen );
572
573 if ( !mTextOnSymbolLabel.isEmpty() && context )
574 {
575 QPainter painter( &mPixmap );
576 painter.setRenderHint( QPainter::Antialiasing );
577 context->setPainter( &painter );
578 bool isNullSize = false;
579 const QFontMetricsF fm( mTextOnSymbolTextFormat.scaledFont( *context, 1.0, &isNullSize ) );
580 if ( !isNullSize )
581 {
582 const qreal yBaselineVCenter = ( mIconSize.height() + fm.ascent() - fm.descent() ) / 2;
583 QgsTextRenderer::drawText( QPointF( mIconSize.width() / 2, yBaselineVCenter ), 0, Qgis::TextHorizontalAlignment::Center,
584 QStringList() << mTextOnSymbolLabel, *context, mTextOnSymbolTextFormat );
585 }
586 }
587 }
588 else
589 {
590 mPixmap = QPixmap( mIconSize * targetScreen.devicePixelRatio() );
591 mPixmap.fill( Qt::transparent );
592 }
593 }
594 return mPixmap;
595 }
596 else if ( role == Qt::CheckStateRole )
597 {
598 if ( !mItem.isCheckable() )
599 return QVariant();
600
601 if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() ) )
602 {
603 if ( !vlayer->renderer() )
604 return QVariant();
605
606 return vlayer->renderer()->legendSymbolItemChecked( mItem.ruleKey() ) ? Qt::Checked : Qt::Unchecked;
607 }
608 }
609 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) )
610 {
611 return mItem.ruleKey();
612 }
613 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::ParentRuleKey ) )
614 {
615 return mItem.parentRuleKey();
616 }
617 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
618 {
620 }
621
622 return QVariant();
623}
624
625bool QgsSymbolLegendNode::setData( const QVariant &value, int role )
626{
627 if ( role != Qt::CheckStateRole )
628 return false;
629
630 if ( !mItem.isCheckable() )
631 return false;
632
633 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
634 if ( !vlayer || !vlayer->renderer() )
635 return false;
636
637 vlayer->renderer()->checkLegendSymbolItem( mItem.ruleKey(), value == Qt::Checked );
638
639 if ( QgsProject *project = vlayer->project() )
640 project->setDirty( true );
641
642 emit dataChanged();
643 vlayer->emitStyleChanged();
644
645 vlayer->triggerRepaint();
646
647 return true;
648}
649
650QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
651{
652 QgsSymbol *s = mCustomSymbol ? mCustomSymbol.get() : mItem.symbol();
653 if ( !s )
654 {
655 return QSizeF();
656 }
657
658 // setup temporary render context
659 QgsRenderContext *context = nullptr;
660 std::unique_ptr< QgsRenderContext > tempRenderContext;
662 if ( ctx && ctx->context )
663 context = ctx->context;
664 else
665 {
666 tempRenderContext = std::make_unique< QgsRenderContext >();
667 // QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
669 tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
670 tempRenderContext->setRendererScale( settings.mapScale() );
671 tempRenderContext->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
672 tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
674 tempRenderContext->setForceVectorOutput( true );
675 tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
676
677 // setup a minimal expression context
678 QgsExpressionContext expContext;
680 tempRenderContext->setExpressionContext( expContext );
681 context = tempRenderContext.get();
682 }
683
684 //Consider symbol size for point markers
685 const double desiredHeight = ctx && ctx->patchSize.height() > 0 ? ctx->patchSize.height() : settings.symbolSize().height();
686 const double desiredWidth = ctx && ctx->patchSize.width() > 0 ? ctx->patchSize.width() : settings.symbolSize().width();
687 double height = desiredHeight;
688 double width = desiredWidth;
689
690 //Center small marker symbols
691 double widthOffset = 0;
692 double heightOffset = 0;
693
694 const double maxSymbolSize = settings.maximumSymbolSize();
695 const double minSymbolSize = settings.minimumSymbolSize();
696
697 if ( QgsMarkerSymbol *markerSymbol = dynamic_cast<QgsMarkerSymbol *>( s ) )
698 {
699 const double size = markerSymbol->size( *context ) / context->scaleFactor();
700 if ( size > 0 )
701 {
702 height = size;
703 width = size;
704 }
705 }
706
707 bool restrictedSizeSymbolOK;
708 const std::unique_ptr<QgsSymbol> minMaxSizeSymbol( QgsSymbolLayerUtils::restrictedSizeSymbol( s, minSymbolSize, maxSymbolSize, context, width, height, &restrictedSizeSymbolOK ) );
709 if ( minMaxSizeSymbol )
710 {
711 s = minMaxSizeSymbol.get();
712 }
713
714 if ( s->type() == Qgis::SymbolType::Marker )
715 {
716 if ( width < desiredWidth )
717 {
718 widthOffset = ( desiredWidth - width ) / 2.0;
719 }
720 if ( height < desiredHeight )
721 {
722 heightOffset = ( desiredHeight - height ) / 2.0;
723 }
724 }
725 if ( ctx && ctx->painter )
726 {
727 const double currentYCoord = ctx->top + ( itemHeight - desiredHeight ) / 2;
728 QPainter *p = ctx->painter;
729
730 //setup painter scaling to dots so that raster symbology is drawn to scale
731 const double dotsPerMM = context->scaleFactor();
732
733 int opacity = 255;
734 if ( QgsMapLayer *layer = layerNode()->layer() )
735 opacity = static_cast<int >( std::round( 255 * layer->opacity() ) );
736
737 const QgsScopedQPainterState painterState( p );
738 context->setPainterFlagsUsingContext( p );
739
740 switch ( settings.symbolAlignment() )
741 {
742 case Qt::AlignLeft:
743 default:
744 p->translate( ctx->columnLeft + widthOffset, currentYCoord + heightOffset );
745 break;
746 case Qt::AlignRight:
747 p->translate( ctx->columnRight - widthOffset - width, currentYCoord + heightOffset );
748 break;
749 }
750
751 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
753 // QGIS 4.0 -- ctx->context will be mandatory
754 const bool useAdvancedEffects = ctx->context ? ctx->context->flags() & Qgis::RenderContextFlag::UseAdvancedEffects : settings.useAdvancedEffects();
756
757 if ( opacity != 255 && useAdvancedEffects )
758 {
759 // if this is a semi transparent layer, we need to draw symbol to an image (to flatten it first)
760
761 const int maxBleed = static_cast< int >( std::ceil( QgsSymbolLayerUtils::estimateMaxSymbolBleed( s, *context ) ) );
762
763 // create image which is same size as legend rect, in case symbol bleeds outside its allotted space
764 const QSize symbolSize( static_cast< int >( std::round( width * dotsPerMM ) ), static_cast<int >( std::round( height * dotsPerMM ) ) );
765 const QSize tempImageSize( symbolSize.width() + maxBleed * 2, symbolSize.height() + maxBleed * 2 );
766 QImage tempImage = QImage( tempImageSize, QImage::Format_ARGB32 );
767 tempImage.fill( Qt::transparent );
768 QPainter imagePainter( &tempImage );
769 context->setPainterFlagsUsingContext( &imagePainter );
770
771 context->setPainter( &imagePainter );
772 imagePainter.translate( maxBleed, maxBleed );
773 s->drawPreviewIcon( &imagePainter, symbolSize, context, false, nullptr, &patchShape, ctx->screenProperties );
774 imagePainter.translate( -maxBleed, -maxBleed );
775 context->setPainter( ctx->painter );
776 //reduce opacity of image
777 imagePainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
778 imagePainter.fillRect( tempImage.rect(), QColor( 0, 0, 0, opacity ) );
779 imagePainter.end();
780 //draw rendered symbol image
781 p->drawImage( -maxBleed, -maxBleed, tempImage );
782 }
783 else if ( !restrictedSizeSymbolOK )
784 {
785 // if there is no restricted symbol size (because of geometry generator mainly) we need to ensure
786 // that there is no drawing outside the given size
787 const int maxBleed = static_cast< int >( std::ceil( QgsSymbolLayerUtils::estimateMaxSymbolBleed( s, *context ) ) );
788 const QSize symbolSize( static_cast< int >( std::round( width * dotsPerMM ) ), static_cast<int >( std::round( height * dotsPerMM ) ) );
789 const QSize maxSize( symbolSize.width() + maxBleed * 2, symbolSize.height() + maxBleed * 2 );
790 p->save();
791 p->setClipRect( -maxBleed, -maxBleed, maxSize.width(), maxSize.height(), Qt::IntersectClip );
792 s->drawPreviewIcon( p, symbolSize, context, false, nullptr, &patchShape, ctx->screenProperties );
793 p->restore();
794 }
795 else
796 {
797 s->drawPreviewIcon( p, QSize( static_cast< int >( std::round( width * dotsPerMM ) ), static_cast< int >( std::round( height * dotsPerMM ) ) ), context, false, nullptr, &patchShape, ctx->screenProperties );
798 }
799
800 if ( !mTextOnSymbolLabel.isEmpty() )
801 {
802 bool isNullSize = false;
803 const QFontMetricsF fm( mTextOnSymbolTextFormat.scaledFont( *context, 1.0, &isNullSize ) );
804 if ( !isNullSize )
805 {
806 const qreal yBaselineVCenter = ( height * dotsPerMM + fm.ascent() - fm.descent() ) / 2;
807 QgsTextRenderer::drawText( QPointF( width * dotsPerMM / 2, yBaselineVCenter ), 0, Qgis::TextHorizontalAlignment::Center,
808 QStringList() << mTextOnSymbolLabel, *context, mTextOnSymbolTextFormat );
809 }
810 }
811 }
812
813 return QSizeF( std::max( width + 2 * widthOffset, static_cast< double >( desiredWidth ) ),
814 std::max( height + 2 * heightOffset, static_cast< double >( desiredHeight ) ) );
815}
816
817QJsonObject QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const
818{
819 QJsonObject json;
820 if ( mItem.scaleMaxDenom() > 0 )
821 {
822 json[ QStringLiteral( "scaleMaxDenom" ) ] = mItem.scaleMaxDenom();
823 }
824 if ( mItem.scaleMinDenom() > 0 )
825 {
826 json[ QStringLiteral( "scaleMinDenom" ) ] = mItem.scaleMinDenom();
827 }
828 mItem.scaleMaxDenom();
829
830 const QgsSymbol *s = mCustomSymbol ? mCustomSymbol.get() : mItem.symbol();
831 if ( !s )
832 {
833 return json;
834 }
835
836
838 // QGIS 4.0 - use render context directly here, and note in the dox that the context must be correctly setup
840 ctx.setScaleFactor( settings.dpi() / 25.4 );
841 ctx.setRendererScale( settings.mapScale() );
842 ctx.setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * ctx.scaleFactor() ) ) );
843 ctx.setForceVectorOutput( true );
846
848
849 // ensure that a minimal expression context is available
850 QgsExpressionContext expContext = context.expressionContext();
852 ctx.setExpressionContext( expContext );
853
854 const QPixmap pix = QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), minimumIconSize(), 0, &ctx );
855 QImage img( pix.toImage().convertToFormat( QImage::Format_ARGB32_Premultiplied ) );
856
857 int opacity = 255;
858 if ( QgsMapLayer *layer = layerNode()->layer() )
859 opacity = ( 255 * layer->opacity() );
860
861 if ( opacity != 255 )
862 {
863 QPainter painter;
864 painter.begin( &img );
865 painter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
866 painter.fillRect( pix.rect(), QColor( 0, 0, 0, opacity ) );
867 painter.end();
868 }
869
870 QByteArray byteArray;
871 QBuffer buffer( &byteArray );
872 img.save( &buffer, "PNG" );
873 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
874
875 json[ QStringLiteral( "icon" ) ] = base64;
876 return json;
877}
878
880{
882 updateLabel();
883}
884
885
887{
888 if ( mSymbolUsesMapUnits )
889 {
890 mPixmap = QPixmap();
891 emit dataChanged();
892 }
893}
894
896{
897 if ( mIconSize == sz )
898 return;
899
900 mIconSize = sz;
901 mPixmap = QPixmap();
902 emit dataChanged();
903}
904
905void QgsSymbolLegendNode::updateLabel()
906{
907 if ( !mLayerNode )
908 return;
909
910 const bool showFeatureCount = mLayerNode->customProperty( QStringLiteral( "showFeatureCount" ), 0 ).toBool();
911 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
912 if ( !mLayerNode->labelExpression().isEmpty() )
913 mLabel = "[%" + mLayerNode->labelExpression() + "%]";
914 else
915 mLabel = symbolLabel();
916
917 if ( showFeatureCount && vl )
918 {
919 const bool estimatedCount = vl->dataProvider() ? QgsDataSourceUri( vl->dataProvider()->dataSourceUri() ).useEstimatedMetadata() : false;
920 const qlonglong count = mEmbeddedInParent ? vl->featureCount() : vl->featureCount( mItem.ruleKey() ) ;
921
922 // if you modify this line, please update QgsLayerTreeModel::data (DisplayRole)
923 mLabel += QStringLiteral( " [%1%2]" ).arg(
924 estimatedCount ? QStringLiteral( "≈" ) : QString(),
925 count != -1 ? QLocale().toString( count ) : tr( "N/A" ) );
926 }
927
928 emit dataChanged();
929}
930
931QString QgsSymbolLegendNode::evaluateLabel( const QgsExpressionContext &context, const QString &label )
932{
933 if ( !mLayerNode )
934 return QString();
935
936 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
937
938 if ( vl )
939 {
940 QgsExpressionContext contextCopy = QgsExpressionContext( context );
942 contextCopy.appendScope( symbolScope );
943 contextCopy.appendScope( vl->createExpressionContextScope() );
944
945 if ( label.isEmpty() )
946 {
947 const QString symLabel = symbolLabel();
948 if ( ! mLayerNode->labelExpression().isEmpty() )
949 mLabel = QgsExpression::replaceExpressionText( "[%" + mLayerNode->labelExpression() + "%]", &contextCopy );
950 else if ( symLabel.contains( "[%" ) )
951 mLabel = QgsExpression::replaceExpressionText( symLabel, &contextCopy );
952 return mLabel;
953 }
954 else
955 {
956 QString eLabel;
957 if ( ! mLayerNode->labelExpression().isEmpty() )
958 eLabel = QgsExpression::replaceExpressionText( label + "[%" + mLayerNode->labelExpression() + "%]", &contextCopy );
959 else if ( label.contains( "[%" ) )
960 eLabel = QgsExpression::replaceExpressionText( label, &contextCopy );
961 return eLabel;
962 }
963 }
964 return mLabel;
965}
966
968{
969 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
970
971 QgsExpressionContextScope *scope = new QgsExpressionContextScope( tr( "Symbol scope" ) );
972 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "symbol_label" ), symbolLabel().remove( "[%" ).remove( "%]" ), true ) );
973 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "symbol_id" ), mItem.ruleKey(), true ) );
974 if ( vl )
975 {
976 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "symbol_count" ), QVariant::fromValue( vl->featureCount( mItem.ruleKey() ) ), true ) );
977 }
978 return scope;
979}
980
981// -------------------------------------------------------------------------
982
983
984QgsSimpleLegendNode::QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon, QObject *parent, const QString &key )
985 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
986 , mLabel( label )
987 , mIcon( icon )
988 , mKey( key )
989{
990}
991
992QVariant QgsSimpleLegendNode::data( int role ) const
993{
994 if ( role == Qt::DisplayRole || role == Qt::EditRole )
995 return mUserLabel.isEmpty() ? mLabel : mUserLabel;
996 else if ( role == Qt::DecorationRole )
997 return mIcon;
998 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) && !mKey.isEmpty() )
999 return mKey;
1000 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
1002 else
1003 return QVariant();
1004}
1005
1006
1007// -------------------------------------------------------------------------
1008
1009QgsImageLegendNode::QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent )
1010 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
1011 , mImage( img )
1012{
1013}
1014
1015QVariant QgsImageLegendNode::data( int role ) const
1016{
1017 if ( role == Qt::DecorationRole )
1018 {
1019 return QPixmap::fromImage( mImage );
1020 }
1021 else if ( role == Qt::SizeHintRole )
1022 {
1023 return mImage.size();
1024 }
1025 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
1026 {
1028 }
1029 return QVariant();
1030}
1031
1032QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
1033{
1034 Q_UNUSED( itemHeight )
1035
1036 if ( ctx && ctx->painter && ctx->context )
1037 {
1038 const QgsScopedRenderContextScaleToPixels scopedScaleToPixels( *( ctx->context ) );
1039 const double scaleFactor = ctx->context->scaleFactor();
1040 const double imgWidth = settings.wmsLegendSize().width() * scaleFactor;
1041 const double imgHeight = settings.wmsLegendSize().height() * scaleFactor;
1042
1043 const QImage scaledImg = mImage.scaled( QSizeF( imgWidth, imgHeight ).toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation );
1044 switch ( settings.symbolAlignment() )
1045 {
1046 case Qt::AlignLeft:
1047 default:
1048 ctx->painter->drawImage( QPointF( ctx->columnLeft * scaleFactor, ctx->top * scaleFactor ), scaledImg );
1049 break;
1050
1051 case Qt::AlignRight:
1052 ctx->painter->drawImage( QPointF( ctx->columnRight * scaleFactor - imgWidth, ctx->top * scaleFactor ), scaledImg );
1053 break;
1054 }
1055 }
1056 return settings.wmsLegendSize();
1057}
1058
1060{
1061 QByteArray byteArray;
1062 QBuffer buffer( &byteArray );
1063 mImage.save( &buffer, "PNG" );
1064 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1065
1066 QJsonObject json;
1067 json[ QStringLiteral( "icon" ) ] = base64;
1068 return json;
1069}
1070
1071// -------------------------------------------------------------------------
1072
1073QgsRasterSymbolLegendNode::QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent, bool isCheckable, const QString &ruleKey, const QString &parentRuleKey )
1074 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
1075 , mColor( color )
1076 , mLabel( label )
1077 , mCheckable( isCheckable )
1078 , mRuleKey( ruleKey )
1079 , mParentRuleKey( parentRuleKey )
1080{
1081}
1082
1084{
1085 if ( mCheckable )
1086 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
1087 else
1088 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
1089}
1090
1091QVariant QgsRasterSymbolLegendNode::data( int role ) const
1092{
1093 switch ( role )
1094 {
1095 case Qt::DecorationRole:
1096 {
1097 const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 ); // TODO: configurable?
1098 QPixmap pix( iconSize, iconSize );
1099 pix.fill( mColor );
1100 return QIcon( pix );
1101 }
1102
1103 case Qt::DisplayRole:
1104 case Qt::EditRole:
1105 return mUserLabel.isEmpty() ? mLabel : mUserLabel;
1106
1107 case static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ):
1109
1110 case static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ):
1111 return mRuleKey;
1112
1114 return mParentRuleKey;
1115
1116 case Qt::CheckStateRole:
1117 {
1118 if ( !mCheckable )
1119 return QVariant();
1120
1121 if ( QgsPointCloudLayer *pclayer = qobject_cast<QgsPointCloudLayer *>( mLayerNode->layer() ) )
1122 {
1123 if ( !pclayer->renderer() )
1124 return QVariant();
1125
1126 return pclayer->renderer()->legendItemChecked( mRuleKey ) ? Qt::Checked : Qt::Unchecked;
1127 }
1128
1129 return QVariant();
1130 }
1131
1132 default:
1133 return QVariant();
1134 }
1135}
1136
1137bool QgsRasterSymbolLegendNode::setData( const QVariant &value, int role )
1138{
1139 if ( role != Qt::CheckStateRole )
1140 return false;
1141
1142 if ( !mCheckable )
1143 return false;
1144
1145 if ( QgsPointCloudLayer *pclayer = qobject_cast<QgsPointCloudLayer *>( mLayerNode->layer() ) )
1146 {
1147 if ( !pclayer->renderer() )
1148 return false;
1149
1150 pclayer->renderer()->checkLegendItem( mRuleKey, value == Qt::Checked );
1151
1152 emit dataChanged();
1153 pclayer->emitStyleChanged();
1154
1155 pclayer->triggerRepaint();
1156 if ( pclayer->sync3DRendererTo2DRenderer() )
1157 pclayer->convertRenderer3DFromRenderer2D();
1158
1159 return true;
1160 }
1161 else
1162 {
1163 return false;
1164 }
1165}
1166
1167
1168QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
1169{
1170 QSizeF size = settings.symbolSize();
1171 double offsetX = 0;
1172 if ( ctx )
1173 {
1174 if ( ctx->patchSize.width() > 0 )
1175 {
1176 if ( ctx->patchSize.width() < size.width() )
1177 offsetX = ( size.width() - ctx->patchSize.width() ) / 2.0;
1178 size.setWidth( ctx->patchSize.width() );
1179 }
1180 if ( ctx->patchSize.height() > 0 )
1181 {
1182 size.setHeight( ctx->patchSize.height() );
1183 }
1184 }
1185
1186 if ( ctx && ctx->painter )
1187 {
1188 QColor itemColor = mColor;
1189 if ( QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( layerNode()->layer() ) )
1190 {
1191 if ( QgsRasterRenderer *rasterRenderer = rasterLayer->renderer() )
1192 itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
1193 }
1194 ctx->painter->setBrush( itemColor );
1195
1196 if ( settings.drawRasterStroke() )
1197 {
1198 QPen pen;
1199 pen.setColor( settings.rasterStrokeColor() );
1200 pen.setWidthF( settings.rasterStrokeWidth() );
1201 pen.setJoinStyle( Qt::MiterJoin );
1202 ctx->painter->setPen( pen );
1203 }
1204 else
1205 {
1206 ctx->painter->setPen( Qt::NoPen );
1207 }
1208
1209 switch ( settings.symbolAlignment() )
1210 {
1211 case Qt::AlignLeft:
1212 default:
1213 ctx->painter->drawRect( QRectF( ctx->columnLeft + offsetX, ctx->top + ( itemHeight - size.height() ) / 2,
1214 size.width(), size.height() ) );
1215 break;
1216
1217 case Qt::AlignRight:
1218 ctx->painter->drawRect( QRectF( ctx->columnRight - size.width() - offsetX, ctx->top + ( itemHeight - size.height() ) / 2,
1219 size.width(), size.height() ) );
1220 break;
1221 }
1222 }
1223 return size;
1224}
1225
1227{
1228 QImage img = QImage( settings.symbolSize().toSize(), QImage::Format_ARGB32 );
1229 img.fill( Qt::transparent );
1230
1231 QPainter painter( &img );
1232 painter.setRenderHint( QPainter::Antialiasing );
1233
1234 QColor itemColor = mColor;
1235 if ( QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( layerNode()->layer() ) )
1236 {
1237 if ( QgsRasterRenderer *rasterRenderer = rasterLayer->renderer() )
1238 itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
1239 }
1240 painter.setBrush( itemColor );
1241
1242 if ( settings.drawRasterStroke() )
1243 {
1244 QPen pen;
1245 pen.setColor( settings.rasterStrokeColor() );
1246 pen.setWidthF( settings.rasterStrokeWidth() );
1247 pen.setJoinStyle( Qt::MiterJoin );
1248 painter.setPen( pen );
1249 }
1250 else
1251 {
1252 painter.setPen( Qt::NoPen );
1253 }
1254
1255 painter.drawRect( QRectF( 0, 0, settings.symbolSize().width(), settings.symbolSize().height() ) );
1256
1257 QByteArray byteArray;
1258 QBuffer buffer( &byteArray );
1259 img.save( &buffer, "PNG" );
1260 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1261
1262 QJsonObject json;
1263 json[ QStringLiteral( "icon" ) ] = base64;
1264 return json;
1265}
1266
1267// -------------------------------------------------------------------------
1268
1270 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
1271 , mValid( false )
1272{
1273}
1274
1276
1277QImage QgsWmsLegendNode::getLegendGraphic( bool synchronous ) const
1278{
1279 if ( ! mValid && ! mFetcher )
1280 {
1281 // or maybe in presence of a downloader we should just delete it
1282 // and start a new one ?
1283
1284 QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( mLayerNode->layer() );
1285
1286 if ( layer && layer->isValid() )
1287 {
1288 const QgsLayerTreeModel *mod = model();
1289 if ( ! mod )
1290 return mImage;
1291 const QgsMapSettings *ms = mod->legendFilterMapSettings();
1292
1293 QgsRasterDataProvider *prov = layer->dataProvider();
1294 if ( ! prov )
1295 return mImage;
1296
1297 Q_ASSERT( ! mFetcher );
1298 mFetcher.reset( prov->getLegendGraphicFetcher( ms ) );
1299 if ( mFetcher )
1300 {
1301 connect( mFetcher.get(), &QgsImageFetcher::finish, this, &QgsWmsLegendNode::getLegendGraphicFinished );
1302 connect( mFetcher.get(), &QgsImageFetcher::error, this, &QgsWmsLegendNode::getLegendGraphicErrored );
1303 connect( mFetcher.get(), &QgsImageFetcher::progress, this, &QgsWmsLegendNode::getLegendGraphicProgress );
1304 mFetcher->start();
1305 if ( synchronous )
1306 {
1307 QEventLoop loop;
1308 // The slots getLegendGraphicFinished and getLegendGraphicErrored will destroy the fetcher
1309 connect( mFetcher.get(), &QObject::destroyed, &loop, &QEventLoop::quit );
1310 loop.exec();
1311 }
1312 }
1313 }
1314 else
1315 {
1316 QgsDebugError( QStringLiteral( "Failed to download legend graphics: layer is not valid." ) );
1317 }
1318 }
1319
1320 return mImage;
1321}
1322
1323QVariant QgsWmsLegendNode::data( int role ) const
1324{
1325 if ( role == Qt::DecorationRole )
1326 {
1327 return QPixmap::fromImage( getLegendGraphic() );
1328 }
1329 else if ( role == Qt::SizeHintRole )
1330 {
1331 return getLegendGraphic().size();
1332 }
1333 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
1334 {
1336 }
1337 return QVariant();
1338}
1339
1340QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
1341{
1342 Q_UNUSED( itemHeight )
1343
1344 const QImage image = getLegendGraphic( settings.synchronousLegendRequests() );
1345
1346 double px2mm = 1000. / image.dotsPerMeterX();
1347 double mmWidth = image.width() * px2mm;
1348 double mmHeight = image.height() * px2mm;
1349
1350 QSize targetSize = QSize( mmWidth, mmHeight );
1351 if ( settings.wmsLegendSize().width() < mmWidth )
1352 {
1353 double targetHeight = mmHeight * settings.wmsLegendSize().width() / mmWidth;
1354 targetSize = QSize( settings.wmsLegendSize().width(), targetHeight );
1355 }
1356 else if ( settings.wmsLegendSize().height() < mmHeight )
1357 {
1358 double targetWidth = mmWidth * settings.wmsLegendSize().height() / mmHeight;
1359 targetSize = QSize( targetWidth, settings.wmsLegendSize().height() );
1360 }
1361
1362 if ( ctx && ctx->painter )
1363 {
1364 QImage smoothImage = image.scaled( targetSize / px2mm, Qt::KeepAspectRatio, Qt::SmoothTransformation );
1365
1366 switch ( settings.symbolAlignment() )
1367 {
1368 case Qt::AlignLeft:
1369 default:
1370 ctx->painter->drawImage( QRectF( ctx->columnLeft,
1371 ctx->top,
1372 targetSize.width(),
1373 targetSize.height() ),
1374 smoothImage,
1375 QRectF( QPointF( 0, 0 ), smoothImage.size() ) );
1376 break;
1377
1378 case Qt::AlignRight:
1379 ctx->painter->drawImage( QRectF( ctx->columnRight - settings.wmsLegendSize().width(),
1380 ctx->top,
1381 targetSize.width(),
1382 targetSize.height() ),
1383 smoothImage,
1384 QRectF( QPointF( 0, 0 ), smoothImage.size() ) );
1385 break;
1386 }
1387 }
1388 return targetSize;
1389}
1390
1392{
1393 QByteArray byteArray;
1394 QBuffer buffer( &byteArray );
1395 mImage.save( &buffer, "PNG" );
1396 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1397
1398 QJsonObject json;
1399 json[ QStringLiteral( "icon" ) ] = base64;
1400 return json;
1401}
1402
1403QImage QgsWmsLegendNode::renderMessage( const QString &msg ) const
1404{
1405 const int fontHeight = 10;
1406 const int margin = fontHeight / 2;
1407 const int nlines = 1;
1408
1409 const int w = 512, h = fontHeight * nlines + margin * ( nlines + 1 );
1410 QImage image( w, h, QImage::Format_ARGB32_Premultiplied );
1411 QPainter painter;
1412 painter.begin( &image );
1413 painter.setPen( QColor( 255, 0, 0 ) );
1414 painter.setFont( QFont( QStringLiteral( "Chicago" ), fontHeight ) );
1415 painter.fillRect( 0, 0, w, h, QColor( 255, 255, 255 ) );
1416 painter.drawText( 0, margin + fontHeight, msg );
1417 //painter.drawText(0,2*(margin+fontHeight),tr("retrying in 5 seconds…"));
1418 painter.end();
1419
1420 return image;
1421}
1422
1423void QgsWmsLegendNode::getLegendGraphicProgress( qint64 cur, qint64 tot )
1424{
1425 const QString msg = tot > 0 ? tr( "Downloading: %1% (%2)" ).arg( static_cast< int >( std::round( 100 * cur / tot ) ) ).arg( QgsFileUtils::representFileSize( tot ) )
1426 : tr( "Downloading: %1" ).arg( QgsFileUtils::representFileSize( cur ) );
1427 mImage = renderMessage( msg );
1428 emit dataChanged();
1429}
1430
1431void QgsWmsLegendNode::getLegendGraphicErrored( const QString & )
1432{
1433 if ( ! mFetcher )
1434 return; // must be coming after finish
1435
1436 mImage = QImage();
1437 emit dataChanged();
1438
1439 mFetcher.reset();
1440
1441 mValid = true; // we consider it valid anyway
1442}
1443
1444void QgsWmsLegendNode::getLegendGraphicFinished( const QImage &image )
1445{
1446 if ( ! mFetcher )
1447 return; // must be coming after error
1448
1449 if ( ! image.isNull() )
1450 {
1451 if ( image != mImage )
1452 {
1453 mImage = image;
1454 setUserPatchSize( mImage.size() );
1455 emit dataChanged();
1456 }
1457 mValid = true; // only if not null I guess
1458 }
1459 mFetcher.reset();
1460}
1461
1463{
1464 // TODO: do this only if this extent != prev extent ?
1465 mValid = false;
1466 emit dataChanged();
1467}
1468
1470{
1471 return getLegendGraphic( true );
1472}
1473
1474// -------------------------------------------------------------------------
1475
1477 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
1478 , mSettings( new QgsDataDefinedSizeLegend( settings ) )
1479{
1480}
1481
1486
1487QVariant QgsDataDefinedSizeLegendNode::data( int role ) const
1488{
1489 if ( role == Qt::DecorationRole )
1490 {
1491 cacheImage();
1492 return QPixmap::fromImage( mImage );
1493 }
1494 else if ( role == Qt::SizeHintRole )
1495 {
1496 cacheImage();
1497 return mImage.size();
1498 }
1499 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
1500 {
1502 }
1503 return QVariant();
1504}
1505
1507{
1508 // setup temporary render context if none specified
1509 QgsRenderContext *context = nullptr;
1510 std::unique_ptr< QgsRenderContext > tempRenderContext;
1511 if ( ctx && ctx->context )
1512 context = ctx->context;
1513 else
1514 {
1515 tempRenderContext = std::make_unique< QgsRenderContext >();
1516 // QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
1518 tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
1519 tempRenderContext->setRendererScale( settings.mapScale() );
1520 tempRenderContext->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
1521 tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
1522 tempRenderContext->setForceVectorOutput( true );
1523 tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
1524 tempRenderContext->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
1526
1527 // setup a minimal expression context
1528 QgsExpressionContext expContext;
1530 tempRenderContext->setExpressionContext( expContext );
1531 context = tempRenderContext.get();
1532 }
1533
1534 if ( context->painter() )
1535 {
1536 context->painter()->save();
1537 context->painter()->translate( ctx->columnLeft, ctx->top );
1538
1539 // scale to pixels
1540 context->painter()->scale( 1 / context->scaleFactor(), 1 / context->scaleFactor() );
1541 }
1542
1543 QgsDataDefinedSizeLegend ddsLegend( *mSettings );
1544 ddsLegend.setFont( settings.style( QgsLegendStyle::SymbolLabel ).textFormat().toQFont() );
1545 ddsLegend.setTextColor( settings.style( QgsLegendStyle::SymbolLabel ).textFormat().color() );
1546
1547 QSizeF contentSize;
1548 double labelXOffset;
1549 ddsLegend.drawCollapsedLegend( *context, &contentSize, &labelXOffset );
1550
1551 if ( context->painter() )
1552 context->painter()->restore();
1553
1554 ItemMetrics im;
1555 im.symbolSize = QSizeF( ( contentSize.width() - labelXOffset ) / context->scaleFactor(), contentSize.height() / context->scaleFactor() );
1556 im.labelSize = QSizeF( labelXOffset / context->scaleFactor(), contentSize.height() / context->scaleFactor() );
1557 return im;
1558}
1559
1560
1561void QgsDataDefinedSizeLegendNode::cacheImage() const
1562{
1563 if ( mImage.isNull() )
1564 {
1565 std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
1566 if ( !context )
1567 {
1568 context.reset( new QgsRenderContext );
1569 Q_ASSERT( context ); // to make cppcheck happy
1570 context->setScaleFactor( 96 / 25.4 );
1571 }
1572 mImage = mSettings->collapsedLegendImage( *context );
1573 }
1574}
1575
1576QgsVectorLabelLegendNode::QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent ): QgsLayerTreeModelLegendNode( nodeLayer, parent ), mLabelSettings( labelSettings )
1577{
1578}
1579
1583
1584QVariant QgsVectorLabelLegendNode::data( int role ) const
1585{
1586 if ( role == Qt::DisplayRole )
1587 {
1588 return mUserLabel;
1589 }
1590 if ( role == Qt::DecorationRole )
1591 {
1592 const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 );
1593 return QgsPalLayerSettings::labelSettingsPreviewPixmap( mLabelSettings, QSize( iconSize, iconSize ), mLabelSettings.legendString() );
1594 }
1595 return QVariant();
1596}
1597
1598QSizeF QgsVectorLabelLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
1599{
1600 Q_UNUSED( itemHeight );
1601 if ( !ctx )
1602 {
1603 return QSizeF( 0, 0 );
1604 }
1605
1606 const QgsRenderContext *renderContext = ctx->context;
1607 if ( renderContext )
1608 {
1609 return drawSymbol( settings, *renderContext, ctx->columnLeft, ctx->top );
1610 }
1611
1612 return QSizeF( 0, 0 );
1613}
1614
1615QSizeF QgsVectorLabelLegendNode::drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset, double yOffset ) const
1616{
1617 const QStringList textLines( mLabelSettings.legendString() );
1618 const QgsTextFormat textFormat = mLabelSettings.format();
1619 QgsRenderContext ctx( renderContext );
1620 double textWidth, textHeight;
1621 textWidthHeight( textWidth, textHeight, ctx, textFormat, textLines );
1622 textWidth /= renderContext.scaleFactor();
1623 textHeight /= renderContext.scaleFactor();
1624 const QPointF textPos( renderContext.scaleFactor() * ( xOffset + settings.symbolSize().width() / 2.0 - textWidth / 2.0 ), renderContext.scaleFactor() * ( yOffset + settings.symbolSize().height() / 2.0 + textHeight / 2.0 ) );
1625
1626 const QgsScopedRenderContextScaleToPixels scopedScaleToPixels( ctx );
1627 QgsTextRenderer::drawText( textPos, 0.0, Qgis::TextHorizontalAlignment::Left, textLines, ctx, textFormat );
1628
1629 const double symbolWidth = std::max( textWidth, settings.symbolSize().width() );
1630 const double symbolHeight = std::max( textHeight, settings.symbolSize().height() );
1631 return QSizeF( symbolWidth, symbolHeight );
1632}
1633
1634QJsonObject QgsVectorLabelLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const
1635{
1636 Q_UNUSED( settings );
1637
1638 const double mmToPixel = 96.0 / 25.4; //settings.dpi() is deprecated
1639
1640 const QStringList textLines( mLabelSettings.legendString() );
1641 const QgsTextFormat textFormat = mLabelSettings.format();
1642 QgsRenderContext ctx( context );
1643 ctx.setScaleFactor( mmToPixel );
1644
1645 double textWidth, textHeight;
1646 textWidthHeight( textWidth, textHeight, ctx, textFormat, textLines );
1647 const QPixmap previewPixmap = QgsPalLayerSettings::labelSettingsPreviewPixmap( mLabelSettings, QSize( textWidth, textHeight ), mLabelSettings.legendString() );
1648
1649 QByteArray byteArray;
1650 QBuffer buffer( &byteArray );
1651 previewPixmap.save( &buffer, "PNG" );
1652 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1653
1654 QJsonObject json;
1655 json[ QStringLiteral( "icon" ) ] = base64;
1656 return json;
1657}
1658
1659void QgsVectorLabelLegendNode::textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const
1660{
1661 QFontMetricsF fm = QgsTextRenderer::fontMetrics( ctx, textFormat );
1662 height = QgsTextRenderer::textHeight( ctx, textFormat, 'A', true );
1663 width = QgsTextRenderer::textWidth( ctx, textFormat, textLines, &fm );
1664}
@ 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.
@ RenderSymbolPreview
The render is for a symbol preview only and map based properties may not be available,...
@ LosslessImageRendering
Render images losslessly whenever possible, instead of the default lossy jpeg rendering used for some...
@ Antialiasing
Use antialiasing while drawing.
@ UseAdvancedEffects
Enable layer opacity and blending effects.
@ Marker
Marker symbol.
@ Line
Line symbol.
TextHorizontalAlignment
Text horizontal alignment.
Definition qgis.h:2732
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.
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.
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...
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.
Class for storing the component parts of a RDBMS 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.
QgsLegendPatchShape patchShape() const
Returns the symbol patch shape to use when rendering the legend node symbol.
QString name() const override
Returns the layer's name.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
QSizeF patchSize() const
Returns the user (overridden) size for the legend node.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
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.
@ 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.
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.
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.
virtual QSizeF userPatchSize() const
Returns the user (overridden) size for the legend node.
virtual ItemMetrics draw(const QgsLegendSettings &settings, ItemContext *ctx)
Entry point called from QgsLegendRenderer to do the rendering.
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.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
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.
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
QgsLegendStyle style(QgsLegendStyle::Style s) const
Returns the style for a legend component.
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 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
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.
@ Right
Right side.
@ Left
Left side.
double margin(Side side) const
Returns the margin (in mm) for the specified side of the component.
@ Symbol
Symbol icon (excluding label)
@ SymbolLabel
Symbol label (excluding icon)
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
QString parentRuleKey() const
Key of the parent legend node.
int scaleMaxDenom() const
Max scale denominator of the scale range.
void setSymbol(QgsSymbol *s)
Sets the symbol of the item.
int scaleMinDenom() const
Min scale denominator of the scale range.
QgsSymbol * symbol() const
Returns associated symbol. May be nullptr.
QString ruleKey() const
Returns unique identifier of the rule for identification of the item within renderer.
bool isCheckable() const
Returns whether the item is user-checkable - whether renderer supports enabling/disabling it.
QString label() const
Returns text label.
Base class for all map layer types.
Definition qgsmaplayer.h:76
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.
The QgsMapSettings class contains configuration for rendering of the map.
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,...
Definition qgsproject.h:107
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.
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.
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.
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.
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...
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.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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 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 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 double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
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.
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.
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.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
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.
Definition qgssymbol.h:231
bool usesMapUnits() const
Returns true if the symbol has any components which use map unit based sizes.
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.
Definition qgssymbol.h:293
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...
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 scaledFont(const QgsRenderContext &context, double scaleFactor=1.0, bool *isZeroSize=nullptr) const
Returns a font with the size scaled to match the format's size settings (including units and map unit...
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
QVariant data(int role) const override
data Returns data associated with the item
Represents a vector layer which manages a vector based data sets.
QgsExpressionContextScope * createExpressionContextScope() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
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.
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
Definition qgis.h:6494
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6493
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5917
QList< QgsLegendSymbolItem > QgsLegendSymbolList
#define QgsDebugError(str)
Definition qgslogger.h:38
Single variable definition for use within a QgsExpressionContextScope.
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.