Initial implementation of app.popUpMenuEx

It is defenitely not a full implementation but it's enough for the one
file we have that needs it for now

BUGS: 472764
This commit is contained in:
Albert Astals Cid 2023-08-02 01:25:25 +02:00
parent 0a3f4a78a3
commit 532cb0cdcd
12 changed files with 112 additions and 16 deletions

View file

@ -4509,6 +4509,27 @@ void Document::processValidateAction(const Action *action, Okular::FormFieldText
returnCode = event->returnCode();
}
void Document::processFormMouseUpScripAction(const Action *action, Okular::FormField *ff)
{
if (!action || action->actionType() != Action::Script) {
return;
}
// Lookup the page of the FormFieldText
int foundPage = d->findFieldPageNumber(ff);
if (foundPage == -1) {
qCDebug(OkularCoreDebug) << "Could not find page for formfield!";
return;
}
std::shared_ptr<Event> event = Event::createFieldMouseUpEvent(ff, d->m_pagesVector[foundPage]);
const ScriptAction *linkscript = static_cast<const ScriptAction *>(action);
d->executeScriptEvent(event, linkscript);
}
void Document::processSourceReference(const SourceReference *ref)
{
if (!ref) {

View file

@ -702,6 +702,13 @@ public:
*/
void processValidateAction(const Action *action, Okular::FormFieldText *fft, bool &returnCode);
/**
* Processes the mouse up @p action on @p ff.
*
* @since 23.12
*/
void processFormMouseUpScripAction(const Action *action, Okular::FormField *ff);
/**
* Returns a list of the bookmarked.pages
*/

View file

@ -361,3 +361,7 @@ function AFPercent_Keystroke( nDec, sepStyle )
event.rc = !isNaN(event.change) || event.change == "." || event.change == ","
}
}
app.popUpMenuEx = function() {
return app.okular_popUpMenuEx(arguments);
}

View file

