diff --git a/python/gui/qgslegendinterface.sip b/python/gui/qgslegendinterface.sip index 20d5bad7f9a..8f4e34570c7 100644 --- a/python/gui/qgslegendinterface.sip +++ b/python/gui/qgslegendinterface.sip @@ -21,7 +21,23 @@ class QgsLegendInterface : QObject //! 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: @@ -39,8 +55,20 @@ class QgsLegendInterface : QObject //! 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; }; diff --git a/src/app/legend/qgsapplegendinterface.cpp b/src/app/legend/qgsapplegendinterface.cpp index 28d575851b2..f1397fae4fb 100644 --- a/src/app/legend/qgsapplegendinterface.cpp +++ b/src/app/legend/qgsapplegendinterface.cpp @@ -54,11 +54,59 @@ void QgsAppLegendInterface::updateIndex( QModelIndex oldIndex, QModelIndex newIn } } +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; diff --git a/src/app/legend/qgsapplegendinterface.h b/src/app/legend/qgsapplegendinterface.h index fbf6cfdd314..6596e784d8a 100644 --- a/src/app/legend/qgsapplegendinterface.h +++ b/src/app/legend/qgsapplegendinterface.h @@ -38,7 +38,7 @@ class QgsAppLegendInterface : public QgsLegendInterface /** Constructor */ explicit QgsAppLegendInterface( QgsLegend * legend ); - /** Virtual destructor */ + /** Destructor */ ~QgsAppLegendInterface(); //! Return a string list of groups @@ -47,6 +47,18 @@ class QgsAppLegendInterface : public QgsLegendInterface //! 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 @@ class QgsAppLegendInterface : public QgsLegendInterface //! 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 ); diff --git a/src/app/legend/qgslegend.cpp b/src/app/legend/qgslegend.cpp index dacd3af8a74..97ad1499eac 100755 --- a/src/app/legend/qgslegend.cpp +++ b/src/app/legend/qgslegend.cpp @@ -480,6 +480,13 @@ void QgsLegend::initPixmaps() mPixmaps.mProjectionErrorPixmap = QgisApp::getThemePixmap( "/mIconProjectionProblem.png" ); } +Qt::CheckState QgsLegend::layerCheckState( QgsMapLayer * layer ) +{ + QgsLegendLayer * ll = findLegendLayer( layer ); + + return ll ? ll->checkState( 0 ) : Qt::Unchecked; +} + int QgsLegend::getItemPos( QTreeWidgetItem* item ) { int counter = 1; @@ -548,6 +555,16 @@ void QgsLegend::addLayer( QgsMapLayer * layer ) 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 ) diff --git a/src/app/legend/qgslegend.h b/src/app/legend/qgslegend.h index 37f2890fb34..d1bdb567b25 100755 --- a/src/app/legend/qgslegend.h +++ b/src/app/legend/qgslegend.h @@ -179,6 +179,9 @@ class QgsLegend : public QTreeWidget /**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 ); } @@ -187,6 +190,8 @@ class QgsLegend : public QTreeWidget /*!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*/ diff --git a/src/gui/qgslegendinterface.h b/src/gui/qgslegendinterface.h index 93f590d1d9f..7ed40709dbd 100644 --- a/src/gui/qgslegendinterface.h +++ b/src/gui/qgslegendinterface.h @@ -48,6 +48,22 @@ class GUI_EXPORT QgsLegendInterface : public QObject //! @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 @@ class GUI_EXPORT QgsLegendInterface : public QObject //! 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; };