Skip to content

Commit 8c402bc

Browse files
committedMay 17, 2016
Remove child features when parent's add feature form is cancelled
1 parent 307aabd commit 8c402bc

8 files changed

+192
-6
lines changed
 

‎python/gui/gui.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
%Include qgssvgannotationitem.sip
152152
%Include qgstablewidgetitem.sip
153153
%Include qgstextannotationitem.sip
154+
%Include qgstrackedvectorlayertools.sip
154155
%Include qgsunitselectionwidget.sip
155156
%Include qgsuserinputdockwidget.sip
156157
%Include qgsvariableeditorwidget.sip
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/***************************************************************************
2+
qgstrackedvectorlayertools.sip - QgsTrackedVectorLayerTools
3+
4+
---------------------
5+
begin : 16.5.2016
6+
copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
class QgsTrackedVectorLayerTools : QgsVectorLayerTools
17+
{
18+
%TypeHeaderCode
19+
#include "qgstrackedvectorlayertools.h"
20+
%End
21+
public:
22+
QgsTrackedVectorLayerTools();
23+
24+
bool addFeature( QgsVectorLayer* layer, const QgsAttributeMap& defaultValues, const QgsGeometry& defaultGeometry, QgsFeature* feature ) const;
25+
bool startEditing( QgsVectorLayer* layer ) const ;
26+
bool stopEditing( QgsVectorLayer* layer, bool allowCancel ) const ;
27+
bool saveEdits( QgsVectorLayer* layer ) const ;
28+
29+
/**
30+
* Set the vector layer tools that will be used to interact with the data
31+
*/
32+
void setVectorLayerTools(const QgsVectorLayerTools* tools );
33+
34+
/**
35+
* Delete all features which have been added via this object.
36+
*/
37+
void rollback();
38+
};

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ SET(QGIS_GUI_SRCS
280280
qgssvgannotationitem.cpp
281281
qgstablewidgetitem.cpp
282282
qgstextannotationitem.cpp
283+
qgstrackedvectorlayertools.cpp
283284
qgsunitselectionwidget.cpp
284285
qgsuserinputdockwidget.cpp
285286
qgsvariableeditorwidget.cpp
@@ -601,6 +602,7 @@ SET(QGIS_GUI_HDRS
601602
qgssvgannotationitem.h
602603
qgstablewidgetitem.h
603604
qgstextannotationitem.h
605+
qgstrackedvectorlayertools.h
604606
qgsuserinputdockwidget.h
605607
qgsvectorlayertools.h
606608
qgsvertexmarker.h

‎src/gui/qgsattributedialog.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,25 @@ void QgsAttributeDialog::show( bool autoDelete )
9797
activateWindow();
9898
}
9999

100-
void QgsAttributeDialog::init( QgsVectorLayer* layer, QgsFeature* feature, const QgsAttributeEditorContext &context )
100+
void QgsAttributeDialog::reject()
101101
{
102+
// Delete any actions on other layers that may have been triggered from this dialog
103+
if ( mAttributeForm->mode() == QgsAttributeForm::AddFeatureMode )
104+
mTrackedVectorLayerTools.rollback();
105+
106+
QDialog::reject();
107+
}
108+
109+
void QgsAttributeDialog::init( QgsVectorLayer* layer, QgsFeature* feature, const QgsAttributeEditorContext& context )
110+
{
111+
QgsAttributeEditorContext trackedContext = context;
102112
setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
103113
setLayout( new QGridLayout() );
104114
layout()->setMargin( 0 );
105-
mAttributeForm = new QgsAttributeForm( layer, *feature, context, this );
115+
mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
116+
trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
117+
118+
mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
106119
mAttributeForm->disconnectButtonBox();
107120
layout()->addWidget( mAttributeForm );
108121
QDialogButtonBox* buttonBox = mAttributeForm->findChild<QDialogButtonBox*>();

‎src/gui/qgsattributedialog.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020
#include "qgsfeature.h"
2121
#include "qgsattributeeditorcontext.h"
2222
#include "qgsattributeform.h"
23+
#include "qgstrackedvectorlayertools.h"
2324

2425
#include <QDialog>
2526
#include <QMenuBar>
2627
#include <QGridLayout>
2728

2829
class QgsDistanceArea;
29-
class QgsFeature;
30-
class QgsField;
3130
class QgsHighlight;
32-
class QgsVectorLayer;
33-
class QgsVectorLayerTools;
3431

3532
class GUI_EXPORT QgsAttributeDialog : public QDialog
3633
{
@@ -137,6 +134,7 @@ class GUI_EXPORT QgsAttributeDialog : public QDialog
137134

138135
public slots:
139136
void accept() override;
137+
void reject() override;
140138

141139
//! Show the dialog non-blocking. Reparents this dialog to be a child of the dialog form and is deleted when
142140
//! closed.
@@ -154,11 +152,14 @@ class GUI_EXPORT QgsAttributeDialog : public QDialog
154152
QgsAttributeForm* mAttributeForm;
155153
QgsFeature *mOwnedFeature;
156154

155+
QgsTrackedVectorLayerTools mTrackedVectorLayerTools;
156+
157157
// true if this dialog is editable
158158
bool mEditable;
159159

160160
static int sFormCounter;
161161
static QString sSettingsPath;
162+
162163
};
163164

164165
#endif
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/***************************************************************************
2+
qgstrackedvectorlayertools.cpp - QgsTrackedVectorLayerTools
3+
4+
---------------------
5+
begin : 16.5.2016
6+
copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#include "qgstrackedvectorlayertools.h"
17+
#include "qgsvectorlayer.h"
18+
19+
QgsTrackedVectorLayerTools::QgsTrackedVectorLayerTools()
20+
: mBackend( nullptr )
21+
{
22+
}
23+
24+
bool QgsTrackedVectorLayerTools::addFeature( QgsVectorLayer* layer, const QgsAttributeMap& defaultValues, const QgsGeometry& defaultGeometry, QgsFeature* feature ) const
25+
{
26+
QgsFeature* f = feature;
27+
if ( !feature )
28+
f = new QgsFeature();
29+
30+
if ( mBackend->addFeature( layer, defaultValues, defaultGeometry, f ) )
31+
{
32+
mAddedFeatures[layer].insert( f->id() );
33+
if ( !feature )
34+
delete f;
35+
return true;
36+
}
37+
else
38+
{
39+
if ( !feature )
40+
delete f;
41+
return false;
42+
}
43+
}
44+
45+
bool QgsTrackedVectorLayerTools::startEditing( QgsVectorLayer* layer ) const
46+
{
47+
return mBackend->startEditing( layer );
48+
}
49+
50+
bool QgsTrackedVectorLayerTools::stopEditing( QgsVectorLayer* layer, bool allowCancel ) const
51+
{
52+
return mBackend->stopEditing( layer, allowCancel );
53+
}
54+
55+
bool QgsTrackedVectorLayerTools::saveEdits( QgsVectorLayer* layer ) const
56+
{
57+
return mBackend->saveEdits( layer );
58+
}
59+
60+
void QgsTrackedVectorLayerTools::setVectorLayerTools( const QgsVectorLayerTools* tools )
61+
{
62+
mBackend = tools;
63+
}
64+
65+
void QgsTrackedVectorLayerTools::rollback()
66+
{
67+
QMapIterator<QgsVectorLayer*, QgsFeatureIds> it( mAddedFeatures );
68+
while ( it.hasNext() )
69+
{
70+
it.next();
71+
it.key()->deleteFeatures( it.value() );
72+
}
73+
74+
mAddedFeatures.clear();
75+
}

‎src/gui/qgstrackedvectorlayertools.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/***************************************************************************
2+
qgstrackedvectorlayertools.h - QgsTrackedVectorLayerTools
3+
4+
---------------------
5+
begin : 16.5.2016
6+
copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSTRACKEDVECTORLAYERTOOLS_H
17+
#define QGSTRACKEDVECTORLAYERTOOLS_H
18+
19+
#include "qgsvectorlayertools.h"
20+
21+
class GUI_EXPORT QgsTrackedVectorLayerTools : public QgsVectorLayerTools
22+
{
23+
public:
24+
QgsTrackedVectorLayerTools();
25+
26+
bool addFeature( QgsVectorLayer* layer, const QgsAttributeMap& defaultValues, const QgsGeometry& defaultGeometry, QgsFeature* feature ) const override;
27+
bool startEditing( QgsVectorLayer* layer ) const override;
28+
bool stopEditing( QgsVectorLayer* layer, bool allowCancel ) const override;
29+
bool saveEdits( QgsVectorLayer* layer ) const override;
30+
31+
/**
32+
* Set the vector layer tools that will be used to interact with the data
33+
*/
34+
void setVectorLayerTools( const QgsVectorLayerTools* tools );
35+
36+
/**
37+
* Delete all features which have been added via this object.
38+
*/
39+
void rollback();
40+
41+
private:
42+
43+
const QgsVectorLayerTools* mBackend;
44+
// TODO QGIS3: remove mutable once methods are no longer const
45+
mutable QMap<QgsVectorLayer*, QgsFeatureIds> mAddedFeatures;
46+
};
47+
48+
#endif // QGSTRACKEDVECTORLAYERTOOLS_H

‎src/gui/qgsvectorlayertools.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class GUI_EXPORT QgsVectorLayerTools
4545
* @param defaultGeometry A default geometry to add to the feature
4646
* @param feature Updated feature after adding will be written back to this
4747
* @return True in case of success, False if the operation failed/was aborted
48+
*
49+
* TODO QGIS 3: remove const qualifier
4850
*/
4951
virtual bool addFeature( QgsVectorLayer* layer, const QgsAttributeMap& defaultValues = QgsAttributeMap(), const QgsGeometry& defaultGeometry = QgsGeometry(), QgsFeature* feature = nullptr ) const = 0;
5052

@@ -56,6 +58,8 @@ class GUI_EXPORT QgsVectorLayerTools
5658
* @param layer The layer on which to start an edit session
5759
*
5860
* @return True, if the editing session was started
61+
*
62+
* TODO QGIS 3: remove const qualifier
5963
*/
6064
virtual bool startEditing( QgsVectorLayer* layer ) const = 0;
6165

@@ -66,6 +70,8 @@ class GUI_EXPORT QgsVectorLayerTools
6670
* @param layer The layer to commit
6771
* @param allowCancel True if a cancel button should be offered
6872
* @return True if successful
73+
*
74+
* TODO QGIS 3: remove const qualifier
6975
*/
7076
virtual bool stopEditing( QgsVectorLayer* layer, bool allowCancel = true ) const = 0;
7177

@@ -74,6 +80,8 @@ class GUI_EXPORT QgsVectorLayerTools
7480
*
7581
* @param layer The layer to commit
7682
* @return True if successful
83+
*
84+
* TODO QGIS 3: remove const qualifier
7785
*/
7886
virtual bool saveEdits( QgsVectorLayer* layer ) const = 0;
7987

0 commit comments

Comments
 (0)
Please sign in to comment.