Skip to content

Commit 644dd13

Browse files
committed
Fix add features dialog
Fix #10362
1 parent ee770c4 commit 644dd13

6 files changed

+39
-52
lines changed

src/app/qgsfeatureaction.cpp

+25-23
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ QgsFeatureAction::QgsFeatureAction( const QString &name, QgsFeature &f, QgsVecto
3636
, mFeature( f )
3737
, mAction( action )
3838
, mIdx( defaultAttr )
39+
, mFeatureSaved( false )
3940
{
4041
}
4142

@@ -176,10 +177,6 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes )
176177
}
177178
}
178179

179-
bool res = false;
180-
181-
182-
183180
// show the dialog to enter attribute values
184181
bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool();
185182
// override application-wide setting with any layer setting
@@ -197,46 +194,51 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes )
197194
if ( isDisabledAttributeValuesDlg )
198195
{
199196
mLayer->beginEditCommand( text() );
200-
res = mLayer->addFeature( mFeature );
197+
mFeatureSaved = mLayer->addFeature( mFeature );
201198

202-
if ( res )
199+
if ( mFeatureSaved )
203200
mLayer->endEditCommand();
204201
else
205202
mLayer->destroyEditCommand();
206203
}
207204
else
208205
{
209-
QgsAttributes origValues;
210-
if ( reuseLastValues )
211-
origValues = mFeature.attributes();
212-
213206
QgsAttributeDialog *dialog = newDialog( false );
214207
dialog->setIsAddDialog( true );
215208
dialog->setEditCommandMessage( text() );
209+
210+
connect( dialog->attributeForm(), SIGNAL( featureSaved( QgsFeature ) ), this, SLOT( onFeatureSaved( QgsFeature ) ) );
211+
216212
dialog->exec();
217-
if ( reuseLastValues )
218-
{
219-
connect( dialog->dialog(), SIGNAL( featureSaved( const QgsFeature& feature ) ), this, SLOT( updateLastUsedValues( const QgsFeature& feature ) ) );
220-
}
221213
}
222214

223-
return res;
215+
// Will be set in the onFeatureSaved SLOT
216+
return mFeatureSaved;
224217
}
225218

226-
void QgsFeatureAction::updateLastUsedValues( const QgsFeature& feature )
219+
void QgsFeatureAction::onFeatureSaved( const QgsFeature& feature )
227220
{
228221
QgsAttributeForm* form = qobject_cast<QgsAttributeForm*>( sender() );
229222
Q_ASSERT( form );
230223

231-
QgsFields fields = mLayer->pendingFields();
232-
for ( int idx = 0; idx < fields.count(); ++idx )
224+
mFeatureSaved = true;
225+
226+
QSettings settings;
227+
bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool();
228+
QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );
229+
230+
if ( reuseLastValues )
233231
{
234-
const QgsAttributes &newValues = feature.attributes();
235-
QgsAttributeMap origValues = sLastUsedValues[ mLayer ];
236-
if ( origValues[idx] != newValues[idx] )
232+
QgsFields fields = mLayer->pendingFields();
233+
for ( int idx = 0; idx < fields.count(); ++idx )
237234
{
238-
QgsDebugMsg( QString( "saving %1 for %2" ).arg( sLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) );
239-
sLastUsedValues[ mLayer ][idx] = newValues[idx];
235+
const QgsAttributes &newValues = feature.attributes();
236+
QgsAttributeMap origValues = sLastUsedValues[ mLayer ];
237+
if ( origValues[idx] != newValues[idx] )
238+
{
239+
QgsDebugMsg( QString( "saving %1 for %2" ).arg( sLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) );
240+
sLastUsedValues[ mLayer ][idx] = newValues[idx];
241+
}
240242
}
241243
}
242244
}

src/app/qgsfeatureaction.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class APP_EXPORT QgsFeatureAction : public QAction
5353
bool addFeature( const QgsAttributeMap& defaultAttributes = QgsAttributeMap() );
5454

5555
private slots:
56-
void updateLastUsedValues( const QgsFeature& feature );
56+
void onFeatureSaved( const QgsFeature& feature );
5757

5858
private:
5959
QgsAttributeDialog *newDialog( bool cloneFeature );
@@ -63,6 +63,8 @@ class APP_EXPORT QgsFeatureAction : public QAction
6363
int mAction;
6464
int mIdx;
6565

