Skip to content

Commit

Permalink
Merge pull request #7288 from m-kuhn/19137
Browse files Browse the repository at this point in the history
Take changes of embedded forms into account
  • Loading branch information
m-kuhn authored Jun 22, 2018
2 parents 648b588 + c6b8a10 commit 7ab6597
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
17 changes: 14 additions & 3 deletions src/gui/editorwidgets/qgsrelationwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ void QgsRelationWidgetWrapper::setVisible( bool visible )
mWidget->setVisible( visible );
}

void QgsRelationWidgetWrapper::aboutToSave()
{
// Calling isModified() will emit a beforeModifiedCheck()
// signal that will make the embedded form to send any
// outstanding widget changes to the edit buffer
mRelation.referencingLayer()->isModified();

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

QgsRelation QgsRelationWidgetWrapper::relation() const
{
return mRelation;
Expand Down Expand Up @@ -96,14 +107,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 +124,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 7ab6597

Please sign in to comment.