QGIS API Documentation  3.0.2-Girona (307d082)
qgsrelationreferencesearchwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelationreferencesearchwidgetwrapper.cpp
3  ------------------------------------------
4  Date : 2016-05-25
5  Copyright : (C) 2016 Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
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 
17 
18 #include "qgsfields.h"
20 #include "qgsvectorlayer.h"
21 #include "qgsproject.h"
23 #include "qgsrelationmanager.h"
24 #include "qgssettings.h"
25 
26 #include <QStringListModel>
27 
29  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
30  , mCanvas( canvas )
31 {
32 
33 }
34 
36 {
37  return true;
38 }
39 
41 {
42  return mExpression;
43 }
44 
46 {
47  if ( mWidget )
48  {
49  return mWidget->foreignKey();
50  }
51  return QVariant();
52 }
53 
54 QgsSearchWidgetWrapper::FilterFlags QgsRelationReferenceSearchWidgetWrapper::supportedFlags() const
55 {
56  return EqualTo | NotEqualTo | IsNull | IsNotNull;
57 }
58 
59 QgsSearchWidgetWrapper::FilterFlags QgsRelationReferenceSearchWidgetWrapper::defaultFlags() const
60 {
61  return EqualTo;
62 }
63 
64 QString QgsRelationReferenceSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
65 {
66  QString fieldName = createFieldIdentifier();
67 
68  //clear any unsupported flags
69  flags &= supportedFlags();
70  if ( flags & IsNull )
71  return fieldName + " IS NULL";
72  if ( flags & IsNotNull )
73  return fieldName + " IS NOT NULL";
74 
75  QVariant v = value();
76  if ( !v.isValid() )
77  return QString();
78 
79  switch ( v.type() )
80  {
81  case QVariant::Int:
82  case QVariant::UInt:
83  case QVariant::Double:
84  case QVariant::LongLong:
85  case QVariant::ULongLong:
86  {
87  if ( flags & EqualTo )
88  return fieldName + '=' + v.toString();
89  else if ( flags & NotEqualTo )
90  return fieldName + "<>" + v.toString();
91  break;
92  }
93 
94  default:
95  {
96  if ( flags & EqualTo )
97  return fieldName + "='" + v.toString() + '\'';
98  else if ( flags & NotEqualTo )
99  return fieldName + "<>'" + v.toString() + '\'';
100  break;
101  }
102  }
103 
104  return QString();
105 }
106 
108 {
109  if ( mWidget )
110  {
111  mWidget->showIndeterminateState();
112  }
113 }
114 
116 {
117  if ( mWidget )
118  {
119  mWidget->setEnabled( enabled );
120  }
121 }
122 
124 {
125  return true;
126 }
127 
129 {
130  if ( !value.isValid() )
131  {
132  clearExpression();
133  emit valueCleared();
134  }
135  else
136  {
137  QgsSettings settings;
138  setExpression( value.isNull() ? QgsApplication::nullRepresentation() : value.toString() );
139  emit valueChanged();
140  }
142 }
143 
145 {
146  QString exp = expression;
147  QString nullValue = QgsApplication::nullRepresentation();
148  QString fieldName = layer()->fields().at( mFieldIdx ).name();
149 
150  QString str;
151  if ( exp == nullValue )
152  {
153  str = QStringLiteral( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
154  }
155  else
156  {
157  str = QStringLiteral( "%1 = '%3'" )
158  .arg( QgsExpression::quotedColumnRef( fieldName ),
159  exp.replace( '\'', QLatin1String( "''" ) )
160  );
161  }
162  mExpression = str;
163 }
164 
166 {
167  return new QgsRelationReferenceWidget( parent );
168 }
169 
171 {
172  mWidget = qobject_cast<QgsRelationReferenceWidget *>( editor );
173  if ( !mWidget )
174  return;
175 
176  mWidget->setEditorContext( context(), mCanvas, nullptr );
177 
178  mWidget->setEmbedForm( false );
179  mWidget->setReadOnlySelector( false );
180  mWidget->setAllowMapIdentification( config( QStringLiteral( "MapIdentification" ), false ).toBool() );
181  mWidget->setOrderByValue( config( QStringLiteral( "OrderByValue" ), false ).toBool() );
182  mWidget->setAllowAddFeatures( false );
183  mWidget->setOpenFormButtonVisible( false );
184 
185  if ( config( QStringLiteral( "FilterFields" ), QVariant() ).isValid() )
186  {
187  mWidget->setFilterFields( config( QStringLiteral( "FilterFields" ) ).toStringList() );
188  mWidget->setChainFilters( config( QStringLiteral( "ChainFilters" ) ).toBool() );
189  }
190 
191  QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "Relation" ) ).toString() );
192  mWidget->setRelation( relation, false );
193 
194  mWidget->showIndeterminateState();
196 }
197 
198 
QVariant value() const
Returns a variant representing the current state of the widget.
void setEditorContext(const QgsAttributeEditorContext &context, QgsMapCanvas *canvas, QgsMessageBar *messageBar)
QVariant foreignKey() const
returns the related feature foreign key
Shows a search widget on a filter form.
QString name
Definition: qgsfield.h:57
QgsRelationReferenceSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QgsMapCanvas *canvas, QWidget *parent=nullptr)
Constructor for QgsRelationReferenceSearchWidgetWrapper.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
void setFilterFields(const QStringList &filterFields)
Set the fields for which filter comboboxes will be created.
void foreignKeyChanged(const QVariant &)
void setOpenFormButtonVisible(bool openFormButtonVisible)
Supports searching for non-null values.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void clearExpression()
clears the expression to search for all features
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
QString expression() const override
Will be used to access the widget&#39;s value.
QString createFieldIdentifier() const
Get a field name or expression to use as field comparison.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setAllowMapIdentification(bool allowMapIdentification)
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:145
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes...
Supports searching for null values.
static QString toString(QgsSearchWidgetWrapper::FilterFlag flag)
Returns a translated string representing a filter flag.
void setOrderByValue(bool orderByValue)
Set if the widget will order the combobox entries by value.
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
QgsFields fields() const override
Returns the list of fields of this layer.
void valueChanged()
Emitted when a user changes the value of the search widget.
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
QgsSearchWidgetWrapper::FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QgsRelationManager relationManager
Definition: qgsproject.h:92
void setRelation(const QgsRelation &relation, bool allowNullValue)
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
void onValueChanged(const QVariant &value)
Called when current value of search widget changes.
void setAllowAddFeatures(bool allowAddFeatures)
Determines if a button for adding new features should be shown.
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state...
bool valid() const override
Return true if the widget has been properly initialized.
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.
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
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.
void setChainFilters(bool chainFilters)
Set if filters are chained.