Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
|
|
|
|
* Copyright (C) 2008 by Harri Porten <porten@kde.org> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "kjs_field_p.h"
|
|
|
|
|
|
|
|
#include <kjs/kjsinterpreter.h>
|
|
|
|
#include <kjs/kjsprototype.h>
|
|
|
|
#include <kjs/kjsarguments.h>
|
|
|
|
|
|
|
|
#include <qhash.h>
|
|
|
|
|
2014-09-11 19:12:27 +00:00
|
|
|
#include <QtCore/QDebug>
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
|
|
|
|
#include "../debug_p.h"
|
|
|
|
#include "../document_p.h"
|
|
|
|
#include "../form.h"
|
|
|
|
#include "../page.h"
|
2017-03-02 23:36:08 +00:00
|
|
|
#include "../page_p.h"
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
|
|
|
|
using namespace Okular;
|
|
|
|
|
|
|
|
static KJSPrototype *g_fieldProto;
|
|
|
|
|
2017-03-02 23:36:08 +00:00
|
|
|
typedef QHash< FormField *, Page * > FormCache;
|
|
|
|
Q_GLOBAL_STATIC( FormCache, g_fieldCache )
|
|
|
|
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
// Field.doc
|
|
|
|
static KJSObject fieldGetDoc( KJSContext *context, void * )
|
|
|
|
{
|
|
|
|
return context->interpreter().globalObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field.name
|
|
|
|
static KJSObject fieldGetName( KJSContext *, void *object )
|
|
|
|
{
|
|
|
|
const FormField *field = reinterpret_cast< FormField * >( object );
|
|
|
|
return KJSString( field->name() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field.readonly (getter)
|
|
|
|
static KJSObject fieldGetReadOnly( KJSContext *, void *object )
|
|
|
|
{
|
|
|
|
const FormField *field = reinterpret_cast< FormField * >( object );
|
|
|
|
return KJSBoolean( field->isReadOnly() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field.readonly (setter)
|
|
|
|
static void fieldSetReadOnly( KJSContext *context, void *object, KJSObject value )
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
FormField *field = reinterpret_cast< FormField * >( object );
|
|
|
|
bool b = value.toBoolean( context );
|
|
|
|
field->setReadOnly( b );
|
|
|
|
#else
|
|
|
|
Q_UNUSED( context );
|
|
|
|
Q_UNUSED( object );
|
|
|
|
Q_UNUSED( value );
|
2014-09-11 17:36:01 +00:00
|
|
|
qCDebug(OkularCoreDebug) << "Not implemented: setting readonly property";
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString fieldGetTypeHelper( const FormField *field )
|
|
|
|
{
|
|
|
|
switch ( field->type() )
|
|
|
|
{
|
|
|
|
case FormField::FormButton:
|
|
|
|
{
|
|
|
|
const FormFieldButton *button = static_cast< const FormFieldButton * >( field );
|
|
|
|
switch ( button->buttonType() )
|
|
|
|
{
|
|
|
|
case FormFieldButton::Push:
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringLiteral("button");
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
case FormFieldButton::CheckBox:
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringLiteral("checkbox");
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
case FormFieldButton::Radio:
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringLiteral("radiobutton");
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FormField::FormText:
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringLiteral("text");
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
case FormField::FormChoice:
|
|
|
|
{
|
|
|
|
const FormFieldChoice *choice = static_cast< const FormFieldChoice * >( field );
|
|
|
|
switch ( choice->choiceType() )
|
|
|
|
{
|
|
|
|
case FormFieldChoice::ComboBox:
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringLiteral("combobox");
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
case FormFieldChoice::ListBox:
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringLiteral("listbox");
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FormField::FormSignature:
|
2015-10-29 12:37:11 +00:00
|
|
|
return QStringLiteral("signature");
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field.type
|
|
|
|
static KJSObject fieldGetType( KJSContext *, void *object )
|
|
|
|
{
|
|
|
|
const FormField *field = reinterpret_cast< FormField * >( object );
|
|
|
|
|
|
|
|
return KJSString( fieldGetTypeHelper( field ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field.value (getter)
|
2017-03-02 23:36:08 +00:00
|
|
|
static KJSObject fieldGetValue( KJSContext */*context*/, void *object )
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
{
|
|
|
|
FormField *field = reinterpret_cast< FormField * >( object );
|
|
|
|
|
|
|
|
switch ( field->type() )
|
|
|
|
{
|
|
|
|
case FormField::FormButton:
|
|
|
|
{
|
|
|
|
const FormFieldButton *button = static_cast< const FormFieldButton * >( field );
|
|
|
|
Q_UNUSED( button ); // ###
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FormField::FormText:
|
|
|
|
{
|
|
|
|
const FormFieldText *text = static_cast< const FormFieldText * >( field );
|
|
|
|
return KJSString( text->text() );
|
|
|
|
}
|
|
|
|
case FormField::FormChoice:
|
|
|
|
{
|
|
|
|
const FormFieldChoice *choice = static_cast< const FormFieldChoice * >( field );
|
|
|
|
Q_UNUSED( choice ); // ###
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FormField::FormSignature:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KJSUndefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field.value (setter)
|
|
|
|
static void fieldSetValue( KJSContext *context, void *object, KJSObject value )
|
|
|
|
{
|
|
|
|
FormField *field = reinterpret_cast< FormField * >( object );
|
|
|
|
|
|
|
|
switch ( field->type() )
|
|
|
|
{
|
|
|
|
case FormField::FormButton:
|
|
|
|
{
|
|
|
|
FormFieldButton *button = static_cast< FormFieldButton * >( field );
|
|
|
|
Q_UNUSED( button ); // ###
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FormField::FormText:
|
|
|
|
{
|
2017-03-02 23:36:08 +00:00
|
|
|
FormFieldText *textField = static_cast< FormFieldText * >( field );
|
|
|
|
const QString text = value.toString( context );
|
|
|
|
if ( text != textField->text() )
|
|
|
|
{
|
|
|
|
textField->setText( text );
|
|
|
|
|
|
|
|
Page *page = g_fieldCache->value( field );
|
|
|
|
if (page)
|
|
|
|
{
|
|
|
|
Document *doc = PagePrivate::get( page )->m_doc->m_parent;
|
|
|
|
QMetaObject::invokeMethod( doc, "refreshPixmaps", Qt::QueuedConnection, Q_ARG( int, page->number() ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning() << "Could not get page of field" << field;
|
|
|
|
}
|
|
|
|
}
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FormField::FormChoice:
|
|
|
|
{
|
|
|
|
FormFieldChoice *choice = static_cast< FormFieldChoice * >( field );
|
|
|
|
Q_UNUSED( choice ); // ###
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FormField::FormSignature:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSField::initType( KJSContext *ctx )
|
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
if ( initialized )
|
|
|
|
return;
|
|
|
|
initialized = true;
|
|
|
|
|
|
|
|
if ( !g_fieldProto )
|
|
|
|
g_fieldProto = new KJSPrototype();
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
g_fieldProto->defineProperty( ctx, QStringLiteral("doc"), fieldGetDoc );
|
|
|
|
g_fieldProto->defineProperty( ctx, QStringLiteral("name"), fieldGetName );
|
|
|
|
g_fieldProto->defineProperty( ctx, QStringLiteral("readonly"),
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
fieldGetReadOnly, fieldSetReadOnly );
|
2015-10-29 12:37:11 +00:00
|
|
|
g_fieldProto->defineProperty( ctx, QStringLiteral("type"), fieldGetType );
|
|
|
|
g_fieldProto->defineProperty( ctx, QStringLiteral("value"), fieldGetValue, fieldSetValue );
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KJSObject JSField::wrapField( KJSContext *ctx, FormField *field, Page *page )
|
|
|
|
{
|
|
|
|
// ### cache unique wrapper
|
|
|
|
KJSObject f = g_fieldProto->constructObject( ctx, field );
|
2015-10-29 12:37:11 +00:00
|
|
|
f.setProperty( ctx, QStringLiteral("page"), page->number() );
|
2017-03-02 23:36:08 +00:00
|
|
|
g_fieldCache->insert( field, page );
|
Start implementing the JavaScript scripting API for PDF documents,
thanks to Harri and Maksim and the brand-new public KJS API.
This starts the implementation of some of the API objects needed,
like App, Console, Data, Document, Field, FullScreen, Spell, Util.
Also, workaround the fact that we cannot change the read-only status for form fields.
svn path=/trunk/KDE/kdegraphics/okular/; revision=809259
2008-05-18 16:37:07 +00:00
|
|
|
return f;
|
|
|
|
}
|
2017-03-02 23:36:08 +00:00
|
|
|
|
|
|
|
void JSField::clearCachedFields()
|
|
|
|
{
|
|
|
|
if ( g_fieldCache.exists() )
|
|
|
|
{
|
|
|
|
g_fieldCache->clear();
|
|
|
|
}
|
|
|
|
}
|