From f410fb06c584fa0e893ab6066ea8b03a5323fe07 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Mon, 7 Jun 2010 14:52:20 +1000 Subject: Remove unnecessary CloseSoftwareInputPanel events after TextEdit or TextInput has lost focus Task-number: Reviewed-by: Warwick Allison --- .../graphicsitems/qdeclarativetextedit.cpp | 27 +++----- .../graphicsitems/qdeclarativetextedit_p.h | 1 - .../graphicsitems/qdeclarativetextinput.cpp | 27 +++----- .../graphicsitems/qdeclarativetextinput_p.h | 1 - .../tst_qdeclarativetextedit.cpp | 73 ++++++++++++++-------- .../tst_qdeclarativetextinput.cpp | 72 +++++++++++++-------- 6 files changed, 109 insertions(+), 92 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index d0e0ad3..c086851 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -1391,9 +1391,9 @@ void QDeclarativeTextEditPrivate::updateDefaultTextOption() your application. By default the opening of input panels follows the platform style. On Symbian^1 and - Symbian^3 -based devices the panels are opened by clicking TextEdit and need to be - manually closed by the user. On other platforms the panels are automatically opened - when TextEdit element gains focus and closed when the focus is lost. + Symbian^3 -based devices the panels are opened by clicking TextEdit. On other platforms + the panels are automatically opened when TextEdit element gains focus. Input panels are + always closed if no editor owns focus. . You can disable the automatic behavior by setting the property \c focusOnPress to false and use functions openSoftwareInputPanel() and closeSoftwareInputPanel() to implement @@ -1415,9 +1415,9 @@ void QDeclarativeTextEditPrivate::updateDefaultTextOption() textEdit.openSoftwareInputPanel(); } else { textEdit.focus = false; - textEdit.closeSoftwareInputPanel(); } } + onPressAndHold: textEdit.closeSoftwareInputPanel(); } } \endcode @@ -1442,9 +1442,9 @@ void QDeclarativeTextEdit::openSoftwareInputPanel() your application. By default the opening of input panels follows the platform style. On Symbian^1 and - Symbian^3 -based devices the panels are opened by clicking TextEdit and need to be - manually closed by the user. On other platforms the panels are automatically opened - when TextEdit element gains focus and closed when the focus is lost. + Symbian^3 -based devices the panels are opened by clicking TextEdit. On other platforms + the panels are automatically opened when TextEdit element gains focus. Input panels are + always closed if no editor owns focus. . You can disable the automatic behavior by setting the property \c focusOnPress to false and use functions openSoftwareInputPanel() and closeSoftwareInputPanel() to implement @@ -1466,9 +1466,9 @@ void QDeclarativeTextEdit::openSoftwareInputPanel() textEdit.openSoftwareInputPanel(); } else { textEdit.focus = false; - textEdit.closeSoftwareInputPanel(); } } + onPressAndHold: textEdit.closeSoftwareInputPanel(); } } \endcode @@ -1496,15 +1496,4 @@ void QDeclarativeTextEdit::focusInEvent(QFocusEvent *event) QDeclarativePaintedItem::focusInEvent(event); } -void QDeclarativeTextEdit::focusOutEvent(QFocusEvent *event) -{ - Q_D(const QDeclarativeTextEdit); - if (d->showInputPanelOnFocus) { - if (d->focusOnPress && !isReadOnly()) { - closeSoftwareInputPanel(); - } - } - QDeclarativePaintedItem::focusOutEvent(event); -} - QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h index 0ecb2f3..d08f607 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h @@ -247,7 +247,6 @@ protected: void keyPressEvent(QKeyEvent *); void keyReleaseEvent(QKeyEvent *); void focusInEvent(QFocusEvent *event); - void focusOutEvent(QFocusEvent *event); // mouse filter? void mousePressEvent(QGraphicsSceneMouseEvent *event); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 3911a97..b877c50 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -1234,9 +1234,9 @@ void QDeclarativeTextInput::moveCursorSelection(int position) your application. By default the opening of input panels follows the platform style. On Symbian^1 and - Symbian^3 -based devices the panels are opened by clicking TextInput and need to be - manually closed by the user. On other platforms the panels are automatically opened - when TextInput element gains focus and closed when the focus is lost. + Symbian^3 -based devices the panels are opened by clicking TextInput. On other platforms + the panels are automatically opened when TextInput element gains focus. Input panels are + always closed if no editor owns focus. . You can disable the automatic behavior by setting the property \c focusOnPress to false and use functions openSoftwareInputPanel() and closeSoftwareInputPanel() to implement @@ -1258,9 +1258,9 @@ void QDeclarativeTextInput::moveCursorSelection(int position) textInput.openSoftwareInputPanel(); } else { textInput.focus = false; - textInput.closeSoftwareInputPanel(); } } + onPressAndHold: textInput.closeSoftwareInputPanel(); } } \endqml @@ -1285,9 +1285,9 @@ void QDeclarativeTextInput::openSoftwareInputPanel() your application. By default the opening of input panels follows the platform style. On Symbian^1 and - Symbian^3 -based devices the panels are opened by clicking TextInput and need to be - manually closed by the user. On other platforms the panels are automatically opened - when TextInput element gains focus and closed when the focus is lost. + Symbian^3 -based devices the panels are opened by clicking TextInput. On other platforms + the panels are automatically opened when TextInput element gains focus. Input panels are + always closed if no editor owns focus. . You can disable the automatic behavior by setting the property \c focusOnPress to false and use functions openSoftwareInputPanel() and closeSoftwareInputPanel() to implement @@ -1309,9 +1309,9 @@ void QDeclarativeTextInput::openSoftwareInputPanel() textInput.openSoftwareInputPanel(); } else { textInput.focus = false; - textInput.closeSoftwareInputPanel(); } } + onPressAndHold: textInput.closeSoftwareInputPanel(); } } \endqml @@ -1340,17 +1340,6 @@ void QDeclarativeTextInput::focusInEvent(QFocusEvent *event) QDeclarativePaintedItem::focusInEvent(event); } -void QDeclarativeTextInput::focusOutEvent(QFocusEvent *event) -{ - Q_D(const QDeclarativeTextInput); - if (d->showInputPanelOnFocus) { - if (d->focusOnPress && !isReadOnly()) { - closeSoftwareInputPanel(); - } - } - QDeclarativePaintedItem::focusOutEvent(event); -} - void QDeclarativeTextInputPrivate::init() { Q_Q(QDeclarativeTextInput); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h index e34634a..c539bd3 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h @@ -224,7 +224,6 @@ protected: void keyPressEvent(QKeyEvent* ev); bool event(QEvent *e); void focusInEvent(QFocusEvent *event); - void focusOutEvent(QFocusEvent *event); public Q_SLOTS: void selectAll(); diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp index 9e5285f..053c9ef 100644 --- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp +++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp @@ -862,6 +862,7 @@ void tst_qdeclarativetextedit::openInputPanelOnClick() edit.setFocus(false); edit.setFocus(true); edit.setFocus(false); + QApplication::processEvents(); QCOMPARE(ic.openInputPanelReceived, false); QCOMPARE(ic.closeInputPanelReceived, false); } @@ -887,6 +888,7 @@ void tst_qdeclarativetextedit::openInputPanelOnFocus() QDeclarativeTextEditPrivate *editPrivate = static_cast(pri); editPrivate->showInputPanelOnFocus = true; + // test default values QVERIFY(edit.focusOnPress()); QCOMPARE(ic.openInputPanelReceived, false); QCOMPARE(ic.closeInputPanelReceived, false); @@ -896,75 +898,94 @@ void tst_qdeclarativetextedit::openInputPanelOnFocus() QApplication::processEvents(); QVERIFY(edit.hasFocus()); QCOMPARE(ic.openInputPanelReceived, true); - QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; // no events on release QTest::mouseRelease(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(edit.scenePos())); QCOMPARE(ic.openInputPanelReceived, false); - QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; - // Even with focus already gained, user needs - // to be able to open panel by pressing on the editor + // if already focused, input panel can be opened on press + QVERIFY(edit.hasFocus()); QTest::mousePress(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(edit.scenePos())); QApplication::processEvents(); QCOMPARE(ic.openInputPanelReceived, true); - QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; - // input panel closed on focus lost - edit.setFocus(false); + // input method should stay enabled if focus + // is lost to an item that also accepts inputs + QDeclarativeTextEdit anotherEdit; + scene.addItem(&anotherEdit); + anotherEdit.setFocus(true); + QApplication::processEvents(); + QCOMPARE(ic.openInputPanelReceived, true); + ic.openInputPanelReceived = false; + QCOMPARE(view.inputContext(), &ic); + QVERIFY(view.testAttribute(Qt::WA_InputMethodEnabled)); + + // input method should be disabled if focus + // is lost to an item that doesn't accept inputs + QDeclarativeItem item; + scene.addItem(&item); + item.setFocus(true); QApplication::processEvents(); QCOMPARE(ic.openInputPanelReceived, false); - QCOMPARE(ic.closeInputPanelReceived, true); - ic.closeInputPanelReceived = false; + QVERIFY(view.inputContext() == 0); + QVERIFY(!view.testAttribute(Qt::WA_InputMethodEnabled)); - // no automatic input panel events if focusOnPress is false + // no automatic input panel events should + // be sent if focusOnPress is false + edit.setFocusOnPress(false); + QCOMPARE(focusOnPressSpy.count(),1); edit.setFocusOnPress(false); QCOMPARE(focusOnPressSpy.count(),1); - QTest::mousePress(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(edit.scenePos())); - QTest::mouseRelease(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(edit.scenePos())); edit.setFocus(false); edit.setFocus(true); + QTest::mousePress(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(edit.scenePos())); + QTest::mouseRelease(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(edit.scenePos())); + QApplication::processEvents(); QCOMPARE(ic.openInputPanelReceived, false); QCOMPARE(ic.closeInputPanelReceived, false); - edit.setFocusOnPress(false); - QCOMPARE(focusOnPressSpy.count(),1); - - // one show input panel event when openSoftwareInputPanel is called + // one show input panel event should + // be set when openSoftwareInputPanel is called edit.openSoftwareInputPanel(); QCOMPARE(ic.openInputPanelReceived, true); QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; - // one close input panel event when closeSoftwareInputPanel is called + // one close input panel event should + // be sent when closeSoftwareInputPanel is called edit.closeSoftwareInputPanel(); QCOMPARE(ic.openInputPanelReceived, false); QCOMPARE(ic.closeInputPanelReceived, true); - ic.openInputPanelReceived = false; + ic.closeInputPanelReceived = false; // set focusOnPress back to true edit.setFocusOnPress(true); QCOMPARE(focusOnPressSpy.count(),2); + edit.setFocusOnPress(true); + QCOMPARE(focusOnPressSpy.count(),2); edit.setFocus(false); + QApplication::processEvents(); QCOMPARE(ic.openInputPanelReceived, false); - QCOMPARE(ic.closeInputPanelReceived, true); + QCOMPARE(ic.closeInputPanelReceived, false); ic.closeInputPanelReceived = false; - edit.setFocusOnPress(true); - QCOMPARE(focusOnPressSpy.count(),2); - + // input panel should not re-open + // if focus has already been set edit.setFocus(true); QCOMPARE(ic.openInputPanelReceived, true); - QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; - - // input panel should not open if focus has already been set edit.setFocus(true); QCOMPARE(ic.openInputPanelReceived, false); - QCOMPARE(ic.closeInputPanelReceived, false); + + // input method should be disabled + // if TextEdit loses focus + edit.setFocus(false); + QApplication::processEvents(); + QVERIFY(view.inputContext() == 0); + QVERIFY(!view.testAttribute(Qt::WA_InputMethodEnabled)); } void tst_qdeclarativetextedit::geometrySignals() diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index 370ecfb..c9de0aa 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -833,6 +833,7 @@ void tst_qdeclarativetextinput::openInputPanelOnFocus() QDeclarativeTextInputPrivate *inputPrivate = static_cast(pri); inputPrivate->showInputPanelOnFocus = true; + // test default values QVERIFY(input.focusOnPress()); QCOMPARE(ic.openInputPanelReceived, false); QCOMPARE(ic.closeInputPanelReceived, false); @@ -842,75 +843,94 @@ void tst_qdeclarativetextinput::openInputPanelOnFocus() QApplication::processEvents(); QVERIFY(input.hasFocus()); QCOMPARE(ic.openInputPanelReceived, true); - QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; // no events on release QTest::mouseRelease(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(input.scenePos())); QCOMPARE(ic.openInputPanelReceived, false); - QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; - // Even with focus already gained, user needs - // to be able to open panel by pressing on the editor + // if already focused, input panel can be opened on press + QVERIFY(input.hasFocus()); QTest::mousePress(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(input.scenePos())); QApplication::processEvents(); QCOMPARE(ic.openInputPanelReceived, true); - QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; - // input panel closed on focus lost - input.setFocus(false); + // input method should stay enabled if focus + // is lost to an item that also accepts inputs + QDeclarativeTextInput anotherInput; + scene.addItem(&anotherInput); + anotherInput.setFocus(true); + QApplication::processEvents(); + QCOMPARE(ic.openInputPanelReceived, true); + ic.openInputPanelReceived = false; + QCOMPARE(view.inputContext(), &ic); + QVERIFY(view.testAttribute(Qt::WA_InputMethodEnabled)); + + // input method should be disabled if focus + // is lost to an item that doesn't accept inputs + QDeclarativeItem item; + scene.addItem(&item); + item.setFocus(true); QApplication::processEvents(); QCOMPARE(ic.openInputPanelReceived, false); - QCOMPARE(ic.closeInputPanelReceived, true); - ic.closeInputPanelReceived = false; + QVERIFY(view.inputContext() == 0); + QVERIFY(!view.testAttribute(Qt::WA_InputMethodEnabled)); - // no automatic input panel events if focusOnPress is false + // no automatic input panel events should + // be sent if focusOnPress is false + input.setFocusOnPress(false); + QCOMPARE(focusOnPressSpy.count(),1); input.setFocusOnPress(false); QCOMPARE(focusOnPressSpy.count(),1); - QTest::mousePress(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(input.scenePos())); - QTest::mouseRelease(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(input.scenePos())); input.setFocus(false); input.setFocus(true); + QTest::mousePress(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(input.scenePos())); + QTest::mouseRelease(view.viewport(), Qt::LeftButton, 0, view.mapFromScene(input.scenePos())); + QApplication::processEvents(); QCOMPARE(ic.openInputPanelReceived, false); QCOMPARE(ic.closeInputPanelReceived, false); - input.setFocusOnPress(false); - QCOMPARE(focusOnPressSpy.count(),1); - - // one show input panel event when openSoftwareInputPanel is called + // one show input panel event should + // be set when openSoftwareInputPanel is called input.openSoftwareInputPanel(); QCOMPARE(ic.openInputPanelReceived, true); QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; - // one close input panel event when closeSoftwareInputPanel is called + // one close input panel event should + // be sent when closeSoftwareInputPanel is called input.closeSoftwareInputPanel(); QCOMPARE(ic.openInputPanelReceived, false); QCOMPARE(ic.closeInputPanelReceived, true); - ic.openInputPanelReceived = false; + ic.closeInputPanelReceived = false; // set focusOnPress back to true input.setFocusOnPress(true); QCOMPARE(focusOnPressSpy.count(),2); + input.setFocusOnPress(true); + QCOMPARE(focusOnPressSpy.count(),2); input.setFocus(false); + QApplication::processEvents(); QCOMPARE(ic.openInputPanelReceived, false); - QCOMPARE(ic.closeInputPanelReceived, true); + QCOMPARE(ic.closeInputPanelReceived, false); ic.closeInputPanelReceived = false; - input.setFocusOnPress(true); - QCOMPARE(focusOnPressSpy.count(),2); - + // input panel should not re-open + // if focus has already been set input.setFocus(true); QCOMPARE(ic.openInputPanelReceived, true); - QCOMPARE(ic.closeInputPanelReceived, false); ic.openInputPanelReceived = false; - - // input panel should not open if focus has already been set input.setFocus(true); QCOMPARE(ic.openInputPanelReceived, false); - QCOMPARE(ic.closeInputPanelReceived, false); + + // input method should be disabled + // if TextEdit loses focus + input.setFocus(false); + QApplication::processEvents(); + QVERIFY(view.inputContext() == 0); + QVERIFY(!view.testAttribute(Qt::WA_InputMethodEnabled)); } class MyTextInput : public QDeclarativeTextInput -- cgit v0.12