Skip to content

Commit

Permalink
CheckBox widget: support native Bool fields
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Apr 10, 2017
1 parent cfb8552 commit 88a8a2c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/gui/editorwidgets/qgscheckboxconfigdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ QgsCheckBoxConfigDlg::QgsCheckBoxConfigDlg( QgsVectorLayer *vl, int fieldIdx, QW

connect( leCheckedState, &QLineEdit::textEdited, this, &QgsEditorConfigWidget::changed );
connect( leUncheckedState, &QLineEdit::textEdited, this, &QgsEditorConfigWidget::changed );

if ( vl->fields().at( fieldIdx ).type() == QVariant::Bool )
{
leCheckedState->setEnabled( false );
leUncheckedState->setEnabled( false );

leCheckedState->setPlaceholderText( QStringLiteral( "TRUE" ) );
leUncheckedState->setPlaceholderText( QStringLiteral( "FALSE" ) );
}
}

QVariantMap QgsCheckBoxConfigDlg::config()
Expand All @@ -36,6 +45,9 @@ QVariantMap QgsCheckBoxConfigDlg::config()

void QgsCheckBoxConfigDlg::setConfig( const QVariantMap &config )
{
leCheckedState->setText( config.value( QStringLiteral( "CheckedState" ) ).toString() );
leUncheckedState->setText( config.value( QStringLiteral( "UncheckedState" ) ).toString() );
if ( layer()->fields().at( field() ).type() != QVariant::Bool )
{
leCheckedState->setText( config.value( QStringLiteral( "CheckedState" ) ).toString() );
leUncheckedState->setText( config.value( QStringLiteral( "UncheckedState" ) ).toString() );
}
}
30 changes: 24 additions & 6 deletions src/gui/editorwidgets/qgscheckboxwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ QVariant QgsCheckboxWidgetWrapper::value() const
{
QVariant v;

if ( mGroupBox )
v = mGroupBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );

if ( mCheckBox )
v = mCheckBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
if ( field().type() == QVariant::Bool )
{
if ( mGroupBox )
v = mGroupBox->isChecked();
else if ( mCheckBox )
v = mCheckBox->isChecked();
}
else
{
if ( mGroupBox )
v = mGroupBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );

else if ( mCheckBox )
v = mCheckBox->isChecked() ? config( QStringLiteral( "CheckedState" ) ) : config( QStringLiteral( "UncheckedState" ) );
}

return v;
}
Expand Down Expand Up @@ -68,7 +77,16 @@ bool QgsCheckboxWidgetWrapper::valid() const

void QgsCheckboxWidgetWrapper::setValue( const QVariant &value )
{
bool state = ( value == config( QStringLiteral( "CheckedState" ) ) );
bool state = false;

if ( field().type() == QVariant::Bool )
{
state = value.toBool();
}
else
{
state = ( value == config( QStringLiteral( "CheckedState" ) ) );
}
if ( mGroupBox )
{
mGroupBox->setChecked( state );
Expand Down

0 comments on commit 88a8a2c

Please sign in to comment.