Skip to content

Commit

Permalink
Fix map layer combo box sometimes showing a selected layer which
Browse files Browse the repository at this point in the history
is not applied

This could also have been fixed by changing from the activated
signal to currentIndexChanged for the indexChanged connection,
but it looks like activated was intentionally used here.
  • Loading branch information
nyalldawson committed Jun 10, 2015
1 parent bf25186 commit 7fb4bea
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
11 changes: 10 additions & 1 deletion python/gui/qgsmaplayercombobox.sip
Expand Up @@ -28,9 +28,18 @@ class QgsMapLayerComboBox : QComboBox
//! returns the list of excepted layers
QList<QgsMapLayer*> 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 );
Expand Down
22 changes: 20 additions & 2 deletions src/gui/qgsmaplayercombobox.cpp
Expand Up @@ -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 )
Expand All @@ -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;
Expand All @@ -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 );
}
}

12 changes: 11 additions & 1 deletion src/gui/qgsmaplayercombobox.h
Expand Up @@ -52,9 +52,18 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
//! returns the list of excepted layers
QList<QgsMapLayer*> 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 );
Expand All @@ -65,6 +74,7 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox

protected slots:
void indexChanged( int i );
void rowsChanged();

private:
QgsMapLayerProxyModel* mProxyModel;
Expand Down

0 comments on commit 7fb4bea

Please sign in to comment.