Skip to content

Commit 3b27f58

Browse files
author
wonder
committed
Applied patch from #2672 by Andres Manz: more legend interface functionality.
Thanks for contributing. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13383 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c5ede41 commit 3b27f58

File tree

6 files changed

+151
-4
lines changed

6 files changed

+151
-4
lines changed

python/gui/qgslegendinterface.sip

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,23 @@ class QgsLegendInterface : QObject
2121

2222
//! Return all layers in the project in legend order
2323
//! @note added in 1.5
24-
virtual QList< QgsMapLayer * > layers() const = 0;
24+
virtual QList< QgsMapLayer * > layers() const =0;
25+
26+
//! Check if a group exists
27+
//! @note added in 1.5
28+
virtual bool groupExists( int groupIndex ) =0;
29+
30+
//! Check if a group is expanded
31+
//! @note added in 1.5
32+
virtual bool isGroupExpanded( int groupIndex ) =0;
33+
34+
//! Check if a group is visible
35+
//! @note added in 1.5
36+
virtual bool isGroupVisible( int groupIndex ) =0;
37+
38+
//! Check if a layer is visible
39+
//! @note added in 1.5
40+
virtual bool isLayerVisible( QgsMapLayer * ml ) =0;
2541

2642
signals:
2743

@@ -39,8 +55,20 @@ class QgsLegendInterface : QObject
3955
//! Move a layer to a group
4056
virtual void moveLayer( QgsMapLayer * layer, int groupIndex ) =0;
4157

58+
//! Collapse or expand a group
59+
//! @note added in 1.5
60+
virtual void setGroupExpanded( int groupIndex, bool expand ) =0;
61+
62+
//! Set the visibility of a group
63+
//! @note added in 1.5
64+
virtual void setGroupVisible( int groupIndex, bool visible ) =0;
65+
66+
//! Set the visibility of a layer
67+
//! @note added in 1.5
68+
virtual void setLayerVisible( QgsMapLayer * ml, bool visible ) =0;
69+
4270
//! refresh layer symbology
43-
//! \note added in 1.5
71+
//! @note added in 1.5
4472
virtual void refreshLayerSymbology( QgsMapLayer *layer ) =0;
4573
};
4674

src/app/legend/qgsapplegendinterface.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,59 @@ void QgsAppLegendInterface::updateIndex( QModelIndex oldIndex, QModelIndex newIn
5454
}
5555
}
5656

57+
void QgsAppLegendInterface::setGroupExpanded( int groupIndex, bool expand )
58+
{
59+
mLegend->setExpanded( mLegend->model()->index( groupIndex, 0 ), expand );
60+
}
61+
62+
void QgsAppLegendInterface::setGroupVisible( int groupIndex, bool visible )
63+
{
64+
if ( !groupExists( groupIndex ) )
65+
{
66+
return;
67+
}
68+
69+
Qt::CheckState state = visible ? Qt::Checked : Qt::Unchecked;
70+
mLegend->topLevelItem( groupIndex )->setCheckState( 0, state );
71+
}
72+
73+
void QgsAppLegendInterface::setLayerVisible( QgsMapLayer * ml, bool visible )
74+
{
75+
mLegend->setLayerVisible( ml, visible );
76+
}
77+
5778
QStringList QgsAppLegendInterface::groups()
5879
{
5980
return mLegend->groups();
6081
}
6182

83+
bool QgsAppLegendInterface::groupExists( int groupIndex )
84+
{
85+
QModelIndex mi = mLegend->model()->index( groupIndex, 0 );
86+
return ( mi.isValid() &&
87+
mLegend->isLegendGroup( mi ) );
88+
}
89+
90+
bool QgsAppLegendInterface::isGroupExpanded( int groupIndex )
91+
{
92+
return mLegend->isExpanded( mLegend->model()->index( groupIndex, 0 ) );
93+
}
94+
95+
bool QgsAppLegendInterface::isGroupVisible( int groupIndex )
96+
{
97+
if ( !groupExists( groupIndex ) )
98+
{
99+
return false;
100+
}
101+
102+
return ( Qt::Checked == mLegend->topLevelItem( groupIndex )->checkState( 0 ) );
103+
}
104+
105+
bool QgsAppLegendInterface::isLayerVisible( QgsMapLayer * ml )
106+
{
107+
return ( Qt::Checked == mLegend->layerCheckState( ml ) );
108+
}
109+
62110
QList< QgsMapLayer * > QgsAppLegendInterface::layers() const
63111
{
64112
QList< QgsMapLayer * > items;

src/app/legend/qgsapplegendinterface.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class QgsAppLegendInterface : public QgsLegendInterface
3838
/** Constructor */
3939
explicit QgsAppLegendInterface( QgsLegend * legend );
4040

41-
/** Virtual destructor */
41+
/** Destructor */
4242
~QgsAppLegendInterface();
4343

4444
//! Return a string list of groups
@@ -47,6 +47,18 @@ class QgsAppLegendInterface : public QgsLegendInterface
4747
//! Return all layers in the project in legend order
4848
QList< QgsMapLayer * > layers() const;
4949

50+
//! Check if a group exists
51+
bool groupExists( int groupIndex );
52+
53+
//! Check if a group is expanded
54+
bool isGroupExpanded( int groupIndex );
55+
56+
//! Check if a group is visible
57+
bool isGroupVisible( int groupIndex );
58+
59+
//! Check if a layer is visible
60+
bool isLayerVisible( QgsMapLayer * ml );
61+
5062
public slots:
5163

5264
//! Add a new group
@@ -61,6 +73,15 @@ class QgsAppLegendInterface : public QgsLegendInterface
6173
//! Update an index
6274
void updateIndex( QModelIndex oldIndex, QModelIndex newIndex );
6375

76+
//! Collapse or expand a group
77+
virtual void setGroupExpanded( int groupIndex, bool expand );
78+
79+
//! Set the visibility of a group
80+
virtual void setGroupVisible( int groupIndex, bool visible );
81+
82+
//! Set the visibility of a layer
83+
virtual void setLayerVisible( QgsMapLayer * ml, bool visible );
84+
6485
//! refresh layer symbology
6586
void refreshLayerSymbology( QgsMapLayer *ml );
6687

src/app/legend/qgslegend.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,13 @@ void QgsLegend::initPixmaps()
480480
mPixmaps.mProjectionErrorPixmap = QgisApp::getThemePixmap( "/mIconProjectionProblem.png" );
481481
}
482482

