Navigation Menu

Skip to content

Commit

Permalink
Adapt to QGuiApplication::focusObject() for notifying input method
Browse files Browse the repository at this point in the history
Emitting focusObjectChanged() signal now instead of explicitly setting
inputItem to QInputPanel. Also adapted from deprecated
QInputPanel::inputItem() to QGuiApplication::focusObject().

Change-Id: I97460625cc8bb2820ec7716a06330aecd1b585ea
Reviewed-by: Joona Petrell <joona.t.petrell@nokia.com>
  • Loading branch information
Pekka Vuorela authored and Qt by Nokia committed Feb 2, 2012
1 parent ee9c5f7 commit 4ab22fd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 87 deletions.
25 changes: 7 additions & 18 deletions src/quick/items/qquickcanvas.cpp
Expand Up @@ -73,7 +73,7 @@ void QQuickCanvasPrivate::updateFocusItemTransform()
{
Q_Q(QQuickCanvas);
QQuickItem *focus = q->activeFocusItem();
if (focus && qApp->inputPanel()->inputItem() == focus)
if (focus && qApp->focusObject() == focus)
qApp->inputPanel()->setInputItemTransform(QQuickItemPrivate::get(focus)->itemToCanvasTransform());
}

Expand Down Expand Up @@ -188,6 +188,7 @@ void QQuickCanvas::focusInEvent(QFocusEvent *)
{
Q_D(QQuickCanvas);
d->rootItem->setFocus(true);
d->updateFocusItemTransform();
}


Expand Down Expand Up @@ -379,7 +380,7 @@ void QQuickCanvasPrivate::setFocusInScope(QQuickItem *scope, QQuickItem *item, F
QVarLengthArray<QQuickItem *, 20> changed;

// Does this change the active focus?
if (item == rootItem || scopePrivate->activeFocus && item->isEnabled()) {
if (item == rootItem || (scopePrivate->activeFocus && item->isEnabled())) {
oldActiveFocusItem = activeFocusItem;
newActiveFocusItem = item;
while (newActiveFocusItem->isFocusScope()
Expand Down Expand Up @@ -455,14 +456,12 @@ void QQuickCanvasPrivate::setFocusInScope(QQuickItem *scope, QQuickItem *item, F
afi = afi->parentItem();
}

updateInputMethodData();

QFocusEvent event(QEvent::FocusIn, Qt::OtherFocusReason);
q->sendEvent(newActiveFocusItem, &event);
} else {
updateInputMethodData();
}

emit q->focusObjectChanged(activeFocusItem);

if (!changed.isEmpty())
notifyFocusChangesRecur(changed.data(), changed.count() - 1);
}
Expand Down Expand Up @@ -541,14 +540,12 @@ void QQuickCanvasPrivate::clearFocusInScope(QQuickItem *scope, QQuickItem *item,
Q_ASSERT(newActiveFocusItem == scope);
activeFocusItem = scope;

updateInputMethodData();

QFocusEvent event(QEvent::FocusIn, Qt::OtherFocusReason);
q->sendEvent(newActiveFocusItem, &event);
} else {
updateInputMethodData();
}

emit q->focusObjectChanged(activeFocusItem);

if (!changed.isEmpty())
notifyFocusChangesRecur(changed.data(), changed.count() - 1);
}
Expand Down Expand Up @@ -576,14 +573,6 @@ void QQuickCanvasPrivate::notifyFocusChangesRecur(QQuickItem **items, int remain
}
}

void QQuickCanvasPrivate::updateInputMethodData()
{
QQuickItem *inputItem = 0;
if (activeFocusItem && activeFocusItem->flags() & QQuickItem::ItemAcceptsInputMethod)
inputItem = activeFocusItem;
qApp->inputPanel()->setInputItem(inputItem);
}

void QQuickCanvasPrivate::dirtyItem(QQuickItem *)
{
Q_Q(QQuickCanvas);
Expand Down
1 change: 0 additions & 1 deletion src/quick/items/qquickcanvas_p.h
Expand Up @@ -143,7 +143,6 @@ class QQuickCanvasPrivate : public QWindowPrivate
void clearFocusInScope(QQuickItem *scope, QQuickItem *item, FocusOptions = 0);
void notifyFocusChangesRecur(QQuickItem **item, int remaining);

void updateInputMethodData();
void updateFocusItemTransform();

void dirtyItem(QQuickItem *);
Expand Down
3 changes: 1 addition & 2 deletions src/quick/items/qquickitem.cpp
Expand Up @@ -3077,8 +3077,7 @@ void QQuickItem::setInputMethodHints(Qt::InputMethodHints hints)
if (!d->canvas || d->canvas->activeFocusItem() != this)
return;

QInputPanel *p = qApp->inputPanel();
if (p->inputItem() == this)
if (qApp->focusObject() == this)
qApp->inputPanel()->update(Qt::ImHints);
}

Expand Down
Expand Up @@ -86,7 +86,7 @@ void sendPreeditText(const QString &text, int cursor)
attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, cursor,
text.length(), QVariant()));
QInputMethodEvent event(text, attributes);
QApplication::sendEvent(qApp->inputPanel()->inputItem(), &event);
QApplication::sendEvent(qApp->focusObject(), &event);
}


