~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/core/composer/qgslegendmodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                         qgslegendmodel.cpp  -  description
 
3
                         ------------------
 
4
    begin                : June 2008
 
5
    copyright            : (C) 2008 by Marco Hugentobler
 
6
    email                : marco dot hugentobler at karto dot baug dot ethz 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 "qgslegendmodel.h"
 
19
#include "qgsfield.h"
 
20
#include "qgsmaplayer.h"
 
21
#include "qgsmaplayerregistry.h"
 
22
#include "qgsrasterlayer.h"
 
23
#include "qgsrenderer.h"
 
24
#include "qgssymbol.h"
 
25
#include "qgsvectordataprovider.h"
 
26
#include "qgsvectorlayer.h"
 
27
#include <QDomDocument>
 
28
#include <QDomElement>
 
29
#include <QSettings>
 
30
 
 
31
QgsLegendModel::QgsLegendModel(): QStandardItemModel()
 
32
{
 
33
  if ( QgsMapLayerRegistry::instance() )
 
34
  {
 
35
    connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
 
36
    connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
 
37
  }
 
38
}
 
39
 
 
40
QgsLegendModel::~QgsLegendModel()
 
41
{
 
42
  removeAllSymbols();
 
43
}
 
44
 
 
45
void QgsLegendModel::setLayerSet( const QStringList& layerIds )
 
46
{
 
47
  mLayerIds = layerIds;
 
48
 
 
49
  //for now clear the model and add the new entries
 
50
  clear();
 
51
 
 
52
  QStringList::const_iterator idIter = mLayerIds.constBegin();
 
53
  QgsMapLayer* currentLayer = 0;
 
54
 
 
55
  for ( ; idIter != mLayerIds.constEnd(); ++idIter )
 
56
  {
 
57
    currentLayer = QgsMapLayerRegistry::instance()->mapLayer( *idIter );
 
58
 
 
59
    //addItem for layer
 
60
    QStandardItem* layerItem = new QStandardItem( currentLayer->name() );
 
61
    //set layer id as user data into the item
 
62
    layerItem->setData( QVariant( currentLayer->getLayerID() ) );
 
63
    layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
 
64
 
 
65
    invisibleRootItem()->setChild( invisibleRootItem()->rowCount(), layerItem );
 
66
 
 
67
    switch ( currentLayer->type() )
 
68
    {
 
69
      case QgsMapLayer::VectorLayer:
 
70
        addVectorLayerItems( layerItem, currentLayer );
 
71
        break;
 
72
      case QgsMapLayer::RasterLayer:
 
73
        addRasterLayerItem( layerItem, currentLayer );
 
74
        break;
 
75
      default:
 
76
        break;
 
77
    }
 
78
  }
 
79
 
 
80
}
 
81
 
 
82
int QgsLegendModel::addVectorLayerItems( QStandardItem* layerItem, QgsMapLayer* vlayer )
 
83
{
 
84
  if ( !layerItem || !vlayer )
 
85
  {
 
86
    return 1;
 
87
  }
 
88
 
 
89
  QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer *>( vlayer );
 
90
  if ( !vectorLayer )
 
91
  {
 
92
    return 2;
 
93
  }
 
94
  int opacity = vectorLayer->getTransparency();
 
95
 
 
96
  const QgsRenderer* vectorRenderer = vectorLayer->renderer();
 
97
  if ( !vectorRenderer )
 
98
  {
 
99
    return 3;
 
100
  }
 
101
 
 
102
  //text field that describes classification attribute?
 
103
  QSettings settings;
 
104
  if ( settings.value( "/qgis/showLegendClassifiers", false ).toBool() )
 
105
  {
 
106
    QgsFieldMap layerFields = vectorLayer->pendingFields();
 
107
    QgsAttributeList attributes = vectorRenderer->classificationAttributes();
 
108
    QgsAttributeList::const_iterator att_it = attributes.constBegin();
 
109
    for ( ; att_it != attributes.constEnd(); ++att_it )
 
110
    {
 
111
      QgsFieldMap::const_iterator fieldIt = layerFields.find( *att_it );
 
112
      if ( fieldIt != layerFields.constEnd() )
 
113
      {
 
114
        QString attributeName = vectorLayer->attributeDisplayName( fieldIt.key() );
 
115
        QStandardItem* attributeItem = new QStandardItem( attributeName );
 
116
        layerItem->setChild( layerItem->rowCount(), 0, attributeItem );
 
117
      }
 
118
    }
 
119
  }
 
120
 
 
121
  const QList<QgsSymbol*> vectorSymbols = vectorRenderer->symbols();
 
122
  QList<QgsSymbol*>::const_iterator symbolIt = vectorSymbols.constBegin();
 
123
 
 
124
  for ( ; symbolIt != vectorSymbols.constEnd(); ++symbolIt )
 
125
  {
 
126
    if ( !( *symbolIt ) )
 
127
    {
 
128
      continue;
 
129
    }
 
130
 
 
131
    QStandardItem* currentSymbolItem = itemFromSymbol( *symbolIt, opacity );
 
132
    if ( !currentSymbolItem )
 
133
    {
 
134
      continue;
 
135
    }
 
136
 
 
137
    layerItem->setChild( layerItem->rowCount(), 0, currentSymbolItem );
 
138
 
 
139
  }
 
140
 
 
141
  return 0;
 
142
}
 
