From 6897e042dfbdd048d91b55829bb69f570e605462 Mon Sep 17 00:00:00 2001 From: Andre Heinecke Date: Tue, 20 Mar 2018 22:59:37 +0100 Subject: [PATCH] Add support to set read only from scripts Summary: This makes it possible to set the read only state from scripts. Requires Poppler master with the patch attached to the parent task. Reviewers: #okular Subscribers: aacid Tags: #okular Maniphest Tasks: T8097 Differential Revision: https://phabricator.kde.org/D10867 --- core/form.cpp | 4 ++++ core/form.h | 7 ++++++ core/script/kjs_field.cpp | 39 ++++++++++++++++--------------- generators/poppler/formfields.cpp | 27 +++++++++++++++++++++ generators/poppler/formfields.h | 3 +++ 5 files changed, 61 insertions(+), 19 deletions(-) diff --git a/core/form.cpp b/core/form.cpp index 530499fa3..35b05c2a6 100644 --- a/core/form.cpp +++ b/core/form.cpp @@ -55,6 +55,10 @@ bool FormField::isReadOnly() const return false; } +void FormField::setReadOnly( bool ) +{ +} + bool FormField::isVisible() const { return true; diff --git a/core/form.h b/core/form.h index 359019e7d..20b68c517 100644 --- a/core/form.h +++ b/core/form.h @@ -86,6 +86,13 @@ class OKULARCORE_EXPORT FormField */ virtual bool isReadOnly() const; + /** + * Whether the field is read-only. + * + * @since 1.4 + */ + virtual void setReadOnly( bool value ); + /** * Whether this form field is visible. */ diff --git a/core/script/kjs_field.cpp b/core/script/kjs_field.cpp index b3798ebb7..16235e08f 100644 --- a/core/script/kjs_field.cpp +++ b/core/script/kjs_field.cpp @@ -31,6 +31,23 @@ static KJSPrototype *g_fieldProto; typedef QHash< FormField *, Page * > FormCache; Q_GLOBAL_STATIC( FormCache, g_fieldCache ) + +// Helper for modified fields +static void updateField( FormField *field ) +{ + Page *page = g_fieldCache->value( field ); + if (page) + { + Document *doc = PagePrivate::get( page )->m_doc->m_parent; + QMetaObject::invokeMethod( doc, "refreshPixmaps", Qt::QueuedConnection, Q_ARG( int, page->number() ) ); + emit doc->refreshFormWidget( field ); + } + else + { + qWarning() << "Could not get page of field" << field; + } +} + // Field.doc static KJSObject fieldGetDoc( KJSContext *context, void * ) { @@ -54,16 +71,11 @@ static KJSObject fieldGetReadOnly( KJSContext *, void *object ) // Field.readonly (setter) static void fieldSetReadOnly( KJSContext *context, void *object, KJSObject value ) { -#if 0 FormField *field = reinterpret_cast< FormField * >( object ); bool b = value.toBoolean( context ); field->setReadOnly( b ); -#else - Q_UNUSED( context ); - Q_UNUSED( object ); - Q_UNUSED( value ); - qCDebug(OkularCoreDebug) << "Not implemented: setting readonly property"; -#endif + + updateField( field ); } static QString fieldGetTypeHelper( const FormField *field ) @@ -165,18 +177,7 @@ static void fieldSetValue( KJSContext *context, void *object, KJSObject value ) if ( text != textField->text() ) { textField->setText( text ); - - Page *page = g_fieldCache->value( field ); - if (page) - { - Document *doc = PagePrivate::get( page )->m_doc->m_parent; - QMetaObject::invokeMethod( doc, "refreshPixmaps", Qt::QueuedConnection, Q_ARG( int, page->number() ) ); - emit doc->refreshFormWidget( field ); - } - else - { - qWarning() << "Could not get page of field" << field; - } + updateField( field ); } break; } diff --git a/generators/poppler/formfields.cpp b/generators/poppler/formfields.cpp index 5539525e7..78fc69d12 100644 --- a/generators/poppler/formfields.cpp +++ b/generators/poppler/formfields.cpp @@ -67,6 +67,15 @@ bool PopplerFormFieldButton::isReadOnly() const return m_field->isReadOnly(); } +void PopplerFormFieldButton::setReadOnly( bool value ) +{ +#ifdef HAVE_POPPLER_0_64 + m_field->setReadOnly( value ); +#else + Q_UNUSED( value ); +#endif +} + bool PopplerFormFieldButton::isVisible() const { return m_field->isVisible(); @@ -145,6 +154,15 @@ bool PopplerFormFieldText::isReadOnly() const return m_field->isReadOnly(); } +void PopplerFormFieldText::setReadOnly( bool value ) +{ +#ifdef HAVE_POPPLER_0_64 + m_field->setReadOnly( value ); +#else + Q_UNUSED( value ); +#endif +} + bool PopplerFormFieldText::isVisible() const { return m_field->isVisible(); @@ -238,6 +256,15 @@ bool PopplerFormFieldChoice::isReadOnly() const return m_field->isReadOnly(); } +void PopplerFormFieldChoice::setReadOnly( bool value ) +{ +#ifdef HAVE_POPPLER_0_64 + m_field->setReadOnly( value ); +#else + Q_UNUSED( value ); +#endif +} + bool PopplerFormFieldChoice::isVisible() const { return m_field->isVisible(); diff --git a/generators/poppler/formfields.h b/generators/poppler/formfields.h index c5dfcf5fc..eff478e26 100644 --- a/generators/poppler/formfields.h +++ b/generators/poppler/formfields.h @@ -25,6 +25,7 @@ class PopplerFormFieldButton : public Okular::FormFieldButton QString name() const override; QString uiName() const override; bool isReadOnly() const override; + void setReadOnly( bool value ) override; bool isVisible() const override; // inherited from Okular::FormFieldButton @@ -53,6 +54,7 @@ class PopplerFormFieldText : public Okular::FormFieldText QString name() const override; QString uiName() const override; bool isReadOnly() const override; + void setReadOnly( bool value ) override; bool isVisible() const override; // inherited from Okular::FormFieldText @@ -84,6 +86,7 @@ class PopplerFormFieldChoice : public Okular::FormFieldChoice QString name() const override; QString uiName() const override; bool isReadOnly() const override; + void setReadOnly( bool value ) override; bool isVisible() const override; // inherited from Okular::FormFieldChoice