QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsrelationwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrelationwidgetwrapper.cpp
3 --------------------------------------
4 Date : 14.5.2014
5 Copyright : (C) 2014 Matthias Kuhn
6 Email : matthias at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsrelationwidgetwrapper.cpp"
18
21#include "qgsproject.h"
22#include "qgsrelationmanager.h"
25#include "qgsgui.h"
26#include <QWidget>
27
28QgsRelationWidgetWrapper::QgsRelationWidgetWrapper( QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor, QWidget *parent )
29 : QgsRelationWidgetWrapper( QStringLiteral( "relation_editor" ), vl, relation, editor, parent )
30{
31}
32
33QgsRelationWidgetWrapper::QgsRelationWidgetWrapper( const QString &relationEditorName, QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor, QWidget *parent )
34 : QgsWidgetWrapper( vl, editor, parent )
35 , mRelation( relation )
36 , mRelationEditorId( relationEditorName )
37{
38}
39
40QWidget *QgsRelationWidgetWrapper::createWidget( QWidget *parent )
41{
42 QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
43 if ( form )
45
46 QgsAbstractRelationEditorWidget *relationEditorWidget = QgsGui::relationWidgetRegistry()->create( mRelationEditorId, widgetConfig(), parent );
47
48 if ( !relationEditorWidget )
49 {
50 QgsLogger::warning( QStringLiteral( "Failed to create relation widget \"%1\", fallback to \"basic\" relation widget" ).arg( mRelationEditorId ) );
51 relationEditorWidget = QgsGui::relationWidgetRegistry()->create( QStringLiteral( "relation_editor" ), widgetConfig(), parent );
52 }
53
55
56 return relationEditorWidget;
57}
58
60{
61 if ( mWidget && mRelation.isValid() )
62 mWidget->setFeature( feature );
63}
64
66{
67 if ( mWidget && mRelation.isValid() )
68 mWidget->setMultiEditFeatureIds( fids );
69}
70
72{
73 if ( mWidget )
74 mWidget->setVisible( visible );
75}
76
77void QgsRelationWidgetWrapper::aboutToSave()
78{
79 if ( !mRelation.isValid() || !widget() || !widget()->isVisible() || mRelation.referencingLayer() == mRelation.referencedLayer() || ( mNmRelation.isValid() && mNmRelation.referencedLayer() == mRelation.referencedLayer() ) )
80 return;
81
82 // If the layer is already saved before, return
83 const QgsAttributeEditorContext *ctx = &context();
84 do
85 {
86 if ( ctx->relation().isValid() && ( ctx->relation().referencedLayer() == mRelation.referencingLayer()
87 || ( mNmRelation.isValid() && ctx->relation().referencedLayer() == mNmRelation.referencedLayer() ) )
88 )
89 {
90 return;
91 }
92 ctx = ctx->parentContext();
93 }
94 while ( ctx );
95
96 // Calling isModified() will emit a beforeModifiedCheck()
97 // signal that will make the embedded form to send any
98 // outstanding widget changes to the edit buffer
99 mRelation.referencingLayer()->isModified();
100
101 if ( mNmRelation.isValid() )
102 mNmRelation.referencedLayer()->isModified();
103}
104
106{
107 return mRelation;
108}
109
110void QgsRelationWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
111{
112 if ( mWidget && attributeChanged )
113 {
114 QgsFeature feature { mWidget->feature() };
115 if ( feature.attribute( attribute ) != newValue )
116 {
117 feature.setAttribute( attribute, newValue );
118 QgsAttributeEditorContext newContext { mWidget->editorContext() };
119 newContext.setParentFormFeature( feature );
120 mWidget->setEditorContext( newContext );
121 mWidget->setFeature( feature, false );
122 mWidget->parentFormValueChanged( attribute, newValue );
123 }
124 }
125}
126
133
140
147
149{
150 return false;
151}
152
154{
155 Q_UNUSED( showLabel )
156}
157
159{
160 QgsAbstractRelationEditorWidget *w = qobject_cast<QgsAbstractRelationEditorWidget *>( editor );
161
162 // if the editor cannot be cast to relation editor, insert a new one
163 if ( !w )
164 {
165 w = QgsGui::relationWidgetRegistry()->create( mRelationEditorId, widgetConfig(), editor );
166 if ( ! editor->layout() )
167 {
168 editor->setLayout( new QVBoxLayout( editor ) );
169 }
170 editor->layout()->addWidget( w );
171 }
172
174
175 // read the legacy config of force-suppress-popup to support settings made on autoconfigurated forms
176 // it will be overwritten on specific widget configuration
177 if ( config( QStringLiteral( "force-suppress-popup" ), false ).toBool() )
178 {
179 const_cast<QgsVectorLayerTools *>( myContext.vectorLayerTools() )->setForceSuppressFormPopup( true );
180 }
181
182 /* TODO: this seems to have no effect
183 if ( config( QStringLiteral( "hide-save-child-edits" ), false ).toBool() )
184 {
185 w->setShowSaveChildEditsButton( false );
186 }
187 */
188
189 // read the legacy config of nm-rel to support settings made on autoconfigurated forms
190 // it will be overwritten on specific widget configuration
191 mNmRelation = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "nm-rel" ) ).toString() );
192
193 // If this widget is already embedded by the same relation, reduce functionality
194 const QgsAttributeEditorContext *ctx = &context();
195 do
196 {
197 if ( ( ctx->relation().id() == mRelation.id() && ctx->formMode() == QgsAttributeEditorContext::Embed )
198 || ( mNmRelation.isValid() && ctx->relation().id() == mNmRelation.id() ) )
199 {
200 w->setVisible( false );
201 break;
202 }
203 ctx = ctx->parentContext();
204 }
205 while ( ctx );
206
207 w->setEditorContext( myContext );
208 w->setRelations( mRelation, mNmRelation );
209
210 mWidget = w;
211}
212
214{
215 return mWidget;
216}
217
224
231
238
240{
241 if ( ! mWidget )
242 return;
243 QVariantMap config = mWidget->config();
244 config.insert( "buttons", qgsFlagValueToKeys( buttons ) );
245
246 mWidget->setConfig( config );
247}
248
250{
251 return qgsFlagKeysToValue( mWidget->config().value( QStringLiteral( "buttons" ) ).toString(), QgsAttributeEditorRelation::AllButtons );
252}
253
255{
256 if ( mWidget )
257 {
259 //it's set to true if one widget is configured like this but the setting is done generally (influencing all widgets).
261 }
262}
263
265{
266 if ( mWidget )
267 return mWidget->forceSuppressFormPopup();
268
269 return false;
270}
271
272void QgsRelationWidgetWrapper::setNmRelationId( const QVariant &nmRelationId )
273{
274 if ( mWidget )
275 {
276 mNmRelation = QgsProject::instance()->relationManager()->relation( nmRelationId.toString() );
277
278 // If this widget is already embedded by the same relation, reduce functionality
279 const QgsAttributeEditorContext *ctx = &context();
280 while ( ctx && ctx->relation().isValid() )
281 {
282 if ( ( ctx->relation().id() == mRelation.id() && ctx->formMode() == QgsAttributeEditorContext::Embed )
283 || ( mNmRelation.isValid() && ctx->relation().id() == mNmRelation.id() ) )
284 {
285 mWidget->setVisible( false );
286 break;
287 }
288 ctx = ctx->parentContext();
289 }
290
291 mWidget->setRelations( mRelation, mNmRelation );
292 }
293}
294
296{
297 if ( mWidget )
298 return mWidget->nmRelationId();
299 return QVariant();
300}
301
302
303void QgsRelationWidgetWrapper::setLabel( const QString &label )
304{
305 Q_UNUSED( label )
306}
307
309{
310 return QString();
311}
312
313void QgsRelationWidgetWrapper::setWidgetConfig( const QVariantMap &config )
314{
315 if ( mWidget )
316 mWidget->setConfig( config );
317}
318
320{
321 return mWidget ? mWidget->config() : QVariantMap();
322}
Base class to build new relation widgets.
void setMultiEditFeatureIds(const QgsFeatureIds &fids)
Set multiple feature to edit simultaneously.
virtual void setConfig(const QVariantMap &config)=0
Defines the widget configuration.
bool forceSuppressFormPopup() const
Determines the force suppress form popup status that is configured for this widget.
virtual void setEditorContext(const QgsAttributeEditorContext &context)
Sets the editor context.
void relatedFeaturesChanged()
Emit this signal, whenever the related features changed.
void setRelations(const QgsRelation &relation, const QgsRelation &nmrelation)
Sets the relation(s) for this widget If only one relation is set, it will act as a simple 1:N relatio...
QgsAttributeEditorContext editorContext() const
Returns the attribute editor context.
virtual QVariantMap config() const =0
Returns the widget configuration.
virtual void parentFormValueChanged(const QString &attribute, const QVariant &newValue)=0
Called when an attribute value in the parent widget has changed to newValue.
void setFeature(const QgsFeature &feature, bool update=true)
Sets the feature being edited and updates the UI unless update is set to false.
QgsFeature feature() const
Returns the widget's current feature If the widget is in multiedit mode only the first is returned.
QVariant nmRelationId() const
Determines the relation id of the second relation involved in an N:M relation.
void setForceSuppressFormPopup(bool forceSuppressFormPopup)
Sets force suppress form popup status with forceSuppressFormPopup configured for this widget.
This class contains context information for attribute editor widgets.
FormMode formMode() const
Returns the form mode.
@ Multiple
When showing a list of features (e.g. houses as an embedded form in a district form)
const QgsVectorLayerTools * vectorLayerTools() const
Returns the associated vector layer tools.
@ Embed
A form was embedded as a widget on another form.
void setParentFormFeature(const QgsFeature &feature)
Sets the feature of the currently edited parent form.
const QgsAttributeEditorContext * parentContext() const
const QgsRelation & relation() const
Returns the attribute relation.
@ SaveChildEdits
Save child edits button.
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
static QgsRelationWidgetRegistry * relationWidgetRegistry()
Returns the global relation widget registry, used for managing all known relation widget factories.
Definition qgsgui.cpp:99
static void warning(const QString &msg)
Goes to qWarning.
QgsRelationManager * relationManager
Definition qgsproject.h:117
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
QgsAbstractRelationEditorWidget * create(const QString &widgetType, const QVariantMap &config, QWidget *parent=nullptr) const
Create a relation widget of a given type for a given field.
void relatedFeaturesChanged()
Emit this signal, whenever the related features changed.
Q_DECL_DEPRECATED bool showLabel() const
Defines if a title label should be shown for this widget.
void setVisible(bool visible)
Sets the visibility of the wrapper's widget.
Q_DECL_DEPRECATED void setVisibleButtons(const QgsAttributeEditorRelation::Buttons &buttons)
Defines the buttons which are shown.
QgsRelation relation() const
The relation for which this wrapper is created.
QVariantMap widgetConfig() const
Returns the whole widget config.
Q_DECL_DEPRECATED void setShowLabel(bool showLabel)
Defines if a title label should be shown for this widget.
Q_DECL_DEPRECATED void setLabel(const QString &label=QString())
Sets label for this element If it's empty it takes the relation id as label.
void setWidgetConfig(const QVariantMap &config)
Will set the config of this widget wrapper to the specified config.
void widgetValueChanged(const QString &attribute, const QVariant &newValue, bool attributeChanged)
Will be called when a value in the current edited form or table row changes.
void setForceSuppressFormPopup(bool forceSuppressFormPopup)
Sets force suppress form popup status to forceSuppressFormPopup for this widget and for the vectorLay...
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Q_DECL_DEPRECATED bool showLinkButton() const
Determines if the "link feature" button should be shown.
Q_DECL_DEPRECATED bool showUnlinkButton() const
Determines if the "unlink feature" button should be shown.
bool forceSuppressFormPopup() const
Determines the force suppress form popup status that is configured for this widget.
bool valid() const override
Returns true if the widget has been properly initialized.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setMultiEditFeatureIds(const QgsFeatureIds &fids)
Set multiple feature to edit simultaneously.
Q_DECL_DEPRECATED void setShowUnlinkButton(bool showUnlinkButton)
Determines if the "unlink feature" button should be shown.
Q_DECL_DEPRECATED QgsAttributeEditorRelation::Buttons visibleButtons() const
Returns the buttons which are shown.
QgsRelationWidgetWrapper(QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsRelationWidgetWrapper.
Q_DECL_DEPRECATED QString label() const
Determines the label of this element.
void setNmRelationId(const QVariant &nmRelationId=QVariant())
Sets nmRelationId for the relation id of the second relation involved in an N:M relation.
QVariant nmRelationId() const
Determines the relation id of the second relation involved in an N:M relation.
Q_DECL_DEPRECATED bool showSaveChildEditsButton() const
Determines if the "Save child layer edits" button should be shown.
Q_DECL_DEPRECATED void setShowSaveChildEditsButton(bool showChildEdits)
Determines if the "Save child layer edits" button should be shown.
Q_DECL_DEPRECATED void setShowLinkButton(bool showLinkButton)
Determines if the "link feature" button should be shown.
void setFeature(const QgsFeature &feature) override
Represents a relationship between two vector layers.
Definition qgsrelation.h:44
QgsVectorLayer * referencedLayer
Definition qgsrelation.h:49
QString id
Definition qgsrelation.h:47
QgsVectorLayer * referencingLayer
Definition qgsrelation.h:48
Methods in this class are used to handle basic operations on vector layers.
Represents a vector layer which manages a vector based data sets.
bool isModified() const override
Returns true if the provider has been modified since the last commit.
Manages an editor widget Widget and wrapper share the same parent.
QWidget * widget()
Access the widget managed by this wrapper.
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
QVariantMap config() const
Returns the whole config.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6535
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition qgis.h:6207
T qgsFlagKeysToValue(const QString &keys, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given keys of a flag.
Definition qgis.h:6229
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534
QSet< QgsFeatureId > QgsFeatureIds