143
 
 
144
int QgsLegendModel::addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* rlayer )
 
145
{
 
146
  if ( !layerItem || !rlayer )
 
147
  {
 
148
    return 1;
 
149
  }
 
150
 
 
151
  QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( rlayer );
 
152
  if ( !rasterLayer )
 
153
  {
 
154
    return 2;
 
155
  }
 
156
 
 
157
  QStandardItem* currentSymbolItem = new QStandardItem( QIcon( rasterLayer->legendAsPixmap( true ) ), "" );
 
158
 
 
159
  int currentRowCount = layerItem->rowCount();
 
160
  layerItem->setChild( currentRowCount, 0, currentSymbolItem );
 
161
 
 
162
  return 0;
 
163
}
 
164
 
 
165
void QgsLegendModel::insertSymbol( QgsSymbol* s )
 
166
{
 
167
  QSet<QgsSymbol*>::iterator it = mSymbols.find( s );
 
168
  if ( it != mSymbols.end() )
 
169
  {
 
170
    delete( *it ); //very unlikely
 
171
  }
 
172
  mSymbols.insert( s );
 
173
}
 
174
 
 
175
void QgsLegendModel::removeSymbol( QgsSymbol* s )
 
176
{
 
177
  mSymbols.remove( s );
 
178
}
 
179
 
 
180
void QgsLegendModel::removeAllSymbols()
 
181
{
 
182
  QSet<QgsSymbol*>::iterator it = mSymbols.begin();
 
183
  for ( ; it != mSymbols.end(); ++it )
 
184
  {
 
185
    delete *it;
 
186
  }
 
187
  mSymbols.clear();
 
188
}
 
189
 
 
190
void QgsLegendModel::updateItem( QStandardItem* item )
 
191
{
 
192
  if ( !item )
 
193
  {
 
194
    return;
 
195
  }
 
196
 
 
197
  //is it a toplevel layer item?
 
198
  QModelIndex itemIndex = indexFromItem( item );
 
199
  QModelIndex parentIndex = itemIndex.parent();
 
200
  if ( !parentIndex.isValid() ) // a layer item?
 
201
  {
 
202
    updateLayer( item );
 
203
  }
 
204
 
 
205
  //take QgsSymbol* from user data
 
206
  QVariant symbolVariant = item->data();
 
207
  QgsSymbol* symbol = 0;
 
208
  if ( symbolVariant.canConvert<void*>() )
 
209
  {
 
210
    void* symbolData = symbolVariant.value<void*>();
 
211
    symbol = ( QgsSymbol* )( symbolData );
 
212
  }
 
213
 
 
214
  if ( symbol )  //vector classification item
 
215
  {
 
216
    updateVectorClassificationItem( item, symbol, item->text() );
 
217
  }
 
218
  else if ( !item->icon().isNull() ) //raster classification item
 
219
  {
 
220
    updateRasterClassificationItem( item );
 
221
  }
 
222
}
 
223
 
 
224
void QgsLegendModel::updateLayer( QStandardItem* layerItem )
 
