QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
18QgsCheckboxWidgetWrapper::QgsCheckboxWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
19 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
20
21{
22}
23
24
26{
27 if ( config( QStringLiteral( "AllowNullState" ) ).toBool() && mCheckBox && mCheckBox->checkState() == Qt::PartiallyChecked )
28 {
29 return QVariant();
30 }
31
32 if ( field().type() == QVariant::Bool )
33 {
34 if ( mGroupBox )
35 {
36 return mGroupBox->isChecked();
37 }
38 else if ( mCheckBox )
39 {
40 return mCheckBox->isChecked();
41 }
42 }
43 else
44 {
45 if ( mGroupBox )
46 {
47 return mGroupBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
48 }
49 else if ( mCheckBox )
50 {
51 return mCheckBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
52 }
53 }
54
55 return QVariant();
56}
57
59{
60 if ( mCheckBox )
61 {
62 mIndeterminateStateEnabled = true;
63 whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked );
64 }
65}
66
67QWidget *QgsCheckboxWidgetWrapper::createWidget( QWidget *parent )
68{
69 return new QCheckBox( parent );
70}
71
73{
74 mCheckBox = qobject_cast<QCheckBox *>( editor );
75 mGroupBox = qobject_cast<QGroupBox *>( editor );
76
77 if ( mCheckBox )
78 connect( mCheckBox, &QCheckBox::stateChanged, this, [ = ]( int state )
79 {
80 if ( !mIndeterminateStateEnabled && mCheckBox->checkState() != Qt::PartiallyChecked )
81 {
82 mCheckBox->setTristate( false );
83 }
85 emit valueChanged( state != Qt::Unchecked );
87 emit valuesChanged( state != Qt::Unchecked );
88 } );
89 if ( mGroupBox )
90 connect( mGroupBox, &QGroupBox::toggled, this, [ = ]( bool state )
91 {
93 emit valueChanged( state );
95 emit valuesChanged( state );
96 } );
97}
98
100{
101 return mCheckBox || mGroupBox;
102}
103
104void QgsCheckboxWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
105{
106 Qt::CheckState state = Qt::Unchecked;
107
108 if ( config( QStringLiteral( "AllowNullState" ) ).toBool() && value.isNull() )
109 {
110 state = Qt::PartiallyChecked;
111 }
112 else
113 {
114 if ( field().type() == QVariant::Bool )
115 {
116 state = value.toBool() ? Qt::Checked : Qt::Unchecked;
117 }
118 else
119 {
120 state = value == config( QStringLiteral( "CheckedState" ) ) ? Qt::Checked : Qt::Unchecked;
121 }
122 }
123
124 if ( mGroupBox )
125 {
126 mGroupBox->setChecked( state == Qt::Checked );
127 }
128
129 if ( mCheckBox )
130 {
131 mCheckBox->setTristate( state == Qt::PartiallyChecked );
132 mCheckBox->setCheckState( state );
133 }
134}
bool valid() const override
Returns true if the widget has been properly initialized.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
QgsCheckboxWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsCheckboxWidgetWrapper.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QVariant value() const override
Will be used to access the widget's value.
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.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
QgsField field() const
Access the field.
Represents a vector layer which manages a vector based data sets.
QVariantMap config() const
Returns the whole config.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:5776
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:5775
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:5111