1616 ***************************************************************************/
1717
1818#include " qgscomposerlegenditem.h"
19+ #include " qgsmaplayerregistry.h"
1920#include " qgssymbol.h"
2021#include " qgssymbolv2.h"
2122#include " qgssymbollayerv2utils.h"
23+ #include " qgsvectorlayer.h"
2224#include < QDomDocument>
2325#include < QDomElement>
2426
@@ -79,7 +81,6 @@ void QgsComposerSymbolItem::setSymbol( QgsSymbol* s )
7981
8082QStandardItem* QgsComposerSymbolItem::clone () const
8183{
82- qWarning ( " QgsComposerSymbolItem::clone" );
8384 QgsComposerSymbolItem* cloneItem = new QgsComposerSymbolItem ();
8485 *cloneItem = *this ;
8586 if ( mSymbol )
@@ -97,12 +98,48 @@ void QgsComposerSymbolItem::writeXML( QDomElement& elem, QDomDocument& doc ) con
9798 mSymbol ->writeXML ( vectorClassElem, doc, 0 );
9899 }
99100 vectorClassElem.setAttribute ( " text" , text () );
101+ vectorClassElem.setAttribute ( " layerId" , mLayerID );
102+
100103 elem.appendChild ( vectorClassElem );
101104}
102105
103106void QgsComposerSymbolItem::readXML ( const QDomElement& itemElem )
104107{
105- // soon...
108+ if ( itemElem.isNull () )
109+ {
110+ return ;
111+ }
112+ setText ( itemElem.attribute ( " text" , " " ) );
113+ setLayerID ( itemElem.attribute ( " layerId" , " " ) );
114+
115+ QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance ()->mapLayer ( mLayerID ) );
116+ if ( vLayer )
117+ {
118+ QDomElement symbolElem = itemElem.firstChildElement ( " symbol" );
119+ if ( !symbolElem.isNull () )
120+ {
121+ QgsSymbol* symbol = new QgsSymbol ( vLayer->geometryType () );
122+ symbol->readXML ( symbolElem, vLayer );
123+ setSymbol ( symbol );
124+
125+ // add icon
126+ switch ( symbol->type () )
127+ {
128+ case QGis::Point:
129+ setIcon ( QIcon ( QPixmap::fromImage ( symbol->getPointSymbolAsImage () ) ) );
130+ break ;
131+ case QGis::Line:
132+ setIcon ( QIcon ( QPixmap::fromImage ( symbol->getLineSymbolAsImage () ) ) );
133+ break ;
134+ case QGis::Polygon:
135+ setIcon ( QIcon ( QPixmap::fromImage ( symbol->getPolygonSymbolAsImage () ) ) );
136+ break ;
137+ case QGis::UnknownGeometry:
138+ // should not occur
139+ break ;
140+ }
141+ }
142+ }
106143}
107144
108145// //////////////QgsComposerSymbolV2Item
@@ -153,7 +190,28 @@ void QgsComposerSymbolV2Item::writeXML( QDomElement& elem, QDomDocument& doc ) c
153190
154191void QgsComposerSymbolV2Item::readXML ( const QDomElement& itemElem )
155192{
156- // soon...
193+ if ( itemElem.isNull () )
194+ {
195+ return ;
196+ }
197+
198+ setText ( itemElem.attribute ( " text" , " " ) );
199+ QDomElement symbolsElem = itemElem.firstChildElement ( " symbols" );
200+ if ( !symbolsElem.isNull () )
201+ {
202+ QgsSymbolV2Map loadSymbolMap = QgsSymbolLayerV2Utils::loadSymbols ( symbolsElem );
203+ // we assume there is only one symbol in the map...
204+ QgsSymbolV2Map::iterator mapIt = loadSymbolMap.begin ();
205+ if ( mapIt != loadSymbolMap.end () )
206+ {
207+ QgsSymbolV2* symbolNg = mapIt.value ();
208+ if ( symbolNg )
209+ {
210+ setSymbolV2 ( symbolNg );
211+ setIcon ( QgsSymbolLayerV2Utils::symbolPreviewIcon ( symbolNg, QSize ( 30 , 30 ) ) );
212+ }
213+ }
214+ }
157215}
158216
159217void QgsComposerSymbolV2Item::setSymbolV2 ( QgsSymbolV2* s )
@@ -195,7 +253,45 @@ void QgsComposerLayerItem::writeXML( QDomElement& elem, QDomDocument& doc ) cons
195253
196254void QgsComposerLayerItem::readXML ( const QDomElement& itemElem )
197255{
198- // soon...
256+ if ( itemElem.isNull () )
257+ {
258+ return ;
259+ }
260+ setText ( itemElem.attribute ( " text" , " " ) );
261+ setLayerID ( itemElem.attribute ( " layerId" , " " ) );
262+
263+ // now call readXML for all the child items
264+ QDomNodeList childList = itemElem.childNodes ();
265+ QDomNode currentNode;
266+ QDomElement currentElem;
267+ QgsComposerLegendItem* currentChildItem = 0 ;
268+
269+ int nChildItems = childList.count ();
270+ for ( int i = 0 ; i < nChildItems; ++i )
271+ {
272+ currentNode = childList.at ( i );
273+ if ( !currentNode.isElement () )
274+ {
275+ continue ;
276+ }
277+
278+ currentElem = currentNode.toElement ();
279+ QString elemTag = currentElem.tagName ();
280+ if ( elemTag == " VectorClassificationItem" )
281+ {
282+ currentChildItem = new QgsComposerSymbolItem ();
283+ }
284+ else if ( elemTag == " VectorClassificationItemNg" )
285+ {
286+ currentChildItem = new QgsComposerSymbolV2Item ();
287+ }
288+ else
289+ {
290+ continue ; // unsupported child type
291+ }
292+ currentChildItem->readXML ( currentElem );
293+ appendRow ( currentChildItem );
294+ }
199295}
200296
201297// //////////////////QgsComposerGroupItem
@@ -229,5 +325,43 @@ void QgsComposerGroupItem::writeXML( QDomElement& elem, QDomDocument& doc ) cons
229325
230326void QgsComposerGroupItem::readXML ( const QDomElement& itemElem )
231327{
232- // soon...
328+ if ( itemElem.isNull () )
329+ {
330+ return ;
331+ }
332+ setText ( itemElem.attribute ( " text" , " " ) );
333+
334+ // now call readXML for all the child items
335+ QDomNodeList childList = itemElem.childNodes ();
336+ QDomNode currentNode;
337+ QDomElement currentElem;
338+ QgsComposerLegendItem* currentChildItem = 0 ;
339+
340+ int nChildItems = childList.count ();
341+ for ( int i = 0 ; i < nChildItems; ++i )
342+ {
343+ currentNode = childList.at ( i );
344+ if ( !currentNode.isElement () )
345+ {
346+ continue ;
347+ }
348+
349+ currentElem = currentNode.toElement ();
350+ QString elemTag = currentElem.tagName ();
351+
352+ if ( elemTag == " GroupItem" )
353+ {
354+ currentChildItem = new QgsComposerGroupItem ();
355+ }
356+ else if ( elemTag == " LayerItem" )
357+ {
358+ currentChildItem = new QgsComposerLayerItem ();
359+ }
360+ else
361+ {
362+ continue ; // unsupported child item type
363+ }
364+ currentChildItem->readXML ( currentElem );
365+ appendRow ( currentChildItem );
366+ }
233367}
0 commit comments