QGIS API Documentation  3.0.2-Girona (307d082)
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 
23 #include <QWidget>
24 
25 QgsRelationWidgetWrapper::QgsRelationWidgetWrapper( QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor, QWidget *parent )
26  : QgsWidgetWrapper( vl, editor, parent )
27  , mRelation( relation )
28 
29 {
30 }
31 
32 QWidget *QgsRelationWidgetWrapper::createWidget( QWidget *parent )
33 {
34  return new QgsRelationEditorWidget( parent );
35 }
36 
38 {
39  if ( mWidget && mRelation.isValid() )
40  mWidget->setFeature( feature );
41 }
42 
44 {
45  if ( mWidget )
46  mWidget->setVisible( visible );
47 }
48 
50 {
51  return mRelation;
52 }
53 
55 {
56  return mWidget->showUnlinkButton();
57 }
58 
60 {
61  if ( mWidget )
62  mWidget->setShowUnlinkButton( showUnlinkButton );
63 }
64 
66 {
67  if ( mWidget )
68  return mWidget->showLabel();
69  else
70  return false;
71 }
72 
74 {
75  if ( mWidget )
76  mWidget->setShowLabel( showLabel );
77 }
78 
79 void QgsRelationWidgetWrapper::initWidget( QWidget *editor )
80 {
81  QgsRelationEditorWidget *w = dynamic_cast<QgsRelationEditorWidget *>( editor );
82 
83  // if the editor cannot be cast to relation editor, insert a new one
84  if ( !w )
85  {
86  w = new QgsRelationEditorWidget( editor );
87  w->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
88  if ( ! editor->layout() )
89  {
90  editor->setLayout( new QGridLayout() );
91  }
92  editor->layout()->addWidget( w );
93  }
94 
96 
97  w->setEditorContext( myContext );
98 
99  QgsRelation nmrel = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "nm-rel" ) ).toString() );
100 
101  // If this widget is already embedded by the same relation, reduce functionality
102  const QgsAttributeEditorContext *ctx = &context();
103  do
104  {
105  if ( ( ctx->relation().name() == mRelation.name() && ctx->formMode() == QgsAttributeEditorContext::Embed )
106  || ( nmrel.isValid() && ctx->relation().name() == nmrel.name() ) )
107  {
108  w->setVisible( false );
109  break;
110  }
111  ctx = ctx->parentContext();
112  }
113  while ( ctx );
114 
115 
116  w->setRelations( mRelation, nmrel );
117 
118  mWidget = w;
119 }
120 
122 {
123  return mWidget;
124 }
125 
127 {
128  return mWidget->showLinkButton();
129 }
130 
132 {
133  if ( mWidget )
134  mWidget->setShowLinkButton( showLinkButton );
135 }
QString name
Definition: qgsrelation.h:45
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
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)
Constructor for QgsRelationWidgetWrapper.
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) ...
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.
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:62
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
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:92
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:46
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:383
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.
QgsRelation relation() const
The relation for which this wrapper is created.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Get 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 lable should be shown for this widget.
A form was embedded as a widget on another form.