From 8ed0a827298776ef2574b1298f24071c61dc8b07 Mon Sep 17 00:00:00 2001 From: Alexis Menard Date: Thu, 22 Apr 2010 06:55:07 +0200 Subject: Fix QDeclarativeTextInput and QGraphicsView in regards of input methods hints. When the echoMode changes for QDeclarativeTextInput we need to update the inputmethod hints. Also in QGraphicsView we need to update the input context installed on the view to make the former is aware of the change. For the input context framework the only way to deal with the current widget is focusWidget(). Unfortunately the widget that has the focus is the QGraphicsView so the input context don't know the real object QGraphicsView is actually focusing. We must keep in sync the inputMethodHints of QGV with the object QGraphicsView is focusing so the input context just called focusWidget()->inputMethodhints() to update itself. Task-number:QTBUG-9922 Reviewed-by:janarve Reviewed-by:bnilsen Reviewed-by:Michael Brasser --- .../graphicsitems/qdeclarativetextinput.cpp | 12 ++++- src/gui/graphicsview/qgraphicsitem.cpp | 24 +++++++--- src/gui/graphicsview/qgraphicsview.cpp | 22 ++++++++- .../tst_qdeclarativetextinput.cpp | 17 +++++++ tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp | 52 +++++++++++++++++++++- 5 files changed, 115 insertions(+), 12 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 2161e23..b618183 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -726,8 +726,18 @@ void QDeclarativeTextInput::setEchoMode(QDeclarativeTextInput::EchoMode echo) Q_D(QDeclarativeTextInput); if (echoMode() == echo) return; - + Qt::InputMethodHints imHints = inputMethodHints(); + if (echo == Password || echo == NoEcho) + imHints |= Qt::ImhHiddenText; + else + imHints &= ~Qt::ImhHiddenText; + if (echo != Normal) + imHints |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + else + imHints &= ~(Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + setInputMethodHints(imHints); d->control->setEchoMode((uint)echo); + update(); emit echoModeChanged(echoMode()); } diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index 6cc3f7d..326f130 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -7325,6 +7325,18 @@ void QGraphicsItem::setInputMethodHints(Qt::InputMethodHints hints) { Q_D(QGraphicsItem); d->imHints = hints; + if (!hasFocus()) + return; + d->scene->d_func()->updateInputMethodSensitivityInViews(); +#if !defined(QT_NO_IM) && (defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_OS_SYMBIAN)) + QWidget *fw = QApplication::focusWidget(); + if (!fw) + return; + for (int i = 0 ; i < scene()->views().count() ; ++i) + if (scene()->views().at(i) == fw) + if (QInputContext *inputContext = fw->inputContext()) + inputContext->update(); +#endif } /*! @@ -7337,13 +7349,11 @@ void QGraphicsItem::setInputMethodHints(Qt::InputMethodHints hints) void QGraphicsItem::updateMicroFocus() { #if !defined(QT_NO_IM) && (defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_OS_SYMBIAN)) - if (QWidget *fw = qApp->focusWidget()) { - if (qt_widget_private(fw)->ic || qApp->d_func()->inputContext) { - if (QInputContext *ic = fw->inputContext()) { - if (ic) - ic->update(); - } - } + if (QWidget *fw = QApplication::focusWidget()) { + for (int i = 0 ; i < scene()->views().count() ; ++i) + if (scene()->views().at(i) == fw) + if (QInputContext *inputContext = fw->inputContext()) + inputContext->update(); #ifndef QT_NO_ACCESSIBILITY // ##### is this correct QAccessible::updateAccessibility(fw, 0, QAccessible::StateChanged); diff --git a/src/gui/graphicsview/qgraphicsview.cpp b/src/gui/graphicsview/qgraphicsview.cpp index 114de85..c951dce 100644 --- a/src/gui/graphicsview/qgraphicsview.cpp +++ b/src/gui/graphicsview/qgraphicsview.cpp @@ -1037,10 +1037,28 @@ QList QGraphicsViewPrivate::findItems(const QRegion &exposedReg void QGraphicsViewPrivate::updateInputMethodSensitivity() { Q_Q(QGraphicsView); - bool enabled = scene && scene->focusItem() - && (scene->focusItem()->flags() & QGraphicsItem::ItemAcceptsInputMethod); + QGraphicsItem *focusItem = 0; + bool enabled = scene && (focusItem = scene->focusItem()) + && (focusItem->d_ptr->flags & QGraphicsItem::ItemAcceptsInputMethod); q->setAttribute(Qt::WA_InputMethodEnabled, enabled); q->viewport()->setAttribute(Qt::WA_InputMethodEnabled, enabled); + + if (!enabled) { + q->setInputMethodHints(0); + return; + } + + QGraphicsProxyWidget *proxy = focusItem->d_ptr->isWidget && focusItem->d_ptr->isProxyWidget() + ? static_cast(focusItem) : 0; + if (!proxy) { + q->setInputMethodHints(focusItem->inputMethodHints()); + } else if (QWidget *widget = proxy->widget()) { + if (QWidget *fw = widget->focusWidget()) + widget = fw; + q->setInputMethodHints(widget->inputMethodHints()); + } else { + q->setInputMethodHints(0); + } } /*! diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index fcd7527..83ebe6c 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -644,21 +644,38 @@ void tst_qdeclarativetextinput::echoMode() QVERIFY(input != 0); QTRY_VERIFY(input->hasFocus() == true); QString initial = input->text(); + Qt::InputMethodHints ref; QCOMPARE(initial, QLatin1String("ABCDefgh")); QCOMPARE(input->echoMode(), QDeclarativeTextInput::Normal); QCOMPARE(input->displayText(), input->text()); + //Normal + ref &= ~Qt::ImhHiddenText; + ref &= ~(Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + QCOMPARE(input->inputMethodHints(), ref); input->setEchoMode(QDeclarativeTextInput::NoEcho); QCOMPARE(input->text(), initial); QCOMPARE(input->displayText(), QLatin1String("")); QCOMPARE(input->passwordCharacter(), QLatin1String("*")); + //NoEcho + ref |= Qt::ImhHiddenText; + ref |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + QCOMPARE(input->inputMethodHints(), ref); input->setEchoMode(QDeclarativeTextInput::Password); + //Password + ref |= Qt::ImhHiddenText; + ref |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); QCOMPARE(input->text(), initial); QCOMPARE(input->displayText(), QLatin1String("********")); + QCOMPARE(input->inputMethodHints(), ref); input->setPasswordCharacter(QChar('Q')); QCOMPARE(input->passwordCharacter(), QLatin1String("Q")); QCOMPARE(input->text(), initial); QCOMPARE(input->displayText(), QLatin1String("QQQQQQQQ")); input->setEchoMode(QDeclarativeTextInput::PasswordEchoOnEdit); + //PasswordEchoOnEdit + ref &= ~Qt::ImhHiddenText; + ref |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + QCOMPARE(input->inputMethodHints(), ref); QCOMPARE(input->text(), initial); QCOMPARE(input->displayText(), QLatin1String("QQQQQQQQ")); QTest::keyPress(canvas, Qt::Key_A);//Clearing previous entry is part of PasswordEchoOnEdit diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp index e2f7f18..e1c4791 100644 --- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp @@ -63,6 +63,7 @@ #include #include #include +#include #include "../../shared/util.h" @@ -945,8 +946,52 @@ class ImhTester : public QGraphicsItem void tst_QGraphicsItem::inputMethodHints() { - ImhTester item; - QCOMPARE(item.inputMethodHints(), Qt::ImhNone); + ImhTester *item = new ImhTester; + item->setFlag(QGraphicsItem::ItemAcceptsInputMethod, true); + item->setFlag(QGraphicsItem::ItemIsFocusable, true); + QCOMPARE(item->inputMethodHints(), Qt::ImhNone); + ImhTester *item2 = new ImhTester; + item2->setFlag(QGraphicsItem::ItemAcceptsInputMethod, true); + item2->setFlag(QGraphicsItem::ItemIsFocusable, true); + Qt::InputMethodHints imHints = item2->inputMethodHints(); + imHints |= Qt::ImhHiddenText; + item2->setInputMethodHints(imHints); + QGraphicsScene scene; + scene.addItem(item); + scene.addItem(item2); + QGraphicsView view(&scene); + QApplication::setActiveWindow(&view); + view.show(); + QTest::qWaitForWindowShown(&view); + item->setFocus(); + QTRY_VERIFY(item->hasFocus()); + QCOMPARE(view.inputMethodHints(), item->inputMethodHints()); + item2->setFocus(); + QTRY_VERIFY(item2->hasFocus()); + QCOMPARE(view.inputMethodHints(), item2->inputMethodHints()); + item->setFlag(QGraphicsItem::ItemAcceptsInputMethod, false); + item->setFocus(); + QTRY_VERIFY(item->hasFocus()); + //Focus has changed but the new item doesn't accept input method, no hints. + QCOMPARE(view.inputMethodHints(), 0); + item2->setFocus(); + QTRY_VERIFY(item2->hasFocus()); + QCOMPARE(view.inputMethodHints(), item2->inputMethodHints()); + imHints = item2->inputMethodHints(); + imHints |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + item2->setInputMethodHints(imHints); + QCOMPARE(view.inputMethodHints(), item2->inputMethodHints()); + QGraphicsProxyWidget *widget = new QGraphicsProxyWidget; + QLineEdit *edit = new QLineEdit; + edit->setEchoMode(QLineEdit::Password); + scene.addItem(widget); + widget->setFocus(); + QTRY_VERIFY(widget->hasFocus()); + //No widget on the proxy, so no hints + QCOMPARE(view.inputMethodHints(), 0); + widget->setWidget(edit); + //View should match with the line edit + QCOMPARE(view.inputMethodHints(), edit->inputMethodHints()); } void tst_QGraphicsItem::toolTip() @@ -10055,6 +10100,9 @@ void tst_QGraphicsItem::updateMicroFocus() QApplication::setActiveWindow(&parent); QTest::qWaitForWindowShown(&parent); QTRY_COMPARE(QApplication::activeWindow(), static_cast(&parent)); + //We reset the number of updates that happened previously (initialisation) + ic.nbUpdates = 0; + ic2.nbUpdates = 0; input.doUpdateMicroFocus(); QApplication::processEvents(); QTRY_COMPARE(ic.nbUpdates, 1); -- cgit v0.12