Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 "qgscomposerlegenditem.h"
19 #include "qgsmaplayerregistry.h"
20 #include "qgsrasterlayer.h"
21 #include "qgssymbol.h"
22 #include "qgssymbolv2.h"
23 #include "qgssymbollayerv2utils.h"
24 #include "qgsvectorlayer.h"
25 #include "qgsapplication.h"
26 #include <QDomDocument>
27 #include <QDomElement>
28 
30 {
31 }
32 
33 QgsComposerLegendItem::QgsComposerLegendItem( const QString& text ): QStandardItem( text )
34 {
35 }
36 
37 QgsComposerLegendItem::QgsComposerLegendItem( const QIcon& icon, const QString& text ): QStandardItem( icon, text )
38 {
39 }
40 
42 {
43 }
44 
45 void QgsComposerLegendItem::writeXMLChildren( QDomElement& elem, QDomDocument& doc ) const
46 {
47  int numRows = rowCount();
48  QgsComposerLegendItem* currentItem = 0;
49  for ( int i = 0; i < numRows; ++i )
50  {
51  currentItem = dynamic_cast<QgsComposerLegendItem*>( child( i, 0 ) );
52  if ( currentItem )
53  {
54  currentItem->writeXML( elem, doc );
55  }
56  }
57 }
58 
60 
62 {
63 }
64 
65 QgsComposerSymbolItem::QgsComposerSymbolItem( const QString& text ): QgsComposerLegendItem( text ), mSymbol( 0 )
66 {
67 }
68 
69 QgsComposerSymbolItem::QgsComposerSymbolItem( const QIcon& icon, const QString& text ): QgsComposerLegendItem( icon, text ), mSymbol( 0 )
70 {
71 }
72 
74 {
75  delete mSymbol;
76 }
77 
79 {
80  delete mSymbol;
81  mSymbol = s;
82 }
83 
84 QStandardItem* QgsComposerSymbolItem::clone() const
85 {
87  *cloneItem = *this;
88  if ( mSymbol )
89  {
90  cloneItem->setSymbol( new QgsSymbol( *mSymbol ) );
91  }
92  return cloneItem;
93 }
94 
95 void QgsComposerSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
96 {
97  QDomElement vectorClassElem = doc.createElement( "VectorClassificationItem" );
98  if ( mSymbol )
99  {
100  mSymbol->writeXML( vectorClassElem, doc, 0 );
101  }
102  vectorClassElem.setAttribute( "text", text() );
103  vectorClassElem.setAttribute( "layerId", mLayerID );
104 
105  elem.appendChild( vectorClassElem );
106 }
107 
108 void QgsComposerSymbolItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
109 {
110  if ( itemElem.isNull() )
111  {
112  return;
113  }
114  setText( itemElem.attribute( "text", "" ) );
115  setLayerID( itemElem.attribute( "layerId", "" ) );
116 
118  if ( vLayer )
119  {
120  QDomElement symbolElem = itemElem.firstChildElement( "symbol" );
121  if ( !symbolElem.isNull() )
122  {
123  QgsSymbol* symbol = new QgsSymbol( vLayer->geometryType() );
124  symbol->readXML( symbolElem, vLayer );
125  setSymbol( symbol );
126  if ( !xServerAvailable ) //don't read icon without GUI
127  {
128  return;
129  }
130 
131  //add icon
132  switch ( symbol->type() )
133  {
134  case QGis::Point:
135  setIcon( QIcon( QPixmap::fromImage( symbol->getPointSymbolAsImage() ) ) );
136  break;
137  case QGis::Line:
138  setIcon( QIcon( QPixmap::fromImage( symbol->getLineSymbolAsImage() ) ) );
139  break;
140  case QGis::Polygon:
141  setIcon( QIcon( QPixmap::fromImage( symbol->getPolygonSymbolAsImage() ) ) );
142  break;
143  case QGis::NoGeometry:
144  setIcon( QIcon( QgsApplication::activeThemePath() + "/mIconTableLayer.png" ) );
145  break;
147  // should not occur
148  break;
149  }
150  }
151  }
152 }
153 
155 
156 #include "qgssymbolv2.h"
157 
159 {
160 }
161 
162 QgsComposerSymbolV2Item::QgsComposerSymbolV2Item( const QString& text ): QgsComposerLegendItem( text ), mSymbolV2( 0 )
163 {
164 }
165 
166 QgsComposerSymbolV2Item::QgsComposerSymbolV2Item( const QIcon& icon, const QString& text ): QgsComposerLegendItem( icon, text ), mSymbolV2( 0 )
167 {
168 }
169 
171 {
172  delete mSymbolV2;
173 }
174 
175 QStandardItem* QgsComposerSymbolV2Item::clone() const
176 {
178  *cloneItem = *this;
179  if ( mSymbolV2 )
180  {
181  cloneItem->setSymbolV2( mSymbolV2->clone() );
182  }
183  return cloneItem;
184 }
185 
186 void QgsComposerSymbolV2Item::writeXML( QDomElement& elem, QDomDocument& doc ) const
187 {
188  QDomElement vectorClassElem = doc.createElement( "VectorClassificationItemNg" );
189  if ( mSymbolV2 )
190  {
191  QgsSymbolV2Map saveSymbolMap;
192  saveSymbolMap.insert( "classificationSymbol", mSymbolV2 );
193  QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( saveSymbolMap, "symbols", doc );
194  vectorClassElem.appendChild( symbolsElem );
195  }
196  vectorClassElem.setAttribute( "text", text() );
197  elem.appendChild( vectorClassElem );
198 }
199 
200 void QgsComposerSymbolV2Item::readXML( const QDomElement& itemElem, bool xServerAvailable )
201 {
202  if ( itemElem.isNull() )
203  {
204  return;
205  }
206 
207  setText( itemElem.attribute( "text", "" ) );
208  QDomElement symbolsElem = itemElem.firstChildElement( "symbols" );
209  if ( !symbolsElem.isNull() )
210  {
211  QgsSymbolV2Map loadSymbolMap = QgsSymbolLayerV2Utils::loadSymbols( symbolsElem );
212  //we assume there is only one symbol in the map...
213  QgsSymbolV2Map::iterator mapIt = loadSymbolMap.begin();
214  if ( mapIt != loadSymbolMap.end() )
215  {
216  QgsSymbolV2* symbolNg = mapIt.value();
217  if ( symbolNg )
218  {
219  setSymbolV2( symbolNg );
220  if ( xServerAvailable )
221  {
222  setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolNg, QSize( 30, 30 ) ) );
223  }
224  }
225  }
226  }
227 }
228 
230 {
231  delete mSymbolV2;
232  mSymbolV2 = s;
233 }
234 
236 
238 {
239 }
240 
242 {
243 }
244 
245 QgsComposerRasterSymbolItem::QgsComposerRasterSymbolItem( const QIcon& icon, const QString& text ): QgsComposerLegendItem( icon, text )
246 {
247 }
248 
250 {
251 }
252 
254 {
256  *cloneItem = *this;
257  cloneItem->setLayerID( mLayerID );
258  return cloneItem;
259 }
260 
261 void QgsComposerRasterSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
262 {
263  QDomElement rasterClassElem = doc.createElement( "RasterClassificationItem" );
264  rasterClassElem.setAttribute( "layerId", mLayerID );
265  rasterClassElem.setAttribute( "text", text() );
266  rasterClassElem.setAttribute( "color", mColor.name() );
267  elem.appendChild( rasterClassElem );
268 }
269 
270 void QgsComposerRasterSymbolItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
271 {
272  if ( itemElem.isNull() )
273  {
274  return;
275  }
276  setText( itemElem.attribute( "text", "" ) );
277  setLayerID( itemElem.attribute( "layerId", "" ) );
278  setColor( QColor( itemElem.attribute( "color" ) ) );
279 
280  if ( xServerAvailable )
281  {
282  QPixmap itemPixmap( 20, 20 );
283  itemPixmap.fill( mColor );
284  setIcon( QIcon( itemPixmap ) );
285  }
286 }
287 
289 
291 {
292 }
293 
295 {
296 }
297 
299 {
300 }
301 
302 QStandardItem* QgsComposerLayerItem::clone() const
303 {
304  QgsComposerLayerItem* cloneItem = new QgsComposerLayerItem();
305  *cloneItem = *this;
306  cloneItem->setLayerID( mLayerID );
307  return cloneItem;
308 }
309 
310 void QgsComposerLayerItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
311 {
312  QDomElement layerItemElem = doc.createElement( "LayerItem" );
313  layerItemElem.setAttribute( "layerId", mLayerID );
314  layerItemElem.setAttribute( "text", text() );
315  writeXMLChildren( layerItemElem, doc );
316  elem.appendChild( layerItemElem );
317 }
318 
319 void QgsComposerLayerItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
320 {
321  if ( itemElem.isNull() )
322  {
323  return;
324  }
325  setText( itemElem.attribute( "text", "" ) );
326  setLayerID( itemElem.attribute( "layerId", "" ) );
327 
328  //now call readXML for all the child items
329  QDomNodeList childList = itemElem.childNodes();
330  QDomNode currentNode;
331  QDomElement currentElem;
332  QgsComposerLegendItem* currentChildItem = 0;
333 
334  int nChildItems = childList.count();
335  for ( int i = 0; i < nChildItems; ++i )
336  {
337  currentNode = childList.at( i );
338  if ( !currentNode.isElement() )
339  {
340  continue;
341  }
342 
343  currentElem = currentNode.toElement();
344  QString elemTag = currentElem.tagName();
345  if ( elemTag == "VectorClassificationItem" )
346  {
347  currentChildItem = new QgsComposerSymbolItem();
348  }
349  else if ( elemTag == "VectorClassificationItemNg" )
350  {
351  currentChildItem = new QgsComposerSymbolV2Item();
352  }
353  else if ( elemTag == "RasterClassificationItem" )
354  {
355  currentChildItem = new QgsComposerRasterSymbolItem();
356  }
357  else
358  {
359  continue; //unsupported child type
360  }
361  currentChildItem->readXML( currentElem, xServerAvailable );
362  appendRow( currentChildItem );
363  }
364 }
365 
367 
369 {
370 }
371 
373 {
374 }
375 
377 {
378 }
379 
380 QStandardItem* QgsComposerGroupItem::clone() const
381 {
382  QgsComposerGroupItem* cloneItem = new QgsComposerGroupItem();
383  *cloneItem = *this;
384  return cloneItem;
385 }
386 
387 void QgsComposerGroupItem::writeXML( QDomElement& elem, QDomDocument& doc ) const
388 {
389  QDomElement layerGroupElem = doc.createElement( "GroupItem" );
390  layerGroupElem.setAttribute( "text", text() );
391  writeXMLChildren( layerGroupElem, doc );
392  elem.appendChild( layerGroupElem );
393 }
394 
395 void QgsComposerGroupItem::readXML( const QDomElement& itemElem, bool xServerAvailable )
396 {
397  if ( itemElem.isNull() )
398  {
399  return;
400  }
401  setText( itemElem.attribute( "text", "" ) );
402 
403  //now call readXML for all the child items
404  QDomNodeList childList = itemElem.childNodes();
405  QDomNode currentNode;
406  QDomElement currentElem;
407  QgsComposerLegendItem* currentChildItem = 0;
408 
409  int nChildItems = childList.count();
410  for ( int i = 0; i < nChildItems; ++i )
411  {
412  currentNode = childList.at( i );
413  if ( !currentNode.isElement() )
414  {
415  continue;
416  }
417 
418  currentElem = currentNode.toElement();
419  QString elemTag = currentElem.tagName();
420 
421  if ( elemTag == "GroupItem" )
422  {
423  currentChildItem = new QgsComposerGroupItem();
424  }
425  else if ( elemTag == "LayerItem" )
426  {
427  currentChildItem = new QgsComposerLayerItem();
428  }
429  else
430  {
431  continue; //unsupported child item type
432  }
433  currentChildItem->readXML( currentElem, xServerAvailable );
434  appendRow( currentChildItem );
435  }
436 }