From 73e690edd3301a8790513686a140cbf2576f338b Mon Sep 17 00:00:00 2001 From: jef Date: Sat, 25 Sep 2010 20:11:22 +0000 Subject: [PATCH] save feature attribute changes from plugins to layer git-svn-id: http://svn.osgeo.org/qgis/trunk@14284 c8812cc2-4d05-0410-92ff-de0c093fc19c --- python/gui/qgisinterface.sip | 2 +- src/app/qgisappinterface.cpp | 65 +++++++++++++++++++++++++++++++++--- src/app/qgisappinterface.h | 5 ++- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/python/gui/qgisinterface.sip b/python/gui/qgisinterface.sip index dbb61df9172..68558dde143 100644 --- a/python/gui/qgisinterface.sip +++ b/python/gui/qgisinterface.sip @@ -258,7 +258,7 @@ class QgisInterface : QObject //! open feature form // @added in 1.6 - virtual bool openFeatureForm( QgsVectorLayer *l, QgsFeature &f ) = 0; + virtual bool openFeatureForm( QgsVectorLayer *l, QgsFeature &f, bool updateFeatureOnly = false ) = 0; signals: /** Emited whenever current (selected) layer changes. diff --git a/src/app/qgisappinterface.cpp b/src/app/qgisappinterface.cpp index 83bd82599e1..40f06c2e396 100644 --- a/src/app/qgisappinterface.cpp +++ b/src/app/qgisappinterface.cpp @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include "qgisappinterface.h" #include "qgisapp.h" @@ -32,6 +34,8 @@ #include "qgsattributedialog.h" #include "qgsfield.h" #include "qgsvectordataprovider.h" +#include "qgsfeatureaction.h" +#include "qgsattributeaction.h" QgisAppInterface::QgisAppInterface( QgisApp * _qgis ) : qgis( _qgis ), @@ -356,7 +360,7 @@ QAction *QgisAppInterface::actionCheckQgisVersion() { return qgis->actionCheckQg QAction *QgisAppInterface::actionHelpSeparator2() { return qgis->actionHelpSeparator2(); } QAction *QgisAppInterface::actionAbout() { return qgis->actionAbout(); } -bool QgisAppInterface::openFeatureForm( QgsVectorLayer *vlayer, QgsFeature &f ) +bool QgisAppInterface::openFeatureForm( QgsVectorLayer *vlayer, QgsFeature &f, bool updateFeatureOnly ) { if ( !vlayer ) return false; @@ -373,8 +377,61 @@ bool QgisAppInterface::openFeatureForm( QgsVectorLayer *vlayer, QgsFeature &f ) } } - QgsAttributeDialog *mypDialog = new QgsAttributeDialog( vlayer, &f ); - bool res = mypDialog->exec(); - delete mypDialog; + QgsAttributeMap src = f.attributeMap(); + + if ( !updateFeatureOnly && vlayer->isEditable() ) + vlayer->beginEditCommand( tr( "Feature form edit" ) ); + + QgsAttributeDialog *ad = new QgsAttributeDialog( vlayer, &f ); + + if ( vlayer->actions()->size() > 0 ) + { + ad->dialog()->setContextMenuPolicy( Qt::ActionsContextMenu ); + + QAction *a = new QAction( tr( "Run actions" ), ad->dialog() ); + a->setEnabled( false ); + ad->dialog()->addAction( a ); + + for ( int i = 0; i < vlayer->actions()->size(); i++ ) + { + const QgsAction &action = vlayer->actions()->at( i ); + + if ( !action.runable() ) + continue; + + QgsFeatureAction *a = new QgsFeatureAction( action.name(), f, vlayer, i, ad->dialog() ); + ad->dialog()->addAction( a ); + connect( a, SIGNAL( triggered() ), a, SLOT( execute() ) ); + + QAbstractButton *pb = ad->dialog()->findChild( action.name() ); + if ( pb ) + connect( pb, SIGNAL( clicked() ), a, SLOT( execute() ) ); + } + } + + bool res = ad->exec(); + + if ( !updateFeatureOnly && vlayer->isEditable() ) + { + if ( res ) + { + const QgsAttributeMap &dst = f.attributeMap(); + for ( QgsAttributeMap::const_iterator it = dst.begin(); it != dst.end(); it++ ) + { + if ( !src.contains( it.key() ) || it.value() != src[it.key()] ) + { + vlayer->changeAttributeValue( f.id(), it.key(), it.value() ); + } + } + vlayer->endEditCommand(); + } + else + { + vlayer->destroyEditCommand(); + } + } + + delete ad; + return res; } diff --git a/src/app/qgisappinterface.h b/src/app/qgisappinterface.h index 33b3bca855f..c392462f53d 100644 --- a/src/app/qgisappinterface.h +++ b/src/app/qgisappinterface.h @@ -264,8 +264,11 @@ class QgisAppInterface : public QgisInterface //! open feature form // returns true when dialog was accepted + // @param l vector layer + // @param f feature to show/modify + // @param updateFeatureOnly only update the feature update (don't change any attributes of the layer) // @added in 1.6 - virtual bool openFeatureForm( QgsVectorLayer *l, QgsFeature &f ); + virtual bool openFeatureForm( QgsVectorLayer *l, QgsFeature &f, bool updateFeatureOnly = false ); signals: void currentThemeChanged( QString );