QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsfield.h"
19 #include "qgsmaplayerregistry.h"
21 #include "qgsvectorlayer.h"
22 #include "qgsproject.h"
23 
24 #include <QSettings>
25 #include <QStringListModel>
26 
28  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
29  , mWidget( nullptr )
30  , mLayer( nullptr )
31  , mCanvas( canvas )
32 {
33 
34 }
35 
37 {
38  return true;
39 }
40 
42 {
43  return mExpression;
44 }
45 
47 {
48  if ( mWidget )
49  {
50  return mWidget->foreignKey();
51  }
52  return QVariant();
53 }
54 
55 QgsSearchWidgetWrapper::FilterFlags QgsRelationReferenceSearchWidgetWrapper::supportedFlags() const
56 {
57  return EqualTo | NotEqualTo | IsNull | IsNotNull;
58 }
59 
60 QgsSearchWidgetWrapper::FilterFlags QgsRelationReferenceSearchWidgetWrapper::defaultFlags() const
61 {
62  return EqualTo;
63 }
64 
65 QString QgsRelationReferenceSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
66 {
67  QString fieldName = QgsExpression::quotedColumnRef( layer()->fields().at( mFieldIdx ).name() );
68 
69  //clear any unsupported flags
70  flags &= supportedFlags();
71  if ( flags & IsNull )
72  return fieldName + " IS NULL";
73  if ( flags & IsNotNull )
74  return fieldName + " IS NOT NULL";
75 
76  QVariant v = value();
77  if ( !v.isValid() )
78  return QString();
79 
80  switch ( v.type() )
81  {
82  case QVariant::Int:
83  case QVariant::UInt:
84  case QVariant::Double:
85  case QVariant::LongLong:
86  case QVariant::ULongLong:
87  {
88  if ( flags & EqualTo )
89  return fieldName + '=' + v.toString();
90  else if ( flags & NotEqualTo )
91  return fieldName + "<>" + v.toString();
92  break;
93  }
94 
95  default:
96  {
97  if ( flags & EqualTo )
98  return fieldName + "='" + v.toString() + '\'';
99  else if ( flags & NotEqualTo )
100  return fieldName + "<>'" + v.toString() + '\'';
101  break;
102  }
103  }
104 
105  return QString();
106 }
107 
109 {
110  if ( mWidget )
111  {
112  mWidget->showIndeterminateState();
113  }
114 }
115 
117 {
118  if ( mWidget )
119  {
120  mWidget->setEnabled( enabled );
121  }
122 }
123 
125 {
126  return true;
127 }
128 
130 {
131  if ( !value.isValid() )
132  {
133  clearExpression();
134  emit valueCleared();
135  }
136  else
137  {
138  QSettings settings;
139  setExpression( value.isNull() ? settings.value( "qgis/nullValue", "NULL" ).toString() : value.toString() );
140  emit valueChanged();
141  }
143 }
144 
146 {
147  QSettings settings;
148  QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
149  QString fieldName = layer()->fields().at( mFieldIdx ).name();
150 
151  QString str;
152  if ( exp == nullValue )
153  {
154  str = QString( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
155  }
156  else
157  {
158  str = QString( "%1 = '%3'" )
159  .arg( QgsExpression::quotedColumnRef( fieldName ),
160  exp.replace( '\'', "''" )
161  );
162  }
163  mExpression = str;
164 }
165 
167 {
168  return new QgsRelationReferenceWidget( parent );
169 }
170 
172 {
173  mWidget = qobject_cast<QgsRelationReferenceWidget*>( editor );
174  if ( !mWidget )
175  return;
176 
177  mWidget->setEditorContext( context(), mCanvas, nullptr );
178 
179  mWidget->setEmbedForm( false );
180  mWidget->setReadOnlySelector( false );
181  mWidget->setAllowMapIdentification( config( "MapIdentification", false ).toBool() );
182  mWidget->setOrderByValue( config( "OrderByValue", false ).toBool() );
183  mWidget->setAllowAddFeatures( false );
184  mWidget->setOpenFormButtonVisible( false );
185 
186  if ( config( "FilterFields", QVariant() ).isValid() )
187  {
188  mWidget->setFilterFields( config( "FilterFields" ).toStringList() );
189  mWidget->setChainFilters( config( "ChainFilters" ).toBool() );
190  }
191 
192  QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( "Relation" ).toString() );
193  mWidget->setRelation( relation, false );
194 
195  mWidget->showIndeterminateState();
196  connect( mWidget, SIGNAL( foreignKeyChanged( QVariant ) ), this, SLOT( onValueChanged( QVariant ) ) );
197 }
198 
199 
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
Manages an editor widget Widget and wrapper share the same parent.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QString name
Definition: qgsfield.h:52
QgsRelationReferenceSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QgsMapCanvas *canvas, QWidget *parent=nullptr)
Constructor for QgsRelationReferenceSearchWidgetWrapper.
void setFilterFields(const QStringList &filterFields)
Set the fields for which filter comboboxes will be created.
void setOpenFormButtonVisible(bool openFormButtonVisible)
static QString toString(FilterFlag flag)
Returns a translated string representing a filter flag.
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
QgsRelationManager * relationManager() const
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setAllowMapIdentification(bool allowMapIdentification)
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:422
QgsFields fields() const
Returns the list of fields of this layer.
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes...
void setOrderByValue(bool orderByValue)
Set if the widget will order the combobox entries by value.
const char * name() const
void setEnabled(bool)
bool isNull() const
void valueChanged()
Emitted when a user changes the value of the search widget.
void onValueChanged(QVariant value)
Called when current value of search widget changes.
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.
void setRelation(const QgsRelation &relation, bool allowNullValue)
QString expression() override
Will be used to access the widget&#39;s value.
virtual QString createExpression(FilterFlags flags) const override
Creates a filter expression based on the current state of the search widget and the specified filter ...
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
QString & replace(int position, int n, QChar after)
QVariant value(const QString &key, const QVariant &defaultValue) const
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:382
void showIndeterminateState()
Sets the widget to display in an indeterminate "mixed value" state.
FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
bool isValid() const
QgsEditorWidgetConfig config() const
Returns the whole config.
Type type() const
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
QgsRelation relation(const QString &id) const
Get access to a relation by its id.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
void setChainFilters(bool chainFilters)
Set if filters are chained.