QGIS API Documentation  2.12.0-Lyon
qgscomposerlegend.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerlegend.cpp - description
3  ---------------------
4  begin : June 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include <limits>
18 
19 #include "qgscomposerlegendstyle.h"
20 #include "qgscomposerlegend.h"
21 #include "qgscomposerlegenditem.h"
22 #include "qgscomposermap.h"
23 #include "qgscomposition.h"
24 #include "qgscomposermodel.h"
25 #include "qgsmaplayerregistry.h"
26 #include "qgslayertree.h"
27 #include "qgslayertreemodel.h"
28 #include "qgslegendrenderer.h"
29 #include "qgslogger.h"
30 #include "qgsproject.h"
31 #include "qgssymbollayerv2utils.h"
32 #include <QDomDocument>
33 #include <QDomElement>
34 #include <QPainter>
35 
37  : QgsComposerItem( composition )
38  , mCustomLayerTree( 0 )
39  , mComposerMap( 0 )
40  , mLegendFilterByMap( false )
41 {
42  mLegendModel2 = new QgsLegendModelV2( QgsProject::instance()->layerTreeRoot() );
43 
44  adjustBoxSize();
45 
46  connect( &mLegendModel, SIGNAL( layersChanged() ), this, SLOT( synchronizeWithModel() ) );
47 }
48 
49 QgsComposerLegend::QgsComposerLegend()
50  : QgsComposerItem( 0 )
51  , mLegendModel2( 0 )
52  , mCustomLayerTree( 0 )
53  , mComposerMap( 0 )
54  , mLegendFilterByMap( false )
55 {
56 
57 }
58 
60 {
61  delete mLegendModel2;
62  delete mCustomLayerTree;
63 }
64 
65 void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
66 {
67  Q_UNUSED( itemStyle );
68  Q_UNUSED( pWidget );
69 
70  if ( !painter )
71  return;
72 
73  if ( !shouldDrawItem() )
74  {
75  return;
76  }
77 
78  int dpi = painter->device()->logicalDpiX();
79  double dotsPerMM = dpi / 25.4;
80 
81  if ( mComposition )
82  {
84  mSettings.setDpi( dpi );
85  }
86  if ( mComposerMap )
87  {
88  mSettings.setMmPerMapUnit( mComposerMap->mapUnitsToMM() );
89 
90  // use a temporary QgsMapSettings to find out real map scale
91  QgsMapSettings ms = mComposerMap->composition()->mapSettings();
92  ms.setOutputSize( QSizeF( mComposerMap->rect().width() * dotsPerMM, mComposerMap->rect().height() * dotsPerMM ).toSize() );
93  ms.setExtent( *mComposerMap->currentMapExtent() );
94  ms.setOutputDpi( dpi );
95  mSettings.setMapScale( ms.scale() );
96  }
97 
98  drawBackground( painter );
99  painter->save();
100  //antialiasing on
101  painter->setRenderHint( QPainter::Antialiasing, true );
102  painter->setPen( QPen( QColor( 0, 0, 0 ) ) );
103 
104  QgsLegendRenderer legendRenderer( mLegendModel2, mSettings );
105  legendRenderer.setLegendSize( rect().size() );
106 
107  //adjust box if width or height is too small
108  QSizeF size = legendRenderer.minimumSize();
109  if ( size.height() > rect().height() || size.width() > rect().width() )
110  {
111  //need to resize box
112  QRectF targetRect = QRectF( pos().x(), pos().y(), rect().width(), rect().height() );
113  if ( size.height() > targetRect.height() )
114  targetRect.setHeight( size.height() );
115  if ( size.width() > rect().width() )
116  targetRect.setWidth( size.width() );
117 
118  //set new rect, respecting position mode and data defined size/position
119  setSceneRect( evalItemRect( targetRect, true ) );
120  }
121 
122  legendRenderer.drawLegend( painter );
123 
124  painter->restore();
125 
126  //draw frame and selection boxes if necessary
127  drawFrame( painter );
128  if ( isSelected() )
129  {
130  drawSelectionBoxes( painter );
131  }
132 }
133 
135 {
136  QgsLegendRenderer legendRenderer( mLegendModel2, mSettings );
137  QSizeF size = legendRenderer.minimumSize();
138  if ( painter )
139  legendRenderer.drawLegend( painter );
140  return size;
141 }
142 
143 
145 {
146  QgsLegendRenderer legendRenderer( mLegendModel2, mSettings );
147  QSizeF size = legendRenderer.minimumSize();
148  QgsDebugMsg( QString( "width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
149  if ( size.isValid() )
150  {
151  QRectF targetRect = QRectF( pos().x(), pos().y(), size.width(), size.height() );
152  //set new rect, respecting position mode and data defined size/position
153  setSceneRect( evalItemRect( targetRect, true ) );
154  }
155 }
156 
157 
158 void QgsComposerLegend::setCustomLayerTree( QgsLayerTreeGroup* rootGroup )
159 {
160  mLegendModel2->setRootGroup( rootGroup ? rootGroup : QgsProject::instance()->layerTreeRoot() );
161 
162  delete mCustomLayerTree;
163  mCustomLayerTree = rootGroup;
164 }
165 
166 
168 {
169  if ( autoUpdate == autoUpdateModel() )
170  return;
171 
172  setCustomLayerTree( autoUpdate ? 0 : QgsLayerTree::toGroup( QgsProject::instance()->layerTreeRoot()->clone() ) );
173  adjustBoxSize();
174 }
175 
177 {
178  return !mCustomLayerTree;
179 }
180 
182 {
183  mLegendFilterByMap = enabled;
184  updateFilterByMap();
185 }
186 
188 {
189  mSettings.setTitle( t );
190 
191  if ( mComposition && id().isEmpty() )
192  {
193  //notify the model that the display name has changed
195  }
196 }
197 QString QgsComposerLegend::title() const { return mSettings.title(); }
198 
199 Qt::AlignmentFlag QgsComposerLegend::titleAlignment() const { return mSettings.titleAlignment(); }
200 void QgsComposerLegend::setTitleAlignment( Qt::AlignmentFlag alignment ) { mSettings.setTitleAlignment( alignment ); }
201 
205 
208 
211 
212 double QgsComposerLegend::boxSpace() const { return mSettings.boxSpace(); }
213 void QgsComposerLegend::setBoxSpace( double s ) { mSettings.setBoxSpace( s ); }
214 
215 double QgsComposerLegend::columnSpace() const { return mSettings.columnSpace(); }
216 void QgsComposerLegend::setColumnSpace( double s ) { mSettings.setColumnSpace( s ); }
217 
218 QColor QgsComposerLegend::fontColor() const { return mSettings.fontColor(); }
219 void QgsComposerLegend::setFontColor( const QColor& c ) { mSettings.setFontColor( c ); }
220 
221 double QgsComposerLegend::symbolWidth() const { return mSettings.symbolSize().width(); }
222 void QgsComposerLegend::setSymbolWidth( double w ) { mSettings.setSymbolSize( QSizeF( w, mSettings.symbolSize().height() ) ); }
223 
224 double QgsComposerLegend::symbolHeight() const { return mSettings.symbolSize().height(); }
225 void QgsComposerLegend::setSymbolHeight( double h ) { mSettings.setSymbolSize( QSizeF( mSettings.symbolSize().width(), h ) ); }
226 
227 double QgsComposerLegend::wmsLegendWidth() const { return mSettings.wmsLegendSize().width(); }
228 void QgsComposerLegend::setWmsLegendWidth( double w ) { mSettings.setWmsLegendSize( QSizeF( w, mSettings.wmsLegendSize().height() ) ); }
229 
230 double QgsComposerLegend::wmsLegendHeight() const {return mSettings.wmsLegendSize().height(); }
231 void QgsComposerLegend::setWmsLegendHeight( double h ) { mSettings.setWmsLegendSize( QSizeF( mSettings.wmsLegendSize().width(), h ) ); }
232 
233 void QgsComposerLegend::setWrapChar( const QString& t ) { mSettings.setWrapChar( t ); }
234 QString QgsComposerLegend::wrapChar() const {return mSettings.wrapChar(); }
235 
236 int QgsComposerLegend::columnCount() const { return mSettings.columnCount(); }
237 void QgsComposerLegend::setColumnCount( int c ) { mSettings.setColumnCount( c ); }
238 
239 bool QgsComposerLegend::splitLayer() const { return mSettings.splitLayer(); }
240 void QgsComposerLegend::setSplitLayer( bool s ) { mSettings.setSplitLayer( s ); }
241 
242 bool QgsComposerLegend::equalColumnWidth() const { return mSettings.equalColumnWidth(); }
244 
245 bool QgsComposerLegend::drawRasterBorder() const { return mSettings.drawRasterBorder(); }
246 void QgsComposerLegend::setDrawRasterBorder( bool enabled ) { mSettings.setDrawRasterBorder( enabled ); }
247 
249 void QgsComposerLegend::setRasterBorderColor( const QColor& color ) { mSettings.setRasterBorderColor( color ); }
250 
251 double QgsComposerLegend::rasterBorderWidth() const { return mSettings.rasterBorderWidth(); }
252 void QgsComposerLegend::setRasterBorderWidth( double width ) { mSettings.setRasterBorderWidth( width ); }
253 
255 {
256  QgsDebugMsg( "Entered" );
257  adjustBoxSize();
258  update();
259 }
260 
262 {
263  // take layer list from map renderer (to have legend order)
265  adjustBoxSize();
266  update();
267 }
268 
270 {
271  if ( elem.isNull() )
272  {
273  return false;
274  }
275 
276  QDomElement composerLegendElem = doc.createElement( "ComposerLegend" );
277  elem.appendChild( composerLegendElem );
278 
279  //write general properties
280  composerLegendElem.setAttribute( "title", mSettings.title() );
281  composerLegendElem.setAttribute( "titleAlignment", QString::number(( int ) mSettings.titleAlignment() ) );
282  composerLegendElem.setAttribute( "columnCount", QString::number( mSettings.columnCount() ) );
283  composerLegendElem.setAttribute( "splitLayer", QString::number( mSettings.splitLayer() ) );
284  composerLegendElem.setAttribute( "equalColumnWidth", QString::number( mSettings.equalColumnWidth() ) );
285 
286  composerLegendElem.setAttribute( "boxSpace", QString::number( mSettings.boxSpace() ) );
287  composerLegendElem.setAttribute( "columnSpace", QString::number( mSettings.columnSpace() ) );
288 
289  composerLegendElem.setAttribute( "symbolWidth", QString::number( mSettings.symbolSize().width() ) );
290  composerLegendElem.setAttribute( "symbolHeight", QString::number( mSettings.symbolSize().height() ) );
291 
292  composerLegendElem.setAttribute( "rasterBorder", mSettings.drawRasterBorder() );
293  composerLegendElem.setAttribute( "rasterBorderColor", QgsSymbolLayerV2Utils::encodeColor( mSettings.rasterBorderColor() ) );
294  composerLegendElem.setAttribute( "rasterBorderWidth", QString::number( mSettings.rasterBorderWidth() ) );
295 
296  composerLegendElem.setAttribute( "wmsLegendWidth", QString::number( mSettings.wmsLegendSize().width() ) );
297  composerLegendElem.setAttribute( "wmsLegendHeight", QString::number( mSettings.wmsLegendSize().height() ) );
298  composerLegendElem.setAttribute( "wrapChar", mSettings.wrapChar() );
299  composerLegendElem.setAttribute( "fontColor", mSettings.fontColor().name() );
300 
301  if ( mComposerMap )
302  {
303  composerLegendElem.setAttribute( "map", mComposerMap->id() );
304  }
305 
306  QDomElement composerLegendStyles = doc.createElement( "styles" );
307  composerLegendElem.appendChild( composerLegendStyles );
308 
309  style( QgsComposerLegendStyle::Title ).writeXML( "title", composerLegendStyles, doc );
310  style( QgsComposerLegendStyle::Group ).writeXML( "group", composerLegendStyles, doc );
311  style( QgsComposerLegendStyle::Subgroup ).writeXML( "subgroup", composerLegendStyles, doc );
312  style( QgsComposerLegendStyle::Symbol ).writeXML( "symbol", composerLegendStyles, doc );
313  style( QgsComposerLegendStyle::SymbolLabel ).writeXML( "symbolLabel", composerLegendStyles, doc );
314 
315  if ( mCustomLayerTree )
316  {
317  // if not using auto-update - store the custom layer tree
318  mCustomLayerTree->writeXML( composerLegendElem );
319  }
320 
321  if ( mLegendFilterByMap )
322  composerLegendElem.setAttribute( "legendFilterByMap", "1" );
323 
324  return _writeXML( composerLegendElem, doc );
325 }
326 
327 static void _readOldLegendGroup( QDomElement& elem, QgsLayerTreeGroup* parentGroup )
328 {
329  QDomElement itemElem = elem.firstChildElement();
330 
331  while ( !itemElem.isNull() )
332  {
333 
334  if ( itemElem.tagName() == "LayerItem" )
335  {
336  QString layerId = itemElem.attribute( "layerId" );
337  if ( QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId ) )
338  {
339  QgsLayerTreeLayer* nodeLayer = parentGroup->addLayer( layer );
340  QString userText = itemElem.attribute( "userText" );
341  if ( !userText.isEmpty() )
342  nodeLayer->setCustomProperty( "legend/title-label", userText );
343  QString style = itemElem.attribute( "style" );
344  if ( !style.isEmpty() )
345  nodeLayer->setCustomProperty( "legend/title-style", style );
346  QString showFeatureCount = itemElem.attribute( "showFeatureCount" );
347  if ( showFeatureCount.toInt() )
348  nodeLayer->setCustomProperty( "showFeatureCount", 1 );
349 
350  // support for individual legend items (user text, order) not implemented yet
351  }
352  }
353  else if ( itemElem.tagName() == "GroupItem" )
354  {
355  QgsLayerTreeGroup* nodeGroup = parentGroup->addGroup( itemElem.attribute( "userText" ) );
356  QString style = itemElem.attribute( "style" );
357  if ( !style.isEmpty() )
358  nodeGroup->setCustomProperty( "legend/title-style", style );
359 
360  _readOldLegendGroup( itemElem, nodeGroup );
361  }
362 
363  itemElem = itemElem.nextSiblingElement();
364  }
365 }
366 
367 bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument& doc )
368 {
369  if ( itemElem.isNull() )
370  {
371  return false;
372  }
373 
374  //read general properties
375  mSettings.setTitle( itemElem.attribute( "title" ) );
376  if ( !itemElem.attribute( "titleAlignment" ).isEmpty() )
377  {
378  mSettings.setTitleAlignment(( Qt::AlignmentFlag )itemElem.attribute( "titleAlignment" ).toInt() );
379  }
380  int colCount = itemElem.attribute( "columnCount", "1" ).toInt();
381  if ( colCount < 1 ) colCount = 1;
382  mSettings.setColumnCount( colCount );
383  mSettings.setSplitLayer( itemElem.attribute( "splitLayer", "0" ).toInt() == 1 );
384  mSettings.setEqualColumnWidth( itemElem.attribute( "equalColumnWidth", "0" ).toInt() == 1 );
385 
386  QDomNodeList stylesNodeList = itemElem.elementsByTagName( "styles" );
387  if ( stylesNodeList.size() > 0 )
388  {
389  QDomNode stylesNode = stylesNodeList.at( 0 );
390  for ( int i = 0; i < stylesNode.childNodes().size(); i++ )
391  {
392  QDomElement styleElem = stylesNode.childNodes().at( i ).toElement();
394  style.readXML( styleElem, doc );
395  QString name = styleElem.attribute( "name" );
397  if ( name == "title" ) s = QgsComposerLegendStyle::Title;
398  else if ( name == "group" ) s = QgsComposerLegendStyle::Group;
399  else if ( name == "subgroup" ) s = QgsComposerLegendStyle::Subgroup;
400  else if ( name == "symbol" ) s = QgsComposerLegendStyle::Symbol;
401  else if ( name == "symbolLabel" ) s = QgsComposerLegendStyle::SymbolLabel;
402  else continue;
403  setStyle( s, style );
404  }
405  }
406 
407  //font color
408  QColor fontClr;
409  fontClr.setNamedColor( itemElem.attribute( "fontColor", "#000000" ) );
410  mSettings.setFontColor( fontClr );
411 
412  //spaces
413  mSettings.setBoxSpace( itemElem.attribute( "boxSpace", "2.0" ).toDouble() );
414  mSettings.setColumnSpace( itemElem.attribute( "columnSpace", "2.0" ).toDouble() );
415 
416  mSettings.setSymbolSize( QSizeF( itemElem.attribute( "symbolWidth", "7.0" ).toDouble(), itemElem.attribute( "symbolHeight", "14.0" ).toDouble() ) );
417  mSettings.setWmsLegendSize( QSizeF( itemElem.attribute( "wmsLegendWidth", "50" ).toDouble(), itemElem.attribute( "wmsLegendHeight", "25" ).toDouble() ) );
418 
419  mSettings.setDrawRasterBorder( itemElem.attribute( "rasterBorder", "1" ) != "0" );
420  mSettings.setRasterBorderColor( QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "rasterBorderColor", "0,0,0" ) ) );
421  mSettings.setRasterBorderWidth( itemElem.attribute( "rasterBorderWidth", "0" ).toDouble() );
422 
423  mSettings.setWrapChar( itemElem.attribute( "wrapChar" ) );
424 
425  //composer map
426  mLegendFilterByMap = itemElem.attribute( "legendFilterByMap", "0" ).toInt();
427  if ( !itemElem.attribute( "map" ).isEmpty() )
428  {
429  setComposerMap( mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() ) );
430  }
431 
432  QDomElement oldLegendModelElem = itemElem.firstChildElement( "Model" );
433  if ( !oldLegendModelElem.isNull() )
434  {
435  // QGIS <= 2.4
436  QgsLayerTreeGroup* nodeRoot = new QgsLayerTreeGroup();
437  _readOldLegendGroup( oldLegendModelElem, nodeRoot );
438  setCustomLayerTree( nodeRoot );
439  }
440  else
441  {
442  // QGIS >= 2.6
443  QDomElement layerTreeElem = itemElem.firstChildElement( "layer-tree-group" );
444  setCustomLayerTree( QgsLayerTreeGroup::readXML( layerTreeElem ) );
445  }
446 
447  //restore general composer item properties
448  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
449  if ( composerItemList.size() > 0 )
450  {
451  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
452  _readXML( composerItemElem, doc );
453  }
454 
455  // < 2.0 projects backward compatibility >>>>>
456  //title font
457  QString titleFontString = itemElem.attribute( "titleFont" );
458  if ( !titleFontString.isEmpty() )
459  {
460  rstyle( QgsComposerLegendStyle::Title ).rfont().fromString( titleFontString );
461  }
462  //group font
463  QString groupFontString = itemElem.attribute( "groupFont" );
464  if ( !groupFontString.isEmpty() )
465  {
466  rstyle( QgsComposerLegendStyle::Group ).rfont().fromString( groupFontString );
467  }
468 
469  //layer font
470  QString layerFontString = itemElem.attribute( "layerFont" );
471  if ( !layerFontString.isEmpty() )
472  {
474  }
475  //item font
476  QString itemFontString = itemElem.attribute( "itemFont" );
477  if ( !itemFontString.isEmpty() )
478  {
480  }
481 
482  if ( !itemElem.attribute( "groupSpace" ).isEmpty() )
483  {
485  }
486  if ( !itemElem.attribute( "layerSpace" ).isEmpty() )
487  {
489  }
490  if ( !itemElem.attribute( "symbolSpace" ).isEmpty() )
491  {
494  }
495  // <<<<<<< < 2.0 projects backward compatibility
496 
497  emit itemChanged();
498  return true;
499 }
500 
502 {
503  if ( !id().isEmpty() )
504  {
505  return id();
506  }
507 
508  //if no id, default to portion of title text
509  QString text = mSettings.title();
510  if ( text.isEmpty() )
511  {
512  return tr( "<legend>" );
513  }
514  if ( text.length() > 25 )
515  {
516  return QString( tr( "%1..." ) ).arg( text.left( 25 ) );
517  }
518  else
519  {
520  return text;
521  }
522 }
523 
525 {
526  if ( mComposerMap )
527  {
528  disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
529  disconnect( mComposerMap, SIGNAL( itemChanged() ), this, SLOT( updateFilterByMap() ) );
530  disconnect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( updateFilterByMap() ) );
531  disconnect( mComposerMap, SIGNAL( layerStyleOverridesChanged() ), this, SLOT( mapLayerStyleOverridesChanged() ) );
532  }
533 
534  mComposerMap = map;
535 
536  if ( map )
537  {
538  QObject::connect( map, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
539  QObject::connect( map, SIGNAL( itemChanged() ), this, SLOT( updateFilterByMap() ) );
540  QObject::connect( map, SIGNAL( extentChanged() ), this, SLOT( updateFilterByMap() ) );
541  QObject::connect( map, SIGNAL( layerStyleOverridesChanged() ), this, SLOT( mapLayerStyleOverridesChanged() ) );
542  }
543 
544  updateFilterByMap();
545 }
546 
548 {
549  setComposerMap( 0 );
550 }
551 
552 void QgsComposerLegend::mapLayerStyleOverridesChanged()
553 {
554  if ( !mComposerMap )
555  return;
556 
557  // map's style has been changed, so make sure to update the legend here
558  if ( mLegendFilterByMap )
559  {
560  // legend is being filtered by map, so we need to re run the hit test too
561  // as the style overrides may also have affected the visible symbols
562  updateFilterByMap();
563  }
564  else
565  {
566  mLegendModel2->setLayerStyleOverrides( mComposerMap->layerStyleOverrides() );
567 
568  Q_FOREACH ( QgsLayerTreeLayer* nodeLayer, mLegendModel2->rootGroup()->findLayers() )
569  mLegendModel2->refreshLayerLegend( nodeLayer );
570 
571  adjustBoxSize();
572  update();
573  }
574 }
575 
576 void QgsComposerLegend::updateFilterByMap()
577 {
578  if ( isRemoved() )
579  return;
580 
581  if ( mComposerMap )
582  mLegendModel2->setLayerStyleOverrides( mComposerMap->layerStyleOverrides() );
583  else
585 
586 
587  if ( mComposerMap && mLegendFilterByMap )
588  {
589  int dpi = mComposition->printResolution();
590 
591  QgsRectangle requestRectangle;
592  mComposerMap->requestedExtent( requestRectangle );
593 
594  QSizeF theSize( requestRectangle.width(), requestRectangle.height() );
595  theSize *= mComposerMap->mapUnitsToMM() * dpi / 25.4;
596 
597  QgsMapSettings ms = mComposerMap->mapSettings( requestRectangle, theSize, dpi );
598 
599  mLegendModel2->setLegendFilterByMap( &ms );
600  }
601  else
602  mLegendModel2->setLegendFilterByMap( 0 );
603 
604  adjustBoxSize();
605  update();
606 }
607 
608 // -------------------------------------------------------------------------
610 #include "qgsvectorlayer.h"
611 
613  : QgsLayerTreeModel( rootNode, parent )
614 {
617 }
618 
620 {
621  // handle custom layer node labels
622  if ( QgsLayerTreeNode* node = index2node( index ) )
623  {
624  if ( QgsLayerTree::isLayer( node ) && ( role == Qt::DisplayRole || role == Qt::EditRole ) && !node->customProperty( "legend/title-label" ).isNull() )
625  {
626  QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node );
627  QString name = node->customProperty( "legend/title-label" ).toString();
628  if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toInt() && role == Qt::DisplayRole )
629  {
630  QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( nodeLayer->layer() );
631  if ( vlayer && vlayer->featureCount() >= 0 )
632  name += QString( " [%1]" ).arg( vlayer->featureCount() );
633  }
634  return name;
635  }
636  }
637 
638  return QgsLayerTreeModel::data( index, role );
639 }
640 
642 {
643  // make the legend nodes selectable even if they are not by default
644  if ( index2legendNode( index ) )
645  return QgsLayerTreeModel::flags( index ) | Qt::ItemIsSelectable;
646 
647  return QgsLayerTreeModel::flags( index );
648 }
double boxSpace() const
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
QString wrapChar() const
void setWrapChar(const QString &t)
Layer tree group node serves as a container for layers and further groups.
QDomNodeList elementsByTagName(const QString &tagname) const
void setLegendSize(QSizeF s)
Set the preferred resulting legend size.
QgsComposerLegend(QgsComposition *composition)
static unsigned index
qreal x() const
qreal y() const
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
double boxSpace() const
void setEqualColumnWidth(bool s)
QgsLayerTreeGroup * addGroup(const QString &name)
Append a new group node with given name. Newly created node is owned by this group.
void setBoxSpace(double s)
void setMmPerMapUnit(double mmPerMapUnit)
double scale() const
Return the calculated scale of the map.
bool splitLayer() const
void readXML(const QDomElement &elem, const QDomDocument &doc)
void setRenderHint(RenderHint hint, bool on)
void setDrawRasterBorder(bool enabled)
Sets whether a border will be drawn around raster symbol items.
QgsLayerTreeGroup * rootGroup() const
Return pointer to the root node of the layer tree. Always a non-null pointer.
QDomNode appendChild(const QDomNode &newChild)
double mapUnitsToMM() const
Returns the conversion factor map units -> mm.
void writeXML(const QString &name, QDomElement &elem, QDomDocument &doc) const
QgsComposerLegendStyle & rstyle(QgsComposerLegendStyle::Style s)
Returns reference to modifiable style.
QString name() const
void setMargin(Side side, double margin)
Item model implementation based on layer tree model for composer legend.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QString attribute(const QString &name, const QString &defValue) const
int columnCount() const
static QgsLayerTreeGroup * readXML(QDomElement &element)
Read group (tree) from XML element and return the newly created group (or null on ...
QgsMapLayer * layer() const
void setSplitLayer(bool s)
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint.
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void setOutputDpi(int dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
void itemChanged()
Emitted when the item changes.
void requestedExtent(QgsRectangle &extent) const
Calculates the extent to request and the yShift of the top-left point in case of rotation.
static QString encodeColor(const QColor &color)
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
void setSymbolSize(QSizeF s)
void drawLegend(QPainter *painter)
Draw the legend with given painter.
void setColumnSpace(double s)
A item that forms part of a map composition.
bool isValid() const
void save()
QDomElement nextSiblingElement(const QString &tagName) const
double columnSpace() const
double columnSpace() const
Flags flags() const
Return OR-ed combination of model flags.
QMap< QString, QString > layerStyleOverrides() const
Getter for stored overrides of styles for layers.
QVariant data(const QModelIndex &index, int role) const override
double wmsLegendWidth() const
virtual void drawFrame(QPainter *p)
Draw black frame around item.
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
void setTitle(const QString &t)
QColor rasterBorderColor() const
Returns the border color for the border drawn around raster symbol items.
void setNamedColor(const QString &name)
double toDouble(bool *ok) const
void updateLegend()
Updates the model and all legend entries.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QDomNodeList childNodes() const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool equalColumnWidth() const
void update(const QRectF &rect)
void updateItemDisplayName(QgsComposerItem *item)
Must be called when an item's display name is modified.
QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group. No type checking is done - use isGroup() to find out whether this operation is ...
Definition: qgslayertree.h:46
void setWmsLegendHeight(double h)
Allow check boxes for legend nodes (if supported by layer's legend)
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle &style)
QgsComposerLegendStyle & rstyle(QgsComposerLegendStyle::Style s)
Returns reference to modifiable style.
QgsLayerTreeLayer * addLayer(QgsMapLayer *layer)
Append a new layer node for given map layer. Newly created node is owned by this group.
void setHeight(qreal height)
void adjustBoxSize()
Sets item box to the whole content.
The QgsMapSettings class contains configuration for rendering of the map.
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
QgsLegendModelV2(QgsLayerTreeGroup *rootNode, QObject *parent=0)
QColor fontColor() const
QDomElement toElement() const
long featureCount(QgsSymbolV2 *symbol)
Number of features rendered with specified symbol.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
double rasterBorderWidth() const
Returns the border width (in millimeters) for the border drawn around raster symbol items...
const char * name() const
QPointF pos() const
QString number(int n, int base)
void setRasterBorderWidth(double width)
Sets the border width for the border drawn around raster symbol items.
bool fromString(const QString &descrip)
int toInt(bool *ok) const
void setRasterBorderWidth(double width)
Sets the border width for the border drawn around raster symbol items.
QColor rasterBorderColor() const
Returns the border color for the border drawn around raster symbol items.
bool equalColumnWidth() const
void setSymbolHeight(double h)
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
int printResolution() const
void setPen(const QColor &color)
void setAttribute(const QString &name, const QString &value)
bool isSelected() const
void setLegendFilterByMap(const QgsMapSettings *settings)
Force only display of legend nodes which are valid for given map settings.
QColor fontColor() const
const QgsComposition * composition() const
Returns the composition the item is attached to.
int toInt(bool *ok, int base) const
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Return legend node for given index.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes. Searches recursively the whole sub-tree.
void setMapScale(double scale)
bool isEmpty() const
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false, const QgsExpressionContext *context=0)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
void setComposerMap(const QgsComposerMap *map)
QPaintDevice * device() const
void synchronizeWithModel()
Data changed.
bool drawRasterBorder() const
Returns whether a border will be drawn around raster symbol items.
void setStyleFont(QgsComposerLegendStyle::Style s, const QFont &f)
Set style font.
QString wrapChar() const
void setTitle(const QString &t)
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
This class is a base class for nodes in a layer tree.
QString title() const
bool writeXML(QDomElement &elem, QDomDocument &doc) const override
Stores state in Dom node.
void setDrawRasterBorder(bool enabled)
Sets whether a border will be drawn around raster symbol items.
void setDpi(int dpi)
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
Sets state from Dom document.
void setFontColor(const QColor &c)
bool isLayer(QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:40
void setColumnSpace(double s)
bool autoUpdateModel() const
void setFlag(Flag f, bool on=true)
Enable or disable a model flag.
virtual void writeXML(QDomElement &parentElement) override
Write group (tree) as XML element and add it to the given parent element...
Graphics scene for map printing.
QSizeF minimumSize()
Run the layout algorithm and determine the size required for legend.
QFont styleFont(QgsComposerLegendStyle::Style s) const
void setLegendFilterByMapEnabled(bool enabled)
Set whether legend items should be filtered to show just the ones visible in the associated map...
Object representing map window.
void setSymbolWidth(double w)
QSizeF paintAndDetermineSize(QPainter *painter)
Paints the legend and calculates its size.
int logicalDpiX() const
void setRasterBorderColor(const QColor &color)
Sets the border color for the border drawn around raster symbol items.
void setRootGroup(QgsLayerTreeGroup *newRootGroup)
Reset the model and use a new root group node.
void invalidateCurrentMap()
Sets mCompositionMap to 0 if the map is deleted.
virtual QString displayName() const override
Get item display name.
QgsRectangle * currentMapExtent()
Returns a pointer to the current map extent, which is either the original user specified extent or th...
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
double symbolWidth() const
virtual bool isRemoved() const
Returns whether this item has been removed from the composition.
void setWmsLegendWidth(double w)
bool isNull() const
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
void restore()
bool useAdvancedEffects() const
Returns true if a composition should use advanced effects such as blend modes.
int id() const
Get identification number.
QgsComposition * mComposition
Composer legend components style.
void setWmsLegendSize(QSizeF s)
void setUseAdvancedEffects(bool use)
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
bool drawRasterBorder() const
Returns whether a border will be drawn around raster symbol items.
qreal width() const
bool splitLayer() const
void setAutoUpdateModel(bool autoUpdate)
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
void setStyleMargin(QgsComposerLegendStyle::Style s, double margin)
Set style margin.
double rasterBorderWidth() const
Returns the border width (in millimeters) for the border drawn around raster symbol items...
QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer. No type checking is done - use isLayer() to find out whether this operation is ...
Definition: qgslayertree.h:52
void setWrapChar(const QString &t)
void setOutputSize(const QSize &size)
Set the size of the resulting map image.
void setWidth(qreal width)
virtual void drawBackground(QPainter *p)
Draw background.
void setColumnCount(int c)
double wmsLegendHeight() const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:353
QDomElement firstChildElement(const QString &tagName) const
void setExtent(const QgsRectangle &rect)
Set coordinates of the rectangle which should be rendered.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
void setEqualColumnWidth(bool s)
QString title() const
int length() const
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
QString left(int n) const
QStringList layers() const
Get list of layer IDs for map rendering The layers are stored in the reverse order of how they are re...
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle &style)
qreal height() const
static QColor decodeColor(const QString &str)
void setLayerSet(const QStringList &layerIds, double scaleDenominator=-1, const QString &rule="")
QString tagName() const
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Set map of map layer style overrides (key: layer ID, value: style name) where a different style shoul...
int size() const
QDomElement createElement(const QString &tagName)
QSizeF symbolSize() const
QSizeF wmsLegendSize() const
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Return layer tree node for given index.
void setFontColor(const QColor &c)
qreal height() const
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:206
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void setBoxSpace(double s)
Allow reordering with drag'n'drop.
double symbolHeight() const
void destroyed(QObject *obj)
qreal width() const
static void _readOldLegendGroup(QDomElement &elem, QgsLayerTreeGroup *parentGroup)
void setSplitLayer(bool s)
void setRasterBorderColor(const QColor &color)
Sets the border color for the border drawn around raster symbol items.
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:211
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the node.
Layer tree node points to a map layer.
The QgsLegendRenderer class handles automatic layout and rendering of legend.
QDomNode at(int index) const
QRectF rect() const
QgsMapSettings mapSettings(const QgsRectangle &extent, const QSizeF &size, int dpi) const
Return map settings that would be used for drawing of the map.
typedef ItemFlags
void setFont(const QFont &font)
QString id() const
Get item's id (which is not necessarly unique)