QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
49 void QgsRelationWidgetWrapper::aboutToSave()
50 {
51  // Calling isModified() will emit a beforeModifiedCheck()
52  // signal that will make the embedded form to send any
53  // outstanding widget changes to the edit buffer
54  mRelation.referencingLayer()->isModified();
55 
56  if ( mNmRelation.isValid() )
57  mNmRelation.referencedLayer()->isModified();
58 }
59 
61 {
62  return mRelation;
63 }
64 
66 {
67  return mWidget->showUnlinkButton();
68 }
69 
71 {
72  if ( mWidget )
73  mWidget->setShowUnlinkButton( showUnlinkButton );
74 }
75 
77 {
78  if ( mWidget )
79  return mWidget->showLabel();
80  else
81  return false;
82 }
83 
85 {
86  if ( mWidget )
87  mWidget->setShowLabel( showLabel );
88 }
89 
90 void QgsRelationWidgetWrapper::initWidget( QWidget *editor )
91 {
92  QgsRelationEditorWidget *w = dynamic_cast<QgsRelationEditorWidget *>( editor );
93 
94  // if the editor cannot be cast to relation editor, insert a new one
95  if ( !w )
96  {
97  w = new QgsRelationEditorWidget( editor );
98  w->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
99  if ( ! editor->layout() )
100  {
101  editor->setLayout( new QGridLayout() );
102  }
103  editor->layout()->addWidget( w );
104  }
105 
107 
108  w->setEditorContext( myContext );
109 
110  mNmRelation = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "nm-rel" ) ).toString() );
111 
112  // If this widget is already embedded by the same relation, reduce functionality
113  const QgsAttributeEditorContext *ctx = &context();
114  do
115  {
116  if ( ( ctx->relation().name() == mRelation.name() && ctx->formMode() == QgsAttributeEditorContext::Embed )
117  || ( mNmRelation.isValid() && ctx->relation().name() == mNmRelation.name() ) )
118  {
119  w->setVisible( false );
120  break;
121  }
122  ctx = ctx->parentContext();
123  }
124  while ( ctx );
125 
126 
127  w->setRelations( mRelation, mNmRelation );
128 
129  mWidget = w;
130 }
131 
133 {
134  return mWidget;
135 }
136 
138 {
139  return mWidget->showLinkButton();
140 }
141 
143 {
144  if ( mWidget )
145  mWidget->setShowLinkButton( showLinkButton );
146 }
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
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:43
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
Returns the attribute relation.
QgsVectorLayer referencedLayer
Definition: qgsrelation.h:44
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:95
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:391
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.
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 lable should be shown for this widget.
A form was embedded as a widget on another form.