Skip to content

Commit

Permalink
[FEATURE] Applied patch from Andres Manz from #2185
Browse files Browse the repository at this point in the history
Adds QgsLegendInterface class to GUI library to allow users to do some operations with groups.


git-svn-id: http://svn.osgeo.org/qgis/trunk@12359 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Dec 7, 2009
1 parent 35e9935 commit 94317ce
Show file tree
Hide file tree
Showing 15 changed files with 384 additions and 16 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -40,3 +40,4 @@ Milena Nowotarska
Anita Graser
Richard Duivenvoorde
Alexander Bruy
Andres Manz
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -7,6 +7,7 @@

%Import core/core.sip

%Include qgslegendinterface.sip
%Include qgisinterface.sip
%Include qgscomposerview.sip
%Include qgsencodingfiledialog.sip
Expand Down
4 changes: 4 additions & 0 deletions python/gui/qgisinterface.sip
Expand Up @@ -25,6 +25,10 @@ class QgisInterface : QObject
/** 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?

Expand Down
37 changes: 37 additions & 0 deletions python/gui/qgslegendinterface.sip
@@ -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;
};

2 changes: 2 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -88,6 +88,7 @@ SET(QGIS_APP_SRCS

legend/qgslegendgroup.cpp
legend/qgslegend.cpp
legend/qgsapplegendinterface.cpp
legend/qgslegenditem.cpp
legend/qgslegendlayer.cpp
legend/qgslegendpropertygroup.cpp
Expand Down Expand Up @@ -185,6 +186,7 @@ SET (QGIS_APP_MOC_HDRS
composer/qgsitempositiondialog.h

legend/qgslegend.h
legend/qgsapplegendinterface.h
legend/qgslegendlayer.h

ogr/qgsopenvectorlayerdialog.h
Expand Down
58 changes: 58 additions & 0 deletions src/app/legend/qgsapplegendinterface.cpp
@@ -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();
}
66 changes: 66 additions & 0 deletions src/app/legend/qgsapplegendinterface.h
@@ -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
87 changes: 73 additions & 14 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -116,11 +116,15 @@ void QgsLegend::handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetI
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()
Expand Down Expand Up @@ -159,6 +163,15 @@ void QgsLegend::selectAll( bool select )
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() )
Expand Down Expand Up @@ -351,6 +364,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )

QgsLegendItem* origin = dynamic_cast<QgsLegendItem *>( mItemBeingMoved );
mItemBeingMoved = NULL;
QModelIndex oldIndex = indexFromItem( origin );

QgsLegendItem* dest = dynamic_cast<QgsLegendItem *>( destItem );

Expand All @@ -371,6 +385,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
{
moveItem( origin, dest );
setCurrentItem( origin );
emit itemMoved( oldIndex, indexFromItem( origin ) );
}
}
else if ( mDropAction == BEFORE )// below center of item
Expand All @@ -381,6 +396,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
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 )
Expand All @@ -390,6 +406,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
{
insertItem( origin, dest );
setCurrentItem( origin );
emit itemMoved( oldIndex, indexFromItem( origin ) );
}
}
else//no action
Expand Down Expand Up @@ -585,19 +602,29 @@ void QgsLegend::legendGroupRemove()
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() )
Expand Down Expand Up @@ -668,7 +695,15 @@ bool QgsLegend::removeLayer( QgsMapLayer* ml, bool askCancelOnEditable )
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()
{
Expand Down Expand Up @@ -1199,6 +1234,30 @@ bool QgsLegend::yCoordAboveCenter( QgsLegendItem* it, int ycoord )
}
}

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()
{
Expand Down

0 comments on commit 94317ce

Please sign in to comment.