QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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#include <QString>
19
20#include "moc_qgscheckboxwidgetwrapper.cpp"
21
22using namespace Qt::StringLiterals;
23
25 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
26
27{
28}
29
30
32{
33 if ( config( u"AllowNullState"_s ).toBool() && mCheckBox && mCheckBox->checkState() == Qt::PartiallyChecked )
34 {
35 return QVariant();
36 }
37
38 if ( field().type() == QMetaType::Type::Bool )
39 {
40 if ( mGroupBox )
41 {
42 return mGroupBox->isChecked();
43 }
44 else if ( mCheckBox )
45 {
46 return mCheckBox->isChecked();
47 }
48 }
49 else
50 {
51 if ( mGroupBox )
52 {
53 return mGroupBox->isChecked() ? config( u"CheckedState"_s ) : config( u"UncheckedState"_s );
54 }
55 else if ( mCheckBox )
56 {
57 return mCheckBox->isChecked() ? config( u"CheckedState"_s ) : config( u"UncheckedState"_s );
58 }
59 }
60
61 return QVariant();
62}
63
65{
66 if ( mCheckBox )
67 {
68 mIndeterminateStateEnabled = true;
69 whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked );
70 }
71}
72
73QWidget *QgsCheckboxWidgetWrapper::createWidget( QWidget *parent )
74{
75 return new QCheckBox( parent );
76}
77
79{
80 mCheckBox = qobject_cast<QCheckBox *>( editor );
81 mGroupBox = qobject_cast<QGroupBox *>( editor );
82
83 if ( mCheckBox )
84 connect( mCheckBox, &QCheckBox::stateChanged, this, [this]( int state ) {
85 if ( !mIndeterminateStateEnabled && mCheckBox->checkState() != Qt::PartiallyChecked )
86 {
87 mCheckBox->setTristate( false );
88 }
90 emit valueChanged( state != Qt::Unchecked );
92 emit valuesChanged( state != Qt::Unchecked );
93 } );
94 if ( mGroupBox )
95 connect( mGroupBox, &QGroupBox::toggled, this, [this]( bool state ) {
97 emit valueChanged( state );
99 emit valuesChanged( state );
100 } );
101}
102
104{
105 return mCheckBox || mGroupBox;
106}
107
108void QgsCheckboxWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
109{
110 Qt::CheckState state = Qt::Unchecked;
111
112 if ( config( u"AllowNullState"_s ).toBool() && value.isNull() )
113 {
114 state = Qt::PartiallyChecked;
115 }
116 else
117 {
118 if ( field().type() == QMetaType::Type::Bool )
119 {
120 state = value.toBool() ? Qt::Checked : Qt::Unchecked;
121 }
122 else
123 {
124 state = value == config( u"CheckedState"_s ) ? Qt::Checked : Qt::Unchecked;
125 }
126 }
127
128 if ( mGroupBox )
129 {
130 mGroupBox->setChecked( state == Qt::Checked );
131 }
132
133 if ( mCheckBox )
134 {
135 mCheckBox->setTristate( state == Qt::PartiallyChecked );
136 mCheckBox->setCheckState( state );
137 }
138}
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.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
int fieldIdx() const
Access the field index.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
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 dataset.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QVariantMap config() const
Returns the whole config.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7451
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7450
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6804