Commit 8ed1d14f authored by Andre Heinecke's avatar Andre Heinecke Committed by Albert Astals Cid

Fix CheckBox script usage

Summary:
This implements setting / getting the value of buttons,
which is important for checkboxes in scripts. It also moves
the checkbox activate action after the value is set so that
the correct value is used when the activation script is
executed.

Reviewers: #okular

Tags: #okular

Maniphest Tasks: T8097

Differential Revision: https://phabricator.kde.org/D10868
parent 6897e042
......@@ -134,8 +134,11 @@ static KJSObject fieldGetValue( KJSContext */*context*/, void *object )
case FormField::FormButton:
{
const FormFieldButton *button = static_cast< const FormFieldButton * >( field );
Q_UNUSED( button ); // ###
break;
if ( button->state() )
{
return KJSString( QStringLiteral( "Yes" ) );
}
return KJSString( QStringLiteral( "Off" ) );
}
case FormField::FormText:
{
......@@ -167,7 +170,17 @@ static void fieldSetValue( KJSContext *context, void *object, KJSObject value )
case FormField::FormButton:
{
FormFieldButton *button = static_cast< FormFieldButton * >( field );
Q_UNUSED( button ); // ###
const QString text = value.toString( context );
if ( text == QStringLiteral( "Yes" ) )
{
button->setState( true );
updateField( field );
}
else if ( text == QStringLiteral( "Off" ) )
{
button->setState( false );
updateField( field );
}
break;
}
case FormField::FormText:
......
......@@ -154,7 +154,8 @@ bool FormWidgetsController::canRedo()
void FormWidgetsController::slotButtonClicked( QAbstractButton *button )
{
int pageNumber = -1;
if ( CheckBoxEdit *check = qobject_cast< CheckBoxEdit * >( button ) )
CheckBoxEdit *check = qobject_cast< CheckBoxEdit * >( button );
if ( check )
{
// Checkboxes need to be uncheckable so if clicking a checked one
// disable the exclusive status temporarily and uncheck it
......@@ -186,6 +187,13 @@ void FormWidgetsController::slotButtonClicked( QAbstractButton *button )
}
if (checked != prevChecked)
emit formButtonsChangedByWidget( pageNumber, formButtons, checked );
if ( check )
{
// The formButtonsChangedByWidget signal changes the value of the underlying
// Okular::FormField of the checkbox. We need to execute the activiation
// action after this.
check->doActivateAction();
}
}
void FormWidgetsController::slotFormButtonsChangedByUndoRedo( int pageNumber, const QList< Okular::FormFieldButton* > & formButtons)
......@@ -194,6 +202,11 @@ void FormWidgetsController::slotFormButtonsChangedByUndoRedo( int pageNumber, co
{
int id = formButton->id();
QAbstractButton* button = m_buttons[id];
CheckBoxEdit *check = qobject_cast< CheckBoxEdit * >( button );
if ( check )
{
emit refreshFormWidget( check->formField() );
}
// temporarily disable exclusiveness of the button group
// since it breaks doing/redoing steps into which all the checkboxes
// are unchecked
......@@ -384,16 +397,33 @@ void CheckBoxEdit::setFormWidgetsController( FormWidgetsController *controller )
FormWidgetIface::setFormWidgetsController( controller );
m_controller->registerRadioButton( this, form );
setChecked( form->state() );
connect( this, &QCheckBox::stateChanged, this, &CheckBoxEdit::slotStateChanged );
}
void CheckBoxEdit::slotStateChanged( int state )
void CheckBoxEdit::doActivateAction()
{
Okular::FormFieldButton *form = static_cast<Okular::FormFieldButton *>(m_ff);
if ( state == Qt::Checked && form->activationAction() )
if ( form->activationAction() )
m_controller->signalAction( form->activationAction() );
}
void CheckBoxEdit::slotRefresh( Okular::FormField * form )
{
if ( form != m_ff )
{
return;
}
FormWidgetIface::slotRefresh( form );
Okular::FormFieldButton *button = static_cast<Okular::FormFieldButton *>(m_ff);
bool oldState = isChecked();
bool newState = button->state();
if ( oldState != newState )
{
setChecked( button->state() );
doActivateAction();
}
}
RadioButtonEdit::RadioButtonEdit( Okular::FormFieldButton * button, QWidget * parent )
: QRadioButton( parent ), FormWidgetIface( this, button )
......
......@@ -192,8 +192,10 @@ class CheckBoxEdit : public QCheckBox, public FormWidgetIface
// reimplemented from FormWidgetIface
void setFormWidgetsController( FormWidgetsController *controller ) override;
private Q_SLOTS:
void slotStateChanged( int state );
void doActivateAction();
protected:
void slotRefresh( Okular::FormField *form ) override;
};
class RadioButtonEdit : public QRadioButton, public FormWidgetIface
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment