From d70c854336f71383d6447773144fb6f161a8ea31 Mon Sep 17 00:00:00 2001 From: Radim Blazek Date: Tue, 29 Sep 2015 21:34:54 +0200 Subject: [PATCH] refresh QgsAttributeForm using updatedFields() signal to cover all changes --- src/gui/qgsattributeform.cpp | 34 ++++++++++++++++++++++++++++++++++ src/gui/qgsattributeform.h | 1 + 2 files changed, 35 insertions(+) diff --git a/src/gui/qgsattributeform.cpp b/src/gui/qgsattributeform.cpp index d9352978799..b10fbed366a 100644 --- a/src/gui/qgsattributeform.cpp +++ b/src/gui/qgsattributeform.cpp @@ -52,8 +52,13 @@ QgsAttributeForm::QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature &featur initPython(); setFeature( feature ); + // Using attributeAdded() attributeDeleted() are not emited on all fields changes (e.g. layer fields changed, + // joined fields changed) -> use updatedFields() instead +#if 0 connect( vl, SIGNAL( attributeAdded( int ) ), this, SLOT( onAttributeAdded( int ) ) ); connect( vl, SIGNAL( attributeDeleted( int ) ), this, SLOT( onAttributeDeleted( int ) ) ); +#endif + connect( vl, SIGNAL( updatedFields() ), this, SLOT( onUpdatedFields() ) ); connect( vl, SIGNAL( beforeAddingExpressionField( QString ) ), this, SLOT( preventFeatureRefresh() ) ); connect( vl, SIGNAL( beforeRemovingExpressionField( int ) ), this, SLOT( preventFeatureRefresh() ) ); } @@ -303,6 +308,35 @@ void QgsAttributeForm::onAttributeDeleted( int idx ) setFeature( mFeature ); } +void QgsAttributeForm::onUpdatedFields() +{ + mPreventFeatureRefresh = false; + if ( mFeature.isValid() ) + { + QgsAttributes attrs( layer()->fields().size() ); + for ( int i = 0; i < layer()->fields().size(); i++ ) + { + int idx = mFeature.fields()->indexFromName( layer()->fields()[i].name() ); + if ( idx != -1 ) + { + attrs[i] = mFeature.attributes()[idx]; + if ( mFeature.attributes()[idx].type() != layer()->fields()[i].type() ) + { + attrs[i].convert( layer()->fields()[i].type() ); + } + } + else + { + attrs[i] = QVariant( layer()->fields()[i].type() ); + } + } + mFeature.setFields( layer()->fields() ); + mFeature.setAttributes( attrs ); + } + init(); + setFeature( mFeature ); +} + void QgsAttributeForm::preventFeatureRefresh() { mPreventFeatureRefresh = true; diff --git a/src/gui/qgsattributeform.h b/src/gui/qgsattributeform.h index 59e9ddc892f..6b53a764681 100644 --- a/src/gui/qgsattributeform.h +++ b/src/gui/qgsattributeform.h @@ -173,6 +173,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget void onAttributeChanged( const QVariant& value ); void onAttributeAdded( int idx ); void onAttributeDeleted( int idx ); + void onUpdatedFields(); void preventFeatureRefresh(); void synchronizeEnabledState();