QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgslayertreeutils.h"
33 #include <QDomDocument>
34 #include <QDomElement>
35 #include <QPainter>
36 
38  : QgsComposerItem( composition )
39  , mCustomLayerTree( nullptr )
40  , mComposerMap( nullptr )
41  , mLegendFilterByMap( false )
42  , mFilterOutAtlas( false )
43  , mFilterAskedForUpdate( false )
44  , mInAtlas( false )
45  , mInitialMapScaleCalculated( false )
46  , mForceResize( false )
47  , mSizeToContents( true )
48 {
49  mLegendModel2 = new QgsLegendModelV2( QgsProject::instance()->layerTreeRoot() );
50 
51  connect( &mLegendModel, SIGNAL( layersChanged() ), this, SLOT( synchronizeWithModel() ) );
52 
53  connect( &composition->atlasComposition(), SIGNAL( renderEnded() ), this, SLOT( onAtlasEnded() ) );
54  connect( &composition->atlasComposition(), SIGNAL( featureChanged( QgsFeature* ) ), this, SLOT( onAtlasFeature( QgsFeature* ) ) );
55 
56  // Connect to the main layertreeroot.
57  // It serves in "auto update mode" as a medium between the main app legend and this one
58  connect( QgsProject::instance()->layerTreeRoot(), SIGNAL( customPropertyChanged( QgsLayerTreeNode*, QString ) ), this, SLOT( nodeCustomPropertyChanged( QgsLayerTreeNode*, QString ) ) );
59 }
60 
62  : QgsComposerItem( nullptr )
63  , mLegendModel2( nullptr )
64  , mCustomLayerTree( nullptr )
65  , mComposerMap( nullptr )
66  , mLegendFilterByMap( false )
67  , mLegendFilterByExpression( false )
68  , mFilterOutAtlas( false )
69  , mFilterAskedForUpdate( false )
70  , mInAtlas( false )
71  , mInitialMapScaleCalculated( false )
72  , mForceResize( false )
73  , mSizeToContents( true )
74 {
75 
76 }
77 
79 {
80  delete mLegendModel2;
81  delete mCustomLayerTree;
82 }
83 
84 void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
85 {
86  Q_UNUSED( itemStyle );
87  Q_UNUSED( pWidget );
88 
89  if ( !painter )
90  return;
91 
92  if ( !shouldDrawItem() )
93  {
94  return;
95  }
96 
97  if ( mFilterAskedForUpdate )
98  {
99  mFilterAskedForUpdate = false;
100  doUpdateFilterByMap();
101  }
102 
103  int dpi = painter->device()->logicalDpiX();
104  double dotsPerMM = dpi / 25.4;
105 
106  if ( mComposition )
107  {
109  mSettings.setDpi( dpi );
110  }
111  if ( mComposerMap )
112  {
113  mSettings.setMmPerMapUnit( mComposerMap->mapUnitsToMM() );
114 
115  // use a temporary QgsMapSettings to find out real map scale
116  QgsMapSettings ms = mComposerMap->composition()->mapSettings();
117  ms.setOutputSize( QSizeF( mComposerMap->rect().width() * dotsPerMM, mComposerMap->rect().height() * dotsPerMM ).toSize() );
118  ms.setExtent( *mComposerMap->currentMapExtent() );
119  ms.setOutputDpi( dpi );
120  mSettings.setMapScale( ms.scale() );
121  }
122  mInitialMapScaleCalculated = true;
123 
124  QgsLegendRenderer legendRenderer( mLegendModel2, mSettings );
125  legendRenderer.setLegendSize( mForceResize && mSizeToContents ? QSize() : rect().size() );
126 
127  //adjust box if width or height is too small
128  if ( mSizeToContents )
129  {
130  QSizeF size = legendRenderer.minimumSize();
131  if ( mForceResize )
132  {
133  mForceResize = false;
134  //set new rect, respecting position mode and data defined size/position
135  QRectF targetRect = QRectF( pos().x(), pos().y(), size.width(), size.height() );
136  setSceneRect( evalItemRect( targetRect, true ) );
137  }
138  else if ( size.height() > rect().height() || size.width() > rect().width() )
139  {
140  //need to resize box
141  QRectF targetRect = QRectF( pos().x(), pos().y(), rect().width(), rect().height() );
142  if ( size.height() > targetRect.height() )
143  targetRect.setHeight( size.height() );
144  if ( size.width() > rect().width() )
145  targetRect.setWidth( size.width() );
146 
147  //set new rect, respecting position mode and data defined size/position
148  setSceneRect( evalItemRect( targetRect, true ) );
149  }
150  }
151 
152  drawBackground( painter );
153  painter->save();
154  //antialiasing on
155  painter->setRenderHint( QPainter::Antialiasing, true );
156  painter->setPen( QPen( QColor( 0, 0, 0 ) ) );
157 
158  if ( !mSizeToContents )
159  {
160  // set a clip region to crop out parts of legend which don't fit
161  QRectF thisPaintRect = QRectF( 0, 0, rect().width(), rect().height() );
162  painter->setClipRect( thisPaintRect );
163  }
164 
165  legendRenderer.drawLegend( painter );
166 
167  painter->restore();
168 
169  //draw frame and selection boxes if necessary
170  drawFrame( painter );
171  if ( isSelected() )
172  {
173  drawSelectionBoxes( painter );
174  }
175 }
176 
178 {
179  if ( mFilterAskedForUpdate )
180  {
181  mFilterAskedForUpdate = false;
182  doUpdateFilterByMap();
183  }
184 
185  QgsLegendRenderer legendRenderer( mLegendModel2, mSettings );
186  QSizeF size = legendRenderer.minimumSize();
187  if ( painter )
188  legendRenderer.drawLegend( painter );
189  return size;
190 }
191 
192 
194 {
195  if ( !mSizeToContents )
196  return;
197 
198  if ( !mInitialMapScaleCalculated )
199  {
200  // this is messy - but until we have painted the item we have no knowledge of the current DPI
201  // and so cannot correctly calculate the map scale. This results in incorrect size calculations
202  // for marker symbols with size in map units, causing the legends to initially expand to huge
203  // sizes if we attempt to calculate the box size first.
204  return;
205  }
206 
207  QgsLegendRenderer legendRenderer( mLegendModel2, mSettings );
208  QSizeF size = legendRenderer.minimumSize();
209  QgsDebugMsg( QString( "width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
210  if ( size.isValid() )
211  {
212  QRectF targetRect = QRectF( pos().x(), pos().y(), size.width(), size.height() );
213  //set new rect, respecting position mode and data defined size/position
214  setSceneRect( evalItemRect( targetRect, true ) );
215  }
216 }
217 
219 {
220  mSizeToContents = enabled;
221 }
222 
224 {
225  return mSizeToContents;
226 }
227 
228 void QgsComposerLegend::setCustomLayerTree( QgsLayerTreeGroup* rootGroup )
229 {
230  mLegendModel2->setRootGroup( rootGroup ? rootGroup : QgsProject::instance()->layerTreeRoot() );
231 
232  delete mCustomLayerTree;
233  mCustomLayerTree = rootGroup;
234 }
235 
236 
238 {
239  if ( autoUpdate == autoUpdateModel() )
240  return;
241 
242  setCustomLayerTree( autoUpdate ? nullptr : QgsLayerTree::toGroup( QgsProject::instance()->layerTreeRoot()->clone() ) );
243  adjustBoxSize();
244  updateItem();
245 }
246 
247 void QgsComposerLegend::nodeCustomPropertyChanged( QgsLayerTreeNode*, const QString& )
248 {
249  if ( autoUpdateModel() )
250  {
251  // in "auto update" mode, some parameters on the main app legend may have been changed (expression filtering)
252  // we must then call updateItem to reflect the changes
253  updateItem();
254  }
255 }
256 
258 {
259  return !mCustomLayerTree;
260 }
261 
263 {
264  mLegendFilterByMap = enabled;
265  updateItem();
266 }
267 
269 {
270  mSettings.setTitle( t );
271 
272  if ( mComposition && id().isEmpty() )
273  {
274  //notify the model that the display name has changed
276  }
277 }
278 QString QgsComposerLegend::title() const { return mSettings.title(); }
279 
280 Qt::AlignmentFlag QgsComposerLegend::titleAlignment() const { return mSettings.titleAlignment(); }
281 void QgsComposerLegend::setTitleAlignment( Qt::AlignmentFlag alignment ) { mSettings.setTitleAlignment( alignment ); }
282 
286 
289 
292 
293 double QgsComposerLegend::boxSpace() const { return mSettings.boxSpace(); }
294 void QgsComposerLegend::setBoxSpace( double s ) { mSettings.setBoxSpace( s ); }
295 
296 double QgsComposerLegend::columnSpace() const { return mSettings.columnSpace(); }
297 void QgsComposerLegend::setColumnSpace( double s ) { mSettings.setColumnSpace( s ); }
298 
299 QColor QgsComposerLegend::fontColor() const { return mSettings.fontColor(); }
300 void QgsComposerLegend::setFontColor( const QColor& c ) { mSettings.setFontColor( c ); }
301 
302 double QgsComposerLegend::symbolWidth() const { return mSettings.symbolSize().width(); }
303 void QgsComposerLegend::setSymbolWidth( double w ) { mSettings.setSymbolSize( QSizeF( w, mSettings.symbolSize().height() ) ); }
304 
305 double QgsComposerLegend::symbolHeight() const { return mSettings.symbolSize().height(); }
306 void QgsComposerLegend::setSymbolHeight( double h ) { mSettings.setSymbolSize( QSizeF( mSettings.symbolSize().width(), h ) ); }
307 
308 double QgsComposerLegend::wmsLegendWidth() const { return mSettings.wmsLegendSize().width(); }
309 void QgsComposerLegend::setWmsLegendWidth( double w ) { mSettings.setWmsLegendSize( QSizeF( w, mSettings.wmsLegendSize().height() ) ); }
310 
311 double QgsComposerLegend::wmsLegendHeight() const {return mSettings.wmsLegendSize().height(); }
312 void QgsComposerLegend::setWmsLegendHeight( double h ) { mSettings.setWmsLegendSize( QSizeF( mSettings.wmsLegendSize().width(), h ) ); }
313 
314 void QgsComposerLegend::setWrapChar( const QString& t ) { mSettings.setWrapChar( t ); }
315 QString QgsComposerLegend::wrapChar() const {return mSettings.wrapChar(); }
316 
317 int QgsComposerLegend::columnCount() const { return mSettings.columnCount(); }
318 void QgsComposerLegend::setColumnCount( int c ) { mSettings.setColumnCount( c ); }
319 
320 bool QgsComposerLegend::splitLayer() const { return mSettings.splitLayer(); }
321 void QgsComposerLegend::setSplitLayer( bool s ) { mSettings.setSplitLayer( s ); }
322 
323 bool QgsComposerLegend::equalColumnWidth() const { return mSettings.equalColumnWidth(); }
325 
326 bool QgsComposerLegend::drawRasterBorder() const { return mSettings.drawRasterBorder(); }
327 void QgsComposerLegend::setDrawRasterBorder( bool enabled ) { mSettings.setDrawRasterBorder( enabled ); }
328 
330 void QgsComposerLegend::setRasterBorderColor( const QColor& color ) { mSettings.setRasterBorderColor( color ); }
331 
332 double QgsComposerLegend::rasterBorderWidth() const { return mSettings.rasterBorderWidth(); }
333 void QgsComposerLegend::setRasterBorderWidth( double width ) { mSettings.setRasterBorderWidth( width ); }
334 
336 {
337  adjustBoxSize();
338  updateItem();
339 }
340 
342 {
343  // take layer list from map renderer (to have legend order)
344  mLegendModel.blockSignals( true );
346  mLegendModel.blockSignals( false );
347  adjustBoxSize();
348  updateItem();
349 }
350 
352 {
353  updateFilterByMap( false );
355 }
356 
358 {
359  if ( elem.isNull() )
360  {
361  return false;
362  }
363 
364  QDomElement composerLegendElem = doc.createElement( "ComposerLegend" );
365  elem.appendChild( composerLegendElem );
366 
367  //write general properties
368  composerLegendElem.setAttribute( "title", mSettings.title() );
369  composerLegendElem.setAttribute( "titleAlignment", QString::number( static_cast< int >( mSettings.titleAlignment() ) ) );
370  composerLegendElem.setAttribute( "columnCount", QString::number( mSettings.columnCount() ) );
371  composerLegendElem.setAttribute( "splitLayer", QString::number( mSettings.splitLayer() ) );
372  composerLegendElem.setAttribute( "equalColumnWidth", QString::number( mSettings.equalColumnWidth() ) );
373 
374  composerLegendElem.setAttribute( "boxSpace", QString::number( mSettings.boxSpace() ) );
375  composerLegendElem.setAttribute( "columnSpace", QString::number( mSettings.columnSpace() ) );
376 
377  composerLegendElem.setAttribute( "symbolWidth", QString::number( mSettings.symbolSize().width() ) );
378  composerLegendElem.setAttribute( "symbolHeight", QString::number( mSettings.symbolSize().height() ) );
379 
380  composerLegendElem.setAttribute( "rasterBorder", mSettings.drawRasterBorder() );
381  composerLegendElem.setAttribute( "rasterBorderColor", QgsSymbolLayerV2Utils::encodeColor( mSettings.rasterBorderColor() ) );
382  composerLegendElem.setAttribute( "rasterBorderWidth", QString::number( mSettings.rasterBorderWidth() ) );
383 
384  composerLegendElem.setAttribute( "wmsLegendWidth", QString::number( mSettings.wmsLegendSize().width() ) );
385  composerLegendElem.setAttribute( "wmsLegendHeight", QString::number( mSettings.wmsLegendSize().height() ) );
386  composerLegendElem.setAttribute( "wrapChar", mSettings.wrapChar() );
387  composerLegendElem.setAttribute( "fontColor", mSettings.fontColor().name() );
388 
389  composerLegendElem.setAttribute( "resizeToContents", mSizeToContents );
390 
391  if ( mComposerMap )
392  {
393  composerLegendElem.setAttribute( "map", mComposerMap->id() );
394  }
395 
396  QDomElement composerLegendStyles = doc.createElement( "styles" );
397  composerLegendElem.appendChild( composerLegendStyles );
398 
399  style( QgsComposerLegendStyle::Title ).writeXML( "title", composerLegendStyles, doc );
400  style( QgsComposerLegendStyle::Group ).writeXML( "group", composerLegendStyles, doc );
401  style( QgsComposerLegendStyle::Subgroup ).writeXML( "subgroup", composerLegendStyles, doc );
402  style( QgsComposerLegendStyle::Symbol ).writeXML( "symbol", composerLegendStyles, doc );
403  style( QgsComposerLegendStyle::SymbolLabel ).writeXML( "symbolLabel", composerLegendStyles, doc );
404 
405  if ( mCustomLayerTree )
406  {
407  // if not using auto-update - store the custom layer tree
408  mCustomLayerTree->writeXML( composerLegendElem );
409  }
410 
411  if ( mLegendFilterByMap )
412  {
413  composerLegendElem.setAttribute( "legendFilterByMap", "1" );
414  }
415 
416  composerLegendElem.setAttribute( "legendFilterByAtlas", mFilterOutAtlas ? "1" : "0" );
417 
418  return _writeXML( composerLegendElem, doc );
419 }
420 
421 static void _readOldLegendGroup( QDomElement& elem, QgsLayerTreeGroup* parentGroup )
422 {
423  QDomElement itemElem = elem.firstChildElement();
424 
425  while ( !itemElem.isNull() )
426  {
427 
428  if ( itemElem.tagName() == "LayerItem" )
429  {
430  QString layerId = itemElem.attribute( "layerId" );
431  if ( QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId ) )
432  {
433  QgsLayerTreeLayer* nodeLayer = parentGroup->addLayer( layer );
434  QString userText = itemElem.attribute( "userText" );
435  if ( !userText.isEmpty() )
436  nodeLayer->setCustomProperty( "legend/title-label", userText );
437  QString style = itemElem.attribute( "style" );
438  if ( !style.isEmpty() )
439  nodeLayer->setCustomProperty( "legend/title-style", style );
440  QString showFeatureCount = itemElem.attribute( "showFeatureCount" );
441  if ( showFeatureCount.toInt() )
442  nodeLayer->setCustomProperty( "showFeatureCount", 1 );
443 
444  // support for individual legend items (user text, order) not implemented yet
445  }
446  }
447  else if ( itemElem.tagName() == "GroupItem" )
448  {
449  QgsLayerTreeGroup* nodeGroup = parentGroup->addGroup( itemElem.attribute( "userText" ) );
450  QString style = itemElem.attribute( "style" );
451  if ( !style.isEmpty() )
452  nodeGroup->setCustomProperty( "legend/title-style", style );
453 
454  _readOldLegendGroup( itemElem, nodeGroup );
455  }
456 
457  itemElem = itemElem.nextSiblingElement();
458  }
459 }
460 
461 bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument& doc )
462 {
463  if ( itemElem.isNull() )
464  {
465  return false;
466  }
467 
468  //read general properties
469  mSettings.setTitle( itemElem.attribute( "title" ) );
470  if ( !itemElem.attribute( "titleAlignment" ).isEmpty() )
471  {
472  mSettings.setTitleAlignment( static_cast< Qt::AlignmentFlag >( itemElem.attribute( "titleAlignment" ).toInt() ) );
473  }
474  int colCount = itemElem.attribute( "columnCount", "1" ).toInt();
475  if ( colCount < 1 ) colCount = 1;
476  mSettings.setColumnCount( colCount );
477  mSettings.setSplitLayer( itemElem.attribute( "splitLayer", "0" ).toInt() == 1 );
478  mSettings.setEqualColumnWidth( itemElem.attribute( "equalColumnWidth", "0" ).toInt() == 1 );
479 
480  QDomNodeList stylesNodeList = itemElem.elementsByTagName( "styles" );
481  if ( !stylesNodeList.isEmpty() )
482  {
483  QDomNode stylesNode = stylesNodeList.at( 0 );
484  for ( int i = 0; i < stylesNode.childNodes().size(); i++ )
485  {
486  QDomElement styleElem = stylesNode.childNodes().at( i ).toElement();
488  style.readXML( styleElem, doc );
489  QString name = styleElem.attribute( "name" );
491  if ( name == "title" ) s = QgsComposerLegendStyle::Title;
492  else if ( name == "group" ) s = QgsComposerLegendStyle::Group;
493  else if ( name == "subgroup" ) s = QgsComposerLegendStyle::Subgroup;
494  else if ( name == "symbol" ) s = QgsComposerLegendStyle::Symbol;
495  else if ( name == "symbolLabel" ) s = QgsComposerLegendStyle::SymbolLabel;
496  else continue;
497  setStyle( s, style );
498  }
499  }
500 
501  //font color
502  QColor fontClr;
503  fontClr.setNamedColor( itemElem.attribute( "fontColor", "#000000" ) );
504  mSettings.setFontColor( fontClr );
505 
506  //spaces
507  mSettings.setBoxSpace( itemElem.attribute( "boxSpace", "2.0" ).toDouble() );
508  mSettings.setColumnSpace( itemElem.attribute( "columnSpace", "2.0" ).toDouble() );
509 
510  mSettings.setSymbolSize( QSizeF( itemElem.attribute( "symbolWidth", "7.0" ).toDouble(), itemElem.attribute( "symbolHeight", "14.0" ).toDouble() ) );
511  mSettings.setWmsLegendSize( QSizeF( itemElem.attribute( "wmsLegendWidth", "50" ).toDouble(), itemElem.attribute( "wmsLegendHeight", "25" ).toDouble() ) );
512 
513  mSettings.setDrawRasterBorder( itemElem.attribute( "rasterBorder", "1" ) != "0" );
514  mSettings.setRasterBorderColor( QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "rasterBorderColor", "0,0,0" ) ) );
515  mSettings.setRasterBorderWidth( itemElem.attribute( "rasterBorderWidth", "0" ).toDouble() );
516 
517  mSettings.setWrapChar( itemElem.attribute( "wrapChar" ) );
518 
519  mSizeToContents = itemElem.attribute( "resizeToContents", "1" ) != "0";
520 
521  //composer map
522  mLegendFilterByMap = itemElem.attribute( "legendFilterByMap", "0" ).toInt();
523  if ( !itemElem.attribute( "map" ).isEmpty() )
524  {
525  setComposerMap( mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() ) );
526  }
527  mFilterOutAtlas = itemElem.attribute( "legendFilterByAtlas", "0" ).toInt();
528 
529  QDomElement oldLegendModelElem = itemElem.firstChildElement( "Model" );
530  if ( !oldLegendModelElem.isNull() )
531  {
532  // QGIS <= 2.4
533  QgsLayerTreeGroup* nodeRoot = new QgsLayerTreeGroup();
534  _readOldLegendGroup( oldLegendModelElem, nodeRoot );
535  setCustomLayerTree( nodeRoot );
536  }
537  else
538  {
539  // QGIS >= 2.6
540  QDomElement layerTreeElem = itemElem.firstChildElement( "layer-tree-group" );
541  setCustomLayerTree( QgsLayerTreeGroup::readXML( layerTreeElem, true ) );
542  }
543 
544  //restore general composer item properties
545  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
546  if ( !composerItemList.isEmpty() )
547  {
548  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
549  _readXML( composerItemElem, doc );
550  }
551 
552  // < 2.0 projects backward compatibility >>>>>
553  //title font
554  QString titleFontString = itemElem.attribute( "titleFont" );
555  if ( !titleFontString.isEmpty() )
556  {
557  rstyle( QgsComposerLegendStyle::Title ).rfont().fromString( titleFontString );
558  }
559  //group font
560  QString groupFontString = itemElem.attribute( "groupFont" );
561  if ( !groupFontString.isEmpty() )
562  {
563  rstyle( QgsComposerLegendStyle::Group ).rfont().fromString( groupFontString );
564  }
565 
566  //layer font
567  QString layerFontString = itemElem.attribute( "layerFont" );
568  if ( !layerFontString.isEmpty() )
569  {
571  }
572  //item font
573  QString itemFontString = itemElem.attribute( "itemFont" );
574  if ( !itemFontString.isEmpty() )
575  {
577  }
578 
579  if ( !itemElem.attribute( "groupSpace" ).isEmpty() )
580  {
582  }
583  if ( !itemElem.attribute( "layerSpace" ).isEmpty() )
584  {
586  }
587  if ( !itemElem.attribute( "symbolSpace" ).isEmpty() )
588  {
591  }
592  // <<<<<<< < 2.0 projects backward compatibility
593 
594  emit itemChanged();
595  return true;
596 }
597 
599 {
600  if ( !id().isEmpty() )
601  {
602  return id();
603  }
604 
605  //if no id, default to portion of title text
606  QString text = mSettings.title();
607  if ( text.isEmpty() )
608  {
609  return tr( "<legend>" );
610  }
611  if ( text.length() > 25 )
612  {
613  return QString( tr( "%1..." ) ).arg( text.left( 25 ) );
614  }
615  else
616  {
617  return text;
618  }
619 }
620 
622 {
623  if ( mComposerMap )
624  {
625  disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
626  disconnect( mComposerMap, SIGNAL( itemChanged() ), this, SLOT( updateFilterByMap() ) );
627  disconnect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( updateFilterByMap() ) );
628  disconnect( mComposerMap, SIGNAL( layerStyleOverridesChanged() ), this, SLOT( mapLayerStyleOverridesChanged() ) );
629  }
630 
631  mComposerMap = map;
632 
633  if ( map )
634  {
635  QObject::connect( map, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
636  QObject::connect( map, SIGNAL( itemChanged() ), this, SLOT( updateFilterByMap() ) );
637  QObject::connect( map, SIGNAL( extentChanged() ), this, SLOT( updateFilterByMap() ) );
638  QObject::connect( map, SIGNAL( layerStyleOverridesChanged() ), this, SLOT( mapLayerStyleOverridesChanged() ) );
639  }
640 
641  updateItem();
642 }
643 
645 {
646  setComposerMap( nullptr );
647 }
648 
649 void QgsComposerLegend::mapLayerStyleOverridesChanged()
650 {
651  if ( !mComposerMap )
652  return;
653 
654  // map's style has been changed, so make sure to update the legend here
655  if ( mLegendFilterByMap )
656  {
657  // legend is being filtered by map, so we need to re run the hit test too
658  // as the style overrides may also have affected the visible symbols
659  updateFilterByMap( false );
660  }
661  else
662  {
663  mLegendModel2->setLayerStyleOverrides( mComposerMap->layerStyleOverrides() );
664 
665  Q_FOREACH ( QgsLayerTreeLayer* nodeLayer, mLegendModel2->rootGroup()->findLayers() )
666  mLegendModel2->refreshLayerLegend( nodeLayer );
667  }
668 
669  adjustBoxSize();
670  updateItem();
671 }
672 
673 void QgsComposerLegend::updateFilterByMap( bool redraw )
674 {
675  if ( isRemoved() )
676  return;
677  // ask for update
678  // the actual update will take place before the redraw.
679  // This is to avoid multiple calls to the filter
680  mFilterAskedForUpdate = true;
681 
682  if ( redraw )
684 }
685 
686 void QgsComposerLegend::doUpdateFilterByMap()
687 {
688  if ( mComposerMap )
689  mLegendModel2->setLayerStyleOverrides( mComposerMap->layerStyleOverrides() );
690  else
692 
693 
694  bool filterByExpression = QgsLayerTreeUtils::hasLegendFilterExpression( *( mCustomLayerTree ? mCustomLayerTree : QgsProject::instance()->layerTreeRoot() ) );
695 
696  if ( mComposerMap && ( mLegendFilterByMap || filterByExpression || mInAtlas ) )
697  {
698  int dpi = mComposition->printResolution();
699 
700  QgsRectangle requestRectangle;
701  mComposerMap->requestedExtent( requestRectangle );
702 
703  QSizeF theSize( requestRectangle.width(), requestRectangle.height() );
704  theSize *= mComposerMap->mapUnitsToMM() * dpi / 25.4;
705 
706  QgsMapSettings ms = mComposerMap->mapSettings( requestRectangle, theSize, dpi );
707 
708  QgsGeometry filterPolygon;
709  if ( mInAtlas )
710  {
711  filterPolygon = composition()->atlasComposition().currentGeometry( composition()->mapSettings().destinationCrs() );
712  }
713  mLegendModel2->setLegendFilter( &ms, /* useExtent */ mInAtlas || mLegendFilterByMap, filterPolygon, /* useExpressions */ true );
714  }
715  else
716  mLegendModel2->setLegendFilterByMap( nullptr );
717 
718  mForceResize = true;
719 }
720 
722 {
723  mFilterOutAtlas = doFilter;
724 }
725 
727 {
728  return mFilterOutAtlas;
729 }
730 
731 void QgsComposerLegend::onAtlasFeature( QgsFeature* feat )
732 {
733  if ( !feat )
734  return;
735  mInAtlas = mFilterOutAtlas;
736  updateFilterByMap();
737 }
738 
739 void QgsComposerLegend::onAtlasEnded()
740 {
741  mInAtlas = false;
742  updateFilterByMap();
743 }
744 
745 // -------------------------------------------------------------------------
747 #include "qgsvectorlayer.h"
748 
750  : QgsLayerTreeModel( rootNode, parent )
751 {
754 }
755 
757 {
758  // handle custom layer node labels
759  if ( QgsLayerTreeNode* node = index2node( index ) )
760  {
761  if ( QgsLayerTree::isLayer( node ) && ( role == Qt::DisplayRole || role == Qt::EditRole ) && !node->customProperty( "legend/title-label" ).isNull() )
762  {
763  QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node );
764  QString name = node->customProperty( "legend/title-label" ).toString();
765  if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toInt() && role == Qt::DisplayRole )
766  {
767  QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( nodeLayer->layer() );
768  if ( vlayer && vlayer->featureCount() >= 0 )
769  name += QString( " [%1]" ).arg( vlayer->featureCount() );
770  }
771  return name;
772  }
773  }
774 
775  return QgsLayerTreeModel::data( index, role );
776 }
777 
779 {
780  // make the legend nodes selectable even if they are not by default
781  if ( index2legendNode( index ) )
782  return QgsLayerTreeModel::flags( index ) | Qt::ItemIsSelectable;
783 
784  return QgsLayerTreeModel::flags( index );
785 }
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
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.
QgsMapSettings mapSettings(const QgsRectangle &extent, QSizeF size, int dpi) const
Return map settings that would be used for drawing of the map.
QgsComposerLegend(QgsComposition *composition)
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
void setExtent(const QgsRectangle &rect, bool magnified=true)
Set coordinates of the rectangle which should be rendered.
QgsLayerTreeGroup * rootGroup() const
Return pointer to the root node of the layer tree. Always a non-null pointer.
double mapUnitsToMM() const
Returns the conversion factor map units -> mm.
void setEqualColumnWidth(bool s)
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
QgsLayerTreeGroup * addGroup(const QString &name)
Append a new group node with given name. Newly created node is owned by this group.
static QgsLayerTreeGroup * readXML(QDomElement &element, bool looseMatch=false)
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on ...
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
void setBoxSpace(double s)
void setMmPerMapUnit(double mmPerMapUnit)
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
int columnCount() 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.
QDomNode appendChild(const QDomNode &newChild)
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.
QString wrapChar() const
QString attribute(const QString &name, const QString &defValue) 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 itemChanged()
Emitted when the item changes.
static QString encodeColor(const QColor &color)
void setSymbolSize(QSizeF s)
void drawLegend(QPainter *painter)
Draw the legend with given painter.
int printResolution() const
void setColumnSpace(double s)
A item that forms part of a map composition.
double rasterBorderWidth() const
Returns the border width (in millimeters) for the border drawn around raster symbol items...
void setOutputDpi(double dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
bool isValid() const
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false, const QgsExpressionContext *context=nullptr)
Evaluates an item&#39;s bounding rect to consider data defined position and size of item and reference po...
void save()
QDomElement nextSiblingElement(const QString &tagName) const
int id() const
Get identification number.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
QVariant data(const QModelIndex &index, int role) const override
QColor rasterBorderColor() const
Returns the border color for the border drawn around raster symbol items.
virtual void drawFrame(QPainter *p)
Draw black frame around item.
void setTitle(const QString &t)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
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
void writeXML(const QString &name, QDomElement &elem, QDomDocument &doc) const
QString tr(const char *sourceText, const char *disambiguation, int n)
void updateItemDisplayName(QgsComposerItem *item)
Must be called when an item&#39;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&#39;s legend)
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle &style)
void setResizeToContents(bool enabled)
Sets whether the legend should automatically resize to fit its contents.
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.
QDomElement toElement() const
long featureCount(QgsSymbolV2 *symbol)
Number of features rendered with specified symbol.
bool isEmpty() const
The QgsLayerTreeModel class is model implementation for Qt item views framework.
QSizeF wmsLegendSize() const
const char * name() const
QPointF pos() const
QString number(int n, int base)
void setOutputSize(QSize size)
Set the size of the resulting map image.
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.
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
bool equalColumnWidth() const
void setSymbolHeight(double h)
double rasterBorderWidth() const
Returns the border width (in millimeters) for the border drawn around raster symbol items...
void setLegendFilterOutAtlas(bool doFilter)
When set to true, during an atlas rendering, it will filter out legend elements where features are ou...
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
void setPen(const QColor &color)
void setAttribute(const QString &name, const QString &value)
bool isSelected() const
double scale() const
Return the calculated scale of the map.
void setLegendFilterByMap(const QgsMapSettings *settings)
Force only display of legend nodes which are valid for given map settings.
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:207
int toInt(bool *ok, int base) const
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Return legend node for given index.
bool useAdvancedEffects() const
Returns true if a composition should use advanced effects such as blend modes.
void setMapScale(double scale)
bool isEmpty() const
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QColor fontColor() const
void setComposerMap(const QgsComposerMap *map)
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
QPaintDevice * device() const
void synchronizeWithModel()
Data changed.
void setStyleFont(QgsComposerLegendStyle::Style s, const QFont &f)
Set style font.
void setTitle(const QString &t)
This class is a base class for nodes in a layer tree.
bool writeXML(QDomElement &elem, QDomDocument &doc) const override
Stores state in Dom node.
QMap< QString, QString > layerStyleOverrides() const
Getter for stored overrides of styles for layers.
double symbolWidth() const
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
double wmsLegendHeight() const
void setDrawRasterBorder(bool enabled)
Sets whether a border will be drawn around raster symbol items.
void setDpi(int dpi)
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
Sets state from Dom document.
double boxSpace() const
QString wrapChar() const
void setFontColor(const QColor &c)
static bool hasLegendFilterExpression(const QgsLayerTreeGroup &group)
Test if one of the layers in a group has an expression filter.
bool isLayer(QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:40
void setColumnSpace(double s)
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 <layer-tree-group> and add it to the given parent element...
Graphics scene for map printing.
virtual void updateItem() override
Update() overloading.
QSizeF minimumSize()
Run the layout algorithm and determine the size required for legend.
void setLegendFilterByMapEnabled(bool enabled)
Set whether legend items should be filtered to show just the ones visible in the associated map...
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
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.
bool blockSignals(bool block)
bool autoUpdateModel() const
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
virtual bool isRemoved() const
Returns whether this item has been removed from the composition.
void setWmsLegendWidth(double w)
QSizeF symbolSize() const
bool resizeToContents() const
Returns whether the legend should automatically resize to fit its contents.
bool isNull() const
QgsMapLayer * layer() const
void restore()
double symbolHeight() const
QgsLegendModelV2(QgsLayerTreeGroup *rootNode, QObject *parent=nullptr)
QgsComposition * mComposition
Composer legend components style.
void setWmsLegendSize(QSizeF s)
QgsGeometry currentGeometry(const QgsCoordinateReferenceSystem &projectedTo=QgsCoordinateReferenceSystem()) const
Returns the current atlas geometry in the given projection system (default to the coverage layer&#39;s CR...
void setUseAdvancedEffects(bool use)
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
qreal width() const
void setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
void setAutoUpdateModel(bool autoUpdate)
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Return layer tree node for given index.
void setStyleMargin(QgsComposerLegendStyle::Style s, double margin)
Set style margin.
const QgsComposition * composition() const
Returns the composition the item is attached to.
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 setWidth(qreal width)
bool equalColumnWidth() const
virtual void drawBackground(QPainter *p)
Draw background.
void setColumnCount(int c)
double columnSpace() const
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:382
QDomElement firstChildElement(const QString &tagName) const
void requestedExtent(QgsRectangle &extent) const
Calculates the extent to request and the yShift of the top-left point in case of rotation.
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)
int length() const
QString title() const
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
QString left(int n) const
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle &style)
qreal height() const
QgsAtlasComposition & atlasComposition()
double wmsLegendWidth() const
static QColor decodeColor(const QString &str)
void setLayerSet(const QStringList &layerIds, double scaleDenominator=-1, const QString &rule="")
Flags flags() const
Return OR-ed combination of model flags.
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
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes. Searches recursively the whole sub-tree.
QgsComposerItem(QgsComposition *composition, bool manageZValue=true)
Constructor.
QColor rasterBorderColor() const
Returns the border color for the border drawn around raster symbol items.
QDomElement createElement(const QString &tagName)
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 setFontColor(const QColor &c)
bool legendFilterOutAtlas() const
Whether to filter out legend elements outside of the current atlas feature.
qreal height() const
QColor fontColor() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
void setLegendFilter(const QgsMapSettings *settings, bool useExtent=true, const QgsGeometry &polygon=QgsGeometry(), bool useExpressions=true)
Filter display of legend nodes for given map settings.
Represents a vector layer which manages a vector based data sets.
bool drawRasterBorder() const
Returns whether a border will be drawn around raster symbol items.
bool splitLayer() const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString id() const
Get item&#39;s id (which is not necessarly unique)
const QgsRectangle * currentMapExtent() const
Returns a pointer to the current map extent, which is either the original user specified extent or th...
void setBoxSpace(double s)
Allow reordering with drag&#39;n&#39;drop.
void destroyed(QObject *obj)
qreal width() const
double columnSpace() const
static void _readOldLegendGroup(QDomElement &elem, QgsLayerTreeGroup *parentGroup)
void setSplitLayer(bool s)
bool drawRasterBorder() const
Returns whether a border will be drawn around raster symbol items.
void setRasterBorderColor(const QColor &color)
Sets the border color for the border drawn around raster symbol items.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:212
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.
QString title() const
QDomNode at(int index) const
QRectF rect() const
QFont styleFont(QgsComposerLegendStyle::Style s) const
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
typedef ItemFlags
void setFont(const QFont &font)
double boxSpace() const