QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
22#include "qgslayertreemodel.h"
23#include "qgslegendsettings.h"
24#include "qgsrasterlayer.h"
25#include "qgsrenderer.h"
26#include "qgssymbollayerutils.h"
27#include "qgsimageoperation.h"
28#include "qgsvectorlayer.h"
29#include "qgspointcloudlayer.h"
31#include "qgsrasterrenderer.h"
33#include "qgsexpression.h"
34#include "qgstextrenderer.h"
35#include "qgssettings.h"
36#include "qgsfileutils.h"
37#include "qgsmarkersymbol.h"
38#include "qgsvariantutils.h"
39#include "qgslayertreelayer.h"
41
42#include <QBuffer>
43
45 : QObject( parent )
46 , mLayerNode( nodeL )
47 , mEmbeddedInParent( false )
48{
49}
50
52{
53 return qobject_cast<QgsLayerTreeModel *>( parent() );
54}
55
57{
58 return Qt::ItemIsEnabled;
59}
60
61bool QgsLayerTreeModelLegendNode::setData( const QVariant &value, int role )
62{
63 Q_UNUSED( value )
64 Q_UNUSED( role )
65 return false;
66}
67
69{
71 return mLayerNode->patchSize();
72
73 return mUserSize;
74}
75
77{
78 if ( mUserSize == size )
79 return;
80
81 mUserSize = size;
82 emit sizeChanged();
83}
84
86{
87 const QFont symbolLabelFont = settings.style( QgsLegendStyle::SymbolLabel ).font();
88
89 const double textHeight = settings.fontHeightCharacterMM( symbolLabelFont, QChar( '0' ) );
90 // itemHeight here is not really item height, it is only for symbol
91 // vertical alignment purpose, i.e. OK take single line height
92 // if there are more lines, those run under the symbol
93 const double itemHeight = std::max( static_cast< double >( ctx && ctx->patchSize.height() > 0 ? ctx->patchSize.height() : settings.symbolSize().height() ), textHeight );
94
95 ItemMetrics im;
96 im.symbolSize = drawSymbol( settings, ctx, itemHeight );
97 im.labelSize = drawSymbolText( settings, ctx, im.symbolSize );
98 return im;
99}
100
102{
103 QJsonObject json = exportSymbolToJson( settings, context );
104 const QString text = data( Qt::DisplayRole ).toString();
105 json[ QStringLiteral( "title" ) ] = text;
106 return json;
107}
108
109QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
110{
111 const QIcon symbolIcon = data( Qt::DecorationRole ).value<QIcon>();
112 if ( symbolIcon.isNull() )
113 return QSizeF();
114
115 QSizeF size = settings.symbolSize();
116 if ( ctx )
117 {
118 if ( ctx->patchSize.width() > 0 )
119 size.setWidth( ctx->patchSize.width( ) );
120 if ( ctx->patchSize.height() > 0 )
121 size.setHeight( ctx->patchSize.height( ) );
122 }
123
124 if ( ctx && ctx->painter )
125 {
126 switch ( settings.symbolAlignment() )
127 {
128 case Qt::AlignLeft:
129 default:
130 symbolIcon.paint( ctx->painter,
131 static_cast< int >( ctx->columnLeft ),
132 static_cast< int >( ctx->top + ( itemHeight - size.height() ) / 2 ),
133 static_cast< int >( size.width() ),
134 static_cast< int >( size.height() ) );
135 break;
136
137 case Qt::AlignRight:
138 symbolIcon.paint( ctx->painter,
139 static_cast< int >( ctx->columnRight - size.width() ),
140 static_cast< int >( ctx->top + ( itemHeight - size.height() ) / 2 ),
141 static_cast< int >( size.width() ),
142 static_cast< int >( size.height() ) );
143 break;
144 }
145 }
146 return size;
147}
148
150{
151 const QIcon icon = data( Qt::DecorationRole ).value<QIcon>();
152 if ( icon.isNull() )
153 return QJsonObject();
154
155 const QImage image( icon.pixmap( settings.symbolSize().width(), settings.symbolSize().height() ).toImage() );
156 QByteArray byteArray;
157 QBuffer buffer( &byteArray );
158 image.save( &buffer, "PNG" );
159 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
160
161 QJsonObject json;
162 json[ QStringLiteral( "icon" ) ] = base64;
163 return json;
164}
165
166QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const
167{
168 QSizeF labelSize( 0, 0 );
169
170 const QFont symbolLabelFont = settings.style( QgsLegendStyle::SymbolLabel ).font();
171 const double textHeight = settings.fontHeightCharacterMM( symbolLabelFont, QChar( '0' ) );
172 const double textDescent = settings.fontDescentMillimeters( symbolLabelFont );
173
174 const QgsExpressionContext tempContext;
175
176 const QStringList lines = settings.evaluateItemText( data( Qt::DisplayRole ).toString(), ctx && ctx->context ? ctx->context->expressionContext() : tempContext );
177
178 labelSize.rheight() = lines.count() * textHeight + ( lines.count() - 1 ) * ( settings.lineSpacing() + textDescent );
179
180 double labelXMin = 0.0;
181 double labelXMax = 0.0;
182 double labelY = 0.0;
183 if ( ctx && ctx->painter )
184 {
185 ctx->painter->setPen( settings.fontColor() );
186 switch ( settings.symbolAlignment() )
187 {
188 case Qt::AlignLeft:
189 default:
190 labelXMin = ctx->columnLeft + std::max( static_cast< double >( symbolSize.width() ), ctx->maxSiblingSymbolWidth )
193 labelXMax = ctx->columnRight;
194 break;
195
196 case Qt::AlignRight:
197 labelXMin = ctx->columnLeft;
198 // NOTE -- while the below calculations use the flipped margins from the style, that's only done because
199 // those are the only margins we expose and use for now! (and we expose them as generic margins, not side-specific
200 // ones) TODO when/if we expose other margin settings, these should be reversed...
201 labelXMax = ctx->columnRight - std::max( static_cast< double >( symbolSize.width() ), ctx->maxSiblingSymbolWidth )
204 break;
205 }
206
207 labelY = ctx->top;
208
209 // Vertical alignment of label with symbol
210 if ( labelSize.height() < symbolSize.height() )
211 labelY += symbolSize.height() / 2 - labelSize.height() / 2; // label centered with symbol
212
213 labelY += textHeight;
214 }
215
216 for ( QStringList::ConstIterator itemPart = lines.constBegin(); itemPart != lines.constEnd(); ++itemPart )
217 {
218 const double lineWidth = settings.textWidthMillimeters( symbolLabelFont, *itemPart );
219 labelSize.rwidth() = std::max( lineWidth, double( labelSize.width() ) );
220
221 if ( ctx && ctx->painter )
222 {
223 switch ( settings.style( QgsLegendStyle::SymbolLabel ).alignment() )
224 {
225 case Qt::AlignLeft:
226 default:
227 settings.drawText( ctx->painter, labelXMin, labelY, *itemPart, symbolLabelFont );
228 break;
229
230 case Qt::AlignRight:
231 settings.drawText( ctx->painter, labelXMax - lineWidth, labelY, *itemPart, symbolLabelFont );
232 break;
233
234 case Qt::AlignHCenter:
235 settings.drawText( ctx->painter, labelXMin + ( labelXMax - labelXMin - lineWidth ) / 2.0, labelY, *itemPart, symbolLabelFont );
236 break;
237 }
238
239 if ( itemPart != ( lines.end() - 1 ) )
240 labelY += textDescent + settings.lineSpacing() + textHeight;
241 }
242 }
243
244 return labelSize;
245}
246
248{
249 checkAll( true );
250}
251
253{
254 checkAll( false );
255}
256
258{
259 if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() ) )
260 {
261 if ( !vlayer->renderer() )
262 return;
263
264 const QgsLegendSymbolList symbolList = vlayer->renderer()->legendSymbolItems();
265 for ( const auto &item : symbolList )
266 {
267 vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), ! vlayer->renderer()->legendSymbolItemChecked( item.ruleKey() ) );
268 }
269
270 emit dataChanged();
271 vlayer->emitStyleChanged();
272 vlayer->triggerRepaint();
273 }
274 else if ( QgsPointCloudLayer *pclayer = qobject_cast<QgsPointCloudLayer *>( mLayerNode->layer() ) )
275 {
276 if ( !pclayer->renderer() )
277 return;
278
279 const QStringList ruleKeys = pclayer->renderer()->legendRuleKeys();
280 for ( const QString &rule : ruleKeys )
281 {
282 pclayer->renderer()->checkLegendItem( rule, !pclayer->renderer()->legendItemChecked( rule ) );
283 }
284
285 emit dataChanged();
286 pclayer->emitStyleChanged();
287 pclayer->triggerRepaint();
288 }
289}
290
291// -------------------------------------------------------------------------
292
295
297 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
298 , mItem( item )
299 , mSymbolUsesMapUnits( false )
300{
302 mIconSize = QSize( iconSize, iconSize );
303
304 if ( MINIMUM_SIZE < 0 )
305 {
306 // it's FAR too expensive to construct a QgsSettings object for every symbol node, especially for complex
307 // projects. So only read the valid size ranges once, and store them for subsequent use
308 const QgsSettings settings;
309 MINIMUM_SIZE = settings.value( "/qgis/legendsymbolMinimumSize", 0.5 ).toDouble();
310 MAXIMUM_SIZE = settings.value( "/qgis/legendsymbolMaximumSize", 20.0 ).toDouble();
311 }
312
313 updateLabel();
314 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( nodeLayer->layer() ) )
315 connect( vl, &QgsVectorLayer::symbolFeatureCountMapChanged, this, &QgsSymbolLegendNode::updateLabel );
316
317 connect( nodeLayer, &QObject::destroyed, this, [ = ]() { mLayerNode = nullptr; } );
318
319 if ( const QgsSymbol *symbol = mItem.symbol() )
320 {
321 mSymbolUsesMapUnits = symbol->usesMapUnits();
322 }
323}
324
326
327Qt::ItemFlags QgsSymbolLegendNode::flags() const
328{
329 if ( mItem.isCheckable() )
330 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
331 else
332 return Qt::ItemIsEnabled;
333}
334
335
337{
338 const std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
339 return minimumIconSize( context.get() );
340}
341
343{
345 const int largeIconSize = QgsLayerTreeModel::scaleIconSize( 512 );
346 QSize minSz( iconSize, iconSize );
347 if ( mItem.symbol() && ( mItem.symbol()->type() == Qgis::SymbolType::Marker
348 || mItem.symbol()->type() == Qgis::SymbolType::Line ) )
349 {
350 int maxSize = largeIconSize;
351
352 // unusued width, height variables
353 double width = 0.0;
354 double height = 0.0;
355 bool ok;
356 std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), MINIMUM_SIZE, MAXIMUM_SIZE, context, width, height, &ok ) );
357
358 if ( !ok && context )
359 {
360 // It's not possible to get a restricted size symbol, so we restrict
361 // pixmap target size to be sure it would fit MAXIMUM_SIZE
362 maxSize = static_cast<int>( std::round( MAXIMUM_SIZE * context->scaleFactor() ) );
363 }
364
365 const QSize size( mItem.symbol()->type() == Qgis::SymbolType::Marker ? maxSize : minSz.width(),
366 maxSize );
367
370 context ).toImage(),
371 minSz,
372 true ).size();
373 }
374
375 if ( !mTextOnSymbolLabel.isEmpty() && context )
376 {
377 const double w = QgsTextRenderer::textWidth( *context, mTextOnSymbolTextFormat, QStringList() << mTextOnSymbolLabel );
378 const double h = QgsTextRenderer::textHeight( *context, mTextOnSymbolTextFormat, QStringList() << mTextOnSymbolLabel, Qgis::TextLayoutMode::Point );
379 int wInt = ceil( w ), hInt = ceil( h );
380 if ( wInt > minSz.width() ) minSz.setWidth( wInt );
381 if ( hInt > minSz.height() ) minSz.setHeight( hInt );
382 }
383
384 return minSz;
385}
386
388{
389 return mItem.symbol();
390}
391
393{
394 QString label;
395 if ( mEmbeddedInParent )
396 {
397 const QVariant legendlabel = mLayerNode->customProperty( QStringLiteral( "legend/title-label" ) );
398 const QString layerName = QgsVariantUtils::isNull( legendlabel ) ? mLayerNode->name() : legendlabel.toString();
399 label = mUserLabel.isEmpty() ? layerName : mUserLabel;
400 }
401 else
402 label = mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
403 return label;
404}
405
407{
408 if ( mEmbeddedInParent )
409 {
410 return mLayerNode->patchShape();
411 }
412 else
413 {
414 return mPatchShape;
415 }
416}
417
419{
420 mPatchShape = shape;
421}
422
424{
425 return mCustomSymbol.get();
426}
427
429{
430 mCustomSymbol.reset( symbol );
431}
432
434{
435 if ( !symbol )
436 return;
437
438 std::unique_ptr< QgsSymbol > s( symbol ); // this method takes ownership of symbol
439 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
440 if ( !vlayer || !vlayer->renderer() )
441 return;
442
443 mItem.setSymbol( s.get() ); // doesn't transfer ownership
444 vlayer->renderer()->setLegendSymbolItem( mItem.ruleKey(), s.release() ); // DOES transfer ownership!
445
446 mPixmap = QPixmap();
447
448 emit dataChanged();
449 vlayer->triggerRepaint();
450}
451
453{
454 double scale = 0.0;
455 double mupp = 0.0;
456 int dpi = 0;
457 if ( auto *lModel = model() )
458 lModel->legendMapViewData( &mupp, &dpi, &scale );
459
460 if ( qgsDoubleNear( mupp, 0.0 ) || dpi == 0 || qgsDoubleNear( scale, 0.0 ) )
461 return nullptr;
462
463 // setup temporary render context
464 std::unique_ptr<QgsRenderContext> context = std::make_unique<QgsRenderContext>( );
465 context->setScaleFactor( dpi / 25.4 );
466 context->setRendererScale( scale );
467 context->setMapToPixel( QgsMapToPixel( mupp ) );
468 context->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
469 context->setFlag( Qgis::RenderContextFlag::RenderSymbolPreview, true );
470
471 QgsExpressionContext expContext;
473 context->setExpressionContext( expContext );
474
475 return context.release();
476}
477
478void QgsLayerTreeModelLegendNode::checkAll( bool state )
479{
480 if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() ) )
481 {
482 if ( !vlayer->renderer() )
483 return;
484
485 const QgsLegendSymbolList symbolList = vlayer->renderer()->legendSymbolItems();
486 for ( const auto &item : symbolList )
487 {
488 vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), state );
489 }
490
491 emit dataChanged();
492 vlayer->emitStyleChanged();
493 vlayer->triggerRepaint();
494 }
495 else if ( QgsPointCloudLayer *pclayer = qobject_cast<QgsPointCloudLayer *>( mLayerNode->layer() ) )
496 {
497 if ( !pclayer->renderer() )
498 return;
499
500 const QStringList ruleKeys = pclayer->renderer()->legendRuleKeys();
501 for ( const QString &rule : ruleKeys )
502 {
503 pclayer->renderer()->checkLegendItem( rule, state );
504 }
505
506 emit dataChanged();
507 pclayer->emitStyleChanged();
508 pclayer->triggerRepaint();
509 }
510}
511
512QVariant QgsSymbolLegendNode::data( int role ) const
513{
514 if ( role == Qt::DisplayRole )
515 {
516 return mLabel;
517 }
518 else if ( role == Qt::EditRole )
519 {
520 return mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
521 }
522 else if ( role == Qt::DecorationRole )
523 {
524 if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
525 {
526 QPixmap pix;
527 if ( mItem.symbol() )
528 {
529 std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
530
531 // unusued width, height variables
532 double width = 0.0;
533 double height = 0.0;
534 const std::unique_ptr<QgsSymbol> symbol( QgsSymbolLayerUtils::restrictedSizeSymbol( mItem.symbol(), MINIMUM_SIZE, MAXIMUM_SIZE, context.get(), width, height ) );
535 pix = QgsSymbolLayerUtils::symbolPreviewPixmap( symbol ? symbol.get() : mItem.symbol(), mIconSize, 0, context.get() );
536
537 if ( !mTextOnSymbolLabel.isEmpty() && context )
538 {
539 QPainter painter( &pix );
540 painter.setRenderHint( QPainter::Antialiasing );
541 context->setPainter( &painter );
542 bool isNullSize = false;
543 const QFontMetricsF fm( mTextOnSymbolTextFormat.scaledFont( *context, 1.0, &isNullSize ) );
544 if ( !isNullSize )
545 {
546 const qreal yBaselineVCenter = ( mIconSize.height() + fm.ascent() - fm.descent() ) / 2;
547 QgsTextRenderer::drawText( QPointF( mIconSize.width() / 2, yBaselineVCenter ), 0, Qgis::TextHorizontalAlignment::Center,
548 QStringList() << mTextOnSymbolLabel, *context, mTextOnSymbolTextFormat );
549 }
550 }
551 }
552 else
553 {
554 pix = QPixmap( mIconSize );
555 pix.fill( Qt::transparent );
556 }
557
558 mPixmap = pix;
559 }
560 return mPixmap;
561 }
562 else if ( role == Qt::CheckStateRole )
563 {
564 if ( !mItem.isCheckable() )
565 return QVariant();
566
567 if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() ) )
568 {
569 if ( !vlayer->renderer() )
570 return QVariant();
571
572 return vlayer->renderer()->legendSymbolItemChecked( mItem.ruleKey() ) ? Qt::Checked : Qt::Unchecked;
573 }
574 }
575 else if ( role == RuleKeyRole )
576 {
577 return mItem.ruleKey();
578 }
579 else if ( role == ParentRuleKeyRole )
580 {
581 return mItem.parentRuleKey();
582 }
584 {
586 }
587
588 return QVariant();
589}
590
591bool QgsSymbolLegendNode::setData( const QVariant &value, int role )
592{
593 if ( role != Qt::CheckStateRole )
594 return false;
595
596 if ( !mItem.isCheckable() )
597 return false;
598
599 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
600 if ( !vlayer || !vlayer->renderer() )
601 return false;
602
603 vlayer->renderer()->checkLegendSymbolItem( mItem.ruleKey(), value == Qt::Checked );
604
605 emit dataChanged();
606 vlayer->emitStyleChanged();
607
608 vlayer->triggerRepaint();
609
610 return true;
611}
612
613
614
615QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
616{
617 QgsSymbol *s = mCustomSymbol ? mCustomSymbol.get() : mItem.symbol();
618 if ( !s )
619 {
620 return QSizeF();
621 }
622
623 // setup temporary render context
624 QgsRenderContext *context = nullptr;
625 std::unique_ptr< QgsRenderContext > tempRenderContext;
627 if ( ctx && ctx->context )
628 context = ctx->context;
629 else
630 {
631 tempRenderContext = std::make_unique< QgsRenderContext >();
632 // QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
634 tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
635 tempRenderContext->setRendererScale( settings.mapScale() );
636 tempRenderContext->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
637 tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
639 tempRenderContext->setForceVectorOutput( true );
640 tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
641
642 // setup a minimal expression context
643 QgsExpressionContext expContext;
645 tempRenderContext->setExpressionContext( expContext );
646 context = tempRenderContext.get();
647 }
648
649 //Consider symbol size for point markers
650 const double desiredHeight = ctx && ctx->patchSize.height() > 0 ? ctx->patchSize.height() : settings.symbolSize().height();
651 const double desiredWidth = ctx && ctx->patchSize.width() > 0 ? ctx->patchSize.width() : settings.symbolSize().width();
652 double height = desiredHeight;
653 double width = desiredWidth;
654
655 //Center small marker symbols
656 double widthOffset = 0;
657 double heightOffset = 0;
658
659 const double maxSymbolSize = settings.maximumSymbolSize();
660 const double minSymbolSize = settings.minimumSymbolSize();
661
662 if ( QgsMarkerSymbol *markerSymbol = dynamic_cast<QgsMarkerSymbol *>( s ) )
663 {
664 const double size = markerSymbol->size( *context ) / context->scaleFactor();
665 height = size;
666 width = size;
667 }
668
669 const std::unique_ptr<QgsSymbol> minMaxSizeSymbol( QgsSymbolLayerUtils::restrictedSizeSymbol( s, minSymbolSize, maxSymbolSize, context, width, height ) );
670 if ( minMaxSizeSymbol )
671 {
672 s = minMaxSizeSymbol.get();
673 }
674
675 if ( s->type() == Qgis::SymbolType::Marker )
676 {
677 if ( width < desiredWidth )
678 {
679 widthOffset = ( desiredWidth - width ) / 2.0;
680 }
681 if ( height < desiredHeight )
682 {
683 heightOffset = ( desiredHeight - height ) / 2.0;
684 }
685 }
686 if ( ctx && ctx->painter )
687 {
688 const double currentYCoord = ctx->top + ( itemHeight - desiredHeight ) / 2;
689 QPainter *p = ctx->painter;
690
691 //setup painter scaling to dots so that raster symbology is drawn to scale
692 const double dotsPerMM = context->scaleFactor();
693
694 int opacity = 255;
695 if ( QgsMapLayer *layer = layerNode()->layer() )
696 opacity = static_cast<int >( std::round( 255 * layer->opacity() ) );
697
698 const QgsScopedQPainterState painterState( p );
699 context->setPainterFlagsUsingContext( p );
700
701 switch ( settings.symbolAlignment() )
702 {
703 case Qt::AlignLeft:
704 default:
705 p->translate( ctx->columnLeft + widthOffset, currentYCoord + heightOffset );
706 break;
707 case Qt::AlignRight:
708 p->translate( ctx->columnRight - widthOffset - width, currentYCoord + heightOffset );
709 break;
710 }
711
712 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
714 // QGIS 4.0 -- ctx->context will be mandatory
715 const bool useAdvancedEffects = ctx->context ? ctx->context->flags() & Qgis::RenderContextFlag::UseAdvancedEffects : settings.useAdvancedEffects();
717 if ( opacity != 255 && useAdvancedEffects )
718 {
719 const int maxBleed = static_cast< int >( std::ceil( QgsSymbolLayerUtils::estimateMaxSymbolBleed( s, *context ) ) );
720
721 //semi transparent layer, so need to draw symbol to an image (to flatten it first)
722 //create image which is same size as legend rect, in case symbol bleeds outside its allotted space
723 const QSize symbolSize( static_cast< int >( std::round( width * dotsPerMM ) ), static_cast<int >( std::round( height * dotsPerMM ) ) );
724 const QSize tempImageSize( symbolSize.width() + maxBleed * 2, symbolSize.height() + maxBleed * 2 );
725 QImage tempImage = QImage( tempImageSize, QImage::Format_ARGB32 );
726 tempImage.fill( Qt::transparent );
727 QPainter imagePainter( &tempImage );
728 context->setPainterFlagsUsingContext( &imagePainter );
729
730 context->setPainter( &imagePainter );
731 imagePainter.translate( maxBleed, maxBleed );
732 s->drawPreviewIcon( &imagePainter, symbolSize, context, false, nullptr, &patchShape );
733 imagePainter.translate( -maxBleed, -maxBleed );
734 context->setPainter( ctx->painter );
735 //reduce opacity of image
736 imagePainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
737 imagePainter.fillRect( tempImage.rect(), QColor( 0, 0, 0, opacity ) );
738 imagePainter.end();
739 //draw rendered symbol image
740 p->drawImage( -maxBleed, -maxBleed, tempImage );
741 }
742 else
743 {
744 s->drawPreviewIcon( p, QSize( static_cast< int >( std::round( width * dotsPerMM ) ), static_cast< int >( std::round( height * dotsPerMM ) ) ), context, false, nullptr, &patchShape );
745 }
746
747 if ( !mTextOnSymbolLabel.isEmpty() )
748 {
749 bool isNullSize = false;
750 const QFontMetricsF fm( mTextOnSymbolTextFormat.scaledFont( *context, 1.0, &isNullSize ) );
751 if ( !isNullSize )
752 {
753 const qreal yBaselineVCenter = ( height * dotsPerMM + fm.ascent() - fm.descent() ) / 2;
754 QgsTextRenderer::drawText( QPointF( width * dotsPerMM / 2, yBaselineVCenter ), 0, Qgis::TextHorizontalAlignment::Center,
755 QStringList() << mTextOnSymbolLabel, *context, mTextOnSymbolTextFormat );
756 }
757 }
758 }
759
760 return QSizeF( std::max( width + 2 * widthOffset, static_cast< double >( desiredWidth ) ),
761 std::max( height + 2 * heightOffset, static_cast< double >( desiredHeight ) ) );
762}
763
764QJsonObject QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const
765{
766 const QgsSymbol *s = mCustomSymbol ? mCustomSymbol.get() : mItem.symbol();
767 if ( !s )
768 {
769 return QJsonObject();
770 }
771
772
774 // QGIS 4.0 - use render context directly here, and note in the dox that the context must be correctly setup
776 ctx.setScaleFactor( settings.dpi() / 25.4 );
777 ctx.setRendererScale( settings.mapScale() );
778 ctx.setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * ctx.scaleFactor() ) ) );
779 ctx.setForceVectorOutput( true );
782
784
785 // ensure that a minimal expression context is available
786 QgsExpressionContext expContext = context.expressionContext();
788 ctx.setExpressionContext( expContext );
789
790 const QPixmap pix = QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), minimumIconSize(), 0, &ctx );
791 QImage img( pix.toImage().convertToFormat( QImage::Format_ARGB32_Premultiplied ) );
792
793 int opacity = 255;
794 if ( QgsMapLayer *layer = layerNode()->layer() )
795 opacity = ( 255 * layer->opacity() );
796
797 if ( opacity != 255 )
798 {
799 QPainter painter;
800 painter.begin( &img );
801 painter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
802 painter.fillRect( pix.rect(), QColor( 0, 0, 0, opacity ) );
803 painter.end();
804 }
805
806 QByteArray byteArray;
807 QBuffer buffer( &byteArray );
808 img.save( &buffer, "PNG" );
809 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
810
811 QJsonObject json;
812 json[ QStringLiteral( "icon" ) ] = base64;
813 if ( mItem.scaleMaxDenom() > 0 )
814 {
815 json[ QStringLiteral( "scaleMaxDenom" ) ] = mItem.scaleMaxDenom();
816 }
817 if ( mItem.scaleMinDenom() > 0 )
818 {
819 json[ QStringLiteral( "scaleMinDenom" ) ] = mItem.scaleMinDenom();
820 }
821 mItem.scaleMaxDenom();
822 return json;
823}
824
826{
828 updateLabel();
829}
830
831
833{
834 if ( mSymbolUsesMapUnits )
835 {
836 mPixmap = QPixmap();
837 emit dataChanged();
838 }
839}
840
841
842void QgsSymbolLegendNode::updateLabel()
843{
844 if ( !mLayerNode )
845 return;
846
847 const bool showFeatureCount = mLayerNode->customProperty( QStringLiteral( "showFeatureCount" ), 0 ).toBool();
848 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
849 mLabel = symbolLabel();
850
851 if ( showFeatureCount && vl )
852 {
853 const bool estimatedCount = QgsDataSourceUri( vl->dataProvider()->dataSourceUri() ).useEstimatedMetadata();
854 const qlonglong count = mEmbeddedInParent ? vl->featureCount() : vl->featureCount( mItem.ruleKey() ) ;
855
856 // if you modify this line, please update QgsLayerTreeModel::data (DisplayRole)
857 mLabel += QStringLiteral( " [%1%2]" ).arg(
858 estimatedCount ? QStringLiteral( "≈" ) : QString(),
859 count != -1 ? QLocale().toString( count ) : tr( "N/A" ) );
860 }
861
862 emit dataChanged();
863}
864
865QString QgsSymbolLegendNode::evaluateLabel( const QgsExpressionContext &context, const QString &label )
866{
867 if ( !mLayerNode )
868 return QString();
869
870 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
871
872 if ( vl )
873 {
874 QgsExpressionContext contextCopy = QgsExpressionContext( context );
875 QgsExpressionContextScope *symbolScope = createSymbolScope();
876 contextCopy.appendScope( symbolScope );
877 contextCopy.appendScope( vl->createExpressionContextScope() );
878
879 if ( label.isEmpty() )
880 {
881 if ( ! mLayerNode->labelExpression().isEmpty() )
882 mLabel = QgsExpression::replaceExpressionText( "[%" + mLayerNode->labelExpression() + "%]", &contextCopy );
883 else if ( mLabel.contains( "[%" ) )
884 {
885 const QString symLabel = symbolLabel();
886 mLabel = QgsExpression::replaceExpressionText( symLabel, &contextCopy );
887 }
888 return mLabel;
889 }
890 else
891 {
892 QString eLabel;
893 if ( ! mLayerNode->labelExpression().isEmpty() )
894 eLabel = QgsExpression::replaceExpressionText( label + "[%" + mLayerNode->labelExpression() + "%]", &contextCopy );
895 else if ( label.contains( "[%" ) )
896 eLabel = QgsExpression::replaceExpressionText( label, &contextCopy );
897 return eLabel;
898 }
899 }
900 return mLabel;
901}
902
903QgsExpressionContextScope *QgsSymbolLegendNode::createSymbolScope() const
904{
905 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
906
907 QgsExpressionContextScope *scope = new QgsExpressionContextScope( tr( "Symbol scope" ) );
908 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "symbol_label" ), symbolLabel().remove( "[%" ).remove( "%]" ), true ) );
909 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "symbol_id" ), mItem.ruleKey(), true ) );
910 if ( vl )
911 {
912 scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "symbol_count" ), QVariant::fromValue( vl->featureCount( mItem.ruleKey() ) ), true ) );
913 }
914 return scope;
915}
916
917// -------------------------------------------------------------------------
918
919
920QgsSimpleLegendNode::QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon, QObject *parent, const QString &key )
921 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
922 , mLabel( label )
923 , mIcon( icon )
924 , mKey( key )
925{
926}
927
928QVariant QgsSimpleLegendNode::data( int role ) const
929{
930 if ( role == Qt::DisplayRole || role == Qt::EditRole )
931 return mUserLabel.isEmpty() ? mLabel : mUserLabel;
932 else if ( role == Qt::DecorationRole )
933 return mIcon;
934 else if ( role == RuleKeyRole && !mKey.isEmpty() )
935 return mKey;
938 else
939 return QVariant();
940}
941
942
943// -------------------------------------------------------------------------
944
945QgsImageLegendNode::QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent )
946 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
947 , mImage( img )
948{
949}
950
951QVariant QgsImageLegendNode::data( int role ) const
952{
953 if ( role == Qt::DecorationRole )
954 {
955 return QPixmap::fromImage( mImage );
956 }
957 else if ( role == Qt::SizeHintRole )
958 {
959 return mImage.size();
960 }
962 {
964 }
965 return QVariant();
966}
967
968QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
969{
970 Q_UNUSED( itemHeight )
971
972 if ( ctx && ctx->painter && ctx->context )
973 {
974 const QgsScopedRenderContextScaleToPixels scopedScaleToPixels( *( ctx->context ) );
975 const double scaleFactor = ctx->context->scaleFactor();
976 const double imgWidth = settings.wmsLegendSize().width() * scaleFactor;
977 const double imgHeight = settings.wmsLegendSize().height() * scaleFactor;
978
979 const QImage scaledImg = mImage.scaled( QSizeF( imgWidth, imgHeight ).toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation );
980 switch ( settings.symbolAlignment() )
981 {
982 case Qt::AlignLeft:
983 default:
984 ctx->painter->drawImage( QPointF( ctx->columnLeft * scaleFactor, ctx->top * scaleFactor ), scaledImg );
985 break;
986
987 case Qt::AlignRight:
988 ctx->painter->drawImage( QPointF( ctx->columnRight * scaleFactor - imgWidth, ctx->top * scaleFactor ), scaledImg );
989 break;
990 }
991 }
992 return settings.wmsLegendSize();
993}
994
996{
997 QByteArray byteArray;
998 QBuffer buffer( &byteArray );
999 mImage.save( &buffer, "PNG" );
1000 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1001
1002 QJsonObject json;
1003 json[ QStringLiteral( "icon" ) ] = base64;
1004 return json;
1005}
1006
1007// -------------------------------------------------------------------------
1008
1009QgsRasterSymbolLegendNode::QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent, bool isCheckable, const QString &ruleKey )
1010 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
1011 , mColor( color )
1012 , mLabel( label )
1013 , mCheckable( isCheckable )
1014 , mRuleKey( ruleKey )
1015{
1016}
1017
1019{
1020 if ( mCheckable )
1021 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
1022 else
1023 return Qt::ItemIsEnabled;
1024}
1025
1026QVariant QgsRasterSymbolLegendNode::data( int role ) const
1027{
1028 switch ( role )
1029 {
1030 case Qt::DecorationRole:
1031 {
1032 const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 ); // TODO: configurable?
1033 QPixmap pix( iconSize, iconSize );
1034 pix.fill( mColor );
1035 return QIcon( pix );
1036 }
1037
1038 case Qt::DisplayRole:
1039 case Qt::EditRole:
1040 return mUserLabel.isEmpty() ? mLabel : mUserLabel;
1041
1044
1046 return mRuleKey;
1047
1048 case Qt::CheckStateRole:
1049 {
1050 if ( !mCheckable )
1051 return QVariant();
1052
1053 if ( QgsPointCloudLayer *pclayer = qobject_cast<QgsPointCloudLayer *>( mLayerNode->layer() ) )
1054 {
1055 if ( !pclayer->renderer() )
1056 return QVariant();
1057
1058 return pclayer->renderer()->legendItemChecked( mRuleKey ) ? Qt::Checked : Qt::Unchecked;
1059 }
1060
1061 return QVariant();
1062 }
1063
1064 default:
1065 return QVariant();
1066 }
1067}
1068
1069bool QgsRasterSymbolLegendNode::setData( const QVariant &value, int role )
1070{
1071 if ( role != Qt::CheckStateRole )
1072 return false;
1073
1074 if ( !mCheckable )
1075 return false;
1076
1077 if ( QgsPointCloudLayer *pclayer = qobject_cast<QgsPointCloudLayer *>( mLayerNode->layer() ) )
1078 {
1079 if ( !pclayer->renderer() )
1080 return false;
1081
1082 pclayer->renderer()->checkLegendItem( mRuleKey, value == Qt::Checked );
1083
1084 emit dataChanged();
1085 pclayer->emitStyleChanged();
1086
1087 pclayer->triggerRepaint();
1088 return true;
1089 }
1090 else
1091 {
1092 return false;
1093 }
1094}
1095
1096
1097QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
1098{
1099 QSizeF size = settings.symbolSize();
1100 double offsetX = 0;
1101 if ( ctx )
1102 {
1103 if ( ctx->patchSize.width() > 0 )
1104 {
1105 if ( ctx->patchSize.width() < size.width() )
1106 offsetX = ( size.width() - ctx->patchSize.width() ) / 2.0;
1107 size.setWidth( ctx->patchSize.width() );
1108 }
1109 if ( ctx->patchSize.height() > 0 )
1110 {
1111 size.setHeight( ctx->patchSize.height() );
1112 }
1113 }
1114
1115 if ( ctx && ctx->painter )
1116 {
1117 QColor itemColor = mColor;
1118 if ( QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( layerNode()->layer() ) )
1119 {
1120 if ( QgsRasterRenderer *rasterRenderer = rasterLayer->renderer() )
1121 itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
1122 }
1123 ctx->painter->setBrush( itemColor );
1124
1125 if ( settings.drawRasterStroke() )
1126 {
1127 QPen pen;
1128 pen.setColor( settings.rasterStrokeColor() );
1129 pen.setWidthF( settings.rasterStrokeWidth() );
1130 pen.setJoinStyle( Qt::MiterJoin );
1131 ctx->painter->setPen( pen );
1132 }
1133 else
1134 {
1135 ctx->painter->setPen( Qt::NoPen );
1136 }
1137
1138 switch ( settings.symbolAlignment() )
1139 {
1140 case Qt::AlignLeft:
1141 default:
1142 ctx->painter->drawRect( QRectF( ctx->columnLeft + offsetX, ctx->top + ( itemHeight - size.height() ) / 2,
1143 size.width(), size.height() ) );
1144 break;
1145
1146 case Qt::AlignRight:
1147 ctx->painter->drawRect( QRectF( ctx->columnRight - size.width() - offsetX, ctx->top + ( itemHeight - size.height() ) / 2,
1148 size.width(), size.height() ) );
1149 break;
1150 }
1151 }
1152 return size;
1153}
1154
1156{
1157 QImage img = QImage( settings.symbolSize().toSize(), QImage::Format_ARGB32 );
1158 img.fill( Qt::transparent );
1159
1160 QPainter painter( &img );
1161 painter.setRenderHint( QPainter::Antialiasing );
1162
1163 QColor itemColor = mColor;
1164 if ( QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( layerNode()->layer() ) )
1165 {
1166 if ( QgsRasterRenderer *rasterRenderer = rasterLayer->renderer() )
1167 itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
1168 }
1169 painter.setBrush( itemColor );
1170
1171 if ( settings.drawRasterStroke() )
1172 {
1173 QPen pen;
1174 pen.setColor( settings.rasterStrokeColor() );
1175 pen.setWidthF( settings.rasterStrokeWidth() );
1176 pen.setJoinStyle( Qt::MiterJoin );
1177 painter.setPen( pen );
1178 }
1179 else
1180 {
1181 painter.setPen( Qt::NoPen );
1182 }
1183
1184 painter.drawRect( QRectF( 0, 0, settings.symbolSize().width(), settings.symbolSize().height() ) );
1185
1186 QByteArray byteArray;
1187 QBuffer buffer( &byteArray );
1188 img.save( &buffer, "PNG" );
1189 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1190
1191 QJsonObject json;
1192 json[ QStringLiteral( "icon" ) ] = base64;
1193 return json;
1194}
1195
1196// -------------------------------------------------------------------------
1197
1199 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
1200 , mValid( false )
1201{
1202}
1203
1205
1206QImage QgsWmsLegendNode::getLegendGraphic() const
1207{
1208 if ( ! mValid && ! mFetcher )
1209 {
1210 // or maybe in presence of a downloader we should just delete it
1211 // and start a new one ?
1212
1213 QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( mLayerNode->layer() );
1214
1215 if ( layer && layer->isValid() )
1216 {
1217 const QgsLayerTreeModel *mod = model();
1218 if ( ! mod )
1219 return mImage;
1220 const QgsMapSettings *ms = mod->legendFilterMapSettings();
1221
1222 QgsRasterDataProvider *prov = layer->dataProvider();
1223 if ( ! prov )
1224 return mImage;
1225
1226 Q_ASSERT( ! mFetcher );
1227 mFetcher.reset( prov->getLegendGraphicFetcher( ms ) );
1228 if ( mFetcher )
1229 {
1230 connect( mFetcher.get(), &QgsImageFetcher::finish, this, &QgsWmsLegendNode::getLegendGraphicFinished );
1231 connect( mFetcher.get(), &QgsImageFetcher::error, this, &QgsWmsLegendNode::getLegendGraphicErrored );
1232 connect( mFetcher.get(), &QgsImageFetcher::progress, this, &QgsWmsLegendNode::getLegendGraphicProgress );
1233 mFetcher->start();
1234 }
1235 }
1236 else
1237 {
1238 QgsDebugMsg( tr( "Failed to download legend graphics: layer is not valid." ) );
1239 }
1240 }
1241
1242 return mImage;
1243}
1244
1245QVariant QgsWmsLegendNode::data( int role ) const
1246{
1247 if ( role == Qt::DecorationRole )
1248 {
1249 return QPixmap::fromImage( getLegendGraphic() );
1250 }
1251 else if ( role == Qt::SizeHintRole )
1252 {
1253 return getLegendGraphic().size();
1254 }
1256 {
1258 }
1259 return QVariant();
1260}
1261
1262QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
1263{
1264 Q_UNUSED( itemHeight )
1265
1266 const QImage image = getLegendGraphic();
1267
1268 double px2mm = 1000. / image.dotsPerMeterX();
1269 double mmWidth = image.width() * px2mm;
1270 double mmHeight = image.height() * px2mm;
1271
1272 QSize targetSize = QSize( mmWidth, mmHeight );
1273 if ( settings.wmsLegendSize().width() < mmWidth )
1274 {
1275 double targetHeight = mmHeight * settings.wmsLegendSize().width() / mmWidth;
1276 targetSize = QSize( settings.wmsLegendSize().width(), targetHeight );
1277 }
1278 else if ( settings.wmsLegendSize().height() < mmHeight )
1279 {
1280 double targetWidth = mmWidth * settings.wmsLegendSize().height() / mmHeight;
1281 targetSize = QSize( targetWidth, settings.wmsLegendSize().height() );
1282 }
1283
1284 if ( ctx && ctx->painter )
1285 {
1286 QImage smoothImage = image.scaled( targetSize / px2mm, Qt::KeepAspectRatio, Qt::SmoothTransformation );
1287
1288 switch ( settings.symbolAlignment() )
1289 {
1290 case Qt::AlignLeft:
1291 default:
1292 ctx->painter->drawImage( QRectF( ctx->columnLeft,
1293 ctx->top,
1294 targetSize.width(),
1295 targetSize.height() ),
1296 smoothImage,
1297 QRectF( QPointF( 0, 0 ), smoothImage.size() ) );
1298 break;
1299
1300 case Qt::AlignRight:
1301 ctx->painter->drawImage( QRectF( ctx->columnRight - settings.wmsLegendSize().width(),
1302 ctx->top,
1303 targetSize.width(),
1304 targetSize.height() ),
1305 smoothImage,
1306 QRectF( QPointF( 0, 0 ), smoothImage.size() ) );
1307 break;
1308 }
1309 }
1310 return targetSize;
1311}
1312
1314{
1315 QByteArray byteArray;
1316 QBuffer buffer( &byteArray );
1317 mImage.save( &buffer, "PNG" );
1318 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1319
1320 QJsonObject json;
1321 json[ QStringLiteral( "icon" ) ] = base64;
1322 return json;
1323}
1324
1325QImage QgsWmsLegendNode::renderMessage( const QString &msg ) const
1326{
1327 const int fontHeight = 10;
1328 const int margin = fontHeight / 2;
1329 const int nlines = 1;
1330
1331 const int w = 512, h = fontHeight * nlines + margin * ( nlines + 1 );
1332 QImage image( w, h, QImage::Format_ARGB32_Premultiplied );
1333 QPainter painter;
1334 painter.begin( &image );
1335 painter.setPen( QColor( 255, 0, 0 ) );
1336 painter.setFont( QFont( QStringLiteral( "Chicago" ), fontHeight ) );
1337 painter.fillRect( 0, 0, w, h, QColor( 255, 255, 255 ) );
1338 painter.drawText( 0, margin + fontHeight, msg );
1339 //painter.drawText(0,2*(margin+fontHeight),tr("retrying in 5 seconds…"));
1340 painter.end();
1341
1342 return image;
1343}
1344
1345void QgsWmsLegendNode::getLegendGraphicProgress( qint64 cur, qint64 tot )
1346{
1347 const QString msg = tot > 0 ? tr( "Downloading: %1% (%2)" ).arg( static_cast< int >( std::round( 100 * cur / tot ) ) ).arg( QgsFileUtils::representFileSize( tot ) )
1348 : tr( "Downloading: %1" ).arg( QgsFileUtils::representFileSize( cur ) );
1349 mImage = renderMessage( msg );
1350 emit dataChanged();
1351}
1352
1353void QgsWmsLegendNode::getLegendGraphicErrored( const QString & )
1354{
1355 if ( ! mFetcher )
1356 return; // must be coming after finish
1357
1358 mImage = QImage();
1359 emit dataChanged();
1360
1361 mFetcher.reset();
1362
1363 mValid = true; // we consider it valid anyway
1364}
1365
1366void QgsWmsLegendNode::getLegendGraphicFinished( const QImage &image )
1367{
1368 if ( ! mFetcher )
1369 return; // must be coming after error
1370
1371 if ( ! image.isNull() )
1372 {
1373 if ( image != mImage )
1374 {
1375 mImage = image;
1376 setUserPatchSize( mImage.size() );
1377 emit dataChanged();
1378 }
1379 mValid = true; // only if not null I guess
1380 }
1381 mFetcher.reset();
1382}
1383
1385{
1386 // TODO: do this only if this extent != prev extent ?
1387 mValid = false;
1388 emit dataChanged();
1389}
1390
1391// -------------------------------------------------------------------------
1392
1394 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
1395 , mSettings( new QgsDataDefinedSizeLegend( settings ) )
1396{
1397}
1398
1400{
1401 delete mSettings;
1402}
1403
1404QVariant QgsDataDefinedSizeLegendNode::data( int role ) const
1405{
1406 if ( role == Qt::DecorationRole )
1407 {
1408 cacheImage();
1409 return QPixmap::fromImage( mImage );
1410 }
1411 else if ( role == Qt::SizeHintRole )
1412 {
1413 cacheImage();
1414 return mImage.size();
1415 }
1417 {
1419 }
1420 return QVariant();
1421}
1422
1424{
1425 // setup temporary render context if none specified
1426 QgsRenderContext *context = nullptr;
1427 std::unique_ptr< QgsRenderContext > tempRenderContext;
1428 if ( ctx && ctx->context )
1429 context = ctx->context;
1430 else
1431 {
1432 tempRenderContext = std::make_unique< QgsRenderContext >();
1433 // QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
1435 tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
1436 tempRenderContext->setRendererScale( settings.mapScale() );
1437 tempRenderContext->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
1438 tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
1439 tempRenderContext->setForceVectorOutput( true );
1440 tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
1441 tempRenderContext->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
1443
1444 // setup a minimal expression context
1445 QgsExpressionContext expContext;
1447 tempRenderContext->setExpressionContext( expContext );
1448 context = tempRenderContext.get();
1449 }
1450
1451 if ( context->painter() )
1452 {
1453 context->painter()->save();
1454 context->painter()->translate( ctx->columnLeft, ctx->top );
1455
1456 // scale to pixels
1457 context->painter()->scale( 1 / context->scaleFactor(), 1 / context->scaleFactor() );
1458 }
1459
1460 QgsDataDefinedSizeLegend ddsLegend( *mSettings );
1461 ddsLegend.setFont( settings.style( QgsLegendStyle::SymbolLabel ).font() );
1462 ddsLegend.setTextColor( settings.fontColor() );
1463
1464 QSizeF contentSize;
1465 double labelXOffset;
1466 ddsLegend.drawCollapsedLegend( *context, &contentSize, &labelXOffset );
1467
1468 if ( context->painter() )
1469 context->painter()->restore();
1470
1471 ItemMetrics im;
1472 im.symbolSize = QSizeF( ( contentSize.width() - labelXOffset ) / context->scaleFactor(), contentSize.height() / context->scaleFactor() );
1473 im.labelSize = QSizeF( labelXOffset / context->scaleFactor(), contentSize.height() / context->scaleFactor() );
1474 return im;
1475}
1476
1477
1478void QgsDataDefinedSizeLegendNode::cacheImage() const
1479{
1480 if ( mImage.isNull() )
1481 {
1482 std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
1483 if ( !context )
1484 {
1485 context.reset( new QgsRenderContext );
1486 Q_ASSERT( context ); // to make cppcheck happy
1487 context->setScaleFactor( 96 / 25.4 );
1488 }
1489 mImage = mSettings->collapsedLegendImage( *context );
1490 }
1491}
1492
1493QgsVectorLabelLegendNode::QgsVectorLabelLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsPalLayerSettings &labelSettings, QObject *parent ): QgsLayerTreeModelLegendNode( nodeLayer, parent ), mLabelSettings( labelSettings )
1494{
1495}
1496
1498{
1499}
1500
1501QVariant QgsVectorLabelLegendNode::data( int role ) const
1502{
1503 if ( role == Qt::DisplayRole )
1504 {
1505 return mUserLabel;
1506 }
1507 if ( role == Qt::DecorationRole )
1508 {
1510 return QgsPalLayerSettings::labelSettingsPreviewPixmap( mLabelSettings, QSize( iconSize, iconSize ), mLabelSettings.legendString() );
1511 }
1512 return QVariant();
1513}
1514
1515QSizeF QgsVectorLabelLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
1516{
1517 Q_UNUSED( itemHeight );
1518 if ( !ctx )
1519 {
1520 return QSizeF( 0, 0 );
1521 }
1522
1523 const QgsRenderContext *renderContext = ctx->context;
1524 if ( renderContext )
1525 {
1526 return drawSymbol( settings, *renderContext, ctx->columnLeft, ctx->top );
1527 }
1528
1529 return QSizeF( 0, 0 );
1530}
1531
1532QSizeF QgsVectorLabelLegendNode::drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset, double yOffset ) const
1533{
1534 const QStringList textLines( mLabelSettings.legendString() );
1535 const QgsTextFormat textFormat = mLabelSettings.format();
1536 QgsRenderContext ctx( renderContext );
1537 double textWidth, textHeight;
1538 textWidthHeight( textWidth, textHeight, ctx, textFormat, textLines );
1539 textWidth /= renderContext.scaleFactor();
1540 textHeight /= renderContext.scaleFactor();
1541 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 ) );
1542
1543 const QgsScopedRenderContextScaleToPixels scopedScaleToPixels( ctx );
1544 QgsTextRenderer::drawText( textPos, 0.0, Qgis::TextHorizontalAlignment::Left, textLines, ctx, textFormat );
1545
1546 const double symbolWidth = std::max( textWidth, settings.symbolSize().width() );
1547 const double symbolHeight = std::max( textHeight, settings.symbolSize().height() );
1548 return QSizeF( symbolWidth, symbolHeight );
1549}
1550
1551QJsonObject QgsVectorLabelLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const
1552{
1553 Q_UNUSED( settings );
1554
1555 const double mmToPixel = 96.0 / 25.4; //settings.dpi() is deprecated
1556
1557 const QStringList textLines( mLabelSettings.legendString() );
1558 const QgsTextFormat textFormat = mLabelSettings.format();
1559 QgsRenderContext ctx( context );
1560 ctx.setScaleFactor( mmToPixel );
1561
1562 double textWidth, textHeight;
1563 textWidthHeight( textWidth, textHeight, ctx, textFormat, textLines );
1564 const QPixmap previewPixmap = QgsPalLayerSettings::labelSettingsPreviewPixmap( mLabelSettings, QSize( textWidth, textHeight ), mLabelSettings.legendString() );
1565
1566 QByteArray byteArray;
1567 QBuffer buffer( &byteArray );
1568 previewPixmap.save( &buffer, "PNG" );
1569 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1570
1571 QJsonObject json;
1572 json[ QStringLiteral( "icon" ) ] = base64;
1573 return json;
1574}
1575
1576void QgsVectorLabelLegendNode::textWidthHeight( double &width, double &height, QgsRenderContext &ctx, const QgsTextFormat &textFormat, const QStringList &textLines ) const
1577{
1578 QFontMetricsF fm = QgsTextRenderer::fontMetrics( ctx, textFormat );
1579 height = QgsTextRenderer::textHeight( ctx, textFormat, 'A', true );
1580 width = QgsTextRenderer::textWidth( ctx, textFormat, textLines, &fm );
1581}
@ Point
Text at point of origin layout mode.
@ 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.
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)
item in symbology was 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.
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.
@ ParentRuleKeyRole
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2....
@ RuleKeyRole
Rule key of the node (QString)
@ NodeTypeRole
Type of node. Added in 3.16.
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.
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...
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
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.
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
QSizeF symbolSize() const
Returns the default symbol size (in millimeters) used for legend items.
QColor fontColor() const
Returns the font color 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.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...
Q_DECL_DEPRECATED bool useAdvancedEffects() const
double fontHeightCharacterMM(const QFont &font, QChar c) const
Returns the font height of a character in millimeters.
Q_DECL_DEPRECATED int dpi() const
double lineSpacing() const
Returns the line spacing to use between lines of legend text.
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.
double margin(Side side)
Returns the margin (in mm) for the specified side of the component.
Qt::Alignment alignment() const
Returns the alignment for the legend component.
QFont font() const
Returns the font used for rendering this legend component.
@ Right
Right side.
@ Left
Left side.
@ 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:73
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.
bool isValid
Definition: qgsmaplayer.h:81
The QgsMapSettings class contains configuration for rendering of the map.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
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)
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.
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())
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.
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.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
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)
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.
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:93
void drawPreviewIcon(QPainter *painter, QSize size, QgsRenderContext *customContext=nullptr, bool selected=false, const QgsExpressionContext *expressionContext=nullptr, const QgsLegendPatchShape *patchShape=nullptr)
Draws an icon of the symbol that occupies an area given by size using the specified painter.
Definition: qgssymbol.cpp:923
bool usesMapUnits() const
Returns true if the symbol has any components which use map unit based sizes.
Definition: qgssymbol.cpp:630
Qgis::SymbolType type() const
Returns the symbol's type.
Definition: qgssymbol.h:152
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
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...
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 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 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 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())
Draws text within a rectangle using the specified settings.
static bool isNull(const QVariant &variant)
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.
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".
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:3061
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:3060
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2527
QList< QgsLegendSymbolItem > QgsLegendSymbolList
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
Single variable definition for use within a QgsExpressionContextScope.
double top
Top y-position of legend item.
QgsLegendPatchShape patchShape
The patch shape to render for the node.
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.
double columnLeft
Left side of current legend column.
double columnRight
Right side of current legend column.
Q_NOWARN_DEPRECATED_POP QgsRenderContext * context
Render context, if available.