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