@ -57,17 +57,19 @@ Event::EventType Event::eventType() const
QString Event::name() const
{
switch (d->m_eventType) {
case (FieldCalculate):
case FieldCalculate:
return QStringLiteral("Calculate");
case (FieldFormat):
case FieldFormat:
return QStringLiteral("Format");
case (FieldKeystroke):
case FieldKeystroke:
return QStringLiteral("Keystroke");
case (FieldFocus):
case FieldFocus:
return QStringLiteral("Focus");
case (FieldValidate):
case FieldValidate:
return QStringLiteral("Validate");
case (UnknownEvent):
case FieldMouseUp:
return QStringLiteral("MouseUp");
case UnknownEvent:
default:
return QStringLiteral("Unknown");
}
@ -76,13 +78,14 @@ QString Event::name() const
QString Event::type() const
{
switch (d->m_eventType) {
case (FieldCalculate):
case (FieldFormat):
case (FieldKeystroke):
case (FieldFocus):
case (FieldValidate):
case FieldCalculate:
case FieldFormat:
case FieldKeystroke:
case FieldFocus:
case FieldValidate:
case FieldMouseUp:
return QStringLiteral("Field");
case (UnknownEvent):
case UnknownEvent:
default:
return QStringLiteral("Unknown");
}
@ -269,3 +272,12 @@ std::shared_ptr<Event> Event::createFormValidateEvent(FormField *target, Page *t
}
return ret;
}
std::shared_ptr<Event> Event::createFieldMouseUpEvent(FormField *target, Page *targetPage)
{
std::shared_ptr<Event> ret = std::make_shared<Event>(Event::FieldMouseUp);
ret->setTarget(target);
ret->setTargetPage(targetPage);
ret->setShiftModifier(QApplication::keyboardModifiers() & Qt::ShiftModifier);
return ret;
}

View file

@ -59,7 +59,7 @@ public:
FieldMouseDown, /// < Not implemented.
FieldMouseEnter, /// < Not implemented.
FieldMouseExit, /// < Not implemented.
FieldMouseUp, /// < Not implemented.
FieldMouseUp, /// < This event is the result of a mouse up on a field.
/* Validates the field after every change is committed
* (clicked outside or tabbed to another field).
* The enter event is not handled
@ -116,6 +116,7 @@ public:
static std::shared_ptr<Event> createKeystrokeEvent(FormField *target, Page *targetPage);
static std::shared_ptr<Event> createFormFocusEvent(FormField *target, Page *targetPage, const QString &targetName = QString());
static std::shared_ptr<Event> createFormValidateEvent(FormField *target, Page *targetPage, const QString &targetName = QString());
static std::shared_ptr<Event> createFieldMouseUpEvent(FormField *target, Page *targetPage);
private:
class Private;

View file

@ -15,6 +15,7 @@
#include <KLocalizedString>
#include <QCheckBox>
#include <QJSEngine>
#include <QMenu>
#include <QMessageBox>
#include "../document_p.h"
@ -328,6 +329,33 @@ void JSApp::clearTimeOut(const QJSValue &oTime)
}
}
// app.popUpMenuEx()
QJSValue JSApp::okular_popUpMenuEx(const QJSValue &arguments)
{
static const char *kResultProperty = "result";
const int nArgs = arguments.property(QStringLiteral("length")).toInt();
if (nArgs == 0) {
return {};
}
QMenu m;
for (int i = 0; i < nArgs; ++i) {
const QJSValue item = arguments.property(i);
const QString cName = item.property(QStringLiteral("cName")).toString();
const QJSValue cResultProperty = item.property(QStringLiteral("cResult"));
QAction *a = m.addAction(cName);
if (cResultProperty.isUndefined()) {
a->setProperty(kResultProperty, cName);
} else {
a->setProperty(kResultProperty, cResultProperty.toString());
}
}
QAction *result = m.exec(QCursor::pos());
return result ? result->property(kResultProperty).toString() : QString();
}
JSApp::JSApp(DocumentPrivate *doc, QTimer *watchdogTimer, QObject *parent)
: QObject(parent)
, m_doc(doc)

View file

@ -10,6 +10,7 @@
#include <QJSValue>
#include <QObject>
#include <QPoint>
class QTimer;
@ -57,6 +58,7 @@ public:
Q_INVOKABLE void clearInterval(const QJSValue &oInterval);
Q_INVOKABLE QJSValue setTimeOut(const QString &cExpr, int nMilliseconds);
Q_INVOKABLE void clearTimeOut(const QJSValue &oTime);
Q_INVOKABLE QJSValue okular_popUpMenuEx(const QJSValue &arguments);
private:
QJSValue wrapTimer(QTimer *timer) const;

View file

@ -63,7 +63,8 @@ QJSValue JSEvent::target() const
case Event::FieldFormat:
case Event::FieldKeystroke:
case Event::FieldFocus:
case Event::FieldValidate: {
case Event::FieldValidate:
case Event::FieldMouseUp: {
FormField *target = static_cast<FormField *>(m_event->target());
if (target) {
return JSField::wrapField(qjsEngine(this), target, m_event->targetPage());

View file

@ -73,6 +73,11 @@ void FormWidgetsController::signalAction(Okular::Action *a)
Q_EMIT action(a);
}
void FormWidgetsController::signalMouseUpAction(Okular::Action *action, Okular::FormField *form)
{
Q_EMIT mouseUpAction(action, form);
}
void FormWidgetsController::processScriptAction(Okular::Action *a, Okular::FormField *field, Okular::Annotation::AdditionalActionType type)
{
// If it's not a Action Script or if the field is not a FormText, handle it normally
@ -1211,9 +1216,9 @@ void SignatureEdit::signUnsignedSignature()
} \
Okular::Action *act = m_ff->activationAction(); \
if (act && !qobject_cast<CheckBoxEdit *>(this)) { \
m_controller->signalAction(act); \
m_controller->signalMouseUpAction(act, m_ff); \
} else if ((act = m_ff->additionalAction(Okular::Annotation::MouseReleased))) { \
m_controller->signalAction(act); \
m_controller->signalMouseUpAction(act, m_ff); \
} \
BaseClass::mouseReleaseEvent(event); \
} \

View file

@ -60,6 +60,7 @@ public:
~FormWidgetsController() override;
void signalAction(Okular::Action *action);
void signalMouseUpAction(Okular::Action *action, Okular::FormField *form);
void processScriptAction(Okular::Action *a, Okular::FormField *field, Okular::Annotation::AdditionalActionType type);
@ -94,6 +95,8 @@ Q_SIGNALS:
void action(Okular::Action *action);
void mouseUpAction(Okular::Action *action, Okular::FormField *form);
void refreshFormWidget(Okular::FormField *form);
private Q_SLOTS:

View file

@ -281,6 +281,7 @@ FormWidgetsController *PageViewPrivate::formWidgetsController()
formsWidgetController = new FormWidgetsController(document);
QObject::connect(formsWidgetController, &FormWidgetsController::changed, q, &PageView::slotFormChanged);
QObject::connect(formsWidgetController, &FormWidgetsController::action, q, &PageView::slotAction);
QObject::connect(formsWidgetController, &FormWidgetsController::mouseUpAction, q, &PageView::slotMouseUpAction);
}
return formsWidgetController;
@ -5341,6 +5342,15 @@ void PageView::slotAction(Okular::Action *action)
d->document->processAction(action);
}
void PageView::slotMouseUpAction(Okular::Action *action, Okular::FormField *form)
{
if (form && action->actionType() == Okular::Action::Script) {
d->document->processFormMouseUpScripAction(action, form);
} else {
d->document->processAction(action);
}
}
void PageView::externalKeyPressEvent(QKeyEvent *e)
{
keyPressEvent(e);

View file

@ -36,6 +36,7 @@ namespace Okular
class Action;
class Document;
class DocumentViewport;
class FormField;
class FormFieldSignature;
class Annotation;
class MovieAction;
@ -130,6 +131,7 @@ public Q_SLOTS:
void slotSelectPage();
void slotAction(Okular::Action *action);
void slotMouseUpAction(Okular::Action *action, Okular::FormField *form);
void slotFormChanged(int pageNumber);
void externalKeyPressEvent(QKeyEvent *e);