Skip to content

Commit d97d40e

Browse files
author
mhugent
committedApr 21, 2011
Improve python bindings for adding legend groups (#3263). Patch from Marco Bernasocchi
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15787 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed
 

‎python/gui/qgslegendinterface.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class QgsLegendInterface : QObject
5252
//! Add a new group
5353
//! @note added parent parameter in 1.7
5454
virtual int addGroup( QString name, bool expand = true, QTreeWidgetItem* parent =0 ) = 0;
55+
56+
//! Add a new group
57+
//! @note added parentIndex parameter in 1.7
58+
virtual int addGroup( QString name, bool expand, int parentIndex ) = 0;
5559

5660
//! Remove group on index
5761
virtual void removeGroup( int groupIndex ) =0;

‎src/app/legend/qgsapplegendinterface.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ int QgsAppLegendInterface::addGroup( QString name, bool expand, QTreeWidgetItem*
3737
return mLegend->addGroup( name, expand, parent );
3838
}
3939

40+
int QgsAppLegendInterface::addGroup( QString name, bool expand, int parentIndex )
41+
{
42+
return mLegend->addGroup( name, expand, parentIndex );
43+
}
44+
4045
void QgsAppLegendInterface::removeGroup( int groupIndex )
4146
{
4247
mLegend->removeGroup( groupIndex );

‎src/app/legend/qgsapplegendinterface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class QgsAppLegendInterface : public QgsLegendInterface
6767
//! Add a new group
6868
int addGroup( QString name, bool expand = true, QTreeWidgetItem* parent = 0 );
6969

70+
//! Add a new group at a specified index
71+
int addGroup( QString name, bool expand, int groupIndex );
72+
7073
//! Remove all groups with the given name
7174
void removeGroup( int groupIndex );
7275

‎src/app/legend/qgslegend.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ int QgsLegend::addGroupToCurrentItem( QString name, bool expand )
159159

160160
int QgsLegend::addGroup( QString name, bool expand, QTreeWidgetItem* parent )
161161
{
162-
if ( name.isEmpty() )
162+
bool nameEmpty = name.isEmpty();
163+
if ( nameEmpty )
163164
name = tr( "group" ); // some default name if none specified
164165

165166
QgsLegendGroup *parentGroup = dynamic_cast<QgsLegendGroup *>( parent );
@@ -173,10 +174,18 @@ int QgsLegend::addGroup( QString name, bool expand, QTreeWidgetItem* parent )
173174
QModelIndex groupIndex = indexFromItem( group );
174175
setExpanded( groupIndex, expand );
175176
setCurrentItem( group );
176-
openEditor();
177+
if ( nameEmpty )
178+
openEditor();
179+
177180
return groupIndex.row();
178181
}
179182

183+
int QgsLegend::addGroup( QString name, bool expand, int groupIndex )
184+
{
185+
QgsLegendGroup * lg = dynamic_cast<QgsLegendGroup *>( topLevelItem( groupIndex ) );
186+
return addGroup( name, expand, lg );
187+
}
188+
180189
void QgsLegend::removeAll()
181190
{
182191
clear();

‎src/app/legend/qgslegend.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ class QgsLegend : public QTreeWidget
237237
*/
238238
int addGroup( QString name = QString(), bool expand = true, QTreeWidgetItem* parent = 0 );
239239

240+
/*!
241+
* Slot called when user wishes to add a new empty layer group to the legend.
242+
* All parameter are mandatory to be used to programatically nest a new group
243+
* @param name name of the new group
244+
* @param expand expand the group
245+
* @return index of inserted group
246+
*/
247+
int addGroup( QString name, bool expand, int parentIndex );
248+
240249
/*!
241250
* Removes all groups with the given name.
242251
* @param name name of the groups to remove

‎src/gui/qgslegendinterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class GUI_EXPORT QgsLegendInterface : public QObject
8484
//! a parent group can be given to nest the new group in it
8585
virtual int addGroup( QString name, bool expand = true, QTreeWidgetItem* parent = 0 ) = 0;
8686

87+
//! Add a new group
88+
//! a parent group index has to be given to nest the new group in it
89+
virtual int addGroup( QString name, bool expand, int parentIndex ) = 0;
90+
8791
//! Remove group on index
8892
virtual void removeGroup( int groupIndex ) = 0;
8993

0 commit comments

Comments
 (0)
Please sign in to comment.