225
{
 
226
  if ( !layerItem )
 
227
  {
 
228
    return;
 
229
  }
 
230
 
 
231
  QString layerId = layerItem->data().toString();
 
232
  QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
 
233
  if ( mapLayer )
 
234
  {
 
235
    //delete all the entries under layer item
 
236
    int currentRowCount = layerItem->rowCount();
 
237
    for ( int i = currentRowCount - 1; i >= 0; --i )
 
238
    {
 
239
      layerItem->removeRow( i );
 
240
    }
 
241
 
 
242
    //and add the new ones...
 
243
    switch ( mapLayer->type() )
 
244
    {
 
245
      case QgsMapLayer::VectorLayer:
 
246
        addVectorLayerItems( layerItem, mapLayer );
 
247
        break;
 
248
      case QgsMapLayer::RasterLayer:
 
249
        addRasterLayerItem( layerItem, mapLayer );
 
250
        break;
 
251
      default:
 
252
        break;
 
253
    }
 
254
  }
 
255
}
 
256
 
 
257
void QgsLegendModel::updateVectorClassificationItem( QStandardItem* classificationItem, QgsSymbol* symbol, QString itemText )
 
258
{
 
259
  //this function uses the following logic to find a classification match:
 
260
  //first test if there is a symbol where lowerbound - upperbound equels itemText
 
261
  //if no match found, test if there is a symbol where label equals itemText
 
262
  //still no match found. Test, if there is a symbol with same pen/brush/point symbol
 
263
 
 
264
  //get parent item
 
265
  QStandardItem* parentItem = classificationItem->parent();
 
266
  if ( !parentItem )
 
267
  {
 
268
    return;
 
269
  }
 
270
 
 
271
  //get maplayer object from parent item
 
272
  QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( parentItem->data().toString() );
 
273
  if ( !ml )
 
274
  {
 
275
    return;
 
276
  }
 
277
  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer *>( ml );
 
278
  if ( !vl )
 
279
  {
 
280
    return;
 
281
  }
 
282
  int opacity = vl->getTransparency();
 
283
 
 
284
  const QgsRenderer* layerRenderer = vl->renderer();
 
285
  if ( !layerRenderer )
 
286
  {
 
287
    return;
 
288
  }
 
289
 
 
290
  QList<QgsSymbol*> symbolList = layerRenderer->symbols();
 
291
  QList<QgsSymbol*>::iterator symbolIt;
 
292
  QgsSymbol* currentSymbol = 0;
 
293
 
 
294
  //try to find a symbol where lowerbound - upperbound matches item text
 
295
  symbolIt = symbolList.begin();
 
296
  for ( ; symbolIt != symbolList.end(); ++symbolIt )
 
297
  {
 
298
    currentSymbol = *symbolIt;
 
299
    if ( currentSymbol->lowerValue() + " - " + currentSymbol->upperValue() == itemText )
 
300
    {
 
301
      removeSymbol( symbol );
 
302
      parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity ) );
 
303
      parentItem->removeRow( classificationItem->row() );
 
304
      return;
 
305
    }
 
306
  }
 
307
 
 
308
  //try to find a symbol where lower value matches item text (non-numeric classifications)
 
309
  symbolIt = symbolList.begin();
 
310
  for ( ; symbolIt != symbolList.end(); ++symbolIt )
 
311
  {
 
312
    currentSymbol = *symbolIt;
 
313
    if ( currentSymbol->lowerValue() == itemText )
 
314
    {
 
315
      removeSymbol( symbol );
 
316
      parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity ) );
 
317
      parentItem->removeRow( classificationItem->row() );
 
318
      return;
 
319
    }
 
320
  }
 
321
 
 
322
  //try to find a symbol where label matches item text
 
323
  symbolIt = symbolList.begin();
 
324
  for ( ; symbolIt != symbolList.end(); ++symbolIt )
 
325
  {
 
326
    currentSymbol = *symbolIt;
 
327
    if ( currentSymbol->label() == itemText )
 
328
    {
 
329
      removeSymbol( symbol );
 
330
      parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity ) );
 
331
      parentItem->removeRow( classificationItem->row() );
 
332
      return;
 
333
    }
 
334
  }
 
335
}
 
336
 
 
337
 
 
338
void QgsLegendModel::updateRasterClassificationItem( QStandardItem* classificationItem )
 
