diff --git a/python/gui/qgsmaplayercombobox.sip b/python/gui/qgsmaplayercombobox.sip index 06b1c3fa362..ac3a57f3513 100644 --- a/python/gui/qgsmaplayercombobox.sip +++ b/python/gui/qgsmaplayercombobox.sip @@ -28,9 +28,18 @@ class QgsMapLayerComboBox : QComboBox //! returns the list of excepted layers QList exceptedLayerList() const; - //! currentLayer returns the current layer selected in the combo box + /** Returns the current layer selected in the combo box. + * @see layer + */ QgsMapLayer* currentLayer() const; + /** Return the layer currently shown at the specified index within the combo box. + * @param layerIndex position of layer to return + * @note added in QGIS 2.10 + * @see currentLayer + */ + QgsMapLayer* layer( int layerIndex ) const; + public slots: //! setLayer set the current layer selected in the combo void setLayer( QgsMapLayer* layer ); diff --git a/src/gui/qgsmaplayercombobox.cpp b/src/gui/qgsmaplayercombobox.cpp index 0efc00f35e4..610d70a474a 100644 --- a/src/gui/qgsmaplayercombobox.cpp +++ b/src/gui/qgsmaplayercombobox.cpp @@ -24,6 +24,8 @@ QgsMapLayerComboBox::QgsMapLayerComboBox( QWidget *parent ) : setModel( mProxyModel ); connect( this, SIGNAL( activated( int ) ), this, SLOT( indexChanged( int ) ) ); + connect( mProxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) ); + connect( mProxyModel, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) ); } void QgsMapLayerComboBox::setLayer( QgsMapLayer *layer ) @@ -45,9 +47,12 @@ void QgsMapLayerComboBox::setLayer( QgsMapLayer *layer ) QgsMapLayer* QgsMapLayerComboBox::currentLayer() const { - int i = currentIndex(); + return layer( currentIndex() ); +} - const QModelIndex proxyIndex = mProxyModel->index( i, 0 ); +QgsMapLayer *QgsMapLayerComboBox::layer( int layerIndex ) const +{ + const QModelIndex proxyIndex = mProxyModel->index( layerIndex, 0 ); if ( !proxyIndex.isValid() ) { return 0; @@ -74,3 +79,16 @@ void QgsMapLayerComboBox::indexChanged( int i ) emit layerChanged( layer ); } +void QgsMapLayerComboBox::rowsChanged() +{ + if ( count() == 1 ) + { + //currently selected layer item has changed + emit layerChanged( currentLayer() ); + } + else if ( count() == 0 ) + { + emit layerChanged( 0 ); + } +} + diff --git a/src/gui/qgsmaplayercombobox.h b/src/gui/qgsmaplayercombobox.h index 65101837279..fa76cb5af32 100644 --- a/src/gui/qgsmaplayercombobox.h +++ b/src/gui/qgsmaplayercombobox.h @@ -52,9 +52,18 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox //! returns the list of excepted layers QList exceptedLayerList() const {return mProxyModel->exceptedLayerList();} - //! currentLayer returns the current layer selected in the combo box + /** Returns the current layer selected in the combo box. + * @see layer + */ QgsMapLayer* currentLayer() const; + /** Return the layer currently shown at the specified index within the combo box. + * @param layerIndex position of layer to return + * @note added in QGIS 2.10 + * @see currentLayer + */ + QgsMapLayer* layer( int layerIndex ) const; + public slots: //! setLayer set the current layer selected in the combo void setLayer( QgsMapLayer* layer ); @@ -65,6 +74,7 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox protected slots: void indexChanged( int i ); + void rowsChanged(); private: QgsMapLayerProxyModel* mProxyModel;