Skip to content
Permalink
Browse files

Take changes of embedded forms into account

when clicking on ok of an attribute form, tell all embedded widgets (all
widgets actually) that we are going to close the form and it's the last
call to push changes to the edit buffer or they will be lost.

Fix #19137
  • Loading branch information
m-kuhn committed Jun 21, 2018
1 parent 353eb65 commit 0749835071e8c73f6cca82e7c7718144eb90de7f
@@ -153,6 +153,15 @@ Sets the editor widget's property collection, used for data defined overrides.
.. seealso:: :py:func:`dataDefinedProperties`

.. versionadded:: 3.0
%End

void notifyAboutToSave();
%Docstring
Notify this widget, that the containing form is about to save and
that any pending changes should be pushed to the edit buffer or they
might be lost.

.. versionadded:: 3.2
%End

protected:
@@ -97,6 +97,11 @@ QgsWidgetWrapper *QgsWidgetWrapper::fromWidget( QWidget *widget )
return widget->property( "EWV2Wrapper" ).value<QgsWidgetWrapper *>();
}

void QgsWidgetWrapper::notifyAboutToSave()
{
aboutToSave();
}

void QgsWidgetWrapper::initWidget( QWidget *editor )
{
Q_UNUSED( editor )
@@ -106,3 +111,8 @@ void QgsWidgetWrapper::setEnabled( bool enabled )
{
Q_UNUSED( enabled );
}

void QgsWidgetWrapper::aboutToSave()
{

}
@@ -190,6 +190,15 @@ class GUI_EXPORT QgsWidgetWrapper : public QObject
*/
void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mPropertyCollection = collection; }

/**
* Notify this widget, that the containing form is about to save and
* that any pending changes should be pushed to the edit buffer or they
* might be lost.
*
* \since QGIS 3.2
*/
void notifyAboutToSave();

protected:

/**
@@ -234,6 +243,15 @@ class GUI_EXPORT QgsWidgetWrapper : public QObject
virtual void setEnabled( bool enabled );

private:

/**
* Called when the containing attribute form is about to save.
* Use this to push any widget states to the edit buffer.
*
* \since QGIS 3.2
*/
virtual void aboutToSave();

QgsAttributeEditorContext mContext;
QVariantMap mConfig;
QWidget *mWidget = nullptr;
@@ -46,6 +46,14 @@ void QgsRelationWidgetWrapper::setVisible( bool visible )
mWidget->setVisible( visible );
}

void QgsRelationWidgetWrapper::aboutToSave()
{
mRelation.referencingLayer()->isModified();

if ( mNmRelation.isValid() )
mNmRelation.referencedLayer()->isModified();
}

QgsRelation QgsRelationWidgetWrapper::relation() const
{
return mRelation;
@@ -96,14 +104,14 @@ void QgsRelationWidgetWrapper::initWidget( QWidget *editor )

w->setEditorContext( myContext );

QgsRelation nmrel = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "nm-rel" ) ).toString() );
mNmRelation = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "nm-rel" ) ).toString() );

// If this widget is already embedded by the same relation, reduce functionality
const QgsAttributeEditorContext *ctx = &context();
do
{
if ( ( ctx->relation().name() == mRelation.name() && ctx->formMode() == QgsAttributeEditorContext::Embed )
|| ( nmrel.isValid() && ctx->relation().name() == nmrel.name() ) )
|| ( mNmRelation.isValid() && ctx->relation().name() == mNmRelation.name() ) )
{
w->setVisible( false );
break;
@@ -113,7 +121,7 @@ void QgsRelationWidgetWrapper::initWidget( QWidget *editor )
while ( ctx );


w->setRelations( mRelation, nmrel );
w->setRelations( mRelation, mNmRelation );

mWidget = w;
}
@@ -103,7 +103,9 @@ class GUI_EXPORT QgsRelationWidgetWrapper : public QgsWidgetWrapper
void setVisible( bool visible );

private:
void aboutToSave() override;
QgsRelation mRelation;
QgsRelation mNmRelation;
QgsRelationEditorWidget *mWidget = nullptr;
};

@@ -290,7 +290,7 @@ bool QgsAttributeForm::saveEdits()
QgsAttributes src = mFeature.attributes();
QgsAttributes dst = mFeature.attributes();

Q_FOREACH ( QgsWidgetWrapper *ww, mWidgets )
for ( QgsWidgetWrapper *ww : qgis::as_const( mWidgets ) )
{
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
if ( eww )
@@ -588,6 +588,11 @@ bool QgsAttributeForm::save()
if ( mIsSaving )
return true;

for ( QgsWidgetWrapper *wrapper : qgis::as_const( mWidgets ) )
{
wrapper->notifyAboutToSave();
}

// only do the dirty checks when editing an existing feature - for new
// features we need to add them even if the attributes are unchanged from the initial
// default values

0 comments on commit 0749835

Please sign in to comment.
You can’t perform that action at this time.