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

Added support for Form Focus Events

parent 9fcd3359
......@@ -31,6 +31,8 @@ private slots:
void testTimeFormat_data();
void testSpecialFormat();
void testSpecialFormat_data();
void testFocusAction();
void testFocusAction_data();
private:
Okular::Document *m_document;
......@@ -136,6 +138,22 @@ void FormatTest::testSpecialFormat_data()
QTest::newRow( "field invalid SSN" ) << QStringLiteral( "CPF" ) << QStringLiteral( "1234567890" ) << false << QStringLiteral( "123-45-6789" );
}
void FormatTest::testFocusAction()
{
QFETCH( QString, result );
Okular::FormFieldText *fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ "Validate/Focus" ] );
m_document->processFocusAction( fft->additionalAction( Okular::Annotation::FocusIn ), fft );
QCOMPARE( fft->text(), result );
}
void FormatTest::testFocusAction_data()
{
QTest::addColumn< QString >( "result" );
QTest::newRow( "when focuses" ) << QStringLiteral( "No" );
}
void FormatTest::cleanupTestCase()
{
m_document->closeDocument();
......
......@@ -4405,6 +4405,43 @@ void Document::processKeystrokeAction( const Action * action, Okular::FormFieldT
returnCode = event->returnCode();
}
void Document::processFocusAction( const Action * action, Okular::FormField *field )
{
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;
}
}
if ( foundPage == -1 )
{
qCDebug( OkularCoreDebug ) << "Could not find page for formfield!";
return;
}
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 );
}
void Document::processSourceReference( const SourceReference * ref )
{
if ( !ref )
......@@ -5379,7 +5416,7 @@ void DocumentPrivate::setRotationInternal( int r, bool notify )
{
Rotation rotation = (Rotation)r;
if ( !m_generator || ( m_rotation == rotation ) )
return;
return;
// tell the pages to rotate
QVector< Okular::Page * >::const_iterator pIt = m_pagesVector.constBegin();
......
......@@ -682,6 +682,13 @@ class OKULARCORE_EXPORT Document : public QObject
*/
void processKeystrokeAction( const Action *action, Okular::FormFieldText *field, bool &returnCode );
/**
* Processes the given focus action on the field.
*
* @since 1.9
*/
void processFocusAction( const Action *action, Okular::FormField *field );
/**
* Returns a list of the bookmarked.pages
*/
......
......@@ -7,6 +7,7 @@
* (at your option) any later version. *
***************************************************************************/
#include <QApplication>
#include "event_p.h"
#include "../form.h"
......@@ -21,7 +22,8 @@ class Event::Private
m_source( nullptr ),
m_sourcePage( nullptr ),
m_eventType( eventType ),
m_returnCode( false )
m_returnCode( false ),
m_shiftModifier( false )
{
}
......@@ -33,6 +35,7 @@ class Event::Private
QString m_targetName;
QVariant m_value;
bool m_returnCode;
bool m_shiftModifier;
};
Event::Event(): d( new Private( UnknownEvent ) )
......@@ -59,6 +62,8 @@ QString Event::name() const
return QStringLiteral( "Format" );
case ( FieldKeystroke ):
return QStringLiteral( "Keystroke" );
case ( FieldFocus ):
return QStringLiteral( "Focus" );
case ( UnknownEvent ):
default:
return QStringLiteral( "Unknown" );
......@@ -72,6 +77,7 @@ QString Event::type() const
case ( FieldCalculate ):
case ( FieldFormat ):
case ( FieldKeystroke ):
case ( FieldFocus ):
return QStringLiteral( "Field" );
case ( UnknownEvent ):
default:
......@@ -154,6 +160,16 @@ void Event::setReturnCode( bool returnCode )
d->m_returnCode = returnCode;
}
bool Event::shiftModifier() const
{
return d->m_shiftModifier;
}
void Event::setShiftModifier( bool shiftModifier )
{
d->m_shiftModifier = shiftModifier;
}
// static
std::shared_ptr<Event> Event::createFormCalculateEvent( FormField *target,
Page *targetPage,
......@@ -209,3 +225,21 @@ std::shared_ptr<Event> Event::createKeystrokeEvent( FormField *target, Page *tar
}
return ret;
}
std::shared_ptr<Event> Event::createFormFocusEvent( FormField *target,
Page *targetPage,
const QString &targetName )
{
std::shared_ptr<Event> ret( new Event( Event::FieldFocus ) );
ret->setTarget( target );
ret->setTargetPage( targetPage );
ret->setTargetName( targetName );
ret->setShiftModifier( QApplication::keyboardModifiers() & Qt::ShiftModifier );
FormFieldText *fft = dynamic_cast< FormFieldText * >(target);
if ( fft )
{
ret->setValue( QVariant( fft->text() ) );
}
return ret;
}
\ No newline at end of file
......@@ -56,7 +56,7 @@ class Event
ExternalExec, /// < Not implemented.
FieldBlur, /// < Not implemented.
FieldCalculate, /// < This event is defined in a field re-calculation.
FieldFocus, /// < Not implemented.
FieldFocus, /// < This event is defined when the field gains or loses focus.
FieldFormat, /// < When a format action is executed
FieldKeystroke, /// < Checks if the entered value is valid.
FieldMouseDown, /// < Not implemented.
......@@ -100,6 +100,10 @@ class Event
bool returnCode() const;
void setReturnCode(bool returnCode);
// Checks if the shift key was down when creating the event.
bool shiftModifier() const;
void setShiftModifier(bool shiftModifier);
static std::shared_ptr<Event> createFormCalculateEvent( FormField *target,
Page *targetPage,
FormField *source = nullptr,
......@@ -108,6 +112,9 @@ class Event
static std::shared_ptr<Event> createFormatEvent( FormField *target, Page *targetPage,
const QString &targetName = QString() );
static std::shared_ptr<Event> createKeystrokeEvent( FormField *target, Page *targetPage );
static std::shared_ptr<Event> createFormFocusEvent( FormField *target,
Page *targetPage,
const QString &targetName = QString() );
private:
class Private;
std::shared_ptr<Private> d;
......
......@@ -49,6 +49,13 @@ static void eventSetTargetName( KJSContext *ctx, void *object, KJSObject value )
event->setTargetName ( value.toString ( ctx ) );
}
// Event.shift
static KJSObject eventGetShift( KJSContext *, void *object )
{
const Event *event = reinterpret_cast< Event * >( object );
return KJSBoolean( event->shiftModifier() );
}
// Event.source
static KJSObject eventGetSource( KJSContext *ctx, void *object )
{
......@@ -71,6 +78,7 @@ static KJSObject eventGetTarget( KJSContext *ctx, void *object )
case Event::FieldCalculate:
case Event::FieldFormat:
case Event::FieldKeystroke:
case Event::FieldFocus:
{
FormField *target = static_cast< FormField * >( event->target() );
if ( target )
......@@ -123,6 +131,7 @@ void JSEvent::initType( KJSContext *ctx )
g_eventProto->defineProperty( ctx, QStringLiteral( "type" ), eventGetType );
g_eventProto->defineProperty( ctx, QStringLiteral( "targetName" ), eventGetTargetName,
eventSetTargetName );
g_eventProto->defineProperty( ctx, QStringLiteral( "shift" ), eventGetShift );
g_eventProto->defineProperty( ctx, QStringLiteral( "source" ), eventGetSource );
g_eventProto->defineProperty( ctx, QStringLiteral( "target" ), eventGetTarget );
g_eventProto->defineProperty( ctx, QStringLiteral( "value" ), eventGetValue, eventSetValue );
......
......@@ -28,6 +28,7 @@
#include <QPainter>
// local includes
#include "core/action.h"
#include "core/form.h"
#include "core/document.h"
#include "debug_ui.h"
......@@ -507,6 +508,8 @@ bool FormLineEdit::event( QEvent* e )
const auto fft = static_cast< Okular::FormFieldText * > ( m_ff );
setText( fft->text() );
m_editing = true;
if( const Okular::Action *action = m_ff->additionalAction( Okular::Annotation::FocusIn ) )
emit m_controller->focusAction( action, fft );
}
else if ( e->type() == QEvent::FocusOut )
{
......
......@@ -14,6 +14,7 @@
#define _OKULAR_FORMWIDGETS_H_
#include "core/area.h"
#include "core/form.h"
#include <qcheckbox.h>
#include <qcombobox.h>
......@@ -116,6 +117,8 @@ class FormWidgetsController : public QObject
void action( Okular::Action *action );
void focusAction( const Okular::Action *action, Okular::FormFieldText *ff );
void formatAction( const Okular::Action *action, Okular::FormFieldText *ff );
void keystrokeAction( const Okular::Action *action, Okular::FormFieldText *ff, bool &ok );
......
......@@ -272,6 +272,10 @@ FormWidgetsController* PageViewPrivate::formWidgetsController()
q, [this] (const Okular::Action *action, Okular::FormFieldText *fft, bool &ok ) {
document->processKeystrokeAction( action, fft, ok );
} );
QObject::connect( formsWidgetController, &FormWidgetsController::focusAction,
q, [this] (const Okular::Action *action, Okular::FormFieldText *fft ) {
document->processFocusAction( action, fft );
} );
}
return formsWidgetController;
......
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