QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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 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
56QWidget *QgsCheckboxWidgetWrapper::createWidget( QWidget *parent )
57{
58 return new QCheckBox( parent );
59}
60
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
89void 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}
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:3061
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:3060
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:2453