QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
19 #include "qgsfield.h"
20 #include "qgsfieldvalidator.h"
21 
22 #include <QSettings>
23 #include <QSizePolicy>
24 
26  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
27  , mComboBox( nullptr )
28 {
29 }
30 
32 {
33  return new QComboBox( parent );
34 }
35 
36 void QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged( int idx )
37 {
38  if ( mComboBox )
39  {
40  if ( idx == 0 )
41  {
43  emit valueCleared();
44  }
45  else
46  {
47  setExpression( mComboBox->itemData( idx ).toString() );
48  emit valueChanged();
49  }
51  }
52 }
53 
55 {
56  return true;
57 }
58 
60 {
61  return mExpression;
62 }
63 
65 {
66  return true;
67 }
68 
69 QgsSearchWidgetWrapper::FilterFlags QgsValueMapSearchWidgetWrapper::supportedFlags() const
70 {
71  return EqualTo | NotEqualTo | IsNull | IsNotNull;
72 }
73 
74 QgsSearchWidgetWrapper::FilterFlags QgsValueMapSearchWidgetWrapper::defaultFlags() const
75 {
76  return EqualTo;
77 }
78 
79 QString QgsValueMapSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
80 {
81  //if deselect value, always pass
82  if ( mComboBox->currentIndex() == 0 )
83  return QString();
84 
85  //clear any unsupported flags
86  flags &= supportedFlags();
87 
88  QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type();
89  QString fieldName = QgsExpression::quotedColumnRef( layer()->fields().at( mFieldIdx ).name() );
90 
91  if ( flags & IsNull )
92  return fieldName + " IS NULL";
93  if ( flags & IsNotNull )
94  return fieldName + " IS NOT NULL";
95 
96  QString currentKey = mComboBox->itemData( mComboBox->currentIndex() ).toString();
97 
98  switch ( fldType )
99  {
100  case QVariant::Int:
101  case QVariant::UInt:
102  case QVariant::Double:
103  case QVariant::LongLong:
104  case QVariant::ULongLong:
105  {
106  if ( flags & EqualTo )
107  return fieldName + '=' + currentKey;
108  else if ( flags & NotEqualTo )
109  return fieldName + "<>" + currentKey;
110  break;
111  }
112 
113  default:
114  {
115  if ( flags & EqualTo )
116  return fieldName + "='" + currentKey + '\'';
117  else if ( flags & NotEqualTo )
118  return fieldName + "<>'" + currentKey + '\'';
119  break;
120  }
121  }
122 
123  return QString();
124 }
125 
127 {
128  mComboBox->setCurrentIndex( 0 );
129 }
130 
132 {
133  mComboBox->setEnabled( enabled );
134 }
135 
137 {
138  mComboBox = qobject_cast<QComboBox*>( editor );
139 
140  if ( mComboBox )
141  {
142  const QgsEditorWidgetConfig cfg = config();
143  QgsEditorWidgetConfig::ConstIterator it = cfg.constBegin();
144  mComboBox->addItem( tr( "Please select" ), "" );
145 
146  while ( it != cfg.constEnd() )
147  {
148  mComboBox->addItem( it.key(), it.value() );
149  ++it;
150  }
151  connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( comboBoxIndexChanged( int ) ) );
152  }
153 }
154 
156 {
157  QString fieldName = layer()->fields().at( mFieldIdx ).name();
158  QString str;
159 
160  str = QString( "%1 = '%2'" )
161  .arg( QgsExpression::quotedColumnRef( fieldName ),
162  exp.replace( '\'', "''" ) );
163 
164  mExpression = str;
165 }
166 
FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
Manages an editor widget Widget and wrapper share the same parent.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QString name
Definition: qgsfield.h:52
bool valid() const override
Return true if the widget has been properly initialized.
QString expression() override
Will be used to access the widget&#39;s value.
static QString toString(FilterFlag flag)
Returns a translated string representing a filter flag.
virtual void setEnabled(bool enabled) override
void clearExpression()
clears the expression to search for all features
QString tr(const char *sourceText, const char *disambiguation, int n)
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:422
QgsFields fields() const
Returns the list of fields of this layer.
void addItem(const QString &text, const QVariant &userData)
const char * name() const
void setEnabled(bool)
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
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...
FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QVariant itemData(int index, int role) const
QString & replace(int position, int n, QChar after)
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsValueMapSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state...
virtual QString createExpression(FilterFlags flags) const override
Creates a filter expression based on the current state of the search widget and the specified filter ...
QgsEditorWidgetConfig config() const
Returns the whole config.
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:97
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.