[QGIS Commit] r12358 - in trunk/qgis: python/gui src/app src/app/legend src/gui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Dec 7 13:59:26 EST 2009


Author: jef
Date: 2009-12-07 13:59:25 -0500 (Mon, 07 Dec 2009)
New Revision: 12358

Modified:
   trunk/qgis/python/gui/qgisinterface.sip
   trunk/qgis/src/app/legend/qgslegend.cpp
   trunk/qgis/src/app/legend/qgslegend.h
   trunk/qgis/src/app/legend/qgslegenditem.h
   trunk/qgis/src/app/qgisapp.cpp
   trunk/qgis/src/app/qgisapp.h
   trunk/qgis/src/app/qgisappinterface.cpp
   trunk/qgis/src/app/qgisappinterface.h
   trunk/qgis/src/gui/qgisinterface.h
Log:
[FEATURE] add QgisInterface::setActiveLayer()
also fixes #2167


Modified: trunk/qgis/python/gui/qgisinterface.sip
===================================================================
--- trunk/qgis/python/gui/qgisinterface.sip	2009-12-07 18:18:30 UTC (rev 12357)
+++ trunk/qgis/python/gui/qgisinterface.sip	2009-12-07 18:59:25 UTC (rev 12358)
@@ -52,6 +52,10 @@
     //! Get pointer to the active layer (layer selected in the legend)
     virtual QgsMapLayer *activeLayer()=0;
 
+    //! Get pointer to the active layer (layer selected in the legend)
+    //! added in 1.4
+    virtual bool setActiveLayer( QgsMapLayer * )=0;
+
     //! Add an icon to the plugins toolbar
     virtual int addToolBarIcon(QAction *qAction) =0;
     //! Remove an action (icon) from the plugin toolbar

Modified: trunk/qgis/src/app/legend/qgslegend.cpp
===================================================================
--- trunk/qgis/src/app/legend/qgslegend.cpp	2009-12-07 18:18:30 UTC (rev 12357)
+++ trunk/qgis/src/app/legend/qgslegend.cpp	2009-12-07 18:59:25 UTC (rev 12358)
@@ -145,18 +145,16 @@
   bool renderFlagState = mMapCanvas->renderFlag();
   mMapCanvas->setRenderFlag( false );
 
-  QTreeWidgetItem* theItem = firstItem();
-
-  while ( theItem )
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
   {
     QgsLegendItem* litem = dynamic_cast<QgsLegendItem *>( theItem );
-    if ( litem && litem->type() == QgsLegendItem::LEGEND_LAYER_FILE )
+    if ( litem && litem->type() == QgsLegendItem::LEGEND_LAYER )
     {
       theItem->setCheckState( 0, ( select ? Qt::Checked : Qt::Unchecked ) );
       handleItemChange( theItem, 0 );
     }
-    theItem = nextItem( theItem );
   }
+
   // Turn on rendering (if it was on previously)
   mMapCanvas->setRenderFlag( renderFlagState );
 }
@@ -168,10 +166,9 @@
     return;
   }
 
-  QTreeWidgetItem* theItem = firstItem();
   QgsDebugMsg( "called." );
 
-  while ( theItem )
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
   {
     QgsLegendItem *li = dynamic_cast<QgsLegendItem *>( theItem );
     if ( li )
@@ -188,7 +185,6 @@
       }
 
     }
-    theItem = nextItem( theItem );
   }
 
   updateMapCanvasLayerSet();
@@ -468,16 +464,17 @@
 int QgsLegend::getItemPos( QTreeWidgetItem* item )
 {
   int counter = 1;
-  QTreeWidgetItem* theItem = firstItem();
-  while ( theItem )
+
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
   {
     if ( theItem == item )
     {
       return counter;
     }
-    theItem = nextItem( theItem );
+
     ++counter;
   }
+
   return -1;
 }
 
@@ -568,6 +565,16 @@
   }
 }
 