483+
Qt::CheckState QgsLegend::layerCheckState( QgsMapLayer * layer )
484+
{
485+
QgsLegendLayer * ll = findLegendLayer( layer );
486+
487+
return ll ? ll->checkState( 0 ) : Qt::Unchecked;
488+
}
489+
483490
int QgsLegend::getItemPos( QTreeWidgetItem* item )
484491
{
485492
int counter = 1;
@@ -548,6 +555,16 @@ void QgsLegend::addLayer( QgsMapLayer * layer )
548555
doItemsLayout();
549556
}
550557

558+
void QgsLegend::setLayerVisible( QgsMapLayer * layer, bool visible )
559+
{
560+
QgsLegendLayer * ll = findLegendLayer( layer );
561+
if ( ll )
562+
{
563+
Qt::CheckState cs = visible ? Qt::Checked : Qt::Unchecked;
564+
ll->setCheckState( 0, cs );
565+
}
566+
}
567+
551568
void QgsLegend::setMapCanvas( QgsMapCanvas * canvas )
552569
{
553570
if ( mMapCanvas )

src/app/legend/qgslegend.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ class QgsLegend : public QTreeWidget
179179
/**Returns structure with legend pixmaps*/
180180
QgsLegendPixmaps& pixmaps() { return mPixmaps; }
181181

182+
/**Returns a layers check state*/
183+
Qt::CheckState layerCheckState( QgsMapLayer * layer );
184+
182185

183186
void updateCheckStates( QTreeWidgetItem* item, Qt::CheckState state ) { item->setData( 0, Qt::UserRole, state ); }
184187

@@ -187,6 +190,8 @@ class QgsLegend : public QTreeWidget
187190
/*!Adds a new layer group with the maplayer to the canvas*/
188191
void addLayer( QgsMapLayer * layer );
189192

193+
void setLayerVisible( QgsMapLayer * layer, bool visible );
194+
190195
void setMapCanvas( QgsMapCanvas * canvas );
191196

192197
/**Updates symbology items for a layer*/

src/gui/qgslegendinterface.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ class GUI_EXPORT QgsLegendInterface : public QObject
4848
//! @note added in 1.5
4949
virtual QList< QgsMapLayer * > layers() const = 0;
5050

51+
//! Check if a group exists
52+
//! @note added in 1.5
53+
virtual bool groupExists( int groupIndex ) = 0;
54+
55+
//! Check if a group is expanded
56+
//! @note added in 1.5
57+
virtual bool isGroupExpanded( int groupIndex ) = 0;
58+
59+
//! Check if a group is visible
60+
//! @note added in 1.5
61+
virtual bool isGroupVisible( int groupIndex ) = 0;
62+
63+
//! Check if a layer is visible
64+
//! @note added in 1.5
65+
virtual bool isLayerVisible( QgsMapLayer * ml ) = 0;
66+
5167
signals:
5268

5369
//! emitted when a group index has changed
@@ -64,8 +80,20 @@ class GUI_EXPORT QgsLegendInterface : public QObject
6480
//! Move a layer to a group
6581
virtual void moveLayer( QgsMapLayer * ml, int groupIndex ) = 0;
6682

83+
//! Collapse or expand a group
84+
//! @note added in 1.5
85+
virtual void setGroupExpanded( int groupIndex, bool expand ) = 0;
86+
87+
//! Set the visibility of a group
88+
//! @note added in 1.5
89+
virtual void setGroupVisible( int groupIndex, bool visible ) = 0;
90+
91+
//! Set the visibility of a layer
92+
//! @note added in 1.5
93+
virtual void setLayerVisible( QgsMapLayer * ml, bool visible ) = 0;
94+
6795
//! Refresh layer symbology
68-
// @noted added in 1.5
96+
//! @note added in 1.5
6997
virtual void refreshLayerSymbology( QgsMapLayer *ml ) = 0;
7098
};
7199

0 commit comments

Comments
 (0)