QGIS API Documentation  3.6.0-Noosa (5873452)
qgsvaluemapsearchwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdefaultsearchwidgettwrapper.cpp
3  --------------------------------------
4  Date : 31.5.2015
5  Copyright : (C) 2015 Karolina Alexiou (carolinux)
6  Email : carolinegr 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 #include "qgstexteditconfigdlg.h"
18 #include "qgsvaluemapconfigdlg.h"
20 
21 #include "qgsfields.h"
22 #include "qgsfieldvalidator.h"
23 
24 #include <QSettings>
25 #include <QSizePolicy>
26 
28  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
29 
30 {
31 }
32 
34 {
35  return new QComboBox( parent );
36 }
37 
38 void QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged( int idx )
39 {
40  if ( mComboBox )
41  {
42  if ( idx == 0 )
43  {
45  emit valueCleared();
46  }
47  else
48  {
49  setExpression( mComboBox->itemData( idx ).toString() );
50  emit valueChanged();
51  }
53  }
54 }
55 
57 {
58  return true;
59 }
60 
62 {
63  return mExpression;
64 }
65 
67 {
68  return true;
69 }
70 
71 QgsSearchWidgetWrapper::FilterFlags QgsValueMapSearchWidgetWrapper::supportedFlags() const
72 {
73  return EqualTo | NotEqualTo | IsNull | IsNotNull;
74 }
75 
76 QgsSearchWidgetWrapper::FilterFlags QgsValueMapSearchWidgetWrapper::defaultFlags() const
77 {
78  return EqualTo;
79 }
80 
81 QString QgsValueMapSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
82 {
83  //clear any unsupported flags
84  flags &= supportedFlags();
85 
86  QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type();
87  QString fieldName = createFieldIdentifier();
88 
89  if ( flags & IsNull )
90  return fieldName + " IS NULL";
91  if ( flags & IsNotNull )
92  return fieldName + " IS NOT NULL";
93 
94  //if deselect value, always pass
95  if ( mComboBox->currentIndex() == 0 )
96  return QString();
97 
98  QString currentKey = mComboBox->currentData().toString();
99 
100  switch ( fldType )
101  {
102  case QVariant::Int:
103  case QVariant::UInt:
104  case QVariant::Double:
105  case QVariant::LongLong:
106  case QVariant::ULongLong:
107  {
108  if ( flags & EqualTo )
109  return fieldName + '=' + currentKey;
110  else if ( flags & NotEqualTo )
111  return fieldName + "<>" + currentKey;
112  break;
113  }
114 
115  default:
116  {
117  if ( flags & EqualTo )
118  return fieldName + "='" + currentKey + '\'';
119  else if ( flags & NotEqualTo )
120  return fieldName + "<>'" + currentKey + '\'';
121  break;
122  }
123  }
124 
125  return QString();
126 }
127 
129 {
130  mComboBox->setCurrentIndex( 0 );
131 }
132 
134 {
135  mComboBox->setEnabled( enabled );
136 }
137 
139 {
140  mComboBox = qobject_cast<QComboBox *>( editor );
141 
142  if ( mComboBox )
143  {
144  QgsValueMapConfigDlg::populateComboBox( mComboBox, config(), true );
145  mComboBox->insertItem( 0, tr( "Please select" ), QString() );
146 
147  connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged );
148  }
149 }
150 
152 {
153  QString exp = expression;
154  QString fieldName = layer()->fields().at( mFieldIdx ).name();
155  QString str;
156 
157  str = QStringLiteral( "%1 = '%2'" )
158  .arg( QgsExpression::quotedColumnRef( fieldName ),
159  exp.replace( '\'', QLatin1String( "''" ) ) );
160 
161  mExpression = str;
162 }
163 
QgsSearchWidgetWrapper::FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
Shows a search widget on a filter form.
QString expression() const override
Will be used to access the widget&#39;s value.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QString name
Definition: qgsfield.h:58
bool valid() const override
Returns true if the widget has been properly initialized.
Supports searching for non-null values.
void clearExpression()
clears the expression to search for all features
QVariantMap config() const
Returns the whole config.
QString createFieldIdentifier() const
Gets a field name or expression to use as field comparison.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
Supports searching for null values.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
void valueChanged()
Emitted when a user changes the value of the search widget.
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes...
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsValueMapSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsValueMapSearchWidgetWrapper.
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state...
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
void setExpression(const QString &exp) override
Represents a vector layer which manages a vector based data sets.
QVariant::Type type
Definition: qgsfield.h:56
static void populateComboBox(QComboBox *comboBox, const QVariantMap &configuration, bool skipNull)
Populates a comboBox with the appropriate entries based on a value map configuration.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.