+bool QgsLegend::setCurrentLayer( QgsMapLayer *layer )
+{
+  QgsLegendLayer *ll = findLegendLayer( layer );
+  if ( !ll )
+    return false;
+
+  setCurrentItem( ll );
+  return true;
+}
+
 void QgsLegend::legendGroupRemove()
 {
   if ( !mMapCanvas || mMapCanvas->isDrawing() )
@@ -755,21 +762,17 @@
 
 void QgsLegend::expandAll()
 {
-  QTreeWidgetItem* theItem = firstItem();
-  while ( theItem )
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
   {
     setExpanded( indexFromItem( theItem ), true );
-    theItem = nextItem( theItem );
   }
 }
 
 void QgsLegend::collapseAll()
 {
-  QTreeWidgetItem* theItem = firstItem();
-  while ( theItem )
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
   {
     setExpanded( indexFromItem( theItem ), false );
-    theItem = nextItem( theItem );
   }
 }
 
@@ -784,108 +787,106 @@
   QDomElement legendlayerfilenode;
   Qt::CheckState cstate; //check state for legend layers and legend groups
 
-  QTreeWidgetItem* currentItem = firstItem();
-  while ( currentItem )
+  for ( QTreeWidgetItem* currentItem = firstItem(); currentItem;   currentItem = nextItem( currentItem ) )
   {
     QgsLegendItem *item = dynamic_cast<QgsLegendItem *>( currentItem );
-    if ( item )
+    if ( !item )
+      continue;
+
+    switch ( item->type() )
     {
-      switch ( item->type() )
-      {
-        case QgsLegendItem::LEGEND_GROUP:
-          //make sure the legendnode is 'legend' again after a legend group
-          if ( !( item->parent() ) )
-          {
-            legendnode = tmplegendnode;
-          }
-          legendgroupnode = document.createElement( "legendgroup" );
-          legendgroupnode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
-          legendgroupnode.setAttribute( "name", item->text( 0 ) );
-          cstate = item->checkState( 0 );
-          if ( cstate == Qt::Checked )
-          {
-            legendgroupnode.setAttribute( "checked", "Qt::Checked" );
-          }
-          else if ( cstate == Qt::Unchecked )
-          {
-            legendgroupnode.setAttribute( "checked", "Qt::Unchecked" );
-          }
-          else if ( cstate == Qt::PartiallyChecked )
-          {
-            legendgroupnode.setAttribute( "checked", "Qt::PartiallyChecked" );
-          }
-          legendnode.appendChild( legendgroupnode );
-          tmplegendnode =  legendnode;
-          legendnode = legendgroupnode;
-          break;
+      case QgsLegendItem::LEGEND_GROUP:
+        //make sure the legendnode is 'legend' again after a legend group
+        if ( !( item->parent() ) )
+        {
+          legendnode = tmplegendnode;
+        }
+        legendgroupnode = document.createElement( "legendgroup" );
+        legendgroupnode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
+        legendgroupnode.setAttribute( "name", item->text( 0 ) );
+        cstate = item->checkState( 0 );
+        if ( cstate == Qt::Checked )
+        {
+          legendgroupnode.setAttribute( "checked", "Qt::Checked" );
+        }
+        else if ( cstate == Qt::Unchecked )
+        {
+          legendgroupnode.setAttribute( "checked", "Qt::Unchecked" );
+        }
+        else if ( cstate == Qt::PartiallyChecked )
+        {
+          legendgroupnode.setAttribute( "checked", "Qt::PartiallyChecked" );
+        }
+        legendnode.appendChild( legendgroupnode );
+        tmplegendnode =  legendnode;
+        legendnode = legendgroupnode;
+        break;
 
-        case QgsLegendItem::LEGEND_LAYER:
-          //make sure the legendnode is 'legend' again after a legend group
-          if ( !( item->parent() ) )
-          {
-            legendnode = tmplegendnode;
-          }
-          legendlayernode = document.createElement( "legendlayer" );
-          legendlayernode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
-          cstate = item->checkState( 0 );
-          if ( cstate == Qt::Checked )
-          {
-            legendlayernode.setAttribute( "checked", "Qt::Checked" );
-          }
-          else if ( cstate == Qt::Unchecked )
-          {
-            legendlayernode.setAttribute( "checked", "Qt::Unchecked" );
-          }
-          else if ( cstate == Qt::PartiallyChecked )
-          {
-            legendlayernode.setAttribute( "checked", "Qt::PartiallyChecked" );
-          }
-          legendlayernode.setAttribute( "name", item->text( 0 ) );
-          legendnode.appendChild( legendlayernode );
+      case QgsLegendItem::LEGEND_LAYER:
+        //make sure the legendnode is 'legend' again after a legend group
+        if ( !( item->parent() ) )
+        {
+          legendnode = tmplegendnode;
+        }
+        legendlayernode = document.createElement( "legendlayer" );
+        legendlayernode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
+        cstate = item->checkState( 0 );
+        if ( cstate == Qt::Checked )
+        {
+          legendlayernode.setAttribute( "checked", "Qt::Checked" );
+        }
+        else if ( cstate == Qt::Unchecked )
+        {
+          legendlayernode.setAttribute( "checked", "Qt::Unchecked" );
+        }
+        else if ( cstate == Qt::PartiallyChecked )
+        {
+          legendlayernode.setAttribute( "checked", "Qt::PartiallyChecked" );
+        }
+        legendlayernode.setAttribute( "name", item->text( 0 ) );
+        legendnode.appendChild( legendlayernode );
 
-          // save the information about layer
-          // emulate a legend layer file group and a legend layer file
-          // to keep it compatible with older projects
-          {
-            QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( item );
-            QgsMapLayer* layer = ll->layer();
+        // save the information about layer
+        // emulate a legend layer file group and a legend layer file
+        // to keep it compatible with older projects
+        {
+          QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( item );
+          QgsMapLayer* layer = ll->layer();
 
-            layerfilegroupnode = document.createElement( "filegroup" );
-            layerfilegroupnode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
-            layerfilegroupnode.setAttribute( "hidden", isItemHidden( item ) ? "true" : "false" );
-            legendlayernode.appendChild( layerfilegroupnode );
+          layerfilegroupnode = document.createElement( "filegroup" );
+          layerfilegroupnode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
+          layerfilegroupnode.setAttribute( "hidden", isItemHidden( item ) ? "true" : "false" );
+          legendlayernode.appendChild( layerfilegroupnode );
 
-            legendlayerfilenode = document.createElement( "legendlayerfile" );
+          legendlayerfilenode = document.createElement( "legendlayerfile" );
 
-            // layer id
-            legendlayerfilenode.setAttribute( "layerid", layer->getLayerID() );
-            layerfilegroupnode.appendChild( legendlayerfilenode );
+          // layer id
+          legendlayerfilenode.setAttribute( "layerid", layer->getLayerID() );
+          layerfilegroupnode.appendChild( legendlayerfilenode );
 
-            // visible flag
-            legendlayerfilenode.setAttribute( "visible", ll->isVisible() );
+          // visible flag
+          legendlayerfilenode.setAttribute( "visible", ll->isVisible() );
 
-            // show in overview flag
-            legendlayerfilenode.setAttribute( "isInOverview", ll->isInOverview() );
-          }
-          break;
+          // show in overview flag
+          legendlayerfilenode.setAttribute( "isInOverview", ll->isInOverview() );
+        }
+        break;
 
-        case QgsLegendItem::LEGEND_PROPERTY_GROUP:
-          legendpropertynode = document.createElement( "propertygroup" );
-          legendpropertynode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
-          legendlayernode.appendChild( legendpropertynode );
-          break;
+      case QgsLegendItem::LEGEND_PROPERTY_GROUP:
+        legendpropertynode = document.createElement( "propertygroup" );
+        legendpropertynode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
+        legendlayernode.appendChild( legendpropertynode );
+        break;
 
-        case QgsLegendItem::LEGEND_SYMBOL_GROUP:
-          legendsymbolnode = document.createElement( "symbolgroup" );
-          legendsymbolnode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
-          legendlayernode.appendChild( legendsymbolnode );
-          break;
+      case QgsLegendItem::LEGEND_SYMBOL_GROUP:
+        legendsymbolnode = document.createElement( "symbolgroup" );
+        legendsymbolnode.setAttribute( "open", isItemExpanded( item ) ? "true" : "false" );
+        legendlayernode.appendChild( legendsymbolnode );
+        break;
 
-        default: //do nothing for the leaf nodes
-          break;
-      }
+      default: //do nothing for the leaf nodes
+        break;
     }
-    currentItem = nextItem( currentItem );
   }
   return true;
 }
