QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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;
163  if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Marker )
164  {
165  QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
167  QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), QSize( 512, 512 ), context.data() ).toImage(),
168  mIconSize,
169  true ).size();
170  }
171  else if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Line )
172  {
173  QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
175  QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), QSize( mIconSize.width(), 512 ),
176  context.data() ).toImage(),
177  mIconSize,
178  true ).size();
179  }
180  else
181  {
182  minSz = mIconSize;
183  }
184 
185  if ( mItem.level() != 0 && !( model() && model()->testFlag( QgsLayerTreeModel::ShowLegendAsTree ) ) )
186  minSz.setWidth( indentSize + minSz.width() );
187 
188  return minSz;
189 }
190 
191 inline
192 QgsRenderContext * QgsSymbolV2LegendNode::createTemporaryRenderContext() const
193 {
194  double scale = 0.0;
195  double mupp = 0.0;
196  int dpi = 0;
197  if ( model() )
198  model()->legendMapViewData( &mupp, &dpi, &scale );
199  bool validData = mupp != 0 && dpi != 0 && scale != 0;
200 
201  // setup temporary render context
203  context->setScaleFactor( dpi / 25.4 );
204  context->setRendererScale( scale );
205  context->setMapToPixel( QgsMapToPixel( mupp ) );
206  return validData ? context.take() : 0;
207 }
208 
210 {
211  if ( role == Qt::DisplayRole )
212  {
213  return mLabel;
214  }
215  else if ( role == Qt::EditRole )
216  {
217  return mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
218  }
219  else if ( role == Qt::DecorationRole )
220  {
221  if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
222  {
223  QPixmap pix;
224  if ( mItem.symbol() )
225  {
226  QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
227  pix = QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), mIconSize, context.data() );
228  }
229  else
230  {
231  pix = QPixmap( mIconSize );
232  pix.fill( Qt::transparent );
233  }
234 
235  if ( mItem.level() == 0 || ( model() && model()->testFlag( QgsLayerTreeModel::ShowLegendAsTree ) ) )
236  mPixmap = pix;
237  else
238  {
239  // ident the symbol icon to make it look like a tree structure
240  QPixmap pix2( pix.width() + mItem.level() * indentSize, pix.height() );
241  pix2.fill( Qt::transparent );
242  QPainter p( &pix2 );
243  p.drawPixmap( mItem.level() * indentSize, 0, pix );
244  p.end();
245  mPixmap = pix2;
246  }
247  }
248  return mPixmap;
249  }
250  else if ( role == Qt::CheckStateRole )
251  {
252  if ( !mItem.isCheckable() )
253  return QVariant();
254 
255  QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( mLayerNode->layer() );
256  if ( !vlayer || !vlayer->rendererV2() )
257  return QVariant();
258 
259  return vlayer->rendererV2()->legendSymbolItemChecked( mItem.ruleKey() ) ? Qt::Checked : Qt::Unchecked;
260  }
261  else if ( role == RuleKeyRole )
262  {
263  return mItem.ruleKey();
264  }
265  else if ( role == SymbolV2LegacyRuleKeyRole )
266  {
267  return QVariant::fromValue<void*>( mItem.legacyRuleKey() );
268  }
269  else if ( role == ParentRuleKeyRole )
270  {
271  return mItem.parentRuleKey();
272  }
273 
274  return QVariant();
275 }
276 
277 bool QgsSymbolV2LegendNode::setData( const QVariant& value, int role )
278 {
279  if ( role != Qt::CheckStateRole )
280  return false;
281 
282  if ( !mItem.isCheckable() )
283  return false;
284 
285  QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( mLayerNode->layer() );
286  if ( !vlayer || !vlayer->rendererV2() )
287  return false;
288 
289  vlayer->rendererV2()->checkLegendSymbolItem( mItem.ruleKey(), value == Qt::Checked );
290 
291  emit dataChanged();
292 
293  vlayer->triggerRepaint();
294 
295  return true;
296 }
297 
298 
299 
300 QSizeF QgsSymbolV2LegendNode::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
301 {
302  QgsSymbolV2* s = mItem.symbol();
303  if ( !s )
304  {
305  return QSizeF();
306  }
307 
308  // setup temporary render context
309  QgsRenderContext context;
310  context.setScaleFactor( settings.dpi() / 25.4 );
311  context.setRendererScale( settings.mapScale() );
312  context.setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * context.scaleFactor() ) ) );
313  context.setForceVectorOutput( true );
314  context.setPainter( ctx ? ctx->painter : 0 );
315 
316  //Consider symbol size for point markers
317  double height = settings.symbolSize().height();
318  double width = settings.symbolSize().width();
319  double size = 0;
320  //Center small marker symbols
321  double widthOffset = 0;
322  double heightOffset = 0;
323 
324  if ( QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s ) )
325  {
326  // allow marker symbol to occupy bigger area if necessary
327  size = markerSymbol->size() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context, s->outputUnit(), s->mapUnitScale() ) / context.scaleFactor();
328  height = size;
329  width = size;
330  if ( width < settings.symbolSize().width() )
331  {
332  widthOffset = ( settings.symbolSize().width() - width ) / 2.0;
333  }
334  if ( height < settings.symbolSize().height() )
335  {
336  heightOffset = ( settings.symbolSize().height() - height ) / 2.0;
337  }
338  }
339 
340  if ( ctx )
341  {
342  double currentXPosition = ctx->point.x();
343  double currentYCoord = ctx->point.y() + ( itemHeight - settings.symbolSize().height() ) / 2;
344  QPainter* p = ctx->painter;
345 
346  //setup painter scaling to dots so that raster symbology is drawn to scale
347  double dotsPerMM = context.scaleFactor();
348 
349  int opacity = 255;
350  if ( QgsVectorLayer* vectorLayer = dynamic_cast<QgsVectorLayer*>( layerNode()->layer() ) )
351  opacity = 255 - ( 255 * vectorLayer->layerTransparency() / 100 );
352 
353  p->save();
354  p->setRenderHint( QPainter::Antialiasing );
355  p->translate( currentXPosition + widthOffset, currentYCoord + heightOffset );
356  p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
357  if ( opacity != 255 && settings.useAdvancedEffects() )
358  {
359  //semi transparent layer, so need to draw symbol to an image (to flatten it first)
360  //create image which is same size as legend rect, in case symbol bleeds outside its alloted space
361  QSize tempImageSize( width * dotsPerMM, height * dotsPerMM );
362  QImage tempImage = QImage( tempImageSize, QImage::Format_ARGB32 );
363  tempImage.fill( Qt::transparent );
364  QPainter imagePainter( &tempImage );
365  context.setPainter( &imagePainter );
366  s->drawPreviewIcon( &imagePainter, tempImageSize, &context );
367  context.setPainter( ctx->painter );
368  //reduce opacity of image
369  imagePainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
370  imagePainter.fillRect( tempImage.rect(), QColor( 0, 0, 0, opacity ) );
371  imagePainter.end();
372  //draw rendered symbol image
373  p->drawImage( 0, 0, tempImage );
374  }
375  else
376  {
377  s->drawPreviewIcon( p, QSize( width * dotsPerMM, height * dotsPerMM ), &context );
378  }
379  p->restore();
380  }
381 
382  return QSizeF( qMax( width + 2 * widthOffset, ( double ) settings.symbolSize().width() ),
383  qMax( height + 2 * heightOffset, ( double ) settings.symbolSize().height() ) );
384 }
385 
386 
388 {
390  updateLabel();
391 }
392 
393 
395 {
396  if ( mSymbolUsesMapUnits )
397  {
398  mPixmap = QPixmap();
399  emit dataChanged();
400  }
401 }
402 
403 
404 void QgsSymbolV2LegendNode::updateLabel()
405 {
406  bool showFeatureCount = mLayerNode->customProperty( "showFeatureCount", 0 ).toBool();
407  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mLayerNode->layer() );
408 
409  if ( mEmbeddedInParent )
410  {
411  QString layerName = mLayerNode->layerName();
412  if ( !mLayerNode->customProperty( "legend/title-label" ).isNull() )
413  layerName = mLayerNode->customProperty( "legend/title-label" ).toString();
414 
415  mLabel = mUserLabel.isEmpty() ? layerName : mUserLabel;
416  if ( showFeatureCount && vl && vl->pendingFeatureCount() >= 0 )
417  mLabel += QString( " [%1]" ).arg( vl->pendingFeatureCount() );
418  }
419  else
420  {
421  mLabel = mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
422  if ( showFeatureCount && vl && mItem.legacyRuleKey() )
423  mLabel += QString( " [%1]" ).arg( vl->featureCount( mItem.legacyRuleKey() ) );
424  }
425 }
426 
427 
428 
429 // -------------------------------------------------------------------------
430 
431 
432 QgsSimpleLegendNode::QgsSimpleLegendNode( QgsLayerTreeLayer* nodeLayer, const QString& label, const QIcon& icon, QObject* parent, const QString& key )
433  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
434  , mLabel( label )
435  , mIcon( icon )
436  , mKey( key )
437 {
438 }
439 
441 {
442  if ( role == Qt::DisplayRole || role == Qt::EditRole )
443  return mUserLabel.isEmpty() ? mLabel : mUserLabel;
444  else if ( role == Qt::DecorationRole )
445  return mIcon;
446  else if ( role == RuleKeyRole && !mKey.isEmpty() )
447  return mKey;
448  else
449  return QVariant();
450 }
451 
452 
453 // -------------------------------------------------------------------------
454 
456  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
457  , mImage( img )
458 {
459 }
460 
462 {
463  if ( role == Qt::DecorationRole )
464  {
465  return QPixmap::fromImage( mImage );
466  }
467  else if ( role == Qt::SizeHintRole )
468  {
469  return mImage.size();
470  }
471  return QVariant();
472 }
473 
474 QSizeF QgsImageLegendNode::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
475 {
476  Q_UNUSED( itemHeight );
477 
478  if ( ctx )
479  {
480  ctx->painter->drawImage( QRectF( ctx->point.x(), ctx->point.y(), settings.wmsLegendSize().width(), settings.wmsLegendSize().height() ),
481  mImage, QRectF( 0, 0, mImage.width(), mImage.height() ) );
482  }
483  return settings.wmsLegendSize();
484 }
485 
486 // -------------------------------------------------------------------------
487 
489  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
490  , mColor( color )
491  , mLabel( label )
492 {
493 }
494 
496 {
497  if ( role == Qt::DecorationRole )
498  {
499  QSize iconSize( 16, 16 ); // TODO: configurable?
500  QPixmap pix( iconSize );
501  pix.fill( mColor );
502  return QIcon( pix );
503  }
504  else if ( role == Qt::DisplayRole || role == Qt::EditRole )
505  return mUserLabel.isEmpty() ? mLabel : mUserLabel;
506  else
507  return QVariant();
508 }
509 
510 
511 QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
512 {
513  if ( ctx )
514  {
515  QColor itemColor = mColor;
516  if ( QgsRasterLayer* rasterLayer = dynamic_cast<QgsRasterLayer*>( layerNode()->layer() ) )
517  {
518  if ( QgsRasterRenderer* rasterRenderer = rasterLayer->renderer() )
519  itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
520  }
521 
522  ctx->painter->setBrush( itemColor );
523  ctx->painter->drawRect( QRectF( ctx->point.x(), ctx->point.y() + ( itemHeight - settings.symbolSize().height() ) / 2,
524  settings.symbolSize().width(), settings.symbolSize().height() ) );
525  }
526  return settings.symbolSize();
527 }
528 
529 // -------------------------------------------------------------------------
530 
532  : QgsLayerTreeModelLegendNode( nodeLayer, parent )
533  , mValid( false )
534 {
535 }
536 
537 const QImage& QgsWMSLegendNode::getLegendGraphic() const
538 {
539  if ( ! mValid && ! mFetcher )
540  {
541  // or maybe in presence of a downloader we should just delete it
542  // and start a new one ?
543 
544  QgsRasterLayer* layer = qobject_cast<QgsRasterLayer*>( mLayerNode->layer() );
545  const QgsLayerTreeModel* mod = model();
546  if ( ! mod ) return mImage;
547  const QgsMapSettings* ms = mod->legendFilterByMap();
548 
549  QgsRasterDataProvider* prov = layer->dataProvider();
550 
551  Q_ASSERT( ! mFetcher );
552  mFetcher.reset( prov->getLegendGraphicFetcher( ms ) );
553  if ( mFetcher )
554  {
555  connect( mFetcher.data(), SIGNAL( finish( const QImage& ) ), this, SLOT( getLegendGraphicFinished( const QImage& ) ) );
556  connect( mFetcher.data(), SIGNAL( error( const QString& ) ), this, SLOT( getLegendGraphicErrored( const QString& ) ) );
557  connect( mFetcher.data(), SIGNAL( progress( qint64, qint64 ) ), this, SLOT( getLegendGraphicProgress( qint64, qint64 ) ) );
558  mFetcher->start();
559  } // else QgsDebugMsg("XXX No legend supported ?");
560 
561  }
562 
563  return mImage;
564 }
565 
567 {
568  //QgsDebugMsg( QString("XXX data called with role %1 -- mImage size is %2x%3").arg(role).arg(mImage.width()).arg(mImage.height()) );
569 
570  if ( role == Qt::DecorationRole )
571  {
572  return QPixmap::fromImage( getLegendGraphic() );
573  }
574  else if ( role == Qt::SizeHintRole )
575  {
576  return getLegendGraphic().size();
577  }
578  return QVariant();
579 }
580 
581 QSizeF QgsWMSLegendNode::drawSymbol( const QgsLegendSettings& settings, ItemContext* ctx, double itemHeight ) const
582 {
583  Q_UNUSED( itemHeight );
584 
585  if ( ctx )
586  {
587  ctx->painter->drawImage( QRectF( ctx->point, settings.wmsLegendSize() ),
588  mImage,
589  QRectF( QPointF( 0, 0 ), mImage.size() ) );
590  }
591  return settings.wmsLegendSize();
592 }
593 
594 /* private */
595 QImage QgsWMSLegendNode::renderMessage( const QString& msg ) const
596 {
597  const int fontHeight = 10;
598  const int margin = fontHeight / 2;
599  const int nlines = 1;
600 
601  const int w = 512, h = fontHeight * nlines + margin * ( nlines + 1 );
602  QImage theImage( w, h, QImage::Format_ARGB32_Premultiplied );
603  QPainter painter;
604  painter.begin( &theImage );
605  painter.setPen( QColor( 255, 0, 0 ) );
606  painter.setFont( QFont( "Chicago", fontHeight ) );
607  painter.fillRect( 0, 0, w, h, QColor( 255, 255, 255 ) );
608  painter.drawText( 0, margin + fontHeight, msg );
609  //painter.drawText(0,2*(margin+fontHeight),QString("retrying in 5 seconds..."));
610  painter.end();
611 
612  return theImage;
613 }
614 
615 void QgsWMSLegendNode::getLegendGraphicProgress( qint64 cur, qint64 tot )
616 {
617  QString msg = QString( "Downloading... %1/%2" ).arg( cur ).arg( tot );
618  //QgsDebugMsg ( QString("XXX %1").arg(msg) );
619  mImage = renderMessage( msg );
620  emit dataChanged();
621 }
622 
623 void QgsWMSLegendNode::getLegendGraphicErrored( const QString& msg )
624 {
625  if ( ! mFetcher ) return; // must be coming after finish
626 
627  mImage = renderMessage( msg );
628  //QgsDebugMsg( QString("XXX emitting dataChanged after writing an image of %1x%2").arg(mImage.width()).arg(mImage.height()) );
629 
630  emit dataChanged();
631 
632  mFetcher.reset();
633 
634  mValid = true; // we consider it valid anyway
635  // ... but remove validity after 5 seconds
636  //QTimer::singleShot(5000, this, SLOT(invalidateMapBasedData()));
637 }
638 
639 void QgsWMSLegendNode::getLegendGraphicFinished( const QImage& theImage )
640 {
641  if ( ! mFetcher ) return; // must be coming after error
642 
643  //QgsDebugMsg( QString("XXX legend graphic finished, image is %1x%2").arg(theImage.width()).arg(theImage.height()) );
644  if ( ! theImage.isNull() )
645  {
646  if ( theImage != mImage )
647  {
648  mImage = theImage;
649  //QgsDebugMsg( QString("XXX emitting dataChanged") );
650  emit dataChanged();
651  }
652  mValid = true; // only if not null I guess
653  }
654  mFetcher.reset();
655 }
656 
658 {
659  //QgsDebugMsg( QString("XXX invalidateMapBasedData called") );
660  // TODO: do this only if this extent != prev extent ?
661  mValid = false;
662  emit dataChanged();
663 }
void setForceVectorOutput(bool force)
QStringList splitStringForWrapping(QString stringToSplt) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
QSize size() const
QgsSymbolV2LegendNode(QgsLayerTreeLayer *nodeLayer, const QgsLegendSymbolItemV2 &item, QObject *parent=0)
virtual void checkLegendSymbolItem(QString key, bool state=true)
item in symbology was checked
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:86
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 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...
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.
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
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
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)
int count(const T &value) const
qreal x() const
qreal y() const
void setScaleFactor(double factor)
bool isNull() const
void fill(uint pixelValue)
virtual bool legendSymbolItemChecked(QString key)
items of symbology items in legend is checked
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.
int pendingFeatureCount()
returns feature count after commit
void setBrush(const QBrush &brush)
void setPainter(QPainter *p)
void drawText(const QPointF &position, const QString &text)
typedef Iterator
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 long featureCount() const
Number of features in the layer.
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)
QgsRasterSymbolLegendNode(QgsLayerTreeLayer *nodeLayer, const QColor &color, const QString &label, QObject *parent=0)
static double lineWidthScaleFactor(const QgsRenderContext &c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale &scale=QgsMapUnitScale())
Returns the line width scale factor depending on the unit and the paint device.
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...