Skip to content

Commit 0749835

Browse files
committed
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
1 parent 353eb65 commit 0749835

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ void QgsRelationWidgetWrapper::setVisible( bool visible )
4646
mWidget->setVisible( visible );
4747
}
4848

49+
void QgsRelationWidgetWrapper::aboutToSave()
50+
{
51+
mRelation.referencingLayer()->isModified();
52+
53+
if ( mNmRelation.isValid() )
54+
mNmRelation.referencedLayer()->isModified();
55+
}
56+
4957
QgsRelation QgsRelationWidgetWrapper::relation() const
5058
{
5159
return mRelation;
@@ -96,14 +104,14 @@ void QgsRelationWidgetWrapper::initWidget( QWidget *editor )
96104

97105
w->setEditorContext( myContext );
98106

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

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

115123

116-
w->setRelations( mRelation, nmrel );
124+
w->setRelations( mRelation, mNmRelation );
117125

118126
mWidget = w;
119127
}

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)