QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
36 QgsComposerLegendItem::QgsComposerLegendItem( const QString& text, QgsComposerLegendStyle::Style s ): QStandardItem( text )
37  , mStyle( s )
38 {
39 }
40 
41 QgsComposerLegendItem::QgsComposerLegendItem( const QIcon& icon, const QString& text, QgsComposerLegendStyle::Style s ): QStandardItem( icon, text )
42  , mStyle( s )
43 {
44 }
45 
47 {
48 }
49 
50 void QgsComposerLegendItem::writeXMLChildren( QDomElement& elem, QDomDocument& doc ) const
51 {
52  int numRows = rowCount();
53  QgsComposerLegendItem* currentItem = 0;
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 
76 QgsComposerSymbolV2Item::QgsComposerSymbolV2Item( const QIcon& icon, const QString& text ): QgsComposerLegendItem( icon, text, QgsComposerLegendStyle::Symbol ), mSymbolV2( 0 )
77 {
78 }
79 
81 {
82  delete mSymbolV2;
83 }
84 
85 QStandardItem* QgsComposerSymbolV2Item::clone() const
86 {
88  *cloneItem = *this;
89  if ( mSymbolV2 )
90  {
91  cloneItem->setSymbolV2( mSymbolV2->clone() );
92  }
93  return cloneItem;
94 }
95 
96 void QgsComposerSymbolV2Item::writeXML( QDomElement& elem, QDomDocument& doc ) const
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 
157 QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem( const QIcon& icon, const QString& text ): QgsComposerLegendItem( icon, text, QgsComposerLegendStyle::Symbol )
158 {
159 }
160 
162 {
163 }
164 
166 {
168  *cloneItem = *this;
169  cloneItem->setLayerID( mLayerID );
170  return cloneItem;
171 }
172 
173 void QgsComposerRasterSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
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 
218 QStandardItem* QgsComposerLayerItem::clone() const
219 {
220  QgsComposerLayerItem* cloneItem = new QgsComposerLayerItem();
221  *cloneItem = *this;
222  cloneItem->setLayerID( mLayerID );
223  return cloneItem;
224 }
225 
226 void QgsComposerLayerItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
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 = 0;
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, 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 
325 QStandardItem* QgsComposerGroupItem::clone() const
326 {
327  QgsComposerGroupItem* cloneItem = new QgsComposerGroupItem();
328  *cloneItem = *this;
329  return cloneItem;
330 }
331 
332 void QgsComposerGroupItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
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 = 0;
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 }