Skip to content

Commit

Permalink
Don't show misleading error message when user CANCELS adding a
Browse files Browse the repository at this point in the history
feature from GPS location
  • Loading branch information
nyalldawson committed Nov 23, 2022
1 parent a876fa4 commit 84c8787
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 55 deletions.
85 changes: 52 additions & 33 deletions src/app/gps/qgsappgpsdigitizing.cpp
Expand Up @@ -323,28 +323,37 @@ void QgsAppGpsDigitizing::createFeature()
case QgsWkbTypes::PointGeometry:
{
QgsFeatureAction action( tr( "Feature Added" ), f, vlayer, QUuid(), -1, this );
if ( action.addFeature( attrMap ) )
const QgsFeatureAction::AddFeatureResult result = action.addFeature( attrMap );
switch ( result )
{
if ( QgsProject::instance()->gpsSettings()->automaticallyCommitFeatures() )
case QgsFeatureAction::AddFeatureResult::Success:
{
// should canvas->isDrawing() be checked?
if ( !vlayer->commitChanges() ) //assumed to be vector layer and is editable and is in editing mode (preconditions have been tested)
if ( QgsProject::instance()->gpsSettings()->automaticallyCommitFeatures() )
{
QgisApp::instance()->messageBar()->pushCritical(
tr( "Save Layer Edits" ),
tr( "Could not commit changes to layer %1\n\nErrors: %2\n" )
.arg( vlayer->name(),
vlayer->commitErrors().join( QLatin1String( "\n " ) ) ) );
}
// should canvas->isDrawing() be checked?
if ( !vlayer->commitChanges() ) //assumed to be vector layer and is editable and is in editing mode (preconditions have been tested)
{
QgisApp::instance()->messageBar()->pushCritical(
tr( "Save Layer Edits" ),
tr( "Could not commit changes to layer %1\n\nErrors: %2\n" )
.arg( vlayer->name(),
vlayer->commitErrors().join( QLatin1String( "\n " ) ) ) );
}

vlayer->startEditing();
vlayer->startEditing();
}
}
}
else
{
QgisApp::instance()->messageBar()->pushCritical( QString(), tr( "Could not create new feature in layer %1" ).arg( vlayer->name() ) );
}
break;

case QgsFeatureAction::AddFeatureResult::Canceled:
case QgsFeatureAction::AddFeatureResult::Pending:
break;

case QgsFeatureAction::AddFeatureResult::LayerStateError:
case QgsFeatureAction::AddFeatureResult::FeatureError:
QgisApp::instance()->messageBar()->pushCritical( QString(), tr( "Could not create new feature in layer %1" ).arg( vlayer->name() ) );
break;
}
break;
}

Expand All @@ -355,31 +364,41 @@ void QgsAppGpsDigitizing::createFeature()

QgsFeatureAction action( tr( "Feature added" ), f, vlayer, QUuid(), -1, this );

if ( action.addFeature( attrMap ) )
const QgsFeatureAction::AddFeatureResult result = action.addFeature( attrMap );
switch ( result )
{
if ( QgsProject::instance()->gpsSettings()->automaticallyCommitFeatures() )
case QgsFeatureAction::AddFeatureResult::Success:
{
if ( !vlayer->commitChanges() )
if ( QgsProject::instance()->gpsSettings()->automaticallyCommitFeatures() )
{
QgisApp::instance()->messageBar()->pushCritical( tr( "Save Layer Edits" ),
tr( "Could not commit changes to layer %1\n\nErrors: %2\n" )
.arg( vlayer->name(),
vlayer->commitErrors().join( QLatin1String( "\n " ) ) ) );
if ( !vlayer->commitChanges() )
{
QgisApp::instance()->messageBar()->pushCritical( tr( "Save Layer Edits" ),
tr( "Could not commit changes to layer %1\n\nErrors: %2\n" )
.arg( vlayer->name(),
vlayer->commitErrors().join( QLatin1String( "\n " ) ) ) );
}

vlayer->startEditing();
}
delete mRubberBand;
mRubberBand = nullptr;

vlayer->startEditing();
// delete the elements of mCaptureList
resetTrack();
}
delete mRubberBand;
mRubberBand = nullptr;
break;

// delete the elements of mCaptureList
resetTrack();
}
else
{
QgisApp::instance()->messageBar()->pushCritical( QString(), tr( "Could not create new feature in layer %1" ).arg( vlayer->name() ) );
}
case QgsFeatureAction::AddFeatureResult::Canceled:
case QgsFeatureAction::AddFeatureResult::Pending:
break;

