QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <QDomDocument>
32 #include <QDomElement>
33 #include <QPainter>
34 
36  : QgsComposerItem( composition )
37  , mCustomLayerTree( 0 )
38  , mComposerMap( 0 )
39  , mLegendFilterByMap( false )
40 {
41  mLegendModel2 = new QgsLegendModelV2( QgsProject::instance()->layerTreeRoot() );
42 
43  adjustBoxSize();
44 
45  connect( &mLegendModel, SIGNAL( layersChanged() ), this, SLOT( synchronizeWithModel() ) );
46 }
47 
48 QgsComposerLegend::QgsComposerLegend()
49  : QgsComposerItem( 0 )
50  , mLegendModel2( 0 )
51  , mCustomLayerTree( 0 )
52  , mComposerMap( 0 )
53 {
54 
55 }
56 
58 {
59  delete mLegendModel2;
60  delete mCustomLayerTree;
61 }
62 
63 void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
64 {
65  Q_UNUSED( itemStyle );
66  Q_UNUSED( pWidget );
67 
68  if ( !painter )
69  return;
70 
71  if ( !shouldDrawItem() )
72  {
73  return;
74  }
75 
76  int dpi = painter->device()->logicalDpiX();
77  double dotsPerMM = dpi / 25.4;
78 
79  if ( mComposition )
80  {
82  mSettings.setDpi( dpi );
83  }
84  if ( mComposerMap )
85  {
86  mSettings.setMmPerMapUnit( mComposerMap->mapUnitsToMM() );
87 
88  // use a temporary QgsMapSettings to find out real map scale
89  QgsMapSettings ms = mComposerMap->composition()->mapSettings();
90  ms.setOutputSize( QSizeF( mComposerMap->rect().width() * dotsPerMM, mComposerMap->rect().height() * dotsPerMM ).toSize() );
91  ms.setExtent( *mComposerMap->currentMapExtent() );
92  ms.setOutputDpi( dpi );
93  mSettings.setMapScale( ms.scale() );
94  }
95 
96  drawBackground( painter );
97  painter->save();
98  //antialiasing on
99  painter->setRenderHint( QPainter::Antialiasing, true );
100  painter->setPen( QPen( QColor( 0, 0, 0 ) ) );
101 
102  QgsLegendRenderer legendRenderer( mLegendModel2, mSettings );
103  legendRenderer.setLegendSize( rect().size() );
104 
105  //adjust box if width or height is too small
106  QSizeF size = legendRenderer.minimumSize();
107  if ( size.height() > rect().height() || size.width() > rect().width() )
108  {
109  //need to resize box
110  QRectF targetRect = QRectF( pos().x(), pos().y(), rect().width(), rect().height() );
111  if ( size.height() > targetRect.height() )
112  targetRect.setHeight( size.height() );
113  if ( size.width() > rect().width() )
114  targetRect.setWidth( size.width() );
115 
116  //set new rect, respecting position mode and data defined size/position
117  setSceneRect( evalItemRect( targetRect, true ) );
118  }
119 
120  legendRenderer.drawLegend( painter );
121 
122  painter->restore();
123 
124  //draw frame and selection boxes if necessary
125  drawFrame( painter );
126  if ( isSelected() )
127  {
128  drawSelectionBoxes( painter );
129  }
130 }
131 
132 QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
133 {
134  QgsLegendRenderer legendRenderer( mLegendModel2, mSettings );
135  QSizeF size = legendRenderer.minimumSize();
136  if ( painter )
137  legendRenderer.drawLegend( painter );
138  return size;
139 }
140 
141 
143 {
144  QgsLegendRenderer legendRenderer( mLegendModel2, mSettings );
145  QSizeF size = legendRenderer.minimumSize();
146  QgsDebugMsg( QString( "width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
147  if ( size.isValid() )
148  {
149  QRectF targetRect = QRectF( pos().x(), pos().y(), size.width(), size.height() );
150  //set new rect, respecting position mode and data defined size/position
151  setSceneRect( evalItemRect( targetRect, true ) );
152  }
153 }
154 
155 
156 void QgsComposerLegend::setCustomLayerTree( QgsLayerTreeGroup* rootGroup )
157 {
158  mLegendModel2->setRootGroup( rootGroup ? rootGroup : QgsProject::instance()->layerTreeRoot() );
159 
160  delete mCustomLayerTree;
161  mCustomLayerTree = rootGroup;
162 }
163 
164 
166 {
167  if ( autoUpdate == autoUpdateModel() )
168  return;
169 
170  setCustomLayerTree( autoUpdate ? 0 : QgsLayerTree::toGroup( QgsProject::instance()->layerTreeRoot()->clone() ) );
171  adjustBoxSize();
172 }
173 
175 {
176  return !mCustomLayerTree;
177 }
178 
180 {
181  mLegendFilterByMap = enabled;
182  updateFilterByMap();
183 }
184 
185 void QgsComposerLegend::setTitle( const QString& t )
186 {
187  mSettings.setTitle( t );
188 
189  if ( mComposition && id().isEmpty() )
190  {
191  //notify the model that the display name has changed
193  }
194 }
195 QString QgsComposerLegend::title() const { return mSettings.title(); }
196 
197 Qt::AlignmentFlag QgsComposerLegend::titleAlignment() const { return mSettings.titleAlignment(); }
198 void QgsComposerLegend::setTitleAlignment( Qt::AlignmentFlag alignment ) { mSettings.setTitleAlignment( alignment ); }
199 
203 
204 QFont QgsComposerLegend::styleFont( QgsComposerLegendStyle::Style s ) const { return mSettings.style( s ).font(); }
206 
209 
210 double QgsComposerLegend::boxSpace() const { return mSettings.boxSpace(); }
211 void QgsComposerLegend::setBoxSpace( double s ) { mSettings.setBoxSpace( s ); }
212 
213 double QgsComposerLegend::columnSpace() const { return mSettings.columnSpace(); }
214 void QgsComposerLegend::setColumnSpace( double s ) { mSettings.setColumnSpace( s ); }
215 
216 QColor QgsComposerLegend::fontColor() const { return mSettings.fontColor(); }
217 void QgsComposerLegend::setFontColor( const QColor& c ) { mSettings.setFontColor( c ); }
218 
219 double QgsComposerLegend::symbolWidth() const { return mSettings.symbolSize().width(); }
220 void QgsComposerLegend::setSymbolWidth( double w ) { mSettings.setSymbolSize( QSizeF( w, mSettings.symbolSize().height() ) ); }
221 
222 double QgsComposerLegend::symbolHeight() const { return mSettings.symbolSize().height(); }
223 void QgsComposerLegend::setSymbolHeight( double h ) { mSettings.setSymbolSize( QSizeF( mSettings.symbolSize().width(), h ) ); }
224 
225 double QgsComposerLegend::wmsLegendWidth() const { return mSettings.wmsLegendSize().width(); }
226 void QgsComposerLegend::setWmsLegendWidth( double w ) { mSettings.setWmsLegendSize( QSizeF( w, mSettings.wmsLegendSize().height() ) ); }
227 
228 double QgsComposerLegend::wmsLegendHeight() const {return mSettings.wmsLegendSize().height(); }
229 void QgsComposerLegend::setWmsLegendHeight( double h ) { mSettings.setWmsLegendSize( QSizeF( mSettings.wmsLegendSize().width(), h ) ); }
230 
231 void QgsComposerLegend::setWrapChar( const QString& t ) { mSettings.setWrapChar( t ); }
232 QString QgsComposerLegend::wrapChar() const {return mSettings.wrapChar(); }
233 
234 int QgsComposerLegend::columnCount() const { return mSettings.columnCount(); }
235 void QgsComposerLegend::setColumnCount( int c ) { mSettings.setColumnCount( c ); }
236 
237 int QgsComposerLegend::splitLayer() const { return mSettings.splitLayer(); }
238 void QgsComposerLegend::setSplitLayer( bool s ) { mSettings.setSplitLayer( s ); }
239 
240 int QgsComposerLegend::equalColumnWidth() const { return mSettings.equalColumnWidth(); }
242 
243 
245 {
246  QgsDebugMsg( "Entered" );
247  adjustBoxSize();
248  update();
249 }
250 
252 {
253  // take layer list from map renderer (to have legend order)
254  mLegendModel.setLayerSet( mComposition ? mComposition->mapSettings().layers() : QStringList() );
255  adjustBoxSize();
256  update();
257 }
258 
259 bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const
260 {
261  if ( elem.isNull() )
262  {
263  return false;
264  }
265 
266  QDomElement composerLegendElem = doc.createElement( "ComposerLegend" );
267  elem.appendChild( composerLegendElem );
268 
269  //write general properties
270  composerLegendElem.setAttribute( "title", mSettings.title() );
271  composerLegendElem.setAttribute( "titleAlignment", QString::number(( int ) mSettings.titleAlignment() ) );
272  composerLegendElem.setAttribute( "columnCount", QString::number( mSettings.columnCount() ) );
273  composerLegendElem.setAttribute( "splitLayer", QString::number( mSettings.splitLayer() ) );
274  composerLegendElem.setAttribute( "equalColumnWidth", QString::number( mSettings.equalColumnWidth() ) );
275 
276  composerLegendElem.setAttribute( "boxSpace", QString::number( mSettings.boxSpace() ) );
277  composerLegendElem.setAttribute( "columnSpace", QString::number( mSettings.columnSpace() ) );
278 
279  composerLegendElem.setAttribute( "symbolWidth", QString::number( mSettings.symbolSize().width() ) );
280  composerLegendElem.setAttribute( "symbolHeight", QString::number( mSettings.symbolSize().height() ) );
281  composerLegendElem.setAttribute( "wmsLegendWidth", QString::number( mSettings.wmsLegendSize().width() ) );
282  composerLegendElem.setAttribute( "wmsLegendHeight", QString::number( mSettings.wmsLegendSize().height() ) );
283  composerLegendElem.setAttribute( "wrapChar", mSettings.wrapChar() );
284  composerLegendElem.setAttribute( "fontColor", mSettings.fontColor().name() );
285 
286  if ( mComposerMap )
287  {
288  composerLegendElem.setAttribute( "map", mComposerMap->id() );
289  }
290 
291  QDomElement composerLegendStyles = doc.createElement( "styles" );
292  composerLegendElem.appendChild( composerLegendStyles );
293 
294  style( QgsComposerLegendStyle::Title ).writeXML( "title", composerLegendStyles, doc );
295  style( QgsComposerLegendStyle::Group ).writeXML( "group", composerLegendStyles, doc );
296  style( QgsComposerLegendStyle::Subgroup ).writeXML( "subgroup", composerLegendStyles, doc );
297  style( QgsComposerLegendStyle::Symbol ).writeXML( "symbol", composerLegendStyles, doc );
298  style( QgsComposerLegendStyle::SymbolLabel ).writeXML( "symbolLabel", composerLegendStyles, doc );
299 
300  if ( mCustomLayerTree )
301  {
302  // if not using auto-update - store the custom layer tree
303  mCustomLayerTree->writeXML( composerLegendElem );
304  }
305 
306  if ( mLegendFilterByMap )
307  composerLegendElem.setAttribute( "legendFilterByMap", "1" );
308 
309  return _writeXML( composerLegendElem, doc );
310 }
311 
312 static void _readOldLegendGroup( QDomElement& elem, QgsLayerTreeGroup* parentGroup )
313 {
314  QDomElement itemElem = elem.firstChildElement();
315 
316  while ( !itemElem.isNull() )
317  {
318 
319  if ( itemElem.tagName() == "LayerItem" )
320  {
321  QString layerId = itemElem.attribute( "layerId" );
322  if ( QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId ) )
323  {
324  QgsLayerTreeLayer* nodeLayer = parentGroup->addLayer( layer );
325  QString userText = itemElem.attribute( "userText" );
326  if ( !userText.isEmpty() )
327  nodeLayer->setCustomProperty( "legend/title-label", userText );
328  QString style = itemElem.attribute( "style" );
329  if ( !style.isEmpty() )
330  nodeLayer->setCustomProperty( "legend/title-style", style );
331  QString showFeatureCount = itemElem.attribute( "showFeatureCount" );
332  if ( showFeatureCount.toInt() )
333  nodeLayer->setCustomProperty( "showFeatureCount", 1 );
334 
335  // support for individual legend items (user text, order) not implemented yet
336  }
337  }
338  else if ( itemElem.tagName() == "GroupItem" )
339  {
340  QgsLayerTreeGroup* nodeGroup = parentGroup->addGroup( itemElem.attribute( "userText" ) );
341  QString style = itemElem.attribute( "style" );
342  if ( !style.isEmpty() )
343  nodeGroup->setCustomProperty( "legend/title-style", style );
344 
345  _readOldLegendGroup( itemElem, nodeGroup );
346  }
347 
348  itemElem = itemElem.nextSiblingElement();
349  }
350 }
351 
352 bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument& doc )
353 {
354  if ( itemElem.isNull() )
355  {
356  return false;
357  }
358 
359  //read general properties
360  mSettings.setTitle( itemElem.attribute( "title" ) );
361  if ( !itemElem.attribute( "titleAlignment" ).isEmpty() )
362  {
363  mSettings.setTitleAlignment(( Qt::AlignmentFlag )itemElem.attribute( "titleAlignment" ).toInt() );
364  }
365  int colCount = itemElem.attribute( "columnCount", "1" ).toInt();
366  if ( colCount < 1 ) colCount = 1;
367  mSettings.setColumnCount( colCount );
368  mSettings.setSplitLayer( itemElem.attribute( "splitLayer", "0" ).toInt() == 1 );
369  mSettings.setEqualColumnWidth( itemElem.attribute( "equalColumnWidth", "0" ).toInt() == 1 );
370 
371  QDomNodeList stylesNodeList = itemElem.elementsByTagName( "styles" );
372  if ( stylesNodeList.size() > 0 )
373  {
374  QDomNode stylesNode = stylesNodeList.at( 0 );
375  for ( int i = 0; i < stylesNode.childNodes().size(); i++ )
376  {
377  QDomElement styleElem = stylesNode.childNodes().at( i ).toElement();
379  style.readXML( styleElem, doc );
380  QString name = styleElem.attribute( "name" );
382  if ( name == "title" ) s = QgsComposerLegendStyle::Title;
383  else if ( name == "group" ) s = QgsComposerLegendStyle::Group;
384  else if ( name == "subgroup" ) s = QgsComposerLegendStyle::Subgroup;
385  else if ( name == "symbol" ) s = QgsComposerLegendStyle::Symbol;
386  else if ( name == "symbolLabel" ) s = QgsComposerLegendStyle::SymbolLabel;
387  else continue;
388  setStyle( s, style );
389  }
390  }
391 
392  //font color
393  QColor fontClr;
394  fontClr.setNamedColor( itemElem.attribute( "fontColor", "#000000" ) );
395  mSettings.setFontColor( fontClr );
396 
397  //spaces
398  mSettings.setBoxSpace( itemElem.attribute( "boxSpace", "2.0" ).toDouble() );
399  mSettings.setColumnSpace( itemElem.attribute( "columnSpace", "2.0" ).toDouble() );
400 
401  mSettings.setSymbolSize( QSizeF( itemElem.attribute( "symbolWidth", "7.0" ).toDouble(), itemElem.attribute( "symbolHeight", "14.0" ).toDouble() ) );
402  mSettings.setWmsLegendSize( QSizeF( itemElem.attribute( "wmsLegendWidth", "50" ).toDouble(), itemElem.attribute( "wmsLegendHeight", "25" ).toDouble() ) );
403 
404  mSettings.setWrapChar( itemElem.attribute( "wrapChar" ) );
405 
406  //composer map
407  mLegendFilterByMap = itemElem.attribute( "legendFilterByMap", "0" ).toInt();
408  if ( !itemElem.attribute( "map" ).isEmpty() )
409  {
410  setComposerMap( mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() ) );
411  }
412 
413  QDomElement oldLegendModelElem = itemElem.firstChildElement( "Model" );
414  if ( !oldLegendModelElem.isNull() )
415  {
416  // QGIS <= 2.4
417  QgsLayerTreeGroup* nodeRoot = new QgsLayerTreeGroup();
418  _readOldLegendGroup( oldLegendModelElem, nodeRoot );
419  setCustomLayerTree( nodeRoot );
420  }
421  else
422  {
423  // QGIS >= 2.6
424  QDomElement layerTreeElem = itemElem.firstChildElement( "layer-tree-group" );
425  setCustomLayerTree( QgsLayerTreeGroup::readXML( layerTreeElem ) );
426  }
427 
428  //restore general composer item properties
429  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
430  if ( composerItemList.size() > 0 )
431  {
432  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
433  _readXML( composerItemElem, doc );
434  }
435 
436  // < 2.0 projects backward compatibility >>>>>
437  //title font
438  QString titleFontString = itemElem.attribute( "titleFont" );
439  if ( !titleFontString.isEmpty() )
440  {
441  rstyle( QgsComposerLegendStyle::Title ).rfont().fromString( titleFontString );
442  }
443  //group font
444  QString groupFontString = itemElem.attribute( "groupFont" );
445  if ( !groupFontString.isEmpty() )
446  {
447  rstyle( QgsComposerLegendStyle::Group ).rfont().fromString( groupFontString );
448  }
449 
450  //layer font
451  QString layerFontString = itemElem.attribute( "layerFont" );
452  if ( !layerFontString.isEmpty() )
453  {
454  rstyle( QgsComposerLegendStyle::Subgroup ).rfont().fromString( layerFontString );
455  }
456  //item font
457  QString itemFontString = itemElem.attribute( "itemFont" );
458  if ( !itemFontString.isEmpty() )
459  {
460  rstyle( QgsComposerLegendStyle::SymbolLabel ).rfont().fromString( itemFontString );
461  }
462 
463  if ( !itemElem.attribute( "groupSpace" ).isEmpty() )
464  {
465  rstyle( QgsComposerLegendStyle::Group ).setMargin( QgsComposerLegendStyle::Top, itemElem.attribute( "groupSpace", "3.0" ).toDouble() );
466  }
467  if ( !itemElem.attribute( "layerSpace" ).isEmpty() )
468  {
469  rstyle( QgsComposerLegendStyle::Subgroup ).setMargin( QgsComposerLegendStyle::Top, itemElem.attribute( "layerSpace", "3.0" ).toDouble() );
470  }
471  if ( !itemElem.attribute( "symbolSpace" ).isEmpty() )
472  {
473  rstyle( QgsComposerLegendStyle::Symbol ).setMargin( QgsComposerLegendStyle::Top, itemElem.attribute( "symbolSpace", "2.0" ).toDouble() );
474  rstyle( QgsComposerLegendStyle::SymbolLabel ).setMargin( QgsComposerLegendStyle::Top, itemElem.attribute( "symbolSpace", "2.0" ).toDouble() );
475  }
476  // <<<<<<< < 2.0 projects backward compatibility
477 
478  emit itemChanged();
479  return true;
480 }
481 
483 {
484  if ( !id().isEmpty() )
485  {
486  return id();
487  }
488 
489  //if no id, default to portion of title text
490  QString text = mSettings.title();
491  if ( text.isEmpty() )
492  {
493  return tr( "<legend>" );
494  }
495  if ( text.length() > 25 )
496  {
497  return QString( tr( "%1..." ) ).arg( text.left( 25 ) );
498  }
499  else
500  {
501  return text;
502  }
503 }
504 
506 {
507  if ( mComposerMap )
508  {
509  disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
510  disconnect( mComposerMap, SIGNAL( itemChanged() ), this, SLOT( updateFilterByMap() ) );
511  disconnect( mComposerMap, SIGNAL( extentChanged() ), this, SLOT( updateFilterByMap() ) );
512  }
513 
514  mComposerMap = map;
515 
516  if ( map )
517  {
518  QObject::connect( map, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
519  QObject::connect( map, SIGNAL( itemChanged() ), this, SLOT( updateFilterByMap() ) );
520  QObject::connect( map, SIGNAL( extentChanged() ), this, SLOT( updateFilterByMap() ) );
521  }
522 
523  updateFilterByMap();
524 }
525 
527 {
528  setComposerMap( 0 );
529 }
530 
531 void QgsComposerLegend::updateFilterByMap()
532 {
533  if ( isRemoved() )
534  return;
535 
536  if ( mComposerMap && mLegendFilterByMap )
537  {
538  int dpi = mComposition->printResolution();
539 
540  QgsRectangle requestRectangle;
541  mComposerMap->requestedExtent( requestRectangle );
542 
543  QSizeF theSize( requestRectangle.width(), requestRectangle.height() );
544  theSize *= mComposerMap->mapUnitsToMM() * dpi / 25.4;
545 
546  QgsMapSettings ms = mComposerMap->mapSettings( requestRectangle, theSize, dpi );
547 
548  mLegendModel2->setLegendFilterByMap( &ms );
549  }
550  else
551  mLegendModel2->setLegendFilterByMap( 0 );
552 
553  adjustBoxSize();
554  update();
555 }
556 
557 // -------------------------------------------------------------------------
559 #include "qgsvectorlayer.h"
560 
562  : QgsLayerTreeModel( rootNode, parent )
563 {
566 }
567 
568 QVariant QgsLegendModelV2::data( const QModelIndex& index, int role ) const
569 {
570  // handle custom layer node labels
571  if ( QgsLayerTreeNode* node = index2node( index ) )
572  {
573  if ( QgsLayerTree::isLayer( node ) && ( role == Qt::DisplayRole || role == Qt::EditRole ) && !node->customProperty( "legend/title-label" ).isNull() )
574  {
575  QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node );
576  QString name = node->customProperty( "legend/title-label" ).toString();
577  if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toInt() && role == Qt::DisplayRole )
578  {
579  QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( nodeLayer->layer() );
580  if ( vlayer && vlayer->pendingFeatureCount() >= 0 )
581  name += QString( " [%1]" ).arg( vlayer->pendingFeatureCount() );
582  }
583  return name;
584  }
585  }
586 
587  return QgsLayerTreeModel::data( index, role );
588 }
589 
590 Qt::ItemFlags QgsLegendModelV2::flags( const QModelIndex& index ) const
591 {
592  // make the legend nodes selectable even if they are not by default
593  if ( index2legendNode( index ) )
594  return QgsLayerTreeModel::flags( index ) | Qt::ItemIsSelectable;
595 
596  return QgsLayerTreeModel::flags( index );
597 }
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.
void setLegendSize(QSizeF s)
Set the preferred resulting legend size.
QgsComposerLegend(QgsComposition *composition)
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:48
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.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
void readXML(const QDomElement &elem, const QDomDocument &doc)
double mapUnitsToMM() const
Returns the conversion factor map units -> mm.
QgsComposerLegendStyle & rstyle(QgsComposerLegendStyle::Style s)
Returns reference to modifiable style.
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.
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)
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
bool writeXML(QDomElement &elem, QDomDocument &doc) const
stores state in Dom node
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.
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.
double columnSpace() const
double columnSpace() const
Flags flags() const
Return OR-ed combination of model flags.
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)
void updateLegend()
Updates the model and all legend entries.
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)
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 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
The QgsLayerTreeModel class is model implementation for Qt item views framework.
QVariant data(const QModelIndex &index, int role) const
void setLayerSet(const QStringList &layerIds, double scaleDenominator=-1, QString rule="")
int splitLayer() const
void setSymbolHeight(double h)
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
int printResolution() 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.
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Return legend node for given index.
void setMapScale(double scale)
void setComposerMap(const QgsComposerMap *map)
void synchronizeWithModel()
Data changed.
void setStyleFont(QgsComposerLegendStyle::Style s, const QFont &f)
Set style font.
QString wrapChar() const
int pendingFeatureCount()
returns feature count after commit
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
void setDpi(int dpi)
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
void setFontColor(const QColor &c)
virtual QString displayName() const
Get item display name.
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.
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.
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style)
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.
QgsRectangle * currentMapExtent()
Returns a pointer to the current map extent, which is either the original user specified extent or th...
virtual void writeXML(QDomElement &parentElement)
Write group (tree) as XML element and add it to the given parent element...
double symbolWidth() const
virtual bool isRemoved() const
Returns whether this item has been removed from the composition.
void setWmsLegendWidth(double w)
void writeXML(QString name, QDomElement &elem, QDomDocument &doc) const
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
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)
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
void setUseAdvancedEffects(bool use)
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
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.
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.
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:362
void setExtent(const QgsRectangle &rect)
Set coordinates of the rectangle which should be rendered.
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...
int equalColumnWidth() const
void setEqualColumnWidth(bool s)
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style)
QString title() const
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
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 paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget)
Reimplementation of QCanvasItem::paint.
QSizeF symbolSize() const
QSizeF wmsLegendSize() const
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Return layer tree node for given index.
void setFontColor(const QColor &c)
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:198
Represents a vector layer which manages a vector based data sets.
double size
Definition: qgssvgcache.cpp:77
bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
sets state from Dom document
void setBoxSpace(double s)
Allow reordering with drag'n'drop.
double symbolHeight() const
int equalColumnWidth() const
static void _readOldLegendGroup(QDomElement &elem, QgsLayerTreeGroup *parentGroup)
void setSplitLayer(bool s)
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:203
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.
QgsMapSettings mapSettings(const QgsRectangle &extent, const QSizeF &size, int dpi) const
Return map settings that would be used for drawing of the map.
#define tr(sourceText)
void setFont(const QFont &font)
QString id() const
Get item's id (which is not necessarly unique)