339
{
 
340
  if ( !classificationItem )
 
341
  {
 
342
    return;
 
343
  }
 
344
 
 
345
  QStandardItem* parentItem = classificationItem->parent();
 
346
  if ( !parentItem )
 
347
  {
 
348
    return;
 
349
  }
 
350
 
 
351
  QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( parentItem->data().toString() );
 
352
  if ( !ml )
 
353
  {
 
354
    return;
 
355
  }
 
356
 
 
357
  QgsRasterLayer* rl = qobject_cast<QgsRasterLayer *>( ml );
 
358
  if ( !rl )
 
359
  {
 
360
    return;
 
361
  }
 
362
 
 
363
  QStandardItem* currentSymbolItem = new QStandardItem( QIcon( rl->legendAsPixmap( true ) ), "" );
 
364
  parentItem->insertRow( 0, currentSymbolItem );
 
365
  parentItem->removeRow( 1 );
 
366
}
 
367
 
 
368
void QgsLegendModel::removeLayer( const QString& layerId )
 
369
{
 
370
  QStandardItem* currentLayerItem = 0;
 
371
 
 
372
  int numRootItems = rowCount();
 
373
  for ( int i = 0; i < numRootItems ; ++i )
 
374
  {
 
375
    currentLayerItem = item( i );
 
376
    if ( !currentLayerItem )
 
377
    {
 
378
      continue;
 
379
    }
 
380
 
 
381
    QString currentId = currentLayerItem->data().toString();
 
382
    if ( currentId == layerId )
 
383
    {
 
384
      removeRow( i ); //todo: also remove the subitems and their symbols...
 
385
      emit layersChanged();
 
386
      return;
 
387
    }
 
388
  }
 
389
}
 
390
 
 
391
void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer )
 
392
{
 
393
  if ( !theMapLayer )
 
394
  {
 
395
    return;
 
396
  }
 
397
 
 
398
  //append new layer item
 
399
  QStandardItem* layerItem = new QStandardItem( theMapLayer->name() );
 
400
  //set layer id as user data into the item
 
401
  layerItem->setData( QVariant( theMapLayer->getLayerID() ) );
 
402
  layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
 
403
  invisibleRootItem()->setChild( invisibleRootItem()->rowCount(), layerItem );
 
404
 
 
405
  //and child items of layer
 
406
  switch ( theMapLayer->type() )
 
407
  {
 
408
    case QgsMapLayer::VectorLayer:
 
409
      addVectorLayerItems( layerItem, theMapLayer );
 
410
      break;
 
411
    case QgsMapLayer::RasterLayer:
 
412
      addRasterLayerItem( layerItem, theMapLayer );
 
413
      break;
 
414
    default:
 
415
      break;
 
416
  }
 
417
  emit layersChanged();
 
418
}
 
419
 
 
420
QStandardItem* QgsLegendModel::itemFromSymbol( QgsSymbol* s, int opacity )
 
421
{
 
422
  QStandardItem* currentSymbolItem = 0;
 
423
 
 
424
  //label
 
425
  QString itemText;
 
426
  QString label;
 
427
 
 
428
  QString lowerValue = s->lowerValue();
 
429
  QString upperValue = s->upperValue();
 
430
 
 
431
  label = s->label();
 
432
 
 
433
  //Take the label as item text if it is there
 
434
  if ( !label.isEmpty() )
 
435
  {
 
436
    itemText = label;
 
437
  }
 
438
  //take single value
 
439
  else if ( lowerValue == upperValue || upperValue.isEmpty() )
 
440
  {
 
441
    itemText = lowerValue;
 
442
  }
 
443
  else //or value range
 
444
  {
 
445
    itemText = lowerValue + " - " + upperValue;
 
446
  }
 
447
 
 
448
  //icon item
 
449
  QImage symbolImage;
 
450
  switch ( s->type() )
 
451
  {
 
452
    case QGis::Point:
 
453
      symbolImage =  s->getPointSymbolAsImage();
 
454
      break;
 
455
    case QGis::Line:
 
456
      symbolImage = s->getLineSymbolAsImage();
 
457
      break;
 
458
    case QGis::Polygon:
 
459
      symbolImage = s->getPolygonSymbolAsImage();
 
460
      break;
 
461
    default:
 
462
      return 0;
 
463
  }
 
464
 
 
465
  if ( opacity != 255 )
 
466
  {
 
467
    //todo: manipulate image pixel by pixel...
 
468
    QRgb oldColor;
 
469
    for ( int i = 0; i < symbolImage.height(); ++i )
 
470
    {
 
471
      QRgb* scanLineBuffer = ( QRgb* ) symbolImage.scanLine( i );
 
472
      for ( int j = 0; j < symbolImage.width(); ++j )
 
473
      {
 
474
        oldColor = symbolImage.pixel( j, i );
 
475
        scanLineBuffer[j] = qRgba( qRed( oldColor ), qGreen( oldColor ), qBlue( oldColor ), opacity );
 
476
      }
 
477
    }
 
478
  }
 
479
 
 
480
  currentSymbolItem = new QStandardItem( QIcon( QPixmap::fromImage( symbolImage ) ), itemText );
 
481
 
 
482
  if ( !currentSymbolItem )
 
483
  {
 
484
    return 0;
 
485
  }
 
486
 
 
487
  //Pass deep copy of QgsSymbol as user data. Cast to void* necessary such that QMetaType handles it
 
488
  QgsSymbol* symbolCopy = new QgsSymbol( *s );
 
489
  currentSymbolItem->setData( QVariant::fromValue(( void* )symbolCopy ) );
 
490
  insertSymbol( symbolCopy );
 
491
 
 
492
  currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
 
493
  return currentSymbolItem;
 
494
}
 
