Skip to content

Commit

Permalink
Take changes of embedded forms into account
Browse files Browse the repository at this point in the history
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 0749835
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/gui/editorwidgets/core/qgswidgetwrapper.cpp
Expand Up @@ -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 )
Expand All @@ -106,3 +111,8 @@ void QgsWidgetWrapper::setEnabled( bool enabled )
{
Q_UNUSED( enabled );
}

void QgsWidgetWrapper::aboutToSave()
{

}
18 changes: 18 additions & 0 deletions src/gui/editorwidgets/core/qgswidgetwrapper.h
Expand Up @@ -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:

/**
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 11 additions & 3 deletions src/gui/editorwidgets/qgsrelationwidgetwrapper.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -113,7 +121,7 @@ void QgsRelationWidgetWrapper::initWidget( QWidget *editor )
while ( ctx );


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

mWidget = w;
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsrelationwidgetwrapper.h
Expand Up @@ -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;
};

Expand Down
7 changes: 6 additions & 1 deletion src/gui/qgsattributeform.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0749835

Please sign in to comment.