/
qgsdefaultsearchwidgetwrapper.cpp
113 lines (99 loc) · 3.58 KB
/
qgsdefaultsearchwidgetwrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/***************************************************************************
qgsdefaultsearchwidgettwrapper.cpp
--------------------------------------
Date : 31.5.2015
Copyright : (C) 2015 Karolina Alexiou (carolinux)
Email : carolinegr at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsdefaultsearchwidgetwrapper.h"
#include "qgsfield.h"
#include "qgsfieldvalidator.h"
#include <QSettings>
#include <QHBoxLayout>
QgsDefaultSearchWidgetWrapper::QgsDefaultSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent )
: QgsSearchWidgetWrapper( vl, fieldIdx, parent )
, mLineEdit( NULL )
, mCheckbox( NULL )
, mContainer( NULL )
, mCaseString( QString( "LIKE" ) )
{
}
QString QgsDefaultSearchWidgetWrapper::expression()
{
return mExpression;
}
void QgsDefaultSearchWidgetWrapper::setCaseString( Qt::CheckState caseSensitiveCheckState )
{
if ( caseSensitiveCheckState == Qt::Checked )
{
mCaseString = "LIKE";
}
else
{
mCaseString = "ILIKE";
}
// need to update also the line edit
setExpression( mLineEdit->text() );
}
void QgsDefaultSearchWidgetWrapper::setExpression( QString exp )
{
QVariant::Type fldType = layer()->fields()[mFieldIdx].type();
bool numeric = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong );
QSettings settings;
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
QString fieldName = layer()->fields()[mFieldIdx].name();
QString str;
if ( exp == nullValue )
{
str = QString( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
}
else
{
str = QString( "%1 %2 '%3'" )
.arg( QgsExpression::quotedColumnRef( fieldName ) )
.arg( numeric ? "=" : mCaseString )
.arg( numeric
? exp.replace( "'", "''" )
:
"%" + exp.replace( "'", "''" ) + "%" ); // escape quotes
}
mExpression = str;
emit expressionChanged( mExpression );
}
QWidget* QgsDefaultSearchWidgetWrapper::createWidget( QWidget* parent )
{
return new QWidget( parent );
}
bool QgsDefaultSearchWidgetWrapper::applyDirectly()
{
return false;
}
void QgsDefaultSearchWidgetWrapper::initWidget( QWidget* widget )
{
mContainer = widget;
mContainer->setLayout( new QHBoxLayout() );
mLineEdit = new QgsFilterLineEdit();
mCheckbox = new QCheckBox( "Case sensitive" );
mContainer->layout()->addWidget( mLineEdit );
mContainer->layout()->addWidget( mCheckbox );
connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( setExpression( QString ) ) );
connect( mLineEdit, SIGNAL( returnPressed() ), this, SLOT( filterChanged() ) );
connect( mCheckbox, SIGNAL( stateChanged( int ) ), this, SLOT( setCaseString( int ) ) );
mCheckbox->setChecked( Qt::Unchecked );
mCaseString = "ILIKE";
}
bool QgsDefaultSearchWidgetWrapper::valid()
{
return true;
}
void QgsDefaultSearchWidgetWrapper::filterChanged()
{
emit expressionChanged( mExpression );
}