[QGIS Commit] r12359 - 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 16:03:49 EST 2009


Author: wonder
Date: 2009-12-07 16:03:45 -0500 (Mon, 07 Dec 2009)
New Revision: 12359

Added:
   trunk/qgis/python/gui/qgslegendinterface.sip
   trunk/qgis/src/app/legend/qgsapplegendinterface.cpp
   trunk/qgis/src/app/legend/qgsapplegendinterface.h
   trunk/qgis/src/gui/qgslegendinterface.cpp
   trunk/qgis/src/gui/qgslegendinterface.h
Modified:
   trunk/qgis/CONTRIBUTORS
   trunk/qgis/python/gui/gui.sip
   trunk/qgis/python/gui/qgisinterface.sip
   trunk/qgis/src/app/CMakeLists.txt
   trunk/qgis/src/app/legend/qgslegend.cpp
   trunk/qgis/src/app/legend/qgslegend.h
   trunk/qgis/src/app/qgisappinterface.cpp
   trunk/qgis/src/app/qgisappinterface.h
   trunk/qgis/src/gui/CMakeLists.txt
   trunk/qgis/src/gui/qgisinterface.h
Log:
[FEATURE] Applied patch from Andres Manz from #2185
Adds QgsLegendInterface class to GUI library to allow users to do some operations with groups.


Modified: trunk/qgis/CONTRIBUTORS
===================================================================
--- trunk/qgis/CONTRIBUTORS	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/CONTRIBUTORS	2009-12-07 21:03:45 UTC (rev 12359)
@@ -40,3 +40,4 @@
 Anita Graser
 Richard Duivenvoorde
 Alexander Bruy
+Andres Manz

Modified: trunk/qgis/python/gui/gui.sip
===================================================================
--- trunk/qgis/python/gui/gui.sip	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/python/gui/gui.sip	2009-12-07 21:03:45 UTC (rev 12359)
@@ -7,6 +7,7 @@
 
 %Import core/core.sip
 
+%Include qgslegendinterface.sip
 %Include qgisinterface.sip
 %Include qgscomposerview.sip
 %Include qgsencodingfiledialog.sip

Modified: trunk/qgis/python/gui/qgisinterface.sip
===================================================================
--- trunk/qgis/python/gui/qgisinterface.sip	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/python/gui/qgisinterface.sip	2009-12-07 21:03:45 UTC (rev 12359)
@@ -25,6 +25,10 @@
     /** Virtual destructor */
     virtual ~QgisInterface();
     
+    /** Get pointer to legend interface
+      \note added in 1.4
+     */
+    virtual QgsLegendInterface* legendInterface()=0;
 
   public slots: // TODO: do these functions really need to be slots?
 

Added: trunk/qgis/python/gui/qgslegendinterface.sip
===================================================================
--- trunk/qgis/python/gui/qgslegendinterface.sip	                        (rev 0)
+++ trunk/qgis/python/gui/qgslegendinterface.sip	2009-12-07 21:03:45 UTC (rev 12359)
@@ -0,0 +1,37 @@
+/**
+ * \class QgsLegendInterface
+ * \brief Abstract base class to make QgsLegend available to plugins.
+ */
+class QgsLegendInterface : QObject
+{
+%TypeHeaderCode
+#include <qgslegendinterface.h>
+%End
+
+  public:
+
+    /** Constructor */
+    QgsLegendInterface();
+
+    /** Virtual destructor */
+    ~QgsLegendInterface();
+
+    virtual QStringList groups() =0;
+
+  signals:
+
+    //! emitted when a group index has changed
+    void groupIndexChanged( int oldIndex, int newIndex );
+
+  public slots:
+
+    //! Add a new group
+    virtual int addGroup( QString name, bool expand = true ) =0;
+
+    //! Remove group on index
+    virtual void removeGroup( int groupIndex ) =0;
+
+    //! Move a layer to a group
+    virtual void moveLayer( QgsMapLayer * layer, int groupIndex ) =0;
+};
+

Modified: trunk/qgis/src/app/CMakeLists.txt
===================================================================
--- trunk/qgis/src/app/CMakeLists.txt	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/src/app/CMakeLists.txt	2009-12-07 21:03:45 UTC (rev 12359)
@@ -88,6 +88,7 @@
 
   legend/qgslegendgroup.cpp
   legend/qgslegend.cpp
+  legend/qgsapplegendinterface.cpp
   legend/qgslegenditem.cpp
   legend/qgslegendlayer.cpp
   legend/qgslegendpropertygroup.cpp
