save feature attribute changes from plugins to layer

git-svn-id: http://svn.osgeo.org/qgis/trunk@14284 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
jef 2010-09-25 20:11:22 +00:00
parent c8c01721e0
commit 73e690edd3
3 changed files with 66 additions and 6 deletions

View File

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

View File

@ -20,6 +20,8 @@
#include <QFileInfo>
#include <QString>
#include <QMenu>
#include <QDialog>
#include <QAbstractButton>
#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<QAbstractButton *>( 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;
}

View File

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