diff options
author | Paul Olav Tvete <paul.tvete@digia.com> | 2014-01-20 12:30:35 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-02-15 10:38:35 +0100 |
commit | f8dbed12266c42785c1e4758eed05833ec035f33 (patch) | |
tree | 85067c8722cfc3854d9e22845fcd633f06cb4422 /src/widgets/widgets/qwidgettextcontrol.cpp | |
parent | 25e7fe16504ad4784ea2d75204ffa855ca6d3e1b (diff) |
Extending the inputMethodQuery API
Currently, inputMethodQuery() only provides information about the
current paragraph. On some platforms, such as Android, the input method
needs information about the global cursor position, and more of the
surrounding text. Some queries need to pass parameters.
The current inputmethodQuery() implementation does not allow parameters to
be passed. Changing this would require new or modified virtual functions, which
is not possible until Qt 6. Therefore, a completely new mechanism is needed.
Change-Id: Ic64fd90198ade70aa0fa6fa5ad3867dfa7ed763c
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Diffstat (limited to 'src/widgets/widgets/qwidgettextcontrol.cpp')
-rw-r--r-- | src/widgets/widgets/qwidgettextcontrol.cpp | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/src/widgets/widgets/qwidgettextcontrol.cpp b/src/widgets/widgets/qwidgettextcontrol.cpp index 06b513f3e0..98f85e681b 100644 --- a/src/widgets/widgets/qwidgettextcontrol.cpp +++ b/src/widgets/widgets/qwidgettextcontrol.cpp @@ -2024,7 +2024,7 @@ void QWidgetTextControlPrivate::inputMethodEvent(QInputMethodEvent *e) emit q->microFocusChanged(); } -QVariant QWidgetTextControl::inputMethodQuery(Qt::InputMethodQuery property) const +QVariant QWidgetTextControl::inputMethodQuery(Qt::InputMethodQuery property, QVariant argument) const { Q_D(const QWidgetTextControl); QTextBlock block = d->cursor.block(); @@ -2043,6 +2043,47 @@ QVariant QWidgetTextControl::inputMethodQuery(Qt::InputMethodQuery property) con return QVariant(); // No limit. case Qt::ImAnchorPosition: return QVariant(d->cursor.anchor() - block.position()); + case Qt::ImAbsolutePosition: + return QVariant(d->cursor.position()); + case Qt::ImTextAfterCursor: + { + int maxLength = argument.isValid() ? argument.toInt() : 1024; + QTextCursor tmpCursor = d->cursor; + int localPos = d->cursor.position() - block.position(); + QString result = block.text().mid(localPos); + while (result.length() < maxLength) { + int currentBlock = tmpCursor.blockNumber(); + tmpCursor.movePosition(QTextCursor::NextBlock); + if (tmpCursor.blockNumber() == currentBlock) + break; + result += QLatin1Char('\n') + tmpCursor.block().text(); + } + return QVariant(result); + } + case Qt::ImTextBeforeCursor: + { + int maxLength = argument.isValid() ? argument.toInt() : 1024; + QTextCursor tmpCursor = d->cursor; + int localPos = d->cursor.position() - block.position(); + int numBlocks = 0; + int resultLen = localPos; + while (resultLen < maxLength) { + int currentBlock = tmpCursor.blockNumber(); + tmpCursor.movePosition(QTextCursor::PreviousBlock); + if (tmpCursor.blockNumber() == currentBlock) + break; + numBlocks++; + resultLen += tmpCursor.block().length(); + } + QString result; + while (numBlocks) { + result += tmpCursor.block().text() + QLatin1Char('\n'); + tmpCursor.movePosition(QTextCursor::NextBlock); + --numBlocks; + } + result += block.text().mid(0,localPos); + return QVariant(result); + } default: return QVariant(); } |