QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
20 #include "qgsproject.h"
21 #include "qgsrelationmanager.h"
24 #include "qgsgui.h"
25 #include <QWidget>
26 
27 QgsRelationWidgetWrapper::QgsRelationWidgetWrapper( QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor, QWidget *parent )
28  : QgsRelationWidgetWrapper( QStringLiteral( "relation_editor" ), vl, relation, editor, parent )
29 {
30 }
31 
32 QgsRelationWidgetWrapper::QgsRelationWidgetWrapper( const QString &relationEditorName, QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor, QWidget *parent )
33  : QgsWidgetWrapper( vl, editor, parent )
34  , mRelation( relation )
35  , mRelationEditorId( relationEditorName )
36 {
37 }
38 
39 QWidget *QgsRelationWidgetWrapper::createWidget( QWidget *parent )
40 {
41  QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
42  if ( form )
44 
45  QgsAbstractRelationEditorWidget *relationEditorWidget = QgsGui::instance()->relationWidgetRegistry()->create( mRelationEditorId, widgetConfig(), parent );
46 
47  if ( !relationEditorWidget )
48  {
49  QgsLogger::warning( QStringLiteral( "Failed to create relation widget \"%1\", fallback to \"basic\" relation widget" ).arg( mRelationEditorId ) );
50  relationEditorWidget = QgsGui::instance()->relationWidgetRegistry()->create( QStringLiteral( "relation_editor" ), widgetConfig(), parent );
51  }
52 
54 
55  return relationEditorWidget;
56 }
57 
59 {
60  if ( mWidget && mRelation.isValid() )
61  mWidget->setFeature( feature );
62 }
63 
65 {
66  if ( mWidget )
67  mWidget->setVisible( visible );
68 }
69 
70 void QgsRelationWidgetWrapper::aboutToSave()
71 {
72  if ( !mRelation.isValid() || !widget() || !widget()->isVisible() || mRelation.referencingLayer() == mRelation.referencedLayer() )
73  return;
74 
75  // If the layer is already saved before, return
76  const QgsAttributeEditorContext *ctx = &context();
77  do
78  {
79  if ( ctx->relation().isValid() && ( ctx->relation().referencedLayer() == mRelation.referencingLayer()
80  || ( mNmRelation.isValid() && ctx->relation().referencedLayer() == mNmRelation.referencedLayer() ) )
81  )
82  {
83  return;
84  }
85  ctx = ctx->parentContext();
86  }
87  while ( ctx );
88 
89  // Calling isModified() will emit a beforeModifiedCheck()
90  // signal that will make the embedded form to send any
91  // outstanding widget changes to the edit buffer
92  mRelation.referencingLayer()->isModified();
93 
94  if ( mNmRelation.isValid() )
95  mNmRelation.referencedLayer()->isModified();
96 }
97 
99 {
100  return mRelation;
101 }
102 
103 void QgsRelationWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
104 {
105  if ( mWidget && attributeChanged )
106  {
107  QgsFeature feature { mWidget->feature() };
108  if ( feature.attribute( attribute ) != newValue )
109  {
110  feature.setAttribute( attribute, newValue );
111  QgsAttributeEditorContext newContext { mWidget->editorContext() };
112  newContext.setParentFormFeature( feature );
113  mWidget->setEditorContext( newContext );
114  mWidget->setFeature( feature, false );
115  mWidget->parentFormValueChanged( attribute, newValue );
116  }
117  }
118 }
119 
121 {
123  return visibleButtons().testFlag( QgsAttributeEditorRelation::Button::Unlink );
125 }
126 
128 {
132 }
133 
134 void QgsRelationWidgetWrapper::setShowSaveChildEditsButton( bool showSaveChildEditsButton )
135 {
139 }
140 
142 {
143  return false;
144 }
145 
147 {
148  Q_UNUSED( showLabel )
149 }
150 
152 {
153  QgsAbstractRelationEditorWidget *w = qobject_cast<QgsAbstractRelationEditorWidget *>( editor );
154 
155  // if the editor cannot be cast to relation editor, insert a new one
156  if ( !w )
157  {
158  w = QgsGui::instance()->relationWidgetRegistry()->create( mRelationEditorId, widgetConfig(), editor );
159  editor->layout()->addWidget( w );
160  }
161 
163 
164  // read the legacy config of force-suppress-popup to support settings made on autoconfigurated forms
165  // it will be overwritten on specific widget configuration
166  if ( config( QStringLiteral( "force-suppress-popup" ), false ).toBool() )
167  {
168  const_cast<QgsVectorLayerTools *>( myContext.vectorLayerTools() )->setForceSuppressFormPopup( true );
169  }
170 
171  /* TODO: this seems to have no effect
172  if ( config( QStringLiteral( "hide-save-child-edits" ), false ).toBool() )
173  {
174  w->setShowSaveChildEditsButton( false );
175  }
176  */
177 
178  // read the legacy config of nm-rel to support settings made on autoconfigurated forms
179  // it will be overwritten on specific widget configuration
180  mNmRelation = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "nm-rel" ) ).toString() );
181 
182  // If this widget is already embedded by the same relation, reduce functionality
183  const QgsAttributeEditorContext *ctx = &context();
184  do
185  {
186  if ( ( ctx->relation().id() == mRelation.id() && ctx->formMode() == QgsAttributeEditorContext::Embed )
187  || ( mNmRelation.isValid() && ctx->relation().id() == mNmRelation.id() ) )
188  {
189  w->setVisible( false );
190  break;
191  }
192  ctx = ctx->parentContext();
193  }
194  while ( ctx );
195 
196  w->setEditorContext( myContext );
197  w->setRelations( mRelation, mNmRelation );
198 
199  mWidget = w;
200 }
201 
203 {
204  return mWidget;
205 }
206 
208 {
210  return visibleButtons().testFlag( QgsAttributeEditorRelation::Button::Link );
212 }
213 
215 {
219 }
220 
222 {
224  return visibleButtons().testFlag( QgsAttributeEditorRelation::Button::SaveChildEdits );
226 }
227 
228 void QgsRelationWidgetWrapper::setVisibleButtons( const QgsAttributeEditorRelation::Buttons &buttons )
229 {
230  if ( ! mWidget )
231  return;
232  QVariantMap config = mWidget->config();
233  config.insert( "buttons", qgsFlagValueToKeys( buttons ) );
234 
235  mWidget->setConfig( config );
236 }
237 
238 QgsAttributeEditorRelation::Buttons QgsRelationWidgetWrapper::visibleButtons() const
239 {
240  return qgsFlagKeysToValue( mWidget->config().value( QStringLiteral( "buttons" ) ).toString(), QgsAttributeEditorRelation::AllButtons );
241 }
242 
243 void QgsRelationWidgetWrapper::setForceSuppressFormPopup( bool forceSuppressFormPopup )
244 {
245  if ( mWidget )
246  {
248  //it's set to true if one widget is configured like this but the setting is done generally (influencing all widgets).
250  {
251  const_cast<QgsVectorLayerTools *>( mWidget->editorContext().vectorLayerTools() )->setForceSuppressFormPopup( true );
252  }
253  }
254 }
255 
257 {
258  if ( mWidget )
259  return mWidget->forceSuppressFormPopup();
260 
261  return false;
262 }
263 
264 void QgsRelationWidgetWrapper::setNmRelationId( const QVariant &nmRelationId )
265 {
266  if ( mWidget )
267  {
268  mNmRelation = QgsProject::instance()->relationManager()->relation( nmRelationId.toString() );
269 
270  // If this widget is already embedded by the same relation, reduce functionality
271  const QgsAttributeEditorContext *ctx = &context();
272  while ( ctx && ctx->relation().isValid() )
273  {
274  if ( ( ctx->relation().id() == mRelation.id() && ctx->formMode() == QgsAttributeEditorContext::Embed )
275  || ( mNmRelation.isValid() && ctx->relation().id() == mNmRelation.id() ) )
276  {
277  mWidget->setVisible( false );
278  break;
279  }
280  ctx = ctx->parentContext();
281  }
282 
283  mWidget->setRelations( mRelation, mNmRelation );
284  }
285 }
286 
288 {
289  if ( mWidget )
290  return mWidget->nmRelationId();
291  return QVariant();
292 }
293 
294 
295 void QgsRelationWidgetWrapper::setLabel( const QString &label )
296 {
297  Q_UNUSED( label )
298 }
299 
301 {
302  return QString();
303 }
304 
305 void QgsRelationWidgetWrapper::setWidgetConfig( const QVariantMap &config )
306 {
307  if ( mWidget )
308  mWidget->setConfig( config );
309 }
310 
312 {
313  return mWidget ? mWidget->config() : QVariantMap();
314 }
Base class to build new relation widgets.
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.
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.
const QgsAttributeEditorContext * parentContext() const
FormMode formMode() const
Returns the form mode.
const QgsRelation & relation() const
Returns the attribute relation.
@ Multiple
When showing a list of features (e.g. houses as an embedded form in a district form)
@ 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 QgsVectorLayerTools * vectorLayerTools() const
Returns the associated vector layer tools.
@ 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:56
bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
Definition: qgsfeature.cpp:255
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:67
static QgsRelationWidgetRegistry * relationWidgetRegistry()
Returns the global relation widget registry, used for managing all known relation widget factories.
Definition: qgsgui.cpp:88
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:122
QgsRelationManager * relationManager
Definition: qgsproject.h:111
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:467
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.
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
QgsVectorLayer * referencedLayer
Definition: qgsrelation.h:48
Q_GADGET QString id
Definition: qgsrelation.h:46
QgsVectorLayer * referencingLayer
Definition: qgsrelation.h:47
bool isValid
Definition: qgsrelation.h:50
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:1742
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:1741
T qgsFlagKeysToValue(const QString &keys, const T &defaultValue)
Returns the value corresponding to the given keys of a flag.
Definition: qgis.h:1460
QString qgsFlagValueToKeys(const T &value)
Returns the value for the given keys of a flag.
Definition: qgis.h:1448