CheckBox widget: support native Bool fields

This commit is contained in:
Matthias Kuhn 2017-04-07 13:01:17 +02:00
parent cfb8552b14
commit 88a8a2c94d
2 changed files with 38 additions and 8 deletions

View File

@ -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()
@ -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() );
}
}

View File

@ -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;
}
@ -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 );