Expand Down Expand Up @@ -2433,7 +2433,7 @@ void tst_qdeclarativetextedit::preeditMicroFocus()
sendPreeditText(preeditText, 0);
ic.clear();
QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>());
QApplication::sendEvent(qApp->inputPanel()->inputItem(), &imEvent);
QApplication::sendEvent(qApp->focusObject(), &imEvent);
currentRect = edit.inputMethodQuery(Qt::ImMicroFocus).toRect();
QCOMPARE(currentRect, previousRect);
#if defined(Q_WS_X11) || defined(Q_WS_QWS)
Expand Down
Expand Up @@ -79,7 +79,7 @@ void sendPreeditText(const QString &text, int cursor)
attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, cursor,
text.length(), QVariant()));
QInputMethodEvent event(text, attributes);
QApplication::sendEvent(qApp->inputPanel()->inputItem(), &event);
QApplication::sendEvent(qApp->focusObject(), &event);
}


Expand Down Expand Up @@ -2634,7 +2634,7 @@ void tst_qdeclarativetextinput::preeditMicroFocus()
sendPreeditText(preeditText, 0);
ic.clear();
QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>());
QApplication::sendEvent(qApp->inputPanel()->inputItem(), &imEvent);
QApplication::sendEvent(qApp->focusObject(), &imEvent);
currentRect = input.inputMethodQuery(Qt::ImMicroFocus).toRect();
QCOMPARE(currentRect, previousRect);
#if defined(Q_WS_X11) || defined(Q_WS_QWS)
Expand Down Expand Up @@ -2705,23 +2705,23 @@ void tst_qdeclarativetextinput::inputMethodComposing()

{
QInputMethodEvent inputEvent(text.mid(3), QList<QInputMethodEvent::Attribute>());
QApplication::sendEvent(qApp->inputPanel()->inputItem(), &inputEvent);
QApplication::sendEvent(qApp->focusObject(), &inputEvent);
}

QCOMPARE(input.isInputMethodComposing(), true);
QCOMPARE(spy.count(), 1);

{
QInputMethodEvent inputEvent(text.mid(12), QList<QInputMethodEvent::Attribute>());
QApplication::sendEvent(qApp->inputPanel()->inputItem(), &inputEvent);
QApplication::sendEvent(qApp->focusObject(), &inputEvent);
}

QCOMPARE(input.isInputMethodComposing(), true);
QCOMPARE(spy.count(), 1);

{
QInputMethodEvent inputEvent;
QApplication::sendEvent(qApp->inputPanel()->inputItem(), &inputEvent);
QApplication::sendEvent(qApp->focusObject(), &inputEvent);
}
QCOMPARE(input.isInputMethodComposing(), false);
QCOMPARE(spy.count(), 2);
Expand Down
49 changes: 21 additions & 28 deletions tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp
Expand Up @@ -722,14 +722,14 @@ void tst_qquicktextedit::hAlign_RightToLeft()
QTRY_COMPARE(&canvas, qGuiApp->focusWindow());

textEdit->setText(QString());
{ QInputMethodEvent ev(rtlText, QList<QInputMethodEvent::Attribute>()); QGuiApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &ev); }
{ QInputMethodEvent ev(rtlText, QList<QInputMethodEvent::Attribute>()); QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); }
QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight);
{ QInputMethodEvent ev("Hello world!", QList<QInputMethodEvent::Attribute>()); QGuiApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &ev); }
{ QInputMethodEvent ev("Hello world!", QList<QInputMethodEvent::Attribute>()); QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); }
QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignLeft);

// Clear pre-edit text. TextEdit should maybe do this itself on setText, but that may be
// redundant as an actual input method may take care of it.
{ QInputMethodEvent ev; QGuiApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &ev); }
{ QInputMethodEvent ev; QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); }

// empty text with implicit alignment follows the system locale-based
// keyboard input direction from qApp->inputPanel()->inputDirection
Expand Down Expand Up @@ -1738,7 +1738,7 @@ void tst_qquicktextedit::positionAt()
texteditObject->setCursorPosition(0);

QInputMethodEvent inputEvent(preeditText, QList<QInputMethodEvent::Attribute>());
QGuiApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &inputEvent);
QGuiApplication::sendEvent(qGuiApp->focusObject(), &inputEvent);

// Check all points within the preedit text return the same position.
QCOMPARE(texteditObject->positionAt(0, y0), 0);
Expand Down Expand Up @@ -2138,7 +2138,7 @@ void tst_qquicktextedit::textInput()
QSignalSpy spy(edit, SIGNAL(textChanged()));
QInputMethodEvent event;
event.setCommitString( "Hello world!", 0, 0);
QGuiApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &event);
QGuiApplication::sendEvent(qGuiApp->focusObject(), &event);
QCOMPARE(edit->text(), QString("Hello world!"));
QCOMPARE(spy.count(), 1);

