QGIS API Documentation 3.99.0-Master (357b655ed83)
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}
35
37{
38 return true;
39}
40
45
47{
48 QVariant v;
49
50 const QMetaType::Type fieldType = layer()->fields().at( mFieldIdx ).type();
51
52 if ( mCheckBox )
53 {
54 if ( fieldType == QMetaType::Type::Bool )
55 {
56 v = mCheckBox->isChecked();
57 }
58 else
59 {
60 v = mCheckBox->isChecked() ? config( u"CheckedState"_s, true ) : config( u"UncheckedState"_s, false );
61 }
62 }
63
64 return v;
65}
66
71
76
78{
79 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
80 const QString fieldName = createFieldIdentifier();
81
82 //clear any unsupported flags
83 flags &= supportedFlags();
84 if ( flags & IsNull )
85 return fieldName + " IS NULL";
86
87 if ( flags & IsNotNull )
88 return fieldName + " IS NOT NULL";
89
90 const QVariant v = value();
91 if ( !v.isValid() )
92 return QString();
93
94 switch ( fldType )
95 {
96 case QMetaType::Type::Int:
97 case QMetaType::Type::UInt:
98 case QMetaType::Type::Double:
99 case QMetaType::Type::LongLong:
100 case QMetaType::Type::ULongLong:
101 case QMetaType::Type::Bool:
102 {
103 if ( flags & EqualTo )
104 return fieldName + '=' + v.toString();
105 else if ( flags & NotEqualTo )
106 return fieldName + "<>" + v.toString();
107 break;
108 }
109
110 default:
111 {
112 if ( flags & EqualTo )
113 return fieldName + "='" + v.toString() + '\'';
114 else if ( flags & NotEqualTo )
115 return fieldName + "<>'" + v.toString() + '\'';
116 break;
117 }
118 }
119
120 return QString();
121}
122
124{
125 if ( mCheckBox )
126 {
127 whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked );
128 }
129}
130
132{
133 if ( mCheckBox )
134 {
135 mCheckBox->setEnabled( enabled );
136 }
137}
138
140{
141 return true;
142}
143
145{
146 QString exp = expression;
147 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
148 const QMetaType::Type fieldType = layer()->fields().at( mFieldIdx ).type();
149
150 QString str;
151 switch ( fieldType )
152 {
153 case QMetaType::Type::Bool:
154 case QMetaType::Type::Int:
155 case QMetaType::Type::UInt:
156 case QMetaType::Type::LongLong:
157 case QMetaType::Type::ULongLong:
158 case QMetaType::Type::Double:
159 str = u"%1 = %2"_s.arg( QgsExpression::quotedColumnRef( fieldName ), exp );
160 break;
161
162 default:
163 str = u"%1 = '%2'"_s
164 .arg( QgsExpression::quotedColumnRef( fieldName ), exp.replace( '\'', "''"_L1 ) );
165 break;
166 }
167 mExpression = str;
168}
169
170void QgsCheckboxSearchWidgetWrapper::stateChanged( int )
171{
172 if ( mCheckBox )
173 {
174 mCheckBox->setTristate( false );
175
176 QString exp;
177 const QVariant currentValue = value();
178 if ( currentValue.userType() == QMetaType::Type::Bool )
179 {
180 exp = currentValue.toBool() ? u"TRUE"_s : u"FALSE"_s;
181 }
182 else
183 {
184 exp = currentValue.toString();
185 }
186
187 setExpression( exp );
188 emit valueChanged();
190 }
191}
192
194{
195 QCheckBox *c = new QCheckBox( parent );
196 c->setChecked( Qt::PartiallyChecked );
197 return c;
198}
199
201{
202 mCheckBox = qobject_cast<QCheckBox *>( editor );
203
204 if ( mCheckBox )
205 {
206 mCheckBox->setChecked( Qt::PartiallyChecked );
207 connect( mCheckBox, &QCheckBox::stateChanged, this, &QgsCheckboxSearchWidgetWrapper::stateChanged );
208 }
209}
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:6839