QGIS API Documentation  2.17.0-Master (06698cd)
qgscomposerlegenditem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerlegenditem.cpp - description
3  -------------------------
4  begin : May 2010
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole 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 
18 #include "qgscomposerlegendstyle.h"
19 #include "qgscomposerlegenditem.h"
20 #include "qgscomposerlegend.h"
21 #include "qgsmaplayerregistry.h"
22 #include "qgsrasterlayer.h"
23 #include "qgsrendererv2.h"
24 #include "qgssymbolv2.h"
25 #include "qgssymbollayerv2utils.h"
26 #include "qgsvectorlayer.h"
27 #include "qgsapplication.h"
28 #include <QDomDocument>
29 #include <QDomElement>
30 
32  , mStyle( s )
33 {
34 }
35 
37  , mStyle( s )
38 {
39 }
40 
42  , mStyle( s )
43 {
44 }
45 
47 {
48 }
49 
51 {
52  int numRows = rowCount();
53  QgsComposerLegendItem* currentItem = nullptr;
54  for ( int i = 0; i < numRows; ++i )
55  {
56  currentItem = dynamic_cast<QgsComposerLegendItem*>( child( i, 0 ) );
57  if ( currentItem )
58  {
59  currentItem->writeXML( elem, doc );
60  }
61  }
62 }
63 
64 
66 
67 
69 {
70 }
71 
73 {
74 }
75 
77 {
78 }
79 
81 {
82  delete mSymbolV2;
83 }
84 
86 {
88  *cloneItem = *this;
89  if ( mSymbolV2 )
90  {
91  cloneItem->setSymbolV2( mSymbolV2->clone() );
92  }
93  return cloneItem;
94 }
95 
97 {
98  QDomElement vectorClassElem = doc.createElement( "VectorClassificationItemNg" );
99  if ( mSymbolV2 )
100  {
101  QgsSymbolV2Map saveSymbolMap;
102  saveSymbolMap.insert( "classificationSymbol", mSymbolV2 );
103  QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( saveSymbolMap, "symbols", doc );
104  vectorClassElem.appendChild( symbolsElem );
105  }
106  vectorClassElem.setAttribute( "text", text() );
107  vectorClassElem.setAttribute( "userText", userText() );
108  elem.appendChild( vectorClassElem );
109 }
110 
111 void QgsComposerSymbolV2Item::readXML( const QDomElement& itemElem, bool xServerAvailable )
112 {
113  if ( itemElem.isNull() )
114  {
115  return;
116  }
117 
118  setText( itemElem.attribute( "text", "" ) );
119  setUserText( itemElem.attribute( "userText", "" ) );
120  QDomElement symbolsElem = itemElem.firstChildElement( "symbols" );
121  if ( !symbolsElem.isNull() )
122  {
123  QgsSymbolV2Map loadSymbolMap = QgsSymbolLayerV2Utils::loadSymbols( symbolsElem );
124  //we assume there is only one symbol in the map...
125  QgsSymbolV2Map::iterator mapIt = loadSymbolMap.begin();
126  if ( mapIt != loadSymbolMap.end() )
127  {
128  QgsSymbolV2* symbolNg = mapIt.value();
129  if ( symbolNg )
130  {
131  setSymbolV2( symbolNg );
132  if ( xServerAvailable )
133  {
134  setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolNg, QSize( 30, 30 ) ) );
135  }
136  }
137  }
138  }
139 }
140 
142 {
143  delete mSymbolV2;
144  mSymbolV2 = s;
145 }
146 
148 
150 {
151 }
152 
154 {
155 }
156 
158 {
159 }
160 
162 {
163 }
164 
166 {
168  *cloneItem = *this;
169  cloneItem->setLayerID( mLayerID );
170  return cloneItem;
171 }
172 
174 {
175  QDomElement rasterClassElem = doc.createElement( "RasterClassificationItem" );
176  rasterClassElem.setAttribute( "layerId", mLayerID );
177  rasterClassElem.setAttribute( "text", text() );
178  rasterClassElem.setAttribute( "userText", userText() );
179  rasterClassElem.setAttribute( "color", mColor.name() );
180  elem.appendChild( rasterClassElem );
181 }
182 
183 void QgsComposerRasterSymbolItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
184 {
185  if ( itemElem.isNull() )
186  {
187  return;
188  }
189  setText( itemElem.attribute( "text", "" ) );
190  setUserText( itemElem.attribute( "userText", "" ) );
191  setLayerID( itemElem.attribute( "layerId", "" ) );
192  setColor( QColor( itemElem.attribute( "color" ) ) );
193 
194  if ( xServerAvailable )
195  {
196  QPixmap itemPixmap( 20, 20 );
197  itemPixmap.fill( mColor );
198  setIcon( QIcon( itemPixmap ) );
199  }
200 }
201 
203 
205  , mShowFeatureCount( false )
206 {
207 }
208 
210  , mShowFeatureCount( false )
211 {
212 }
213 
215 {
216 }
217 
219 {
220  QgsComposerLayerItem* cloneItem = new QgsComposerLayerItem();
221  *cloneItem = *this;
222  cloneItem->setLayerID( mLayerID );
223  return cloneItem;
224 }
225 
227 {
228  QDomElement layerItemElem = doc.createElement( "LayerItem" );
229  layerItemElem.setAttribute( "layerId", mLayerID );
230  layerItemElem.setAttribute( "text", text() );
231  layerItemElem.setAttribute( "userText", userText() );
232  layerItemElem.setAttribute( "showFeatureCount", showFeatureCount() );
233  layerItemElem.setAttribute( "style", QgsComposerLegendStyle::styleName( mStyle ) );
234  writeXMLChildren( layerItemElem, doc );
235  elem.appendChild( layerItemElem );
236 }
237 
238 void QgsComposerLayerItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
239 {
240  if ( itemElem.isNull() )
241  {
242  return;
243  }
244  setText( itemElem.attribute( "text", "" ) );
245  setUserText( itemElem.attribute( "userText", "" ) );
246  setLayerID( itemElem.attribute( "layerId", "" ) );
247  setShowFeatureCount( itemElem.attribute( "showFeatureCount", "" ) == "1" ? true : false );
248  setStyle( QgsComposerLegendStyle::styleFromName( itemElem.attribute( "style", "subgroup" ) ) );
249 
250  //now call readXML for all the child items
251  QDomNodeList childList = itemElem.childNodes();
252  QDomNode currentNode;
253  QDomElement currentElem;
254  QgsComposerLegendItem* currentChildItem = nullptr;
255 
256  int nChildItems = childList.count();
257  for ( int i = 0; i < nChildItems; ++i )
258  {
259  currentNode = childList.at( i );
260  if ( !currentNode.isElement() )
261  {
262  continue;
263  }
264 
265  currentElem = currentNode.toElement();
266  QString elemTag = currentElem.tagName();
267  if ( elemTag == "VectorClassificationItem" )
268  {
269  continue; // legacy - unsupported
270  }
271  else if ( elemTag == "VectorClassificationItemNg" )
272  {
273  currentChildItem = new QgsComposerSymbolV2Item();
274  }
275  else if ( elemTag == "RasterClassificationItem" )
276  {
277  currentChildItem = new QgsComposerRasterSymbolItem();
278  }
279  else
280  {
281  continue; //unsupported child type
282  }
283  currentChildItem->readXML( currentElem, xServerAvailable );
284  appendRow( currentChildItem );
285  }
286 }
287 
288 void QgsComposerLayerItem::setDefaultStyle( double scaleDenominator, const QString& rule )
289 {
290  // set default style according to number of symbols
291  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerID() ) );
292  if ( vLayer )
293  {
294  QgsFeatureRendererV2* renderer = vLayer->rendererV2();
295  if ( renderer )
296  {
297  QPair<QString, QgsSymbolV2*> symbolItem = renderer->legendSymbolItems( scaleDenominator, rule ).value( 0 );
298  if ( renderer->legendSymbolItems( scaleDenominator, rule ).size() > 1 || !symbolItem.first.isEmpty() )
299  {
301  }
302  else
303  {
304  // Hide title by default for single symbol
306  }
307  }
308  }
309 }
310 
312 
314 {
315 }
316 
318 {
319 }
320 
322 {
323 }
324 
326 {
327  QgsComposerGroupItem* cloneItem = new QgsComposerGroupItem();
328  *cloneItem = *this;
329  return cloneItem;
330 }
331 
333 {
334  QDomElement layerGroupElem = doc.createElement( "GroupItem" );
335  // text is always user text, but for forward compatibility for now write both
336  layerGroupElem.setAttribute( "text", text() );
337  layerGroupElem.setAttribute( "userText", userText() );
338  layerGroupElem.setAttribute( "style", QgsComposerLegendStyle::styleName( mStyle ) );
339  writeXMLChildren( layerGroupElem, doc );
340  elem.appendChild( layerGroupElem );
341 }
342 
343 void QgsComposerGroupItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
344 {
345  if ( itemElem.isNull() )
346  {
347  return;
348  }
349  // text is always user text but for backward compatibility we read also text
350  QString userText = itemElem.attribute( "userText", "" );
351  if ( userText.isEmpty() )
352  {
353  userText = itemElem.attribute( "text", "" );
354  }
355  setText( userText );
356  setUserText( userText );
357 
358  setStyle( QgsComposerLegendStyle::styleFromName( itemElem.attribute( "style", "group" ) ) );
359 
360  //now call readXML for all the child items
361  QDomNodeList childList = itemElem.childNodes();
362  QDomNode currentNode;
363  QDomElement currentElem;
364  QgsComposerLegendItem* currentChildItem = nullptr;
365 
366  int nChildItems = childList.count();
367  for ( int i = 0; i < nChildItems; ++i )
368  {
369  currentNode = childList.at( i );
370  if ( !currentNode.isElement() )
371  {
372  continue;
373  }
374 
375  currentElem = currentNode.toElement();
376  QString elemTag = currentElem.tagName();
377 
378  if ( elemTag == "GroupItem" )
379  {
380  currentChildItem = new QgsComposerGroupItem();
381  }
382  else if ( elemTag == "LayerItem" )
383  {
384  currentChildItem = new QgsComposerLayerItem();
385  }
386  else
387  {
388  continue; //unsupported child item type
389  }
390  currentChildItem->readXML( currentElem, xServerAvailable );
391 
392  QList<QStandardItem *> itemsList;
393  itemsList << currentChildItem << new QgsComposerStyleItem( currentChildItem );
394  appendRow( itemsList );
395  }
396 }
397 
399 {
400 }
401 
403 {
404  setData( QgsComposerLegendStyle::styleLabel( item->style() ), Qt::DisplayRole );
405 }
406 
408 {
409 }
static QDomElement saveSymbols(QgsSymbolV2Map &symbols, const QString &tagName, QDomDocument &doc)
virtual void readXML(const QDomElement &itemElem, bool xServerAvailable=true)=0
Read item content from xml.
static Style styleFromName(const QString &styleName)
Get style from name, used in project file.
static QgsSymbolV2Map loadSymbols(QDomElement &element)
void setIcon(const QIcon &icon)
void setLayerID(const QString &id)
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
virtual QStandardItem * clone() const override
virtual void writeXML(QDomElement &elem, QDomDocument &doc) const override
virtual void writeXML(QDomElement &elem, QDomDocument &doc) const override
QDomNode appendChild(const QDomNode &newChild)
void fill(const QColor &color)
QString name() const
QString attribute(const QString &name, const QString &defValue) const
virtual void readXML(const QDomElement &itemElem, bool xServerAvailable=true) override
Read item content from xml.
virtual QStandardItem * clone() const override
virtual QgsSymbolV2 * clone() const =0
bool isElement() const
virtual QStandardItem * clone() const override
virtual QString userText() const
QDomNodeList childNodes() const
QString text() const
void setLayerID(const QString &id)
virtual void writeXML(QDomElement &elem, QDomDocument &doc) const override
int size() const
virtual void setData(const QVariant &value, int role)
void setStyle(QgsComposerLegendStyle::Style style)
T value(int i) const
QDomElement toElement() const
static QString styleName(Style s)
Get name for style, used in project file.
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
int count() const
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="")
return a list of item text / symbol
void setAttribute(const QString &name, const QString &value)
void appendRow(const QList< QStandardItem * > &items)
bool isEmpty() const
virtual void writeXML(QDomElement &elem, QDomDocument &doc) const =0
static QString styleLabel(Style s)
Get style label, translated, used in UI.
iterator end()
T & value() const
QgsComposerLegendStyle::Style mStyle
void setSymbolV2(QgsSymbolV2 *s)
Set symbol (takes ownership)
iterator begin()
QStandardItem * child(int row, int column) const
QgsComposerLegendItem(QgsComposerLegendStyle::Style s=QgsComposerLegendStyle::Undefined)
virtual void readXML(const QDomElement &itemElem, bool xServerAvailable=true) override
Read item content from xml.
Abstract base class for the legend item types.
bool isNull() const
void setText(const QString &text)
Composer legend components style.
virtual QStandardItem * clone() const override
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QDomElement firstChildElement(const QString &tagName) const
QgsComposerLegendStyle::Style style() const
void writeXMLChildren(QDomElement &elem, QDomDocument &doc) const
void setShowFeatureCount(bool show)
virtual void writeXML(QDomElement &elem, QDomDocument &doc) const override
virtual void readXML(const QDomElement &itemElem, bool xServerAvailable=true) override
Read item content from xml.
int rowCount() const
virtual void readXML(const QDomElement &itemElem, bool xServerAvailable=true) override
Read item content from xml.
iterator insert(const Key &key, const T &value)
QString tagName() const
QDomElement createElement(const QString &tagName)
Represents a vector layer which manages a vector based data sets.
void setDefaultStyle(double scaleDenominator=-1, const QString &rule="")
virtual void setUserText(const QString &text)
QIcon icon() const
QDomNode at(int index) const