QGIS API Documentation  3.6.0-Noosa (5873452)
qgsvaluerelationwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvaluerelationwidgetwrapper.cpp
3  --------------------------------------
4  Date : 5.1.2014
5  Copyright : (C) 2014 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 
17 
18 #include "qgis.h"
19 #include "qgsfields.h"
20 #include "qgsproject.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsfilterlineedit.h"
24 #include "qgsfeatureiterator.h"
26 #include "qgsattributeform.h"
27 #include "qgsattributes.h"
28 
29 #include <QHeaderView>
30 #include <QComboBox>
31 #include <QLineEdit>
32 #include <QTableWidget>
33 #include <QStringListModel>
34 #include <QCompleter>
35 
36 QgsValueRelationWidgetWrapper::QgsValueRelationWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
37  : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
38 {
39 }
40 
41 
43 {
44  QVariant v;
45 
46  if ( mComboBox )
47  {
48  int cbxIdx = mComboBox->currentIndex();
49  if ( cbxIdx > -1 )
50  {
51  v = mComboBox->currentData();
52  }
53  }
54 
55  const int nofColumns = columnCount();
56 
57  if ( mTableWidget )
58  {
59  QStringList selection;
60  for ( int j = 0; j < mTableWidget->rowCount(); j++ )
61  {
62  for ( int i = 0; i < nofColumns; ++i )
63  {
64  QTableWidgetItem *item = mTableWidget->item( j, i );
65  if ( item )
66  {
67  if ( item->checkState() == Qt::Checked )
68  selection << item->data( Qt::UserRole ).toString();
69  }
70  }
71  }
72 
73  if ( layer()->fields().at( fieldIdx() ).type() == QVariant::Map )
74  {
75  QVariantList vl;
76  //store as QVariantList because it's json
77  for ( const QString &s : qgis::as_const( selection ) )
78  {
79  vl << s;
80  }
81  v = vl;
82  }
83  else
84  {
85  //store as hstore string
86  v = selection.join( ',' ).prepend( '{' ).append( '}' );
87  }
88  }
89 
90  if ( mLineEdit )
91  {
92  for ( const QgsValueRelationFieldFormatter::ValueRelationItem &item : qgis::as_const( mCache ) )
93  {
94  if ( item.value == mLineEdit->text() )
95  {
96  v = item.key;
97  break;
98  }
99  }
100  }
101 
102  return v;
103 }
104 
106 {
107  QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
108  if ( form )
110 
111  mExpression = config().value( QStringLiteral( "FilterExpression" ) ).toString();
112 
113  if ( config( QStringLiteral( "AllowMulti" ) ).toBool() )
114  {
115  return new QTableWidget( parent );
116  }
117  else if ( config( QStringLiteral( "UseCompleter" ) ).toBool() )
118  {
119  return new QgsFilterLineEdit( parent );
120  }
121  {
122  return new QComboBox( parent );
123  }
124 }
125 
127 {
128 
129  mComboBox = qobject_cast<QComboBox *>( editor );
130  mTableWidget = qobject_cast<QTableWidget *>( editor );
131  mLineEdit = qobject_cast<QLineEdit *>( editor );
132 
133  // Read current initial form values from the editor context
135 
136  if ( mComboBox )
137  {
138  connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
139  this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ), Qt::UniqueConnection );
140  }
141  else if ( mTableWidget )
142  {
143  mTableWidget->horizontalHeader()->setResizeMode( QHeaderView::Stretch );
144  mTableWidget->horizontalHeader()->setVisible( false );
145  mTableWidget->verticalHeader()->setResizeMode( QHeaderView::Stretch );
146  mTableWidget->verticalHeader()->setVisible( false );
147  mTableWidget->setShowGrid( false );
148  mTableWidget->setEditTriggers( QAbstractItemView::NoEditTriggers );
149  mTableWidget->setSelectionMode( QAbstractItemView::NoSelection );
150  connect( mTableWidget, &QTableWidget::itemChanged, this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ), Qt::UniqueConnection );
151  }
152  else if ( mLineEdit )
153  {
154  connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value ) { emit valueChanged( value ); }, Qt::UniqueConnection );
155  }
156 }
157 
159 {
160  return mTableWidget || mLineEdit || mComboBox;
161 }
162 
164 {
165  if ( mTableWidget )
166  {
167  QStringList checkList;
168 
169  if ( layer()->fields().at( fieldIdx() ).type() == QVariant::Map )
170  {
171  //because of json it's stored as QVariantList
172  checkList = value.toStringList();
173  }
174  else
175  {
177  }
178 
179  QTableWidgetItem *lastChangedItem = nullptr;
180 
181  const int nofColumns = columnCount();
182 
183  // This block is needed because item->setCheckState triggers dataChanged gets back to value()
184  // and iterate over all items again! This can be extremely slow on large items sets.
185  for ( int j = 0; j < mTableWidget->rowCount(); j++ )
186  {
187  auto signalBlockedTableWidget = whileBlocking( mTableWidget );
188  Q_UNUSED( signalBlockedTableWidget )
189 
190  for ( int i = 0; i < nofColumns; ++i )
191  {
192  QTableWidgetItem *item = mTableWidget->item( j, i );
193  if ( item )
194  {
195  item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
196  //re-set enabled state because it's lost after reloading items
197  item->setFlags( mEnabled ? item->flags() | Qt::ItemIsEnabled : item->flags() & ~Qt::ItemIsEnabled );
198  lastChangedItem = item;
199  }
200  }
201  }
202  // let's trigger the signal now, once and for all
203  if ( lastChangedItem )
204  lastChangedItem->setCheckState( checkList.contains( lastChangedItem->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
205 
206  }
207  else if ( mComboBox )
208  {
209  // findData fails to tell a 0 from a NULL
210  // See: "Value relation, value 0 = NULL" - https://issues.qgis.org/issues/19981
211  int idx = -1; // default to not found
212  for ( int i = 0; i < mComboBox->count(); i++ )
213  {
214  QVariant v( mComboBox->itemData( i ) );
215  if ( qgsVariantEqual( v, value ) )
216  {
217  idx = i;
218  break;
219  }
220  }
221  mComboBox->setCurrentIndex( idx );
222  }
223  else if ( mLineEdit )
224  {
225  for ( const QgsValueRelationFieldFormatter::ValueRelationItem &i : qgis::as_const( mCache ) )
226  {
227  if ( i.key == value )
228  {
229  mLineEdit->setText( i.value );
230  break;
231  }
232  }
233  }
234 }
235 
236 void QgsValueRelationWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
237 {
238 
239  // Do nothing if the value has not changed
240  if ( attributeChanged )
241  {
242  QVariant oldValue( value( ) );
243  setFormFeatureAttribute( attribute, newValue );
244  // Update combos if the value used in the filter expression has changed
246  && QgsValueRelationFieldFormatter::expressionFormAttributes( mExpression ).contains( attribute ) )
247  {
248  populate();
249  // Restore value
250  setValue( value( ) );
251  // If the value has changed as a result of another widget's value change,
252  // we need to emit the signal to make sure other dependent widgets are
253  // updated.
254  if ( oldValue != value() && fieldIdx() < formFeature().fields().count() )
255  {
256  QString attributeName( formFeature().fields().names().at( fieldIdx() ) );
257  setFormFeatureAttribute( attributeName, value( ) );
258  emitValueChanged( );
259  }
260  }
261  }
262 }
263 
264 
266 {
267  setFormFeature( feature );
268  whileBlocking( this )->populate();
269  whileBlocking( this )->setValue( feature.attribute( fieldIdx() ) );
270  // A bit of logic to set the default value if AllowNull is false and this is a new feature
271  // Note that this needs to be here after the cache has been created/updated by populate()
272  // and signals unblocked (we want this to propagate to the feature itself)
273  if ( formFeature().isValid()
274  && ! formFeature().attribute( fieldIdx() ).isValid()
275  && ! mCache.empty()
276  && ! config( QStringLiteral( "AllowNull" ) ).toBool( ) )
277  {
278  // This is deferred because at the time the feature is set in one widget it is not
279  // set in the next, which is typically the "down" in a drill-down
280  QTimer::singleShot( 0, this, [ this ]
281  {
282  setValue( mCache.at( 0 ).key );
283  } );
284  }
285 }
286 
287 int QgsValueRelationWidgetWrapper::columnCount() const
288 {
289  return std::max( 1, config( QStringLiteral( "NofColumns" ) ).toInt() );
290 }
291 
292 void QgsValueRelationWidgetWrapper::populate( )
293 {
294  // Initialize, note that signals are blocked, to avoid double signals on new features
296  {
298  }
299  else if ( mCache.empty() )
300  {
302  }
303 
304  if ( mComboBox )
305  {
306  mComboBox->clear();
307  if ( config( QStringLiteral( "AllowNull" ) ).toBool( ) )
308  {
309  whileBlocking( mComboBox )->addItem( tr( "(no selection)" ), QVariant( field().type( ) ) );
310  }
311 
312  for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : qgis::as_const( mCache ) )
313  {
314  whileBlocking( mComboBox )->addItem( element.value, element.key );
315  }
316  }
317  else if ( mTableWidget )
318  {
319  const int nofColumns = columnCount();
320 
321  if ( ! mCache.empty() )
322  {
323  mTableWidget->setRowCount( ( mCache.size() + nofColumns - 1 ) / nofColumns );
324  }
325  else
326  mTableWidget->setRowCount( 1 );
327  mTableWidget->setColumnCount( nofColumns );
328 
329  whileBlocking( mTableWidget )->clear();
330  int row = 0;
331  int column = 0;
332  for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : qgis::as_const( mCache ) )
333  {
334  if ( column == nofColumns )
335  {
336  row++;
337  column = 0;
338  }
339  QTableWidgetItem *item = nullptr;
340  item = new QTableWidgetItem( element.value );
341  item->setData( Qt::UserRole, element.key );
342  whileBlocking( mTableWidget )->setItem( row, column, item );
343  column++;
344  }
345  }
346  else if ( mLineEdit )
347  {
348  QStringList values;
349  values.reserve( mCache.size() );
350  for ( const QgsValueRelationFieldFormatter::ValueRelationItem &i : qgis::as_const( mCache ) )
351  {
352  values << i.value;
353  }
354  QStringListModel *m = new QStringListModel( values, mLineEdit );
355  QCompleter *completer = new QCompleter( m, mLineEdit );
356  completer->setCaseSensitivity( Qt::CaseInsensitive );
357  mLineEdit->setCompleter( completer );
358  }
359 }
360 
362 {
363  const int nofColumns = columnCount();
364 
365  if ( mTableWidget )
366  {
367  for ( int j = 0; j < mTableWidget->rowCount(); j++ )
368  {
369  for ( int i = 0; i < nofColumns; ++i )
370  {
371  whileBlocking( mTableWidget )->item( j, i )->setCheckState( Qt::PartiallyChecked );
372  }
373  }
374  }
375  else if ( mComboBox )
376  {
377  whileBlocking( mComboBox )->setCurrentIndex( -1 );
378  }
379  else if ( mLineEdit )
380  {
381  whileBlocking( mLineEdit )->clear();
382  }
383 }
384 
386 {
387  if ( mEnabled == enabled )
388  return;
389 
390  mEnabled = enabled;
391 
392  if ( mTableWidget )
393  {
394  auto signalBlockedTableWidget = whileBlocking( mTableWidget );
395  Q_UNUSED( signalBlockedTableWidget )
396 
397  for ( int j = 0; j < mTableWidget->rowCount(); j++ )
398  {
399  for ( int i = 0; i < mTableWidget->columnCount(); ++i )
400  {
401  QTableWidgetItem *item = mTableWidget->item( j, i );
402  if ( item )
403  {
404  item->setFlags( enabled ? item->flags() | Qt::ItemIsEnabled : item->flags() & ~Qt::ItemIsEnabled );
405  }
406  }
407  }
408  }
409  else
411 }
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:183
void emitValueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
void setFeature(const QgsFeature &feature) override
Will be called when the feature changes.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
Manages an editor widget Widget and wrapper share the same parent.
void setFormFeature(const QgsFeature &feature)
Set the feature currently being edited to feature.
void setValue(const QVariant &value) override
void setEnabled(bool enabled) override
Is used to enable or disable the edit functionality of the managed widget.
QVariantMap config() const
Returns the whole config.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
bool valid() const override
Returns true if the widget has been properly initialized.
void widgetValueChanged(const QString &attribute, const QVariant &newValue, bool attributeChanged)
Will be called when a value in the current edited form or table row changes.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
QgsFeature formFeature() const
The feature currently being edited, in its current state.
bool setFormFeatureAttribute(const QString &attributeName, const QVariant &attributeValue)
Update the feature currently being edited by changing its attribute attributeName to attributeValue...
void widgetValueChanged(const QString &attribute, const QVariant &value, bool attributeChanged)
Notifies about changes of attributes.
QVariant createCache(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const override
Create a cache for a given field.
static QSet< QString > expressionFormAttributes(const QString &expression)
Returns a list of attributes required by the form context expression.
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
void setEnabled(bool enabled) override
Is used to enable or disable the edit functionality of the managed widget.
QgsValueRelationWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsValueRelationWidgetWrapper.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:212
void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
static QStringList valueToStringList(const QVariant &value)
Utility to convert an array or a string representation of an array value to a string list...
static bool expressionRequiresFormScope(const QString &expression)
Check if the expression requires a form scope (i.e.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
int fieldIdx() const
Access the field index.
bool qgsVariantEqual(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether they are equal, NULL values are treated as equal...
Definition: qgis.cpp:289
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
QVariant value() const override
Will be used to access the widget&#39;s value.