summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Heinecke <[email protected]>2018-03-20 22:59:42 +0100
committerAlbert Astals Cid <[email protected]>2018-03-20 23:19:29 +0100
commit8ed1d14f8d5c5744734473b70222b74b37fc3e0c (patch)
treecc1ce1744efddec5105438945bf71973b700e6f9
parent6897e042dfbdd048d91b55829bb69f570e605462 (diff)
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
-rw-r--r--core/script/kjs_field.cpp19
-rw-r--r--ui/formwidgets.cpp38
-rw-r--r--ui/formwidgets.h6
3 files changed, 54 insertions, 9 deletions
diff --git a/core/script/kjs_field.cpp b/core/script/kjs_field.cpp
index 16235e0..2cc9bd0 100644
--- a/core/script/kjs_field.cpp
+++ b/core/script/kjs_field.cpp
@@ -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:
diff --git a/ui/formwidgets.cpp b/ui/formwidgets.cpp
index dd9704a..68f4595 100644
--- a/ui/formwidgets.cpp
+++ b/ui/formwidgets.cpp
@@ -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 )
diff --git a/ui/formwidgets.h b/ui/formwidgets.h
index 22ae1a9..22ea721 100644
--- a/ui/formwidgets.h
+++ b/ui/formwidgets.h
@@ -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