@@ -185,6 +186,7 @@
   composer/qgsitempositiondialog.h
 
   legend/qgslegend.h
+  legend/qgsapplegendinterface.h
   legend/qgslegendlayer.h
   
   ogr/qgsopenvectorlayerdialog.h

Added: trunk/qgis/src/app/legend/qgsapplegendinterface.cpp
===================================================================
--- trunk/qgis/src/app/legend/qgsapplegendinterface.cpp	                        (rev 0)
+++ trunk/qgis/src/app/legend/qgsapplegendinterface.cpp	2009-12-07 21:03:45 UTC (rev 12359)
@@ -0,0 +1,58 @@
+/***************************************************************************
+    qgsapplegendinterface.cpp
+     --------------------------------------
+    Date                 : 19-Nov-2009
+    Copyright            : (C) 2009 by Andres Manz
+    Email                : manz dot andres at gmail dot com
+****************************************************************************/
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+/* $Id$ */
+
+#include "qgsapplegendinterface.h"
+
+#include "qgslegend.h"
+
+
+QgsAppLegendInterface::QgsAppLegendInterface( QgsLegend * legend )
+    : mLegend( legend )
+{
+}
+
+QgsAppLegendInterface::~QgsAppLegendInterface()
+{
+}
+
+int QgsAppLegendInterface::addGroup( QString name, bool expand )
+{
+  return mLegend->addGroup( name, expand );
+}
+
+void QgsAppLegendInterface::removeGroup( int groupIndex )
+{
+  mLegend->removeGroup( groupIndex );
+}
+
+void QgsAppLegendInterface::moveLayer( QgsMapLayer * ml, int groupIndex )
+{
+  mLegend->moveLayer( ml, groupIndex );
+}
+
+void QgsAppLegendInterface::updateIndex( const QModelIndex &oldIndex, const QModelIndex& newIndex)
+{
+  if ( mLegend->isLegendGroup( newIndex ) )
+  {
+    emit groupIndexChanged( oldIndex.row(), newIndex.row() );
+  }
+}
+
+QStringList QgsAppLegendInterface::groups()
+{
+  return mLegend->groups();
+}

Added: trunk/qgis/src/app/legend/qgsapplegendinterface.h
===================================================================
--- trunk/qgis/src/app/legend/qgsapplegendinterface.h	                        (rev 0)
+++ trunk/qgis/src/app/legend/qgsapplegendinterface.h	2009-12-07 21:03:45 UTC (rev 12359)
@@ -0,0 +1,66 @@
+/***************************************************************************
+    qgsapplegendinterface.h
+     --------------------------------------
+    Date                 : 23-Nov-2009
+    Copyright            : (C) 2009 by Andres Manz
+    Email                : manz dot andres at gmail dot com
+****************************************************************************/
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+/* $Id$ */
+
+#ifndef QGSLEGENDAPPIFACE_H
+#define QGSLEGENDAPPIFACE_H
+
+#include "qgslegendinterface.h"
+
+class QModelIndex;
+class QgsLegend;
+class QgsMapLayer;
+
+/** \ingroup gui
+ * QgsLegendInterface
+ * Abstract base class to make QgsLegend available to plugins.
+ */
+class QgsAppLegendInterface : public QgsLegendInterface
+{
+    Q_OBJECT
+
+  public:
+
+    /** Constructor */
+    explicit QgsAppLegendInterface( QgsLegend * legend );
+
+    /** Virtual destructor */
+    ~QgsAppLegendInterface();
+
+    //! Return a string list of groups
+    QStringList groups();
+
+  public slots:
+
+    //! Add a new group
+    int addGroup( QString name, bool expand = true );
+
+    //! Remove all groups with the given name
+    void removeGroup( int groupIndex );
+
+    //! Move a layer to a group
+    void moveLayer( QgsMapLayer * ml, int groupIndex );
+
+    //! Update an index
+    void updateIndex( const QModelIndex &oldIndex, const QModelIndex &newIndex );
+
+  private:
+
+    //! Pointer to QgsLegend object
+    QgsLegend *mLegend;
+};
+
+#endif //QGSLEGENDAPPIFACE_H

Modified: trunk/qgis/src/app/legend/qgslegend.cpp
===================================================================
--- trunk/qgis/src/app/legend/qgslegend.cpp	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/src/app/legend/qgslegend.cpp	2009-12-07 21:03:45 UTC (rev 12359)
@@ -116,11 +116,15 @@
   emit currentLayerChanged( layer );
 }
 
