QGIS API Documentation  2.17.0-Master (8784312)
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 "qgsfield.h"
20 #include "qgsmaplayerregistry.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsfilterlineedit.h"
24 
25 #include <QStringListModel>
26 #include <QCompleter>
27 
30 {
31  return qgsVariantLessThan( p1.first, p2.first );
32 }
33 
36 {
37  return qgsVariantLessThan( p1.second, p2.second );
38 }
39 
41  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
42  , mComboBox( nullptr )
43  , mListWidget( nullptr )
44  , mLineEdit( nullptr )
45  , mLayer( nullptr )
46 {
47 }
48 
49 
51 {
52  QVariant v;
53 
54  if ( mComboBox )
55  {
56  int cbxIdx = mComboBox->currentIndex();
57  if ( cbxIdx > -1 )
58  {
59  v = mComboBox->itemData( mComboBox->currentIndex() );
60  }
61  }
62 
63  if ( mListWidget )
64  {
65  QStringList selection;
66  for ( int i = 0; i < mListWidget->count(); ++i )
67  {
68  QListWidgetItem* item = mListWidget->item( i );
69  if ( item->checkState() == Qt::Checked )
70  selection << item->data( Qt::UserRole ).toString();
71  }
72 
73  v = selection.join( "," ).prepend( '{' ).append( '}' );
74  }
75 
76  if ( mLineEdit )
77  {
78  Q_FOREACH ( const ValueRelationItem& i , mCache )
79  {
80  if ( i.second == mLineEdit->text() )
81  {
82  v = i.first;
83  break;
84  }
85  }
86  }
87 
88  return v;
89 }
90 
92 {
93  if ( config( "AllowMulti" ).toBool() )
94  {
95  return new QListWidget( parent );
96  }
97  else if ( config( "UseCompleter" ).toBool() )
98  {
99  return new QgsFilterLineEdit( parent );
100  }
101  {
102  return new QComboBox( parent );
103  }
104 }
105 
107 {
108  mCache = createCache( config() );
109 
110  mComboBox = qobject_cast<QComboBox*>( editor );
111  mListWidget = qobject_cast<QListWidget*>( editor );
112  mLineEdit = qobject_cast<QLineEdit*>( editor );
113 
114  if ( mComboBox )
115  {
116  if ( config( "AllowNull" ).toBool() )
117  {
118  mComboBox->addItem( tr( "(no selection)" ), QVariant( field().type() ) );
119  }
120 
121  Q_FOREACH ( const ValueRelationItem& element, mCache )
122  {
123  mComboBox->addItem( element.second, element.first );
124  }
125 
126  connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( valueChanged() ) );
127  }
128  else if ( mListWidget )
129  {
130  Q_FOREACH ( const ValueRelationItem& element, mCache )
131  {
132  QListWidgetItem *item;
133  item = new QListWidgetItem( element.second );
134  item->setData( Qt::UserRole, element.first );
135 
136  mListWidget->addItem( item );
137  }
138  connect( mListWidget, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( valueChanged() ) );
139  }
140  else if ( mLineEdit )
141  {
142  QStringList values;
143  Q_FOREACH ( const ValueRelationItem& i, mCache )
144  {
145  values << i.second;
146  }
147 
148  QStringListModel* m = new QStringListModel( values, mLineEdit );
149  QCompleter* completer = new QCompleter( m, mLineEdit );
150  completer->setCaseSensitivity( Qt::CaseInsensitive );
151  mLineEdit->setCompleter( completer );
152  }
153 }
154 
156 {
157  return mListWidget || mLineEdit || mComboBox;
158 }
159 
161 {
162  if ( mListWidget )
163  {
164  QStringList checkList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );
165 
166  for ( int i = 0; i < mListWidget->count(); ++i )
167  {
168  QListWidgetItem* item = mListWidget->item( i );
169  item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
170  }
171  }
172  else if ( mComboBox )
173  {
174  mComboBox->setCurrentIndex( mComboBox->findData( value ) );
175  }
176  else if ( mLineEdit )
177  {
178  Q_FOREACH ( ValueRelationItem i, mCache )
179  {
180  if ( i.first == value )
181  {
182  mLineEdit->setText( i.second );
183  break;
184  }
185  }
186  }
187 }
188 
189 
191 {
192  ValueRelationCache cache;
193 
194  QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( config.value( "Layer" ).toString() ) );
195 
196  if ( !layer )
197  return cache;
198 
199  int ki = layer->fieldNameIndex( config.value( "Key" ).toString() );
200  int vi = layer->fieldNameIndex( config.value( "Value" ).toString() );
201 
202  QgsFeatureRequest request;
203 
205  request.setSubsetOfAttributes( QgsAttributeList() << ki << vi );
206  if ( !config.value( "FilterExpression" ).toString().isEmpty() )
207  {
208  request.setFilterExpression( config.value( "FilterExpression" ).toString() );
209  }
210 
211  QgsFeatureIterator fit = layer->getFeatures( request );
212 
213  QgsFeature f;
214  while ( fit.nextFeature( f ) )
215  {
216  cache.append( ValueRelationItem( f.attribute( ki ), f.attribute( vi ).toString() ) );
217  }
218 
219  if ( config.value( "OrderByValue" ).toBool() )
220  {
221  qSort( cache.begin(), cache.end(), orderByValueLessThan );
222  }
223  else
224  {
225  qSort( cache.begin(), cache.end(), orderByKeyLessThan );
226  }
227 
228  return cache;
229 }
230 
232 {
233  if ( mListWidget )
234  {
235  mListWidget->blockSignals( true );
236  for ( int i = 0; i < mListWidget->count(); ++i )
237  {
238  mListWidget->item( i )->setCheckState( Qt::PartiallyChecked );
239  }
240  mListWidget->blockSignals( false );
241  }
242  else if ( mComboBox )
243  {
244  whileBlocking( mComboBox )->setCurrentIndex( -1 );
245  }
246  else if ( mLineEdit )
247  {
248  whileBlocking( mLineEdit )->clear();
249  }
250 }
Wrapper for iterator of features from vector data provider or vector layer.
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
QString & append(QChar ch)
int fieldIdx() const
Access the field index.
Qt::CheckState checkState() const
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
void valueChanged()
Will call the value() method to determine the emitted value.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
void append(const T &value)
iterator begin()
Manages an editor widget Widget and wrapper share the same parent.
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QString & prepend(QChar ch)
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
QgsField field() const
Access the field.
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
void setValue(const QVariant &value) override
QString join(const QString &separator) const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
QString & remove(int position, int n)
bool valid() const override
Return true if the widget has been properly initialized.
QString tr(const char *sourceText, const char *disambiguation, int n)
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:269
void addItem(const QString &text, const QVariant &userData)
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
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)...
virtual QVariant data(int role) const
QList< int > QgsAttributeList
Lineedit with builtin clear button.
QgsFeatureRequest & setFlags(const QgsFeatureRequest::Flags &flags)
Set flags that affect how features will be fetched.
void setCheckState(Qt::CheckState state)
QListWidgetItem * item(int row) const
QVariant itemData(int index, int role) const
bool blockSignals(bool block)
virtual void setData(int role, const QVariant &value)
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:333
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:271
int findData(const QVariant &data, int role, QFlags< Qt::MatchFlag > flags) const
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
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.
bool nextFeature(QgsFeature &f)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
static bool orderByKeyLessThan(const QgsValueRelationWidgetWrapper::ValueRelationItem &p1, const QgsValueRelationWidgetWrapper::ValueRelationItem &p2)
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
QString toString() const
iterator end()
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)