case QgsFeatureAction::AddFeatureResult::LayerStateError:
case QgsFeatureAction::AddFeatureResult::FeatureError:
QgisApp::instance()->messageBar()->pushCritical( QString(), tr( "Could not create new feature in layer %1" ).arg( vlayer->name() ) );
break;

}
mBlockGpsStateChanged--;

break;
Expand Down
35 changes: 27 additions & 8 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -701,11 +701,21 @@ void QgsAttributeTableDialog::mActionAddFeatureViaAttributeTable_triggered()
QgsFeature f;
QgsFeatureAction action( tr( "Geometryless feature added" ), f, mLayer, QUuid(), -1, this );
action.setForceSuppressFormPopup( true ); // we're already showing the table, allowing users to enter the new feature's attributes directly
if ( action.addFeature() )

const QgsFeatureAction::AddFeatureResult result = action.addFeature();
switch ( result )
{
masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) );
mMainView->setCurrentEditSelection( QgsFeatureIds() << action.feature().id() );
mMainView->tableView()->scrollToFeature( action.feature().id(), 0 );
case QgsFeatureAction::AddFeatureResult::Success:
case QgsFeatureAction::AddFeatureResult::Pending:
masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) );
mMainView->setCurrentEditSelection( QgsFeatureIds() << action.feature().id() );
mMainView->tableView()->scrollToFeature( action.feature().id(), 0 );
break;

case QgsFeatureAction::AddFeatureResult::LayerStateError:
case QgsFeatureAction::AddFeatureResult::Canceled:
case QgsFeatureAction::AddFeatureResult::FeatureError:
break;
}
}

Expand All @@ -723,11 +733,20 @@ void QgsAttributeTableDialog::mActionAddFeatureViaAttributeForm_triggered()
QgsFeatureAction action( tr( "Feature Added" ), f, mLayer, QUuid(), -1, this );
QgsAttributeTableModel *masterModel = mMainView->masterModel();

if ( action.addFeature() )
const QgsFeatureAction::AddFeatureResult result = action.addFeature();
switch ( result )
{
masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) );
mMainView->setCurrentEditSelection( QgsFeatureIds() << action.feature().id() );
mMainView->tableView()->scrollToFeature( action.feature().id(), 0 );
case QgsFeatureAction::AddFeatureResult::Success:
case QgsFeatureAction::AddFeatureResult::Pending:
masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) );
mMainView->setCurrentEditSelection( QgsFeatureIds() << action.feature().id() );
mMainView->tableView()->scrollToFeature( action.feature().id(), 0 );
break;

case QgsFeatureAction::AddFeatureResult::LayerStateError:
case QgsFeatureAction::AddFeatureResult::Canceled:
case QgsFeatureAction::AddFeatureResult::FeatureError:
break;
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/app/qgsfeatureaction.cpp
Expand Up @@ -167,10 +167,10 @@ bool QgsFeatureAction::editFeature( bool showModal )
return true;
}

bool QgsFeatureAction::addFeature( const QgsAttributeMap &defaultAttributes, bool showModal, std::unique_ptr< QgsExpressionContextScope > scope, bool hideParent, std::unique_ptr<QgsHighlight> highlight )
QgsFeatureAction::AddFeatureResult QgsFeatureAction::addFeature( const QgsAttributeMap &defaultAttributes, bool showModal, std::unique_ptr< QgsExpressionContextScope > scope, bool hideParent, std::unique_ptr<QgsHighlight> highlight )
{
if ( !mLayer || !mLayer->isEditable() )
return false;
return AddFeatureResult::LayerStateError;

const bool reuseAllLastValues = QgsSettingsRegistryCore::settingsDigitizingReuseLastValues.value();
QgsDebugMsgLevel( QStringLiteral( "reuseAllLastValues: %1" ).arg( reuseAllLastValues ), 2 );
Expand Down Expand Up @@ -235,6 +235,7 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap &defaultAttributes, boo
if ( mForceSuppressFormPopup )
isDisabledAttributeValuesDlg = true;

bool dialogWasShown = false;
if ( isDisabledAttributeValuesDlg )
{
mLayer->beginEditCommand( text() );
Expand Down Expand Up @@ -277,15 +278,17 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap &defaultAttributes, boo
hideParentWidget();
}
mFeature = nullptr;
return true;
return AddFeatureResult::Pending;
}

dialogWasShown = true;
dialog->exec();
emit addFeatureFinished();
}

// Will be set in the onFeatureSaved SLOT
return mFeatureSaved;
// assume dialog was canceled if dialog was shown yet feature wasn't added
return mFeatureSaved ? AddFeatureResult::Success : ( dialogWasShown ? AddFeatureResult::Canceled : AddFeatureResult::FeatureError );
}

