Skip to content
Permalink
Browse files
Merge pull request #7288 from m-kuhn/19137
Take changes of embedded forms into account
  • Loading branch information
m-kuhn committed 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.
@@ -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,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;
@@ -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;
@@ -113,7 +124,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 7ab6597

Please sign in to comment.