495
 
 
496
bool QgsLegendModel::writeXML( QDomElement& composerLegendElem, QDomDocument& doc ) const
 
497
{
 
498
  if ( composerLegendElem.isNull() )
 
499
  {
 
500
    return false;
 
501
  }
 
502
 
 
503
  QDomElement legendModelElem = doc.createElement( "Model" );
 
504
 
 
505
  //iterate over all items...
 
506
  QStandardItem* currentLayerItem = 0;
 
507
  QStandardItem* currentClassificationItem = 0;
 
508
  int numRootItems = rowCount();
 
509
 
 
510
  for ( int i = 0; i < numRootItems; ++i )
 
511
  {
 
512
    currentLayerItem = item( i );
 
513
    QDomElement newLayerItem = doc.createElement( "LayerItem" );
 
514
    newLayerItem.setAttribute( "layerId", currentLayerItem->data().toString() );
 
515
    newLayerItem.setAttribute( "text", currentLayerItem->text() );
 
516
 
 
517
    //add layer/classification items
 
518
    int numClassItems = currentLayerItem->rowCount();
 
519
    for ( int j = 0; j < numClassItems; ++j )
 
520
    {
 
521
      currentClassificationItem = currentLayerItem->child( j );
 
522
 
 
523
      //store text and QgsSymbol for vector classification items
 
524
      QVariant symbolVariant = currentClassificationItem->data();
 
525
      QgsSymbol* symbol = 0;
 
526
      if ( symbolVariant.canConvert<void*>() )
 
527
      {
 
528
        void* symbolData = symbolVariant.value<void*>();
 
529
        symbol = ( QgsSymbol* )( symbolData );
 
530
      }
 
531
      if ( symbol )
 
532
      {
 
533
        QDomElement vectorClassElem = doc.createElement( "VectorClassificationItem" );
 
534
        vectorClassElem.setAttribute( "text", currentClassificationItem->text() );
 
535
        symbol->writeXML( vectorClassElem, doc, 0 );
 
536
        newLayerItem.appendChild( vectorClassElem );
 
537
        continue;
 
538
      }
 
539
 
 
540
      //a text item
 
541
      if ( currentClassificationItem->icon().isNull() )
 
542
      {
 
543
        QDomElement textItemElem = doc.createElement( "TextItem" );
 
544
        textItemElem.setAttribute( "text", currentClassificationItem->text() );
 
545
        newLayerItem.appendChild( textItemElem );
 
546
      }
 
547
      else //else it can only be a raster item
 
548
      {
 
549
        QDomElement rasterClassElem = doc.createElement( "RasterItem" );
 
550
        rasterClassElem.setAttribute( "text", currentClassificationItem->text() );
 
551
        //storing the layer id also in the raster item makes parsing easier
 
552
        rasterClassElem.setAttribute( "layerId", currentLayerItem->data().toString() );
 
553
        newLayerItem.appendChild( rasterClassElem );
 
554
      }
 
555
    }
 
556
 
 
557
    legendModelElem.appendChild( newLayerItem );
 
558
  }
 
559
 
 
560
  composerLegendElem.appendChild( legendModelElem );
 
561
  return true;
 
562
}
 
