Skip to content
Permalink
Browse files

[FEATURE] Applied patch from Andres Manz from #2185

Adds QgsLegendInterface class to GUI library to allow users to do some operations with groups.


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12359 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Dec 7, 2009
1 parent cf519de commit b0d68ae2f04713a07b388734355a6f2ee9f782e6
@@ -40,3 +40,4 @@ Milena Nowotarska
Anita Graser
Richard Duivenvoorde
Alexander Bruy
Andres Manz
@@ -7,6 +7,7 @@

%Import core/core.sip

%Include qgslegendinterface.sip
%Include qgisinterface.sip
%Include qgscomposerview.sip
%Include qgsencodingfiledialog.sip
@@ -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?

@@ -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;
};

@@ -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
@@ -185,6 +186,7 @@ SET (QGIS_APP_MOC_HDRS
composer/qgsitempositiondialog.h

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

ogr/qgsopenvectorlayerdialog.h
@@ -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();
}
@@ -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
@@ -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()
@@ -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() )
@@ -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 );

@@ -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
@@ -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 )
@@ -390,6 +406,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
{
insertItem( origin, dest );
setCurrentItem( origin );
emit itemMoved( oldIndex, indexFromItem( origin ) );
}
}
else//no action
@@ -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() )
@@ -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()
{
@@ -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()
{

0 comments on commit b0d68ae

Please sign in to comment.
You can’t perform that action at this time.