Fix add features dialog

Fix #10362
This commit is contained in:
Matthias Kuhn 2014-05-27 18:09:34 +02:00
parent ee770c4f81
commit 644dd13c3a
6 changed files with 39 additions and 52 deletions

View File

@ -36,6 +36,7 @@ QgsFeatureAction::QgsFeatureAction( const QString &name, QgsFeature &f, QgsVecto
, mFeature( f ) , mFeature( f )
, mAction( action ) , mAction( action )
, mIdx( defaultAttr ) , mIdx( defaultAttr )
, mFeatureSaved( false )
{ {
} }
@ -176,10 +177,6 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes )
} }
} }
bool res = false;
// show the dialog to enter attribute values // show the dialog to enter attribute values
bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool(); bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool();
// override application-wide setting with any layer setting // override application-wide setting with any layer setting
@ -197,46 +194,51 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes )
if ( isDisabledAttributeValuesDlg ) if ( isDisabledAttributeValuesDlg )
{ {
mLayer->beginEditCommand( text() ); mLayer->beginEditCommand( text() );
res = mLayer->addFeature( mFeature ); mFeatureSaved = mLayer->addFeature( mFeature );
if ( res ) if ( mFeatureSaved )
mLayer->endEditCommand(); mLayer->endEditCommand();
else else
mLayer->destroyEditCommand(); mLayer->destroyEditCommand();
} }
else else
{ {
QgsAttributes origValues;
if ( reuseLastValues )
origValues = mFeature.attributes();
QgsAttributeDialog *dialog = newDialog( false ); QgsAttributeDialog *dialog = newDialog( false );
dialog->setIsAddDialog( true ); dialog->setIsAddDialog( true );
dialog->setEditCommandMessage( text() ); dialog->setEditCommandMessage( text() );
connect( dialog->attributeForm(), SIGNAL( featureSaved( QgsFeature ) ), this, SLOT( onFeatureSaved( QgsFeature ) ) );
dialog->exec(); dialog->exec();
if ( reuseLastValues )
{
connect( dialog->dialog(), SIGNAL( featureSaved( const QgsFeature& feature ) ), this, SLOT( updateLastUsedValues( const QgsFeature& feature ) ) );
}
} }
return res; // Will be set in the onFeatureSaved SLOT
return mFeatureSaved;
} }
void QgsFeatureAction::updateLastUsedValues( const QgsFeature& feature ) void QgsFeatureAction::onFeatureSaved( const QgsFeature& feature )
{ {
QgsAttributeForm* form = qobject_cast<QgsAttributeForm*>( sender() ); QgsAttributeForm* form = qobject_cast<QgsAttributeForm*>( sender() );
Q_ASSERT( form ); Q_ASSERT( form );
QgsFields fields = mLayer->pendingFields(); mFeatureSaved = true;
for ( int idx = 0; idx < fields.count(); ++idx )
QSettings settings;
bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool();
QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );
if ( reuseLastValues )
{ {
const QgsAttributes &newValues = feature.attributes(); QgsFields fields = mLayer->pendingFields();
QgsAttributeMap origValues = sLastUsedValues[ mLayer ]; for ( int idx = 0; idx < fields.count(); ++idx )
if ( origValues[idx] != newValues[idx] )
{ {
QgsDebugMsg( QString( "saving %1 for %2" ).arg( sLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) ); const QgsAttributes &newValues = feature.attributes();
sLastUsedValues[ mLayer ][idx] = newValues[idx]; QgsAttributeMap origValues = sLastUsedValues[ mLayer ];
if ( origValues[idx] != newValues[idx] )
{
QgsDebugMsg( QString( "saving %1 for %2" ).arg( sLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) );
sLastUsedValues[ mLayer ][idx] = newValues[idx];
}
} }
} }
} }

View File

@ -53,7 +53,7 @@ class APP_EXPORT QgsFeatureAction : public QAction
bool addFeature( const QgsAttributeMap& defaultAttributes = QgsAttributeMap() ); bool addFeature( const QgsAttributeMap& defaultAttributes = QgsAttributeMap() );
private slots: private slots:
void updateLastUsedValues( const QgsFeature& feature ); void onFeatureSaved( const QgsFeature& feature );
private: private:
QgsAttributeDialog *newDialog( bool cloneFeature ); QgsAttributeDialog *newDialog( bool cloneFeature );
@ -63,6 +63,8 @@ class APP_EXPORT QgsFeatureAction : public QAction
int mAction; int mAction;
int mIdx; int mIdx;
bool mFeatureSaved;
static QMap<QgsVectorLayer *, QgsAttributeMap> sLastUsedValues; static QMap<QgsVectorLayer *, QgsAttributeMap> sLastUsedValues;
}; };

View File