66+
bool mFeatureSaved;
67+
6668
static QMap<QgsVectorLayer *, QgsAttributeMap> sLastUsedValues;
6769
};
6870

src/app/qgsmaptooladdfeature.cpp

+3-22
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
133133
//grass provider has its own mechanism of feature addition
134134
if ( provider->capabilities() & QgsVectorDataProvider::AddFeatures )
135135
{
136-
QgsFeature* f = new QgsFeature( vlayer->pendingFields(), 0 );
136+
QgsFeature f( vlayer->pendingFields(), 0 );
137137

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

148-
f->setGeometry( g );
148+
f.setGeometry( g );
149149

150-
vlayer->beginEditCommand( tr( "Feature added" ) );
151-
152-
if ( addFeature( vlayer, f ) )
153-
{
154-
vlayer->endEditCommand();
155-
}
156-
else
157-
{
158-
delete f;
159-
vlayer->destroyEditCommand();
160-
}
150+
addFeature( vlayer, &f );
161151

162152
mCanvas->refresh();
163153
}
@@ -307,8 +297,6 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
307297
}
308298
}
309299

310-
vlayer->beginEditCommand( tr( "Feature added" ) );
311-
312300
if ( addFeature( vlayer, f ) )
313301
{
314302
//add points to other features to keep topology up-to-date
@@ -336,13 +324,6 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
336324
{
337325
vlayer->addTopologicalPoints( f->geometry() );
338326
}
339-
340-
vlayer->endEditCommand();
341-
}
342-
else
343-
{
344-
delete f;
345-
vlayer->destroyEditCommand();
346327
}
347328

348329
stopCapturing();

src/gui/qgsattributedialog.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,6 @@ void QgsAttributeDialog::init( QgsVectorLayer* layer, QgsFeature* feature, QgsAt
149149
mAttributeForm = new QgsAttributeForm( layer, *feature, context, parent );
150150
mDialog->layout()->addWidget( mAttributeForm );
151151
QDialogButtonBox* buttonBox = mAttributeForm->findChild<QDialogButtonBox*>();
152-
connect( buttonBox, SIGNAL( rejected() ), mDialog, SLOT( close() ) );
153-
connect( buttonBox, SIGNAL( accepted() ), mDialog, SLOT( close() ) );
152+
connect( buttonBox, SIGNAL( rejected() ), mDialog, SLOT( reject() ) );
153+
connect( buttonBox, SIGNAL( accepted() ), mDialog, SLOT( accept() ) );
154154
}

src/gui/qgsattributedialog.h

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class GUI_EXPORT QgsAttributeDialog : public QObject
8484

8585
QDialog* dialog() { return mDialog; }
8686

87+
QgsAttributeForm* attributeForm() { return mAttributeForm; }
88+
8789
const QgsFeature* feature() { return &mAttributeForm->feature(); }
8890

8991
/**

src/gui/qgsattributeform.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ bool QgsAttributeForm::save()
132132
if ( !success )
133133
return false;
134134

135+
QgsFeature updatedFeature = QgsFeature( mFeature );
135136

136137
if ( mFeature.isValid() || mIsAddDialog )
137138
{
@@ -161,7 +162,6 @@ bool QgsAttributeForm::save()
161162
}
162163
}
163164

164-
QgsFeature updatedFeature = QgsFeature( mFeature );
165165
updatedFeature.setAttributes( dst );
166166

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

175175
if ( doUpdate )
176176
{
177-
mLayer->beginEditCommand( mEditCommandMessage );
178-
179177
if ( mIsAddDialog )
180178
{
181179
mFeature.setValid( true );
@@ -188,6 +186,8 @@ bool QgsAttributeForm::save()
188186
}
189187
else
190188
{
189+
mLayer->beginEditCommand( mEditCommandMessage );
190+
191191
for ( int i = 0; i < dst.count(); ++i )
192192
{
193193
if ( dst[i] == src[i] || !src[i].isValid() )
@@ -209,7 +209,7 @@ bool QgsAttributeForm::save()
209209
}
210210
}
211211

212-
emit featureSaved( mFeature );
212+
emit featureSaved( updatedFeature );
213213

214214
mIsSaving = false;
215215

0 commit comments

Comments
 (0)