@@ -1118,8 +1119,7 @@
 QgsLegendLayer* QgsLegend::findLegendLayer( const QString& layerKey )
 {
   QgsLegendLayer* theLegendLayer = 0;
-  QTreeWidgetItem* theItem = firstItem();
-  do
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
   {
     theLegendLayer = dynamic_cast<QgsLegendLayer *>( theItem );
     if ( theLegendLayer ) //item is a legend layer
@@ -1130,10 +1130,29 @@
       }
     }
   }
-  while (( theItem = nextItem( theItem ) ) );
+
   return 0;
 }
 
+QgsLegendLayer* QgsLegend::findLegendLayer( const QgsMapLayer *layer )
+{
+  QgsLegendLayer* theLegendLayer = 0;
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
+  {
+    theLegendLayer = dynamic_cast<QgsLegendLayer *>( theItem );
+    if ( theLegendLayer ) //item is a legend layer
+    {
+      if ( theLegendLayer->layer() == layer )
+      {
+        return theLegendLayer;
+      }
+    }
+  }
+
+  return 0;
+}
+
+
 void QgsLegend::adjustIconSize()
 {
   if ( mPixmapWidthValues.size() > 0 && mPixmapHeightValues.size() > 0 )
@@ -1338,17 +1357,15 @@
   QList<QgsMapCanvasLayer> layers;
 
   // create list of the layers
-  QTreeWidgetItem* theItem = firstItem();
-  while ( theItem )
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
   {
     QgsLegendItem *li = dynamic_cast<QgsLegendItem *>( theItem );
-    QgsLegendLayer* ll = qobject_cast<QgsLegendLayer *>( li );
+    QgsLegendLayer *ll = qobject_cast<QgsLegendLayer *>( li );
     if ( ll )
     {
       QgsMapCanvasLayer& lyr = ll->canvasLayer();
       layers.append( lyr );
     }
-    theItem = nextItem( theItem );
   }
 
   // set layers in canvas
@@ -1362,15 +1379,14 @@
 
 void QgsLegend::enableOverviewModeAllLayers( bool isInOverview )
 {
-  QTreeWidgetItem* theItem = firstItem();
-  while ( theItem )
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
   {
     QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer *>( theItem );
     if ( ll )
     {
       ll->setInOverview( isInOverview );
     }
-    theItem = nextItem( theItem );
+
   }
   updateMapCanvasLayerSet();
   updateOverview();
@@ -1379,8 +1395,7 @@
 std::deque<QString> QgsLegend::layerIDs()
 {
   std::deque<QString> layers;
-  QTreeWidgetItem* theItem = firstItem();
-  while ( theItem )
+  for ( QTreeWidgetItem* theItem = firstItem(); theItem; theItem = nextItem( theItem ) )
   {
     QgsLegendItem *li = dynamic_cast<QgsLegendItem *>( theItem );
     QgsLegendLayer* ll = qobject_cast<QgsLegendLayer *>( li );
@@ -1389,7 +1404,6 @@
       QgsMapLayer *lyr = ll->layer();
       layers.push_front( lyr->getLayerID() );
     }
-    theItem = nextItem( theItem );
   }
 
 #ifdef QGISDEBUG
@@ -1581,7 +1595,7 @@
 
 void QgsLegend::legendLayerZoom()
 {
-  if(!mMapCanvas)
+  if ( !mMapCanvas )
   {
     return;
   }
@@ -1595,12 +1609,12 @@
   QgsRectangle extent = theLayer->extent();
 
   //transform extent if otf-projection is on
-  if(mMapCanvas->hasCrsTransformEnabled())
+  if ( mMapCanvas->hasCrsTransformEnabled() )
   {
     QgsMapRenderer* renderer = mMapCanvas->mapRenderer();
-    if(renderer)
+    if ( renderer )
     {
-      extent = renderer->layerExtentToOutputExtent(theLayer, extent);
+      extent = renderer->layerExtentToOutputExtent( theLayer, extent );
     }
   }
 

Modified: trunk/qgis/src/app/legend/qgslegend.h
===================================================================
--- trunk/qgis/src/app/legend/qgslegend.h	2009-12-07 18:18:30 UTC (rev 12357)
+++ trunk/qgis/src/app/legend/qgslegend.h	2009-12-07 18:59:25 UTC (rev 12358)
@@ -103,6 +103,10 @@
     Else, 0 is returned.*/
     QgsMapLayer* currentLayer();
 
+    /*!set the current layer
+    returns true if the layer exists, false otherwise*/
+    bool setCurrentLayer( QgsMapLayer *layer );
+
     /**Writes the content of the legend to a project file*/
     bool writeXML( QDomNode & layer_node, QDomDocument & document );
 
@@ -285,9 +289,12 @@
     /**Moves an item back to the position where storeInitialPosition has been called*/
     void resetToInitialPosition( QTreeWidgetItem* li );
 
-    /**Returns the legend layer to which a map layer gelongs*/
+    /**Returns the legend layer to which a map layer belongs to*/
     QgsLegendLayer* findLegendLayer( const QString& layerKey );
 
+    /**Returns the legend layer to which a map layer belongs to*/
+    QgsLegendLayer* findLegendLayer( const QgsMapLayer *layer );
+
     /**Checks mPixmapWidthValues and mPixmapHeightValues and sets a new icon size if necessary*/
     void adjustIconSize();
 

Modified: trunk/qgis/src/app/legend/qgslegenditem.h
===================================================================
--- trunk/qgis/src/app/legend/qgslegenditem.h	2009-12-07 18:18:30 UTC (rev 12357)
+++ trunk/qgis/src/app/legend/qgslegenditem.h	2009-12-07 18:59:25 UTC (rev 12358)
@@ -52,9 +52,7 @@
       LEGEND_SYMBOL_GROUP,
       LEGEND_SYMBOL_ITEM,
       LEGEND_VECTOR_SYMBOL_ITEM,
-      LEGEND_LAYER_FILE_GROUP,
-      LEGEND_LAYER_FILE
-    } ;
+    };
 
     /**Describes the action that will be done if the mouse button will be released after a drag*/
     enum DRAG_ACTION

Modified: trunk/qgis/src/app/qgisapp.cpp
===================================================================
--- trunk/qgis/src/app/qgisapp.cpp	2009-12-07 18:18:30 UTC (rev 12357)
+++ trunk/qgis/src/app/qgisapp.cpp	2009-12-07 18:59:25 UTC (rev 12358)
@@ -4567,9 +4567,15 @@
 /** Get a pointer to the currently selected map layer */
 QgsMapLayer *QgisApp::activeLayer()
 {
-  return ( mMapLegend->currentLayer() );
+  return mMapLegend->currentLayer();
 }
 
+/** set the current layer */
+bool QgisApp::setActiveLayer( QgsMapLayer *layer )
+{
+  return mMapLegend->setCurrentLayer( layer );
+}
+
 /** Add a vector layer directly without prompting user for location
   The caller must provide information compatible with the provider plugin
   using the vectorLayerPath and baseName. The provider can use these

Modified: trunk/qgis/src/app/qgisapp.h
===================================================================
--- trunk/qgis/src/app/qgisapp.h	2009-12-07 18:18:30 UTC (rev 12357)
+++ trunk/qgis/src/app/qgisapp.h	2009-12-07 18:59:25 UTC (rev 12358)
@@ -474,6 +474,8 @@
     */
     //! Return pointer to the active layer
     QgsMapLayer *activeLayer();
+    //! set the active layer
+    bool setActiveLayer( QgsMapLayer * );
     //! Open the help contents in a browser
     void helpContents();
     //! Open the QGIS homepage in users browser

Modified: trunk/qgis/src/app/qgisappinterface.cpp
===================================================================
--- trunk/qgis/src/app/qgisappinterface.cpp	2009-12-07 18:18:30 UTC (rev 12357)
+++ trunk/qgis/src/app/qgisappinterface.cpp	2009-12-07 18:59:25 UTC (rev 12358)
@@ -17,7 +17,6 @@
  ***************************************************************************/
 /* $Id$ */
 
-#include <iostream>
 #include <QFileInfo>
 #include <QString>
 #include <QMenu>
@@ -108,6 +107,11 @@
   return qgis->activeLayer();
 }
 
