QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgscheckboxwidgetwrapper.cpp"
18
19QgsCheckboxWidgetWrapper::QgsCheckboxWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
20 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
21
22{
23}
24
25
27{
28 if ( config( QStringLiteral( "AllowNullState" ) ).toBool() && mCheckBox && mCheckBox->checkState() == Qt::PartiallyChecked )
29 {
30 return QVariant();
31 }
32
33 if ( field().type() == QMetaType::Type::Bool )
34 {
35 if ( mGroupBox )
36 {
37 return mGroupBox->isChecked();
38 }
39 else if ( mCheckBox )
40 {
41 return mCheckBox->isChecked();
42 }
43 }
44 else
45 {
46 if ( mGroupBox )
47 {
48 return mGroupBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
49 }
50 else if ( mCheckBox )
51 {
52 return mCheckBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
53 }
54 }
55
56 return QVariant();
57}
58
60{
61 if ( mCheckBox )
62 {
63 mIndeterminateStateEnabled = true;
64 whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked );
65 }
66}
67
68QWidget *QgsCheckboxWidgetWrapper::createWidget( QWidget *parent )
69{
70 return new QCheckBox( parent );
71}
72
74{
75 mCheckBox = qobject_cast<QCheckBox *>( editor );
76 mGroupBox = qobject_cast<QGroupBox *>( editor );
77
78 if ( mCheckBox )
79 connect( mCheckBox, &QCheckBox::stateChanged, this, [ = ]( int state )
80 {
81 if ( !mIndeterminateStateEnabled && mCheckBox->checkState() != Qt::PartiallyChecked )
82 {
83 mCheckBox->setTristate( false );
84 }
86 emit valueChanged( state != Qt::Unchecked );
88 emit valuesChanged( state != Qt::Unchecked );
89 } );
90 if ( mGroupBox )
91 connect( mGroupBox, &QGroupBox::toggled, this, [ = ]( bool state )
92 {
94 emit valueChanged( state );
96 emit valuesChanged( state );
97 } );
98}
99
101{
102 return mCheckBox || mGroupBox;
103}
104
105void QgsCheckboxWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
106{
107 Qt::CheckState state = Qt::Unchecked;
108
109 if ( config( QStringLiteral( "AllowNullState" ) ).toBool() && value.isNull() )
110 {
111 state = Qt::PartiallyChecked;
112 }
113 else
114 {
115 if ( field().type() == QMetaType::Type::Bool )
116 {
117 state = value.toBool() ? Qt::Checked : Qt::Unchecked;
118 }
119 else
120 {
121 state = value == config( QStringLiteral( "CheckedState" ) ) ? Qt::Checked : Qt::Unchecked;
122 }
123 }
124
125 if ( mGroupBox )
126 {
127 mGroupBox->setChecked( state == Qt::Checked );
128 }
129
130 if ( mCheckBox )
131 {
132 mCheckBox->setTristate( state == Qt::PartiallyChecked );
133 mCheckBox->setCheckState( state );
134 }
135}
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:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5862