[QGIS Commit] r9038 - branches/advanced_printing_branch/src/app/composer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat Aug 9 01:59:42 EDT 2008


Author: mhugent
Date: 2008-08-09 01:59:41 -0400 (Sat, 09 Aug 2008)
New Revision: 9038

Modified:
   branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp
   branches/advanced_printing_branch/src/app/composer/qgslegendmodel.h
Log:
Add new layers to composer legend automatically

Modified: branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp	2008-08-08 18:49:45 UTC (rev 9037)
+++ branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp	2008-08-09 05:59:41 UTC (rev 9038)
@@ -33,6 +33,7 @@
   if(QgsMapLayerRegistry::instance())
     {
       connect(QgsMapLayerRegistry::instance(), SIGNAL(layerWillBeRemoved(QString)), this, SLOT(removeLayer(const QString&)));
+      connect(QgsMapLayerRegistry::instance(), SIGNAL(layerWasAdded(QgsMapLayer*)), this, SLOT(addLayer(QgsMapLayer*)));
     }
 }
 
@@ -43,8 +44,6 @@
 
 void QgsLegendModel::setLayerSet(const QStringList& layerIds)
 {
-  //in future check which layers have newly been added or deleted
-
   mLayerIds = layerIds;
   
   //for now clear the model and add the new entries
@@ -296,13 +295,42 @@
       QString currentId = currentLayerItem->data().toString();
       if(currentId == layerId)
 	{
-	  removeRow(i);
+	  removeRow(i); //todo: also remove the subitems and their symbols...
 	  emit layersChanged();
 	  return;
 	}
     }
 }
 
+void QgsLegendModel::addLayer(QgsMapLayer* theMapLayer)
+{
+  if(!theMapLayer)
+    {
+      return;
+    }
+
+  //append new layer item
+  QStandardItem* layerItem = new QStandardItem(theMapLayer->name());
+  //set layer id as user data into the item
+  layerItem->setData(QVariant(theMapLayer->getLayerID()));
+  layerItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
+  invisibleRootItem()->setChild (invisibleRootItem()->rowCount(), layerItem); 
+
+  //and child items of layer
+  switch(theMapLayer->type())
+    {
+    case QgsMapLayer::VECTOR:
+      addVectorLayerItems(layerItem, theMapLayer);
+      break;
+    case QgsMapLayer::RASTER:
+      addRasterLayerItem(layerItem, theMapLayer);
+      break;
+    default:
+      break;
+    }
+  emit layersChanged();
+}
+
 bool QgsLegendModel::writeXML(QDomElement& composerLegendElem, QDomDocument& doc)
 {
   if(composerLegendElem.isNull())

Modified: branches/advanced_printing_branch/src/app/composer/qgslegendmodel.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgslegendmodel.h	2008-08-08 18:49:45 UTC (rev 9037)
+++ branches/advanced_printing_branch/src/app/composer/qgslegendmodel.h	2008-08-09 05:59:41 UTC (rev 9038)
@@ -45,6 +45,7 @@
 
  public slots:
   void removeLayer(const QString& layerId);
+  void addLayer(QgsMapLayer* theMapLayer);
 
  signals:
   void layersChanged();



More information about the QGIS-commit mailing list