Skip to content

Commit

Permalink
Keep attributes when adding 1-N related features with a multi-edition…
Browse files Browse the repository at this point in the history
… attribute form
  • Loading branch information
Djedouas committed Jul 23, 2024
1 parent 623ceab commit 7010edc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
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

0 comments on commit 7010edc

Please sign in to comment.