QGIS API Documentation  2.14.0-Essen
qgsdefaultsearchwidgetwrapper.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 
18 #include "qgsfield.h"
19 #include "qgsfieldvalidator.h"
20 
21 #include <QSettings>
22 #include <QHBoxLayout>
23 
25  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
26  , mLineEdit( nullptr )
27  , mCheckbox( nullptr )
28  , mContainer( nullptr )
29  , mCaseString( QString( "LIKE" ) )
30 {
31 }
32 
33 
35 {
36  return mExpression;
37 }
38 
39 void QgsDefaultSearchWidgetWrapper::setCaseString( int caseSensitiveCheckState )
40 {
41  if ( caseSensitiveCheckState == Qt::Checked )
42  {
43  mCaseString = "LIKE";
44  }
45  else
46  {
47  mCaseString = "ILIKE";
48  }
49  // need to update also the line edit
50  setExpression( mLineEdit->text() );
51 }
52 
54 {
55  QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type();
56  bool numeric = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong );
57 
58  QSettings settings;
59  QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
60  QString fieldName = layer()->fields().at( mFieldIdx ).name();
61  QString str;
62  if ( exp == nullValue )
63  {
64  str = QString( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
65  }
66  else
67  {
68  str = QString( "%1 %2 '%3'" )
69  .arg( QgsExpression::quotedColumnRef( fieldName ),
70  numeric ? "=" : mCaseString,
71  numeric ?
72  exp.replace( '\'', "''" )
73  :
74  '%' + exp.replace( '\'', "''" ) + '%' ); // escape quotes
75  }
76  mExpression = str;
77 }
78 
80 {
81  return new QWidget( parent );
82 }
83 
85 {
86  return false;
87 }
88 
90 {
91  mContainer = widget;
92  mContainer->setLayout( new QHBoxLayout() );
93  mLineEdit = new QgsFilterLineEdit();
94  mCheckbox = new QCheckBox( "Case sensitive" );
95  mContainer->layout()->addWidget( mLineEdit );
96  mContainer->layout()->addWidget( mCheckbox );
97  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( setExpression( QString ) ) );
98  connect( mLineEdit, SIGNAL( returnPressed() ), this, SLOT( filterChanged() ) );
99  connect( mCheckbox, SIGNAL( stateChanged( int ) ), this, SLOT( setCaseString( int ) ) );
100  mCheckbox->setChecked( Qt::Unchecked );
101  mCaseString = "ILIKE";
102 }
103 
105 {
106  return true;
107 }
108 
109 void QgsDefaultSearchWidgetWrapper::filterChanged()
110 {
112 }
QLayout * layout() const
Manages an editor widget Widget and wrapper share the same parent.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QgsFields fields() const
Returns the list of fields of this layer.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void setLayout(QLayout *layout)
QString name() const
Gets the name of the field.
Definition: qgsfield.cpp:84
QString expression() override
Will be used to access the widget&#39;s value.
Lineedit with builtin clear button.
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:385
void addWidget(QWidget *w)
bool valid() const override
Return true if the widget has been properly initialized.
void setChecked(bool)
QString & replace(int position, int n, QChar after)
QVariant value(const QString &key, const QVariant &defaultValue) const
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes...
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
QgsDefaultSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
QWidget * widget()
Access the widget managed by this wrapper.
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.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:89