void QgsFeatureAction::setForceSuppressFormPopup( bool force )
Expand Down
21 changes: 15 additions & 6 deletions src/app/qgsfeatureaction.h
Expand Up @@ -39,6 +39,15 @@ class APP_EXPORT QgsFeatureAction : public QAction
public:
QgsFeatureAction( const QString &name, QgsFeature &f, QgsVectorLayer *vl, QUuid actionId = QUuid(), int defaultAttr = -1, QObject *parent = nullptr );

enum class AddFeatureResult
{
Success = 0,
LayerStateError = 1,
Canceled = 2,
Pending = 3,
FeatureError = 4
};

/**
* Add a new feature to the layer.
* Will set the default values to recently used or provider defaults based on settings
Expand All @@ -50,13 +59,13 @@ class APP_EXPORT QgsFeatureAction : public QAction
* \param hideParent If the parent widget should be hidden, when the used dialog is opened
* \param highlight Optional canvas highlight for feature
*
* \returns TRUE if feature was added if showModal is true. If showModal is FALSE, returns TRUE in every case
* \returns result if feature was added if showModal is true. If showModal is FALSE, returns Pending in every case
*/
bool addFeature( const QgsAttributeMap &defaultAttributes = QgsAttributeMap(),
bool showModal = true,
std::unique_ptr<QgsExpressionContextScope >scope = std::unique_ptr< QgsExpressionContextScope >(),
bool hideParent = false,
std::unique_ptr<QgsHighlight> highlight = std::unique_ptr<QgsHighlight>() );
AddFeatureResult addFeature( const QgsAttributeMap &defaultAttributes = QgsAttributeMap(),
bool showModal = true,
std::unique_ptr<QgsExpressionContextScope >scope = std::unique_ptr< QgsExpressionContextScope >(),
bool hideParent = false,
std::unique_ptr<QgsHighlight> highlight = std::unique_ptr<QgsHighlight>() );

public slots:
void execute();
Expand Down
16 changes: 14 additions & 2 deletions src/app/qgsguivectorlayertools.cpp
Expand Up @@ -35,10 +35,22 @@ bool QgsGuiVectorLayerTools::addFeature( QgsVectorLayer *layer, const QgsAttribu
QgsFeatureAction *a = new QgsFeatureAction( tr( "Add feature" ), *f, layer, QUuid(), -1, parentWidget );
a->setForceSuppressFormPopup( forceSuppressFormPopup() );
connect( a, &QgsFeatureAction::addFeatureFinished, a, &QObject::deleteLater );
const bool added = a->addFeature( defaultValues, showModal, nullptr, hideParent );
const QgsFeatureAction::AddFeatureResult result = a->addFeature( defaultValues, showModal, nullptr, hideParent );
if ( !feat )
delete f;
return added;

switch ( result )
{
case QgsFeatureAction::AddFeatureResult::Success:
case QgsFeatureAction::AddFeatureResult::Pending:
return true;

case QgsFeatureAction::AddFeatureResult::LayerStateError:
case QgsFeatureAction::AddFeatureResult::Canceled:
case QgsFeatureAction::AddFeatureResult::FeatureError:
return false;
}
BUILTIN_UNREACHABLE
}

bool QgsGuiVectorLayerTools::startEditing( QgsVectorLayer *layer ) const
Expand Down
15 changes: 13 additions & 2 deletions src/app/qgsmaptooladdfeature.cpp
Expand Up @@ -81,10 +81,21 @@ bool QgsMapToolAddFeature::addFeature( QgsVectorLayer *vlayer, const QgsFeature
highlight = createHighlight( vlayer, f );
}

const bool res = action->addFeature( QgsAttributeMap(), showModal, std::move( scope ), false, std::move( highlight ) );
const QgsFeatureAction::AddFeatureResult res = action->addFeature( QgsAttributeMap(), showModal, std::move( scope ), false, std::move( highlight ) );
if ( showModal )
delete action;
return res;

switch ( res )
{
case QgsFeatureAction::AddFeatureResult::Success:
case QgsFeatureAction::AddFeatureResult::Pending:
return true;
case QgsFeatureAction::AddFeatureResult::LayerStateError:
case QgsFeatureAction::AddFeatureResult::Canceled:
case QgsFeatureAction::AddFeatureResult::FeatureError:
return false;
}
BUILTIN_UNREACHABLE
}

void QgsMapToolAddFeature::featureDigitized( const QgsFeature &feature )
Expand Down

0 comments on commit 84c8787

Please sign in to comment.