Index: python/gui/qgslegendinterface.sip =================================================================== --- python/gui/qgslegendinterface.sip (revision 13375) +++ python/gui/qgslegendinterface.sip (working copy) @@ -21,8 +21,24 @@ //! Return all layers in the project in legend order //! @note added in 1.5 - virtual QList< QgsMapLayer * > layers() const = 0; + virtual QList< QgsMapLayer * > layers() const =0; + //! Check if a group exists + //! @note added in 1.5 + virtual bool groupExists( int groupIndex ) =0; + + //! Check if a group is expanded + //! @note added in 1.5 + virtual bool isGroupExpanded( int groupIndex ) =0; + + //! Check if a group is visible + //! @note added in 1.5 + virtual bool isGroupVisible( int groupIndex ) =0; + + //! Check if a layer is visible + //! @note added in 1.5 + virtual bool isLayerVisible( QgsMapLayer * ml ) =0; + signals: //! emitted when a group index has changed @@ -39,8 +55,20 @@ //! Move a layer to a group virtual void moveLayer( QgsMapLayer * layer, int groupIndex ) =0; + //! Collapse or expand a group + //! @note added in 1.5 + virtual void setGroupExpanded( int groupIndex, bool expand ) =0; + + //! Set the visibility of a group + //! @note added in 1.5 + virtual void setGroupVisible( int groupIndex, bool visible ) =0; + + //! Set the visibility of a layer + //! @note added in 1.5 + virtual void setLayerVisible( QgsMapLayer * ml, bool visible ) =0; + //! refresh layer symbology - //! \note added in 1.5 + //! @note added in 1.5 virtual void refreshLayerSymbology( QgsMapLayer *layer ) =0; }; Index: src/app/legend/qgsapplegendinterface.h =================================================================== --- src/app/legend/qgsapplegendinterface.h (revision 13375) +++ src/app/legend/qgsapplegendinterface.h (working copy) @@ -38,7 +38,7 @@ /** Constructor */ explicit QgsAppLegendInterface( QgsLegend * legend ); - /** Virtual destructor */ + /** Destructor */ ~QgsAppLegendInterface(); //! Return a string list of groups @@ -47,6 +47,18 @@ //! Return all layers in the project in legend order QList< QgsMapLayer * > layers() const; + //! Check if a group exists + bool groupExists( int groupIndex ); + + //! Check if a group is expanded + bool isGroupExpanded( int groupIndex ); + + //! Check if a group is visible + bool isGroupVisible( int groupIndex ); + + //! Check if a layer is visible + bool isLayerVisible( QgsMapLayer * ml ); + public slots: //! Add a new group @@ -61,6 +73,15 @@ //! Update an index void updateIndex( QModelIndex oldIndex, QModelIndex newIndex ); + //! Collapse or expand a group + virtual void setGroupExpanded( int groupIndex, bool expand ); + + //! Set the visibility of a group + virtual void setGroupVisible( int groupIndex, bool visible ); + + //! Set the visibility of a layer + virtual void setLayerVisible( QgsMapLayer * ml, bool visible ); + //! refresh layer symbology void refreshLayerSymbology( QgsMapLayer *ml ); Index: src/app/legend/qgslegend.cpp =================================================================== --- src/app/legend/qgslegend.cpp (revision 13375) +++ src/app/legend/qgslegend.cpp (working copy) @@ -480,6 +480,13 @@ mPixmaps.mProjectionErrorPixmap = QgisApp::getThemePixmap( "/mIconProjectionProblem.png" ); } +Qt::CheckState QgsLegend::layerCheckState( QgsMapLayer * layer ) +{ + QgsLegendLayer * ll = findLegendLayer( layer ); + + return ll ? Qt::Unchecked : ll->checkState( 0 ); +} + int QgsLegend::getItemPos( QTreeWidgetItem* item ) { int counter = 1; @@ -548,6 +555,16 @@ doItemsLayout(); } +void QgsLegend::setLayerVisible( QgsMapLayer * layer, bool visible ) +{ + QgsLegendLayer * ll = findLegendLayer( layer ); + if ( ll ) + { + Qt::CheckState cs = visible ? Qt::Checked : Qt::Unchecked; + ll->setCheckState( 0, cs ); + } +} + void QgsLegend::setMapCanvas( QgsMapCanvas * canvas ) { if ( mMapCanvas ) Index: src/app/legend/qgslegend.h =================================================================== --- src/app/legend/qgslegend.h (revision 13375) +++ src/app/legend/qgslegend.h (working copy) @@ -179,7 +179,10 @@ /**Returns structure with legend pixmaps*/ QgsLegendPixmaps& pixmaps() { return mPixmaps; } + /**Returns a layers check state*/ + Qt::CheckState layerCheckState( QgsMapLayer * layer ); + void updateCheckStates( QTreeWidgetItem* item, Qt::CheckState state ) { item->setData( 0, Qt::UserRole, state ); } public slots: @@ -187,6 +190,8 @@ /*!Adds a new layer group with the maplayer to the canvas*/ void addLayer( QgsMapLayer * layer ); + void setLayerVisible( QgsMapLayer * layer, bool visible ); + void setMapCanvas( QgsMapCanvas * canvas ); /**Updates symbology items for a layer*/ Index: src/app/legend/qgsapplegendinterface.cpp =================================================================== --- src/app/legend/qgsapplegendinterface.cpp (revision 13375) +++ src/app/legend/qgsapplegendinterface.cpp (working copy) @@ -54,11 +54,59 @@ } } +void QgsAppLegendInterface::setGroupExpanded( int groupIndex, bool expand ) +{ + mLegend->setExpanded( mLegend->model()->index( groupIndex, 0 ), expand ); +} + +void QgsAppLegendInterface::setGroupVisible( int groupIndex, bool visible ) +{ + if ( !groupExists( groupIndex ) ) + { + return; + } + + Qt::CheckState state = visible ? Qt::Checked : Qt::Unchecked; + mLegend->topLevelItem( groupIndex )->setCheckState( 0, state ); +} + +void QgsAppLegendInterface::setLayerVisible( QgsMapLayer * ml, bool visible ) +{ + mLegend->setLayerVisible( ml, visible ); +} + QStringList QgsAppLegendInterface::groups() { return mLegend->groups(); } +bool QgsAppLegendInterface::groupExists( int groupIndex ) +{ + QModelIndex mi = mLegend->model()->index( groupIndex, 0 ); + return ( mi.isValid() && + mLegend->isLegendGroup( mi ) ); +} + +bool QgsAppLegendInterface::isGroupExpanded( int groupIndex ) +{ + return mLegend->isExpanded( mLegend->model()->index( groupIndex, 0 ) ); +} + +bool QgsAppLegendInterface::isGroupVisible( int groupIndex ) +{ + if ( !groupExists( groupIndex ) ) + { + return false; + } + + return ( Qt::Checked == mLegend->topLevelItem( groupIndex )->checkState( 0 ) ); +} + +bool QgsAppLegendInterface::isLayerVisible( QgsMapLayer * ml ) +{ + return ( Qt::Checked == mLegend->layerCheckState( ml ) ); +} + QList< QgsMapLayer * > QgsAppLegendInterface::layers() const { QList< QgsMapLayer * > items; Index: src/gui/qgslegendinterface.h =================================================================== --- src/gui/qgslegendinterface.h (revision 13375) +++ src/gui/qgslegendinterface.h (working copy) @@ -48,6 +48,22 @@ //! @note added in 1.5 virtual QList< QgsMapLayer * > layers() const = 0; + //! Check if a group exists + //! @note added in 1.5 + virtual bool groupExists( int groupIndex ) = 0; + + //! Check if a group is expanded + //! @note added in 1.5 + virtual bool isGroupExpanded( int groupIndex ) = 0; + + //! Check if a group is visible + //! @note added in 1.5 + virtual bool isGroupVisible( int groupIndex ) = 0; + + //! Check if a layer is visible + //! @note added in 1.5 + virtual bool isLayerVisible( QgsMapLayer * ml ) = 0; + signals: //! emitted when a group index has changed @@ -64,8 +80,20 @@ //! Move a layer to a group virtual void moveLayer( QgsMapLayer * ml, int groupIndex ) = 0; + //! Collapse or expand a group + //! @note added in 1.5 + virtual void setGroupExpanded( int groupIndex, bool expand ) = 0; + + //! Set the visibility of a group + //! @note added in 1.5 + virtual void setGroupVisible( int groupIndex, bool visible ) = 0; + + //! Set the visibility of a layer + //! @note added in 1.5 + virtual void setLayerVisible( QgsMapLayer * ml, bool visible ) = 0; + //! Refresh layer symbology - // @noted added in 1.5 + //! @note added in 1.5 virtual void refreshLayerSymbology( QgsMapLayer *ml ) = 0; };