QGIS API Documentation  2.12.0-Lyon
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 
21 #include <QWidget>
22 
24  : QgsWidgetWrapper( vl, editor, parent )
25  , mRelation( relation )
26  , mWidget( NULL )
27 {
28 }
29 
31 {
32  return new QgsRelationEditorWidget( parent );
33 }
34 
36 {
37  if ( mWidget && mRelation.isValid() )
38  mWidget->setRelationFeature( mRelation, feature );
39 }
40 
42 {
43  QgsRelationEditorWidget* w = dynamic_cast<QgsRelationEditorWidget*>( editor );
44 
45  // if the editor cannot be cast to relation editor, insert a new one
46  if ( !w )
47  {
48  w = new QgsRelationEditorWidget( editor );
49  w->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
50  if ( ! editor->layout() )
51  {
52  editor->setLayout( new QGridLayout() );
53  }
54  editor->layout()->addWidget( w );
55  }
56 
58 
59  w->setEditorContext( myContext );
60 
61  // If this widget is already embedded by the same relation, reduce functionality
62  const QgsAttributeEditorContext* ctx = &context();
63  do
64  {
65  if ( ctx->relation().name() == mRelation.name() && ctx->formMode() == QgsAttributeEditorContext::Embed )
66  {
67  w->setVisible( false );
68  break;
69  }
70  ctx = ctx->parentContext();
71  }
72  while ( ctx );
73 
74  mWidget = w;
75 }
76 
78 {
79  return mWidget;
80 }
QLayout * layout() const
bool isValid() const
Returns the validity of this relation.
void setFeature(const QgsFeature &feature) override
This class contains context information for attribute editor widgets.
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:176
const QgsAttributeEditorContext & context()
Returns information about the context in which this widget is shown.
void setLayout(QLayout *layout)
QgsRelationWidgetWrapper(QgsVectorLayer *vl, const QgsRelation &relation, QWidget *editor=0, QWidget *parent=0)
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void setSizePolicy(QSizePolicy)
void addWidget(QWidget *w)
void setRelationFeature(const QgsRelation &relation, const QgsFeature &feature)
const QgsRelation & relation() const
void setEditorContext(const QgsAttributeEditorContext &context)
bool valid() override
Return true if the widget has been properly initialized.
const QgsAttributeEditorContext * parentContext() const
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Represents a vector layer which manages a vector based data sets.
Manages an editor widget Widget and wrapper share the same parent.
A form was embedded as a widget on another form.