QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
18 #include "qgsfields.h"
20 #include "qgsvectorlayer.h"
21 
22 #include <QSettings>
23 #include <QCheckBox>
24 
26  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
27 
28 {
29 }
30 
32 {
33  return true;
34 }
35 
37 {
38  return mExpression;
39 }
40 
42 {
43  QVariant v;
44 
45  if ( mCheckBox )
46  v = mCheckBox->isChecked() ? config( QStringLiteral( "CheckedState" ), true ) : config( QStringLiteral( "UncheckedState" ), false );
47 
48  return v;
49 }
50 
51 QgsSearchWidgetWrapper::FilterFlags QgsCheckboxSearchWidgetWrapper::supportedFlags() const
52 {
53  return EqualTo | IsNull | IsNotNull;
54 }
55 
56 QgsSearchWidgetWrapper::FilterFlags QgsCheckboxSearchWidgetWrapper::defaultFlags() const
57 {
58  return EqualTo;
59 }
60 
61 QString QgsCheckboxSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
62 {
63  const QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type();
64  const QString fieldName = createFieldIdentifier();
65 
66  //clear any unsupported flags
67  flags &= supportedFlags();
68  if ( flags & IsNull )
69  return fieldName + " IS NULL";
70 
71  if ( flags & IsNotNull )
72  return fieldName + " IS NOT NULL";
73 
74  const QVariant v = value();
75  if ( !v.isValid() )
76  return QString();
77 
78  switch ( fldType )
79  {
80  case QVariant::Int:
81  case QVariant::UInt:
82  case QVariant::Double:
83  case QVariant::LongLong:
84  case QVariant::ULongLong:
85  case QVariant::Bool:
86  {
87  if ( flags & EqualTo )
88  return fieldName + '=' + v.toString();
89  else if ( flags & NotEqualTo )
90  return fieldName + "<>" + v.toString();
91  break;
92  }
93 
94  default:
95  {
96  if ( flags & EqualTo )
97  return fieldName + "='" + v.toString() + '\'';
98  else if ( flags & NotEqualTo )
99  return fieldName + "<>'" + v.toString() + '\'';
100  break;
101  }
102  }
103 
104  return QString();
105 }
106 
108 {
109  if ( mCheckBox )
110  {
111  whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked );
112  }
113 }
114 
116 {
117  if ( mCheckBox )
118  {
119  mCheckBox->setEnabled( enabled );
120  }
121 }
122 
124 {
125  return true;
126 }
127 
128 void QgsCheckboxSearchWidgetWrapper::setExpression( const QString &expression )
129 {
130  QString exp = expression;
131  const QString fieldName = layer()->fields().at( mFieldIdx ).name();
132 
133  const QString str = QStringLiteral( "%1 = '%3'" )
134  .arg( QgsExpression::quotedColumnRef( fieldName ),
135  exp.replace( '\'', QLatin1String( "''" ) )
136  );
137  mExpression = str;
138 }
139 
140 void QgsCheckboxSearchWidgetWrapper::stateChanged( int )
141 {
142  if ( mCheckBox )
143  {
144  mCheckBox->setTristate( false );
145  const QString exp = value().toString();
146  setExpression( exp );
147  emit valueChanged();
149  }
150 }
151 
153 {
154  QCheckBox *c = new QCheckBox( parent );
155  c->setChecked( Qt::PartiallyChecked );
156  return c;
157 }
158 
160 {
161  mCheckBox = qobject_cast<QCheckBox *>( editor );
162 
163  if ( mCheckBox )
164  {
165  mCheckBox->setChecked( Qt::PartiallyChecked );
166  connect( mCheckBox, &QCheckBox::stateChanged, this, &QgsCheckboxSearchWidgetWrapper::stateChanged );
167  }
168 }
169 
170 
qgsfields.h
QgsSearchWidgetWrapper::expressionChanged
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QgsSearchWidgetWrapper::valueChanged
void valueChanged()
Emitted when a user changes the value of the search widget.
qgscheckboxsearchwidgetwrapper.h
QgsSearchWidgetWrapper::NotEqualTo
@ NotEqualTo
Supports not equal to.
Definition: qgssearchwidgetwrapper.h:97
QgsCheckboxSearchWidgetWrapper::value
QVariant value() const
Returns a variant representing the current state of the widget.
Definition: qgscheckboxsearchwidgetwrapper.cpp:41
QgsCheckboxSearchWidgetWrapper::supportedFlags
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
Definition: qgscheckboxsearchwidgetwrapper.cpp:51
QgsSearchWidgetWrapper
Shows a search widget on a filter form.
Definition: qgssearchwidgetwrapper.h:85
QgsField::name
QString name
Definition: qgsfield.h:60
QgsCheckboxSearchWidgetWrapper::createExpression
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
Definition: qgscheckboxsearchwidgetwrapper.cpp:61
QgsCheckboxSearchWidgetWrapper::clearWidget
void clearWidget() override
Definition: qgscheckboxsearchwidgetwrapper.cpp:107
QgsCheckboxSearchWidgetWrapper::QgsCheckboxSearchWidgetWrapper
QgsCheckboxSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsCheckboxSearchWidgetWrapper.
Definition: qgscheckboxsearchwidgetwrapper.cpp:25
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsWidgetWrapper::layer
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
Definition: qgswidgetwrapper.cpp:92
QgsSearchWidgetWrapper::mFieldIdx
int mFieldIdx
Definition: qgssearchwidgetwrapper.h:266
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:2191
QgsCheckboxSearchWidgetWrapper::setEnabled
void setEnabled(bool enabled) override
Definition: qgscheckboxsearchwidgetwrapper.cpp:115
QgsCheckboxSearchWidgetWrapper::expression
QString expression() const override
Will be used to access the widget's value.
Definition: qgscheckboxsearchwidgetwrapper.cpp:36
QgsSearchWidgetWrapper::mExpression
QString mExpression
Definition: qgssearchwidgetwrapper.h:265
QgsSearchWidgetWrapper::IsNull
@ IsNull
Supports searching for null values.
Definition: qgssearchwidgetwrapper.h:106
qgsvectorlayer.h
qgscheckboxwidgetfactory.h
QgsWidgetWrapper::config
QVariantMap config() const
Returns the whole config.
Definition: qgswidgetwrapper.cpp:82
c
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
Definition: porting_processing.dox:1
str
#define str(x)
Definition: qgis.cpp:37
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsCheckboxSearchWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgscheckboxsearchwidgetwrapper.cpp:152
QgsExpression::quotedColumnRef
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
Definition: qgsexpression.cpp:68
QgsCheckboxSearchWidgetWrapper::setExpression
void setExpression(const QString &expression) override
Definition: qgscheckboxsearchwidgetwrapper.cpp:128
QgsCheckboxSearchWidgetWrapper::defaultFlags
QgsSearchWidgetWrapper::FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
Definition: qgscheckboxsearchwidgetwrapper.cpp:56
QgsCheckboxSearchWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgscheckboxsearchwidgetwrapper.cpp:123
QgsCheckboxSearchWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgscheckboxsearchwidgetwrapper.cpp:159
QgsSearchWidgetWrapper::createFieldIdentifier
QString createFieldIdentifier() const
Gets a field name or expression to use as field comparison.
Definition: qgssearchwidgetwrapper.cpp:104
QgsSearchWidgetWrapper::EqualTo
@ EqualTo
Supports equal to.
Definition: qgssearchwidgetwrapper.h:96
QgsFields::at
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
QgsSearchWidgetWrapper::IsNotNull
@ IsNotNull
Supports searching for non-null values.
Definition: qgssearchwidgetwrapper.h:108
QgsCheckboxSearchWidgetWrapper::applyDirectly
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes.
Definition: qgscheckboxsearchwidgetwrapper.cpp:31
QgsField::type
QVariant::Type type
Definition: qgsfield.h:58