QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 "qgslayertree.h"
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 "qgsrasterrenderer.h"
32 #include "qgsfeatureid.h"
33 #include "qgslayoutitem.h"
35 #include "qgsexpression.h"
36 #include "qgstextrenderer.h"
37 
38 
40  : QObject( parent )
41  , mLayerNode( nodeL )
42  , mEmbeddedInParent( false )
43 {
44 }
45 
47 {
48  return qobject_cast<QgsLayerTreeModel *>( parent() );
49 }
50 
52 {
53  return Qt::ItemIsEnabled;
54 }
55 
56 bool QgsLayerTreeModelLegendNode::setData( const QVariant &value, int role )
57 {
58  Q_UNUSED( value )
59  Q_UNUSED( role )
60  return false;
61 }
62 
64 {
65  if ( mEmbeddedInParent )
66  return mLayerNode->patchSize();
67 
68  return mUserSize;
69 }
70 
72 {
73  if ( mUserSize == size )
74  return;
75 
76  mUserSize = size;
77  emit sizeChanged();
78 }
79 
81 {
82  QFont symbolLabelFont = settings.style( QgsLegendStyle::SymbolLabel ).font();
83 
84  double textHeight = settings.fontHeightCharacterMM( symbolLabelFont, QChar( '0' ) );
85  // itemHeight here is not really item height, it is only for symbol
86  // vertical alignment purpose, i.e. OK take single line height
87  // if there are more lines, those run under the symbol
88  double itemHeight = std::max( static_cast< double >( ctx && ctx->patchSize.height() > 0 ? ctx->patchSize.height() : settings.symbolSize().height() ), textHeight );
89 
90  ItemMetrics im;
91  im.symbolSize = drawSymbol( settings, ctx, itemHeight );
92  im.labelSize = drawSymbolText( settings, ctx, im.symbolSize );
93  return im;
94 }
95 
97 {
98  QJsonObject json = exportSymbolToJson( settings, context );
99  const QString text = data( Qt::DisplayRole ).toString();
100  json[ QStringLiteral( "title" ) ] = text;
101  return json;
102 }
103 
104 QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
105 {
106  QIcon symbolIcon = data( Qt::DecorationRole ).value<QIcon>();
107  if ( symbolIcon.isNull() )
108  return QSizeF();
109 
110  QSizeF size = settings.symbolSize();
111  if ( ctx )
112  {
113  if ( ctx->patchSize.width() > 0 )
114  size.setWidth( ctx->patchSize.width( ) );
115  if ( ctx->patchSize.height() > 0 )
116  size.setHeight( ctx->patchSize.height( ) );
117  }
118 
119  if ( ctx && ctx->painter )
120  {
121  switch ( settings.symbolAlignment() )
122  {
123  case Qt::AlignLeft:
124  default:
125  symbolIcon.paint( ctx->painter,
126  static_cast< int >( ctx->columnLeft ),
127  static_cast< int >( ctx->top + ( itemHeight - size.height() ) / 2 ),
128  static_cast< int >( size.width() ),
129  static_cast< int >( size.height() ) );
130  break;
131 
132  case Qt::AlignRight:
133  symbolIcon.paint( ctx->painter,
134  static_cast< int >( ctx->columnRight - size.width() ),
135  static_cast< int >( ctx->top + ( itemHeight - size.height() ) / 2 ),
136  static_cast< int >( size.width() ),
137  static_cast< int >( size.height() ) );
138  break;
139  }
140  }
141  return size;
142 }
143 
145 {
146  const QIcon icon = data( Qt::DecorationRole ).value<QIcon>();
147  if ( icon.isNull() )
148  return QJsonObject();
149 
150  const QImage image( icon.pixmap( settings.symbolSize().width(), settings.symbolSize().height() ).toImage() );
151  QByteArray byteArray;
152  QBuffer buffer( &byteArray );
153  image.save( &buffer, "PNG" );
154  const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
155 
156  QJsonObject json;
157  json[ QStringLiteral( "icon" ) ] = base64;
158  return json;
159 }
160 
161 QSizeF QgsLayerTreeModelLegendNode::drawSymbolText( const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize ) const
162 {
163  QSizeF labelSize( 0, 0 );
164 
165  QFont symbolLabelFont = settings.style( QgsLegendStyle::SymbolLabel ).font();
166  double textHeight = settings.fontHeightCharacterMM( symbolLabelFont, QChar( '0' ) );
167  double textDescent = settings.fontDescentMillimeters( symbolLabelFont );
168 
169  QgsExpressionContext tempContext;
170 
171  const QStringList lines = settings.evaluateItemText( data( Qt::DisplayRole ).toString(), ctx && ctx->context ? ctx->context->expressionContext() : tempContext );
172 
173  labelSize.rheight() = lines.count() * textHeight + ( lines.count() - 1 ) * ( settings.lineSpacing() + textDescent );
174 
175  double labelXMin = 0.0;
176  double labelXMax = 0.0;
177  double labelY = 0.0;
178  if ( ctx && ctx->painter )
179  {
180  ctx->painter->setPen( settings.fontColor() );
181  switch ( settings.symbolAlignment() )
182  {
183  case Qt::AlignLeft:
184  default:
185  labelXMin = ctx->columnLeft + std::max( static_cast< double >( symbolSize.width() ), ctx->maxSiblingSymbolWidth )
188  labelXMax = ctx->columnRight;
189  break;
190 
191  case Qt::AlignRight:
192  labelXMin = ctx->columnLeft;
193  // NOTE -- while the below calculations use the flipped margins from the style, that's only done because
194  // those are the only margins we expose and use for now! (and we expose them as generic margins, not side-specific
195  // ones) TODO when/if we expose other margin settings, these should be reversed...
196  labelXMax = ctx->columnRight - std::max( static_cast< double >( symbolSize.width() ), ctx->maxSiblingSymbolWidth )
199  break;
200  }
201 
202  labelY = ctx->top;
203 
204  // Vertical alignment of label with symbol
205  if ( labelSize.height() < symbolSize.height() )
206  labelY += symbolSize.height() / 2 - labelSize.height() / 2; // label centered with symbol
207 
208  labelY += textHeight;
209  }
210 
211  for ( QStringList::ConstIterator itemPart = lines.constBegin(); itemPart != lines.constEnd(); ++itemPart )
212  {
213  const double lineWidth = settings.textWidthMillimeters( symbolLabelFont, *itemPart );
214  labelSize.rwidth() = std::max( lineWidth, double( labelSize.width() ) );
215 
216  if ( ctx && ctx->painter )
217  {
218  switch ( settings.style( QgsLegendStyle::SymbolLabel ).alignment() )
219  {
220  case Qt::AlignLeft:
221  default:
222  settings.drawText( ctx->painter, labelXMin, labelY, *itemPart, symbolLabelFont );
223  break;
224 
225  case Qt::AlignRight:
226  settings.drawText( ctx->painter, labelXMax - lineWidth, labelY, *itemPart, symbolLabelFont );
227  break;
228 
229  case Qt::AlignHCenter:
230  settings.drawText( ctx->painter, labelXMin + ( labelXMax - labelXMin - lineWidth ) / 2.0, labelY, *itemPart, symbolLabelFont );
231  break;
232  }
233 
234  if ( itemPart != ( lines.end() - 1 ) )
235  labelY += textDescent + settings.lineSpacing() + textHeight;
236  }
237  }
238 
239  return labelSize;
240 }
241 
242 // -------------------------------------------------------------------------
243 
245  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
246  , mItem( item )
247  , mSymbolUsesMapUnits( false )
248 {
249  const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 );
250  mIconSize = QSize( iconSize, iconSize );
251 
252  updateLabel();
253  connect( qobject_cast<QgsVectorLayer *>( nodeLayer->layer() ), &QgsVectorLayer::symbolFeatureCountMapChanged, this, &QgsSymbolLegendNode::updateLabel );
254  connect( nodeLayer, &QObject::destroyed, this, [ = ]() { mLayerNode = nullptr; } );
255 
256  if ( auto *lSymbol = mItem.symbol() )
257  mSymbolUsesMapUnits = ( lSymbol->outputUnit() != QgsUnitTypes::RenderMillimeters );
258 }
259 
260 Qt::ItemFlags QgsSymbolLegendNode::flags() const
261 {
262  if ( mItem.isCheckable() )
263  return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
264  else
265  return Qt::ItemIsEnabled;
266 }
267 
268 
270 {
271  std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
272  return minimumIconSize( context.get() );
273 }
274 
276 {
277  const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 );
278  const int largeIconSize = QgsLayerTreeModel::scaleIconSize( 512 );
279  QSize minSz( iconSize, iconSize );
280  if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbol::Marker )
281  {
283  QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), QSize( largeIconSize, largeIconSize ), 0,
284  context ).toImage(),
285  minSz,
286  true ).size();
287  }
288  else if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbol::Line )
289  {
291  QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), QSize( minSz.width(), largeIconSize ), 0,
292  context ).toImage(),
293  minSz,
294  true ).size();
295  }
296 
297  if ( !mTextOnSymbolLabel.isEmpty() && context )
298  {
299  double w = QgsTextRenderer::textWidth( *context, mTextOnSymbolTextFormat, QStringList() << mTextOnSymbolLabel );
300  double h = QgsTextRenderer::textHeight( *context, mTextOnSymbolTextFormat, QStringList() << mTextOnSymbolLabel, QgsTextRenderer::Point );
301  int wInt = ceil( w ), hInt = ceil( h );
302  if ( wInt > minSz.width() ) minSz.setWidth( wInt );
303  if ( hInt > minSz.height() ) minSz.setHeight( hInt );
304  }
305 
306  return minSz;
307 }
308 
310 {
311  return mItem.symbol();
312 }
313 
315 {
316  QString label;
317  if ( mEmbeddedInParent )
318  {
319  QVariant legendlabel = mLayerNode->customProperty( QStringLiteral( "legend/title-label" ) );
320  QString layerName = legendlabel.isNull() ? mLayerNode->name() : legendlabel.toString();
321  label = mUserLabel.isEmpty() ? layerName : mUserLabel;
322  }
323  else
324  label = mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
325  return label;
326 }
327 
329 {
330  if ( mEmbeddedInParent )
331  {
332  return mLayerNode->patchShape();
333  }
334  else
335  {
336  return mPatchShape;
337  }
338 }
339 
341 {
342  mPatchShape = shape;
343 }
344 
346 {
347  return mCustomSymbol.get();
348 }
349 
351 {
352  mCustomSymbol.reset( symbol );
353 }
354 
356 {
357  if ( !symbol )
358  return;
359 
360  std::unique_ptr< QgsSymbol > s( symbol ); // this method takes ownership of symbol
361  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
362  if ( !vlayer || !vlayer->renderer() )
363  return;
364 
365  mItem.setSymbol( s.get() ); // doesn't transfer ownership
366  vlayer->renderer()->setLegendSymbolItem( mItem.ruleKey(), s.release() ); // DOES transfer ownership!
367 
368  mPixmap = QPixmap();
369 
370  emit dataChanged();
371  vlayer->triggerRepaint();
372 }
373 
375 {
376  checkAll( true );
377 }
378 
380 {
381  checkAll( false );
382 }
383 
385 {
386  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
387  if ( !vlayer || !vlayer->renderer() )
388  return;
389 
390  const QgsLegendSymbolList symbolList = vlayer->renderer()->legendSymbolItems();
391  for ( const auto &item : symbolList )
392  {
393  vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), ! vlayer->renderer()->legendSymbolItemChecked( item.ruleKey() ) );
394  }
395 
396  emit dataChanged();
397  vlayer->triggerRepaint();
398 }
399 
401 {
402  double scale = 0.0;
403  double mupp = 0.0;
404  int dpi = 0;
405  if ( auto *lModel = model() )
406  lModel->legendMapViewData( &mupp, &dpi, &scale );
407 
408  if ( qgsDoubleNear( mupp, 0.0 ) || dpi == 0 || qgsDoubleNear( scale, 0.0 ) )
409  return nullptr;
410 
411  // setup temporary render context
412  std::unique_ptr<QgsRenderContext> context = qgis::make_unique<QgsRenderContext>( );
413  context->setScaleFactor( dpi / 25.4 );
414  context->setRendererScale( scale );
415  context->setMapToPixel( QgsMapToPixel( mupp ) );
416  context->setFlag( QgsRenderContext::Antialiasing, true );
417  context->setFlag( QgsRenderContext::RenderSymbolPreview, true );
418  return context.release();
419 }
420 
421 void QgsSymbolLegendNode::checkAll( bool state )
422 {
423  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
424  if ( !vlayer || !vlayer->renderer() )
425  return;
426 
427  const QgsLegendSymbolList symbolList = vlayer->renderer()->legendSymbolItems();
428  for ( const auto &item : symbolList )
429  {
430  vlayer->renderer()->checkLegendSymbolItem( item.ruleKey(), state );
431  }
432 
433  emit dataChanged();
434  vlayer->triggerRepaint();
435 }
436 
437 QVariant QgsSymbolLegendNode::data( int role ) const
438 {
439  if ( role == Qt::DisplayRole )
440  {
441  return mLabel;
442  }
443  else if ( role == Qt::EditRole )
444  {
445  return mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
446  }
447  else if ( role == Qt::DecorationRole )
448  {
449  if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
450  {
451  QPixmap pix;
452  if ( mItem.symbol() )
453  {
454  std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
455  pix = QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), mIconSize, 0, context.get() );
456 
457  if ( !mTextOnSymbolLabel.isEmpty() && context )
458  {
459  QPainter painter( &pix );
460  painter.setRenderHint( QPainter::Antialiasing );
461  context->setPainter( &painter );
462  QFontMetricsF fm( mTextOnSymbolTextFormat.scaledFont( *context ) );
463  qreal yBaselineVCenter = ( mIconSize.height() + fm.ascent() - fm.descent() ) / 2;
464  QgsTextRenderer::drawText( QPointF( mIconSize.width() / 2, yBaselineVCenter ), 0, QgsTextRenderer::AlignCenter,
465  QStringList() << mTextOnSymbolLabel, *context, mTextOnSymbolTextFormat );
466  }
467  }
468  else
469  {
470  pix = QPixmap( mIconSize );
471  pix.fill( Qt::transparent );
472  }
473 
474  mPixmap = pix;
475  }
476  return mPixmap;
477  }
478  else if ( role == Qt::CheckStateRole )
479  {
480  if ( !mItem.isCheckable() )
481  return QVariant();
482 
483  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
484  if ( !vlayer || !vlayer->renderer() )
485  return QVariant();
486 
487  return vlayer->renderer()->legendSymbolItemChecked( mItem.ruleKey() ) ? Qt::Checked : Qt::Unchecked;
488  }
489  else if ( role == RuleKeyRole )
490  {
491  return mItem.ruleKey();
492  }
493  else if ( role == ParentRuleKeyRole )
494  {
495  return mItem.parentRuleKey();
496  }
497 
498  return QVariant();
499 }
500 
501 bool QgsSymbolLegendNode::setData( const QVariant &value, int role )
502 {
503  if ( role != Qt::CheckStateRole )
504  return false;
505 
506  if ( !mItem.isCheckable() )
507  return false;
508 
509  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
510  if ( !vlayer || !vlayer->renderer() )
511  return false;
512 
513  vlayer->renderer()->checkLegendSymbolItem( mItem.ruleKey(), value == Qt::Checked );
514 
515  emit dataChanged();
516  vlayer->emitStyleChanged();
517 
518  vlayer->triggerRepaint();
519 
520  return true;
521 }
522 
523 
524 
525 QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
526 {
527  QgsSymbol *s = mCustomSymbol ? mCustomSymbol.get() : mItem.symbol();
528  if ( !s )
529  {
530  return QSizeF();
531  }
532 
533  // setup temporary render context
534  QgsRenderContext *context = nullptr;
535  std::unique_ptr< QgsRenderContext > tempRenderContext;
537  if ( ctx && ctx->context )
538  context = ctx->context;
539  else
540  {
541  tempRenderContext = qgis::make_unique< QgsRenderContext >();
542  // QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
544  tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
545  tempRenderContext->setRendererScale( settings.mapScale() );
546  tempRenderContext->setFlag( QgsRenderContext::Antialiasing, true );
547  tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
549  tempRenderContext->setForceVectorOutput( true );
550  tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
551 
552  // setup a minimal expression context
553  QgsExpressionContext expContext;
555  tempRenderContext->setExpressionContext( expContext );
556  context = tempRenderContext.get();
557  }
558 
559  //Consider symbol size for point markers
560  const double desiredHeight = ctx && ctx->patchSize.height() > 0 ? ctx->patchSize.height() : settings.symbolSize().height();
561  const double desiredWidth = ctx && ctx->patchSize.width() > 0 ? ctx->patchSize.width() : settings.symbolSize().width();
562  double height = desiredHeight;
563  double width = desiredWidth;
564 
565  //Center small marker symbols
566  double widthOffset = 0;
567  double heightOffset = 0;
568 
569  double maxSymbolSize = settings.maximumSymbolSize();
570  double minSymbolSize = settings.minimumSymbolSize();
571 
572  if ( QgsMarkerSymbol *markerSymbol = dynamic_cast<QgsMarkerSymbol *>( s ) )
573  {
574  double size = markerSymbol->size( *context ) / context->scaleFactor();
575  height = size;
576  width = size;
577  }
578 
579  std::unique_ptr<QgsSymbol> minMaxSizeSymbol( QgsSymbolLayerUtils::restrictedSizeSymbol( s, minSymbolSize, maxSymbolSize, context, width, height ) );
580  if ( minMaxSizeSymbol )
581  {
582  s = minMaxSizeSymbol.get();
583  }
584 
585  if ( s->type() == QgsSymbol::Marker )
586  {
587  if ( width < desiredWidth )
588  {
589  widthOffset = ( desiredWidth - width ) / 2.0;
590  }
591  if ( height < desiredHeight )
592  {
593  heightOffset = ( desiredHeight - height ) / 2.0;
594  }
595  }
596  if ( ctx && ctx->painter )
597  {
598  double currentYCoord = ctx->top + ( itemHeight - desiredHeight ) / 2;
599  QPainter *p = ctx->painter;
600 
601  //setup painter scaling to dots so that raster symbology is drawn to scale
602  double dotsPerMM = context->scaleFactor();
603 
604  int opacity = 255;
605  if ( QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layerNode()->layer() ) )
606  opacity = static_cast<int >( std::round( 255 * vectorLayer->opacity() ) );
607 
608  QgsScopedQPainterState painterState( p );
609  context->setPainterFlagsUsingContext( p );
610 
611  switch ( settings.symbolAlignment() )
612  {
613  case Qt::AlignLeft:
614  default:
615  p->translate( ctx->columnLeft + widthOffset, currentYCoord + heightOffset );
616  break;
617  case Qt::AlignRight:
618  p->translate( ctx->columnRight - widthOffset - width, currentYCoord + heightOffset );
619  break;
620  }
621 
622  p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
624  // QGIS 4.0 -- ctx->context will be mandatory
625  const bool useAdvancedEffects = ctx->context ? ctx->context->flags() & QgsRenderContext::UseAdvancedEffects : settings.useAdvancedEffects();
627  if ( opacity != 255 && useAdvancedEffects )
628  {
629  const int maxBleed = static_cast< int >( std::ceil( QgsSymbolLayerUtils::estimateMaxSymbolBleed( s, *context ) ) );
630 
631  //semi transparent layer, so need to draw symbol to an image (to flatten it first)
632  //create image which is same size as legend rect, in case symbol bleeds outside its allotted space
633  const QSize symbolSize( static_cast< int >( std::round( width * dotsPerMM ) ), static_cast<int >( std::round( height * dotsPerMM ) ) );
634  const QSize tempImageSize( symbolSize.width() + maxBleed * 2, symbolSize.height() + maxBleed * 2 );
635  QImage tempImage = QImage( tempImageSize, QImage::Format_ARGB32 );
636  tempImage.fill( Qt::transparent );
637  QPainter imagePainter( &tempImage );
638  context->setPainterFlagsUsingContext( &imagePainter );
639 
640  context->setPainter( &imagePainter );
641  imagePainter.translate( maxBleed, maxBleed );
642  s->drawPreviewIcon( &imagePainter, symbolSize, context, false, nullptr, &patchShape );
643  imagePainter.translate( -maxBleed, -maxBleed );
644  context->setPainter( ctx->painter );
645  //reduce opacity of image
646  imagePainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
647  imagePainter.fillRect( tempImage.rect(), QColor( 0, 0, 0, opacity ) );
648  imagePainter.end();
649  //draw rendered symbol image
650  p->drawImage( -maxBleed, -maxBleed, tempImage );
651  }
652  else
653  {
654  s->drawPreviewIcon( p, QSize( static_cast< int >( std::round( width * dotsPerMM ) ), static_cast< int >( std::round( height * dotsPerMM ) ) ), context, false, nullptr, &patchShape );
655  }
656 
657  if ( !mTextOnSymbolLabel.isEmpty() )
658  {
659  QFontMetricsF fm( mTextOnSymbolTextFormat.scaledFont( *context ) );
660  qreal yBaselineVCenter = ( height * dotsPerMM + fm.ascent() - fm.descent() ) / 2;
661  QgsTextRenderer::drawText( QPointF( width * dotsPerMM / 2, yBaselineVCenter ), 0, QgsTextRenderer::AlignCenter,
662  QStringList() << mTextOnSymbolLabel, *context, mTextOnSymbolTextFormat );
663  }
664  }
665 
666  return QSizeF( std::max( width + 2 * widthOffset, static_cast< double >( desiredWidth ) ),
667  std::max( height + 2 * heightOffset, static_cast< double >( desiredHeight ) ) );
668 }
669 
670 QJsonObject QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const
671 {
672  const QgsSymbol *s = mCustomSymbol ? mCustomSymbol.get() : mItem.symbol();
673  if ( !s )
674  {
675  return QJsonObject();
676  }
677 
678 
679  QgsRenderContext ctx;
680  // QGIS 4.0 - use render context directly here, and note in the dox that the context must be correctly setup
682  ctx.setScaleFactor( settings.dpi() / 25.4 );
683  ctx.setRendererScale( settings.mapScale() );
684  ctx.setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * ctx.scaleFactor() ) ) );
685  ctx.setForceVectorOutput( true );
688 
690 
691  // ensure that a minimal expression context is available
692  QgsExpressionContext expContext = context.expressionContext();
694  ctx.setExpressionContext( expContext );
695 
696  const QPixmap pix = QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), minimumIconSize(), 0, &ctx );
697  QImage img( pix.toImage().convertToFormat( QImage::Format_ARGB32_Premultiplied ) );
698 
699  int opacity = 255;
700  if ( QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layerNode()->layer() ) )
701  opacity = ( 255 * vectorLayer->opacity() );
702 
703  if ( opacity != 255 )
704  {
705  QPainter painter;
706  painter.begin( &img );
707  painter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
708  painter.fillRect( pix.rect(), QColor( 0, 0, 0, opacity ) );
709  painter.end();
710  }
711 
712  QByteArray byteArray;
713  QBuffer buffer( &byteArray );
714  img.save( &buffer, "PNG" );
715  const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
716 
717  QJsonObject json;
718  json[ QStringLiteral( "icon" ) ] = base64;
719  return json;
720 }
721 
723 {
725  updateLabel();
726 }
727 
728 
730 {
731  if ( mSymbolUsesMapUnits )
732  {
733  mPixmap = QPixmap();
734  emit dataChanged();
735  }
736 }
737 
738 
739 void QgsSymbolLegendNode::updateLabel()
740 {
741  if ( !mLayerNode )
742  return;
743 
744  bool showFeatureCount = mLayerNode->customProperty( QStringLiteral( "showFeatureCount" ), 0 ).toBool();
745  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
746  mLabel = symbolLabel();
747 
748  if ( showFeatureCount && vl )
749  {
750  qlonglong count = mEmbeddedInParent ? vl->featureCount() : vl->featureCount( mItem.ruleKey() ) ;
751  mLabel += QStringLiteral( " [%1]" ).arg( count != -1 ? QLocale().toString( count ) : tr( "N/A" ) );
752  }
753 
754  emit dataChanged();
755 }
756 
757 QString QgsSymbolLegendNode::evaluateLabel( const QgsExpressionContext &context, const QString &label )
758 {
759  if ( !mLayerNode )
760  return QString();
761 
762  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
763 
764  if ( vl )
765  {
766  QgsExpressionContext contextCopy = QgsExpressionContext( context );
767  QgsExpressionContextScope *symbolScope = createSymbolScope();
768  contextCopy.appendScope( symbolScope );
769  contextCopy.appendScope( vl->createExpressionContextScope() );
770 
771  if ( label.isEmpty() )
772  {
773  if ( ! mLayerNode->labelExpression().isEmpty() )
774  mLabel = QgsExpression::replaceExpressionText( "[%" + mLayerNode->labelExpression() + "%]", &contextCopy );
775  else if ( mLabel.contains( "[%" ) )
776  {
777  const QString symLabel = symbolLabel();
778  mLabel = QgsExpression::replaceExpressionText( symLabel, &contextCopy );
779  }
780  return mLabel;
781  }
782  else
783  {
784  QString eLabel;
785  if ( ! mLayerNode->labelExpression().isEmpty() )
786  eLabel = QgsExpression::replaceExpressionText( label + "[%" + mLayerNode->labelExpression() + "%]", &contextCopy );
787  else if ( label.contains( "[%" ) )
788  eLabel = QgsExpression::replaceExpressionText( label, &contextCopy );
789  return eLabel;
790  }
791  }
792  return mLabel;
793 }
794 
795 QgsExpressionContextScope *QgsSymbolLegendNode::createSymbolScope() const
796 {
797  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerNode->layer() );
798 
799  QgsExpressionContextScope *scope = new QgsExpressionContextScope( tr( "Symbol scope" ) );
800  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "symbol_label" ), symbolLabel().remove( "[%" ).remove( "%]" ), true ) );
801  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "symbol_id" ), mItem.ruleKey(), true ) );
802  if ( vl )
803  {
804  scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "symbol_count" ), QVariant::fromValue( vl->featureCount( mItem.ruleKey() ) ), true ) );
805  }
806  return scope;
807 }
808 
809 // -------------------------------------------------------------------------
810 
811 
812 QgsSimpleLegendNode::QgsSimpleLegendNode( QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon, QObject *parent, const QString &key )
813  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
814  , mLabel( label )
815  , mIcon( icon )
816  , mKey( key )
817 {
818 }
819 
820 QVariant QgsSimpleLegendNode::data( int role ) const
821 {
822  if ( role == Qt::DisplayRole || role == Qt::EditRole )
823  return mUserLabel.isEmpty() ? mLabel : mUserLabel;
824  else if ( role == Qt::DecorationRole )
825  return mIcon;
826  else if ( role == RuleKeyRole && !mKey.isEmpty() )
827  return mKey;
828  else
829  return QVariant();
830 }
831 
832 
833 // -------------------------------------------------------------------------
834 
835 QgsImageLegendNode::QgsImageLegendNode( QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent )
836  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
837  , mImage( img )
838 {
839 }
840 
841 QVariant QgsImageLegendNode::data( int role ) const
842 {
843  if ( role == Qt::DecorationRole )
844  {
845  return QPixmap::fromImage( mImage );
846  }
847  else if ( role == Qt::SizeHintRole )
848  {
849  return mImage.size();
850  }
851  return QVariant();
852 }
853 
854 QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
855 {
856  Q_UNUSED( itemHeight )
857 
858  if ( ctx && ctx->painter )
859  {
860  switch ( settings.symbolAlignment() )
861  {
862  case Qt::AlignLeft:
863  default:
864  ctx->painter->drawImage( QRectF( ctx->columnLeft, ctx->top, settings.wmsLegendSize().width(), settings.wmsLegendSize().height() ),
865  mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
866  break;
867 
868  case Qt::AlignRight:
869  ctx->painter->drawImage( QRectF( ctx->columnRight - settings.wmsLegendSize().width(), ctx->top, settings.wmsLegendSize().width(), settings.wmsLegendSize().height() ),
870  mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
871  break;
872  }
873  }
874  return settings.wmsLegendSize();
875 }
876 
878 {
879  QByteArray byteArray;
880  QBuffer buffer( &byteArray );
881  mImage.save( &buffer, "PNG" );
882  const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
883 
884  QJsonObject json;
885  json[ QStringLiteral( "icon" ) ] = base64;
886  return json;
887 }
888 
889 // -------------------------------------------------------------------------
890 
891 QgsRasterSymbolLegendNode::QgsRasterSymbolLegendNode( QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent )
892  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
893  , mColor( color )
894  , mLabel( label )
895 {
896 }
897 
898 QVariant QgsRasterSymbolLegendNode::data( int role ) const
899 {
900  if ( role == Qt::DecorationRole )
901  {
902  const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 ); // TODO: configurable?
903  QPixmap pix( iconSize, iconSize );
904  pix.fill( mColor );
905  return QIcon( pix );
906  }
907  else if ( role == Qt::DisplayRole || role == Qt::EditRole )
908  return mUserLabel.isEmpty() ? mLabel : mUserLabel;
909  else
910  return QVariant();
911 }
912 
913 
914 QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
915 {
916  QSizeF size = settings.symbolSize();
917  double offsetX = 0;
918  if ( ctx )
919  {
920  if ( ctx->patchSize.width() > 0 )
921  {
922  if ( ctx->patchSize.width() < size.width() )
923  offsetX = ( size.width() - ctx->patchSize.width() ) / 2.0;
924  size.setWidth( ctx->patchSize.width() );
925  }
926  if ( ctx->patchSize.height() > 0 )
927  {
928  size.setHeight( ctx->patchSize.height() );
929  }
930  }
931 
932  if ( ctx && ctx->painter )
933  {
934  QColor itemColor = mColor;
935  if ( QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( layerNode()->layer() ) )
936  {
937  if ( QgsRasterRenderer *rasterRenderer = rasterLayer->renderer() )
938  itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
939  }
940  ctx->painter->setBrush( itemColor );
941 
942  if ( settings.drawRasterStroke() )
943  {
944  QPen pen;
945  pen.setColor( settings.rasterStrokeColor() );
946  pen.setWidthF( settings.rasterStrokeWidth() );
947  pen.setJoinStyle( Qt::MiterJoin );
948  ctx->painter->setPen( pen );
949  }
950  else
951  {
952  ctx->painter->setPen( Qt::NoPen );
953  }
954 
955  switch ( settings.symbolAlignment() )
956  {
957  case Qt::AlignLeft:
958  default:
959  ctx->painter->drawRect( QRectF( ctx->columnLeft + offsetX, ctx->top + ( itemHeight - size.height() ) / 2,
960  size.width(), size.height() ) );
961  break;
962 
963  case Qt::AlignRight:
964  ctx->painter->drawRect( QRectF( ctx->columnRight - size.width() - offsetX, ctx->top + ( itemHeight - size.height() ) / 2,
965  size.width(), size.height() ) );
966  break;
967  }
968  }
969  return size;
970 }
971 
973 {
974  QImage img = QImage( settings.symbolSize().toSize(), QImage::Format_ARGB32 );
975  img.fill( Qt::transparent );
976 
977  QPainter painter( &img );
978  painter.setRenderHint( QPainter::Antialiasing );
979 
980  QColor itemColor = mColor;
981  if ( QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( layerNode()->layer() ) )
982  {
983  if ( QgsRasterRenderer *rasterRenderer = rasterLayer->renderer() )
984  itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
985  }
986  painter.setBrush( itemColor );
987 
988  if ( settings.drawRasterStroke() )
989  {
990  QPen pen;
991  pen.setColor( settings.rasterStrokeColor() );
992  pen.setWidthF( settings.rasterStrokeWidth() );
993  pen.setJoinStyle( Qt::MiterJoin );
994  painter.setPen( pen );
995  }
996  else
997  {
998  painter.setPen( Qt::NoPen );
999  }
1000 
1001  painter.drawRect( QRectF( 0, 0, settings.symbolSize().width(), settings.symbolSize().height() ) );
1002 
1003  QByteArray byteArray;
1004  QBuffer buffer( &byteArray );
1005  img.save( &buffer, "PNG" );
1006  const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1007 
1008  QJsonObject json;
1009  json[ QStringLiteral( "icon" ) ] = base64;
1010  return json;
1011 }
1012 
1013 // -------------------------------------------------------------------------
1014 
1016  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
1017  , mValid( false )
1018 {
1019 }
1020 
1021 QImage QgsWmsLegendNode::getLegendGraphic() const
1022 {
1023  if ( ! mValid && ! mFetcher )
1024  {
1025  // or maybe in presence of a downloader we should just delete it
1026  // and start a new one ?
1027 
1028  QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( mLayerNode->layer() );
1029  const QgsLayerTreeModel *mod = model();
1030  if ( ! mod )
1031  return mImage;
1032  const QgsMapSettings *ms = mod->legendFilterMapSettings();
1033 
1034  QgsRasterDataProvider *prov = layer->dataProvider();
1035  if ( ! prov )
1036  return mImage;
1037 
1038  Q_ASSERT( ! mFetcher );
1039  mFetcher.reset( prov->getLegendGraphicFetcher( ms ) );
1040  if ( mFetcher )
1041  {
1042  connect( mFetcher.get(), &QgsImageFetcher::finish, this, &QgsWmsLegendNode::getLegendGraphicFinished );
1043  connect( mFetcher.get(), &QgsImageFetcher::error, this, &QgsWmsLegendNode::getLegendGraphicErrored );
1044  connect( mFetcher.get(), &QgsImageFetcher::progress, this, &QgsWmsLegendNode::getLegendGraphicProgress );
1045  mFetcher->start();
1046  }
1047  }
1048 
1049  return mImage;
1050 }
1051 
1052 QVariant QgsWmsLegendNode::data( int role ) const
1053 {
1054  if ( role == Qt::DecorationRole )
1055  {
1056  return QPixmap::fromImage( getLegendGraphic() );
1057  }
1058  else if ( role == Qt::SizeHintRole )
1059  {
1060  return getLegendGraphic().size();
1061  }
1062  return QVariant();
1063 }
1064 
1065 QSizeF QgsWmsLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight ) const
1066 {
1067  Q_UNUSED( itemHeight )
1068 
1069  if ( ctx && ctx->painter )
1070  {
1071  switch ( settings.symbolAlignment() )
1072  {
1073  case Qt::AlignLeft:
1074  default:
1075  ctx->painter->drawImage( QRectF( ctx->columnLeft,
1076  ctx->top,
1077  settings.wmsLegendSize().width(),
1078  settings.wmsLegendSize().height() ),
1079  mImage,
1080  QRectF( QPointF( 0, 0 ), mImage.size() ) );
1081  break;
1082 
1083  case Qt::AlignRight:
1084  ctx->painter->drawImage( QRectF( ctx->columnRight - settings.wmsLegendSize().width(),
1085  ctx->top,
1086  settings.wmsLegendSize().width(),
1087  settings.wmsLegendSize().height() ),
1088  mImage,
1089  QRectF( QPointF( 0, 0 ), mImage.size() ) );
1090  break;
1091  }
1092  }
1093  return settings.wmsLegendSize();
1094 }
1095 
1097 {
1098  QByteArray byteArray;
1099  QBuffer buffer( &byteArray );
1100  mImage.save( &buffer, "PNG" );
1101  const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
1102 
1103  QJsonObject json;
1104  json[ QStringLiteral( "icon" ) ] = base64;
1105  return json;
1106 }
1107 
1108 QImage QgsWmsLegendNode::renderMessage( const QString &msg ) const
1109 {
1110  const int fontHeight = 10;
1111  const int margin = fontHeight / 2;
1112  const int nlines = 1;
1113 
1114  const int w = 512, h = fontHeight * nlines + margin * ( nlines + 1 );
1115  QImage image( w, h, QImage::Format_ARGB32_Premultiplied );
1116  QPainter painter;
1117  painter.begin( &image );
1118  painter.setPen( QColor( 255, 0, 0 ) );
1119  painter.setFont( QFont( QStringLiteral( "Chicago" ), fontHeight ) );
1120  painter.fillRect( 0, 0, w, h, QColor( 255, 255, 255 ) );
1121  painter.drawText( 0, margin + fontHeight, msg );
1122  //painter.drawText(0,2*(margin+fontHeight),tr("retrying in 5 seconds…"));
1123  painter.end();
1124 
1125  return image;
1126 }
1127 
1128 void QgsWmsLegendNode::getLegendGraphicProgress( qint64 cur, qint64 tot )
1129 {
1130  QString msg = QStringLiteral( "Downloading... %1/%2" ).arg( cur ).arg( tot );
1131  mImage = renderMessage( msg );
1132  emit dataChanged();
1133 }
1134 
1135 void QgsWmsLegendNode::getLegendGraphicErrored( const QString & )
1136 {
1137  if ( ! mFetcher )
1138  return; // must be coming after finish
1139 
1140  mImage = QImage();
1141  emit dataChanged();
1142 
1143  mFetcher.reset();
1144 
1145  mValid = true; // we consider it valid anyway
1146 }
1147 
1148 void QgsWmsLegendNode::getLegendGraphicFinished( const QImage &image )
1149 {
1150  if ( ! mFetcher )
1151  return; // must be coming after error
1152 
1153  if ( ! image.isNull() )
1154  {
1155  if ( image != mImage )
1156  {
1157  mImage = image;
1158  setUserPatchSize( mImage.size() );
1159  emit dataChanged();
1160  }
1161  mValid = true; // only if not null I guess
1162  }
1163  mFetcher.reset();
1164 }
1165 
1167 {
1168  // TODO: do this only if this extent != prev extent ?
1169  mValid = false;
1170  emit dataChanged();
1171 }
1172 
1173 // -------------------------------------------------------------------------
1174 
1176  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
1177  , mSettings( new QgsDataDefinedSizeLegend( settings ) )
1178 {
1179 }
1180 
1182 {
1183  delete mSettings;
1184 }
1185 
1186 QVariant QgsDataDefinedSizeLegendNode::data( int role ) const
1187 {
1188  if ( role == Qt::DecorationRole )
1189  {
1190  cacheImage();
1191  return QPixmap::fromImage( mImage );
1192  }
1193  else if ( role == Qt::SizeHintRole )
1194  {
1195  cacheImage();
1196  return mImage.size();
1197  }
1198  return QVariant();
1199 }
1200 
1202 {
1203  // setup temporary render context if none specified
1204  QgsRenderContext *context = nullptr;
1205  std::unique_ptr< QgsRenderContext > tempRenderContext;
1206  if ( ctx && ctx->context )
1207  context = ctx->context;
1208  else
1209  {
1210  tempRenderContext = qgis::make_unique< QgsRenderContext >();
1211  // QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
1213  tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
1214  tempRenderContext->setRendererScale( settings.mapScale() );
1215  tempRenderContext->setFlag( QgsRenderContext::Antialiasing, true );
1216  tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
1217  tempRenderContext->setForceVectorOutput( true );
1218  tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
1219  tempRenderContext->setFlag( QgsRenderContext::Antialiasing, true );
1221 
1222  // setup a minimal expression context
1223  QgsExpressionContext expContext;
1225  tempRenderContext->setExpressionContext( expContext );
1226  context = tempRenderContext.get();
1227  }
1228 
1229  if ( context->painter() )
1230  {
1231  context->painter()->save();
1232  context->painter()->translate( ctx->columnLeft, ctx->top );
1233 
1234  // scale to pixels
1235  context->painter()->scale( 1 / context->scaleFactor(), 1 / context->scaleFactor() );
1236  }
1237 
1238  QgsDataDefinedSizeLegend ddsLegend( *mSettings );
1239  ddsLegend.setFont( settings.style( QgsLegendStyle::SymbolLabel ).font() );
1240  ddsLegend.setTextColor( settings.fontColor() );
1241 
1242  QSizeF contentSize;
1243  double labelXOffset;
1244  ddsLegend.drawCollapsedLegend( *context, &contentSize, &labelXOffset );
1245 
1246  if ( context->painter() )
1247  context->painter()->restore();
1248 
1249  ItemMetrics im;
1250  im.symbolSize = QSizeF( ( contentSize.width() - labelXOffset ) / context->scaleFactor(), contentSize.height() / context->scaleFactor() );
1251  im.labelSize = QSizeF( labelXOffset / context->scaleFactor(), contentSize.height() / context->scaleFactor() );
1252  return im;
1253 }
1254 
1255 
1256 void QgsDataDefinedSizeLegendNode::cacheImage() const
1257 {
1258  if ( mImage.isNull() )
1259  {
1260  std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
1261  if ( !context )
1262  {
1263  context.reset( new QgsRenderContext );
1264  Q_ASSERT( context ); // to make cppcheck happy
1265  context->setScaleFactor( 96 / 25.4 );
1266  }
1267  mImage = mSettings->collapsedLegendImage( *context );
1268  }
1269 }
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsLayerTreeLayer::labelExpression
QString labelExpression() const
Returns the expression member of the LayerTreeNode.
Definition: qgslayertreelayer.h:144
QgsMapLayer::emitStyleChanged
void emitStyleChanged()
Triggers an emission of the styleChanged() signal.
Definition: qgsmaplayer.cpp:1853
QgsFeatureRenderer::setLegendSymbolItem
virtual void setLegendSymbolItem(const QString &key, QgsSymbol *symbol)
Sets the symbol to be used for a legend symbol item.
Definition: qgsrenderer.cpp:347
QgsSymbolLegendNode::QgsSymbolLegendNode
QgsSymbolLegendNode(QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItem &item, QObject *parent=nullptr)
Constructor for QgsSymbolLegendNode.
Definition: qgslayertreemodellegendnode.cpp:244
qgsexpressioncontextutils.h
QgsExpressionContext::appendScopes
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
Definition: qgsexpressioncontext.cpp:495
QgsLegendStyle::Symbol
@ Symbol
Symbol icon (excluding label)
Definition: qgslegendstyle.h:47
QgsLegendSettings::symbolAlignment
Qt::AlignmentFlag symbolAlignment() const
Returns the alignment for placement of legend symbols.
Definition: qgslegendsettings.h:291
QgsSymbolLegendNode::exportSymbolToJson
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
Adds a symbol in base64 string within a JSON object with the key "icon".
Definition: qgslayertreemodellegendnode.cpp:670
QgsVectorLayer::createExpressionContextScope
QgsExpressionContextScope * createExpressionContextScope() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgsvectorlayer.cpp:4957
QgsSymbolLegendNode::symbolLabel
QString symbolLabel() const
Label of the symbol, user defined label will be used, otherwise will default to the label made by QGI...
Definition: qgslayertreemodellegendnode.cpp:314
QgsWmsLegendNode::QgsWmsLegendNode
QgsWmsLegendNode(QgsLayerTreeLayer *nodeLayer, QObject *parent=nullptr)
Constructor for QgsWmsLegendNode.
Definition: qgslayertreemodellegendnode.cpp:1015
QgsRenderContext::setPainterFlagsUsingContext
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
Definition: qgsrendercontext.cpp:143
QgsSymbolLegendNode::setCustomSymbol
void setCustomSymbol(QgsSymbol *symbol)
Sets the node's custom symbol.
Definition: qgslayertreemodellegendnode.cpp:350
QgsLegendSettings::useAdvancedEffects
Q_DECL_DEPRECATED bool useAdvancedEffects() const
Definition: qgslegendsettings.cpp:51
QgsLayerTreeLayer::patchShape
QgsLegendPatchShape patchShape() const
Returns the symbol patch shape to use when rendering the legend node symbol.
Definition: qgslayertreelayer.cpp:227
qgsrasterlayer.h
QgsTextFormat::scaledFont
QFont scaledFont(const QgsRenderContext &context, double scaleFactor=1.0) const
Returns a font with the size scaled to match the format's size settings (including units and map unit...
Definition: qgstextformat.cpp:156
QgsLegendSymbolItem::setSymbol
void setSymbol(QgsSymbol *s)
Sets the symbol of the item.
Definition: qgslegendsymbolitem.cpp:79
qgslayertreemodellegendnode.h
QgsRenderContext::expressionContext
QgsExpressionContext & expressionContext()
Gets the expression context.
Definition: qgsrendercontext.h:596
QgsImageLegendNode::drawSymbol
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
Definition: qgslayertreemodellegendnode.cpp:854
QgsLegendSettings::maximumSymbolSize
double maximumSymbolSize() const
Returns the maximum symbol size (in mm).
Definition: qgslegendsettings.h:243
QgsLegendSettings::mmPerMapUnit
Q_DECL_DEPRECATED double mmPerMapUnit() const
Definition: qgslegendsettings.cpp:41
QgsLayerTreeModelLegendNode::ItemContext::columnRight
double columnRight
Right side of current legend column.
Definition: qgslayertreemodellegendnode.h:179
QgsTextRenderer::drawText
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, VAlignment vAlignment=AlignTop)
Draws text within a rectangle using the specified settings.
Definition: qgstextrenderer.cpp:75
QgsSymbolLegendNode::invalidateMapBasedData
void invalidateMapBasedData() override
Notification from model that information from associated map view has changed.
Definition: qgslayertreemodellegendnode.cpp:729
QgsLayerTreeModelLegendNode::data
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
QgsDataDefinedSizeLegendNode::draw
ItemMetrics draw(const QgsLegendSettings &settings, ItemContext *ctx) override
Entry point called from QgsLegendRenderer to do the rendering.
Definition: qgslayertreemodellegendnode.cpp:1201
QgsExpressionContextScope::addVariable
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Definition: qgsexpressioncontext.cpp:93
qgstextrenderer.h
QgsLegendSettings::textWidthMillimeters
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...
Definition: qgslegendsettings.cpp:156
QgsDataDefinedSizeLegend::collapsedLegendImage
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...
Definition: qgsdatadefinedsizelegend.cpp:331
QgsLayerTreeModelLegendNode::ItemContext::patchSize
QSizeF patchSize
Symbol patch size to render for the node.
Definition: qgslayertreemodellegendnode.h:202
QgsLayerTreeModelLegendNode::drawSymbolText
virtual QSizeF drawSymbolText(const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize) const
Draws label on the right side of the item.
Definition: qgslayertreemodellegendnode.cpp:161
qgsexpression.h
QgsSymbolLegendNode::checkAllItems
void checkAllItems()
Checks all items belonging to the same layer as this node.
Definition: qgslayertreemodellegendnode.cpp:374
QgsTextRenderer::AlignCenter
@ AlignCenter
Center align.
Definition: qgstextrenderer.h:61
QgsRasterSymbolLegendNode::drawSymbol
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
Definition: qgslayertreemodellegendnode.cpp:914
QgsRenderContext::UseAdvancedEffects
@ UseAdvancedEffects
Enable layer opacity and blending effects.
Definition: qgsrendercontext.h:74
QgsRenderContext::setFlag
void setFlag(Flag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
Definition: qgsrendercontext.cpp:179
QgsRenderContext::setPainter
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Definition: qgsrendercontext.h:491
qgssymbollayerutils.h
QgsLayerTreeModelLegendNode::setUserPatchSize
virtual void setUserPatchSize(QSizeF size)
Sets the user (overridden) size for the legend node.
Definition: qgslayertreemodellegendnode.cpp:71
QgsLegendSettings::drawText
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
Definition: qgslegendsettings.cpp:116
QgsRasterSymbolLegendNode::exportSymbolToJson
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
Adds a symbol in base64 string within a JSON object with the key "icon".
Definition: qgslayertreemodellegendnode.cpp:972
QgsSymbolLegendNode::patchShape
QgsLegendPatchShape patchShape() const
Returns the symbol patch shape to use when rendering the legend node symbol.
Definition: qgslayertreemodellegendnode.cpp:328
QgsSimpleLegendNode::data
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
Definition: qgslayertreemodellegendnode.cpp:820
QgsDataDefinedSizeLegend
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
Definition: qgsdatadefinedsizelegend.h:42
QgsLegendSettings::mapScale
Q_DECL_DEPRECATED double mapScale() const
Returns the legend map scale.
Definition: qgslegendsettings.cpp:61
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsLegendSettings::style
QgsLegendStyle style(QgsLegendStyle::Style s) const
Returns the style for a legend component.
Definition: qgslegendsettings.h:81
qgsrasterrenderer.h
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
QgsLayerTreeModelLegendNode::exportToJson
QJsonObject exportToJson(const QgsLegendSettings &settings, const QgsRenderContext &context)
Entry point called from QgsLegendRenderer to do the rendering in a JSON object.
Definition: qgslayertreemodellegendnode.cpp:96
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:307
QgsFeatureRenderer::legendSymbolItems
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
Definition: qgsrenderer.cpp:353
qgsfeatureid.h
QgsLayerTreeModelLegendNode::ItemContext::patchShape
QgsLegendPatchShape patchShape
The patch shape to render for the node.
Definition: qgslayertreemodellegendnode.h:193
QgsLayerTreeLayer::name
QString name() const override
Returns the layer's name.
Definition: qgslayertreelayer.cpp:81
QgsLayerTreeModel
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Definition: qgslayertreemodel.h:54
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
qgsimageoperation.h
QgsRenderContext::scaleFactor
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
Definition: qgsrendercontext.h:333
qgsvectorlayerfeaturecounter.h
QgsLegendSettings::rasterStrokeColor
QColor rasterStrokeColor() const
Returns the stroke color for the stroke drawn around raster symbol items.
Definition: qgslegendsettings.h:320
QgsImageLegendNode::data
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
Definition: qgslayertreemodellegendnode.cpp:841
QgsDataDefinedSizeLegendNode::QgsDataDefinedSizeLegendNode
QgsDataDefinedSizeLegendNode(QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent=nullptr)
Construct the node using QgsDataDefinedSizeLegend as definition of the node's appearance.
Definition: qgslayertreemodellegendnode.cpp:1175
QgsImageLegendNode::exportSymbolToJson
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
Adds a symbol in base64 string within a JSON object with the key "icon".
Definition: qgslayertreemodellegendnode.cpp:877
QgsLegendStyle::SymbolLabel
@ SymbolLabel
Symbol label (excluding icon)
Definition: qgslegendstyle.h:48
QgsVectorLayer::featureCount
long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
Definition: qgsvectorlayer.cpp:751
QgsDataDefinedSizeLegendNode::~QgsDataDefinedSizeLegendNode
~QgsDataDefinedSizeLegendNode() override
Definition: qgslayertreemodellegendnode.cpp:1181
QgsLegendSymbolItem::ruleKey
QString ruleKey() const
Returns unique identifier of the rule for identification of the item within renderer.
Definition: qgslegendsymbolitem.h:60
QgsLayerTreeModelLegendNode::model
QgsLayerTreeModel * model() const
Returns pointer to model owning this legend node.
Definition: qgslayertreemodellegendnode.cpp:46
QgsLegendSettings::drawRasterStroke
bool drawRasterStroke() const
Returns whether a stroke will be drawn around raster symbol items.
Definition: qgslegendsettings.h:300
QgsLegendPatchShape
Represents a patch shape for use in map legends.
Definition: qgslegendpatchshape.h:31
QgsLegendSettings::dpi
Q_DECL_DEPRECATED int dpi() const
Definition: qgslegendsettings.cpp:81
QgsGuiUtils::iconSize
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
Definition: qgsguiutils.cpp:250
QgsFeatureRenderer::legendSymbolItemChecked
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
Definition: qgsrenderer.cpp:335
QgsLayerTreeModelLegendNode::exportSymbolToJson
virtual QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const
Adds a symbol in base64 string within a JSON object with the key "icon".
Definition: qgslayertreemodellegendnode.cpp:144
QgsTextRenderer::textWidth
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.
Definition: qgstextrenderer.cpp:496
QgsLayerTreeModelLegendNode::sizeChanged
void sizeChanged()
Emitted when the size of this node changes.
QgsLayerTreeModelLegendNode::flags
virtual Qt::ItemFlags flags() const
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
Definition: qgslayertreemodellegendnode.cpp:51
QgsLayerTreeModel::scaleIconSize
static int scaleIconSize(int standardSize)
Scales an layer tree model icon size to compensate for display pixel density, making the icon size hi...
Definition: qgslayertreemodel.cpp:722
QgsLayerTreeModelLegendNode::draw
virtual ItemMetrics draw(const QgsLegendSettings &settings, ItemContext *ctx)
Entry point called from QgsLegendRenderer to do the rendering.
Definition: qgslayertreemodellegendnode.cpp:80
QgsMapLayer::triggerRepaint
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
Definition: qgsmaplayer.cpp:1826
QgsWmsLegendNode::invalidateMapBasedData
void invalidateMapBasedData() override
Notification from model that information from associated map view has changed.
Definition: qgslayertreemodellegendnode.cpp:1166
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:797
QgsLayerTreeModelLegendNode::ItemMetrics::labelSize
QSizeF labelSize
Definition: qgslayertreemodellegendnode.h:208
QgsLegendSymbolItem
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
Definition: qgslegendsymbolitem.h:37
QgsTextRenderer::Point
@ Point
Text at point of origin draw mode.
Definition: qgstextrenderer.h:44
QgsSymbolLayerUtils::symbolPreviewPixmap
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.
Definition: qgssymbollayerutils.cpp:767
QgsImageFetcher::progress
void progress(qint64 received, qint64 total)
Emitted to report progress.
QgsRasterSymbolLegendNode::QgsRasterSymbolLegendNode
QgsRasterSymbolLegendNode(QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent=nullptr)
Constructor for QgsRasterSymbolLegendNode.
Definition: qgslayertreemodellegendnode.cpp:891
QgsSymbolLegendNode::setPatchShape
void setPatchShape(const QgsLegendPatchShape &shape)
Sets the symbol patch shape to use when rendering the legend node symbol.
Definition: qgslayertreemodellegendnode.cpp:340
QgsSymbolLegendNode::setData
bool setData(const QVariant &value, int role) override
Sets some data associated with the item. Default implementation does nothing and returns false.
Definition: qgslayertreemodellegendnode.cpp:501
QgsLayerTreeModelLegendNode::QgsLayerTreeModelLegendNode
QgsLayerTreeModelLegendNode(QgsLayerTreeLayer *nodeL, QObject *parent=nullptr)
Construct the node with pointer to its parent layer node.
Definition: qgslayertreemodellegendnode.cpp:39
QgsLegendSettings::rasterStrokeWidth
double rasterStrokeWidth() const
Returns the stroke width (in millimeters) for the stroke drawn around raster symbol items.
Definition: qgslegendsettings.h:341
QgsFeatureRenderer::checkLegendSymbolItem
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
item in symbology was checked
Definition: qgsrenderer.cpp:341
QgsLayerTreeModelLegendNode::mEmbeddedInParent
bool mEmbeddedInParent
Definition: qgslayertreemodellegendnode.h:274
qgslayoutitem.h
QgsWmsLegendNode::drawSymbol
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
Definition: qgslayertreemodellegendnode.cpp:1065
QgsRasterRenderer
Raster renderer pipe that applies colors to a raster.
Definition: qgsrasterrenderer.h:39
QgsMarkerSymbol
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:931
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsLegendSettings::minimumSymbolSize
double minimumSymbolSize() const
Returns the minimum symbol size (in mm).
Definition: qgslegendsettings.h:261
QgsLayerTreeModelLegendNode::userPatchSize
virtual QSizeF userPatchSize() const
Returns the user (overridden) size for the legend node.
Definition: qgslayertreemodellegendnode.cpp:63
QgsLegendStyle::alignment
Qt::Alignment alignment() const
Returns the alignment for the legend component.
Definition: qgslegendstyle.h:114
QgsImageLegendNode::QgsImageLegendNode
QgsImageLegendNode(QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent=nullptr)
Constructor for QgsImageLegendNode.
Definition: qgslayertreemodellegendnode.cpp:835
qgslegendsettings.h
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:44
QgsLayerTreeModel::legendFilterMapSettings
const QgsMapSettings * legendFilterMapSettings() const
Returns the current map settings used for the current legend filter (or nullptr if none is enabled)
Definition: qgslayertreemodel.h:250
QgsRasterDataProvider::getLegendGraphicFetcher
virtual QgsImageFetcher * getLegendGraphicFetcher(const QgsMapSettings *mapSettings)
Returns a new image downloader for the raster legend.
Definition: qgsrasterdataprovider.h:312
QgsLegendSettings::wmsLegendSize
QSizeF wmsLegendSize() const
Returns the size (in millimeters) of WMS legend graphics shown in the legend.
Definition: qgslegendsettings.h:359
QgsRenderContext::setMapToPixel
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
Definition: qgsrendercontext.h:420
QgsLegendSettings
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
Definition: qgslegendsettings.h:39
QgsRasterLayer::dataProvider
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
Definition: qgsrasterlayer.cpp:234
QgsLegendStyle::Right
@ Right
Right side.
Definition: qgslegendstyle.h:59
QgsDataDefinedSizeLegend::setTextColor
void setTextColor(const QColor &color)
Sets text color for rendering of labels - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:132
QgsSymbolLegendNode::flags
Qt::ItemFlags flags() const override
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
Definition: qgslayertreemodellegendnode.cpp:260
QgsLayerTreeModelLegendNode::ParentRuleKeyRole
@ ParentRuleKeyRole
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2....
Definition: qgslayertreemodellegendnode.h:66
QgsTextRenderer::textHeight
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode=Point, QFontMetricsF *fontMetrics=nullptr)
Returns the height of a text based on a given format.
Definition: qgstextrenderer.cpp:567
QgsLayerTreeModelLegendNode::ItemContext
Definition: qgslayertreemodellegendnode.h:137
qgsrenderer.h
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:71
QgsScopedQPainterState
Scoped object for saving and restoring a QPainter object's state.
Definition: qgsrendercontext.h:1120
QgsLayerTreeLayer::layer
QgsMapLayer * layer() const
Returns the map layer associated with this node.
Definition: qgslayertreelayer.h:74
qgslayertree.h
QgsLayerTreeModelLegendNode::setEmbeddedInParent
virtual void setEmbeddedInParent(bool embedded)
Definition: qgslayertreemodellegendnode.h:85
QgsLayerTreeModelLegendNode::ItemMetrics::symbolSize
QSizeF symbolSize
Definition: qgslayertreemodellegendnode.h:207
QgsLegendSymbolItem::label
QString label() const
Returns text label.
Definition: qgslegendsymbolitem.h:58
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext.
Definition: qgsexpressioncontext.h:112
QgsRenderContext::setRendererScale
void setRendererScale(double scale)
Sets the renderer map scale.
Definition: qgsrendercontext.h:483
QgsLayerTreeModelLegendNode::ItemContext::maxSiblingSymbolWidth
double maxSiblingSymbolWidth
Largest symbol width, considering all other sibling legend components associated with the current com...
Definition: qgslayertreemodellegendnode.h:186
QgsExpressionContext::appendScope
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Definition: qgsexpressioncontext.cpp:490
qgsvectorlayer.h
QgsRenderContext::setForceVectorOutput
void setForceVectorOutput(bool force)
Sets whether rendering operations should use vector operations instead of any faster raster shortcuts...
Definition: qgsrendercontext.cpp:281
QgsLayerTreeModelLegendNode::setData
virtual bool setData(const QVariant &value, int role)
Sets some data associated with the item. Default implementation does nothing and returns false.
Definition: qgslayertreemodellegendnode.cpp:56
QgsRenderContext::LosslessImageRendering
@ LosslessImageRendering
Render images losslessly whenever possible, instead of the default lossy jpeg rendering used for some...
Definition: qgsrendercontext.h:84
QgsLegendStyle::Left
@ Left
Left side.
Definition: qgslegendstyle.h:58
QgsSymbolLegendNode::toggleAllItems
void toggleAllItems()
Toggle all items belonging to the same layer as this node.
Definition: qgslayertreemodellegendnode.cpp:384
QgsLayerTreeModelLegendNode::ItemMetrics
Definition: qgslayertreemodellegendnode.h:206
QgsDataDefinedSizeLegendNode::data
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
Definition: qgslayertreemodellegendnode.cpp:1186
QgsLayerTreeModelLegendNode::layerNode
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
Definition: qgslayertreemodellegendnode.h:70
QgsWmsLegendNode::exportSymbolToJson
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
Adds a symbol in base64 string within a JSON object with the key "icon".
Definition: qgslayertreemodellegendnode.cpp:1096
QgsLegendSymbolItem::symbol
QgsSymbol * symbol() const
Returns associated symbol. May be nullptr.
Definition: qgslegendsymbolitem.h:56
QgsLayerTreeModelLegendNode::createTemporaryRenderContext
QgsRenderContext * createTemporaryRenderContext() const
Returns a temporary context or nullptr if legendMapViewData are not valid.
Definition: qgslayertreemodellegendnode.cpp:400
QgsMapToPixel
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:38
QgsSymbol::drawPreviewIcon
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:525
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsLegendStyle::font
QFont font() const
Returns the font used for rendering this legend component.
Definition: qgslegendstyle.h:68
QgsLayerTreeNode::customProperty
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.
Definition: qgslayertreenode.cpp:189
QgsLayerTreeModelLegendNode::mPatchShape
QgsLegendPatchShape mPatchShape
Definition: qgslayertreemodellegendnode.h:276
QgsLegendSymbolItem::isCheckable
bool isCheckable() const
Returns whether the item is user-checkable - whether renderer supports enabling/disabling it.
Definition: qgslegendsymbolitem.h:62
QgsSymbolLayerUtils::restrictedSizeSymbol
static QgsSymbol * restrictedSizeSymbol(const QgsSymbol *s, double minSize, double maxSize, QgsRenderContext *context, double &width, double &height)
Creates a new symbol with size restricted to min/max size if original size is out of min/max range.
Definition: qgssymbollayerutils.cpp:4643
QgsSymbolLegendNode::drawSymbol
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
Definition: qgslayertreemodellegendnode.cpp:525
QgsLayerTreeModelLegendNode::ItemContext::top
double top
Top y-position of legend item.
Definition: qgslayertreemodellegendnode.h:163
QgsSymbolLegendNode::setEmbeddedInParent
void setEmbeddedInParent(bool embedded) override
Definition: qgslayertreemodellegendnode.cpp:722
QgsSymbol::Line
@ Line
Line symbol.
Definition: qgssymbol.h:88
QgsSymbolLegendNode::data
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
Definition: qgslayertreemodellegendnode.cpp:437
QgsImageFetcher::finish
void finish(const QImage &legend)
Emitted when the download completes.
QgsLegendSettings::symbolSize
QSizeF symbolSize() const
Returns the default symbol size (in millimeters) used for legend items.
Definition: qgslegendsettings.h:228
QgsLegendSettings::fontHeightCharacterMM
double fontHeightCharacterMM(const QFont &font, QChar c) const
Returns the font height of a character in millimeters.
Definition: qgslegendsettings.cpp:167
QgsSymbol::type
SymbolType type() const
Returns the symbol's type.
Definition: qgssymbol.h:122
QgsVectorLayer::symbolFeatureCountMapChanged
void symbolFeatureCountMapChanged()
Emitted when the feature count for symbols on this layer has been recalculated.
QgsSymbol::Marker
@ Marker
Marker symbol.
Definition: qgssymbol.h:87
QgsSymbolLegendNode::evaluateLabel
QString evaluateLabel(const QgsExpressionContext &context=QgsExpressionContext(), const QString &label=QString())
Evaluates and returns the text label of the current node.
Definition: qgslayertreemodellegendnode.cpp:757
QgsRenderContext::Antialiasing
@ Antialiasing
Use antialiasing while drawing.
Definition: qgsrendercontext.h:79
QgsRenderContext::setExpressionContext
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
Definition: qgsrendercontext.h:588
qgslayertreemodel.h
QgsLayerTreeModelLegendNode::ItemContext::painter
QPainter * painter
Painter.
Definition: qgslayertreemodellegendnode.h:145
QgsWmsLegendNode::data
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
Definition: qgslayertreemodellegendnode.cpp:1052
QgsRenderContext::RenderSymbolPreview
@ RenderSymbolPreview
The render is for a symbol preview only and map based properties may not be available,...
Definition: qgsrendercontext.h:83
QgsLegendSettings::fontColor
QColor fontColor() const
Returns the font color used for legend items.
Definition: qgslegendsettings.h:197
qgsdatadefinedsizelegend.h
QgsLegendSettings::lineSpacing
double lineSpacing() const
Returns the line spacing to use between lines of legend text.
Definition: qgslegendsettings.h:373
QgsRasterSymbolLegendNode::data
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
Definition: qgslayertreemodellegendnode.cpp:898
QgsSymbolLayerUtils::estimateMaxSymbolBleed
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
Definition: qgssymbollayerutils.cpp:822
QgsImageOperation::nonTransparentImageRect
static QRect nonTransparentImageRect(const QImage &image, QSize minSize=QSize(), bool center=false)
Calculates the non-transparent region of an image.
Definition: qgsimageoperation.cpp:773
QgsSymbolLegendNode::minimumIconSize
QSize minimumIconSize() const
Calculates the minimum icon size to prevent cropping.
Definition: qgslayertreemodellegendnode.cpp:269
QgsLegendSettings::fontDescentMillimeters
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
Definition: qgslegendsettings.cpp:181
QgsLayerTreeModelLegendNode::drawSymbol
virtual QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const
Draws symbol on the left side of the item.
Definition: qgslayertreemodellegendnode.cpp:104
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:179
QgsLayerTreeLayer::patchSize
QSizeF patchSize() const
Returns the user (overridden) size for the legend node.
Definition: qgslayertreelayer.h:171
QgsExpressionContextScope::StaticVariable
Single variable definition for use within a QgsExpressionContextScope.
Definition: qgsexpressioncontext.h:119
QgsSymbolLegendNode::customSymbol
QgsSymbol * customSymbol() const
Returns the node's custom symbol.
Definition: qgslayertreemodellegendnode.cpp:345
QgsDataDefinedSizeLegend::drawCollapsedLegend
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...
Definition: qgsdatadefinedsizelegend.cpp:162
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsLegendSymbolList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Definition: qgslegendsymbolitem.h:144
QgsLayerTreeModelLegendNode::RuleKeyRole
@ RuleKeyRole
Rule key of the node (QString)
Definition: qgslayertreemodellegendnode.h:65
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:796
QgsExpression::replaceExpressionText
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...
Definition: qgsexpression.cpp:430
QgsSymbolLegendNode::iconSize
QSize iconSize() const
Definition: qgslayertreemodellegendnode.h:328
QgsLayerTreeModelLegendNode::mUserSize
QSizeF mUserSize
Definition: qgslayertreemodellegendnode.h:277
QgsLayerTreeModelLegendNode::dataChanged
void dataChanged()
Emitted on internal data change so the layer tree model can forward the signal to views.
QgsRasterDataProvider
Base class for raster data providers.
Definition: qgsrasterdataprovider.h:89
QgsRenderContext::setScaleFactor
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
Definition: qgsrendercontext.h:476
QgsLegendSymbolItem::parentRuleKey
QString parentRuleKey() const
Key of the parent legend node.
Definition: qgslegendsymbolitem.h:89
QgsLayerTreeModelLegendNode::ItemContext::columnLeft
double columnLeft
Left side of current legend column.
Definition: qgslayertreemodellegendnode.h:171
QgsLayerTreeModelLegendNode::mLayerNode
QgsLayerTreeLayer * mLayerNode
Definition: qgslayertreemodellegendnode.h:273
QgsSymbolLegendNode::uncheckAllItems
void uncheckAllItems()
Unchecks all items belonging to the same layer as this node.
Definition: qgslayertreemodellegendnode.cpp:379
QgsImageFetcher::error
void error(const QString &msg)
Emitted when an error occurs.
QgsRenderContext::flags
Flags flags() const
Returns combination of flags used for rendering.
Definition: qgsrendercontext.cpp:187
QgsSimpleLegendNode::QgsSimpleLegendNode
QgsSimpleLegendNode(QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon=QIcon(), QObject *parent=nullptr, const QString &key=QString())
Constructor for QgsSimpleLegendNode.
Definition: qgslayertreemodellegendnode.cpp:812
QgsSymbolLegendNode::symbol
const QgsSymbol * symbol() const
Returns the symbol used by the legend node.
Definition: qgslayertreemodellegendnode.cpp:309
QgsLayerTreeModelLegendNode::ItemContext::context
Q_NOWARN_DEPRECATED_POP QgsRenderContext * context
Render context, if available.
Definition: qgslayertreemodellegendnode.h:143
QgsLayerTreeModelLegendNode::mUserLabel
QString mUserLabel
Definition: qgslayertreemodellegendnode.h:275
QgsVectorLayer::renderer
QgsFeatureRenderer * renderer()
Returns renderer.
Definition: qgsvectorlayer.h:892
QgsLegendStyle::margin
double margin(Side side)
Returns the margin (in mm) for the specified side of the component.
Definition: qgslegendstyle.h:91
QgsSymbolLegendNode::setSymbol
void setSymbol(QgsSymbol *symbol)
Sets the symbol to be used by the legend node.
Definition: qgslayertreemodellegendnode.cpp:355
QgsDataDefinedSizeLegend::setFont
void setFont(const QFont &font)
Sets font used for rendering of labels - only valid for collapsed legend.
Definition: qgsdatadefinedsizelegend.h:127
QgsLegendSettings::evaluateItemText
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...
Definition: qgslegendsettings.cpp:91
QgsLayerTreeModelLegendNode
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Definition: qgslayertreemodellegendnode.h:51