QGIS API Documentation  2.14.0-Essen
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  QgsRelationEditorWidget* w = dynamic_cast<QgsRelationEditorWidget*>( editor );
45 
46  // if the editor cannot be cast to relation editor, insert a new one
47  if ( !w )
48  {
49  w = new QgsRelationEditorWidget( editor );
50  w->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
51  if ( ! editor->layout() )
52  {
53  editor->setLayout( new QGridLayout() );
54  }
55  editor->layout()->addWidget( w );
56  }
57 
59 
60  w->setEditorContext( myContext );
61 
62  // If this widget is already embedded by the same relation, reduce functionality
63  const QgsAttributeEditorContext* ctx = &context();
64  do
65  {
66  if ( ctx->relation().name() == mRelation.name() && ctx->formMode() == QgsAttributeEditorContext::Embed )
67  {
68  w->setVisible( false );
69  break;
70  }
71  ctx = ctx->parentContext();
72  }
73  while ( ctx );
74 
75  QgsRelation nmrel = QgsProject::instance()->relationManager()->relation( config( "nm-rel" ).toString() );
76 
77  w->setRelations( mRelation, nmrel );
78 
79  mWidget = w;
80 }
81 
83 {
84  return mWidget;
85 }
QLayout * layout() const
bool isValid() const
Returns the validity of this relation.
void setFeature(const QgsFeature &feature) override
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)
When showing a list of features (e.g. houses as an embedded form in a district form) ...
virtual void setVisible(bool visible)
QString name() const
Returns a human readable name for this relation.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
void setLayout(QLayout *layout)
QgsRelation relation(const QString &id) const
Get access to a relation by its id.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void setSizePolicy(QSizePolicy)
void addWidget(QWidget *w)
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...
const QgsRelation & relation() const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:381
void setEditorContext(const QgsAttributeEditorContext &context)
QgsEditorWidgetConfig config() const
Returns the whole config.
const QgsAttributeEditorContext * parentContext() const
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QgsRelationManager * relationManager() const
Manages an editor widget Widget and wrapper share the same parent.
void setFeature(const QgsFeature &feature)
A form was embedded as a widget on another form.