/
qgsrelationwidgetwrapper.cpp
296 lines (251 loc) · 8.82 KB
/
qgsrelationwidgetwrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/***************************************************************************
qgsrelationwidgetwrapper.cpp
--------------------------------------
Date : 14.5.2014
Copyright : (C) 2014 Matthias Kuhn
Email : matthias at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsrelationwidgetwrapper.h"
#include "qgsrelationeditorwidget.h"
#include "qgsattributeeditorcontext.h"
#include "qgsproject.h"
#include "qgsrelationmanager.h"
#include <QWidget>
QgsRelationWidgetWrapper::QgsRelationWidgetWrapper( QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor, QWidget *parent )
: QgsWidgetWrapper( vl, editor, parent )
, mRelation( relation )
{
}
QWidget *QgsRelationWidgetWrapper::createWidget( QWidget *parent )
{
QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
if ( form )
connect( form, &QgsAttributeForm::widgetValueChanged, this, &QgsRelationWidgetWrapper::widgetValueChanged );
return new QgsRelationEditorWidget( parent );
}
void QgsRelationWidgetWrapper::setFeature( const QgsFeature &feature )
{
if ( mWidget && mRelation.isValid() )
mWidget->setFeature( feature );
}
void QgsRelationWidgetWrapper::setVisible( bool visible )
{
if ( mWidget )
mWidget->setVisible( visible );
}
void QgsRelationWidgetWrapper::aboutToSave()
{
if ( !mRelation.isValid() || !widget() || !widget()->isVisible() || mRelation.referencingLayer() == mRelation.referencedLayer() )
return;
// If the layer is already saved before, return
const QgsAttributeEditorContext *ctx = &context();
do
{
if ( ctx->relation().isValid() && ( ctx->relation().referencedLayer() == mRelation.referencingLayer()
|| ( mNmRelation.isValid() && ctx->relation().referencedLayer() == mNmRelation.referencedLayer() ) )
)
{
return;
}
ctx = ctx->parentContext();
}
while ( ctx );
// 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;
}
void QgsRelationWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
{
if ( mWidget && attributeChanged )
{
QgsFeature feature { mWidget->feature() };
if ( feature.attribute( attribute ) != newValue )
{
feature.setAttribute( attribute, newValue );
QgsAttributeEditorContext newContext { mWidget->editorContext() };
newContext.setParentFormFeature( feature );
mWidget->setEditorContext( newContext );
mWidget->setFeature( feature, false );
mWidget->parentFormValueChanged( attribute, newValue );
}
}
}
bool QgsRelationWidgetWrapper::showUnlinkButton() const
{
Q_NOWARN_DEPRECATED_PUSH
return mWidget->showUnlinkButton();
Q_NOWARN_DEPRECATED_POP
}
void QgsRelationWidgetWrapper::setShowUnlinkButton( bool showUnlinkButton )
{
Q_NOWARN_DEPRECATED_PUSH
if ( mWidget )
mWidget->setShowUnlinkButton( showUnlinkButton );
Q_NOWARN_DEPRECATED_POP
}
void QgsRelationWidgetWrapper::setShowSaveChildEditsButton( bool showSaveChildEditsButton )
{
Q_NOWARN_DEPRECATED_PUSH
if ( mWidget )
mWidget->setShowSaveChildEditsButton( showSaveChildEditsButton );
Q_NOWARN_DEPRECATED_POP
}
bool QgsRelationWidgetWrapper::showLabel() const
{
if ( mWidget )
{
return mWidget->showLabel();
}
return false;
}
void QgsRelationWidgetWrapper::setShowLabel( bool showLabel )
{
if ( mWidget )
mWidget->setShowLabel( showLabel );
}
void QgsRelationWidgetWrapper::initWidget( QWidget *editor )
{
QgsRelationEditorWidget *w = qobject_cast<QgsRelationEditorWidget *>( editor );
// if the editor cannot be cast to relation editor, insert a new one
if ( !w )
{
w = new QgsRelationEditorWidget( editor );
w->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
if ( ! editor->layout() )
{
editor->setLayout( new QGridLayout() );
}
editor->layout()->addWidget( w );
}
QgsAttributeEditorContext myContext( QgsAttributeEditorContext( context(), mRelation, QgsAttributeEditorContext::Multiple, QgsAttributeEditorContext::Embed ) );
// read the legacy config of force-suppress-popup to support settings made on autoconfigurated forms
// it will be overwritten on specific widget configuration
if ( config( QStringLiteral( "force-suppress-popup" ), false ).toBool() )
{
const_cast<QgsVectorLayerTools *>( myContext.vectorLayerTools() )->setForceSuppressFormPopup( true );
}
/* TODO: this seems to have no effect
if ( config( QStringLiteral( "hide-save-child-edits" ), false ).toBool() )
{
w->setShowSaveChildEditsButton( false );
}
*/
// read the legacy config of nm-rel to support settings made on autoconfigurated forms
// it will be overwritten on specific widget configuration
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 )
|| ( mNmRelation.isValid() && ctx->relation().name() == mNmRelation.name() ) )
{
w->setVisible( false );
break;
}
ctx = ctx->parentContext();
}
while ( ctx );
w->setRelations( mRelation, mNmRelation );
w->setEditorContext( myContext );
mWidget = w;
}
bool QgsRelationWidgetWrapper::valid() const
{
return mWidget;
}
bool QgsRelationWidgetWrapper::showLinkButton() const
{
return visibleButtons().testFlag( QgsAttributeEditorRelation::Button::Link );
}
void QgsRelationWidgetWrapper::setShowLinkButton( bool showLinkButton )
{
Q_NOWARN_DEPRECATED_PUSH
if ( mWidget )
mWidget->setShowLinkButton( showLinkButton );
Q_NOWARN_DEPRECATED_POP
}
bool QgsRelationWidgetWrapper::showSaveChildEditsButton() const
{
return visibleButtons().testFlag( QgsAttributeEditorRelation::Button::SaveChildEdits );
}
void QgsRelationWidgetWrapper::setVisibleButtons( const QgsAttributeEditorRelation::Buttons &buttons )
{
if ( mWidget )
mWidget->setVisibleButtons( buttons );
}
QgsAttributeEditorRelation::Buttons QgsRelationWidgetWrapper::visibleButtons() const
{
return mWidget->visibleButtons();
}
void QgsRelationWidgetWrapper::setForceSuppressFormPopup( bool forceSuppressFormPopup )
{
if ( mWidget )
{
mWidget->setForceSuppressFormPopup( forceSuppressFormPopup );
//it's set to true if one widget is configured like this but the setting is done generally (influencing all widgets).
if ( forceSuppressFormPopup )
{
const_cast<QgsVectorLayerTools *>( mWidget->editorContext().vectorLayerTools() )->setForceSuppressFormPopup( true );
}
}
}
bool QgsRelationWidgetWrapper::forceSuppressFormPopup() const
{
if ( mWidget )
return mWidget->forceSuppressFormPopup();
return false;
}
void QgsRelationWidgetWrapper::setNmRelationId( const QVariant &nmRelationId )
{
if ( mWidget )
{
mWidget->setNmRelationId( nmRelationId );
mNmRelation = QgsProject::instance()->relationManager()->relation( nmRelationId.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 )
|| ( mNmRelation.isValid() && ctx->relation().name() == mNmRelation.name() ) )
{
mWidget->setVisible( false );
break;
}
ctx = ctx->parentContext();
}
while ( ctx );
mWidget->setRelations( mRelation, mNmRelation );
}
}
QVariant QgsRelationWidgetWrapper::nmRelationId() const
{
if ( mWidget )
return mWidget->nmRelationId();
return QVariant();
}
void QgsRelationWidgetWrapper::setLabel( const QString &label )
{
if ( mWidget )
mWidget->setLabel( label );
}
QString QgsRelationWidgetWrapper::label() const
{
if ( mWidget )
return mWidget->label();
return QString();
}