From 51980dd07efdaf837f9f043db988536b84836592 Mon Sep 17 00:00:00 2001 From: Pratham Gandhi Date: Wed, 5 Jun 2024 02:29:11 +0530 Subject: [PATCH] Implemented DocOpen event and fixed doc() function of JSField --- core/document.cpp | 4 +++- core/script/event.cpp | 11 +++++++++++ core/script/event_p.h | 3 ++- core/script/js_event.cpp | 4 ++++ core/script/js_field.cpp | 2 +- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/core/document.cpp b/core/document.cpp index 1edb635fe..344efcae6 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -2562,7 +2562,9 @@ Document::OpenResult Document::openDocument(const QString &docFile, const QUrl & if (!docScripts.isEmpty()) { d->m_scripter = new Scripter(d); for (const QString &docscript : docScripts) { - d->m_scripter->execute(JavaScript, docscript); + const Okular::ScriptAction *linkScript = new Okular::ScriptAction(Okular::JavaScript, docscript); + std::shared_ptr event = Event::createDocEvent(Event::DocOpen); + d->executeScriptEvent(event, linkScript); } } diff --git a/core/script/event.cpp b/core/script/event.cpp index 6c4feaaa3..4fc10ae17 100644 --- a/core/script/event.cpp +++ b/core/script/event.cpp @@ -73,6 +73,8 @@ QString Event::name() const return QStringLiteral("Validate"); case FieldMouseUp: return QStringLiteral("MouseUp"); + case DocOpen: + return QStringLiteral("Open"); case UnknownEvent: default: return QStringLiteral("Unknown"); @@ -89,6 +91,8 @@ QString Event::type() const case FieldValidate: case FieldMouseUp: return QStringLiteral("Field"); + case DocOpen: + return QStringLiteral("Doc"); case UnknownEvent: default: return QStringLiteral("Unknown"); @@ -305,3 +309,10 @@ std::shared_ptr Event::createFieldMouseUpEvent(FormField *target, Page *t ret->setShiftModifier(QApplication::keyboardModifiers() & Qt::ShiftModifier); return ret; } + +std::shared_ptr Event::createDocEvent(Event::EventType type) +{ + std::shared_ptr ret = std::make_shared(type); + // TODO set target name. + return ret; +} \ No newline at end of file diff --git a/core/script/event_p.h b/core/script/event_p.h index 03b5b8c40..2e2157263 100644 --- a/core/script/event_p.h +++ b/core/script/event_p.h @@ -46,7 +46,7 @@ public: ConsoleExec, /// < Not implemented. DocDidPrint, /// < Not implemented. DocDidSave, /// < Not implemented. - DocOpen, /// < Not implemented. + DocOpen, /// < This event is triggered when a document is opened. The document level script functions are scanned and any exposed scripts are executed DocWillClose, /// < Not implemented. DocWillPrint, /// < Not implemented. DocWillSave, /// < Not implemented. @@ -123,6 +123,7 @@ public: static std::shared_ptr createFormFocusEvent(FormField *target, Page *targetPage, const QString &targetName = QString()); static std::shared_ptr createFormValidateEvent(FormField *target, Page *targetPage, const QString &targetName = QString()); static std::shared_ptr createFieldMouseUpEvent(FormField *target, Page *targetPage); + static std::shared_ptr createDocEvent(Event::EventType type); private: class Private; diff --git a/core/script/js_event.cpp b/core/script/js_event.cpp index 49a1c39f4..3135c9dbf 100644 --- a/core/script/js_event.cpp +++ b/core/script/js_event.cpp @@ -71,6 +71,10 @@ QJSValue JSEvent::target() const } break; } + case Event::DocOpen: { + return qjsEngine(this)->globalObject().property(QStringLiteral("Doc")); + break; + } default: { } } diff --git a/core/script/js_field.cpp b/core/script/js_field.cpp index 25b5d6923..e3653495b 100644 --- a/core/script/js_field.cpp +++ b/core/script/js_field.cpp @@ -45,7 +45,7 @@ static void updateField(FormField *field) // Field.doc QJSValue JSField::doc() const { - return qjsEngine(this)->globalObject(); + return qjsEngine(this)->globalObject().property(QStringLiteral("Doc")); } // Field.name