Skip to content

Commit

Permalink
fix crash deleting expression field when layer is not editable
Browse files Browse the repository at this point in the history
  • Loading branch information
brushtyler committed Sep 29, 2015
1 parent 5b0bfc6 commit 7028457
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2954,6 +2954,7 @@ const QList< QgsVectorJoinInfo > QgsVectorLayer::vectorJoins() const

int QgsVectorLayer::addExpressionField( const QString& exp, const QgsField& fld )
{
emit beforeAddingExpressionField( fld.name() );
mExpressionFieldBuffer->addExpression( exp, fld );
updateFields();
int idx = mUpdatedFields.indexFromName( fld.name() );
Expand All @@ -2963,6 +2964,7 @@ int QgsVectorLayer::addExpressionField( const QString& exp, const QgsField& fld

void QgsVectorLayer::removeExpressionField( int index )
{
emit beforeRemovingExpressionField( index );
int oi = mUpdatedFields.fieldOriginIndex( index );
mExpressionFieldBuffer->removeExpression( oi );
updateFields();
Expand Down
19 changes: 17 additions & 2 deletions src/core/qgsvectorlayer.h
Expand Up @@ -1874,22 +1874,37 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer

/**
* Will be emitted, when a new attribute has been added to this vector layer.
* Applies only to types {@link QgsFields::OriginEdit} and {@link QgsFields::OriginProvider}
* Applies only to types {@link QgsFields::OriginEdit}, {@link QgsFields::OriginProvider} and {@link QgsFields::OriginExpression }
*
* @param idx The index of the new attribute
*
* @see updatedFields()
*/
void attributeAdded( int idx );
/**
* Will be emitted, when an expression field is going to be added to this vector layer.
* Applies only to types {@link QgsFields::OriginExpression }
*
* @param fieldName The name of the attribute to be added
*/
void beforeAddingExpressionField( QString fieldName );
/**
* Will be emitted, when an attribute has been deleted from this vector layer.
* Applies only to types {@link QgsFields::OriginEdit} and {@link QgsFields::OriginProvider}
* Applies only to types {@link QgsFields::OriginEdit}, {@link QgsFields::OriginProvider} and {@link QgsFields::OriginExpression }
*
* @param idx The index of the deleted attribute
*
* @see updatedFields()
*/
void attributeDeleted( int idx );
/**
* Will be emitted, when an expression field is going to be deleted from this vector layer.
* Applies only to types {@link QgsFields::OriginExpression }
*
* @param idx The index of the attribute to be deleted
*/
void beforeRemovingExpressionField( int idx );

/**
* Emitted when a new feature has been added to the layer
*
Expand Down
13 changes: 11 additions & 2 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -45,6 +45,7 @@ QgsAttributeForm::QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature &featur
, mFormNr( sFormCounter++ )
, mIsSaving( false )
, mIsAddDialog( false )
, mPreventFeatureRefresh( false )
, mEditCommandMessage( tr( "Attributes changed" ) )
{
init();
Expand All @@ -53,6 +54,8 @@ QgsAttributeForm::QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature &featur

connect( vl, SIGNAL( attributeAdded( int ) ), this, SLOT( onAttributeAdded( int ) ) );
connect( vl, SIGNAL( attributeDeleted( int ) ), this, SLOT( onAttributeDeleted( int ) ) );
connect( vl, SIGNAL( beforeAddingExpressionField( QString ) ), this, SLOT( preventFeatureRefresh() ) );
connect( vl, SIGNAL( beforeRemovingExpressionField( int ) ), this, SLOT( preventFeatureRefresh() ) );
}

QgsAttributeForm::~QgsAttributeForm()
Expand Down Expand Up @@ -274,7 +277,7 @@ void QgsAttributeForm::onAttributeChanged( const QVariant& value )

void QgsAttributeForm::onAttributeAdded( int idx )
{
Q_UNUSED( idx ) // only used for Q_ASSERT
mPreventFeatureRefresh = false;
if ( mFeature.isValid() )
{
QgsAttributes attrs = mFeature.attributes();
Expand All @@ -288,6 +291,7 @@ void QgsAttributeForm::onAttributeAdded( int idx )

void QgsAttributeForm::onAttributeDeleted( int idx )
{
mPreventFeatureRefresh = false;
if ( mFeature.isValid() )
{
QgsAttributes attrs = mFeature.attributes();
Expand All @@ -299,9 +303,14 @@ void QgsAttributeForm::onAttributeDeleted( int idx )
setFeature( mFeature );
}

void QgsAttributeForm::preventFeatureRefresh()
{
mPreventFeatureRefresh = true;
}

void QgsAttributeForm::refreshFeature()
{
if ( mLayer->isEditable() || !mFeature.isValid() )
if ( mPreventFeatureRefresh || mLayer->isEditable() || !mFeature.isValid() )
return;

// reload feature if layer changed although not editable
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgsattributeform.h
Expand Up @@ -174,6 +174,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
void onAttributeAdded( int idx );
void onAttributeDeleted( int idx );

void preventFeatureRefresh();
void synchronizeEnabledState();

private:
Expand Down Expand Up @@ -210,6 +211,9 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
bool mIsSaving;
bool mIsAddDialog;

//! Flag to prevent refreshFeature() to change mFeature
bool mPreventFeatureRefresh;

QString mEditCommandMessage;
};

Expand Down

0 comments on commit 7028457

Please sign in to comment.