Skip to content
Permalink
Browse files

Fix map layer combo box sometimes showing a selected layer which

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 7fb4bea279fb52f5f9212e41860433fea982e361
Showing with 41 additions and 4 deletions.
  1. +10 −1 python/gui/qgsmaplayercombobox.sip
  2. +20 −2 src/gui/qgsmaplayercombobox.cpp
  3. +11 −1 src/gui/qgsmaplayercombobox.h
@@ -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 );
@@ -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 );
}
}

@@ -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 );
@@ -65,6 +74,7 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox

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

private:
QgsMapLayerProxyModel* mProxyModel;

0 comments on commit 7fb4bea

Please sign in to comment.
You can’t perform that action at this time.