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