QGIS API Documentation  2.99.0-Master (37c43df)
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 "qgsmaplayerregistry.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsfilterlineedit.h"
24 #include "qgsfeatureiterator.h"
25 
26 #include <QStringListModel>
27 #include <QCompleter>
28 
31 {
32  return qgsVariantLessThan( p1.first, p2.first );
33 }
34 
37 {
38  return qgsVariantLessThan( p1.second, p2.second );
39 }
40 
42  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
43  , mComboBox( nullptr )
44  , mListWidget( nullptr )
45  , mLineEdit( nullptr )
46  , mLayer( nullptr )
47 {
48 }
49 
50 
52 {
53  QVariant v;
54 
55  if ( mComboBox )
56  {
57  int cbxIdx = mComboBox->currentIndex();
58  if ( cbxIdx > -1 )
59  {
60  v = mComboBox->currentData();
61  }
62  }
63 
64  if ( mListWidget )
65  {
66  QStringList selection;
67  for ( int i = 0; i < mListWidget->count(); ++i )
68  {
69  QListWidgetItem* item = mListWidget->item( i );
70  if ( item->checkState() == Qt::Checked )
71  selection << item->data( Qt::UserRole ).toString();
72  }
73 
74  v = selection.join( QStringLiteral( "," ) ).prepend( '{' ).append( '}' );
75  }
76 
77  if ( mLineEdit )
78  {
79  Q_FOREACH ( const ValueRelationItem& i , mCache )
80  {
81  if ( i.second == mLineEdit->text() )
82  {
83  v = i.first;
84  break;
85  }
86  }
87  }
88 
89  return v;
90 }
91 
93 {
94  if ( config( QStringLiteral( "AllowMulti" ) ).toBool() )
95  {
96  return new QListWidget( parent );
97  }
98  else if ( config( QStringLiteral( "UseCompleter" ) ).toBool() )
99  {
100  return new QgsFilterLineEdit( parent );
101  }
102  {
103  return new QComboBox( parent );
104  }
105 }
106 
108 {
109  mCache = createCache( config() );
110 
111  mComboBox = qobject_cast<QComboBox*>( editor );
112  mListWidget = qobject_cast<QListWidget*>( editor );
113  mLineEdit = qobject_cast<QLineEdit*>( editor );
114 
115  if ( mComboBox )
116  {
117  if ( config( QStringLiteral( "AllowNull" ) ).toBool() )
118  {
119  mComboBox->addItem( tr( "(no selection)" ), QVariant( field().type() ) );
120  }
121 
122  Q_FOREACH ( const ValueRelationItem& element, mCache )
123  {
124  mComboBox->addItem( element.second, element.first );
125  }
126 
127  connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( valueChanged() ) );
128  }
129  else if ( mListWidget )
130  {
131  Q_FOREACH ( const ValueRelationItem& element, mCache )
132  {
133  QListWidgetItem *item;
134  item = new QListWidgetItem( element.second );
135  item->setData( Qt::UserRole, element.first );
136 
137  mListWidget->addItem( item );
138  }
139  connect( mListWidget, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( valueChanged() ) );
140  }
141  else if ( mLineEdit )
142  {
143  QStringList values;
144  values.reserve( mCache.size() );
145  Q_FOREACH ( const ValueRelationItem& i, mCache )
146  {
147  values << i.second;
148  }
149 
150  QStringListModel* m = new QStringListModel( values, mLineEdit );
151  QCompleter* completer = new QCompleter( m, mLineEdit );
152  completer->setCaseSensitivity( Qt::CaseInsensitive );
153  mLineEdit->setCompleter( completer );
154  }
155 }
156 
158 {
159  return mListWidget || mLineEdit || mComboBox;
160 }
161 
163 {
164  if ( mListWidget )
165  {
166  QStringList checkList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );
167 
168  for ( int i = 0; i < mListWidget->count(); ++i )
169  {
170  QListWidgetItem* item = mListWidget->item( i );
171  item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
172  }
173  }
174  else if ( mComboBox )
175  {
176  mComboBox->setCurrentIndex( mComboBox->findData( value ) );
177  }
178  else if ( mLineEdit )
179  {
180  Q_FOREACH ( ValueRelationItem i, mCache )
181  {
182  if ( i.first == value )
183  {
184  mLineEdit->setText( i.second );
185  break;
186  }
187  }
188  }
189 }
190 
191 
193 {
194  ValueRelationCache cache;
195 
196  QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( config.value( QStringLiteral( "Layer" ) ).toString() ) );
197 
198  if ( !layer )
199  return cache;
200 
201  int ki = layer->fields().lookupField( config.value( QStringLiteral( "Key" ) ).toString() );
202  int vi = layer->fields().lookupField( config.value( QStringLiteral( "Value" ) ).toString() );
203 
204  QgsFeatureRequest request;
205 
207  request.setSubsetOfAttributes( QgsAttributeList() << ki << vi );
208  if ( !config.value( QStringLiteral( "FilterExpression" ) ).toString().isEmpty() )
209  {
210  request.setFilterExpression( config.value( QStringLiteral( "FilterExpression" ) ).toString() );
211  }
212 
213  QgsFeatureIterator fit = layer->getFeatures( request );
214 
215  QgsFeature f;
216  while ( fit.nextFeature( f ) )
217  {
218  cache.append( ValueRelationItem( f.attribute( ki ), f.attribute( vi ).toString() ) );
219  }
220 
221  if ( config.value( QStringLiteral( "OrderByValue" ) ).toBool() )
222  {
223  qSort( cache.begin(), cache.end(), orderByValueLessThan );
224  }
225  else
226  {
227  qSort( cache.begin(), cache.end(), orderByKeyLessThan );
228  }
229 
230  return cache;
231 }
232 
234 {
235  if ( mListWidget )
236  {
237  mListWidget->blockSignals( true );
238  for ( int i = 0; i < mListWidget->count(); ++i )
239  {
240  mListWidget->item( i )->setCheckState( Qt::PartiallyChecked );
241  }
242  mListWidget->blockSignals( false );
243  }
244  else if ( mComboBox )
245  {
246  whileBlocking( mComboBox )->setCurrentIndex( -1 );
247  }
248  else if ( mLineEdit )
249  {
250  whileBlocking( mLineEdit )->clear();
251  }
252 }
int lookupField(const QString &fieldName) const
Look up field&#39;s index from the field name.
Definition: qgsfields.cpp:291
Wrapper for iterator of features from vector data provider or vector layer.
void valueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
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.
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
void setValue(const QVariant &value) override
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
bool valid() const override
Return true if the widget has been properly initialized.
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
Definition: qgis.cpp:140
QgsFields fields() const
Returns the list of fields of this layer.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
QPair< QVariant, QString > ValueRelationItem
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const
Query the layer for features specified in request.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
static ValueRelationCache createCache(const QgsEditorWidgetConfig &config)
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:176
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
static bool orderByValueLessThan(const QgsValueRelationWidgetWrapper::ValueRelationItem &p1, const QgsValueRelationWidgetWrapper::ValueRelationItem &p2)
QgsEditorWidgetConfig config() const
Returns the whole config.
int fieldIdx() const
Access the field index.
bool nextFeature(QgsFeature &f)
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
Geometry is not required. It may still be returned if e.g. required for a filter condition.
static bool orderByKeyLessThan(const QgsValueRelationWidgetWrapper::ValueRelationItem &p1, const QgsValueRelationWidgetWrapper::ValueRelationItem &p2)
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:277
QVector< ValueRelationItem > ValueRelationCache
QVariant value() const override
Will be used to access the widget&#39;s value.
QgsValueRelationWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Set flags that affect how features will be fetched.