QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 "moc_qgscheckboxwidgetwrapper.cpp"
19
21 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
22
23{
24}
25
26
28{
29 if ( config( QStringLiteral( "AllowNullState" ) ).toBool() && mCheckBox && mCheckBox->checkState() == Qt::PartiallyChecked )
30 {
31 return QVariant();
32 }
33
34 if ( field().type() == QMetaType::Type::Bool )
35 {
36 if ( mGroupBox )
37 {
38 return mGroupBox->isChecked();
39 }
40 else if ( mCheckBox )
41 {
42 return mCheckBox->isChecked();
43 }
44 }
45 else
46 {
47 if ( mGroupBox )
48 {
49 return mGroupBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
50 }
51 else if ( mCheckBox )
52 {
53 return mCheckBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
54 }
55 }
56
57 return QVariant();
58}
59
61{
62 if ( mCheckBox )
63 {
64 mIndeterminateStateEnabled = true;
65 whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked );
66 }
67}
68
69QWidget *QgsCheckboxWidgetWrapper::createWidget( QWidget *parent )
70{
71 return new QCheckBox( parent );
72}
73
75{
76 mCheckBox = qobject_cast<QCheckBox *>( editor );
77 mGroupBox = qobject_cast<QGroupBox *>( editor );
78
79 if ( mCheckBox )
80 connect( mCheckBox, &QCheckBox::stateChanged, this, [this]( int state ) {
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, [this]( bool state ) {
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() == QMetaType::Type::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.
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:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6511