Remove child features when parent's add feature form is cancelled

This commit is contained in:
Matthias Kuhn 2016-05-17 00:18:05 +02:00
parent 307aabd66a
commit 8c402bcf42
8 changed files with 192 additions and 6 deletions

View File

@ -151,6 +151,7 @@
%Include qgssvgannotationitem.sip
%Include qgstablewidgetitem.sip
%Include qgstextannotationitem.sip
%Include qgstrackedvectorlayertools.sip
%Include qgsunitselectionwidget.sip
%Include qgsuserinputdockwidget.sip
%Include qgsvariableeditorwidget.sip

View File

@ -0,0 +1,38 @@
/***************************************************************************
qgstrackedvectorlayertools.sip - QgsTrackedVectorLayerTools
---------------------
begin : 16.5.2016
copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
class QgsTrackedVectorLayerTools : QgsVectorLayerTools
{
%TypeHeaderCode
#include "qgstrackedvectorlayertools.h"
%End
public:
QgsTrackedVectorLayerTools();
bool addFeature( QgsVectorLayer* layer, const QgsAttributeMap& defaultValues, const QgsGeometry& defaultGeometry, QgsFeature* feature ) const;
bool startEditing( QgsVectorLayer* layer ) const ;
bool stopEditing( QgsVectorLayer* layer, bool allowCancel ) const ;
bool saveEdits( QgsVectorLayer* layer ) const ;
/**
* Set the vector layer tools that will be used to interact with the data
*/
void setVectorLayerTools(const QgsVectorLayerTools* tools );
/**
* Delete all features which have been added via this object.
*/
void rollback();
};

View File