-void QgsLegend::addGroup()
+int QgsLegend::addGroup( QString name, bool expand )
 {
-  QgsLegendGroup* group = new QgsLegendGroup( this, tr( "group" ) );
+  if ( name.isEmpty() )
+    name = tr( "group" ); // some default name if none specified
+  QgsLegendGroup* group = new QgsLegendGroup( this, name );
   group->setData( 0, Qt::UserRole, Qt::Checked );
-  setExpanded( indexFromItem( group ), true );
+  QModelIndex groupIndex = indexFromItem( group );
+  setExpanded( groupIndex, expand );
+  return groupIndex.row();
 }
 
 void QgsLegend::removeAll()
@@ -159,6 +163,15 @@
   mMapCanvas->setRenderFlag( renderFlagState );
 }
 
+void QgsLegend::removeGroup( int groupIndex )
+{
+  QgsLegendGroup * lg = dynamic_cast<QgsLegendGroup *>( topLevelItem( groupIndex ) );
+  if ( lg )
+  {
+    removeGroup( lg );
+  }
+}
+
 void QgsLegend::removeLayer( QString layer_key )
 {
   if ( !mMapCanvas || mMapCanvas->isDrawing() )
@@ -351,6 +364,7 @@
 
   QgsLegendItem* origin = dynamic_cast<QgsLegendItem *>( mItemBeingMoved );
   mItemBeingMoved = NULL;
+  QModelIndex oldIndex = indexFromItem( origin );
 
   QgsLegendItem* dest = dynamic_cast<QgsLegendItem *>( destItem );
 
@@ -371,6 +385,7 @@
       {
         moveItem( origin, dest );
         setCurrentItem( origin );
+        emit itemMoved( oldIndex, indexFromItem( origin ) );
       }
     }
     else if ( mDropAction == BEFORE )// below center of item
@@ -381,6 +396,7 @@
         moveItem( origin, dest ); // Insert after, as above...
         moveItem( dest, origin ); // ... and then switch places!
         setCurrentItem( origin );
+        emit itemMoved( oldIndex, indexFromItem( origin ) );
       }
     }
     else if ( mDropAction == INTO_GROUP )
@@ -390,6 +406,7 @@
       {
         insertItem( origin, dest );
         setCurrentItem( origin );
+        emit itemMoved( oldIndex, indexFromItem( origin ) );
       }
     }
     else//no action
@@ -585,19 +602,29 @@
   QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( currentItem() );
   if ( lg )
   {
-    //delete the legend layers first
-    QTreeWidgetItem * child = lg->child( 0 );
-    while ( child )
-    {
-      setCurrentItem( child );
-      removeCurrentLayer();
-      child = lg->child( 0 );
-    }
-    delete lg;
-    adjustIconSize();
+    removeGroup( lg );
   }
 }
 
+void QgsLegend::removeGroup( QgsLegendGroup * lg )
+{
+  if ( !mMapCanvas || mMapCanvas->isDrawing() )
+  {
+    return;
+  }
+
+  //delete the legend layers first
+  QTreeWidgetItem * child = lg->child( 0 );
+  while ( child )
+  {
+    setCurrentItem( child );
+    removeCurrentLayer();
+    child = lg->child( 0 );
+  }
+  delete lg;
+  adjustIconSize();
+}
+
 void QgsLegend::removeCurrentLayer()
 {
   if ( !mMapCanvas || mMapCanvas->isDrawing() )
@@ -668,8 +695,16 @@
   return true;
 }
 
+void QgsLegend::moveLayer( QgsMapLayer * ml, int groupIndex )
+{
+  QgsLegendLayer *layer = findLegendLayer( ml->getLayerID() );
+  QgsLegendGroup *group = dynamic_cast<QgsLegendGroup*>( topLevelItem( groupIndex ) );
+  if ( layer && group )
+  {
+    insertItem( layer, group );
+  }
+}
 
-
 void QgsLegend::legendLayerShowProperties()
 {
   if ( !mMapCanvas || mMapCanvas->isDrawing() )
@@ -1199,6 +1234,30 @@
   }
 }
 