+bool QgisAppInterface::setActiveLayer( QgsMapLayer *layer )
+{
+  return qgis->setActiveLayer( layer );
+}
+
 void QgisAppInterface::addPluginToMenu( QString name, QAction* action )
 {
   qgis->addPluginToMenu( name, action );

Modified: trunk/qgis/src/app/qgisappinterface.h
===================================================================
--- trunk/qgis/src/app/qgisappinterface.h	2009-12-07 18:18:30 UTC (rev 12357)
+++ trunk/qgis/src/app/qgisappinterface.h	2009-12-07 18:59:25 UTC (rev 12358)
@@ -68,6 +68,9 @@
     //! Get pointer to the active layer (layer selected in the legend)
     QgsMapLayer *activeLayer();
 
+    //! set the active layer (layer selected in the legend)
+    bool setActiveLayer( QgsMapLayer *layer );
+
     //! Add an icon to the plugins toolbar
     int addToolBarIcon( QAction *qAction );
     //! Remove an icon (action) from the plugin toolbar

Modified: trunk/qgis/src/gui/qgisinterface.h
===================================================================
--- trunk/qgis/src/gui/qgisinterface.h	2009-12-07 18:18:30 UTC (rev 12357)
+++ trunk/qgis/src/gui/qgisinterface.h	2009-12-07 18:59:25 UTC (rev 12358)
@@ -95,6 +95,11 @@
     //! Get pointer to the active layer (layer selected in the legend)
     virtual QgsMapLayer *activeLayer() = 0;
 
+    //! Set the active layer (layer gets selected in the legend)
+    //! returns true if the layer exists, false otherwise
+    //! added in 1.4
+    virtual bool setActiveLayer( QgsMapLayer * ) = 0;
+
     //! Add an icon to the plugins toolbar
     virtual int addToolBarIcon( QAction *qAction ) = 0;
 



More information about the QGIS-commit mailing list