Skip to content

Commit b0d68ae

Browse files
author
wonder
committedDec 7, 2009
[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

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
{

‎src/app/legend/qgslegend.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <set>
2626
#include <QTreeWidget>
2727

28+
class QgsLegendGroup;
2829
class QgsLegendLayer;
2930
class QgsLegendItem;
3031
class QgsMapLayer;
@@ -116,6 +117,12 @@ class QgsLegend : public QTreeWidget
116117
/**Returns true, if the y-coordinate is >= the center of the item*/
117118
bool yCoordAboveCenter( QgsLegendItem* it, int ycoord );
118119

120+
/**Returns true, if the item at index is a QgsLegendGroup*/
121+
bool isLegendGroup( const QModelIndex &index );
122+
123+
/**Returns a string list of groups*/
124+
QStringList groups();
125+
119126
/**Returns the first item in the hierarchy*/
120127
QTreeWidgetItem* firstItem();
121128

@@ -202,9 +209,19 @@ class QgsLegend : public QTreeWidget
202209
/*!
203210
* Slot called when user wishes to add a new empty layer group to the legend.
204211
* The user will be prompted for the name of the newly added group.
212+
* @param name name of the new group
213+
* @param expand expand the group
214+
* @return void
215+
*/
216+
int addGroup( QString name = QString(), bool expand = true );
217+
218+
/*!
219+
* Removes all groups with the given name.
220+
* @param name name of the groups to remove
205221
* @return void
206222
*/
207-
void addGroup();
223+
void removeGroup( int groupIndex );
224+
208225
void removeLayer( QString );
209226

210227
/** called to read legend settings from project */
@@ -223,6 +240,14 @@ class QgsLegend : public QTreeWidget
223240
@param return false if canceled or in case of error, true else*/
224241
bool removeLayer( QgsMapLayer* ml, bool askCancelOnEditable );
225242

243+
/*!
244+
* Moves a layer to a group.
245+
* @param ml the maplayer to move
246+
* @param groupIndex index of group
247+
* @return false if the group does not exist, false otherwise
248+
*/
249+
void moveLayer( QgsMapLayer* ml, int groupIndex );
250+
226251
/**Toggle show in overview for current layer*/
227252
void legendLayerShowInOverview();
228253

@@ -338,6 +363,8 @@ class QgsLegend : public QTreeWidget
338363
void handleRightClickEvent( QTreeWidgetItem* item, const QPoint& position );
339364
/**Removes the current legend group*/
340365
void legendGroupRemove();
366+
/**Removes a legend group and its layers*/
367+
void removeGroup( QgsLegendGroup * lg );
341368
/**Sets all listview items to open*/
342369
void expandAll();
343370
/**Sets all listview items to closed*/
@@ -444,6 +471,8 @@ class QgsLegend : public QTreeWidget
444471
QWidget *mInsertionLine;
445472

446473
signals:
474+
void itemMoved( QModelIndex oldIndex, QModelIndex newIndex );
475+
447476
void zOrderChanged( QgsLegend * lv );
448477

449478
//! Emited whenever current (selected) layer changes

‎src/app/qgisappinterface.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#include "qgsshortcutsmanager.h"
3232

3333
QgisAppInterface::QgisAppInterface( QgisApp * _qgis )
34-
: qgis( _qgis )
34+
: qgis( _qgis ),
35+
legendIface( _qgis->legend() )
3536
{
3637
// connect signals
3738
connect( qgis->legend(), SIGNAL( currentLayerChanged( QgsMapLayer * ) ),
@@ -45,6 +46,11 @@ QgisAppInterface::~QgisAppInterface()
4546
{
4647
}
4748

49+
QgsLegendInterface* QgisAppInterface::legendInterface()
50+
{
51+
return &legendIface;
52+
}
53+
4854
void QgisAppInterface::zoomFull()
4955
{
5056
qgis->zoomFull();

‎src/app/qgisappinterface.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define QGISIFACE_H
2121

2222
#include "qgisinterface.h"
23+
#include "qgsapplegendinterface.h"
2324

2425
class QgisApp;
2526

@@ -42,6 +43,8 @@ class QgisAppInterface : public QgisInterface
4243
QgisAppInterface( QgisApp *qgisapp );
4344
~QgisAppInterface();
4445

46+
QgsLegendInterface* legendInterface();
47+
4548
/* Exposed functions */
4649
//! Zoom map to full extent
4750
void zoomFull();
@@ -261,6 +264,9 @@ class QgisAppInterface : public QgisInterface
261264

262265
//! Pointer to the QgisApp object
263266
QgisApp *qgis;
267+
268+
//! Pointer to the LegendInterface object
269+
QgsAppLegendInterface legendIface;
264270
};
265271

266272

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.cpp
1919

2020
qgisgui.cpp
2121
qgisinterface.cpp
22+
qgslegendinterface.cpp
2223
qgscolorbutton.cpp
2324
qgscomposerview.cpp
2425
qgscursors.cpp
@@ -65,6 +66,7 @@ symbology-ng/qgsvectorcolorbrewercolorrampv2dialog.h
6566
qgscomposerview.h
6667
qgsdetaileditemdelegate.h
6768
qgsdetaileditemwidget.h
69+
qgslegendinterface.h
6870
qgisinterface.h
6971
qgsencodingfiledialog.h
7072
qgsgenericprojectionselector.h

‎src/gui/qgisinterface.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class QgsMapLayer;
3737
class QgsMapCanvas;
3838
class QgsRasterLayer;
3939
class QgsVectorLayer;
40+
class QgsLegendInterface;
4041

4142
/** \ingroup gui
4243
* QgisInterface
@@ -62,6 +63,11 @@ class GUI_EXPORT QgisInterface : public QObject
6263
/** Virtual destructor */
6364
virtual ~QgisInterface();
6465

66+
/** Get pointer to legend interface
67+
\note added in 1.4
68+
*/
69+
virtual QgsLegendInterface* legendInterface() = 0;
70+
6571

6672
public slots: // TODO: do these functions really need to be slots?
6773

‎src/gui/qgslegendinterface.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/***************************************************************************
2+
qgslegendinterface.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 "qgslegendinterface.h"
19+
20+
QgsLegendInterface::QgsLegendInterface()
21+
{
22+
}
23+
24+
QgsLegendInterface::~QgsLegendInterface()
25+
{
26+
}
27+

‎src/gui/qgslegendinterface.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/***************************************************************************
2+
qgslegendinterface.h
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+
#ifndef QGSLEGENDINTERFACE_H
19+
#define QGSLEGENDINTERFACE_H
20+
21+
#include <QObject>
22+
#include <QStringList>
23+
24+
class QgsMapLayer;
25+
26+
/** \ingroup gui
27+
* QgsLegendInterface
28+
* Abstract base class to make QgsLegend available to plugins.
29+
*
30+
* \note added in 1.4
31+
*/
32+
class GUI_EXPORT QgsLegendInterface : public QObject
33+
{
34+
Q_OBJECT
35+
36+
public:
37+
38+
/** Constructor */
39+
QgsLegendInterface();
40+
41+
/** Virtual destructor */
42+
virtual ~QgsLegendInterface();
43+
44+
//! Return a string list of groups
45+
virtual QStringList groups() = 0;
46+
47+
signals:
48+
49+
//! emitted when a group index has changed
50+
void groupIndexChanged( int oldIndex, int newIndex );
51+
52+
public slots:
53+
54+
//! Add a new group
55+
virtual int addGroup( QString name, bool expand = true ) = 0;
56+
57+
//! Remove group on index
58+
virtual void removeGroup( int groupIndex ) = 0;
59+
60+
//! Move a layer to a group
61+
virtual void moveLayer( QgsMapLayer * ml, int groupIndex ) = 0;
62+
};
63+
64+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.