QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 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, QgsMapCanvas* canvas, QgsMessageBar* messageBar, QWidget* parent )
23  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
24  , mCanvas( canvas )
25  , mMessageBar( messageBar )
26 {
27 }
28 
30 {
32  w->setSizePolicy( w->sizePolicy().horizontalPolicy(), QSizePolicy::Expanding );
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 
52  mWidget->setEmbedForm( showForm );
53  mWidget->setReadOnlySelector( readOnlyWidget );
54  mWidget->setAllowMapIdentification( mapIdent );
55 
56  QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( "Relation" ).toString() );
57 
58  // If this widget is already embedded by the same relation, reduce functionality
59  const QgsAttributeEditorContext* ctx = &context();
60  do
61  {
62  if ( ctx->relation().name() == relation.name() )
63  {
64  mWidget->setEmbedForm( false );
65  mWidget->setReadOnlySelector( false );
66  mWidget->setAllowMapIdentification( false );
67  }
68  ctx = ctx->parentContext();
69  }
70  while ( ctx );
71 
72  mWidget->setRelation( relation, config( "AllowNULL" ).toBool() );
73 
74  connect( mWidget, SIGNAL( foreignKeyChanged( QVariant ) ), this, SLOT( foreignKeyChanged( QVariant ) ) );
75 }
76 
78 {
79  if ( !mWidget )
80  return QVariant( field().type() );
81 
82  QVariant v = mWidget->foreignKey();
83 
84  if ( v.isNull() )
85  {
86  return QVariant( field().type() );
87  }
88  else
89  {
90  return v;
91  }
92 }
93 
94 void QgsRelationReferenceWidgetWrapper::setValue( const QVariant& value )
95 {
96  if ( !mWidget )
97  return;
98 
99  mWidget->setForeignKey( value );
100 }
101 
103 {
104  if ( !mWidget )
105  return;
106 
107  mWidget->setRelationEditable( enabled );
108 }
109 
110 void QgsRelationReferenceWidgetWrapper::foreignKeyChanged( QVariant value )
111 {
112  if ( !value.isValid() || value.isNull() )
113  {
114  value = QVariant( field().type() );
115  }
116  emit valueChanged( value );
117 }
virtual QWidget * createWidget(QWidget *parent)
This method should create a new widget with the provided parent.
const QString name() const
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
QgsField field()
Access the field.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
void setAllowMapIdentification(bool allowMapIdentification)
void setForeignKey(const QVariant &value)
this sets the related feature using from the foreign key
const QgsAttributeEditorContext & context()
Returns information about the context in which this widget is shown.
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
Get access to a relation by its id.
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.
const QgsRelation & relation() const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:362
const QgsAttributeEditorContext * parentContext() const
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)