From 6bf790e27d437631d2241942062e16b3c8191095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Arve=20S=C3=A6ther?= Date: Thu, 26 Nov 2015 10:40:21 +0100 Subject: Add support for input method selection handles QQuickTextInput needs to report back Qt::ImhAnchorRectangle, and make it possible to query the cursor position of any given point with inputMethodQuery(Qt::ImCursorPosition, point) Change-Id: Iabe1946e7a8642b51c4601b51e2a13763bdbbd0c Reviewed-by: Richard Moe Gustavsen --- src/quick/items/qquicktextinput.cpp | 50 +++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'src/quick/items/qquicktextinput.cpp') diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index 6551af4396..f93857a892 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -369,7 +369,7 @@ void QQuickTextInput::setFont(const QFont &font) d->updateLayout(); updateCursorRectangle(); #ifndef QT_NO_IM - updateInputMethod(Qt::ImCursorRectangle | Qt::ImFont); + updateInputMethod(Qt::ImCursorRectangle | Qt::ImFont | Qt::ImAnchorRectangle); #endif } emit fontChanged(d->sourceFont); @@ -1005,6 +1005,36 @@ void QQuickTextInput::q_validatorChanged() } #endif // QT_NO_VALIDATOR +QRectF QQuickTextInputPrivate::anchorRectangle() const +{ + QRectF rect; + int a; + // Unfortunately we cannot use selectionStart() and selectionEnd() + // since they always assume that the selectionStart is logically before selectionEnd. + // To rely on that would cause havoc if the user was interactively moving the end selection + // handle to become before the start selection + if (m_selstart == m_selend) + // This is to handle the case when there is "no selection" while moving the handle onto the + // same position as the other handle (in which case it would hide the selection handles) + a = m_cursor; + else + a = m_selstart == m_cursor ? m_selend : m_selstart; + if (a >= 0) { +#ifndef QT_NO_IM + a += m_preeditCursor; +#endif + if (m_echoMode == QQuickTextInput::NoEcho) + a = 0; + QTextLine l = m_textLayout.lineForTextPosition(a); + if (l.isValid()) { + qreal x = l.cursorToX(a) - hscroll; + qreal y = l.y() - vscroll; + rect.setRect(x, y, 1, l.height()); + } + } + return rect; +} + void QQuickTextInputPrivate::checkIsValid() { Q_Q(QQuickTextInput); @@ -1813,10 +1843,16 @@ QVariant QQuickTextInput::inputMethodQuery(Qt::InputMethodQuery property, QVaria return QVariant((int) d->effectiveInputMethodHints()); case Qt::ImCursorRectangle: return cursorRectangle(); + case Qt::ImAnchorRectangle: + return d->anchorRectangle(); case Qt::ImFont: return font(); - case Qt::ImCursorPosition: + case Qt::ImCursorPosition: { + const QPointF pt = argument.toPointF(); + if (!pt.isNull()) + return QVariant(d->positionAt(pt)); return QVariant(d->m_cursor); + } case Qt::ImSurroundingText: if (d->m_echoMode == PasswordEchoOnEdit && !d->m_passwordEchoEditing) { return QVariant(displayText()); @@ -2613,7 +2649,7 @@ void QQuickTextInput::updateCursorRectangle(bool scroll) d->cursorItem->setHeight(r.height()); } #ifndef QT_NO_IM - updateInputMethod(Qt::ImCursorRectangle); + updateInputMethod(Qt::ImCursorRectangle | Qt::ImAnchorRectangle); #endif } @@ -3147,8 +3183,8 @@ void QQuickTextInputPrivate::setSelection(int start, int length) emit q->selectionChanged(); emitCursorPositionChanged(); #ifndef QT_NO_IM - q->updateInputMethod(Qt::ImCursorRectangle | Qt::ImAnchorPosition - | Qt::ImCursorPosition | Qt::ImCurrentSelection); + q->updateInputMethod(Qt::ImCursorRectangle | Qt::ImAnchorRectangle | Qt::ImCursorPosition | Qt::ImAnchorPosition + | Qt::ImCurrentSelection); #endif } @@ -3346,8 +3382,8 @@ void QQuickTextInputPrivate::processInputMethodEvent(QInputMethodEvent *event) if (selectionChange) { emit q->selectionChanged(); - q->updateInputMethod(Qt::ImCursorRectangle | Qt::ImAnchorPosition - | Qt::ImCursorPosition | Qt::ImCurrentSelection); + q->updateInputMethod(Qt::ImCursorRectangle | Qt::ImAnchorRectangle + | Qt::ImCurrentSelection); } } #endif // QT_NO_IM -- cgit v1.2.3