QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrelationreferencewidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelationreferencewidget.cpp
3  --------------------------------------
4  Date : 20.4.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias dot kuhn at gmx 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 
22 QgsRelationReferenceWidgetWrapper::QgsRelationReferenceWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QgsAttributeEditorContext context, QgsMapCanvas* canvas, QgsMessageBar* messageBar, QWidget* parent )
23  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
24  , mEditorContext( context )
25  , mCanvas( canvas )
26  , mMessageBar( messageBar )
27 {
28 }
29 
31 {
32  return new QgsRelationReferenceWidget( parent );
33 }
34 
36 {
37  QgsRelationReferenceWidget* w = dynamic_cast<QgsRelationReferenceWidget*>( editor );
38  if ( !w )
39  {
40  w = new QgsRelationReferenceWidget( editor );
41  }
42 
43  mWidget = w;
44 
46 
47  bool showForm = config( "ShowForm", true ).toBool();
48  bool mapIdent = config( "MapIdentification", false ).toBool();
49  bool readOnlyWidget = config( "ReadOnly", false ).toBool();
50  mWidget->setEmbedForm( showForm );
52  mWidget->setReadOnlySelector( readOnlyWidget );
53 
54  QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( "Relation" ).toString() );
55  mWidget->setRelation( relation, config( "AllowNULL" ).toBool() );
56 
57  connect( mWidget, SIGNAL( relatedFeatureChanged( QVariant ) ), this, SIGNAL( valueChanged( QVariant ) ) );
58 }
59 
61 {
62  if ( !mWidget )
63  return QVariant( field().type() );
64 
65  QVariant v = mWidget->relatedFeature();
66 
67  if ( v.isNull() )
68  {
69  return QVariant( field().type() );
70  }
71  else
72  {
73  return v;
74  }
75 }
76 
77 void QgsRelationReferenceWidgetWrapper::setValue( const QVariant& value )
78 {
79  if ( !mWidget )
80  return;
81 
82  mWidget->setRelatedFeature( value );
83 }
84 
86 {
87  if ( !mWidget )
88  return;
89 
90  mWidget->setRelationEditable( enabled );
91 }
virtual QWidget * createWidget(QWidget *parent)
This method should create a new widget with the provided parent.
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:43
QgsField field()
Access the field.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
void setRelatedFeature(const QVariant &value)
void setAllowMapIdentification(bool allowMapIdentification)
virtual QVariant value()
Will be used to access the widget's value.
const QgsEditorWidgetConfig config()
Returns the whole config.
QgsRelation relation(const QString &id) const
void setEditorContext(QgsAttributeEditorContext context, QgsMapCanvas *canvas, QgsMessageBar *messageBar)
void setRelation(QgsRelation relation, bool allowNullValue)
virtual void setValue(const QVariant &value)
virtual void initWidget(QWidget *editor)
This method should initialize the editor widget with runtime data.
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:362
QgsRelationReferenceWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QgsAttributeEditorContext context, QgsMapCanvas *canvas, QgsMessageBar *messageBar, QWidget *parent=0)
Represents a vector layer which manages a vector based data sets.
QgsRelationManager * relationManager() const