QGIS API Documentation  2.2.0-Valmiera
 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 #include "qgssymbolv2.h"
68 
70 {
71 }
72 
74 {
75 }
76 
77 QgsComposerSymbolV2Item::QgsComposerSymbolV2Item( const QIcon& icon, const QString& text ): QgsComposerLegendItem( icon, text, QgsComposerLegendStyle::Symbol ), mSymbolV2( 0 )
78 {
79 }
80 
82 {
83  delete mSymbolV2;
84 }
85 
86 QStandardItem* QgsComposerSymbolV2Item::clone() const
87 {
89  *cloneItem = *this;
90  if ( mSymbolV2 )
91  {
92  cloneItem->setSymbolV2( mSymbolV2->clone() );
93  }
94  return cloneItem;
95 }
96 
97 void QgsComposerSymbolV2Item::writeXML( QDomElement& elem, QDomDocument& doc ) const
98 {
99  QDomElement vectorClassElem = doc.createElement( "VectorClassificationItemNg" );
100  if ( mSymbolV2 )
101  {
102  QgsSymbolV2Map saveSymbolMap;
103  saveSymbolMap.insert( "classificationSymbol", mSymbolV2 );
104  QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( saveSymbolMap, "symbols", doc );
105  vectorClassElem.appendChild( symbolsElem );
106  }
107  vectorClassElem.setAttribute( "text", text() );
108  vectorClassElem.setAttribute( "userText", userText() );
109  elem.appendChild( vectorClassElem );
110 }
111 
112 void QgsComposerSymbolV2Item::readXML( const QDomElement& itemElem, bool xServerAvailable )
113 {
114  if ( itemElem.isNull() )
115  {
116  return;
117  }
118 
119  setText( itemElem.attribute( "text", "" ) );
120  setUserText( itemElem.attribute( "userText", "" ) );
121  QDomElement symbolsElem = itemElem.firstChildElement( "symbols" );
122  if ( !symbolsElem.isNull() )
123  {
124  QgsSymbolV2Map loadSymbolMap = QgsSymbolLayerV2Utils::loadSymbols( symbolsElem );
125  //we assume there is only one symbol in the map...
126  QgsSymbolV2Map::iterator mapIt = loadSymbolMap.begin();
127  if ( mapIt != loadSymbolMap.end() )
128  {
129  QgsSymbolV2* symbolNg = mapIt.value();
130  if ( symbolNg )
131  {
132  setSymbolV2( symbolNg );
133  if ( xServerAvailable )
134  {
135  setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolNg, QSize( 30, 30 ) ) );
136  }
137  }
138  }
139  }
140 }
141 
143 {
144  delete mSymbolV2;
145  mSymbolV2 = s;
146 }
147 
149 
151 {
152 }
153 
155 {
156 }
157 
159 {
160 }
161 
163 {
164 }
165 
167 {
169  *cloneItem = *this;
170  cloneItem->setLayerID( mLayerID );
171  return cloneItem;
172 }
173 
174 void QgsComposerRasterSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
175 {
176  QDomElement rasterClassElem = doc.createElement( "RasterClassificationItem" );
177  rasterClassElem.setAttribute( "layerId", mLayerID );
178  rasterClassElem.setAttribute( "text", text() );
179  rasterClassElem.setAttribute( "userText", userText() );
180  rasterClassElem.setAttribute( "color", mColor.name() );
181  elem.appendChild( rasterClassElem );
182 }
183 
184 void QgsComposerRasterSymbolItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
185 {
186  if ( itemElem.isNull() )
187  {
188  return;
189  }
190  setText( itemElem.attribute( "text", "" ) );
191  setUserText( itemElem.attribute( "userText", "" ) );
192  setLayerID( itemElem.attribute( "layerId", "" ) );
193  setColor( QColor( itemElem.attribute( "color" ) ) );
194 
195  if ( xServerAvailable )
196  {
197  QPixmap itemPixmap( 20, 20 );
198  itemPixmap.fill( mColor );
199  setIcon( QIcon( itemPixmap ) );
200  }
201 }
202 
204 
206  , mShowFeatureCount( false )
207 {
208 }
209 
211  , mShowFeatureCount( false )
212 {
213 }
214 
216 {
217 }
218 
219 QStandardItem* QgsComposerLayerItem::clone() const
220 {
221  QgsComposerLayerItem* cloneItem = new QgsComposerLayerItem();
222  *cloneItem = *this;
223  cloneItem->setLayerID( mLayerID );
224  return cloneItem;
225 }
226 
227 void QgsComposerLayerItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
228 {
229  QDomElement layerItemElem = doc.createElement( "LayerItem" );
230  layerItemElem.setAttribute( "layerId", mLayerID );
231  layerItemElem.setAttribute( "text", text() );
232  layerItemElem.setAttribute( "userText", userText() );
233  layerItemElem.setAttribute( "showFeatureCount", showFeatureCount() );
234  layerItemElem.setAttribute( "style", QgsComposerLegendStyle::styleName( mStyle ) );
235  writeXMLChildren( layerItemElem, doc );
236  elem.appendChild( layerItemElem );
237 }
238 
239 void QgsComposerLayerItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
240 {
241  if ( itemElem.isNull() )
242  {
243  return;
244  }
245  setText( itemElem.attribute( "text", "" ) );
246  setUserText( itemElem.attribute( "userText", "" ) );
247  setLayerID( itemElem.attribute( "layerId", "" ) );
248  setShowFeatureCount( itemElem.attribute( "showFeatureCount", "" ) == "1" ? true : false );
249  setStyle( QgsComposerLegendStyle::styleFromName( itemElem.attribute( "style", "subgroup" ) ) );
250 
251  //now call readXML for all the child items
252  QDomNodeList childList = itemElem.childNodes();
253  QDomNode currentNode;
254  QDomElement currentElem;
255  QgsComposerLegendItem* currentChildItem = 0;
256 
257  int nChildItems = childList.count();
258  for ( int i = 0; i < nChildItems; ++i )
259  {
260  currentNode = childList.at( i );
261  if ( !currentNode.isElement() )
262  {
263  continue;
264  }
265 
266  currentElem = currentNode.toElement();
267  QString elemTag = currentElem.tagName();
268  if ( elemTag == "VectorClassificationItem" )
269  {
270  continue; // legacy - unsupported
271  }
272  else if ( elemTag == "VectorClassificationItemNg" )
273  {
274  currentChildItem = new QgsComposerSymbolV2Item();
275  }
276  else if ( elemTag == "RasterClassificationItem" )
277  {
278  currentChildItem = new QgsComposerRasterSymbolItem();
279  }
280  else
281  {
282  continue; //unsupported child type
283  }
284  currentChildItem->readXML( currentElem, xServerAvailable );
285  appendRow( currentChildItem );
286  }
287 }
288 
289 void QgsComposerLayerItem::setDefaultStyle( double scaleDenominator, QString rule )
290 {
291  // set default style according to number of symbols
292  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerID() ) );
293  if ( vLayer )
294  {
295  QgsFeatureRendererV2* renderer = vLayer->rendererV2();
296  if ( renderer )
297  {
298  QPair<QString, QgsSymbolV2*> symbolItem = renderer->legendSymbolItems( scaleDenominator, rule ).value( 0 );
299  if ( renderer->legendSymbolItems( scaleDenominator, rule ).size() > 1 || !symbolItem.first.isEmpty() )
300  {
302  }
303  else
304  {
305  // Hide title by default for single symbol
307  }
308  }
309  }
310 }
311 
313 
315 {
316 }
317 
319 {
320 }
321 
323 {
324 }
325 
326 QStandardItem* QgsComposerGroupItem::clone() const
327 {
328  QgsComposerGroupItem* cloneItem = new QgsComposerGroupItem();
329  *cloneItem = *this;
330  return cloneItem;
331 }
332 
333 void QgsComposerGroupItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
334 {
335  QDomElement layerGroupElem = doc.createElement( "GroupItem" );
336  // text is always user text, but for forward compatibility for now write both
337  layerGroupElem.setAttribute( "text", text() );
338  layerGroupElem.setAttribute( "userText", userText() );
339  layerGroupElem.setAttribute( "style", QgsComposerLegendStyle::styleName( mStyle ) );
340  writeXMLChildren( layerGroupElem, doc );
341  elem.appendChild( layerGroupElem );
342 }
343 
344 void QgsComposerGroupItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
345 {
346  if ( itemElem.isNull() )
347  {
348  return;
349  }
350  // text is always user text but for backward compatibility we read also text
351  QString userText = itemElem.attribute( "userText", "" );
352  if ( userText.isEmpty() )
353  {
354  userText = itemElem.attribute( "text", "" );
355  }
356  setText( userText );
357  setUserText( userText );
358 
359  setStyle( QgsComposerLegendStyle::styleFromName( itemElem.attribute( "style", "group" ) ) );
360 
361  //now call readXML for all the child items
362  QDomNodeList childList = itemElem.childNodes();
363  QDomNode currentNode;
364  QDomElement currentElem;
365  QgsComposerLegendItem* currentChildItem = 0;
366 
367  int nChildItems = childList.count();
368  for ( int i = 0; i < nChildItems; ++i )
369  {
370  currentNode = childList.at( i );
371  if ( !currentNode.isElement() )
372  {
373  continue;
374  }
375 
376  currentElem = currentNode.toElement();
377  QString elemTag = currentElem.tagName();
378 
379  if ( elemTag == "GroupItem" )
380  {
381  currentChildItem = new QgsComposerGroupItem();
382  }
383  else if ( elemTag == "LayerItem" )
384  {
385  currentChildItem = new QgsComposerLayerItem();
386  }
387  else
388  {
389  continue; //unsupported child item type
390  }
391  currentChildItem->readXML( currentElem, xServerAvailable );
392 
393  QList<QStandardItem *> itemsList;
394  itemsList << currentChildItem << new QgsComposerStyleItem( currentChildItem );
395  appendRow( itemsList );
396  }
397 }
398 
400 {
401 }
402 
404 {
405  setData( QgsComposerLegendStyle::styleLabel( item->style() ) , Qt::DisplayRole );
406 }
407 
409 {
410 }