From d2648237c7e3bfc03077b7b1e63c72dc24ff519c Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 12 Mar 2020 16:41:07 +0100 Subject: Auto-capitalize only after space following a sentence-ending character A dot alone should not change to capital letters, only the space following the dot. This is standard behavior on mobile platforms. In inputmethods that support auto-completion and auto-space-insertion, we need to watch out if the auto-inserted space triggered auto- capitalization. If so, then the inserted text needs to be capitalized even though the keyboard will have shown lower-case characters. Fixes: QTBUG-77673 Change-Id: Icd907055d6557a7756468318fba5669eb8f62a28 Reviewed-by: Mitch Curtis --- .../hunspell/hunspellinputmethod/hunspellinputmethod.cpp | 7 ++++++- src/virtualkeyboard/shifthandler.cpp | 10 +++++----- tests/auto/inputpanel/data/tst_inputpanel.qml | 2 ++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/plugins/hunspell/hunspellinputmethod/hunspellinputmethod.cpp b/src/plugins/hunspell/hunspellinputmethod/hunspellinputmethod.cpp index 884ec9df..e7bb0a69 100644 --- a/src/plugins/hunspell/hunspellinputmethod/hunspellinputmethod.cpp +++ b/src/plugins/hunspell/hunspellinputmethod/hunspellinputmethod.cpp @@ -138,6 +138,7 @@ bool HunspellInputMethod::keyEvent(Qt::Key key, const QString &text, Qt::Keyboar QString word = d->wordCandidates.wordAt(0); bool addToWord = d->isValidInputChar(c) && (!word.isEmpty() || !d->isJoiner(c)); if (addToWord) { + QString newText = text; /* Automatic space insertion. */ if (word.isEmpty()) { QString surroundingText = ic->surroundingText(); @@ -153,7 +154,11 @@ bool HunspellInputMethod::keyEvent(Qt::Key key, const QString &text, Qt::Keyboar if (!lastChar.isSpace() && lastChar != Qt::Key_Minus && d->isAutoSpaceAllowed()) { + // auto-insertion of space might trigger auto-capitalization + bool wasShiftActive = ic->isShiftActive(); ic->commit(QLatin1String(" ")); + if (ic->isShiftActive() && !wasShiftActive) + newText = newText.toUpper(); } } } @@ -162,7 +167,7 @@ bool HunspellInputMethod::keyEvent(Qt::Key key, const QString &text, Qt::Keyboar a selection which the pre-edit text will replace. */ d->ignoreUpdate = word.isEmpty(); - word.append(text); + word.append(newText); d->wordCandidates.updateWord(0, word); ic->setPreeditText(word); d->ignoreUpdate = false; diff --git a/src/virtualkeyboard/shifthandler.cpp b/src/virtualkeyboard/shifthandler.cpp index d268282e..b2d44f95 100644 --- a/src/virtualkeyboard/shifthandler.cpp +++ b/src/virtualkeyboard/shifthandler.cpp @@ -298,14 +298,14 @@ void ShiftHandler::autoCapitalize() bool preferLowerCase = d->inputContext->inputMethodHints() & Qt::ImhPreferLowercase; if (cursorPosition == 0) { setShiftActive(!preferLowerCase); - } else { + } else { // space after sentence-ending character triggers auto-capitalization QString text = d->inputContext->surroundingText(); text.truncate(cursorPosition); - text = text.trimmed(); - if (text.length() == 0) - setShiftActive(!preferLowerCase); - else if (text.length() > 0 && d->sentenceEndingCharacters.indexOf(text[text.length() - 1]) >= 0) + if (text.trimmed().length() == 0) setShiftActive(!preferLowerCase); + else if (text.endsWith(QLatin1Char(' '))) + setShiftActive(d->sentenceEndingCharacters.contains(text.rightRef(2)[0]) + && !preferLowerCase); else setShiftActive(false); } diff --git a/tests/auto/inputpanel/data/tst_inputpanel.qml b/tests/auto/inputpanel/data/tst_inputpanel.qml index d46f29ce..b0119541 100644 --- a/tests/auto/inputpanel/data/tst_inputpanel.qml +++ b/tests/auto/inputpanel/data/tst_inputpanel.qml @@ -424,6 +424,8 @@ Rectangle { return [ { initInputMethodHints: Qt.ImhNoPredictiveText, toggleShiftCount: 0, inputSequence: "aaa bbb", outputText: "Aaa bbb", autoCapitalizationEnabled: true, toggleShiftEnabled: true }, { initInputMethodHints: Qt.ImhNoPredictiveText, toggleShiftCount: 1, inputSequence: "aaa bbb", outputText: "aaa bbb", autoCapitalizationEnabled: true, toggleShiftEnabled: true }, + { initInputMethodHints: Qt.ImhNoPredictiveText, toggleShiftCount: 2, inputSequence: "aaa. bbb", outputText: "Aaa. Bbb", autoCapitalizationEnabled: true, toggleShiftEnabled: true }, + { initInputMethodHints: Qt.ImhNoPredictiveText, toggleShiftCount: 2, inputSequence: "aaa.bbb", outputText: "Aaa.bbb", autoCapitalizationEnabled: true, toggleShiftEnabled: true }, { initInputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase, toggleShiftCount: 0, inputSequence: "aaa bbb", outputText: "aaa bbb", autoCapitalizationEnabled: false, toggleShiftEnabled: true }, { initInputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase, toggleShiftCount: 0, inputSequence: "aaa. bbb", outputText: "aaa. bbb", autoCapitalizationEnabled: false, toggleShiftEnabled: true }, { initInputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase, toggleShiftCount: 1, inputSequence: "aaa bbb", outputText: "Aaa bbb", autoCapitalizationEnabled: false, toggleShiftEnabled: true }, -- cgit v1.2.3