QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 #include "qgsapplication.h"
26 
27 #include <QStringListModel>
28 
30  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
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 QString QgsRelationReferenceSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
61 {
62  QString fieldName = createFieldIdentifier();
63 
64  //clear any unsupported flags
65  flags &= supportedFlags();
66  if ( flags & IsNull )
67  return fieldName + " IS NULL";
68  if ( flags & IsNotNull )
69  return fieldName + " IS NOT NULL";
70 
71  QVariant v = value();
72  if ( !v.isValid() )
73  return QString();
74 
75  switch ( v.type() )
76  {
77  case QVariant::Int:
78  case QVariant::UInt:
79  case QVariant::Double:
80  case QVariant::LongLong:
81  case QVariant::ULongLong:
82  {
83  if ( flags & EqualTo )
84  return fieldName + '=' + v.toString();
85  else if ( flags & NotEqualTo )
86  return fieldName + "<>" + v.toString();
87  break;
88  }
89 
90  default:
91  {
92  if ( flags & EqualTo )
93  return fieldName + "='" + v.toString() + '\'';
94  else if ( flags & NotEqualTo )
95  return fieldName + "<>'" + v.toString() + '\'';
96  break;
97  }
98  }
99 
100  return QString();
101 }
102 
104 {
105  if ( mWidget )
106  {
107  mWidget->showIndeterminateState();
108  }
109 }
110 
112 {
113  if ( mWidget )
114  {
115  mWidget->setEnabled( enabled );
116  }
117 }
118 
120 {
121  return true;
122 }
123 
125 {
126  if ( !value.isValid() )
127  {
128  clearExpression();
129  emit valueCleared();
130  }
131  else
132  {
133  QgsSettings settings;
134  setExpression( value.isNull() ? QgsApplication::nullRepresentation() : value.toString() );
135  emit valueChanged();
136  }
138 }
139 
141 {
142  QString exp = expression;
143  QString nullValue = QgsApplication::nullRepresentation();
144  QString fieldName = layer()->fields().at( mFieldIdx ).name();
145 
146  QString str;
147  if ( exp == nullValue )
148  {
149  str = QStringLiteral( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
150  }
151  else
152  {
153  str = QStringLiteral( "%1 = '%3'" )
154  .arg( QgsExpression::quotedColumnRef( fieldName ),
155  exp.replace( '\'', QLatin1String( "''" ) )
156  );
157  }
158  mExpression = str;
159 }
160 
162 {
163  return new QgsRelationReferenceWidget( parent );
164 }
165 
167 {
168  mWidget = qobject_cast<QgsRelationReferenceWidget *>( editor );
169  if ( !mWidget )
170  return;
171 
172  mWidget->setEditorContext( context(), mCanvas, nullptr );
173 
174  mWidget->setEmbedForm( false );
175  mWidget->setReadOnlySelector( false );
176  mWidget->setAllowMapIdentification( config( QStringLiteral( "MapIdentification" ), false ).toBool() );
177  mWidget->setOrderByValue( config( QStringLiteral( "OrderByValue" ), false ).toBool() );
178  mWidget->setAllowAddFeatures( false );
179  mWidget->setOpenFormButtonVisible( false );
180 
181  if ( config( QStringLiteral( "FilterFields" ), QVariant() ).isValid() )
182  {
183  mWidget->setFilterFields( config( QStringLiteral( "FilterFields" ) ).toStringList() );
184  mWidget->setChainFilters( config( QStringLiteral( "ChainFilters" ) ).toBool() );
185  }
186 
187  QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "Relation" ) ).toString() );
188  mWidget->setRelation( relation, false );
189 
190  mWidget->showIndeterminateState();
192 }
193 
194 
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.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QString name
Definition: qgsfield.h:58
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
QVariantMap config() const
Returns the whole config.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
QString expression() const override
Will be used to access the widget&#39;s value.
QString createFieldIdentifier() const
Gets 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
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
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.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
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.
QgsRelationManager relationManager
Definition: qgsproject.h:100
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
Returns true if the widget has been properly initialized.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:438
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
Returns the vector layer associated with the widget.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets 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.