From f31eb3cb68c4fc77a980965badcd13b1542d45a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Netto?= Date: Thu, 22 Aug 2019 19:16:56 -0300 Subject: [PATCH] Created private functions to avoid duplicated code in Document --- core/document.cpp | 113 ++++++++++++++++------------------------------ core/document_p.h | 14 ++++++ 2 files changed, 54 insertions(+), 73 deletions(-) diff --git a/core/document.cpp b/core/document.cpp index c152d18a5..f9700878c 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -2212,6 +2212,36 @@ void DocumentPrivate::clearAndWaitForRequests() while ( startEventLoop ); } +int DocumentPrivate::findFieldPageNumber( Okular::FormField *field ) +{ + // Lookup the page of the FormField + int foundPage = -1; + for ( uint pageIdx = 0, nPages = m_parent->pages(); pageIdx < nPages; pageIdx++ ) + { + const Page *p = m_parent->page( pageIdx ); + if ( p && p->formFields().contains( field ) ) + { + foundPage = static_cast< int >( pageIdx ); + break; + } + } + return foundPage; +} + +void DocumentPrivate::executeScriptEvent( std::shared_ptr< Event > event, const Okular::ScriptAction * linkscript ) +{ + if ( !m_scripter ) + { + m_scripter = new Scripter( this ); + } + m_scripter->setEvent( event.get() ); + m_scripter->execute( linkscript->scriptType(), linkscript->script() ); + + // Clear out the event after execution + m_scripter->setEvent( nullptr ); +} + + Document::Document( QWidget *widget ) : QObject( nullptr ), d( new DocumentPrivate( this ) ) { @@ -4309,16 +4339,7 @@ void Document::processFormatAction( const Action * action, Okular::FormFieldText } // Lookup the page of the FormFieldText - int foundPage = -1; - for ( uint pageIdx = 0, nPages = pages(); pageIdx < nPages; pageIdx++ ) - { - const Page *p = page( pageIdx ); - if ( p && p->formFields().contains( fft ) ) - { - foundPage = static_cast< int >( pageIdx ); - break; - } - } + int foundPage = d->findFieldPageNumber( fft ); if ( foundPage == -1 ) { @@ -4331,15 +4352,8 @@ void Document::processFormatAction( const Action * action, Okular::FormFieldText std::shared_ptr< Event > event = Event::createFormatEvent( fft, d->m_pagesVector[foundPage] ); const ScriptAction * linkscript = static_cast< const ScriptAction * >( action ); - if ( !d->m_scripter ) - { - d->m_scripter = new Scripter( d ); - } - d->m_scripter->setEvent( event.get() ); - d->m_scripter->execute( linkscript->scriptType(), linkscript->script() ); - // Clear out the event after execution - d->m_scripter->setEvent( nullptr ); + d->executeScriptEvent( event, linkscript ); const QString formattedText = event->value().toString(); if ( formattedText != unformattedText ) @@ -4373,16 +4387,7 @@ void Document::processKeystrokeAction( const Action * action, Okular::FormFieldT return; } // Lookup the page of the FormFieldText - int foundPage = -1; - for ( uint pageIdx = 0, nPages = pages(); pageIdx < nPages; pageIdx++ ) - { - const Page *p = page( pageIdx ); - if ( p && p->formFields().contains( fft ) ) - { - foundPage = static_cast< int >( pageIdx ); - break; - } - } + int foundPage = d->findFieldPageNumber( fft ); if ( foundPage == -1 ) { @@ -4393,15 +4398,9 @@ void Document::processKeystrokeAction( const Action * action, Okular::FormFieldT std::shared_ptr< Event > event = Event::createKeystrokeEvent( fft, d->m_pagesVector[foundPage] ); const ScriptAction * linkscript = static_cast< const ScriptAction * >( action ); - if ( !d->m_scripter ) - { - d->m_scripter = new Scripter( d ); - } - d->m_scripter->setEvent( event.get() ); - d->m_scripter->execute( linkscript->scriptType(), linkscript->script() ); - // Clear out the event after execution - d->m_scripter->setEvent( nullptr ); + d->executeScriptEvent( event, linkscript ); + returnCode = event->returnCode(); } @@ -4410,17 +4409,8 @@ void Document::processFocusAction( const Action * action, Okular::FormField *fie if ( !action || action->actionType() != Action::Script ) return; - // Lookup the page of the FormField - int foundPage = -1; - for ( uint pageIdx = 0, nPages = pages(); pageIdx < nPages; pageIdx++ ) - { - const Page *p = page( pageIdx ); - if ( p && p->formFields().contains( field ) ) - { - foundPage = static_cast< int >( pageIdx ); - break; - } - } + // Lookup the page of the FormFieldText + int foundPage = d->findFieldPageNumber( field ); if ( foundPage == -1 ) { @@ -4431,15 +4421,8 @@ void Document::processFocusAction( const Action * action, Okular::FormField *fie std::shared_ptr< Event > event = Event::createFormFocusEvent( field, d->m_pagesVector[foundPage] ); const ScriptAction * linkscript = static_cast< const ScriptAction * >( action ); - if ( !d->m_scripter ) - { - d->m_scripter = new Scripter( d ); - } - d->m_scripter->setEvent( event.get() ); - d->m_scripter->execute( linkscript->scriptType(), linkscript->script() ); - // Clear out the event after execution - d->m_scripter->setEvent( nullptr ); + d->executeScriptEvent( event, linkscript ); } void Document::processValidateAction( const Action * action, Okular::FormFieldText *fft, bool &returnCode ) @@ -4448,16 +4431,7 @@ void Document::processValidateAction( const Action * action, Okular::FormFieldTe return; // Lookup the page of the FormFieldText - int foundPage = -1; - for ( uint pageIdx = 0, nPages = pages(); pageIdx < nPages; pageIdx++ ) - { - const Page *p = page( pageIdx ); - if ( p && p->formFields().contains( fft ) ) - { - foundPage = static_cast< int >( pageIdx ); - break; - } - } + int foundPage = d->findFieldPageNumber( fft ); if ( foundPage == -1 ) { @@ -4468,15 +4442,8 @@ void Document::processValidateAction( const Action * action, Okular::FormFieldTe std::shared_ptr< Event > event = Event::createFormValidateEvent( fft, d->m_pagesVector[foundPage] ); const ScriptAction * linkscript = static_cast< const ScriptAction * >( action ); - if ( !d->m_scripter ) - { - d->m_scripter = new Scripter( d ); - } - d->m_scripter->setEvent( event.get() ); - d->m_scripter->execute( linkscript->scriptType(), linkscript->script() ); - // Clear out the event after execution - d->m_scripter->setEvent( nullptr ); + d->executeScriptEvent( event, linkscript ); returnCode = event->returnCode(); } diff --git a/core/document_p.h b/core/document_p.h index 770a71f01..f9185907a 100644 --- a/core/document_p.h +++ b/core/document_p.h @@ -12,8 +12,10 @@ #define _OKULAR_DOCUMENT_P_H_ #include "document.h" +#include "script/event_p.h" #include "synctex/synctex_parser.h" +#include // qt/kde/system includes #include @@ -41,6 +43,7 @@ struct ArchiveData; struct RunningSearch; namespace Okular { +class ScriptAction; class ConfigInterface; class PageController; class SaveInterface; @@ -227,6 +230,17 @@ class DocumentPrivate void clearAndWaitForRequests(); + + /* + * Executes a ScriptAction with the event passed as parameter. + */ + void executeScriptEvent( std::shared_ptr< Event > event, const Okular::ScriptAction * linkscript ); + + /* + * Find the corresponding page number for the form field passed as parameter. + */ + int findFieldPageNumber( Okular::FormField *field ); + // member variables Document *m_parent; QPointer m_widget;