summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJan Arne Petersen <jan.petersen@kdab.com>2016-04-18 21:44:57 +0200
committerJan Arne Petersen <jan.petersen@kdab.com>2016-04-19 08:44:50 +0000
commitc5ab939aa4f2683c7be61e9b788d50526b811ab8 (patch)
tree168241f2366ce9c74510a91e984a3c219c2f8b47 /src
parentbc9eac9800811417697bebf35c5f11b0e852d123 (diff)
Use function to convert indices for text protocol
Use a proper function instead of left/midRef(index).toUtf8().size(). Makes it more clear what is happening and makes it easier when wayland text protocol switches from byte-based to unicode character-based indices. Also rename parameters of existing indexFromWayland() function to be the same as in the new function. Change-Id: Ie90abdcb264b74a024e96e041d5daf651920e9e4 Reviewed-by: Giulio Camuffo <giulio.camuffo@kdab.com>
Diffstat (limited to 'src')
-rw-r--r--src/client/qwaylandinputcontext.cpp2
-rw-r--r--src/compositor/extensions/qwaylandtextinput.cpp14
-rw-r--r--src/shared/qwaylandinputmethodeventbuilder.cpp21
-rw-r--r--src/shared/qwaylandinputmethodeventbuilder.h3
4 files changed, 23 insertions, 17 deletions
diff --git a/src/client/qwaylandinputcontext.cpp b/src/client/qwaylandinputcontext.cpp
index f211780fd..af8e6f818 100644
--- a/src/client/qwaylandinputcontext.cpp
+++ b/src/client/qwaylandinputcontext.cpp
@@ -157,7 +157,7 @@ void QWaylandTextInput::updateState(Qt::InputMethodQueries queries, uint32_t fla
anchor -= offset;
}
- set_surrounding_text(text, text.leftRef(cursor).toUtf8().size(), text.leftRef(anchor).toUtf8().size());
+ set_surrounding_text(text, QWaylandInputMethodEventBuilder::indexToWayland(text, cursor), QWaylandInputMethodEventBuilder::indexToWayland(text, anchor));
}
if (queries & Qt::ImHints) {
diff --git a/src/compositor/extensions/qwaylandtextinput.cpp b/src/compositor/extensions/qwaylandtextinput.cpp
index 2d317dcb0..530627641 100644
--- a/src/compositor/extensions/qwaylandtextinput.cpp
+++ b/src/compositor/extensions/qwaylandtextinput.cpp
@@ -157,8 +157,8 @@ void QWaylandTextInputPrivate::sendInputMethodEvent(QInputMethodEvent *event)
if (event->replacementStart() <= 0 && (event->replacementLength() >= -event->replacementStart())) {
const int selectionStart = qMin(currentState->cursorPosition, currentState->anchorPosition);
const int selectionEnd = qMax(currentState->cursorPosition, currentState->anchorPosition);
- const int before = currentState->surroundingText.midRef(selectionStart + event->replacementStart(), -event->replacementStart()).toUtf8().size();
- const int after = currentState->surroundingText.midRef(selectionEnd, event->replacementLength() + event->replacementStart()).toUtf8().size();
+ const int before = QWaylandInputMethodEventBuilder::indexToWayland(currentState->surroundingText, -event->replacementStart(), selectionStart + event->replacementStart());
+ const int after = QWaylandInputMethodEventBuilder::indexToWayland(currentState->surroundingText, event->replacementLength() + event->replacementStart(), selectionEnd);
send_delete_surrounding_text(focusResource->handle, before, after);
} else {
// TODO: Implement this case
@@ -175,8 +175,8 @@ void QWaylandTextInputPrivate::sendInputMethodEvent(QInputMethodEvent *event)
if (attribute.type == QInputMethodEvent::Selection) {
afterCommit.cursorPosition = attribute.start;
afterCommit.anchorPosition = attribute.length;
- int cursor = afterCommit.surroundingText.midRef(qMin(attribute.start, afterCommit.cursorPosition), qAbs(attribute.start - afterCommit.cursorPosition)).toUtf8().size();
- int anchor = afterCommit.surroundingText.midRef(qMin(attribute.length, afterCommit.cursorPosition), qAbs(attribute.length - afterCommit.cursorPosition)).toUtf8().size();
+ int cursor = QWaylandInputMethodEventBuilder::indexToWayland(afterCommit.surroundingText, qAbs(attribute.start - afterCommit.cursorPosition), qMin(attribute.start, afterCommit.cursorPosition));
+ int anchor = QWaylandInputMethodEventBuilder::indexToWayland(afterCommit.surroundingText, qAbs(attribute.length - afterCommit.cursorPosition), qMin(attribute.length, afterCommit.cursorPosition));
send_cursor_position(focusResource->handle,
attribute.start < afterCommit.cursorPosition ? -cursor : cursor,
attribute.length < afterCommit.cursorPosition ? -anchor : anchor);
@@ -185,11 +185,11 @@ void QWaylandTextInputPrivate::sendInputMethodEvent(QInputMethodEvent *event)
send_commit_string(focusResource->handle, event->commitString());
foreach (const QInputMethodEvent::Attribute &attribute, event->attributes()) {
if (attribute.type == QInputMethodEvent::Cursor) {
- int index = event->preeditString().leftRef(attribute.start).toUtf8().size();
+ int index = QWaylandInputMethodEventBuilder::indexToWayland(event->preeditString(), attribute.start);
send_preedit_cursor(focusResource->handle, index);
} else if (attribute.type == QInputMethodEvent::TextFormat) {
- int start = currentState->surroundingText.leftRef(attribute.start).toUtf8().size();
- int end = currentState->surroundingText.leftRef(attribute.start + attribute.length).toUtf8().size();
+ int start = QWaylandInputMethodEventBuilder::indexToWayland(currentState->surroundingText, attribute.start);
+ int end = QWaylandInputMethodEventBuilder::indexToWayland(currentState->surroundingText, attribute.start + attribute.length);
// TODO add support for different stylesQWaylandTextInput
send_preedit_styling(focusResource->handle, start, end - start, preedit_style_default);
}
diff --git a/src/shared/qwaylandinputmethodeventbuilder.cpp b/src/shared/qwaylandinputmethodeventbuilder.cpp
index eb527c15a..fe93f5146 100644
--- a/src/shared/qwaylandinputmethodeventbuilder.cpp
+++ b/src/shared/qwaylandinputmethodeventbuilder.cpp
@@ -268,19 +268,24 @@ QWaylandInputMethodContentType QWaylandInputMethodContentType::convert(Qt::Input
return QWaylandInputMethodContentType{hint, purpose};
}
-int QWaylandInputMethodEventBuilder::indexFromWayland(const QString &str, int utf8Index, int baseIndex)
+int QWaylandInputMethodEventBuilder::indexFromWayland(const QString &text, int length, int base)
{
- if (utf8Index == 0)
- return baseIndex;
+ if (length == 0)
+ return base;
- if (utf8Index < 0) {
- const QByteArray &utf8 = str.leftRef(baseIndex).toUtf8();
- return QString::fromUtf8(utf8.left(qMax(utf8.length() + utf8Index, 0))).length();
+ if (length < 0) {
+ const QByteArray &utf8 = text.leftRef(base).toUtf8();
+ return QString::fromUtf8(utf8.left(qMax(utf8.length() + length, 0))).length();
} else {
- const QByteArray &utf8 = str.midRef(baseIndex).toUtf8();
- return QString::fromUtf8(utf8.left(utf8Index)).length() + baseIndex;
+ const QByteArray &utf8 = text.midRef(base).toUtf8();
+ return QString::fromUtf8(utf8.left(length)).length() + base;
}
}
+int QWaylandInputMethodEventBuilder::indexToWayland(const QString &text, int length, int base)
+{
+ return text.midRef(base, length).toUtf8().size();
+}
+
QT_END_NAMESPACE
diff --git a/src/shared/qwaylandinputmethodeventbuilder.h b/src/shared/qwaylandinputmethodeventbuilder.h
index 188a6a94b..3912afc04 100644
--- a/src/shared/qwaylandinputmethodeventbuilder.h
+++ b/src/shared/qwaylandinputmethodeventbuilder.h
@@ -61,7 +61,8 @@ public:
QInputMethodEvent buildCommit(const QString &text);
QInputMethodEvent buildPreedit(const QString &text);
- static int indexFromWayland(const QString &str, int utf8Index, int baseIndex = 0);
+ static int indexFromWayland(const QString &text, int length, int base = 0);
+ static int indexToWayland(const QString &text, int length, int base = 0);
private:
QPair<int, int> replacementForDeleteSurrounding();