QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgslegendmodel.cpp
Go to the documentation of this file.
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 "qgscomposerlegenditem.h"
20 #include "qgsfield.h"
21 #include "qgsmaplayer.h"
22 #include "qgsmaplayerregistry.h"
23 #include "qgsrasterlayer.h"
24 #include "qgsrendererv2.h"
25 #include "qgssymbollayerv2utils.h"
26 #include "qgsvectordataprovider.h"
27 #include "qgsvectorlayer.h"
28 #include <QApplication>
29 #include <QDomDocument>
30 #include <QDomElement>
31 #include <QMimeData>
32 #include <QSettings>
33 #include <QMessageBox>
34 
35 QgsLegendModel::QgsLegendModel(): QStandardItemModel(), mAutoUpdate( true )
36 {
37  setColumnCount( 2 );
38 
40  {
41  connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
42  connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
43  }
44 
45  QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
46  mHasTopLevelWindow = ( topLevelWidgets.size() > 0 );
47 }
48 
50 {
51 }
52 
53 void QgsLegendModel::setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo )
54 {
55  setLayerSet( layerIds );
56 
57  QStandardItem* currentItem = 0;
58  QStandardItem* currentGroupItem = 0;
59  int i = 0;
60 
61  QList< GroupLayerInfo >::const_iterator infoIt = groupInfo.constBegin();
62  for ( ; infoIt != groupInfo.constEnd() && i < invisibleRootItem()->rowCount(); )
63  {
64  currentItem = invisibleRootItem()->child( i, 0 );
65  QString infoKey = infoIt->first;
66  if ( infoKey.isNull() ) //a toplevel layer
67  {
68  ++i;
69  }
70  else //a group
71  {
72  currentGroupItem = addGroup( infoKey, i );
73  ++i;
74  QList<QString> layerList = infoIt->second;
75  QList<QString>::const_iterator groupLayerIt = layerList.constBegin();
76  for ( ; currentItem && ( groupLayerIt != layerList.constEnd() ); ++groupLayerIt )
77  {
78  //check if current item is contained in this group
79  QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( currentItem );
80  if ( !layerItem )
81  {
82  return; //should never happen
83  }
84  QString layerID = layerItem->layerID();
85  if ( layerList.contains( layerID ) )
86  {
87  takeRow( i );
88  currentGroupItem->setChild( currentGroupItem->rowCount(), 0, currentItem );
89  }
90  else
91  {
92  ++i;
93  }
94  currentItem = invisibleRootItem()->child( i, 0 );
95  }
96  }
97  ++infoIt;
98  }
99 }
100 
101 void QgsLegendModel::setLayerSet( const QStringList& layerIds )
102 {
103  mLayerIds = layerIds;
104 
105  //for now clear the model and add the new entries
106  clear();
107 
108  QStringList::const_iterator idIter = mLayerIds.constBegin();
109  QgsMapLayer* currentLayer = 0;
110 
111  for ( ; idIter != mLayerIds.constEnd(); ++idIter )
112  {
113  currentLayer = QgsMapLayerRegistry::instance()->mapLayer( *idIter );
114  addLayer( currentLayer );
115  }
116 }
117 
118 QStandardItem* QgsLegendModel::addGroup( QString text, int position )
119 {
120  if ( text.isNull() )
121  text = tr( "Group" );
122 
123  QgsComposerGroupItem* groupItem = new QgsComposerGroupItem( text );
124  groupItem->setUserText( text );
125 
126  if ( position == -1 )
127  {
128  position = invisibleRootItem()->rowCount();
129  }
130  QList<QStandardItem *> itemsList;
131  itemsList << groupItem << new QgsComposerStyleItem( groupItem );
132  invisibleRootItem()->insertRow( position, itemsList );
133 
134  emit layersChanged();
135  return groupItem;
136 }
137 
138 int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLayer* vlayer )
139 {
140  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );
141 
142  if ( !layerItem || !lItem || !vlayer )
143  {
144  return 1;
145  }
146 
147  QgsFeatureRendererV2* renderer = vlayer->rendererV2();
148  if ( !renderer )
149  {
150  return 2;
151  }
152 
153  if ( lItem->showFeatureCount() )
154  {
155  if ( !vlayer->countSymbolFeatures() )
156  {
157  QgsDebugMsg( "Cannot get feature counts" );
158  }
159  }
160 
161  QgsLegendSymbolList lst = renderer->legendSymbolItems();
162  QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin();
163  int row = 0;
164  for ( ; symbolIt != lst.constEnd(); ++symbolIt )
165  {
166  QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( "" );
167 
168  // Get userText from old item if exists
169  QgsComposerSymbolV2Item* oldSymbolItem = dynamic_cast<QgsComposerSymbolV2Item*>( layerItem->child( row, 0 ) );
170  if ( oldSymbolItem )
171  {
172  currentSymbolItem->setUserText( oldSymbolItem->userText() );
173 
174  }
175 
176  currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
177  if ( symbolIt->second )
178  {
179  if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server
180  {
181  currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) );
182  }
183  currentSymbolItem->setSymbolV2( symbolIt->second->clone() );
184  }
185  layerItem->setChild( row, 0, currentSymbolItem );
186 
187  // updateSymbolV2ItemText needs layer set
188  updateSymbolV2ItemText( currentSymbolItem );
189 
190  row++;
191  }
192 
193  // Delete following old items (if current number of items decreased)
194  for ( int i = layerItem->rowCount() - 1; i >= row; --i )
195  {
196  layerItem->removeRow( i );
197  }
198 
199  return 0;
200 }
201 
202 int QgsLegendModel::addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer* rlayer )
203 {
204  if ( !layerItem || !rlayer )
205  {
206  return 1;
207  }
208 
209  QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( rlayer );
210  if ( !rasterLayer )
211  {
212  return 2;
213  }
214 
215  QList< QPair< QString, QColor > > rasterItemList = rasterLayer->legendSymbologyItems();
216  QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
217  int row = 0;
218  for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
219  {
220  QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( itemIt->first );
221 
222  QgsComposerRasterSymbolItem* oldSymbolItem = dynamic_cast<QgsComposerRasterSymbolItem*>( layerItem->child( row, 0 ) );
223  if ( oldSymbolItem )
224  {
225  currentSymbolItem->setUserText( oldSymbolItem->userText() );
226  currentSymbolItem->setText( currentSymbolItem->userText() );
227  }
228 
229  if ( mHasTopLevelWindow )
230  {
231  QPixmap itemPixmap( 20, 20 );
232  itemPixmap.fill( itemIt->second );
233  currentSymbolItem->setIcon( QIcon( itemPixmap ) );
234  }
235  currentSymbolItem->setLayerID( rasterLayer->id() );
236  currentSymbolItem->setColor( itemIt->second );
237  int currentRowCount = layerItem->rowCount();
238  layerItem->setChild( currentRowCount, 0, currentSymbolItem );
239  row++;
240  }
241 
242  // Delete following old items (if current number of items decreased)
243  for ( int i = layerItem->rowCount() - 1; i >= row; --i )
244  {
245  layerItem->removeRow( i );
246  }
247 
248  return 0;
249 }
250 
251 void QgsLegendModel::updateSymbolV2ItemText( QStandardItem* symbolItem )
252 {
253  QgsComposerSymbolV2Item* sv2Item = dynamic_cast<QgsComposerSymbolV2Item*>( symbolItem );
254  if ( !sv2Item ) return;
255 
256  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( sv2Item->parent() );
257  if ( !lItem ) return;
258 
259  QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
260  if ( !mapLayer ) return;
261 
262  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
263  if ( !vLayer ) return;
264 
265  QgsFeatureRendererV2* renderer = vLayer->rendererV2();
266  if ( !renderer ) return;
267 
268  if ( lItem->showFeatureCount() ) vLayer->countSymbolFeatures();
269 
270  QgsLegendSymbolList symbolList = renderer->legendSymbolItems();
271 
272  QPair<QString, QgsSymbolV2*> symbol = symbolList.value( symbolItem->row() );
273 
274  QString label = sv2Item->userText().isEmpty() ? symbol.first : sv2Item->userText();
275 
276  if ( renderer->type() == "singleSymbol" )
277  {
278  if ( !sv2Item->userText().isEmpty() )
279  {
280  label = sv2Item->userText();
281  }
282  else if ( !lItem->userText().isEmpty() )
283  {
284  label = lItem->userText();
285  }
286  else if ( !vLayer->title().isEmpty() )
287  {
288  label = vLayer->title();
289  }
290  else
291  {
292  label = vLayer->name();
293  }
294  }
295 
296  if ( lItem->showFeatureCount() )
297  {
298  // Add counts to multi symbols layers only or labeled single symbols,
299  // so that single symbol layers are still drawn on single line
300  if ( symbolList.size() > 1 || !label.isEmpty() )
301  {
302  label += QString( " [%1]" ).arg( vLayer->featureCount( symbol.second ) );
303  }
304  }
305  symbolItem->setText( label );
306 }
307 
308 void QgsLegendModel::updateRasterSymbolItemText( QStandardItem* symbolItem )
309 {
310  QgsComposerRasterSymbolItem* rItem = dynamic_cast<QgsComposerRasterSymbolItem*>( symbolItem );
311  if ( !rItem ) return;
312 
313  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( rItem->parent() );
314  if ( !lItem ) return;
315 
316  QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
317  if ( !mapLayer ) return;
318 
319  QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
320  if ( !rLayer ) return;
321 
322  QPair< QString, QColor> symbol = rLayer->legendSymbologyItems().value( symbolItem->row() );
323 
324  QString label = rItem->userText().isEmpty() ? symbol.first : rItem->userText();
325 
326  symbolItem->setText( label );
327 }
328 
329 void QgsLegendModel::updateItem( QStandardItem* item )
330 {
331  if ( !item )
332  {
333  return;
334  }
335 
336  //only layer items are supported for update
337  QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item );
338  if ( ! cItem )
339  {
340  return;
341  }
342 
344  if ( type == QgsComposerLegendItem::LayerItem )
345  {
346  updateLayer( cItem );
347  }
348 }
349 
350 void QgsLegendModel::updateItemText( QStandardItem* item )
351 {
352  if ( !item )
353  {
354  return;
355  }
356 
357  //only layer items are supported for update
358  QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item );
359  if ( ! cItem )
360  {
361  return;
362  }
363 
364  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( cItem );
365  if ( lItem )
366  {
367  updateLayerItemText( lItem );
368  return;
369  }
370 
371  QgsComposerSymbolV2Item* sv2Item = dynamic_cast<QgsComposerSymbolV2Item*>( cItem );
372  if ( sv2Item )
373  {
374  updateSymbolV2ItemText( sv2Item );
375  return;
376  }
377 
378  QgsComposerRasterSymbolItem* rItem = dynamic_cast<QgsComposerRasterSymbolItem*>( cItem );
379  if ( rItem )
380  {
382  return;
383  }
384 
385  // group
386  cItem->setText( cItem->userText() );
387 }
388 
389 void QgsLegendModel::updateLayer( QStandardItem* layerItem )
390 {
391  QgsDebugMsg( "Entered." );
392  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );
393  if ( lItem )
394  {
395  QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
396  if ( mapLayer )
397  {
398  updateLayerItemText( lItem );
399 
400  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
401  if ( vLayer )
402  {
403  addVectorLayerItemsV2( lItem, vLayer );
404  }
405 
406  QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
407  if ( rLayer )
408  {
409  addRasterLayerItems( lItem, rLayer );
410  }
411  }
412  }
413 }
414 
415 void QgsLegendModel::updateLayerItemText( QStandardItem* layerItem )
416 {
417  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );
418  if ( !lItem ) return;
419 
420  QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
421  if ( !mapLayer ) return;
422 
423  QString label = lItem->userText().isEmpty() ? mapLayer->name() : lItem->userText();
424 
425  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
426  if ( vLayer )
427  {
428  addVectorLayerItemsV2( lItem, vLayer );
429  if ( lItem->showFeatureCount() )
430  {
431  label += QString( " [%1]" ).arg( vLayer->featureCount() );
432  }
433  }
434  lItem->setText( label );
435 }
436 
437 void QgsLegendModel::removeLayer( const QString& layerId )
438 {
439  int numRootItems = rowCount();
440  for ( int i = 0; i < numRootItems ; ++i )
441  {
442  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( item( i ) );
443  if ( !lItem )
444  {
445  continue;
446  }
447 
448  if ( layerId == lItem->layerID() )
449  {
450  removeRow( i ); //todo: also remove the subitems and their symbols...
451  emit layersChanged();
452  return;
453  }
454  }
455 }
456 
458 {
459  if ( !theMapLayer )
460  {
461  return;
462  }
463 
464  QgsComposerLayerItem* layerItem = new QgsComposerLayerItem( theMapLayer->name() );
465  if ( theMapLayer->title() != "" )
466  {
467  layerItem->setText( theMapLayer->title() );
468  layerItem->setUserText( theMapLayer->title() );
469  }
470  layerItem->setLayerID( theMapLayer->id() );
471  layerItem->setDefaultStyle();
472  layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
473 
474  QList<QStandardItem *> itemsList;
475  itemsList << layerItem << new QgsComposerStyleItem( layerItem );
476  invisibleRootItem()->appendRow( itemsList );
477 
478  switch ( theMapLayer->type() )
479  {
481  {
482  QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( theMapLayer );
483  if ( vl )
484  {
485  addVectorLayerItemsV2( layerItem, vl );
486  }
487  break;
488  }
490  addRasterLayerItems( layerItem, theMapLayer );
491  break;
492  default:
493  break;
494  }
495  emit layersChanged();
496 }
497 
498 
499 bool QgsLegendModel::writeXML( QDomElement& composerLegendElem, QDomDocument& doc ) const
500 {
501  if ( composerLegendElem.isNull() )
502  {
503  return false;
504  }
505 
506  QDomElement legendModelElem = doc.createElement( "Model" );
507  legendModelElem.setAttribute( "autoUpdate", mAutoUpdate );
508  int nTopLevelItems = invisibleRootItem()->rowCount();
509  QStandardItem* currentItem = 0;
510  QgsComposerLegendItem* currentLegendItem = 0;
511 
512  for ( int i = 0; i < nTopLevelItems; ++i )
513  {
514  currentItem = invisibleRootItem()->child( i, 0 );
515  currentLegendItem = dynamic_cast<QgsComposerLegendItem*>( currentItem );
516  if ( currentLegendItem )
517  {
518  currentLegendItem->writeXML( legendModelElem, doc );
519  }
520  }
521 
522  composerLegendElem.appendChild( legendModelElem );
523  return true;
524 }
525 
526 bool QgsLegendModel::readXML( const QDomElement& legendModelElem, const QDomDocument& doc )
527 {
528  Q_UNUSED( doc );
529 
530  if ( legendModelElem.isNull() )
531  {
532  return false;
533  }
534 
535  clear();
536 
537  QDomNodeList topLevelItemList = legendModelElem.childNodes();
538  QDomElement currentElem;
539  QgsComposerLegendItem* currentItem = 0;
540 
541  int nTopLevelItems = topLevelItemList.size();
542  for ( int i = 0; i < nTopLevelItems; ++i )
543  {
544  currentElem = topLevelItemList.at( i ).toElement();
545  if ( currentElem.isNull() )
546  {
547  continue;
548  }
549 
550  //toplevel items can be groups or layers
551  if ( currentElem.tagName() == "LayerItem" )
552  {
553  currentItem = new QgsComposerLayerItem();
554  }
555  else if ( currentElem.tagName() == "GroupItem" )
556  {
557  currentItem = new QgsComposerGroupItem();
558  }
559  currentItem->readXML( currentElem, mHasTopLevelWindow );
560 
561  QList<QStandardItem *> itemsList;
562  itemsList << currentItem << new QgsComposerStyleItem( currentItem );
563  appendRow( itemsList );
564  }
565 
566  setAutoUpdate( legendModelElem.attribute( "autoUpdate", "1" ).toInt() );
567  return true;
568 }
569 
571 {
572  return Qt::MoveAction;
573 }
574 
575 Qt::ItemFlags QgsLegendModel::flags( const QModelIndex &index ) const
576 {
577  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
578  if ( !index.isValid() )
579  {
580  flags |= Qt::ItemIsDropEnabled;
581  return flags;
582  }
583 
584  QStandardItem* item = itemFromIndex( index );
585  QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item );
586 
587  if ( cItem )
588  {
590  if ( type == QgsComposerLegendItem::GroupItem )
591  {
592  flags |= Qt::ItemIsDragEnabled;
593  flags |= Qt::ItemIsDropEnabled;
594  }
595  else if ( type == QgsComposerLegendItem::LayerItem )
596  {
597  flags |= Qt::ItemIsDragEnabled;
598  }
599  }
600  if ( index.column() == 1 && item )
601  {
602  // Style
603  QStandardItem* firstColumnItem = 0;
604  if ( item->parent() )
605  {
606  firstColumnItem = item->parent()->child( index.row(), 0 );
607  }
608  else
609  {
610  firstColumnItem = QgsLegendModel::item( index.row(), 0 );
611  }
612  cItem = dynamic_cast<QgsComposerLegendItem*>( firstColumnItem );
613 
614  if ( cItem )
615  {
616  if ( cItem->itemType() == QgsComposerLegendItem::GroupItem ||
618  {
619  flags |= Qt::ItemIsEditable;
620  }
621  }
622  }
623  return flags;
624 }
625 
626 bool QgsLegendModel::removeRows( int row, int count, const QModelIndex & parent )
627 {
628  if ( count < 1 )
629  {
630  return false;
631  }
632 
633  if ( parent.isValid() )
634  {
635  for ( int i = row + count - 1; i >= row; --i )
636  {
637  QStandardItem* item = itemFromIndex( parent );
638  if ( item )
639  {
640  item->takeRow( i );
641  }
642  }
643  }
644  else
645  {
646  for ( int i = row + count - 1; i >= row; --i )
647  {
648  takeRow( i );
649  }
650  }
651  return true;
652 }
653 
654 QMimeData* QgsLegendModel::mimeData( const QModelIndexList &indexes ) const
655 {
656  QMimeData* mimeData = new QMimeData();
657  QByteArray encodedData;
658  QDomDocument xmlDoc;
659  QDomElement xmlRootElement = xmlDoc.createElement( "LegendModelDragData" );
660  xmlDoc.appendChild( xmlRootElement );
661 
662  QModelIndexList::const_iterator indexIt = indexes.constBegin();
663  for ( ; indexIt != indexes.constEnd(); ++indexIt )
664  {
665  QStandardItem* sItem = itemFromIndex( *indexIt );
666  if ( sItem )
667  {
668  QgsComposerLegendItem* mItem = dynamic_cast<QgsComposerLegendItem*>( sItem );
669  if ( mItem )
670  {
671  mItem->writeXML( xmlRootElement, xmlDoc );
672  }
673  }
674  }
675  mimeData->setData( "text/xml", xmlDoc.toByteArray() );
676  return mimeData;
677 }
678 
679 QStringList QgsLegendModel::mimeTypes() const
680 {
681  QStringList types;
682  types << "text/xml";
683  return types;
684 }
685 
686 bool QgsLegendModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent )
687 {
688  Q_UNUSED( action );
689  Q_UNUSED( column );
690 
691  if ( !data->hasFormat( "text/xml" ) )
692  {
693  return false;
694  }
695 
696  QStandardItem* dropIntoItem = 0;
697  if ( parent.isValid() )
698  {
699  dropIntoItem = itemFromIndex( parent );
700  }
701  else
702  {
703  dropIntoItem = invisibleRootItem();
704  }
705 
706  //get XML doc
707  QByteArray encodedData = data->data( "text/xml" );
708  QDomDocument xmlDoc;
709  xmlDoc.setContent( encodedData );
710 
711  QDomElement dragDataElem = xmlDoc.documentElement();
712  if ( dragDataElem.tagName() != "LegendModelDragData" )
713  {
714  return false;
715  }
716 
717  QDomNodeList nodeList = dragDataElem.childNodes();
718  int nChildNodes = nodeList.size();
719  QDomElement currentElem;
720  QString currentTagName;
721  QgsComposerLegendItem* currentItem = 0;
722 
723  for ( int i = 0; i < nChildNodes; ++i )
724  {
725  currentElem = nodeList.at( i ).toElement();
726  if ( currentElem.isNull() )
727  {
728  continue;
729  }
730  currentTagName = currentElem.tagName();
731  if ( currentTagName == "LayerItem" )
732  {
733  currentItem = new QgsComposerLayerItem();
734  }
735  else if ( currentTagName == "GroupItem" )
736  {
737  currentItem = new QgsComposerGroupItem();
738  }
739  else
740  {
741  continue;
742  }
743  currentItem->readXML( currentElem );
744  int index;
745  if ( row < 0 )
746  {
747  index = dropIntoItem->rowCount();
748  }
749  else
750  {
751  index = row + i;
752  }
753  QList<QStandardItem *> itemsList;
754  itemsList << currentItem << new QgsComposerStyleItem( currentItem );
755  dropIntoItem->insertRow( index, itemsList );
756  }
757  emit layersChanged();
758  return true;
759 }
760 
761 void QgsLegendModel::setAutoUpdate( bool autoUpdate )
762 {
763  if ( mAutoUpdate == autoUpdate ) //prevent multiple signal/slot connections
764  {
765  return;
766  }
767 
769  if ( autoUpdate )
770  {
772  {
773  connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
774  connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
775  }
776  }
777  else
778  {
780  {
781  disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
782  disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
783  }
784  }
785 }
void removeLayer(const QString &layerId)
virtual void readXML(const QDomElement &itemElem, bool xServerAvailable=true)=0
Read item content from xml.
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
Base class for all map layer types.
Definition: qgsmaplayer.h:45
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:89
void setLayerID(const QString &id)
void updateItemText(QStandardItem *item)
Update single item text using item userText and other properties like showFeatureCount.
QStringList mLayerIds
bool mHasTopLevelWindow
True if this application has toplevel windows (normally true).
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
void updateRasterSymbolItemText(QStandardItem *symbolItem)
Qt::DropActions supportedDropActions() const
void updateItem(QStandardItem *item)
Tries to automatically update a model entry (e.g.
virtual QString userText() const
int addRasterLayerItems(QStandardItem *layerItem, QgsMapLayer *rlayer)
Adds item of raster layer.
void setLayerID(const QString &id)
QString type() const
Definition: qgsrendererv2.h:76
const QString & name() const
Get the display name of the layer.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Implements the drop operation.
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
void updateLayerItemText(QStandardItem *layerItem)
virtual QgsLegendSymbolList legendSymbolItems()
return a list of item text / symbol
void setAutoUpdate(bool autoUpdate)
void updateSymbolV2ItemText(QStandardItem *symbolItem)
virtual void writeXML(QDomElement &elem, QDomDocument &doc) const =0
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:95
bool writeXML(QDomElement &composerLegendElem, QDomDocument &doc) const
void setSymbolV2(QgsSymbolV2 *s)
Set symbol (takes ownership)
bool mAutoUpdate
True if the legend is auto updated when layers are added or removed from the map canvas.
Abstract base class for the legend item types.
bool readXML(const QDomElement &legendModelElem, const QDomDocument &doc)
bool countSymbolFeatures(bool showProgress=true)
Count features for symbols.
QStandardItem * addGroup(QString text=QString::null, int position=-1)
Adds a group.
void layersChanged()
virtual long featureCount() const
Number of features in the layer.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
int addVectorLayerItemsV2(QStandardItem *layerItem, QgsVectorLayer *vlayer)
Adds classification items of vector layers using new symbology.
void setLayerSetAndGroups(const QStringList &layerIds, const QList< GroupLayerInfo > &groupInfo)
Sets layer set and groups.
QMimeData * mimeData(const QModelIndexList &indexes) const
For the drag operation.
virtual ItemType itemType() const =0
QStringList mimeTypes() const
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
void addLayer(QgsMapLayer *theMapLayer)
void updateLayer(QStandardItem *layerItem)
Updates the whole symbology of a layer.
void setLayerSet(const QStringList &layerIds)
Represents a vector layer which manages a vector based data sets.
QList< QPair< QString, QgsSymbolV2 * > > QgsLegendSymbolList
Definition: qgsrendererv2.h:40
const QString & title() const
Definition: qgsmaplayer.h:94
virtual void setUserText(const QString &text)
QList< QPair< QString, QColor > > legendSymbologyItems() const
Returns a list with classification items (Text and color)
Qt::ItemFlags flags(const QModelIndex &index) const
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Implemented to support drag operations.
#define tr(sourceText)