QGIS API Documentation  2.8.2-Wien
 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  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  , 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 
59  QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( "Relation" ).toString() );
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() == relation.name() )
66  {
67  mWidget->setEmbedForm( false );
68  mWidget->setReadOnlySelector( false );
69  mWidget->setAllowMapIdentification( false );
70  }
71  ctx = ctx->parentContext();
72  }
73  while ( ctx );
74 
75  mWidget->setRelation( relation, config( "AllowNULL" ).toBool() );
76 
77  connect( mWidget, SIGNAL( foreignKeyChanged( QVariant ) ), this, SLOT( foreignKeyChanged( QVariant ) ) );
78 }
79 
81 {
82  if ( !mWidget )
83  return QVariant( field().type() );
84 
85  QVariant v = mWidget->foreignKey();
86 
87  if ( v.isNull() )
88  {
89  return QVariant( field().type() );
90  }
91  else
92  {
93  return v;
94  }
95 }
96 
97 void QgsRelationReferenceWidgetWrapper::setValue( const QVariant& value )
98 {
99  if ( !mWidget )
100  return;
101 
102  mWidget->setForeignKey( value );
103 }
104 
106 {
107  if ( !mWidget )
108  return;
109 
110  mWidget->setRelationEditable( enabled );
111 }
112 
113 void QgsRelationReferenceWidgetWrapper::foreignKeyChanged( QVariant value )
114 {
115  if ( !value.isValid() || value.isNull() )
116  {
117  value = QVariant( field().type() );
118  }
119  emit valueChanged( value );
120 }