QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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"
22 #include <QWidget>
23 
24 QgsRelationWidgetWrapper::QgsRelationWidgetWrapper( QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor, QWidget *parent )
25  : QgsWidgetWrapper( vl, editor, parent )
26  , mRelation( relation )
27 
28 {
29 }
30 
31 QWidget *QgsRelationWidgetWrapper::createWidget( QWidget *parent )
32 {
33  return new QgsRelationEditorWidget( parent );
34 }
35 
37 {
38  if ( mWidget && mRelation.isValid() )
39  mWidget->setFeature( feature );
40 }
41 
43 {
44  if ( mWidget )
45  mWidget->setVisible( visible );
46 }
47 
48 void QgsRelationWidgetWrapper::aboutToSave()
49 {
50  if ( !mRelation.isValid() || !widget() || !widget()->isVisible() || mRelation.referencingLayer() == mRelation.referencedLayer() )
51  return;
52 
53  // If the layer is already saved before, return
54  const QgsAttributeEditorContext *ctx = &context();
55  do
56  {
57  if ( ctx->relation().isValid() && ( ctx->relation().referencedLayer() == mRelation.referencingLayer()
58  || ( mNmRelation.isValid() && ctx->relation().referencedLayer() == mNmRelation.referencedLayer() ) )
59  )
60  {
61  return;
62  }
63  ctx = ctx->parentContext();
64  }
65  while ( ctx );
66 
67  // Calling isModified() will emit a beforeModifiedCheck()
68  // signal that will make the embedded form to send any
69  // outstanding widget changes to the edit buffer
70  mRelation.referencingLayer()->isModified();
71 
72  if ( mNmRelation.isValid() )
73  mNmRelation.referencedLayer()->isModified();
74 }
75 
77 {
78  return mRelation;
79 }
80 
82 {
83  return mWidget->showUnlinkButton();
84 }
85 
87 {
88  if ( mWidget )
89  mWidget->setShowUnlinkButton( showUnlinkButton );
90 }
91 
93 {
94  if ( mWidget )
95  return mWidget->showLabel();
96  else
97  return false;
98 }
99 
101 {
102  if ( mWidget )
103  mWidget->setShowLabel( showLabel );
104 }
105 
107 {
108  QgsRelationEditorWidget *w = qobject_cast<QgsRelationEditorWidget *>( editor );
109 
110  // if the editor cannot be cast to relation editor, insert a new one
111  if ( !w )
112  {
113  w = new QgsRelationEditorWidget( editor );
114  w->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
115  if ( ! editor->layout() )
116  {
117  editor->setLayout( new QGridLayout() );
118  }
119  editor->layout()->addWidget( w );
120  }
121 
123 
124  w->setEditorContext( myContext );
125 
126  mNmRelation = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "nm-rel" ) ).toString() );
127 
128  // If this widget is already embedded by the same relation, reduce functionality
129  const QgsAttributeEditorContext *ctx = &context();
130  do
131  {
132  if ( ( ctx->relation().name() == mRelation.name() && ctx->formMode() == QgsAttributeEditorContext::Embed )
133  || ( mNmRelation.isValid() && ctx->relation().name() == mNmRelation.name() ) )
134  {
135  w->setVisible( false );
136  break;
137  }
138  ctx = ctx->parentContext();
139  }
140  while ( ctx );
141 
142  w->setRelations( mRelation, mNmRelation );
143 
144  mWidget = w;
145 }
146 
148 {
149  return mWidget;
150 }
151 
153 {
154  return mWidget->showLinkButton();
155 }
156 
158 {
159  if ( mWidget )
160  mWidget->setShowLinkButton( showLinkButton );
161 }
QString name
Definition: qgsrelation.h:48
bool showUnlinkButton() const
Determines if the "unlink feature" button should be shown.
void setFeature(const QgsFeature &feature) override
void setShowUnlinkButton(bool showUnlinkButton)
Determines if the "unlink feature" button should be shown.
bool valid() const override
Returns true if the widget has been properly initialized.
This class contains context information for attribute editor widgets.
QgsRelationWidgetWrapper(QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsRelationWidgetWrapper.
void setVisible(bool visible)
Sets the visibility of the wrapper&#39;s widget.
QgsVectorLayer referencingLayer
Definition: qgsrelation.h:46
When showing a list of features (e.g. houses as an embedded form in a district form) ...
bool showLinkButton() const
Determines if the "link feature" button should be shown.
void setShowLabel(bool showLabel)
Defines if a title label should be shown for this widget.
QVariantMap config() const
Returns the whole config.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void setShowLinkButton(bool showLinkButton)
Determines if the "link feature" button should be shown.
void setShowUnlinkButton(bool showUnlinkButton)
Determines if the "unlink feature" button should be shown.
const QgsRelation & relation() const
Returns the attribute relation.
QgsVectorLayer referencedLayer
Definition: qgsrelation.h:47
bool showUnlinkButton() const
Determines if the "unlink feature" button should be shown.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsRelationManager relationManager
Definition: qgsproject.h:100
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
const QgsAttributeEditorContext * parentContext() const
FormMode formMode() const
Returns the form mode.
void setRelations(const QgsRelation &relation, const QgsRelation &nmrelation)
Set the relation(s) for this widget If only one relation is set, it will act as a simple 1:N relation...
bool isValid
Definition: qgsrelation.h:49
void setShowLabel(bool showLabel)
Defines if a title label should be shown for this widget.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:442
void setEditorContext(const QgsAttributeEditorContext &context)
virtual bool isModified() const
Returns true if the provider has been modified since the last commit.
bool showLinkButton() const
Determines if the "link feature" button should be shown.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QWidget * widget()
Access the widget managed by this wrapper.
QgsRelation relation() const
The relation for which this wrapper is created.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
Represents a vector layer which manages a vector based data sets.
Manages an editor widget Widget and wrapper share the same parent.
void setFeature(const QgsFeature &feature)
void setShowLinkButton(bool showLinkButton)
Determines if the "link feature" button should be shown.
bool showLabel() const
Defines if a title label should be shown for this widget.
A form was embedded as a widget on another form.