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
remotes/origin/work/fhek/foreground_background_text
Nicolas Fella 4 years ago
parent ae8251a145
commit 6c8cafbb1c
  1. 21
      autotests/formattest.cpp
  2. 14
      core/document.cpp
  3. 2
      core/document.h
  4. 30
      part/formwidgets.cpp

@ -105,35 +105,30 @@ void FormatTest::testSpecialFormat()
m_formattedText = QLatin1String(""); m_formattedText = QLatin1String("");
QFETCH(QString, fieldName); QFETCH(QString, fieldName);
QFETCH(QString, text); QFETCH(QString, text);
QFETCH(bool, edited);
QFETCH(QString, result); QFETCH(QString, result);
Okular::FormFieldText *fft = reinterpret_cast<Okular::FormFieldText *>(m_fields[fieldName]); Okular::FormFieldText *fft = reinterpret_cast<Okular::FormFieldText *>(m_fields[fieldName]);
fft->setText(text); fft->setText(text);
bool ok = false;
m_document->processFormatAction(fft->additionalAction(Okular::FormField::FormatField), fft); m_document->processFormatAction(fft->additionalAction(Okular::FormField::FormatField), fft);
m_document->processKeystrokeAction(fft->additionalAction(Okular::FormField::FieldModified), fft, ok);
QCOMPARE(m_formattedText, result); QCOMPARE(m_formattedText, result);
QCOMPARE(ok, edited);
} }
void FormatTest::testSpecialFormat_data() void FormatTest::testSpecialFormat_data()
{ {
QTest::addColumn<QString>("fieldName"); QTest::addColumn<QString>("fieldName");
QTest::addColumn<QString>("text"); QTest::addColumn<QString>("text");
QTest::addColumn<bool>("edited");
QTest::addColumn<QString>("result"); QTest::addColumn<QString>("result");
// The tests which have invalid edited, keep the same value as when it was formatted before. // 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 validated but not changed") << QStringLiteral("CEP") << QStringLiteral("12345") << QString(QLatin1String(""));
QTest::newRow("field invalid but not changed") << QStringLiteral("CEP") << QStringLiteral("123456") << false << 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") << true << QStringLiteral("12345-6789"); QTest::newRow("field formatted and changed") << QStringLiteral("8Digits") << QStringLiteral("123456789") << QStringLiteral("12345-6789");
QTest::newRow("field invalid 10 digits") << QStringLiteral("8Digits") << QStringLiteral("1234567890") << false << 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") << true << QStringLiteral("(123) 456-7890"); QTest::newRow("field formatted telephone") << QStringLiteral("telefone") << QStringLiteral("1234567890") << QStringLiteral("(123) 456-7890");
QTest::newRow("field invalid telephone") << QStringLiteral("telefone") << QStringLiteral("12345678900") << false << 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") << true << QStringLiteral("123-45-6789"); QTest::newRow("field formatted SSN") << QStringLiteral("CPF") << QStringLiteral("123456789") << QStringLiteral("123-45-6789");
QTest::newRow("field invalid SSN") << QStringLiteral("CPF") << QStringLiteral("1234567890") << false << QStringLiteral("123-45-6789"); QTest::newRow("field invalid SSN") << QStringLiteral("CPF") << QStringLiteral("1234567890") << QStringLiteral("123-45-6789");
} }
void FormatTest::testFocusAction() void FormatTest::testFocusAction()

@ -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) { if (action->actionType() != Action::Script) {
qCDebug(OkularCoreDebug) << "Unsupported action type" << action->actionType() << "for keystroke."; 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 = Event::createKeystrokeEvent(fft, d->m_pagesVector[foundPage]); std::shared_ptr<Event> event = Event::createKeystrokeEvent(fft, d->m_pagesVector[foundPage]);
event->setValue(newValue);
const ScriptAction *linkscript = static_cast<const ScriptAction *>(action); const ScriptAction *linkscript = static_cast<const ScriptAction *>(action);
d->executeScriptEvent(event, linkscript); 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) void Document::processFocusAction(const Action *action, Okular::FormField *field)

@ -686,7 +686,7 @@ public:
* *
* @since 1.9 * @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. * Processes the given focus action on the field.

@ -544,23 +544,14 @@ void FormLineEdit::contextMenuEvent(QContextMenuEvent *event)
void FormLineEdit::slotChanged() void FormLineEdit::slotChanged()
{ {
Okular::FormFieldText *form = static_cast<Okular::FormFieldText *>(m_ff); Okular::FormFieldText *form = static_cast<Okular::FormFieldText *>(m_ff);
QString contents = text();
int cursorPos = cursorPosition(); int cursorPos = cursorPosition();
if (form->additionalAction(Okular::FormField::FieldModified) && m_editing && !form->isReadOnly()) { if (form->additionalAction(Okular::FormField::FieldModified) && m_editing && !form->isReadOnly()) {
bool ok = false; m_controller->document()->processKeystrokeAction(form->additionalAction(Okular::FormField::FieldModified), form, text());
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;
}
} }
if (contents != form->text()) { if (text() != form->text()) {
emit m_controller->formTextChangedByWidget(pageItem()->pageNumber(), form, contents, cursorPos, m_prevCursorPos, m_prevAnchorPos); emit m_controller->formTextChangedByWidget(pageItem()->pageNumber(), form, text(), cursorPos, m_prevCursorPos, m_prevAnchorPos);
} }
m_prevCursorPos = cursorPos; m_prevCursorPos = cursorPos;
@ -704,23 +695,14 @@ void TextAreaEdit::slotHandleTextChangedByUndoRedo(int pageNumber, Okular::FormF
void TextAreaEdit::slotChanged() void TextAreaEdit::slotChanged()
{ {
Okular::FormFieldText *form = static_cast<Okular::FormFieldText *>(m_ff); Okular::FormFieldText *form = static_cast<Okular::FormFieldText *>(m_ff);
QString contents = toPlainText();
int cursorPos = textCursor().position(); int cursorPos = textCursor().position();
if (form->additionalAction(Okular::FormField::FieldModified) && m_editing && !form->isReadOnly()) { if (form->additionalAction(Okular::FormField::FieldModified) && m_editing && !form->isReadOnly()) {
bool ok = false; m_controller->document()->processKeystrokeAction(form->additionalAction(Okular::FormField::FieldModified), form, toPlainText());
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;
}
} }
if (contents != form->text()) { if (toPlainText() != form->text()) {
emit m_controller->formTextChangedByWidget(pageItem()->pageNumber(), form, contents, cursorPos, m_prevCursorPos, m_prevAnchorPos); emit m_controller->formTextChangedByWidget(pageItem()->pageNumber(), form, toPlainText(), cursorPos, m_prevCursorPos, m_prevAnchorPos);
} }
m_prevCursorPos = cursorPos; m_prevCursorPos = cursorPos;
m_prevAnchorPos = textCursor().anchor(); m_prevAnchorPos = textCursor().anchor();

Loading…
Cancel
Save