Index: python/gui/qgslegendinterface.sip =================================================================== --- python/gui/qgslegendinterface.sip (revision 0) +++ python/gui/qgslegendinterface.sip (revision 0) @@ -0,0 +1,34 @@ +/** + * \class QgsLegendInterface + * \brief Abstract base class to make QgsLegend available to plugins. + */ +class QgsLegendInterface : QObject +{ +%TypeHeaderCode +#include +%End + + public: + + /** Constructor */ + QgsLegendInterface(); + + /** Virtual destructor */ + ~QgsLegendInterface(); + + virtual QStringList groups() =0; + + signals: + + void groupIndexChanged( int oldIndex, int newIndex ); + + public slots: + + //! Add a new group + virtual int addGroup( QString name = "group", bool expand = true ) =0; + + virtual void removeGroup( int groupIndex ) =0; + + virtual void moveLayer( QgsMapLayer * layer, int groupIndex ) =0; +}; + Index: python/gui/qgisinterface.sip =================================================================== --- python/gui/qgisinterface.sip (revision 12307) +++ python/gui/qgisinterface.sip (working copy) @@ -25,6 +25,7 @@ /** Virtual destructor */ virtual ~QgisInterface(); + virtual QgsLegendInterface* legendInterface()=0; public slots: // TODO: do these functions really need to be slots? Index: python/gui/gui.sip =================================================================== --- python/gui/gui.sip (revision 12307) +++ python/gui/gui.sip (working copy) @@ -7,6 +7,7 @@ %Import core/core.sip +%Include qgslegendinterface.sip %Include qgisinterface.sip %Include qgscomposerview.sip %Include qgsencodingfiledialog.sip Index: src/app/legend/qgsapplegendinterface.h =================================================================== --- src/app/legend/qgsapplegendinterface.h (revision 0) +++ src/app/legend/qgsapplegendinterface.h (revision 0) @@ -0,0 +1,64 @@ +/*************************************************************************** + qgsapplegendinterface.h - description + -------------------------------------- + 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" +#include "qgslegend.h" +#include "qgsmaplayer.h" + +/** \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 = "group", 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 Index: src/app/legend/qgslegend.cpp =================================================================== --- src/app/legend/qgslegend.cpp (revision 12307) +++ src/app/legend/qgslegend.cpp (working copy) @@ -116,11 +116,13 @@ emit currentLayerChanged( layer ); } -void QgsLegend::addGroup() +int QgsLegend::addGroup( QString name, bool expand ) { - QgsLegendGroup* group = new QgsLegendGroup( this, tr( "group" ) ); + 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() @@ -161,6 +163,15 @@ mMapCanvas->setRenderFlag( renderFlagState ); } +void QgsLegend::removeGroup( int groupIndex ) +{ + QgsLegendGroup * lg = dynamic_cast( topLevelItem( groupIndex ) ); + if( lg ) + { + removeGroup( lg ); + } +} + void QgsLegend::removeLayer( QString layer_key ) { if ( !mMapCanvas || mMapCanvas->isDrawing() ) @@ -355,6 +366,7 @@ QgsLegendItem* origin = dynamic_cast( mItemBeingMoved ); mItemBeingMoved = NULL; + QModelIndex oldIndex = indexFromItem( origin ); QgsLegendItem* dest = dynamic_cast( destItem ); @@ -375,6 +387,7 @@ { moveItem( origin, dest ); setCurrentItem( origin ); + emit itemMoved( oldIndex, indexFromItem( origin ) ); } } else if ( mDropAction == BEFORE )// below center of item @@ -385,6 +398,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 ) @@ -394,6 +408,7 @@ { insertItem( origin, dest ); setCurrentItem( origin ); + emit itemMoved( oldIndex, indexFromItem( origin ) ); } } else//no action @@ -578,19 +593,29 @@ QgsLegendGroup* lg = dynamic_cast( 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() ) @@ -661,8 +686,16 @@ return true; } +void QgsLegend::moveLayer( QgsMapLayer * ml, int groupIndex ) +{ + QgsLegendLayer *layer = findLegendLayer( ml->getLayerID() ); + QgsLegendGroup *group = dynamic_cast( topLevelItem( groupIndex ) ); + if ( layer && group ) + { + insertItem( layer, group ); + } +} - void QgsLegend::legendLayerShowProperties() { if ( !mMapCanvas || mMapCanvas->isDrawing() ) @@ -1180,6 +1213,30 @@ } } +bool QgsLegend::isLegendGroup( const QModelIndex &index ) +{ + return dynamic_cast( itemFromIndex( index ) ); +} + +QStringList QgsLegend::groups() +{ + QStringList groupList; + QTreeWidgetItem *current = firstItem(); + + while ( current ) + { + QgsLegendGroup *group = dynamic_cast( current ); + if ( group ) + { + groupList.append( group->text( 0 ) ); + } + + current = nextItem( current ); + } + + return groupList; +} + /**Returns the first item in the hierarchy*/ QTreeWidgetItem* QgsLegend::firstItem() { Index: src/app/legend/qgslegend.h =================================================================== --- src/app/legend/qgslegend.h (revision 12307) +++ src/app/legend/qgslegend.h (working copy) @@ -25,6 +25,7 @@ #include #include +class QgsLegendGroup; class QgsLegendLayer; class QgsLegendItem; class QgsMapLayer; @@ -112,6 +113,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(); @@ -198,9 +205,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 = "group", 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 */ @@ -219,6 +236,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(); @@ -331,6 +356,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*/ @@ -437,6 +464,8 @@ QWidget *mInsertionLine; signals: + void itemMoved( QModelIndex oldIndex, QModelIndex newIndex ); + void zOrderChanged( QgsLegend * lv ); //! Emited whenever current (selected) layer changes Index: src/app/legend/qgsapplegendinterface.cpp =================================================================== --- src/app/legend/qgsapplegendinterface.cpp (revision 0) +++ src/app/legend/qgsapplegendinterface.cpp (revision 0) @@ -0,0 +1,55 @@ +/*************************************************************************** + qgslegendinterface.h - description + -------------------------------------- + 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" + +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(); +} Index: src/app/qgisappinterface.h =================================================================== --- src/app/qgisappinterface.h (revision 12307) +++ src/app/qgisappinterface.h (working copy) @@ -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(); @@ -258,6 +261,9 @@ //! Pointer to the QgisApp object QgisApp *qgis; + + //! Pointer to the LegendInterface object + QgsAppLegendInterface legendIface; }; Index: src/app/CMakeLists.txt =================================================================== --- src/app/CMakeLists.txt (revision 12307) +++ src/app/CMakeLists.txt (working copy) @@ -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 Index: src/app/qgisappinterface.cpp =================================================================== --- src/app/qgisappinterface.cpp (revision 12307) +++ src/app/qgisappinterface.cpp (working copy) @@ -32,7 +32,8 @@ #include "qgsshortcutsmanager.h" QgisAppInterface::QgisAppInterface( QgisApp * _qgis ) - : qgis( _qgis ) + : qgis( _qgis ), + legendIface( _qgis->legend() ) { // connect signals connect( qgis->legend(), SIGNAL( currentLayerChanged( QgsMapLayer * ) ), @@ -46,6 +47,11 @@ { } +QgsLegendInterface* QgisAppInterface::legendInterface() +{ + return &legendIface; +} + void QgisAppInterface::zoomFull() { qgis->zoomFull(); Index: src/gui/qgslegendinterface.h =================================================================== --- src/gui/qgslegendinterface.h (revision 0) +++ src/gui/qgslegendinterface.h (revision 0) @@ -0,0 +1,61 @@ +/*************************************************************************** + qgslegendinterface.h - description + -------------------------------------- + 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 +#include + +class QgsMapLayer; + +/** \ingroup gui + * QgsLegendInterface + * Abstract base class to make QgsLegend available to plugins. + */ +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: + + void groupIndexChanged( int oldIndex, int newIndex ); + + public slots: + + //! Add a new group + virtual int addGroup( QString name = "group", 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 Index: src/gui/qgslegendinterface.cpp =================================================================== --- src/gui/qgslegendinterface.cpp (revision 0) +++ src/gui/qgslegendinterface.cpp (revision 0) @@ -0,0 +1,28 @@ +/*************************************************************************** + qgslegendinterface.h - description + -------------------------------------- + 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" +#include "../app/legend/qgslegend.h" + +QgsLegendInterface::QgsLegendInterface() +{ +} + +QgsLegendInterface::~QgsLegendInterface() +{ +} + Index: src/gui/CMakeLists.txt =================================================================== --- src/gui/CMakeLists.txt (revision 12307) +++ src/gui/CMakeLists.txt (working copy) @@ -19,6 +19,7 @@ qgisgui.cpp qgisinterface.cpp +qgslegendinterface.cpp qgscolorbutton.cpp qgscomposerview.cpp qgscursors.cpp @@ -64,6 +65,7 @@ qgscomposerview.h qgsdetaileditemdelegate.h qgsdetaileditemwidget.h +qgslegendinterface.h qgisinterface.h qgsencodingfiledialog.h qgsgenericprojectionselector.h Index: src/gui/qgisinterface.h =================================================================== --- src/gui/qgisinterface.h (revision 12307) +++ src/gui/qgisinterface.h (working copy) @@ -37,6 +37,7 @@ class QgsMapCanvas; class QgsRasterLayer; class QgsVectorLayer; +class QgsLegendInterface; /** \ingroup gui * QgisInterface @@ -62,7 +63,9 @@ /** Virtual destructor */ virtual ~QgisInterface(); + virtual QgsLegendInterface* legendInterface() = 0; + public slots: // TODO: do these functions really need to be slots? //! Zoom to full extent of map layers