563
 
 
564
bool QgsLegendModel::readXML( const QDomElement& legendModelElem, const QDomDocument& doc )
 
565
{
 
566
  if ( legendModelElem.isNull() )
 
567
  {
 
568
    return false;
 
569
  }
 
570
 
 
571
  //delete all stored symbols first
 
572
  removeAllSymbols();
 
573
 
 
574
  //iterate over layer items
 
575
  QDomNodeList layerItemList = legendModelElem.elementsByTagName( "LayerItem" );
 
576
  QgsMapLayer* currentLayer = 0; //store current layer to get
 
577
 
 
578
  for ( int i = 0; i < layerItemList.size(); ++i )
 
579
  {
 
580
    QDomElement layerItemElem = layerItemList.at( i ).toElement();
 
581
    QString layerId = layerItemElem.attribute( "layerId" );
 
582
 
 
583
    QStandardItem* layerItem = new QStandardItem( layerItemElem.attribute( "text" ) );
 
584
 
 
585
    //set layer id as user data into the item
 
586
    layerItem->setData( QVariant( layerId ) );
 
587
    layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
 
588
 
 
589
    currentLayer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
 
590
 
 
591
    //go through all children of layerItemElem
 
592
    QDomElement currentChildElement = layerItemElem.firstChildElement();
 
593
    while ( !currentChildElement.isNull() )
 
594
    {
 
595
      QStandardItem* childItem = new QStandardItem( currentChildElement.attribute( "text" ) );
 
596
      if ( currentChildElement.tagName() == "RasterItem" )
 
597
      {
 
598
        //get icon from current layer
 
599
        QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( currentLayer );
 
600
        if ( rasterLayer )
 
601
        {
 
602
          childItem->setIcon( QIcon( rasterLayer->legendAsPixmap( true ) ) );
 
603
        }
 
604
        layerItem->setChild( layerItem->rowCount(), 0, childItem );
 
605
      }
 
606
      else if ( currentChildElement.tagName() == "VectorClassificationItem" )
 
607
      {
 
608
        //read QgsSymbol from xml and get icon
 
609
        QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer *>( currentLayer );
 
610
        if ( vectorLayer )
 
611
        {
 
612
          //look for symbol
 
613
          QDomNodeList symbolNodeList = currentChildElement.elementsByTagName( "symbol" );
 
614
          if ( symbolNodeList.size() > 0 )
 
615
          {
 
616
            QgsSymbol* symbol = new QgsSymbol( vectorLayer->geometryType() );
 
617
            QDomNode symbolNode = symbolNodeList.at( 0 );
 
618
            symbol->readXML( symbolNode, vectorLayer );
 
619
            childItem->setData( QVariant::fromValue(( void* )symbol ) );
 
620
 
 
621
            //add icon
 
622
            switch ( symbol->type() )
 
623
            {
 
624
              case QGis::Point:
 
625
                childItem->setIcon( QIcon( QPixmap::fromImage( symbol->getPointSymbolAsImage() ) ) );
 
626
                break;
 
627
              case QGis::Line:
 
628
                childItem->setIcon( QIcon( QPixmap::fromImage( symbol->getLineSymbolAsImage() ) ) );
 
629
                break;
 
630
              case QGis::Polygon:
 
631
                childItem->setIcon( QIcon( QPixmap::fromImage( symbol->getPolygonSymbolAsImage() ) ) );
 
632
                break;
 
633
              case QGis::UnknownGeometry:
 
634
                // should not occur
 
635
                break;
 
636
            }
 
637
            insertSymbol( symbol );
 
638
          }
 
639
        }
 
640
        layerItem->setChild( layerItem->rowCount(), 0, childItem );
 
641
      }
 
642
      else if ( currentChildElement.tagName() == "TextItem" )
 
643
      {
 
644
        layerItem->setChild( layerItem->rowCount(), 0, childItem );
 
645
      }
 
646
      else //unknown tag name, don't add item
 
647
      {
 
648
        delete childItem;
 
649
      }
 
650
 
 
651
      currentChildElement = currentChildElement.nextSiblingElement();
 
652
    }
 
653
 
 
654
    invisibleRootItem()->setChild( invisibleRootItem()->rowCount(), layerItem );
 
655
  }
 
656
 
 
657
  return true;
 
658
}