Skip to content

Commit b0d68ae

Browse files
author
wonder
committed
[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
1 parent cf519de commit b0d68ae

15 files changed

+384
-16
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ Milena Nowotarska
4040
Anita Graser
4141
Richard Duivenvoorde
4242
Alexander Bruy
43+
Andres Manz

python/gui/gui.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
%Import core/core.sip
99

10+
%Include qgslegendinterface.sip
1011
%Include qgisinterface.sip
1112
%Include qgscomposerview.sip
1213
%Include qgsencodingfiledialog.sip

python/gui/qgisinterface.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class QgisInterface : QObject
2525
/** Virtual destructor */
2626
virtual ~QgisInterface();
2727

28+
/** Get pointer to legend interface
29+
\note added in 1.4
30+
*/
31+
virtual QgsLegendInterface* legendInterface()=0;
2832

2933
public slots: // TODO: do these functions really need to be slots?
3034

python/gui/qgslegendinterface.sip

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* \class QgsLegendInterface
3+
* \brief Abstract base class to make QgsLegend available to plugins.
4+
*/
5+
class QgsLegendInterface : QObject
6+
{
7+
%TypeHeaderCode
8+
#include <qgslegendinterface.h>
9+
%End
10+
11+
public:
12+
13+
/** Constructor */
14+
QgsLegendInterface();
15+
16+
/** Virtual destructor */
17+
~QgsLegendInterface();
18+
19+
virtual QStringList groups() =0;
20+
21+
signals:
22+
23+
//! emitted when a group index has changed
24+
void groupIndexChanged( int oldIndex, int newIndex );
25+
26+
public slots:
27+
28+
//! Add a new group
29+
virtual int addGroup( QString name, bool expand = true ) =0;
30+
31+
//! Remove group on index
32+
virtual void removeGroup( int groupIndex ) =0;
33+
34+
//! Move a layer to a group
35+
virtual void moveLayer( QgsMapLayer * layer, int groupIndex ) =0;
36+
};
37+

src/app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ SET(QGIS_APP_SRCS
8888

8989
legend/qgslegendgroup.cpp
9090
legend/qgslegend.cpp
91+
legend/qgsapplegendinterface.cpp
9192
legend/qgslegenditem.cpp
9293
legend/qgslegendlayer.cpp
9394
legend/qgslegendpropertygroup.cpp
@@ -185,6 +186,7 @@ SET (QGIS_APP_MOC_HDRS
185186
composer/qgsitempositiondialog.h
186187

187188
legend/qgslegend.h
189+
legend/qgsapplegendinterface.h
188190
legend/qgslegendlayer.h
189191

190192
ogr/qgsopenvectorlayerdialog.h
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/***************************************************************************
2+
qgsapplegendinterface.cpp
3+
--------------------------------------
4+
Date : 19-Nov-2009
5+
Copyright : (C) 2009 by Andres Manz
6+
Email : manz dot andres at gmail dot com
7+
****************************************************************************/
8+
/***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
/* $Id$ */
17+
18+
#include "qgsapplegendinterface.h"
19+
20+
#include "qgslegend.h"
21+
22+
23+
QgsAppLegendInterface::QgsAppLegendInterface( QgsLegend * legend )
24+
: mLegend( legend )
25+
{
26+
}
27+
28+
QgsAppLegendInterface::~QgsAppLegendInterface()
29+
{
30+
}
31+
32+
int QgsAppLegendInterface::addGroup( QString name, bool expand )
33+
{
34+
return mLegend->addGroup( name, expand );
35+
}
36+
37+
void QgsAppLegendInterface::removeGroup( int groupIndex )
38+
{
39+
mLegend->removeGroup( groupIndex );
40+
}
41+
42+
void QgsAppLegendInterface::moveLayer( QgsMapLayer * ml, int groupIndex )
43+
{
44+
mLegend->moveLayer( ml, groupIndex );
45+
}
46+
47+
void QgsAppLegendInterface::updateIndex( const QModelIndex &oldIndex, const QModelIndex& newIndex)
48+
{
49+
if ( mLegend->isLegendGroup( newIndex ) )
50+
{
51+
emit groupIndexChanged( oldIndex.row(), newIndex.row() );
52+
}
53+
}
54+
55+
QStringList QgsAppLegendInterface::groups()
56+
{
57+
return mLegend->groups();
58+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/***************************************************************************
2+
qgsapplegendinterface.h
3+
--------------------------------------
4+
Date : 23-Nov-2009
5+
Copyright : (C) 2009 by Andres Manz
6+
Email : manz dot andres at gmail dot com
7+
****************************************************************************/
8+
/***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
/* $Id$ */
17+
18+
#ifndef QGSLEGENDAPPIFACE_H
19+
#define QGSLEGENDAPPIFACE_H
20+
21+
#include "qgslegendinterface.h"
22+
23+
class QModelIndex;
24+
class QgsLegend;
25+
class QgsMapLayer;
26+
27+
/** \ingroup gui
28+
* QgsLegendInterface
29+
* Abstract base class to make QgsLegend available to plugins.
30+
*/
31+
class QgsAppLegendInterface : public QgsLegendInterface
32+
{
33+
Q_OBJECT
34+
35+
public:
36+
37+
/** Constructor */
38+
explicit QgsAppLegendInterface( QgsLegend * legend );
39+
40+
/** Virtual destructor */
41+
~QgsAppLegendInterface();
42+
43+
//! Return a string list of groups
44+
QStringList groups();
45+
46+
public slots:
47+
48+
//! Add a new group
49+
int addGroup( QString name, bool expand = true );
50+
51+
//! Remove all groups with the given name
52+
void removeGroup( int groupIndex );
53+
54+
//! Move a layer to a group
55+
void moveLayer( QgsMapLayer * ml, int groupIndex );
56+
57+
//! Update an index
58+
void updateIndex( const QModelIndex &oldIndex, const QModelIndex &newIndex );
59+
60+
private:
61+
62+
//! Pointer to QgsLegend object
63+
QgsLegend *mLegend;
64+
};
65+
66+
#endif //QGSLEGENDAPPIFACE_H

src/app/legend/qgslegend.cpp

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,15 @@ void QgsLegend::handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetI
116116
emit currentLayerChanged( layer );
117117
}
118118

119-
void QgsLegend::addGroup()
119+
int QgsLegend::addGroup( QString name, bool expand )
120120
{
121-
QgsLegendGroup* group = new QgsLegendGroup( this, tr( "group" ) );
121+
if ( name.isEmpty() )
122+
name = tr( "group" ); // some default name if none specified
123+
QgsLegendGroup* group = new QgsLegendGroup( this, name );
122124
group->setData( 0, Qt::UserRole, Qt::Checked );
123-
setExpanded( indexFromItem( group ), true );
125+
QModelIndex groupIndex = indexFromItem( group );
126+
setExpanded( groupIndex, expand );
127+
return groupIndex.row();
124128
}
125129

126130
void QgsLegend::removeAll()
@@ -159,6 +163,15 @@ void QgsLegend::selectAll( bool select )
159163
mMapCanvas->setRenderFlag( renderFlagState );
160164
}
161165

166+
void QgsLegend::removeGroup( int groupIndex )
167+
{
168+
QgsLegendGroup * lg = dynamic_cast<QgsLegendGroup *>( topLevelItem( groupIndex ) );
169+
if ( lg )
170+
{
171+
removeGroup( lg );
172+
}
173+
}
174+
162175
void QgsLegend::removeLayer( QString layer_key )
163176
{
164177
if ( !mMapCanvas || mMapCanvas->isDrawing() )
@@ -351,6 +364,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
351364

352365
QgsLegendItem* origin = dynamic_cast<QgsLegendItem *>( mItemBeingMoved );
353366
mItemBeingMoved = NULL;
367+
QModelIndex oldIndex = indexFromItem( origin );
354368

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

@@ -371,6 +385,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
371385
{
372386
moveItem( origin, dest );
373387
setCurrentItem( origin );
388+
emit itemMoved( oldIndex, indexFromItem( origin ) );
374389
}
375390
}
376391
else if ( mDropAction == BEFORE )// below center of item
@@ -381,6 +396,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
381396
moveItem( origin, dest ); // Insert after, as above...
382397
moveItem( dest, origin ); // ... and then switch places!
383398
setCurrentItem( origin );
399+
emit itemMoved( oldIndex, indexFromItem( origin ) );
384400
}
385401
}
386402
else if ( mDropAction == INTO_GROUP )
@@ -390,6 +406,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
390406
{
391407
insertItem( origin, dest );
392408
setCurrentItem( origin );
409+
emit itemMoved( oldIndex, indexFromItem( origin ) );
393410
}
394411
}
395412
else//no action
@@ -585,19 +602,29 @@ void QgsLegend::legendGroupRemove()
585602
QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( currentItem() );
586603
if ( lg )
587604
{
588-
//delete the legend layers first
589-
QTreeWidgetItem * child = lg->child( 0 );
590-
while ( child )
591-
{
592-
setCurrentItem( child );
593-
removeCurrentLayer();
594-
child = lg->child( 0 );
595-
}
596-
delete lg;
597-
adjustIconSize();
605+
removeGroup( lg );
598606
}
599607
}
600608

