QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgscheckboxwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscheckboxwidgetwrapper.cpp
3  --------------------------------------
4  Date : 5.1.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias at opengis dot ch
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 QgsCheckboxWidgetWrapper::QgsCheckboxWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
19  : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
20 
21 {
22 }
23 
24 
26 {
27  QVariant v;
28 
29  if ( field().type() == QVariant::Bool )
30  {
31  if ( mGroupBox )
32  v = mGroupBox->isChecked();
33  else if ( mCheckBox )
34  v = mCheckBox->isChecked();
35  }
36  else
37  {
38  if ( mGroupBox )
39  v = mGroupBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
40 
41  else if ( mCheckBox )
42  v = mCheckBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
43  }
44 
45  return v;
46 }
47 
49 {
50  if ( mCheckBox )
51  {
52  whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked );
53  }
54 }
55 
56 QWidget *QgsCheckboxWidgetWrapper::createWidget( QWidget *parent )
57 {
58  return new QCheckBox( parent );
59 }
60 
61 void QgsCheckboxWidgetWrapper::initWidget( QWidget *editor )
62 {
63  mCheckBox = qobject_cast<QCheckBox *>( editor );
64  mGroupBox = qobject_cast<QGroupBox *>( editor );
65 
66  if ( mCheckBox )
67  connect( mCheckBox, &QAbstractButton::toggled, this, [ = ]( bool state )
68  {
70  emit valueChanged( state );
72  emit valuesChanged( state );
73  } );
74  if ( mGroupBox )
75  connect( mGroupBox, &QGroupBox::toggled, this, [ = ]( bool state )
76  {
78  emit valueChanged( state );
80  emit valuesChanged( state );
81  } );
82 }
83 
85 {
86  return mCheckBox || mGroupBox;
87 }
88 
89 void QgsCheckboxWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
90 {
91  bool state = false;
92 
93  if ( field().type() == QVariant::Bool )
94  {
95  state = value.toBool();
96  }
97  else
98  {
99  state = ( value == config( QStringLiteral( "CheckedState" ) ) );
100  }
101  if ( mGroupBox )
102  {
103  mGroupBox->setChecked( state );
104  }
105 
106  if ( mCheckBox )
107  {
108  mCheckBox->setChecked( state );
109  }
110 }
QgsCheckboxWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgscheckboxwidgetwrapper.cpp:84
QgsEditorWidgetWrapper
Definition: qgseditorwidgetwrapper.h:47
QgsCheckboxWidgetWrapper::QgsCheckboxWidgetWrapper
QgsCheckboxWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsCheckboxWidgetWrapper.
Definition: qgscheckboxwidgetwrapper.cpp:18
QgsEditorWidgetWrapper::valueChanged
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:752
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:262
QgsEditorWidgetWrapper::field
QgsField field() const
Access the field.
Definition: qgseditorwidgetwrapper.cpp:39
qgscheckboxwidgetwrapper.h
QgsCheckboxWidgetWrapper::showIndeterminateState
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
Definition: qgscheckboxwidgetwrapper.cpp:48
QgsEditorWidgetWrapper::valuesChanged
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
QgsWidgetWrapper::config
QVariantMap config() const
Returns the whole config.
Definition: qgswidgetwrapper.cpp:81
QgsVectorLayer
Definition: qgsvectorlayer.h:385
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:751
QgsCheckboxWidgetWrapper::value
QVariant value() const override
Will be used to access the widget's value.
Definition: qgscheckboxwidgetwrapper.cpp:25
QgsCheckboxWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgscheckboxwidgetwrapper.cpp:61
QgsCheckboxWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgscheckboxwidgetwrapper.cpp:56