QGIS API Documentation  2.11.0-Master
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( NULL )
25  , mCanvas( canvas )
26  , mMessageBar( messageBar )
27 {
28 }
29 
31 {
33  w->setSizePolicy( w->sizePolicy().horizontalPolicy(), QSizePolicy::Expanding );
34  return w;
35 }
36 
38 {
39  QgsRelationReferenceWidget* w = dynamic_cast<QgsRelationReferenceWidget*>( editor );
40  if ( !w )
41  {
42  w = new QgsRelationReferenceWidget( editor );
43  }
44 
45  mWidget = w;
46 
47  mWidget->setEditorContext( context(), mCanvas, mMessageBar );
48 
49  bool showForm = config( "ShowForm", true ).toBool();
50  bool mapIdent = config( "MapIdentification", false ).toBool();
51  bool readOnlyWidget = config( "ReadOnly", false ).toBool();
52  bool orderByValue = config( "OrderByValue", false ).toBool();
53 
54  mWidget->setEmbedForm( showForm );
55  mWidget->setReadOnlySelector( readOnlyWidget );
56  mWidget->setAllowMapIdentification( mapIdent );
57  mWidget->setOrderByValue( orderByValue );
58  if ( config( "FilterFields", QVariant() ).isValid() )
59  {
60  mWidget->setFilterFields( config( "FilterFields" ).toStringList() );
61  mWidget->setChainFilters( config( "ChainFilters" ).toBool() );
62  }
63 
64  QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( "Relation" ).toString() );
65 
66  // If this widget is already embedded by the same relation, reduce functionality
67  const QgsAttributeEditorContext* ctx = &context();
68  do
69  {
70  if ( ctx->relation().name() == relation.name() )
71  {
72  mWidget->setEmbedForm( false );
73  mWidget->setReadOnlySelector( false );
74  mWidget->setAllowMapIdentification( false );
75  }
76  ctx = ctx->parentContext();
77  }
78  while ( ctx );
79 
80  mWidget->setRelation( relation, config( "AllowNULL" ).toBool() );
81 
82  connect( mWidget, SIGNAL( foreignKeyChanged( QVariant ) ), this, SLOT( foreignKeyChanged( QVariant ) ) );
83 }
84 
86 {
87  if ( !mWidget )
88  return QVariant( field().type() );
89 
90  QVariant v = mWidget->foreignKey();
91 
92  if ( v.isNull() )
93  {
94  return QVariant( field().type() );
95  }
96  else
97  {
98  return v;
99  }
100 }
101 
103 {
104  return mWidget;
105 }
106 
108 {
109  if ( !mWidget || val == value() )
110  return;
111 
112  mWidget->setForeignKey( val );
113 }
114 
116 {
117  if ( !mWidget )
118  return;
119 
120  mWidget->setRelationEditable( enabled );
121 }
122 
123 void QgsRelationReferenceWidgetWrapper::foreignKeyChanged( QVariant value )
124 {
125  if ( !value.isValid() || value.isNull() )
126  {
127  value = QVariant( field().type() );
128  }
129  emit valueChanged( value );
130 }
void setEditorContext(const QgsAttributeEditorContext &context, QgsMapCanvas *canvas, QgsMessageBar *messageBar)
void valueChanged()
Will call the value() method to determine the emitted value.
This class contains context information for attribute editor widgets.
Manages an editor widget Widget and wrapper share the same parent.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:42
QVariant foreignKey()
returns the related feature foreign key
void setFilterFields(QStringList filterFields)
Set the fields for which filter comboboxes will be created.
QString name() const
Returns a human readable name for this relation.
QgsField field()
Access the field.
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()
Returns information about the context in which this widget is shown.
bool isNull() const
const QgsEditorWidgetConfig config()
Returns the whole config.
QgsRelation relation(const QString &id) const
Get access to a relation by its id.
virtual QVariant value() override
Will be used to access the widget's value.
bool valid() override
Return true if the widget has been properly initialized.
void setRelation(QgsRelation relation, bool allowNullValue)
void setSizePolicy(QSizePolicy)
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:352
const QgsAttributeEditorContext * parentContext() const
bool isValid() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
QgsRelationManager * relationManager() const
QgsRelationReferenceWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QgsMapCanvas *canvas, QgsMessageBar *messageBar, QWidget *parent=0)
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