QGIS API Documentation  2.99.0-Master (25b0421)
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"
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  , mIndeterminateState( false )
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( QStringLiteral( "ShowForm" ), false ).toBool();
49  bool mapIdent = config( QStringLiteral( "MapIdentification" ), false ).toBool();
50  bool readOnlyWidget = config( QStringLiteral( "ReadOnly" ), false ).toBool();
51  bool orderByValue = config( QStringLiteral( "OrderByValue" ), false ).toBool();
52  bool showOpenFormButton = config( QStringLiteral( "ShowOpenFormButton" ), true ).toBool();
53 
54  mWidget->setEmbedForm( showForm );
55  mWidget->setReadOnlySelector( readOnlyWidget );
56  mWidget->setAllowMapIdentification( mapIdent );
57  mWidget->setOrderByValue( orderByValue );
58  mWidget->setOpenFormButtonVisible( showOpenFormButton );
59  if ( config( QStringLiteral( "FilterFields" ), QVariant() ).isValid() )
60  {
61  mWidget->setFilterFields( config( QStringLiteral( "FilterFields" ) ).toStringList() );
62  mWidget->setChainFilters( config( QStringLiteral( "ChainFilters" ) ).toBool() );
63  }
64  mWidget->setAllowAddFeatures( config( QStringLiteral( "AllowAddFeatures" ), false ).toBool() );
65 
66  const QVariant relationName = config( QStringLiteral( "Relation" ) );
67  QgsRelation relation = relationName.isValid() ?
68  QgsProject::instance()->relationManager()->relation( relationName.toString() ) :
70 
71  // If this widget is already embedded by the same relation, reduce functionality
72  const QgsAttributeEditorContext *ctx = &context();
73  do
74  {
75  if ( ctx->relation().name() == relation.name() )
76  {
77  mWidget->setEmbedForm( false );
78  mWidget->setReadOnlySelector( false );
79  mWidget->setAllowMapIdentification( false );
80  break;
81  }
82  ctx = ctx->parentContext();
83  }
84  while ( ctx );
85 
86  mWidget->setRelation( relation, config( QStringLiteral( "AllowNULL" ) ).toBool() );
87 
88  connect( mWidget, &QgsRelationReferenceWidget::foreignKeyChanged, this, &QgsRelationReferenceWidgetWrapper::foreignKeyChanged );
89 }
90 
92 {
93  if ( !mWidget )
94  return QVariant( field().type() );
95 
96  QVariant v = mWidget->foreignKey();
97 
98  if ( v.isNull() )
99  {
100  return QVariant( field().type() );
101  }
102  else
103  {
104  return v;
105  }
106 }
107 
109 {
110  return mWidget;
111 }
112 
114 {
115  if ( mWidget )
116  {
117  mWidget->showIndeterminateState();
118  }
119  mIndeterminateState = true;
120 }
121 
123 {
124  if ( !mWidget || ( !mIndeterminateState && val == value() && val.isNull() == value().isNull() ) )
125  return;
126 
127  mIndeterminateState = false;
128  mWidget->setForeignKey( val );
129 }
130 
132 {
133  if ( !mWidget )
134  return;
135 
136  mWidget->setRelationEditable( enabled );
137 }
138 
139 void QgsRelationReferenceWidgetWrapper::foreignKeyChanged( QVariant value )
140 {
141  if ( !value.isValid() || value.isNull() )
142  {
143  value = QVariant( field().type() );
144  }
145  emit valueChanged( value );
146 }
147 
149 {
150  if ( mWidget )
151  {
152  switch ( status )
153  {
155  mWidget->setStyleSheet( QString() );
156  break;
157 
159  mWidget->setStyleSheet( QStringLiteral( ".QComboBox { background-color: #dd7777; }" ) );
160  break;
161 
163  mWidget->setStyleSheet( QStringLiteral( ".QComboBox { background-color: #ffd85d; }" ) );
164  break;
165  }
166  }
167 }
void setEditorContext(const QgsAttributeEditorContext &context, QgsMapCanvas *canvas, QgsMessageBar *messageBar)
QString name
Definition: qgsrelation.h:45
QVariant foreignKey() const
returns the related feature foreign key
Widget failed at least one soft (non-enforced) constraint.
void valueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
void setFilterFields(const QStringList &filterFields)
Set the fields for which filter comboboxes will be created.
void foreignKeyChanged(const QVariant &)
This class contains context information for attribute editor widgets.
Manages an editor widget Widget and wrapper share the same parent.
void setOpenFormButtonVisible(bool openFormButtonVisible)
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
Widget failed at least one hard (enforced) constraint.
QVariantMap config() const
Returns the whole config.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
void setAllowMapIdentification(bool allowMapIdentification)
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
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.
virtual QVariant value() const override
Will be used to access the widget&#39;s value.
QList< QgsRelation > referencingRelations(int idx) const
Get relations, where the foreign key is on this layer.
const QgsRelation & relation() const
QgsRelationManager relationManager
Definition: qgsproject.h:91
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 QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
const QgsAttributeEditorContext * parentContext() const
void updateConstraintWidgetStatus(ConstraintResult status) override
This should update the widget with a visual cue if a constraint status changed.
Widget passed constraints successfully.
ConstraintResult
Result of constraint checks.
bool isValid
Definition: qgsrelation.h:46
void setAllowAddFeatures(bool allowAddFeatures)
Determines if a button for adding new features should be shown.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:383
void showIndeterminateState()
Sets the widget to display in an indeterminate "mixed value" state.
int fieldIdx() const
Access the field index.
bool valid() const override
Return true if the widget has been properly initialized.
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Get access to a relation by its id.
Represents a vector layer which manages a vector based data sets.
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