609+
void QgsLegend::removeGroup( QgsLegendGroup * lg )
610+
{
611+
if ( !mMapCanvas || mMapCanvas->isDrawing() )
612+
{
613+
return;
614+
}
615+
616+
//delete the legend layers first
617+
QTreeWidgetItem * child = lg->child( 0 );
618+
while ( child )
619+
{
620+
setCurrentItem( child );
621+
removeCurrentLayer();
622+
child = lg->child( 0 );
623+
}
624+
delete lg;
625+
adjustIconSize();
626+
}
627+
601628
void QgsLegend::removeCurrentLayer()
602629
{
603630
if ( !mMapCanvas || mMapCanvas->isDrawing() )
@@ -668,7 +695,15 @@ bool QgsLegend::removeLayer( QgsMapLayer* ml, bool askCancelOnEditable )
668695
return true;
669696
}
670697

671-
698+
void QgsLegend::moveLayer( QgsMapLayer * ml, int groupIndex )
699+
{
700+
QgsLegendLayer *layer = findLegendLayer( ml->getLayerID() );
701+
QgsLegendGroup *group = dynamic_cast<QgsLegendGroup*>( topLevelItem( groupIndex ) );
702+
if ( layer && group )
703+
{
704+
insertItem( layer, group );
705+
}
706+
}
672707

673708
void QgsLegend::legendLayerShowProperties()
674709
{
@@ -1199,6 +1234,30 @@ bool QgsLegend::yCoordAboveCenter( QgsLegendItem* it, int ycoord )
11991234
}
12001235
}
12011236

1237+
bool QgsLegend::isLegendGroup( const QModelIndex &index )
1238+
{
1239+
return dynamic_cast<QgsLegendGroup *>( itemFromIndex( index ) );
1240+
}
1241+
1242+
QStringList QgsLegend::groups()
1243+
{
1244+
QStringList groupList;
1245+
QTreeWidgetItem *current = firstItem();
1246+
1247+
while ( current )
1248+
{
1249+
QgsLegendGroup *group = dynamic_cast<QgsLegendGroup *>( current );
1250+
if ( group )
1251+
{
1252+
groupList.append( group->text( 0 ) );
1253+
}
1254+
1255+
current = nextItem( current );
1256+
}
1257+
1258+
return groupList;
1259+
}
1260+
12021261
/**Returns the first item in the hierarchy*/
12031262
QTreeWidgetItem* QgsLegend::firstItem()
12041263
{

0 commit comments

Comments
 (0)