QGIS API Documentation 3.41.0-Master (d5b93354e9c)
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 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 ) {
92 emit valueChanged( state );
94 emit valuesChanged( state );
95 } );
96}
97
99{
100 return mCheckBox || mGroupBox;
101}
102
103void QgsCheckboxWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
104{
105 Qt::CheckState state = Qt::Unchecked;
106
107 if ( config( QStringLiteral( "AllowNullState" ) ).toBool() && value.isNull() )
108 {
109 state = Qt::PartiallyChecked;
110 }
111 else
112 {
113 if ( field().type() == QMetaType::Type::Bool )
114 {
115 state = value.toBool() ? Qt::Checked : Qt::Unchecked;
116 }
117 else
118 {
119 state = value == config( QStringLiteral( "CheckedState" ) ) ? Qt::Checked : Qt::Unchecked;
120 }
121 }
122
123 if ( mGroupBox )
124 {
125 mGroupBox->setChecked( state == Qt::Checked );
126 }
127
128 if ( mCheckBox )
129 {
130 mCheckBox->setTristate( state == Qt::PartiallyChecked );
131 mCheckBox->setCheckState( state );
132 }
133}
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:6601
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6600
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5928