QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
22 #include <QWidget>
23 
25  : QgsWidgetWrapper( vl, editor, parent )
26  , mRelation( relation )
27  , mWidget( nullptr )
28 {
29 }
30 
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 
49 {
50  return mWidget->showUnlinkButton();
51 }
52 
54 {
55  if ( mWidget )
56  mWidget->setShowUnlinkButton( showUnlinkButton );
57 }
58 
60 {
61  if ( mWidget )
62  return mWidget->showLabel();
63  else
64  return false;
65 }
66 
68 {
69  if ( mWidget )
70  mWidget->setShowLabel( showLabel );
71 }
72 
74 {
75  QgsRelationEditorWidget* w = dynamic_cast<QgsRelationEditorWidget*>( editor );
76 
77  // if the editor cannot be cast to relation editor, insert a new one
78  if ( !w )
79  {
80  w = new QgsRelationEditorWidget( editor );
81  w->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
82  if ( ! editor->layout() )
83  {
84  editor->setLayout( new QGridLayout() );
85  }
86  editor->layout()->addWidget( w );
87  }
88 
90 
91  w->setEditorContext( myContext );
92 
93  QgsRelation nmrel = QgsProject::instance()->relationManager()->relation( config( "nm-rel" ).toString() );
94 
95  // If this widget is already embedded by the same relation, reduce functionality
96  const QgsAttributeEditorContext* ctx = &context();
97  do
98  {
99  if (( ctx->relation().name() == mRelation.name() && ctx->formMode() == QgsAttributeEditorContext::Embed )
100  || ( nmrel.isValid() && ctx->relation().name() == nmrel.name() ) )
101  {
102  w->setVisible( false );
103  break;
104  }
105  ctx = ctx->parentContext();
106  }
107  while ( ctx );
108 
109 
110  w->setRelations( mRelation, nmrel );
111 
112  mWidget = w;
113 }
114 
116 {
117  return mWidget;
118 }
119 
121 {
122  return mWidget->showLinkButton();
123 }
124 
126 {
127  if ( mWidget )
128  mWidget->setShowLinkButton( showLinkButton );
129 }
QLayout * layout() const
bool showUnlinkButton() const
Determines if the "unlink feature" button should be shown.
bool isValid() const
Returns the validity of this relation.
void setFeature(const QgsFeature &feature) override
void setShowUnlinkButton(bool showUnlinkButton)
Determines if the "unlink feature" button should be shown.
bool valid() const override
Return 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)
void setVisible(bool visible)
Sets the visibility of the wrapper&#39;s widget.
When showing a list of features (e.g. houses as an embedded form in a district form) ...
virtual void setVisible(bool visible)
bool showLinkButton() const
Determines if the "link feature" button should be shown.
void setShowLabel(bool showLabel)
Defines if a title lable should be shown for this widget.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
QgsRelationManager * relationManager() const
void setShowLinkButton(bool showLinkButton)
Determines if the "link feature" button should be shown.
void setLayout(QLayout *layout)
void setShowUnlinkButton(bool showUnlinkButton)
Determines if the "unlink feature" button should be shown.
const QgsRelation & relation() const
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.
void setSizePolicy(QSizePolicy)
void addWidget(QWidget *w)
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...
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:382
void setEditorContext(const QgsAttributeEditorContext &context)
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.
QgsEditorWidgetConfig config() const
Returns the whole config.
QgsRelation relation(const QString &id) const
Get access to a relation by its id.
QObject * parent() const
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.
QString name() const
Returns a human readable name for this relation.
bool showLabel() const
Defines if a title lable should be shown for this widget.
A form was embedded as a widget on another form.