diff --git a/core/form.cpp b/core/form.cpp index e0aeaf968..960e9080b 100644 --- a/core/form.cpp +++ b/core/form.cpp @@ -147,6 +147,10 @@ void FormFieldButton::setState( bool ) { } +void FormFieldButton::setIcon( Okular::FormField * ) +{ +} + class Okular::FormFieldTextPrivate : public Okular::FormFieldPrivate { diff --git a/core/form.h b/core/form.h index 18612a246..5034cd6e1 100644 --- a/core/form.h +++ b/core/form.h @@ -207,6 +207,13 @@ class OKULARCORE_EXPORT FormFieldButton : public FormField */ virtual QList< int > siblings() const = 0; + /** + * Sets the icon of the Button to the Icon of the field parameter. + * + * @since 1.7 + */ + virtual void setIcon( Okular::FormField *field ); + protected: FormFieldButton(); diff --git a/core/script/kjs_field.cpp b/core/script/kjs_field.cpp index a087a8e74..23a23b753 100644 --- a/core/script/kjs_field.cpp +++ b/core/script/kjs_field.cpp @@ -17,6 +17,7 @@ #include #include +#include #include "../debug_p.h" #include "../document_p.h" @@ -30,6 +31,8 @@ static KJSPrototype *g_fieldProto; typedef QHash< FormField *, Page * > FormCache; Q_GLOBAL_STATIC( FormCache, g_fieldCache ) +typedef QHash< QString, FormField * > ButtonCache; +Q_GLOBAL_STATIC( ButtonCache, g_buttonCache ) // Helper for modified fields @@ -241,6 +244,39 @@ static void fieldSetDisplay( KJSContext *context, void *object, KJSObject value updateField( field ); } +// Instead of getting the Icon, we pick the field. +static KJSObject fieldButtonGetIcon( KJSContext *ctx, void *object, + const KJSArguments & ) +{ + FormField *field = reinterpret_cast< FormField * >( object ); + + KJSObject fieldObject; + fieldObject.setProperty( ctx, QStringLiteral("name").toLatin1().toBase64(), field->name() ); + g_buttonCache->insert( field->name(), field ); + + return fieldObject; +} + +/* +* Now we send to the button what Icon should be drawn on it +*/ +static KJSObject fieldButtonSetIcon( KJSContext *ctx, void *object, + const KJSArguments &arguments ) +{ + FormField *field = reinterpret_cast< FormField * >( object ); + + QString fieldName = arguments.at( 0 ).property( ctx, QStringLiteral("name").toLatin1().toBase64() ).toString( ctx ); + + if( field->type() == Okular::FormField::FormButton ) + { + FormFieldButton *button = static_cast< FormFieldButton * >( field ); + button->setIcon( g_buttonCache->value( fieldName ) ); + } + + updateField( field ); + + return KJSUndefined(); +} void JSField::initType( KJSContext *ctx ) { @@ -260,6 +296,9 @@ void JSField::initType( KJSContext *ctx ) g_fieldProto->defineProperty( ctx, QStringLiteral("value"), fieldGetValue, fieldSetValue ); g_fieldProto->defineProperty( ctx, QStringLiteral("hidden"), fieldGetHidden, fieldSetHidden ); g_fieldProto->defineProperty( ctx, QStringLiteral("display"), fieldGetDisplay, fieldSetDisplay ); + + g_fieldProto->defineFunction( ctx, QStringLiteral("buttonGetIcon"), fieldButtonGetIcon ); + g_fieldProto->defineFunction( ctx, QStringLiteral("buttonSetIcon"), fieldButtonSetIcon ); } KJSObject JSField::wrapField( KJSContext *ctx, FormField *field, Page *page ) @@ -274,7 +313,8 @@ KJSObject JSField::wrapField( KJSContext *ctx, FormField *field, Page *page ) void JSField::clearCachedFields() { if ( g_fieldCache.exists() ) - { g_fieldCache->clear(); - } + + if( g_buttonCache.exists() ) + g_buttonCache->clear(); } diff --git a/generators/poppler/CMakeLists.txt b/generators/poppler/CMakeLists.txt index d3b95ef52..83c3bff08 100644 --- a/generators/poppler/CMakeLists.txt +++ b/generators/poppler/CMakeLists.txt @@ -144,6 +144,18 @@ int main() } " HAVE_POPPLER_0_73) +check_cxx_source_compiles(" +#include +#include +int main() +{ + Poppler::FormFieldIcon icon(nullptr); + Poppler::FormFieldButton *button; + button->setIcon( icon ); + return 0; +} +" HAVE_POPPLER_0_78) + configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/config-okular-poppler.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-okular-poppler.h diff --git a/generators/poppler/config-okular-poppler.h.cmake b/generators/poppler/config-okular-poppler.h.cmake index bbd2b823b..ee7828722 100644 --- a/generators/poppler/config-okular-poppler.h.cmake +++ b/generators/poppler/config-okular-poppler.h.cmake @@ -45,3 +45,6 @@ /* Defined if we have the 0.73 version of the Poppler library */ #cmakedefine HAVE_POPPLER_0_73 1 + +/* Defined if we have the 0.78 version of the Poppler library */ +#cmakedefine HAVE_POPPLER_0_78 1 diff --git a/generators/poppler/formfields.cpp b/generators/poppler/formfields.cpp index f5b41110c..0e463571e 100644 --- a/generators/poppler/formfields.cpp +++ b/generators/poppler/formfields.cpp @@ -139,6 +139,26 @@ QList< int > PopplerFormFieldButton::siblings() const return m_field->siblings(); } +#ifdef HAVE_POPPLER_0_78 +Poppler::FormFieldIcon PopplerFormFieldButton::icon() const +{ + return m_field->icon(); +} +#endif + +void PopplerFormFieldButton::setIcon( Okular::FormField *field ) +{ +#ifdef HAVE_POPPLER_0_78 + if( field->type() == Okular::FormField::FormButton ) + { + PopplerFormFieldButton *button = static_cast< PopplerFormFieldButton * >( field ); + m_field->setIcon( button->icon() ); + } +#else + Q_UNUSED( field ); +#endif +} + PopplerFormFieldText::PopplerFormFieldText( Poppler::FormFieldText * field ) : Okular::FormFieldText(), m_field( field ) diff --git a/generators/poppler/formfields.h b/generators/poppler/formfields.h index 5dbe7a8fc..2f1d0b0d2 100644 --- a/generators/poppler/formfields.h +++ b/generators/poppler/formfields.h @@ -11,6 +11,7 @@ #define _OKULAR_GENERATOR_PDF_FORMFIELDS_H_ #include +#include #include "core/form.h" class PopplerFormFieldButton : public Okular::FormFieldButton @@ -35,6 +36,15 @@ class PopplerFormFieldButton : public Okular::FormFieldButton bool state() const override; void setState( bool state ) override; QList< int > siblings() const override; + void setIcon( Okular::FormField *field ) override; +#ifdef HAVE_POPPLER_0_78 + /* + * Supported only in newer versions of Poppler library. + * + * @since 1.7 + */ + Poppler::FormFieldIcon icon() const; +#endif private: Poppler::FormFieldButton * m_field;