Expand All @@ -2153,12 +2153,12 @@ void tst_qquicktextedit::textInput()
QList<QInputMethodEvent::Attribute> attributes;
QInputMethodEvent event2("preedit", attributes);
event2.setTentativeCommitString("string");
QGuiApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &event2);
QGuiApplication::sendEvent(qGuiApp->focusObject(), &event2);
QCOMPARE(spy.count(), 1);
QCOMPARE(edit->text(), QString("string"));

QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
QGuiApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &queryEvent);
QGuiApplication::sendEvent(qGuiApp->focusObject(), &queryEvent);
QCOMPARE(queryEvent.value(Qt::ImEnabled).toBool(), true);

edit->setReadOnly(true);
Expand Down Expand Up @@ -2270,8 +2270,8 @@ void tst_qquicktextedit::openInputPanel()
// check default values
QVERIFY(edit->focusOnPress());
QVERIFY(!edit->hasActiveFocus());
qDebug() << &edit << qApp->inputPanel()->inputItem();
QCOMPARE(qApp->inputPanel()->inputItem(), static_cast<QObject*>(0));
qDebug() << &edit << qApp->focusObject();
QVERIFY(qApp->focusObject() != edit);

QCOMPARE(qApp->inputPanel()->visible(), false);

Expand All @@ -2281,7 +2281,7 @@ void tst_qquicktextedit::openInputPanel()
QTest::mousePress(&view, Qt::LeftButton, noModifiers, centerPoint);
QGuiApplication::processEvents();
QVERIFY(edit->hasActiveFocus());
QCOMPARE(qApp->inputPanel()->inputItem(), edit);
QCOMPARE(qApp->focusObject(), edit);
QCOMPARE(qApp->inputPanel()->visible(), true);
QTest::mouseRelease(&view, Qt::LeftButton, noModifiers, centerPoint);

Expand All @@ -2300,21 +2300,14 @@ void tst_qquicktextedit::openInputPanel()
anotherEdit.setParentItem(view.rootObject());
anotherEdit.setFocus(true);
QCOMPARE(qApp->inputPanel()->visible(), true);
QCOMPARE(qApp->inputPanel()->inputItem(), qobject_cast<QObject*>(&anotherEdit));
QCOMPARE(qApp->focusObject(), qobject_cast<QObject*>(&anotherEdit));
QCOMPARE(inputPanelVisibilitySpy.count(), 0);

anotherEdit.setFocus(false);
QCOMPARE(qApp->inputPanel()->inputItem(), static_cast<QObject*>(0));
QVERIFY(qApp->focusObject() != &anotherEdit);
QCOMPARE(view.activeFocusItem(), view.rootItem());
anotherEdit.setFocus(true);

// input item should be null if focus is lost to an item that doesn't accept inputs
QQuickItem item;
item.setParentItem(view.rootObject());
item.setFocus(true);
QCOMPARE(qApp->inputPanel()->inputItem(), static_cast<QObject*>(0));
QCOMPARE(view.activeFocusItem(), &item);

qApp->inputPanel()->hide();

// input panel should not be opened if TextEdit is read only
Expand Down Expand Up @@ -2474,15 +2467,15 @@ void tst_qquicktextedit::preeditCursorRectangle()
QRect currentRect;

QInputMethodQueryEvent query(Qt::ImCursorRectangle);
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &query);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
QRect previousRect = query.value(Qt::ImCursorRectangle).toRect();

// Verify that the micro focus rect is positioned the same for position 0 as
// it would be if there was no preedit text.
QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>()
<< QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, preeditText.length(), QVariant()));
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &imEvent);
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &query);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
QCOMPARE(currentRect, previousRect);
QCOMPARE(editSpy.count(), 0);
Expand All @@ -2493,8 +2486,8 @@ void tst_qquicktextedit::preeditCursorRectangle()
for (int i = 1; i <= 5; ++i) {
QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>()
<< QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, i, preeditText.length(), QVariant()));
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &imEvent);
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &query);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
QVERIFY(previousRect.left() < currentRect.left());
QVERIFY(editSpy.count() > 0); editSpy.clear();
Expand All @@ -2504,12 +2497,12 @@ void tst_qquicktextedit::preeditCursorRectangle()

// Verify that if there is no preedit cursor then the micro focus rect is the
// same as it would be if it were positioned at the end of the preedit text.
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &imEvent);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent);
editSpy.clear();
panelSpy.clear();
{ QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>());
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &imEvent); }
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &query);
QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent); }
QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
QCOMPARE(currentRect, previousRect);
QVERIFY(editSpy.count() > 0);
Expand Down Expand Up @@ -2571,7 +2564,7 @@ void tst_qquicktextedit::cursorRectangleSize()
QTest::qWaitForWindowShown(canvas);

QInputMethodQueryEvent event(Qt::ImCursorRectangle);
qApp->sendEvent(qApp->inputPanel()->inputItem(), &event);
qApp->sendEvent(qApp->focusObject(), &event);
QRectF cursorRectFromQuery = event.value(Qt::ImCursorRectangle).toRectF();

QRect cursorRectFromItem = textEdit->cursorRectangle();
Expand Down

0 comments on commit 4ab22fd

Please sign in to comment.