+bool QgsLegend::isLegendGroup( const QModelIndex &index )
+{
+  return dynamic_cast<QgsLegendGroup *>( itemFromIndex( index ) );
+}
+
+QStringList QgsLegend::groups()
+{
+  QStringList groupList;
+  QTreeWidgetItem *current = firstItem();
+
+  while ( current )
+  {
+    QgsLegendGroup *group = dynamic_cast<QgsLegendGroup *>( current );
+    if ( group )
+    {
+      groupList.append( group->text( 0 ) );
+    }
+
+    current = nextItem( current );
+  }
+
+  return groupList;
+}
+
 /**Returns the first item in the hierarchy*/
 QTreeWidgetItem* QgsLegend::firstItem()
 {

Modified: trunk/qgis/src/app/legend/qgslegend.h
===================================================================
--- trunk/qgis/src/app/legend/qgslegend.h	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/src/app/legend/qgslegend.h	2009-12-07 21:03:45 UTC (rev 12359)
@@ -25,6 +25,7 @@
 #include <set>
 #include <QTreeWidget>
 
+class QgsLegendGroup;
 class QgsLegendLayer;
 class QgsLegendItem;
 class QgsMapLayer;
@@ -116,6 +117,12 @@
     /**Returns true, if the y-coordinate is >= the center of the item*/
     bool yCoordAboveCenter( QgsLegendItem* it, int ycoord );
 
+    /**Returns true, if the item at index is a QgsLegendGroup*/
+    bool isLegendGroup( const QModelIndex &index );
+
+    /**Returns a string list of groups*/
+    QStringList groups();
+
     /**Returns the first item in the hierarchy*/
     QTreeWidgetItem* firstItem();
 
@@ -202,9 +209,19 @@
     /*!
      * Slot called when user wishes to add a new empty layer group to the legend.
      * The user will be prompted for the name of the newly added group.
+     * @param name name of the new group
+     * @param expand expand the group
      * @return void
      */
-    void addGroup();
+    int addGroup( QString name = QString(), bool expand = true );
+
+    /*!
+     * Removes all groups with the given name.
+     * @param name name of the groups to remove
+     * @return void
+     */
+    void removeGroup( int groupIndex );
+
     void removeLayer( QString );
 
     /** called to read legend settings from project */
@@ -223,6 +240,14 @@
       @param return false if canceled or in case of error, true else*/
     bool removeLayer( QgsMapLayer* ml, bool askCancelOnEditable );
 
+    /*!
+     * Moves a layer to a group.
+     * @param ml the maplayer to move
+     * @param groupIndex index of group
+     * @return false if the group does not exist, false otherwise
+     */
+    void moveLayer( QgsMapLayer* ml, int groupIndex );
+
     /**Toggle show in overview for current layer*/
     void legendLayerShowInOverview();
 
@@ -338,6 +363,8 @@
     void handleRightClickEvent( QTreeWidgetItem* item, const QPoint& position );
     /**Removes the current legend group*/
     void legendGroupRemove();
+    /**Removes a legend group and its layers*/
+    void removeGroup( QgsLegendGroup * lg );
     /**Sets all listview items to open*/
     void expandAll();
     /**Sets all listview items to closed*/
@@ -444,6 +471,8 @@
     QWidget *mInsertionLine;
 
   signals:
+    void itemMoved( QModelIndex oldIndex, QModelIndex newIndex );
+
     void zOrderChanged( QgsLegend * lv );
 
     //! Emited whenever current (selected) layer changes

Modified: trunk/qgis/src/app/qgisappinterface.cpp
===================================================================
--- trunk/qgis/src/app/qgisappinterface.cpp	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/src/app/qgisappinterface.cpp	2009-12-07 21:03:45 UTC (rev 12359)
@@ -31,7 +31,8 @@
 #include "qgsshortcutsmanager.h"
 
 QgisAppInterface::QgisAppInterface( QgisApp * _qgis )
-    : qgis( _qgis )
+    : qgis( _qgis ),
+    legendIface( _qgis->legend() )
 {
   // connect signals
   connect( qgis->legend(), SIGNAL( currentLayerChanged( QgsMapLayer * ) ),
@@ -45,6 +46,11 @@
 {
 }
 
+QgsLegendInterface* QgisAppInterface::legendInterface()
+{
+  return &legendIface;
+}
+
 void QgisAppInterface::zoomFull()
 {
   qgis->zoomFull();

Modified: trunk/qgis/src/app/qgisappinterface.h
===================================================================
--- trunk/qgis/src/app/qgisappinterface.h	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/src/app/qgisappinterface.h	2009-12-07 21:03:45 UTC (rev 12359)
@@ -20,6 +20,7 @@
 #define QGISIFACE_H
 
 #include "qgisinterface.h"
+#include "qgsapplegendinterface.h"
 
 class QgisApp;
 
@@ -42,6 +43,8 @@
     QgisAppInterface( QgisApp *qgisapp );
     ~QgisAppInterface();
 
+    QgsLegendInterface* legendInterface();
+
     /* Exposed functions */
     //! Zoom map to full extent
     void zoomFull();
@@ -261,6 +264,9 @@
 
     //! Pointer to the QgisApp object
     QgisApp *qgis;
+
+    //! Pointer to the LegendInterface object
+    QgsAppLegendInterface legendIface;
 };
 
 

Modified: trunk/qgis/src/gui/CMakeLists.txt
===================================================================
--- trunk/qgis/src/gui/CMakeLists.txt	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/src/gui/CMakeLists.txt	2009-12-07 21:03:45 UTC (rev 12359)
@@ -19,6 +19,7 @@
 
 qgisgui.cpp
 qgisinterface.cpp
+qgslegendinterface.cpp
 qgscolorbutton.cpp
 qgscomposerview.cpp
 qgscursors.cpp
@@ -65,6 +66,7 @@
 qgscomposerview.h
 qgsdetaileditemdelegate.h
 qgsdetaileditemwidget.h
+qgslegendinterface.h
 qgisinterface.h
 qgsencodingfiledialog.h
 qgsgenericprojectionselector.h

Modified: trunk/qgis/src/gui/qgisinterface.h
===================================================================
--- trunk/qgis/src/gui/qgisinterface.h	2009-12-07 18:59:25 UTC (rev 12358)
+++ trunk/qgis/src/gui/qgisinterface.h	2009-12-07 21:03:45 UTC (rev 12359)
@@ -37,6 +37,7 @@
 class QgsMapCanvas;
 class QgsRasterLayer;
 class QgsVectorLayer;
+class QgsLegendInterface;
 
 /** \ingroup gui
  * QgisInterface
@@ -62,7 +63,12 @@
     /** Virtual destructor */
     virtual ~QgisInterface();
 
+    /** Get pointer to legend interface
+      \note added in 1.4
+     */
+    virtual QgsLegendInterface* legendInterface() = 0;
 
+
   public slots: // TODO: do these functions really need to be slots?
 
     //! Zoom to full extent of map layers

Added: trunk/qgis/src/gui/qgslegendinterface.cpp
===================================================================
--- trunk/qgis/src/gui/qgslegendinterface.cpp	                        (rev 0)
+++ trunk/qgis/src/gui/qgslegendinterface.cpp	2009-12-07 21:03:45 UTC (rev 12359)
@@ -0,0 +1,27 @@
+/***************************************************************************
+    qgslegendinterface.cpp
+     --------------------------------------
+    Date                 : 19-Nov-2009
+    Copyright            : (C) 2009 by Andres Manz
+    Email                : manz dot andres at gmail dot com
+****************************************************************************/
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+/* $Id$ */
+
+#include "qgslegendinterface.h"
+
+QgsLegendInterface::QgsLegendInterface()
+{
+}
+
+QgsLegendInterface::~QgsLegendInterface()
+{
+}
+

Added: trunk/qgis/src/gui/qgslegendinterface.h
===================================================================
--- trunk/qgis/src/gui/qgslegendinterface.h	                        (rev 0)
+++ trunk/qgis/src/gui/qgslegendinterface.h	2009-12-07 21:03:45 UTC (rev 12359)
@@ -0,0 +1,64 @@
+/***************************************************************************
+    qgslegendinterface.h
+     --------------------------------------
+    Date                 : 19-Nov-2009
+    Copyright            : (C) 2009 by Andres Manz
+    Email                : manz dot andres at gmail dot com
+****************************************************************************/
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+/* $Id$ */
+
+#ifndef QGSLEGENDINTERFACE_H
+#define QGSLEGENDINTERFACE_H
+
+#include <QObject>
+#include <QStringList>
+
+class QgsMapLayer;
+
+/** \ingroup gui
+ * QgsLegendInterface
+ * Abstract base class to make QgsLegend available to plugins.
+ *
+ * \note added in 1.4
+ */
+class GUI_EXPORT QgsLegendInterface : public QObject
+{
+    Q_OBJECT
+
+  public:
+
+    /** Constructor */
+    QgsLegendInterface();
+
+    /** Virtual destructor */
+    virtual ~QgsLegendInterface();
+
+    //! Return a string list of groups
+    virtual QStringList groups() = 0;
+
+  signals:
+
+    //! emitted when a group index has changed
+    void groupIndexChanged( int oldIndex, int newIndex );
+
+  public slots:
+
+    //! Add a new group
+    virtual int addGroup( QString name, bool expand = true ) = 0;
+
+    //! Remove group on index
+    virtual void removeGroup( int groupIndex ) = 0;
+
+    //! Move a layer to a group
+    virtual void moveLayer( QgsMapLayer * ml, int groupIndex ) = 0;
+};
+
+#endif



More information about the QGIS-commit mailing list