QGIS API Documentation  2.14.0-Essen
qgsrelationreferencewidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelationreferencewidgetwrapper.cpp
3  --------------------------------------
4  Date : 20.4.2013
5  Copyright : (C) 2013 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 
16 
18 #include "qgsproject.h"
19 #include "qgsrelationmanager.h"
20 
21 
23  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
24  , mWidget( nullptr )
25  , mCanvas( canvas )
26  , mMessageBar( messageBar )
27 {
28 }
29 
31 {
33  return w;
34 }
35 
37 {
38  QgsRelationReferenceWidget* w = dynamic_cast<QgsRelationReferenceWidget*>( editor );
39  if ( !w )
40  {
41  w = new QgsRelationReferenceWidget( editor );
42  }
43 
44  mWidget = w;
45 
46  mWidget->setEditorContext( context(), mCanvas, mMessageBar );
47 
48  bool showForm = config( "ShowForm", true ).toBool();
49  bool mapIdent = config( "MapIdentification", false ).toBool();
50  bool readOnlyWidget = config( "ReadOnly", false ).toBool();
51  bool orderByValue = config( "OrderByValue", false ).toBool();
52 
53  mWidget->setEmbedForm( showForm );
54  mWidget->setReadOnlySelector( readOnlyWidget );
55  mWidget->setAllowMapIdentification( mapIdent );
56  mWidget->setOrderByValue( orderByValue );
57  if ( config( "FilterFields", QVariant() ).isValid() )
58  {
59  mWidget->setFilterFields( config( "FilterFields" ).toStringList() );
60  mWidget->setChainFilters( config( "ChainFilters" ).toBool() );
61  }
62 
63  QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( "Relation" ).toString() );
64 
65  // If this widget is already embedded by the same relation, reduce functionality
66  const QgsAttributeEditorContext* ctx = &context();
67  do
68  {
69  if ( ctx->relation().name() == relation.name() )
70  {
71  mWidget->setEmbedForm( false );
72  mWidget->setReadOnlySelector( false );
73  mWidget->setAllowMapIdentification( false );
74  }
75  ctx = ctx->parentContext();
76  }
77  while ( ctx );
78 
79  mWidget->setRelation( relation, config( "AllowNULL" ).toBool() );
80 
81  connect( mWidget, SIGNAL( foreignKeyChanged( QVariant ) ), this, SLOT( foreignKeyChanged( QVariant ) ) );
82 }
83 
85 {
86  if ( !mWidget )
87  return QVariant( field().type() );
88 
89  QVariant v = mWidget->foreignKey();
90 
91  if ( v.isNull() )
92  {
93  return QVariant( field().type() );
94  }
95  else
96  {
97  return v;
98  }
99 }
100 
102 {
103  return mWidget;
104 }
105 
107 {
108  if ( !mWidget || val == value() )
109  return;
110 
111  mWidget->setForeignKey( val );
112 }
113 
115 {
116  if ( !mWidget )
117  return;
118 
119  mWidget->setRelationEditable( enabled );
120 }
121 
122 void QgsRelationReferenceWidgetWrapper::foreignKeyChanged( QVariant value )
123 {
124  if ( !value.isValid() || value.isNull() )
125  {
126  value = QVariant( field().type() );
127  }
128  emit valueChanged( value );
129 }
void setEditorContext(const QgsAttributeEditorContext &context, QgsMapCanvas *canvas, QgsMessageBar *messageBar)
void valueChanged()
Will call the value() method to determine the emitted value.
void setFilterFields(const QStringList &filterFields)
Set the fields for which filter comboboxes will be created.
This class contains context information for attribute editor widgets.
Manages an editor widget Widget and wrapper share the same parent.
QgsField field() const
Access the field.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:42
QVariant foreignKey()
returns the related feature foreign key
QString name() const
Returns a human readable name for this relation.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:105
void setAllowMapIdentification(bool allowMapIdentification)
void setForeignKey(const QVariant &value)
this sets the related feature using from the foreign key
void setOrderByValue(bool orderByValue)
Set if the widget will order the combobox entries by value.
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
bool isNull() const
QgsRelation relation(const QString &id) const
Get access to a relation by its id.
virtual QVariant value() const override
Will be used to access the widget&#39;s value.
void setRelation(const QgsRelation &relation, bool allowNullValue)
virtual QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
const QgsRelation & relation() const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:381
QgsEditorWidgetConfig config() const
Returns the whole config.
const QgsAttributeEditorContext * parentContext() const
bool isValid() const
bool valid() const override
Return true if the widget has been properly initialized.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QgsRelationReferenceWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QgsMapCanvas *canvas, QgsMessageBar *messageBar, QWidget *parent=nullptr)
QgsRelationManager * relationManager() const
virtual void setValue(const QVariant &value) override
void setChainFilters(bool chainFilters)
Set if filters are chained.
virtual void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
virtual void setEnabled(bool enabled) override