From: Pekka Vuorela Date: Fri, 13 Apr 2012 14:49:52 +0000 (+0300) Subject: QQuickTextInput aligment to follow input method on neutral content X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=a150d1211f9ec49f963227452ee4818746b8c6ed;p=konrad%2Fqtdeclarative.git QQuickTextInput aligment to follow input method on neutral content Earlier input method was followed only on empty editor. This made inputting one space character with arabic to be visualized left to right. As related change arabic numbers are aligned right even though the flow goes left to right. Change-Id: I36448949569f6290faad69de14df424575d8b97e Reviewed-by: Joona Petrell --- diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index c9019c6..9cf187e 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -467,16 +467,48 @@ bool QQuickTextInputPrivate::setHAlign(QQuickTextInput::HAlignment alignment, bo return false; } +Qt::LayoutDirection QQuickTextInputPrivate::textDirection() const +{ + QString text = m_text; + if (text.isEmpty()) + text = m_textLayout.preeditAreaText(); + + const QChar *character = text.constData(); + while (!character->isNull()) { + switch (character->direction()) { + case QChar::DirL: + return Qt::LeftToRight; + case QChar::DirR: + case QChar::DirAL: + case QChar::DirAN: + return Qt::RightToLeft; + default: + break; + } + character++; + } + return Qt::LayoutDirectionAuto; +} + +Qt::LayoutDirection QQuickTextInputPrivate::layoutDirection() const +{ + Qt::LayoutDirection direction = m_layoutDirection; + if (direction == Qt::LayoutDirectionAuto) { + direction = textDirection(); + if (direction == Qt::LayoutDirectionAuto) + direction = qApp->inputMethod()->inputDirection(); + } + return (direction == Qt::LayoutDirectionAuto) ? Qt::LeftToRight : direction; +} + bool QQuickTextInputPrivate::determineHorizontalAlignment() { if (hAlignImplicit) { // if no explicit alignment has been set, follow the natural layout direction of the text - QString text = q_func()->text(); - if (text.isEmpty()) - text = m_textLayout.preeditAreaText(); - bool isRightToLeft = text.isEmpty() ? qApp->inputMethod()->inputDirection() == Qt::RightToLeft - : text.isRightToLeft(); - return setHAlign(isRightToLeft ? QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft); + Qt::LayoutDirection direction = textDirection(); + if (direction == Qt::LayoutDirectionAuto) + direction = qApp->inputMethod()->inputDirection(); + return setHAlign(direction == Qt::RightToLeft ? QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft); } return false; } diff --git a/src/quick/items/qquicktextinput_p_p.h b/src/quick/items/qquicktextinput_p_p.h index d952b27..34aa072 100644 --- a/src/quick/items/qquicktextinput_p_p.h +++ b/src/quick/items/qquicktextinput_p_p.h @@ -380,14 +380,8 @@ public: m_passwordEchoTimer.stop(); } - Qt::LayoutDirection layoutDirection() const { - if (m_layoutDirection == Qt::LayoutDirectionAuto) { - if (m_text.isEmpty()) - return qApp->inputMethod()->inputDirection(); - return m_text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight; - } - return m_layoutDirection; - } + Qt::LayoutDirection textDirection() const; + Qt::LayoutDirection layoutDirection() const; void setLayoutDirection(Qt::LayoutDirection direction) { if (direction != m_layoutDirection) { diff --git a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp index 27e557b..2ea2486 100644 --- a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp +++ b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp @@ -1432,6 +1432,15 @@ void tst_qquicktextinput::horizontalAlignment_RightToLeft() QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight); QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1); QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1); + + // neutral text should fall back to input direction + textInput->setFocus(true); + textInput->resetHAlign(); + textInput->setText(" ()((=<>"); + platformInputContext.setInputDirection(Qt::LeftToRight); + QCOMPARE(textInput->effectiveHAlign(), QQuickTextInput::AlignLeft); + platformInputContext.setInputDirection(Qt::RightToLeft); + QCOMPARE(textInput->effectiveHAlign(), QQuickTextInput::AlignRight); } void tst_qquicktextinput::verticalAlignment()