Commit f31eb3cb authored by Joao Oliveira's avatar Joao Oliveira Committed by Joao Oliveira

Created private functions to avoid duplicated code in Document

parent 17814fc3
Pipeline #6953 passed with stage
in 12 minutes and 31 seconds
......@@ -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();
}
......
......@@ -12,8 +12,10 @@
#define _OKULAR_DOCUMENT_P_H_
#include "document.h"
#include "script/event_p.h"
#include "synctex/synctex_parser.h"
#include <memory>
// qt/kde/system includes
#include <QHash>
......@@ -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<QWidget> m_widget;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment