QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 QString QgsRelationReferenceSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
60 {
61  QString fieldName = createFieldIdentifier();
62 
63  //clear any unsupported flags
64  flags &= supportedFlags();
65  if ( flags & IsNull )
66  return fieldName + " IS NULL";
67  if ( flags & IsNotNull )
68  return fieldName + " IS NOT NULL";
69 
70  QVariant v = value();
71  if ( !v.isValid() )
72  return QString();
73 
74  switch ( v.type() )
75  {
76  case QVariant::Int:
77  case QVariant::UInt:
78  case QVariant::Double:
79  case QVariant::LongLong:
80  case QVariant::ULongLong:
81  {
82  if ( flags & EqualTo )
83  return fieldName + '=' + v.toString();
84  else if ( flags & NotEqualTo )
85  return fieldName + "<>" + v.toString();
86  break;
87  }
88 
89  default:
90  {
91  if ( flags & EqualTo )
92  return fieldName + "='" + v.toString() + '\'';
93  else if ( flags & NotEqualTo )
94  return fieldName + "<>'" + v.toString() + '\'';
95  break;
96  }
97  }
98 
99  return QString();
100 }
101 
103 {
104  if ( mWidget )
105  {
106  mWidget->showIndeterminateState();
107  }
108 }
109 
111 {
112  if ( mWidget )
113  {
114  mWidget->setEnabled( enabled );
115  }
116 }
117 
119 {
120  return true;
121 }
122 
124 {
125  if ( !value.isValid() )
126  {
127  clearExpression();
128  emit valueCleared();
129  }
130  else
131  {
132  QgsSettings settings;
133  setExpression( value.isNull() ? QgsApplication::nullRepresentation() : value.toString() );
134  emit valueChanged();
135  }
137 }
138 
140 {
141  QString exp = expression;
142  QString nullValue = QgsApplication::nullRepresentation();
143  QString fieldName = layer()->fields().at( mFieldIdx ).name();
144 
145  QString str;
146  if ( exp == nullValue )
147  {
148  str = QStringLiteral( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
149  }
150  else
151  {
152  str = QStringLiteral( "%1 = '%3'" )
153  .arg( QgsExpression::quotedColumnRef( fieldName ),
154  exp.replace( '\'', QLatin1String( "''" ) )
155  );
156  }
157  mExpression = str;
158 }
159 
161 {
162  return new QgsRelationReferenceWidget( parent );
163 }
164 
166 {
167  mWidget = qobject_cast<QgsRelationReferenceWidget *>( editor );
168  if ( !mWidget )
169  return;
170 
171  mWidget->setEditorContext( context(), mCanvas, nullptr );
172 
173  mWidget->setEmbedForm( false );
174  mWidget->setReadOnlySelector( false );
175  mWidget->setAllowMapIdentification( config( QStringLiteral( "MapIdentification" ), false ).toBool() );
176  mWidget->setOrderByValue( config( QStringLiteral( "OrderByValue" ), false ).toBool() );
177  mWidget->setAllowAddFeatures( false );
178  mWidget->setOpenFormButtonVisible( false );
179 
180  if ( config( QStringLiteral( "FilterFields" ), QVariant() ).isValid() )
181  {
182  mWidget->setFilterFields( config( QStringLiteral( "FilterFields" ) ).toStringList() );
183  mWidget->setChainFilters( config( QStringLiteral( "ChainFilters" ) ).toBool() );
184  }
185 
186  QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "Relation" ) ).toString() );
187  mWidget->setRelation( relation, false );
188 
189  mWidget->showIndeterminateState();
191 }
192 
193 
void setEditorContext(const QgsAttributeEditorContext &context, QgsMapCanvas *canvas, QgsMessageBar *messageBar)
Shows a search widget on a filter form.
QVariantMap config() const
Returns the whole config.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
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:58
void setFilterFields(const QStringList &filterFields)
Sets 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
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
QVariant value() const
Returns a variant representing the current state of the widget.
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.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setAllowMapIdentification(bool allowMapIdentification)
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)
Sets if the widget will order the combobox entries by value.
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
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.
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QVariant foreignKey() const
returns the related feature foreign key
QgsRelationManager relationManager
Definition: qgsproject.h:100
void setRelation(const QgsRelation &relation, bool allowNullValue)
QString createFieldIdentifier() const
Gets a field name or expression to use as field comparison.
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.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state...
bool valid() const override
Returns true if the widget has been properly initialized.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:411
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.
Represents a vector layer which manages a vector based data sets.
void setChainFilters(bool chainFilters)
Set if filters are chained.