QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  const QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type();
87  const 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  const 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 
151 void QgsValueMapSearchWidgetWrapper::setExpression( const QString &expression )
152 {
153  QString exp = expression;
154  const 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 
qgsfields.h
QgsSearchWidgetWrapper::expressionChanged
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QgsSearchWidgetWrapper::valueChanged
void valueChanged()
Emitted when a user changes the value of the search widget.
QgsSearchWidgetWrapper::NotEqualTo
@ NotEqualTo
Supports not equal to.
Definition: qgssearchwidgetwrapper.h:97
QgsSearchWidgetWrapper
Shows a search widget on a filter form.
Definition: qgssearchwidgetwrapper.h:85
qgstexteditconfigdlg.h
QgsValueMapSearchWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgsvaluemapsearchwidgetwrapper.cpp:66
QgsValueMapSearchWidgetWrapper::QgsValueMapSearchWidgetWrapper
QgsValueMapSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsValueMapSearchWidgetWrapper.
Definition: qgsvaluemapsearchwidgetwrapper.cpp:27
QgsValueMapSearchWidgetWrapper::supportedFlags
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
Definition: qgsvaluemapsearchwidgetwrapper.cpp:71
QgsField::name
QString name
Definition: qgsfield.h:60
QgsSearchWidgetWrapper::valueCleared
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state.
QgsValueMapSearchWidgetWrapper::createExpression
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
Definition: qgsvaluemapsearchwidgetwrapper.cpp:81
QgsValueMapSearchWidgetWrapper::clearWidget
void clearWidget() override
Definition: qgsvaluemapsearchwidgetwrapper.cpp:128
QgsValueMapSearchWidgetWrapper::setEnabled
void setEnabled(bool enabled) override
Definition: qgsvaluemapsearchwidgetwrapper.cpp:133
QgsValueMapConfigDlg::populateComboBox
static void populateComboBox(QComboBox *comboBox, const QVariantMap &configuration, bool skipNull)
Populates a comboBox with the appropriate entries based on a value map configuration.
Definition: qgsvaluemapconfigdlg.cpp:188
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsWidgetWrapper::layer
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
Definition: qgswidgetwrapper.cpp:92
QgsSearchWidgetWrapper::mFieldIdx
int mFieldIdx
Definition: qgssearchwidgetwrapper.h:266
QgsValueMapSearchWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgsvaluemapsearchwidgetwrapper.cpp:33
QgsValueMapSearchWidgetWrapper::setExpression
void setExpression(const QString &exp) override
Definition: qgsvaluemapsearchwidgetwrapper.cpp:151
qgsvaluemapfieldformatter.h
QgsSearchWidgetWrapper::mExpression
QString mExpression
Definition: qgssearchwidgetwrapper.h:265
QgsSearchWidgetWrapper::IsNull
@ IsNull
Supports searching for null values.
Definition: qgssearchwidgetwrapper.h:106
qgsfieldvalidator.h
QgsWidgetWrapper::config
QVariantMap config() const
Returns the whole config.
Definition: qgswidgetwrapper.cpp:82
str
#define str(x)
Definition: qgis.cpp:37
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsExpression::quotedColumnRef
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
Definition: qgsexpression.cpp:68
QgsValueMapSearchWidgetWrapper::defaultFlags
QgsSearchWidgetWrapper::FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
Definition: qgsvaluemapsearchwidgetwrapper.cpp:76
qgsvaluemapsearchwidgetwrapper.h
QgsSearchWidgetWrapper::createFieldIdentifier
QString createFieldIdentifier() const
Gets a field name or expression to use as field comparison.
Definition: qgssearchwidgetwrapper.cpp:104
QgsSearchWidgetWrapper::EqualTo
@ EqualTo
Supports equal to.
Definition: qgssearchwidgetwrapper.h:96
QgsFields::at
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
QgsSearchWidgetWrapper::clearExpression
void clearExpression()
clears the expression to search for all features
Definition: qgssearchwidgetwrapper.cpp:118
QgsValueMapSearchWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgsvaluemapsearchwidgetwrapper.cpp:138
QgsSearchWidgetWrapper::IsNotNull
@ IsNotNull
Supports searching for non-null values.
Definition: qgssearchwidgetwrapper.h:108
QgsValueMapSearchWidgetWrapper::expression
QString expression() const override
Will be used to access the widget's value.
Definition: qgsvaluemapsearchwidgetwrapper.cpp:61
qgsvaluemapconfigdlg.h
QgsField::type
QVariant::Type type
Definition: qgsfield.h:58
QgsValueMapSearchWidgetWrapper::applyDirectly
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes.
Definition: qgsvaluemapsearchwidgetwrapper.cpp:56