From 6c8cafbb1c613c9567dc67f63a63fef08c607e75 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Fri, 4 Feb 2022 16:52:29 +0100 Subject: [PATCH] Simplify keystroke event handling Make it more similar to how we handle the format event. We pass the new value to processKeystrokeAction, that calls the script and sets the new value for the form object according to the result. If needed the widget is refreshed to reflect the new text --- autotests/formattest.cpp | 21 ++++++++------------- core/document.cpp | 14 ++++++++++++-- core/document.h | 2 +- part/formwidgets.cpp | 30 ++++++------------------------ 4 files changed, 27 insertions(+), 40 deletions(-) diff --git a/autotests/formattest.cpp b/autotests/formattest.cpp index f905ae4b0..2fba58b33 100644 --- a/autotests/formattest.cpp +++ b/autotests/formattest.cpp @@ -105,35 +105,30 @@ void FormatTest::testSpecialFormat() m_formattedText = QLatin1String(""); QFETCH(QString, fieldName); QFETCH(QString, text); - QFETCH(bool, edited); QFETCH(QString, result); Okular::FormFieldText *fft = reinterpret_cast(m_fields[fieldName]); fft->setText(text); - bool ok = false; m_document->processFormatAction(fft->additionalAction(Okular::FormField::FormatField), fft); - m_document->processKeystrokeAction(fft->additionalAction(Okular::FormField::FieldModified), fft, ok); QCOMPARE(m_formattedText, result); - QCOMPARE(ok, edited); } void FormatTest::testSpecialFormat_data() { QTest::addColumn("fieldName"); QTest::addColumn("text"); - QTest::addColumn("edited"); QTest::addColumn("result"); // The tests which have invalid edited, keep the same value as when it was formatted before. - QTest::newRow("field validated but not changed") << QStringLiteral("CEP") << QStringLiteral("12345") << true << QString(QLatin1String("")); - QTest::newRow("field invalid but not changed") << QStringLiteral("CEP") << QStringLiteral("123456") << false << QString(QLatin1String("")); - QTest::newRow("field formatted and changed") << QStringLiteral("8Digits") << QStringLiteral("123456789") << true << QStringLiteral("12345-6789"); - QTest::newRow("field invalid 10 digits") << QStringLiteral("8Digits") << QStringLiteral("1234567890") << false << QStringLiteral("12345-6789"); - QTest::newRow("field formatted telephone") << QStringLiteral("telefone") << QStringLiteral("1234567890") << true << QStringLiteral("(123) 456-7890"); - QTest::newRow("field invalid telephone") << QStringLiteral("telefone") << QStringLiteral("12345678900") << false << QStringLiteral("(123) 456-7890"); - QTest::newRow("field formatted SSN") << QStringLiteral("CPF") << QStringLiteral("123456789") << true << QStringLiteral("123-45-6789"); - QTest::newRow("field invalid SSN") << QStringLiteral("CPF") << QStringLiteral("1234567890") << false << QStringLiteral("123-45-6789"); + QTest::newRow("field validated but not changed") << QStringLiteral("CEP") << QStringLiteral("12345") << QString(QLatin1String("")); + QTest::newRow("field invalid but not changed") << QStringLiteral("CEP") << QStringLiteral("123456") << QString(QLatin1String("")); + QTest::newRow("field formatted and changed") << QStringLiteral("8Digits") << QStringLiteral("123456789") << QStringLiteral("12345-6789"); + QTest::newRow("field invalid 10 digits") << QStringLiteral("8Digits") << QStringLiteral("1234567890") << QStringLiteral("12345-6789"); + QTest::newRow("field formatted telephone") << QStringLiteral("telefone") << QStringLiteral("1234567890") << QStringLiteral("(123) 456-7890"); + QTest::newRow("field invalid telephone") << QStringLiteral("telefone") << QStringLiteral("12345678900") << QStringLiteral("(123) 456-7890"); + QTest::newRow("field formatted SSN") << QStringLiteral("CPF") << QStringLiteral("123456789") << QStringLiteral("123-45-6789"); + QTest::newRow("field invalid SSN") << QStringLiteral("CPF") << QStringLiteral("1234567890") << QStringLiteral("123-45-6789"); } void FormatTest::testFocusAction() diff --git a/core/document.cpp b/core/document.cpp index 2b44c54d0..61a84b515 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -4132,7 +4132,7 @@ void Document::processFormatAction(const Action *action, Okular::FormFieldText * } } -void Document::processKeystrokeAction(const Action *action, Okular::FormFieldText *fft, bool &returnCode) +void Document::processKeystrokeAction(const Action *action, Okular::FormFieldText *fft, const QVariant &newValue) { if (action->actionType() != Action::Script) { qCDebug(OkularCoreDebug) << "Unsupported action type" << action->actionType() << "for keystroke."; @@ -4147,12 +4147,22 @@ void Document::processKeystrokeAction(const Action *action, Okular::FormFieldTex } std::shared_ptr event = Event::createKeystrokeEvent(fft, d->m_pagesVector[foundPage]); + event->setValue(newValue); const ScriptAction *linkscript = static_cast(action); d->executeScriptEvent(event, linkscript); - returnCode = event->returnCode(); + if (event->returnCode()) { + fft->setText(event->value().toString()); + + // Only refresh the widget to avoid changing the cursor position + if (event->value().toString() != newValue.toString()) { + emit refreshFormWidget(fft); + } + } else { + emit refreshFormWidget(fft); + } } void Document::processFocusAction(const Action *action, Okular::FormField *field) diff --git a/core/document.h b/core/document.h index 9d4cd916f..3de35c8ad 100644 --- a/core/document.h +++ b/core/document.h @@ -686,7 +686,7 @@ public: * * @since 1.9 */ - void processKeystrokeAction(const Action *action, Okular::FormFieldText *fft, bool &returnCode); + void processKeystrokeAction(const Action *action, Okular::FormFieldText *fft, const QVariant &newValue); /** * Processes the given focus action on the field. diff --git a/part/formwidgets.cpp b/part/formwidgets.cpp index 8fa7ac951..9a1b56066 100644 --- a/part/formwidgets.cpp +++ b/part/formwidgets.cpp @@ -544,23 +544,14 @@ void FormLineEdit::contextMenuEvent(QContextMenuEvent *event) void FormLineEdit::slotChanged() { Okular::FormFieldText *form = static_cast(m_ff); - QString contents = text(); int cursorPos = cursorPosition(); if (form->additionalAction(Okular::FormField::FieldModified) && m_editing && !form->isReadOnly()) { - bool ok = false; - QString oldInputText = form->text(); - form->setText(text()); - m_controller->document()->processKeystrokeAction(form->additionalAction(Okular::FormField::FieldModified), form, ok); - form->setText(oldInputText); - if (!ok) { - setText(oldInputText); - return; - } + m_controller->document()->processKeystrokeAction(form->additionalAction(Okular::FormField::FieldModified), form, text()); } - if (contents != form->text()) { - emit m_controller->formTextChangedByWidget(pageItem()->pageNumber(), form, contents, cursorPos, m_prevCursorPos, m_prevAnchorPos); + if (text() != form->text()) { + emit m_controller->formTextChangedByWidget(pageItem()->pageNumber(), form, text(), cursorPos, m_prevCursorPos, m_prevAnchorPos); } m_prevCursorPos = cursorPos; @@ -704,23 +695,14 @@ void TextAreaEdit::slotHandleTextChangedByUndoRedo(int pageNumber, Okular::FormF void TextAreaEdit::slotChanged() { Okular::FormFieldText *form = static_cast(m_ff); - QString contents = toPlainText(); int cursorPos = textCursor().position(); if (form->additionalAction(Okular::FormField::FieldModified) && m_editing && !form->isReadOnly()) { - bool ok = false; - QString oldInputText = form->text(); - form->setText(toPlainText()); - m_controller->document()->processKeystrokeAction(form->additionalAction(Okular::FormField::FieldModified), form, ok); - form->setText(oldInputText); - if (!ok) { - setText(oldInputText); - return; - } + m_controller->document()->processKeystrokeAction(form->additionalAction(Okular::FormField::FieldModified), form, toPlainText()); } - if (contents != form->text()) { - emit m_controller->formTextChangedByWidget(pageItem()->pageNumber(), form, contents, cursorPos, m_prevCursorPos, m_prevAnchorPos); + if (toPlainText() != form->text()) { + emit m_controller->formTextChangedByWidget(pageItem()->pageNumber(), form, toPlainText(), cursorPos, m_prevCursorPos, m_prevAnchorPos); } m_prevCursorPos = cursorPos; m_prevAnchorPos = textCursor().anchor();