Skip to content

Commit 7ab6597

Browse files
authored
Merge pull request #7288 from m-kuhn/19137
Take changes of embedded forms into account
2 parents 648b588 + c6b8a10 commit 7ab6597

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

python/gui/auto_generated/editorwidgets/core/qgswidgetwrapper.sip.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ Sets the editor widget's property collection, used for data defined overrides.
153153
.. seealso:: :py:func:`dataDefinedProperties`
154154

155155
.. versionadded:: 3.0
156+
%End
157+
158+
void notifyAboutToSave();
159+
%Docstring
160+
Notify this widget, that the containing form is about to save and
161+
that any pending changes should be pushed to the edit buffer or they
162+
might be lost.
163+
164+
.. versionadded:: 3.2
156165
%End
157166

158167
protected:

src/gui/editorwidgets/core/qgswidgetwrapper.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ QgsWidgetWrapper *QgsWidgetWrapper::fromWidget( QWidget *widget )
9797
return widget->property( "EWV2Wrapper" ).value<QgsWidgetWrapper *>();
9898
}
9999

100+
void QgsWidgetWrapper::notifyAboutToSave()
101+
{
102+
aboutToSave();
103+
}
104+
100105
void QgsWidgetWrapper::initWidget( QWidget *editor )
101106
{
102107
Q_UNUSED( editor )
@@ -106,3 +111,8 @@ void QgsWidgetWrapper::setEnabled( bool enabled )
106111
{
107112
Q_UNUSED( enabled );
108113
}
114+
115+
void QgsWidgetWrapper::aboutToSave()
116+
{
117+
118+
}

src/gui/editorwidgets/core/qgswidgetwrapper.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ class GUI_EXPORT QgsWidgetWrapper : public QObject
190190
*/
191191
void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mPropertyCollection = collection; }
192192

193+
/**
194+
* Notify this widget, that the containing form is about to save and
195+
* that any pending changes should be pushed to the edit buffer or they
196+
* might be lost.
197+
*
198+
* \since QGIS 3.2
199+
*/
200+
void notifyAboutToSave();
201+
193202
protected:
194203

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

236245
private:
246+
247+
/**
248+
* Called when the containing attribute form is about to save.
249+
* Use this to push any widget states to the edit buffer.
250+
*
251+
* \since QGIS 3.2
252+
*/
253+
virtual void aboutToSave();
254+
237255
QgsAttributeEditorContext mContext;
238256
QVariantMap mConfig;
239257
QWidget *mWidget = nullptr;

src/gui/editorwidgets/qgsrelationwidgetwrapper.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ void QgsRelationWidgetWrapper::setVisible( bool visible )
4646
mWidget->setVisible( visible );
4747
}
4848

49+
void QgsRelationWidgetWrapper::aboutToSave()
50+
{
51+
// Calling isModified() will emit a beforeModifiedCheck()
52+
// signal that will make the embedded form to send any
53+
// outstanding widget changes to the edit buffer
54+
mRelation.referencingLayer()->isModified();
55+
56+
if ( mNmRelation.isValid() )
57+
mNmRelation.referencedLayer()->isModified();
58+
}
59+
4960
QgsRelation QgsRelationWidgetWrapper::relation() const
5061
{
5162
return mRelation;
@@ -96,14 +107,14 @@ void QgsRelationWidgetWrapper::initWidget( QWidget *editor )
96107

97108
w->setEditorContext( myContext );
98109

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

101112
// If this widget is already embedded by the same relation, reduce functionality
102113
const QgsAttributeEditorContext *ctx = &context();
103114
do
104115
{
105116
if ( ( ctx->relation().name() == mRelation.name() && ctx->formMode() == QgsAttributeEditorContext::Embed )
106-
|| ( nmrel.isValid() && ctx->relation().name() == nmrel.name() ) )
117+
|| ( mNmRelation.isValid() && ctx->relation().name() == mNmRelation.name() ) )
107118
{
108119
w->setVisible( false );
109120
break;
@@ -113,7 +124,7 @@ void QgsRelationWidgetWrapper::initWidget( QWidget *editor )
113124
while ( ctx );
114125

115126

116-
w->setRelations( mRelation, nmrel );
127+
w->setRelations( mRelation, mNmRelation );
117128

118129
mWidget = w;
119130
}

src/gui/editorwidgets/qgsrelationwidgetwrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ class GUI_EXPORT QgsRelationWidgetWrapper : public QgsWidgetWrapper
103103
void setVisible( bool visible );
104104

105105
private:
106+
void aboutToSave() override;
106107
QgsRelation mRelation;
108+
QgsRelation mNmRelation;
107109
QgsRelationEditorWidget *mWidget = nullptr;
108110
};
109111

src/gui/qgsattributeform.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ bool QgsAttributeForm::saveEdits()
290290
QgsAttributes src = mFeature.attributes();
291291
QgsAttributes dst = mFeature.attributes();
292292

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

591+
for ( QgsWidgetWrapper *wrapper : qgis::as_const( mWidgets ) )
592+
{
593+
wrapper->notifyAboutToSave();
594+
}
595+
591596
// only do the dirty checks when editing an existing feature - for new
592597
// features we need to add them even if the attributes are unchanged from the initial
593598
// default values

0 commit comments

Comments
 (0)