Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep attributes when adding 1-N related features with a multi-edition attribute form #58223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ Returns whether the parent widget should be hidden when showing tools' dialogues
void setHideParent( bool hide );
%Docstring
Sets whether the parent widget should be hidden when showing tools' dialogues.
%End

bool forceSuppressFormPopup() const;
%Docstring
Returns whether the add feature form popup should be shown
%End
void setForceSuppressFormPopup( bool forceSuppressFormPopup );
%Docstring
Sets whether the add feature form popup should be shown
%End

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ Returns whether the parent widget should be hidden when showing tools' dialogues
void setHideParent( bool hide );
%Docstring
Sets whether the parent widget should be hidden when showing tools' dialogues.
%End

bool forceSuppressFormPopup() const;
%Docstring
Returns whether the add feature form popup should be shown
%End
void setForceSuppressFormPopup( bool forceSuppressFormPopup );
%Docstring
Sets whether the add feature form popup should be shown
%End

};
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsguivectorlayertools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ bool QgsGuiVectorLayerTools::addFeatureV2( QgsVectorLayer *layer, const QgsAttri
f->setGeometry( defaultGeometry );
QgsFeatureAction *a = new QgsFeatureAction( tr( "Add feature" ), *f, layer, QUuid(), -1, context.parentWidget() );
a->setForceSuppressFormPopup( forceSuppressFormPopup() );

// Override with context
if ( context.forceSuppressFormPopup() )
a->setForceSuppressFormPopup( true );

connect( a, &QgsFeatureAction::addFeatureFinished, a, &QObject::deleteLater );
const QgsFeatureAction::AddFeatureResult result = a->addFeature( defaultValues, context.showModal(), std::unique_ptr<QgsExpressionContextScope>( context.additionalExpressionContextScope() ? new QgsExpressionContextScope( *context.additionalExpressionContextScope() ) : nullptr ), context.hideParent() );
if ( !feature )
Expand Down
11 changes: 11 additions & 0 deletions src/core/vector/qgsvectorlayertoolscontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ class CORE_EXPORT QgsVectorLayerToolsContext
*/
void setHideParent( bool hide ) { mHideParent = hide; }

/**
* Returns whether the add feature form popup should be shown
*/
bool forceSuppressFormPopup() const { return mForceSuppressFormPopup; };

/**
* Sets whether the add feature form popup should be shown
*/
void setForceSuppressFormPopup( bool forceSuppressFormPopup ) { mForceSuppressFormPopup = forceSuppressFormPopup; }

private:

std::unique_ptr< QgsExpressionContext > mExpressionContext;
Expand All @@ -109,6 +119,7 @@ class CORE_EXPORT QgsVectorLayerToolsContext
QWidget *mParentWidget = nullptr;
bool mShowModal = true;
bool mHideParent = false;
bool mForceSuppressFormPopup = false;
};

#endif // QGSVECTORLAYERTOOLSCONTEXT_H
41 changes: 21 additions & 20 deletions src/gui/qgsabstractrelationeditorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "qgsproject.h"
#include "qgstransactiongroup.h"
#include "qgsvectorlayerutils.h"
#include "qgssettingsregistrycore.h"
#include "qgssettingsentryimpl.h"

#include <QMessageBox>
#include <QPushButton>
Expand Down Expand Up @@ -283,35 +285,34 @@ QgsFeatureIds QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &ge
}
else
{
const auto constFieldPairs = mRelation.fieldPairs();
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
keyAttrs.insert( fields.indexFromName( fieldPair.referencingField() ), mFeatureList.first().attribute( fieldPair.referencedField() ) );

const bool prevReuseAllLastValues = QgsSettingsRegistryCore::settingsDigitizingReuseLastValues->value();
QgsVectorLayerToolsContext context;
context.setParentWidget( this );
context.setShowModal( false );
context.setHideParent( true );
std::unique_ptr<QgsExpressionContextScope> scope( QgsExpressionContextUtils::parentFormScope( mFeatureList.first(), mEditorContext.attributeFormModeString() ) );
context.setAdditionalExpressionContextScope( scope.get() );
QgsFeature linkFeature;
if ( !vlTools->addFeatureV2( mRelation.referencingLayer(), keyAttrs, geometry, &linkFeature, context ) )
return QgsFeatureIds();

addedFeatureIds.insert( linkFeature.id() );

// In multiedit add to other features to but without dialog
for ( const QgsFeature &feature : std::as_const( mFeatureList ) )
for ( const QgsFeature &editingFeature : std::as_const( mFeatureList ) )
{
// First feature already added
if ( mFeatureList.first() == feature )
continue;

const auto constFieldPairs = mRelation.fieldPairs();
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
linkFeature.setAttribute( fields.indexFromName( fieldPair.referencingField() ), feature.attribute( fieldPair.referencedField() ) );
keyAttrs.insert( fields.indexFromName( fieldPair.referencingField() ), editingFeature.attribute( fieldPair.referencedField() ) );

mRelation.referencingLayer()->addFeature( linkFeature );
// In multiedit add to other features too but without dialog
if ( editingFeature != mFeatureList.first() )
{
QgsSettingsRegistryCore::settingsDigitizingReuseLastValues->setValue( true );
context.setForceSuppressFormPopup( true );
}
std::unique_ptr<QgsExpressionContextScope> scope( QgsExpressionContextUtils::parentFormScope( editingFeature, mEditorContext.attributeFormModeString() ) );
context.setAdditionalExpressionContextScope( scope.get() );
QgsFeature linkFeature;
if ( !vlTools->addFeatureV2( mRelation.referencingLayer(), keyAttrs, geometry, &linkFeature, context ) )
{
QgsSettingsRegistryCore::settingsDigitizingReuseLastValues->setValue( prevReuseAllLastValues );
return QgsFeatureIds();
}
addedFeatureIds.insert( linkFeature.id() );
}
QgsSettingsRegistryCore::settingsDigitizingReuseLastValues->setValue( prevReuseAllLastValues );
}

updateUi();
Expand Down
Loading