@ -133,7 +133,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
//grass provider has its own mechanism of feature addition //grass provider has its own mechanism of feature addition
if ( provider->capabilities() & QgsVectorDataProvider::AddFeatures ) if ( provider->capabilities() & QgsVectorDataProvider::AddFeatures )
{ {
QgsFeature* f = new QgsFeature( vlayer->pendingFields(), 0 ); QgsFeature f( vlayer->pendingFields(), 0 );
QgsGeometry *g = 0; QgsGeometry *g = 0;
if ( layerWKBType == QGis::WKBPoint || layerWKBType == QGis::WKBPoint25D ) if ( layerWKBType == QGis::WKBPoint || layerWKBType == QGis::WKBPoint25D )
@ -145,19 +145,9 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
g = QgsGeometry::fromMultiPoint( QgsMultiPoint() << savePoint ); g = QgsGeometry::fromMultiPoint( QgsMultiPoint() << savePoint );
} }
f->setGeometry( g ); f.setGeometry( g );
vlayer->beginEditCommand( tr( "Feature added" ) ); addFeature( vlayer, &f );
if ( addFeature( vlayer, f ) )
{
vlayer->endEditCommand();
}
else
{
delete f;
vlayer->destroyEditCommand();
}
mCanvas->refresh(); mCanvas->refresh();
} }
@ -307,8 +297,6 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
} }
} }
vlayer->beginEditCommand( tr( "Feature added" ) );
if ( addFeature( vlayer, f ) ) if ( addFeature( vlayer, f ) )
{ {
//add points to other features to keep topology up-to-date //add points to other features to keep topology up-to-date
@ -336,13 +324,6 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
{ {
vlayer->addTopologicalPoints( f->geometry() ); vlayer->addTopologicalPoints( f->geometry() );
} }
vlayer->endEditCommand();
}
else
{
delete f;
vlayer->destroyEditCommand();
} }
stopCapturing(); stopCapturing();

View File

@ -149,6 +149,6 @@ void QgsAttributeDialog::init( QgsVectorLayer* layer, QgsFeature* feature, QgsAt
mAttributeForm = new QgsAttributeForm( layer, *feature, context, parent ); mAttributeForm = new QgsAttributeForm( layer, *feature, context, parent );
mDialog->layout()->addWidget( mAttributeForm ); mDialog->layout()->addWidget( mAttributeForm );
QDialogButtonBox* buttonBox = mAttributeForm->findChild<QDialogButtonBox*>(); QDialogButtonBox* buttonBox = mAttributeForm->findChild<QDialogButtonBox*>();
connect( buttonBox, SIGNAL( rejected() ), mDialog, SLOT( close() ) ); connect( buttonBox, SIGNAL( rejected() ), mDialog, SLOT( reject() ) );
connect( buttonBox, SIGNAL( accepted() ), mDialog, SLOT( close() ) ); connect( buttonBox, SIGNAL( accepted() ), mDialog, SLOT( accept() ) );
} }

View File

@ -84,6 +84,8 @@ class GUI_EXPORT QgsAttributeDialog : public QObject
QDialog* dialog() { return mDialog; } QDialog* dialog() { return mDialog; }
QgsAttributeForm* attributeForm() { return mAttributeForm; }
const QgsFeature* feature() { return &mAttributeForm->feature(); } const QgsFeature* feature() { return &mAttributeForm->feature(); }
/** /**

View File

@ -132,6 +132,7 @@ bool QgsAttributeForm::save()
if ( !success ) if ( !success )
return false; return false;
QgsFeature updatedFeature = QgsFeature( mFeature );
if ( mFeature.isValid() || mIsAddDialog ) if ( mFeature.isValid() || mIsAddDialog )
{ {
@ -161,7 +162,6 @@ bool QgsAttributeForm::save()
} }
} }
QgsFeature updatedFeature = QgsFeature( mFeature );
updatedFeature.setAttributes( dst ); updatedFeature.setAttributes( dst );
Q_FOREACH( QgsAttributeFormInterface* iface, mInterfaces ) Q_FOREACH( QgsAttributeFormInterface* iface, mInterfaces )
@ -174,8 +174,6 @@ bool QgsAttributeForm::save()
if ( doUpdate ) if ( doUpdate )
{ {
mLayer->beginEditCommand( mEditCommandMessage );
if ( mIsAddDialog ) if ( mIsAddDialog )
{ {
mFeature.setValid( true ); mFeature.setValid( true );
@ -188,6 +186,8 @@ bool QgsAttributeForm::save()
} }
else else
{ {
mLayer->beginEditCommand( mEditCommandMessage );
for ( int i = 0; i < dst.count(); ++i ) for ( int i = 0; i < dst.count(); ++i )
{ {
if ( dst[i] == src[i] || !src[i].isValid() ) if ( dst[i] == src[i] || !src[i].isValid() )
@ -209,7 +209,7 @@ bool QgsAttributeForm::save()
} }
} }
emit featureSaved( mFeature ); emit featureSaved( updatedFeature );
mIsSaving = false; mIsSaving = false;