From: Pekka Vuorela Date: Mon, 9 Jan 2012 14:31:54 +0000 (+0200) Subject: QQuickTextEdit to follow input method direction changes X-Git-Tag: upstream/5.2.1~2885 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dc7aecd0886db44f7729f16b42ea4a6be622f3ba;p=platform%2Fupstream%2Fqtdeclarative.git QQuickTextEdit to follow input method direction changes Change-Id: I458f85452a2ffe1c43438f9588cfc271461a42c6 Reviewed-by: Joona Petrell Reviewed-by: Andrew den Exter --- diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index f56a21b..f1dcada 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -1502,6 +1502,15 @@ void QQuickTextEdit::itemChange(ItemChange change, const ItemChangeData &value) { if (change == ItemActiveFocusHasChanged) { setCursorVisible(value.boolValue); // ### refactor: focus handling && d->canvas && d->canvas->hasFocus()); + + if (value.boolValue) { + q_updateAlignment(); + connect(qApp->inputPanel(), SIGNAL(inputDirectionChanged(Qt::LayoutDirection)), + this, SLOT(q_updateAlignment())); + } else { + disconnect(qApp->inputPanel(), SIGNAL(inputDirectionChanged(Qt::LayoutDirection)), + this, SLOT(q_updateAlignment())); + } } QQuickItem::itemChange(change, value); } @@ -1926,6 +1935,15 @@ void QQuickTextEdit::updateCursor() } } +void QQuickTextEdit::q_updateAlignment() +{ + Q_D(QQuickTextEdit); + if (d->determineHorizontalAlignment()) { + d->updateDefaultTextOption(); + moveCursorDelegate(); + } +} + void QQuickTextEdit::updateTotalLines() { Q_D(QQuickTextEdit); diff --git a/src/quick/items/qquicktextedit_p.h b/src/quick/items/qquicktextedit_p.h index e127416..508f564 100644 --- a/src/quick/items/qquicktextedit_p.h +++ b/src/quick/items/qquicktextedit_p.h @@ -282,6 +282,7 @@ private Q_SLOTS: void q_canPasteChanged(); void updateDocument(); void updateCursor(); + void q_updateAlignment(); private: void updateSize(); diff --git a/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp index ba63e04..b35d955 100644 --- a/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp @@ -97,6 +97,7 @@ public: tst_qquicktextedit(); private slots: + void cleanup(); void text(); void width(); void wrap(); @@ -296,6 +297,13 @@ tst_qquicktextedit::tst_qquicktextedit() // } +void tst_qquicktextedit::cleanup() +{ + // ensure not even skipped tests with custom input context leave it dangling + QInputPanelPrivate *inputPanelPrivate = QInputPanelPrivate::get(qApp->inputPanel()); + inputPanelPrivate->testContext = 0; +} + void tst_qquicktextedit::text() { { @@ -616,6 +624,10 @@ void tst_qquicktextedit::hAlign() void tst_qquicktextedit::hAlign_RightToLeft() { + PlatformInputContext platformInputContext; + QInputPanelPrivate *inputPanelPrivate = QInputPanelPrivate::get(qApp->inputPanel()); + inputPanelPrivate->testContext = &platformInputContext; + QQuickView canvas(testFileUrl("horizontalAlignment_RightToLeft.qml")); QQuickTextEdit *textEdit = canvas.rootObject()->findChild("text"); QVERIFY(textEdit != 0); @@ -716,24 +728,40 @@ void tst_qquicktextedit::hAlign_RightToLeft() // empty text with implicit alignment follows the system locale-based // keyboard input direction from qApp->inputPanel()->inputDirection textEdit->setText(""); - QCOMPARE(textEdit->hAlign(), qApp->inputPanel()->inputDirection() == Qt::LeftToRight ? - QQuickTextEdit::AlignLeft : QQuickTextEdit::AlignRight); - if (qApp->inputPanel()->inputDirection() == Qt::LeftToRight) - QVERIFY(textEdit->positionToRectangle(0).x() < canvas.width()/2); - else - QVERIFY(textEdit->positionToRectangle(0).x() > canvas.width()/2); - textEdit->setHAlign(QQuickTextEdit::AlignRight); + platformInputContext.setInputDirection(Qt::LeftToRight); + QVERIFY(qApp->inputPanel()->inputDirection() == Qt::LeftToRight); + QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignLeft); + QVERIFY(textEdit->positionToRectangle(0).x() < canvas.width()/2); + + QSignalSpy cursorRectangleSpy(textEdit, SIGNAL(cursorRectangleChanged())); + + platformInputContext.setInputDirection(Qt::RightToLeft); + QCOMPARE(cursorRectangleSpy.count(), 1); + QVERIFY(qApp->inputPanel()->inputDirection() == Qt::RightToLeft); QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight); QVERIFY(textEdit->positionToRectangle(0).x() > canvas.width()/2); - // alignment of TextEdit with no text set to it - QString componentStr = "import QtQuick 2.0\nTextEdit {}"; - QDeclarativeComponent textComponent(&engine); - textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); - QQuickTextEdit *textObject = qobject_cast(textComponent.create()); - QCOMPARE(textObject->hAlign(), qApp->inputPanel()->inputDirection() == Qt::LeftToRight ? - QQuickTextEdit::AlignLeft : QQuickTextEdit::AlignRight); - delete textObject; + // set input direction while having content + platformInputContext.setInputDirection(Qt::LeftToRight); + textEdit->setText("a"); + textEdit->setCursorPosition(1); + platformInputContext.setInputDirection(Qt::RightToLeft); + QTest::keyClick(&canvas, Qt::Key_Backspace); + QVERIFY(textEdit->text().isEmpty()); + QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight); + QVERIFY(textEdit->cursorRectangle().left() > canvas.width()/2); + + // input direction changed while not having focus + platformInputContext.setInputDirection(Qt::LeftToRight); + textEdit->setFocus(false); + platformInputContext.setInputDirection(Qt::RightToLeft); + textEdit->setFocus(true); + QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight); + QVERIFY(textEdit->cursorRectangle().left() > canvas.width()/2); + + textEdit->setHAlign(QQuickTextEdit::AlignRight); + QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight); + QVERIFY(textEdit->positionToRectangle(0).x() > canvas.width()/2); } void tst_qquicktextedit::vAlign()