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.
This commit is contained in:
Nyall Dawson 2015-06-10 16:03:38 +10:00
parent bf25186a76
commit 7fb4bea279
3 changed files with 41 additions and 4 deletions

View File

@ -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 );

View File

@ -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 );
}
}

View File

@ -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;