QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
20#include "qgsgui.h"
21#include "qgsproject.h"
23#include "qgsrelationmanager.h"
25
26#include <QString>
27#include <QWidget>
28
29#include "moc_qgsrelationwidgetwrapper.cpp"
30
31using namespace Qt::StringLiterals;
32
34 : QgsRelationWidgetWrapper( u"relation_editor"_s, vl, relation, editor, parent )
35{}
36
37QgsRelationWidgetWrapper::QgsRelationWidgetWrapper( const QString &relationEditorName, QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor, QWidget *parent )
38 : QgsWidgetWrapper( vl, editor, parent )
39 , mRelation( relation )
40 , mRelationEditorId( relationEditorName )
41{}
42
43QWidget *QgsRelationWidgetWrapper::createWidget( QWidget *parent )
44{
45 QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
46 if ( form )
48
49 QgsAbstractRelationEditorWidget *relationEditorWidget = QgsGui::relationWidgetRegistry()->create( mRelationEditorId, widgetConfig(), parent );
50
51 if ( !relationEditorWidget )
52 {
53 QgsLogger::warning( u"Failed to create relation widget \"%1\", fallback to \"basic\" relation widget"_s.arg( mRelationEditorId ) );
54 relationEditorWidget = QgsGui::relationWidgetRegistry()->create( u"relation_editor"_s, widgetConfig(), parent );
55 }
56
58
59 return relationEditorWidget;
60}
61
63{
64 if ( mWidget && mRelation.isValid() )
65 mWidget->setFeature( feature );
66}
67
69{
70 if ( mWidget && mRelation.isValid() )
71 mWidget->setMultiEditFeatureIds( fids );
72}
73
75{
76 if ( mWidget )
77 mWidget->setVisible( visible );
78}
79
80void QgsRelationWidgetWrapper::aboutToSave()
81{
82 if ( !mRelation.isValid()
83 || !widget()
84 || !widget()->isVisible()
85 || mRelation.referencingLayer() == mRelation.referencedLayer()
86 || ( mNmRelation.isValid() && mNmRelation.referencedLayer() == mRelation.referencedLayer() ) )
87 return;
88
89 // If the layer is already saved before, return
90 const QgsAttributeEditorContext *ctx = &context();
91 do
92 {
93 if ( ctx->relation().isValid()
94 && ( ctx->relation().referencedLayer() == mRelation.referencingLayer() || ( mNmRelation.isValid() && ctx->relation().referencedLayer() == mNmRelation.referencedLayer() ) ) )
95 {
96 return;
97 }
98 ctx = ctx->parentContext();
99 } while ( ctx );
100
101 // Calling isModified() will emit a beforeModifiedCheck()
102 // signal that will make the embedded form to send any
103 // outstanding widget changes to the edit buffer
104 ( void ) mRelation.referencingLayer()->isModified();
105
106 if ( mNmRelation.isValid() )
107 {
108 ( void ) mNmRelation.referencedLayer()->isModified();
109 }
110}
111
113{
114 return mRelation;
115}
116
117void QgsRelationWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
118{
119 if ( mWidget && attributeChanged )
120 {
121 QgsFeature feature { mWidget->feature() };
122 if ( feature.attribute( attribute ) != newValue )
123 {
124 feature.setAttribute( attribute, newValue );
125 QgsAttributeEditorContext newContext { mWidget->editorContext() };
126 newContext.setParentFormFeature( feature );
127 mWidget->setEditorContext( newContext );
128 mWidget->setFeature( feature, false );
129 mWidget->parentFormValueChanged( attribute, newValue );
130 }
131 }
132}
133
140
147
154
156{
157 return false;
158}
159
161{
162 Q_UNUSED( showLabel )
163}
164
166{
167 QgsAbstractRelationEditorWidget *w = qobject_cast<QgsAbstractRelationEditorWidget *>( editor );
168
169 // if the editor cannot be cast to relation editor, insert a new one
170 if ( !w )
171 {
172 w = QgsGui::relationWidgetRegistry()->create( mRelationEditorId, widgetConfig(), editor );
173 if ( !editor->layout() )
174 {
175 editor->setLayout( new QVBoxLayout( editor ) );
176 }
177 editor->layout()->addWidget( w );
178 }
179
181
182 // read the legacy config of force-suppress-popup to support settings made on autoconfigurated forms
183 // it will be overwritten on specific widget configuration
184 if ( config( u"force-suppress-popup"_s, false ).toBool() )
185 {
186 const_cast<QgsVectorLayerTools *>( myContext.vectorLayerTools() )->setForceSuppressFormPopup( true );
187 }
188
189 /* TODO: this seems to have no effect
190 if ( config( u"hide-save-child-edits"_s, false ).toBool() )
191 {
192 w->setShowSaveChildEditsButton( false );
193 }
194 */
195
196 // read the legacy config of nm-rel to support settings made on autoconfigurated forms
197 // it will be overwritten on specific widget configuration
198 mNmRelation = QgsProject::instance()->relationManager()->relation( config( u"nm-rel"_s ).toString() );
199
200 // If this widget is already embedded by the same relation, reduce functionality
201 const QgsAttributeEditorContext *ctx = &context();
202 do
203 {
204 if ( ( ctx->relation().id() == mRelation.id() && ctx->formMode() == QgsAttributeEditorContext::Embed ) || ( mNmRelation.isValid() && ctx->relation().id() == mNmRelation.id() ) )
205 {
206 w->setVisible( false );
207 break;
208 }
209 ctx = ctx->parentContext();
210 } while ( ctx );
211
212 w->setEditorContext( myContext );
213 w->setRelations( mRelation, mNmRelation );
214
215 mWidget = w;
216}
217
219{
220 return mWidget;
221}
222
229
236
243
245{
246 if ( !mWidget )
247 return;
248 QVariantMap config = mWidget->config();
249 config.insert( "buttons", qgsFlagValueToKeys( buttons ) );
250
251 mWidget->setConfig( config );
252}
253
258
260{
261 if ( mWidget )
262 {
263 mWidget->setForceSuppressFormPopup( forceSuppressFormPopup );
264 //it's set to true if one widget is configured like this but the setting is done generally (influencing all widgets).
265 const_cast<QgsVectorLayerTools *>( mWidget->editorContext().vectorLayerTools() )->setForceSuppressFormPopup( forceSuppressFormPopup );
266 }
267}
268
270{
271 if ( mWidget )
272 return mWidget->forceSuppressFormPopup();
273
274 return false;
275}
276
278{
279 if ( mWidget )
280 {
281 mNmRelation = QgsProject::instance()->relationManager()->relation( nmRelationId.toString() );
282
283 // If this widget is already embedded by the same relation, reduce functionality
284 const QgsAttributeEditorContext *ctx = &context();
285 while ( ctx && ctx->relation().isValid() )
286 {
287 if ( ( ctx->relation().id() == mRelation.id() && ctx->formMode() == QgsAttributeEditorContext::Embed ) || ( mNmRelation.isValid() && ctx->relation().id() == mNmRelation.id() ) )
288 {
289 mWidget->setVisible( false );
290 break;
291 }
292 ctx = ctx->parentContext();
293 }
294
295 mWidget->setRelations( mRelation, mNmRelation );
296 }
297}
298
300{
301 if ( mWidget )
302 return mWidget->nmRelationId();
303 return QVariant();
304}
305
306
308{
309 Q_UNUSED( label )
310}
311
313{
314 return QString();
315}
316
318{
319 if ( mWidget )
320 mWidget->setConfig( config );
321}
322
324{
325 return mWidget ? mWidget->config() : QVariantMap();
326}
Base class to build new relation widgets.
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...
void setFeature(const QgsFeature &feature, bool update=true)
Sets the feature being edited and updates the UI unless update is set to false.
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.
The attribute form widget for vector layer features.
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:60
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
static QgsRelationWidgetRegistry * relationWidgetRegistry()
Returns the global relation widget registry, used for managing all known relation widget factories.
Definition qgsgui.cpp:114
static void warning(const QString &msg)
Goes to qWarning.
QgsRelationManager * relationManager
Definition qgsproject.h:124
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:42
QgsVectorLayer * referencedLayer
Definition qgsrelation.h:50
QString id
Definition qgsrelation.h:45
QgsVectorLayer * referencingLayer
Definition qgsrelation.h:47
Used to handle basic editing operations on vector layers.
void setForceSuppressFormPopup(bool forceSuppressFormPopup)
Sets force suppress form popup status to forceSuppressFormPopup.
Represents a vector layer which manages a vector based dataset.
QWidget * widget()
Access the widget managed by this wrapper.
QVariant config(const QString &key, const QVariant &defaultVal=QVariant()) const
Use this inside your overridden classes to access the configuration.
QgsWidgetWrapper(QgsVectorLayer *vl, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget 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:7504
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition qgis.h:7215
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:7237
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7503
QSet< QgsFeatureId > QgsFeatureIds