QGIS API Documentation  2.12.0-Lyon
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 
21 #include "qgslayertree.h"
22 #include "qgslayertreemodel.h"
23 #include "qgslegendsettings.h"
24 #include "qgsrasterlayer.h"
25 #include "qgsrendererv2.h"
26 #include "qgssymbollayerv2utils.h"
27 #include "qgsimageoperation.h"
28 #include "qgsvectorlayer.h"
29 
30 
31 
33  : QObject( parent )
34  , mLayerNode( nodeL )
35  , mEmbeddedInParent( false )
36 {
37 }
38 
40 {
41 }
42 
44 {
45  return qobject_cast<QgsLayerTreeModel*>( parent() );
46 }
47 
49 {
50  return Qt::ItemIsEnabled;
51 }
52 
53 bool QgsLayerTreeModelLegendNode::setData( const QVariant& value, int role )
54 {
55  Q_UNUSED( value );
56  Q_UNUSED( role );
57  return false;
58 }
59 
60 
62 {
63  QFont symbolLabelFont = settings.style( QgsComposerLegendStyle::SymbolLabel ).font();
64 
65  double textHeight = settings.fontHeightCharacterMM( symbolLabelFont, QChar( '0' ) );
66  // itemHeight here is not realy item height, it is only for symbol
67  // vertical alignment purpose, i.e. ok take single line height
68  // if there are more lines, thos run under the symbol
69  double itemHeight = qMax(( double ) settings.symbolSize().height(), textHeight );
70 
71  ItemMetrics im;
72  im.symbolSize = drawSymbol( settings, ctx, itemHeight );
73  im.labelSize = drawSymbolText( settings, ctx, im.symbolSize );
74  return im;
75 }
76 
77 
78 QSizeF QgsLayerTreeModelLegendNode::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
79 {
80  QIcon symbolIcon = data( Qt::DecorationRole ).value<QIcon>();
81  if ( symbolIcon.isNull() )
82  return QSizeF();
83 
84  if ( ctx )
85  symbolIcon.paint( ctx->painter, ctx->point.x(), ctx->point.y() + ( itemHeight - settings.symbolSize().height() ) / 2,
86  settings.symbolSize().width(), settings.symbolSize().height() );
87  return settings.symbolSize();
88 }
89 
90 
92 {
93  QSizeF labelSize( 0, 0 );
94 
95  QFont symbolLabelFont = settings.style( QgsComposerLegendStyle::SymbolLabel ).font();
96  double textHeight = settings.fontHeightCharacterMM( symbolLabelFont, QChar( '0' ) );
97 
98  QStringList lines = settings.splitStringForWrapping( data( Qt::DisplayRole ).toString() );
99 
100  labelSize.rheight() = lines.count() * textHeight + ( lines.count() - 1 ) * settings.lineSpacing();
101 
102  double labelX = 0.0, labelY = 0.0;
103  if ( ctx )
104  {
105  ctx->painter->setPen( settings.fontColor() );
106 
107  labelX = ctx->point.x() + qMax(( double ) symbolSize.width(), ctx->labelXOffset );
108  labelY = ctx->point.y();
109 
110  // Vertical alignment of label with symbol
111  if ( labelSize.height() < symbolSize.height() )
112  labelY += symbolSize.height() / 2 + textHeight / 2; // label centered with symbol
113  else
114  labelY += textHeight; // label starts at top and runs under symbol
115  }
116 
117  for ( QStringList::Iterator itemPart = lines.begin(); itemPart != lines.end(); ++itemPart )
118  {
119  labelSize.rwidth() = qMax( settings.textWidthMillimeters( symbolLabelFont, *itemPart ), double( labelSize.width() ) );
120 
121  if ( ctx )
122  {
123  settings.drawText( ctx->painter, labelX, labelY, *itemPart, symbolLabelFont );
124  if ( itemPart != lines.end() )
125  labelY += settings.lineSpacing() + textHeight;
126  }
127  }
128 
129  return labelSize;
130 }
131 
132 // -------------------------------------------------------------------------
133 
134 
136  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
137  , mItem( item )
138  , mSymbolUsesMapUnits( false )
139  , mIconSize( 16, 16 )
140 {
141  updateLabel();
142 
143  if ( mItem.symbol() )
144  mSymbolUsesMapUnits = ( mItem.symbol()->outputUnit() != QgsSymbolV2::MM );
145 }
146 
148 {
149 }
150 
152 {
153  if ( mItem.isCheckable() )
154  return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
155  else
156  return Qt::ItemIsEnabled;
157 }
158 
159 
161 {
162  QSize minSz( 16, 16 );
163  if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Marker )
164  {
165  QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
168  context.data() ).toImage(),
169  minSz,
170  true ).size();
171  }
172  else if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Line )
173  {
174  QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
177  context.data() ).toImage(),
178  minSz,
179  true ).size();
180  }
181 
182  if ( mItem.level() != 0 && !( model() && model()->testFlag( QgsLayerTreeModel::ShowLegendAsTree ) ) )
183  minSz.setWidth( mItem.level() * indentSize + minSz.width() );
184 
185  return minSz;
186 }
187 
188 inline
189 QgsRenderContext * QgsSymbolV2LegendNode::createTemporaryRenderContext() const
190 {
191  double scale = 0.0;
192  double mupp = 0.0;
193  int dpi = 0;
194  if ( model() )
195  model()->legendMapViewData( &mupp, &dpi, &scale );
196  bool validData = mupp != 0 && dpi != 0 && scale != 0;
197 
198  // setup temporary render context
200  context->setScaleFactor( dpi / 25.4 );
201  context->setRendererScale( scale );
202  context->setMapToPixel( QgsMapToPixel( mupp ) );
203  return validData ? context.take() : 0;
204 }
205 
207 {
208  if ( role == Qt::DisplayRole )
209  {
210  return mLabel;
211  }
212  else if ( role == Qt::EditRole )
213  {
214  return mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
215  }
216  else if ( role == Qt::DecorationRole )
217  {
218  if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
219  {
220  QPixmap pix;
221  if ( mItem.symbol() )
222  {
223  QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
224  pix = QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), mIconSize, context.data() );
225  }
226  else
227  {
228  pix = QPixmap( mIconSize );
229  pix.fill( Qt::transparent );
230  }
231 
232  if ( mItem.level() == 0 || ( model() && model()->testFlag( QgsLayerTreeModel::ShowLegendAsTree ) ) )
233  mPixmap = pix;
234  else
235  {
236  // ident the symbol icon to make it look like a tree structure
237  QPixmap pix2( pix.width() + mItem.level() * indentSize, pix.height() );
238  pix2.fill( Qt::transparent );
239  QPainter p( &pix2 );
240  p.drawPixmap( mItem.level() * indentSize, 0, pix );
241  p.end();
242  mPixmap = pix2;
243  }
244  }
245  return mPixmap;
246  }
247  else if ( role == Qt::CheckStateRole )
248  {
249  if ( !mItem.isCheckable() )
250  return QVariant();
251 
252  QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( mLayerNode->layer() );
253  if ( !vlayer || !vlayer->rendererV2() )
254  return QVariant();
255 
256  return vlayer->rendererV2()->legendSymbolItemChecked( mItem.ruleKey() ) ? Qt::Checked : Qt::Unchecked;
257  }
258  else if ( role == RuleKeyRole )
259  {
260  return mItem.ruleKey();
261  }
262  else if ( role == SymbolV2LegacyRuleKeyRole )
263  {
264  return QVariant::fromValue<void*>( mItem.legacyRuleKey() );
265  }
266  else if ( role == ParentRuleKeyRole )
267  {
268  return mItem.parentRuleKey();
269  }
270 
271  return QVariant();
272 }
273 
274 bool QgsSymbolV2LegendNode::setData( const QVariant& value, int role )
275 {
276  if ( role != Qt::CheckStateRole )
277  return false;
278 
279  if ( !mItem.isCheckable() )
280  return false;
281 
282  QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( mLayerNode->layer() );
283  if ( !vlayer || !vlayer->rendererV2() )
284  return false;
285 
286  vlayer->rendererV2()->checkLegendSymbolItem( mItem.ruleKey(), value == Qt::Checked );
287 
288  emit dataChanged();
289 
290  vlayer->triggerRepaint();
291 
292  return true;
293 }
294 
295 
296 
297 QSizeF QgsSymbolV2LegendNode::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
298 {
299  QgsSymbolV2* s = mItem.symbol();
300  if ( !s )
301  {
302  return QSizeF();
303  }
304 
305  // setup temporary render context
306  QgsRenderContext context;
307  context.setScaleFactor( settings.dpi() / 25.4 );
308  context.setRendererScale( settings.mapScale() );
309  context.setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * context.scaleFactor() ) ) );
310  context.setForceVectorOutput( true );
311  context.setPainter( ctx ? ctx->painter : 0 );
312 
313  //Consider symbol size for point markers
314  double height = settings.symbolSize().height();
315  double width = settings.symbolSize().width();
316  double size = 0;
317  //Center small marker symbols
318  double widthOffset = 0;
319  double heightOffset = 0;
320 
321  if ( QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s ) )
322  {
323  // allow marker symbol to occupy bigger area if necessary
324  size = QgsSymbolLayerV2Utils::convertToPainterUnits( context, markerSymbol->size(), s->outputUnit(), s->mapUnitScale() ) / context.scaleFactor();
325  height = size;
326  width = size;
327  if ( width < settings.symbolSize().width() )
328  {
329  widthOffset = ( settings.symbolSize().width() - width ) / 2.0;
330  }
331  if ( height < settings.symbolSize().height() )
332  {
333  heightOffset = ( settings.symbolSize().height() - height ) / 2.0;
334  }
335  }
336 
337  if ( ctx )
338  {
339  double currentXPosition = ctx->point.x();
340  double currentYCoord = ctx->point.y() + ( itemHeight - settings.symbolSize().height() ) / 2;
341  QPainter* p = ctx->painter;
342 
343  //setup painter scaling to dots so that raster symbology is drawn to scale
344  double dotsPerMM = context.scaleFactor();
345 
346  int opacity = 255;
347  if ( QgsVectorLayer* vectorLayer = dynamic_cast<QgsVectorLayer*>( layerNode()->layer() ) )
348  opacity = 255 - ( 255 * vectorLayer->layerTransparency() / 100 );
349 
350  p->save();
351  p->setRenderHint( QPainter::Antialiasing );
352  p->translate( currentXPosition + widthOffset, currentYCoord + heightOffset );
353  p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
354  if ( opacity != 255 && settings.useAdvancedEffects() )
355  {
356  //semi transparent layer, so need to draw symbol to an image (to flatten it first)
357  //create image which is same size as legend rect, in case symbol bleeds outside its alloted space
358  QSize tempImageSize( width * dotsPerMM, height * dotsPerMM );
359  QImage tempImage = QImage( tempImageSize, QImage::Format_ARGB32 );
360  tempImage.fill( Qt::transparent );
361  QPainter imagePainter( &tempImage );
362  context.setPainter( &imagePainter );
363  s->drawPreviewIcon( &imagePainter, tempImageSize, &context );
364  context.setPainter( ctx->painter );
365  //reduce opacity of image
366  imagePainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
367  imagePainter.fillRect( tempImage.rect(), QColor( 0, 0, 0, opacity ) );
368  imagePainter.end();
369  //draw rendered symbol image
370  p->drawImage( 0, 0, tempImage );
371  }
372  else
373  {
374  s->drawPreviewIcon( p, QSize( width * dotsPerMM, height * dotsPerMM ), &context );
375  }
376  p->restore();
377  }
378 
379  return QSizeF( qMax( width + 2 * widthOffset, ( double ) settings.symbolSize().width() ),
380  qMax( height + 2 * heightOffset, ( double ) settings.symbolSize().height() ) );
381 }
382 
383 
385 {
387  updateLabel();
388 }
389 
390 
392 {
393  if ( mSymbolUsesMapUnits )
394  {
395  mPixmap = QPixmap();
396  emit dataChanged();
397  }
398 }
399 
400 
401 void QgsSymbolV2LegendNode::updateLabel()
402 {
403  bool showFeatureCount = mLayerNode->customProperty( "showFeatureCount", 0 ).toBool();
404  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mLayerNode->layer() );
405 
406  if ( mEmbeddedInParent )
407  {
408  QString layerName = mLayerNode->layerName();
409  if ( !mLayerNode->customProperty( "legend/title-label" ).isNull() )
410  layerName = mLayerNode->customProperty( "legend/title-label" ).toString();
411 
412  mLabel = mUserLabel.isEmpty() ? layerName : mUserLabel;
413  if ( showFeatureCount && vl && vl->featureCount() >= 0 )
414  mLabel += QString( " [%1]" ).arg( vl->featureCount() );
415  }
416  else
417  {
418  mLabel = mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
419  if ( showFeatureCount && vl && mItem.legacyRuleKey() )
420  mLabel += QString( " [%1]" ).arg( vl->featureCount( mItem.legacyRuleKey() ) );
421  }
422 }
423 
424 
425 
426 // -------------------------------------------------------------------------
427 
428 
429 QgsSimpleLegendNode::QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon, QObject* parent, const QString& key )
430  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
431  , mLabel( label )
432  , mIcon( icon )
433  , mKey( key )
434 {
435 }
436 
438 {
439  if ( role == Qt::DisplayRole || role == Qt::EditRole )
440  return mUserLabel.isEmpty() ? mLabel : mUserLabel;
441  else if ( role == Qt::DecorationRole )
442  return mIcon;
443  else if ( role == RuleKeyRole && !mKey.isEmpty() )
444  return mKey;
445  else
446  return QVariant();
447 }
448 
449 
450 // -------------------------------------------------------------------------
451 
453  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
454  , mImage( img )
455 {
456 }
457 
459 {
460  if ( role == Qt::DecorationRole )
461  {
462  return QPixmap::fromImage( mImage );
463  }
464  else if ( role == Qt::SizeHintRole )
465  {
466  return mImage.size();
467  }
468  return QVariant();
469 }
470 
471 QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
472 {
473  Q_UNUSED( itemHeight );
474 
475  if ( ctx )
476  {
477  ctx->painter->drawImage( QRectF( ctx->point.x(), ctx->point.y(), settings.wmsLegendSize().width(), settings.wmsLegendSize().height() ),
478  mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
479  }
480  return settings.wmsLegendSize();
481 }
482 
483 // -------------------------------------------------------------------------
484 
486  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
487  , mColor( color )
488  , mLabel( label )
489 {
490 }
491 
493 {
494  if ( role == Qt::DecorationRole )
495  {
496  QSize iconSize( 16, 16 ); // TODO: configurable?
497  QPixmap pix( iconSize );
498  pix.fill( mColor );
499  return QIcon( pix );
500  }
501  else if ( role == Qt::DisplayRole || role == Qt::EditRole )
502  return mUserLabel.isEmpty() ? mLabel : mUserLabel;
503  else
504  return QVariant();
505 }
506 
507 
508 QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
509 {
510  if ( ctx )
511  {
512  QColor itemColor = mColor;
513  if ( QgsRasterLayer* rasterLayer = dynamic_cast<QgsRasterLayer*>( layerNode()->layer() ) )
514  {
515  if ( QgsRasterRenderer* rasterRenderer = rasterLayer->renderer() )
516  itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
517  }
518  ctx->painter->setBrush( itemColor );
519 
520  if ( settings.drawRasterBorder() )
521  {
522  QPen pen;
523  pen.setColor( settings.rasterBorderColor() );
524  pen.setWidthF( settings.rasterBorderWidth() );
525  pen.setJoinStyle( Qt::MiterJoin );
526  ctx->painter->setPen( pen );
527  }
528  else
529  {
530  ctx->painter->setPen( Qt::NoPen );
531  }
532 
533  ctx->painter->drawRect( QRectF( ctx->point.x(), ctx->point.y() + ( itemHeight - settings.symbolSize().height() ) / 2,
534  settings.symbolSize().width(), settings.symbolSize().height() ) );
535  }
536  return settings.symbolSize();
537 }
538 
539 // -------------------------------------------------------------------------
540 
542  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
543  , mValid( false )
544 {
545 }
546 
547 const QImage& QgsWMSLegendNode::getLegendGraphic() const
548 {
549  if ( ! mValid && ! mFetcher )
550  {
551  // or maybe in presence of a downloader we should just delete it
552  // and start a new one ?
553 
554  QgsRasterLayer* layer = qobject_cast<QgsRasterLayer*>( mLayerNode->layer() );
555  const QgsLayerTreeModel* mod = model();
556  if ( ! mod ) return mImage;
557  const QgsMapSettings* ms = mod->legendFilterByMap();
558 
559  QgsRasterDataProvider* prov = layer->dataProvider();
560 
561  Q_ASSERT( ! mFetcher );
562  mFetcher.reset( prov->getLegendGraphicFetcher( ms ) );
563  if ( mFetcher )
564  {
565  connect( mFetcher.data(), SIGNAL( finish( const QImage& ) ), this, SLOT( getLegendGraphicFinished( const QImage& ) ) );
566  connect( mFetcher.data(), SIGNAL( error( const QString& ) ), this, SLOT( getLegendGraphicErrored( const QString& ) ) );
567  connect( mFetcher.data(), SIGNAL( progress( qint64, qint64 ) ), this, SLOT( getLegendGraphicProgress( qint64, qint64 ) ) );
568  mFetcher->start();
569  } // else QgsDebugMsg("XXX No legend supported ?");
570 
571  }
572 
573  return mImage;
574 }
575 
577 {
578  //QgsDebugMsg( QString("XXX data called with role %1 -- mImage size is %2x%3").arg(role).arg(mImage.width()).arg(mImage.height()) );
579 
580  if ( role == Qt::DecorationRole )
581  {
582  return QPixmap::fromImage( getLegendGraphic() );
583  }
584  else if ( role == Qt::SizeHintRole )
585  {
586  return getLegendGraphic().size();
587  }
588  return QVariant();
589 }
590 
591 QSizeF QgsWMSLegendNode::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
592 {
593  Q_UNUSED( itemHeight );
594 
595  if ( ctx )
596  {
597  ctx->painter->drawImage( QRectF( ctx->point, settings.wmsLegendSize() ),
598  mImage,
599  QRectF( QPointF( 0, 0 ), mImage.size() ) );
600  }
601  return settings.wmsLegendSize();
602 }
603 
604 /* private */
605 QImage QgsWMSLegendNode::renderMessage( const QString& msg ) const
606 {
607  const int fontHeight = 10;
608  const int margin = fontHeight / 2;
609  const int nlines = 1;
610 
611  const int w = 512, h = fontHeight * nlines + margin * ( nlines + 1 );
612  QImage theImage( w, h, QImage::Format_ARGB32_Premultiplied );
613  QPainter painter;
614  painter.begin( &theImage );
615  painter.setPen( QColor( 255, 0, 0 ) );
616  painter.setFont( QFont( "Chicago", fontHeight ) );
617  painter.fillRect( 0, 0, w, h, QColor( 255, 255, 255 ) );
618  painter.drawText( 0, margin + fontHeight, msg );
619  //painter.drawText(0,2*(margin+fontHeight),QString("retrying in 5 seconds..."));
620  painter.end();
621 
622  return theImage;
623 }
624 
625 void QgsWMSLegendNode::getLegendGraphicProgress( qint64 cur, qint64 tot )
626 {
627  QString msg = QString( "Downloading... %1/%2" ).arg( cur ).arg( tot );
628  //QgsDebugMsg ( QString("XXX %1").arg(msg) );
629  mImage = renderMessage( msg );
630  emit dataChanged();
631 }
632 
633 void QgsWMSLegendNode::getLegendGraphicErrored( const QString& msg )
634 {
635  if ( ! mFetcher ) return; // must be coming after finish
636 
637  mImage = renderMessage( msg );
638  //QgsDebugMsg( QString("XXX emitting dataChanged after writing an image of %1x%2").arg(mImage.width()).arg(mImage.height()) );
639 
640  emit dataChanged();
641 
642  mFetcher.reset();
643 
644  mValid = true; // we consider it valid anyway
645  // ... but remove validity after 5 seconds
646  //QTimer::singleShot(5000, this, SLOT(invalidateMapBasedData()));
647 }
648 
649 void QgsWMSLegendNode::getLegendGraphicFinished( const QImage& theImage )
650 {
651  if ( ! mFetcher ) return; // must be coming after error
652 
653  //QgsDebugMsg( QString("XXX legend graphic finished, image is %1x%2").arg(theImage.width()).arg(theImage.height()) );
654  if ( ! theImage.isNull() )
655  {
656  if ( theImage != mImage )
657  {
658  mImage = theImage;
659  //QgsDebugMsg( QString("XXX emitting dataChanged") );
660  emit dataChanged();
661  }
662  mValid = true; // only if not null I guess
663  }
664  mFetcher.reset();
665 }
666 
668 {
669  //QgsDebugMsg( QString("XXX invalidateMapBasedData called") );
670  // TODO: do this only if this extent != prev extent ?
671  mValid = false;
672  emit dataChanged();
673 }
void setForceVectorOutput(bool force)
QSize size() const
QgsSymbolV2LegendNode(QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItemV2 &item, QObject *parent=0)
int width() const
double lineSpacing() const
int width() const
bool end()
void fillRect(const QRectF &rectangle, const QBrush &brush)
void setCompositionMode(CompositionMode mode)
void setRenderHint(RenderHint hint, bool on)
void fill(const QColor &color)
SymbolType type() const
Definition: qgssymbolv2.h:95
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QgsMapLayer * layer() const
virtual bool setData(const QVariant &value, int role)
Set some data associated with the item.
virtual QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
virtual QVariant data(int role) const override
Return data associated with the item.
qreal & rwidth()
void scale(qreal sx, qreal sy)
void setRendererScale(double scale)
QString label() const
Return text label.
QgsSymbolV2 * legacyRuleKey() const
Used for older code that identifies legend entries from symbol pointer within renderer.
virtual QgsImageFetcher * getLegendGraphicFetcher(const QgsMapSettings *mapSettings)
Get an image downloader for the raster legend.
virtual void invalidateMapBasedData() override
Notification from model that information from associated map view has changed.
QgsWMSLegendNode(QgsLayerTreeLayer *nodeLayer, QObject *parent=0)
void save()
QgsMapUnitScale mapUnitScale() const
QSize minimumIconSize() const
Get the minimum icon size to prevent cropping.
T value() const
QgsSimpleLegendNode(QgsLayerTreeLayer *nodeLayer, const QString &label, const QIcon &icon=QIcon(), QObject *parent=0, const QString &key=QString())
QString parentRuleKey() const
Key of the parent legend node.
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
void setJoinStyle(Qt::PenJoinStyle style)
void setAlpha(int alpha)
int level() const
Identation level that tells how deep the item is in a hierarchy of items. For flat lists level is 0...
Line symbol.
Definition: qgssymbolv2.h:71
virtual QVariant data(int role) const override
Return data associated with the item.
virtual Qt::ItemFlags flags() const
Return item flags associated with the item.
virtual QVariant data(int role) const override
Return data associated with the item.
double mmPerMapUnit() const
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
double scaleFactor() const
bool isNull() const
QString ruleKey() const
Return unique identifier of the rule for identification of the item within renderer.
virtual Qt::ItemFlags flags() const override
Return item flags associated with the item.
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
item in symbology was checked
Marker symbol.
Definition: qgssymbolv2.h:70
QgsSymbolV2 * symbol() const
Return associated symbol. May be null.
static QRect nonTransparentImageRect(const QImage &image, const QSize &minSize=QSize(), bool center=false)
Calculates the non-transparent region of an image.
void reset(T *other)
The QgsMapSettings class contains configuration for rendering of the map.
QColor fontColor() const
long featureCount(QgsSymbolV2 *symbol)
Number of features rendered with specified symbol.
void drawRect(const QRectF &rectangle)
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
The QgsLayerTreeModel class is model implementation for Qt item views framework.
rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2...
void setFont(const QFont &font)
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
The output shall be in millimeters.
Definition: qgssymbolv2.h:57
int count(const T &value) const
qreal x() const
qreal y() const
void setScaleFactor(double factor)
bool isNull() const
void fill(uint pixelValue)
QColor rasterBorderColor() const
Returns the border color for the border drawn around raster symbol items.
static double convertToPainterUnits(const QgsRenderContext &c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale())
Converts a size from the specied units to painter units.
void setPen(const QColor &color)
int width() const
virtual QVariant data(int role) const override
Return data associated with the item.
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void setWidth(int width)
virtual void setEmbeddedInParent(bool embedded) override
virtual QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const
Draws symbol on the left side of the item.
bool isEmpty() const
QRect rect() const
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
void legendMapViewData(double *mapUnitsPerPixel, int *dpi, double *scale)
Get hints about map view - to be used in legend nodes.
void triggerRepaint()
Will advice the map canvas (and any other interested party) that this layer requires to be repainted...
QgsLayerTreeLayer * layerNode() const
Return pointer to the parent layer node.
QStringList splitStringForWrapping(const QString &stringToSplt) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
void setWidthF(qreal width)
void setBrush(const QBrush &brush)
void setPainter(QPainter *p)
void drawText(const QPointF &position, const QString &text)
typedef Iterator
void setColor(const QColor &color)
void paint(QPainter *painter, const QRect &rect, QFlags< Qt::AlignmentFlag > alignment, Mode mode, State state) const
virtual void invalidateMapBasedData() override
Notification from model that information from associated map view has changed.
void dataChanged()
Emitted on internal data change so the layer tree model can forward the signal to views...
T * data() const
iterator end()
bool testFlag(Flag f) const
Check whether a flag is enabled.
bool isNull() const
int height() const
virtual ItemMetrics draw(const QgsLegendSettings &settings, ItemContext *ctx)
Entry point called from QgsLegendRenderer to do the rendering.
double fontHeightCharacterMM(const QFont &font, const QChar &c) const
Returns the font height of a character in millimeters.
double mapScale() const
void restore()
virtual QSizeF drawSymbolText(const QgsLegendSettings &settings, ItemContext *ctx, const QSizeF &symbolSize) const
Draws label on the right side of the item.
virtual bool setData(const QVariant &value, int role) override
Set some data associated with the item.
virtual void setEmbeddedInParent(bool embedded)
Contains information about the context of a rendering operation.
bool useAdvancedEffects() const
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, QFlags< Qt::ImageConversionFlag > flags)
bool drawRasterBorder() const
Returns whether a border will be drawn around raster symbol items.
QgsRasterSymbolLegendNode(QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent=0)
double rasterBorderWidth() const
Returns the border width (in millimeters) for the border drawn around raster symbol items...
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
QPointF point
Top-left corner of the legend item.
bool isNull() const
QgsLayerTreeModel * model() const
Return pointer to model owning this legend node.
void setMapToPixel(const QgsMapToPixel &mtp)
QgsSymbolV2::OutputUnit outputUnit() const
virtual QVariant data(int role) const =0
Return data associated with the item.
QSize size() const
bool toBool() const
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
void translate(const QPointF &offset)
for QgsSymbolV2LegendNode only - legacy rule key (void ptr, to be cast to QgsSymbolV2 ptr) ...
int height() const
bool isCheckable() const
Return whether the item is user-checkable - whether renderer supports enabling/disabling it...
QgsRasterDataProvider * dataProvider()
Returns the data provider.
qreal & rheight()
virtual QVariant data(int role) const override
Return data associated with the item.
QSizeF symbolSize() const
QSizeF wmsLegendSize() const
qreal height() const
For legends that support it, will show them in a tree instead of a list (needs also ShowLegend)...
static QPixmap symbolPreviewPixmap(QgsSymbolV2 *symbol, QSize size, QgsRenderContext *customContext=0)
void drawPreviewIcon(QPainter *painter, QSize size, QgsRenderContext *customContext=0)
Draw icon of the symbol that occupyies area given by size using the painter.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString layerName() const
QObject * parent() const
QgsImageLegendNode(QgsLayerTreeLayer *nodeLayer, const QImage &img, QObject *parent=0)
Represents a vector layer which manages a vector based data sets.
double labelXOffset
offset from the left side where label should start
bool begin(QPaintDevice *device)
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
iterator begin()
Raster renderer pipe that applies colors to a raster.
qreal width() const
QgsLayerTreeModelLegendNode(QgsLayerTreeLayer *nodeL, QObject *parent=0)
Construct the node with pointer to its parent layer node.
Layer tree node points to a map layer.
Base class for raster data providers.
typedef ItemFlags
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...