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