From 258cd6562a4d4ecc3850f5878e259daa02c34d3a Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Thu, 25 Aug 2011 02:24:38 +0200 Subject: [PATCH] Core work for Reset Forms Action support Heavily inspired in Guillermo Amaral patches CCMAIL: gamaral@kde.org --- core/action.cpp | 46 ++++++++++++++++++++++++++++++++++++++++ core/action.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++- core/document.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ core/document.h | 7 +++++++ core/form.cpp | 17 +++++++++++++++ core/form.h | 14 +++++++++++++ core/form_p.h | 1 + 7 files changed, 187 insertions(+), 1 deletion(-) diff --git a/core/action.cpp b/core/action.cpp index b047ddf5c..b8ad9f08d 100644 --- a/core/action.cpp +++ b/core/action.cpp @@ -429,3 +429,49 @@ QString MovieAction::actionTip() const return i18n( "Play movie..." ); } #endif + +// FormAction + +class Okular::ResetFormActionPrivate : public Okular::ActionPrivate +{ + public: + ResetFormActionPrivate( const QStringList &fieldList, ResetFormAction::Behaviour behaviour ) + : ActionPrivate(), m_fieldList(fieldList), m_behaviour(behaviour) + { + } + + QStringList m_fieldList; + ResetFormAction::Behaviour m_behaviour; +}; + +ResetFormAction::ResetFormAction( const QStringList &fieldList, Behaviour behaviour ) + : Action( *new ResetFormActionPrivate( fieldList, behaviour ) ) +{ +} + +ResetFormAction::~ResetFormAction() +{ +} + +Action::ActionType ResetFormAction::actionType() const +{ + return ResetForm; +} + +QString ResetFormAction::actionTip() const +{ + return i18n( "Reset Forms" ); +} + +ResetFormAction::Behaviour ResetFormAction::behaviour() const +{ + Q_D( const Okular::ResetFormAction ); + return d->m_behaviour; +} + +QStringList ResetFormAction::fieldList() const +{ + Q_D( const Okular::ResetFormAction ); + return d->m_fieldList; +} + diff --git a/core/action.h b/core/action.h index b45956af2..d430d5029 100644 --- a/core/action.h +++ b/core/action.h @@ -27,6 +27,7 @@ class ScriptActionPrivate; class MovieActionPrivate; class Sound; class DocumentViewport; +class ResetFormActionPrivate; /** * @short Encapsulates data that describes an action. @@ -48,7 +49,8 @@ class OKULAR_EXPORT Action DocAction, ///< Start a custom action Sound, ///< Play a sound Movie, ///< Play a movie - Script ///< Executes a Script code + Script, ///< Executes a Script code + ResetForm ///< Start a reset form action @since 0.14 (KDE 4.8) }; /** @@ -431,6 +433,55 @@ class MovieAction : public Action }; #endif +/** + * The ResetFormAction action resets forms on activation + * @since 0.14 (KDE 4.8) + */ +class OKULAR_EXPORT ResetFormAction : public Action +{ + public: + enum Behaviour + { + ResetAllForms, ///< Reset all the forms in the document + ResetFormsInFieldList, ///< Reset the forms whose fully qualified name is in the list + ResetAllFormsExceptFieldList ///< Reset all the forms in the document exect those whose fully qualified name is in the list + }; + + /** + * Creates a new reset form action. + */ + ResetFormAction( const QStringList &fieldList, Behaviour behaviour ); + + /** + * Destroys the document action. + */ + virtual ~ResetFormAction(); + + /** + * Returns the action type. + */ + ActionType actionType() const; + + /** + * Returns the action tip. + */ + QString actionTip() const; + + /** + * Returns the reset behaviour. + */ + Behaviour behaviour() const; + + /** + * Returns the list of field fully qualified names. + */ + QStringList fieldList() const; + + private: + Q_DECLARE_PRIVATE( ResetFormAction ) + Q_DISABLE_COPY( ResetFormAction ) +}; + } #endif diff --git a/core/document.cpp b/core/document.cpp index 9662e4f65..a41782881 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -77,6 +77,7 @@ #include "utils_p.h" #include "view.h" #include "view_p.h" +#include "form.h" #include @@ -3001,6 +3002,55 @@ void Document::processAction( const Action * action ) //const MovieAction * movie = static_cast< const MovieAction * >( action ); // TODO this (Movie action) break; + + case Action::ResetForm: { + const ResetFormAction *resetFormAction = static_cast< const ResetFormAction * >( action ); + foreach( Page *page, d->m_pagesVector ) + { + foreach( FormField* ff, page->formFields() ) + { + bool reset = false; + switch (resetFormAction->behaviour()) + { + case ResetFormAction::ResetAllForms: + reset = true; + break; + case ResetFormAction::ResetFormsInFieldList: + reset = resetFormAction->fieldList().contains(ff->fullyQualifiedName()); + break; + case ResetFormAction::ResetAllFormsExceptFieldList: + reset = !resetFormAction->fieldList().contains(ff->fullyQualifiedName()); + break; + } + + if (reset) + { + switch (ff->type()) { + case Okular::FormField::FormText: { + Okular::FormFieldText* fft = static_cast(ff); + if (!fft->isReadOnly()) { + fft->setText( fft->defaultValue() ); + emit formFieldChanged(fft); + } + } break; + + case Okular::FormField::FormButton: { + Okular::FormFieldButton* ffb = static_cast(ff); + + if (!ffb->isReadOnly()) { + ffb->setState( "true" == ffb->defaultValue() ); + emit formFieldChanged(ffb); + } + } break; + + default: + kDebug() << "Unhandled form field: " << ff->name() << ff->defaultValue(); + break; + } + } + } + } + } break; } } diff --git a/core/document.h b/core/document.h index 27cb442a5..2bcf2807d 100644 --- a/core/document.h +++ b/core/document.h @@ -43,6 +43,7 @@ class DocumentViewport; class EmbeddedFile; class ExportFormat; class FontInfo; +class FormField; class Generator; class Action; class Page; @@ -722,6 +723,12 @@ class OKULAR_EXPORT Document : public QObject * Reports that the current search finished */ void searchFinished( int id, Okular::Document::SearchStatus endStatus ); + + /** + * Reports that some data of the given form changed + * @since 0.14 (KDE 4.8) + */ + void formFieldChanged( Okular::FormField *formField ); private: /// @cond PRIVATE diff --git a/core/form.cpp b/core/form.cpp index b56071bce..f7ad7f944 100644 --- a/core/form.cpp +++ b/core/form.cpp @@ -50,6 +50,12 @@ FormField::FieldType FormField::type() const return d->m_type; } +QString FormField::defaultValue() const +{ + Q_D( const FormField ); + return d->m_default; +} + bool FormField::isReadOnly() const { return false; @@ -66,6 +72,12 @@ Action* FormField::activationAction() const return d->m_activateAction; } +QString FormField::fullyQualifiedName() const +{ + Q_D( const FormField ); + return d->m_fullyQualifiedName; +} + void FormField::setActivationAction( Action *action ) { Q_D( FormField ); @@ -73,6 +85,11 @@ void FormField::setActivationAction( Action *action ) d->m_activateAction = action; } +void FormField::setFullyQualifiedName( const QString &name ) +{ + Q_D( FormField ); + d->m_fullyQualifiedName = name; +} class Okular::FormFieldButtonPrivate : public Okular::FormFieldPrivate { diff --git a/core/form.h b/core/form.h index c6e0104e9..6bcf1387e 100644 --- a/core/form.h +++ b/core/form.h @@ -59,6 +59,12 @@ class OKULAR_EXPORT FormField */ FieldType type() const; + /** + * The field default value. + * @since 0.14 (KDE 4.8) + */ + QString defaultValue() const; + /** * The bouding rect of the field, in normalized coordinates. */ @@ -92,6 +98,12 @@ class OKULAR_EXPORT FormField virtual bool isVisible() const; Action* activationAction() const; + + /** + * The internal fully qualified name of the field + * @since 0.14 (KDE 4.8) + */ + QString fullyQualifiedName() const; protected: /// @cond PRIVATE @@ -101,6 +113,8 @@ class OKULAR_EXPORT FormField /// @endcond void setActivationAction( Action *action ); + /// @since 0.14 (KDE 4.8) + void setFullyQualifiedName( const QString &name ); private: Q_DISABLE_COPY( FormField ) diff --git a/core/form_p.h b/core/form_p.h index 33d778a16..3ba91ff94 100644 --- a/core/form_p.h +++ b/core/form_p.h @@ -33,6 +33,7 @@ class FormFieldPrivate FormField::FieldType m_type; QString m_default; Action *m_activateAction; + QString m_fullyQualifiedName; Q_DECLARE_PUBLIC( FormField ) FormField *q_ptr;