QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 ) { emit valueChanged( state ); } );
68  if ( mGroupBox )
69  connect( mGroupBox, &QGroupBox::toggled, this, [ = ]( bool state ) { emit valueChanged( state ); } );
70 }
71 
73 {
74  return mCheckBox || mGroupBox;
75 }
76 
78 {
79  bool state = false;
80 
81  if ( field().type() == QVariant::Bool )
82  {
83  state = value.toBool();
84  }
85  else
86  {
87  state = ( value == config( QStringLiteral( "CheckedState" ) ) );
88  }
89  if ( mGroupBox )
90  {
91  mGroupBox->setChecked( state );
92  }
93 
94  if ( mCheckBox )
95  {
96  mCheckBox->setChecked( state );
97  }
98 }
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QgsField field() const
Access the field.
Manages an editor widget Widget and wrapper share the same parent.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
QVariantMap config() const
Returns the whole config.
bool valid() const override
Returns true if the widget has been properly initialized.
QgsCheckboxWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsCheckboxWidgetWrapper.
QVariant value() const override
Will be used to access the widget&#39;s value.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:212
void setValue(const QVariant &value) override
void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
Represents a vector layer which manages a vector based data sets.