QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgscheckboxsearchwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscheckboxsearchwidgetwrapper.cpp
3 ---------------------------------
4 Date : 2016-05-23
5 Copyright : (C) 2016 Nyall Dawson
6 Email : nyall dot dawson 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
19#include "qgsfields.h"
20#include "qgsvectorlayer.h"
21
22#include <QCheckBox>
23#include <QSettings>
24#include <QString>
25
26#include "moc_qgscheckboxsearchwidgetwrapper.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
32
33{}
34
36{
37 return true;
38}
39
44
46{
47 QVariant v;
48
49 const QMetaType::Type fieldType = layer()->fields().at( mFieldIdx ).type();
50
51 if ( mCheckBox )
52 {
53 if ( fieldType == QMetaType::Type::Bool )
54 {
55 v = mCheckBox->isChecked();
56 }
57 else
58 {
59 v = mCheckBox->isChecked() ? config( u"CheckedState"_s, true ) : config( u"UncheckedState"_s, false );
60 }
61 }
62
63 return v;
64}
65
70
75
77{
78 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
79 const QString fieldName = createFieldIdentifier();
80
81 //clear any unsupported flags
82 flags &= supportedFlags();
83 if ( flags & IsNull )
84 return fieldName + " IS NULL";
85
86 if ( flags & IsNotNull )
87 return fieldName + " IS NOT NULL";
88
89 const QVariant v = value();
90 if ( !v.isValid() )
91 return QString();
92
93 switch ( fldType )
94 {
95 case QMetaType::Type::Int:
96 case QMetaType::Type::UInt:
97 case QMetaType::Type::Double:
98 case QMetaType::Type::LongLong:
99 case QMetaType::Type::ULongLong:
100 case QMetaType::Type::Bool:
101 {
102 if ( flags & EqualTo )
103 return fieldName + '=' + v.toString();
104 else if ( flags & NotEqualTo )
105 return fieldName + "<>" + v.toString();
106 break;
107 }
108
109 default:
110 {
111 if ( flags & EqualTo )
112 return fieldName + "='" + v.toString() + '\'';
113 else if ( flags & NotEqualTo )
114 return fieldName + "<>'" + v.toString() + '\'';
115 break;
116 }
117 }
118
119 return QString();
120}
121
123{
124 if ( mCheckBox )
125 {
126 whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked );
127 }
128}
129
131{
132 if ( mCheckBox )
133 {
134 mCheckBox->setEnabled( enabled );
135 }
136}
137
139{
140 return true;
141}
142
144{
145 QString exp = expression;
146 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
147 const QMetaType::Type fieldType = layer()->fields().at( mFieldIdx ).type();
148
149 QString str;
150 switch ( fieldType )
151 {
152 case QMetaType::Type::Bool:
153 case QMetaType::Type::Int:
154 case QMetaType::Type::UInt:
155 case QMetaType::Type::LongLong:
156 case QMetaType::Type::ULongLong:
157 case QMetaType::Type::Double:
158 str = u"%1 = %2"_s.arg( QgsExpression::quotedColumnRef( fieldName ), exp );
159 break;
160
161 default:
162 str = u"%1 = '%2'"_s.arg( QgsExpression::quotedColumnRef( fieldName ), exp.replace( '\'', "''"_L1 ) );
163 break;
164 }
165 mExpression = str;
166}
167
168void QgsCheckboxSearchWidgetWrapper::stateChanged( int )
169{
170 if ( mCheckBox )
171 {
172 mCheckBox->setTristate( false );
173
174 QString exp;
175 const QVariant currentValue = value();
176 if ( currentValue.userType() == QMetaType::Type::Bool )
177 {
178 exp = currentValue.toBool() ? u"TRUE"_s : u"FALSE"_s;
179 }
180 else
181 {
182 exp = currentValue.toString();
183 }
184
185 setExpression( exp );
186 emit valueChanged();
188 }
189}
190
192{
193 QCheckBox *c = new QCheckBox( parent );
194 c->setChecked( Qt::PartiallyChecked );
195 return c;
196}
197
199{
200 mCheckBox = qobject_cast<QCheckBox *>( editor );
201
202 if ( mCheckBox )
203 {
204 mCheckBox->setChecked( Qt::PartiallyChecked );
205 connect( mCheckBox, &QCheckBox::stateChanged, this, &QgsCheckboxSearchWidgetWrapper::stateChanged );
206 }
207}
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsSearchWidgetWrapper::FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes.
void setExpression(const QString &expression) override
QString expression() const override
Will be used to access the widget's value.
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
Creates a filter expression based on the current state of the search widget and the specified filter ...
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
bool valid() const override
Returns true if the widget has been properly initialized.
QgsCheckboxSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsCheckboxSearchWidgetWrapper.
QVariant value() const
Returns a variant representing the current state of the widget.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
QMetaType::Type type
Definition qgsfield.h:63
QString name
Definition qgsfield.h:65
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
@ IsNull
Supports searching for null values.
@ IsNotNull
Supports searching for non-null values.
@ NotEqualTo
Supports not equal to.
void valueChanged()
Emitted when a user changes the value of the search widget.
QgsSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Create a new widget wrapper.
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QString createFieldIdentifier() const
Gets a field name or expression to use as field comparison.
QFlags< FilterFlag > FilterFlags
Represents a vector layer which manages a vector based dataset.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QVariantMap config() const
Returns the whole config.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6880