QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsfield.h"
20 #include "qgsvectorlayer.h"
21 
22 #include <QSettings>
23 #include <QCheckBox>
24 
26  : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
27  , mCheckBox( nullptr )
28  , mLayer( nullptr )
29 {
30 }
31 
33 {
34  return true;
35 }
36 
38 {
39  return mExpression;
40 }
41 
43 {
44  QVariant v;
45 
46  if ( mCheckBox )
47  v = mCheckBox->isChecked() ? config( "CheckedState" ) : config( "UncheckedState" );
48 
49  return v;
50 }
51 
52 QgsSearchWidgetWrapper::FilterFlags QgsCheckboxSearchWidgetWrapper::supportedFlags() const
53 {
54  return EqualTo | IsNull | IsNotNull;
55 }
56 
57 QgsSearchWidgetWrapper::FilterFlags QgsCheckboxSearchWidgetWrapper::defaultFlags() const
58 {
59  return EqualTo;
60 }
61 
62 QString QgsCheckboxSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
63 {
64  QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type();
65  QString fieldName = QgsExpression::quotedColumnRef( layer()->fields().at( mFieldIdx ).name() );
66 
67  //clear any unsupported flags
68  flags &= supportedFlags();
69  if ( flags & IsNull )
70  return fieldName + " IS NULL";
71 
72  if ( flags & IsNotNull )
73  return fieldName + " IS NOT NULL";
74 
75  QVariant v = value();
76  if ( !v.isValid() )
77  return QString();
78 
79  switch ( fldType )
80  {
81  case QVariant::Int:
82  case QVariant::UInt:
83  case QVariant::Double:
84  case QVariant::LongLong:
85  case QVariant::ULongLong:
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 
129 {
130  QString fieldName = layer()->fields().at( mFieldIdx ).name();
131 
132  QString str = QString( "%1 = '%3'" )
133  .arg( QgsExpression::quotedColumnRef( fieldName ),
134  exp.replace( '\'', "''" )
135  );
136  mExpression = str;
137 }
138 
139 void QgsCheckboxSearchWidgetWrapper::stateChanged( int )
140 {
141  if ( mCheckBox )
142  {
143  mCheckBox->setTristate( false );
144  QString exp = value().toString();
145  setExpression( exp );
146  emit valueChanged();
148  }
149 }
150 
152 {
153  QCheckBox* c = new QCheckBox( parent );
154  c->setChecked( Qt::PartiallyChecked );
155  return c;
156 }
157 
159 {
160  mCheckBox = qobject_cast<QCheckBox*>( editor );
161 
162  if ( mCheckBox )
163  {
164  mCheckBox->setChecked( Qt::PartiallyChecked );
165  connect( mCheckBox, SIGNAL( stateChanged( int ) ), this, SLOT( stateChanged( int ) ) );
166  }
167 }
168 
169 
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Manages an editor widget Widget and wrapper share the same parent.
bool valid() const override
Return true if the widget has been properly initialized.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QString name
Definition: qgsfield.h:52
virtual QString createExpression(FilterFlags flags) const override
Creates a filter expression based on the current state of the search widget and the specified filter ...
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes...
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:422
QgsFields fields() const
Returns the list of fields of this layer.
FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
const char * name() const
FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
void setEnabled(bool)
void valueChanged()
Emitted when a user changes the value of the search widget.
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void setTristate(bool y)
QVariant value() const
Returns a variant representing the current state of the widget.
bool isChecked() const
QString & replace(int position, int n, QChar after)
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:333
bool isValid() const
virtual void setEnabled(bool enabled) override
QString expression() override
Will be used to access the widget&#39;s value.
QgsEditorWidgetConfig config() const
Returns the whole config.
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:97
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
QgsCheckboxSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsCheckboxSearchWidgetWrapper.