Skip to content

Commit

Permalink
Fix add features dialog
Browse files Browse the repository at this point in the history
Fix #10362
  • Loading branch information
m-kuhn committed May 27, 2014
1 parent ee770c4 commit 644dd13
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 52 deletions.
48 changes: 25 additions & 23 deletions src/app/qgsfeatureaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ QgsFeatureAction::QgsFeatureAction( const QString &name, QgsFeature &f, QgsVecto
, mFeature( f )
, mAction( action )
, mIdx( defaultAttr )
, mFeatureSaved( false )
{
}

Expand Down Expand Up @@ -176,10 +177,6 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes )
}
}

bool res = false;



// show the dialog to enter attribute values
bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool();
// override application-wide setting with any layer setting
Expand All @@ -197,46 +194,51 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes )
if ( isDisabledAttributeValuesDlg )
{
mLayer->beginEditCommand( text() );
res = mLayer->addFeature( mFeature );
mFeatureSaved = mLayer->addFeature( mFeature );

if ( res )
if ( mFeatureSaved )
mLayer->endEditCommand();
else
mLayer->destroyEditCommand();
}
else
{
QgsAttributes origValues;
if ( reuseLastValues )
origValues = mFeature.attributes();

QgsAttributeDialog *dialog = newDialog( false );
dialog->setIsAddDialog( true );
dialog->setEditCommandMessage( text() );

connect( dialog->attributeForm(), SIGNAL( featureSaved( QgsFeature ) ), this, SLOT( onFeatureSaved( QgsFeature ) ) );

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() );
Q_ASSERT( form );

QgsFields fields = mLayer->pendingFields();
for ( int idx = 0; idx < fields.count(); ++idx )
mFeatureSaved = true;

QSettings settings;
bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool();
QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );

if ( reuseLastValues )
{
const QgsAttributes &newValues = feature.attributes();
QgsAttributeMap origValues = sLastUsedValues[ mLayer ];
if ( origValues[idx] != newValues[idx] )
QgsFields fields = mLayer->pendingFields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
QgsDebugMsg( QString( "saving %1 for %2" ).arg( sLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) );
sLastUsedValues[ mLayer ][idx] = newValues[idx];
const QgsAttributes &newValues = feature.attributes();
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];
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/qgsfeatureaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class APP_EXPORT QgsFeatureAction : public QAction
bool addFeature( const QgsAttributeMap& defaultAttributes = QgsAttributeMap() );

private slots:
void updateLastUsedValues( const QgsFeature& feature );
void onFeatureSaved( const QgsFeature& feature );

private:
QgsAttributeDialog *newDialog( bool cloneFeature );
Expand All @@ -63,6 +63,8 @@ class APP_EXPORT QgsFeatureAction : public QAction
int mAction;
int mIdx;

bool mFeatureSaved;

static QMap<QgsVectorLayer *, QgsAttributeMap> sLastUsedValues;
};

Expand Down
25 changes: 3 additions & 22 deletions src/app/qgsmaptooladdfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
//grass provider has its own mechanism of feature addition
if ( provider->capabilities() & QgsVectorDataProvider::AddFeatures )
{
QgsFeature* f = new QgsFeature( vlayer->pendingFields(), 0 );
QgsFeature f( vlayer->pendingFields(), 0 );

QgsGeometry *g = 0;
if ( layerWKBType == QGis::WKBPoint || layerWKBType == QGis::WKBPoint25D )
Expand All @@ -145,19 +145,9 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
g = QgsGeometry::fromMultiPoint( QgsMultiPoint() << savePoint );
}

f->setGeometry( g );
f.setGeometry( g );

vlayer->beginEditCommand( tr( "Feature added" ) );

if ( addFeature( vlayer, f ) )
{
vlayer->endEditCommand();
}
else
{
delete f;
vlayer->destroyEditCommand();
}
addFeature( vlayer, &f );

mCanvas->refresh();
}
Expand Down Expand Up @@ -307,8 +297,6 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
}
}

vlayer->beginEditCommand( tr( "Feature added" ) );

if ( addFeature( vlayer, f ) )
{
//add points to other features to keep topology up-to-date
Expand Down Expand Up @@ -336,13 +324,6 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
{
vlayer->addTopologicalPoints( f->geometry() );
}

vlayer->endEditCommand();
}
else
{
delete f;
vlayer->destroyEditCommand();
}

stopCapturing();
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsattributedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ void QgsAttributeDialog::init( QgsVectorLayer* layer, QgsFeature* feature, QgsAt
mAttributeForm = new QgsAttributeForm( layer, *feature, context, parent );
mDialog->layout()->addWidget( mAttributeForm );
QDialogButtonBox* buttonBox = mAttributeForm->findChild<QDialogButtonBox*>();
connect( buttonBox, SIGNAL( rejected() ), mDialog, SLOT( close() ) );
connect( buttonBox, SIGNAL( accepted() ), mDialog, SLOT( close() ) );
connect( buttonBox, SIGNAL( rejected() ), mDialog, SLOT( reject() ) );
connect( buttonBox, SIGNAL( accepted() ), mDialog, SLOT( accept() ) );
}
2 changes: 2 additions & 0 deletions src/gui/qgsattributedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class GUI_EXPORT QgsAttributeDialog : public QObject

QDialog* dialog() { return mDialog; }

QgsAttributeForm* attributeForm() { return mAttributeForm; }

const QgsFeature* feature() { return &mAttributeForm->feature(); }

/**
Expand Down
8 changes: 4 additions & 4 deletions src/gui/qgsattributeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ bool QgsAttributeForm::save()
if ( !success )
return false;

QgsFeature updatedFeature = QgsFeature( mFeature );

if ( mFeature.isValid() || mIsAddDialog )
{
Expand Down Expand Up @@ -161,7 +162,6 @@ bool QgsAttributeForm::save()
}
}

QgsFeature updatedFeature = QgsFeature( mFeature );
updatedFeature.setAttributes( dst );

Q_FOREACH( QgsAttributeFormInterface* iface, mInterfaces )
Expand All @@ -174,8 +174,6 @@ bool QgsAttributeForm::save()

if ( doUpdate )
{
mLayer->beginEditCommand( mEditCommandMessage );

if ( mIsAddDialog )
{
mFeature.setValid( true );
Expand All @@ -188,6 +186,8 @@ bool QgsAttributeForm::save()
}
else
{
mLayer->beginEditCommand( mEditCommandMessage );

for ( int i = 0; i < dst.count(); ++i )
{
if ( dst[i] == src[i] || !src[i].isValid() )
Expand All @@ -209,7 +209,7 @@ bool QgsAttributeForm::save()
}
}

emit featureSaved( mFeature );
emit featureSaved( updatedFeature );

mIsSaving = false;

Expand Down

0 comments on commit 644dd13

Please sign in to comment.