@ -280,6 +280,7 @@ SET(QGIS_GUI_SRCS
qgssvgannotationitem.cpp
qgstablewidgetitem.cpp
qgstextannotationitem.cpp
qgstrackedvectorlayertools.cpp
qgsunitselectionwidget.cpp
qgsuserinputdockwidget.cpp
qgsvariableeditorwidget.cpp
@ -601,6 +602,7 @@ SET(QGIS_GUI_HDRS
qgssvgannotationitem.h
qgstablewidgetitem.h
qgstextannotationitem.h
qgstrackedvectorlayertools.h
qgsuserinputdockwidget.h
qgsvectorlayertools.h
qgsvertexmarker.h

View File

@ -97,12 +97,25 @@ void QgsAttributeDialog::show( bool autoDelete )
activateWindow();
}
void QgsAttributeDialog::init( QgsVectorLayer* layer, QgsFeature* feature, const QgsAttributeEditorContext &context )
void QgsAttributeDialog::reject()
{
// Delete any actions on other layers that may have been triggered from this dialog
if ( mAttributeForm->mode() == QgsAttributeForm::AddFeatureMode )
mTrackedVectorLayerTools.rollback();
QDialog::reject();
}
void QgsAttributeDialog::init( QgsVectorLayer* layer, QgsFeature* feature, const QgsAttributeEditorContext& context )
{
QgsAttributeEditorContext trackedContext = context;
setWindowTitle( tr( "%1 - Feature Attributes" ).arg( layer->name() ) );
setLayout( new QGridLayout() );
layout()->setMargin( 0 );
mAttributeForm = new QgsAttributeForm( layer, *feature, context, this );
mTrackedVectorLayerTools.setVectorLayerTools( trackedContext.vectorLayerTools() );
trackedContext.setVectorLayerTools( &mTrackedVectorLayerTools );
mAttributeForm = new QgsAttributeForm( layer, *feature, trackedContext, this );
mAttributeForm->disconnectButtonBox();
layout()->addWidget( mAttributeForm );
QDialogButtonBox* buttonBox = mAttributeForm->findChild<QDialogButtonBox*>();

View File

@ -20,17 +20,14 @@
#include "qgsfeature.h"
#include "qgsattributeeditorcontext.h"
#include "qgsattributeform.h"
#include "qgstrackedvectorlayertools.h"
#include <QDialog>
#include <QMenuBar>
#include <QGridLayout>
class QgsDistanceArea;
class QgsFeature;
class QgsField;
class QgsHighlight;
class QgsVectorLayer;
class QgsVectorLayerTools;
class GUI_EXPORT QgsAttributeDialog : public QDialog
{
@ -137,6 +134,7 @@ class GUI_EXPORT QgsAttributeDialog : public QDialog
public slots:
void accept() override;
void reject() override;
//! Show the dialog non-blocking. Reparents this dialog to be a child of the dialog form and is deleted when
//! closed.
@ -154,11 +152,14 @@ class GUI_EXPORT QgsAttributeDialog : public QDialog
QgsAttributeForm* mAttributeForm;
QgsFeature *mOwnedFeature;
QgsTrackedVectorLayerTools mTrackedVectorLayerTools;
// true if this dialog is editable
bool mEditable;
static int sFormCounter;
static QString sSettingsPath;
};
#endif

View File

@ -0,0 +1,75 @@
/***************************************************************************
qgstrackedvectorlayertools.cpp - QgsTrackedVectorLayerTools
---------------------
begin : 16.5.2016
copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgstrackedvectorlayertools.h"
#include "qgsvectorlayer.h"
QgsTrackedVectorLayerTools::QgsTrackedVectorLayerTools()
: mBackend( nullptr )
{
}
bool QgsTrackedVectorLayerTools::addFeature( QgsVectorLayer* layer, const QgsAttributeMap& defaultValues, const QgsGeometry& defaultGeometry, QgsFeature* feature ) const
{
QgsFeature* f = feature;
if ( !feature )
f = new QgsFeature();
if ( mBackend->addFeature( layer, defaultValues, defaultGeometry, f ) )
{
mAddedFeatures[layer].insert( f->id() );
if ( !feature )
delete f;
return true;
}
else
{
if ( !feature )
delete f;
return false;
}
}
bool QgsTrackedVectorLayerTools::startEditing( QgsVectorLayer* layer ) const
{
return mBackend->startEditing( layer );
}
bool QgsTrackedVectorLayerTools::stopEditing( QgsVectorLayer* layer, bool allowCancel ) const
{
return mBackend->stopEditing( layer, allowCancel );
}
bool QgsTrackedVectorLayerTools::saveEdits( QgsVectorLayer* layer ) const
{
return mBackend->saveEdits( layer );
}
void QgsTrackedVectorLayerTools::setVectorLayerTools( const QgsVectorLayerTools* tools )
{
mBackend = tools;
}
void QgsTrackedVectorLayerTools::rollback()
{
QMapIterator<QgsVectorLayer*, QgsFeatureIds> it( mAddedFeatures );
while ( it.hasNext() )
{
it.next();
it.key()->deleteFeatures( it.value() );
}
mAddedFeatures.clear();
}

View File

@ -0,0 +1,48 @@
/***************************************************************************
qgstrackedvectorlayertools.h - QgsTrackedVectorLayerTools
---------------------
begin : 16.5.2016
copyright : (C) 2016 by Matthias Kuhn, OPENGIS.ch
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSTRACKEDVECTORLAYERTOOLS_H
#define QGSTRACKEDVECTORLAYERTOOLS_H
#include "qgsvectorlayertools.h"
class GUI_EXPORT QgsTrackedVectorLayerTools : public QgsVectorLayerTools
{
public:
QgsTrackedVectorLayerTools();
bool addFeature( QgsVectorLayer* layer, const QgsAttributeMap& defaultValues, const QgsGeometry& defaultGeometry, QgsFeature* feature ) const override;
bool startEditing( QgsVectorLayer* layer ) const override;
bool stopEditing( QgsVectorLayer* layer, bool allowCancel ) const override;
bool saveEdits( QgsVectorLayer* layer ) const override;
/**
* Set the vector layer tools that will be used to interact with the data
*/
void setVectorLayerTools( const QgsVectorLayerTools* tools );
/**
* Delete all features which have been added via this object.
*/
void rollback();
private:
const QgsVectorLayerTools* mBackend;
// TODO QGIS3: remove mutable once methods are no longer const
mutable QMap<QgsVectorLayer*, QgsFeatureIds> mAddedFeatures;
};
#endif // QGSTRACKEDVECTORLAYERTOOLS_H

View File

@ -45,6 +45,8 @@ class GUI_EXPORT QgsVectorLayerTools
* @param defaultGeometry A default geometry to add to the feature
* @param feature Updated feature after adding will be written back to this
* @return True in case of success, False if the operation failed/was aborted
*
* TODO QGIS 3: remove const qualifier
*/
virtual bool addFeature( QgsVectorLayer* layer, const QgsAttributeMap& defaultValues = QgsAttributeMap(), const QgsGeometry& defaultGeometry = QgsGeometry(), QgsFeature* feature = nullptr ) const = 0;
@ -56,6 +58,8 @@ class GUI_EXPORT QgsVectorLayerTools
* @param layer The layer on which to start an edit session
*
* @return True, if the editing session was started
*
* TODO QGIS 3: remove const qualifier
*/
virtual bool startEditing( QgsVectorLayer* layer ) const = 0;
@ -66,6 +70,8 @@ class GUI_EXPORT QgsVectorLayerTools
* @param layer The layer to commit
* @param allowCancel True if a cancel button should be offered
* @return True if successful
*
* TODO QGIS 3: remove const qualifier
*/
virtual bool stopEditing( QgsVectorLayer* layer, bool allowCancel = true ) const = 0;
@ -74,6 +80,8 @@ class GUI_EXPORT QgsVectorLayerTools
*
* @param layer The layer to commit
* @return True if successful
*
* TODO QGIS 3: remove const qualifier
*/
virtual bool saveEdits( QgsVectorLayer* layer ) const = 0;