aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJarkko Koivikko <jarkko.koivikko@code-q.fi>2015-02-06 12:40:06 +0200
committerJarkko Koivikko <jarkko.koivikko@code-q.fi>2015-02-24 16:16:30 +0200
commit93dbde8aa65dc0cfbf574b025c0b9a595e2775f7 (patch)
tree09d42a2a9d7bc4b0b66d78263dd4a550d9caa61e
parenta699f1148f742b0648ea1f9f41b1a27d43c3f588 (diff)
Allow custom attributes for pre-edit text
This change allows the input methods to use custom text attributes for the pre-edit text. Change-Id: Ia5064f29b8454cf3ec70d042eef9969e80dd9735 Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
-rw-r--r--src/virtualkeyboard/declarativeinputcontext.cpp64
-rw-r--r--src/virtualkeyboard/declarativeinputcontext.h5
2 files changed, 44 insertions, 25 deletions
diff --git a/src/virtualkeyboard/declarativeinputcontext.cpp b/src/virtualkeyboard/declarativeinputcontext.cpp
index 4dbb6522..a511ee8e 100644
--- a/src/virtualkeyboard/declarativeinputcontext.cpp
+++ b/src/virtualkeyboard/declarativeinputcontext.cpp
@@ -23,7 +23,6 @@
#include "virtualkeyboarddebug.h"
#include "enterkeyaction.h"
-#include <QEvent>
#include <QTextFormat>
#include <QGuiApplication>
#include <QtCore/private/qobject_p.h>
@@ -64,6 +63,7 @@ public:
cursorPosition(0),
inputMethodHints(Qt::ImhNone),
preeditText(),
+ preeditTextAttributes(),
surroundingText(),
selectedText(),
cursorRectangle()
@@ -86,6 +86,7 @@ public:
int cursorPosition;
Qt::InputMethodHints inputMethodHints;
QString preeditText;
+ QList<QInputMethodEvent::Attribute> preeditTextAttributes;
QString surroundingText;
QString selectedText;
QRectF cursorRectangle;
@@ -192,11 +193,26 @@ QString DeclarativeInputContext::preeditText() const
return d->preeditText;
}
-void DeclarativeInputContext::setPreeditText(const QString &text)
+void DeclarativeInputContext::setPreeditText(const QString &text, QList<QInputMethodEvent::Attribute> attributes)
{
- Q_D(DeclarativeInputContext);
- if (text != d->preeditText)
- sendPreedit(text);
+ // Add default attributes
+ if (!text.isEmpty()) {
+ bool containsTextFormat = false;
+ for (QList<QInputMethodEvent::Attribute>::ConstIterator attribute = attributes.constBegin();
+ attribute != attributes.constEnd(); attribute++) {
+ if (attribute->type == QInputMethodEvent::TextFormat) {
+ containsTextFormat = true;
+ break;
+ }
+ }
+ if (!containsTextFormat) {
+ QTextCharFormat textFormat;
+ textFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline);
+ attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, text.length(), textFormat));
+ }
+ }
+
+ sendPreedit(text, attributes);
}
QString DeclarativeInputContext::surroundingText() const
@@ -462,32 +478,34 @@ void DeclarativeInputContext::setFocus(bool enable)
emit focusEditorChanged();
}
-void DeclarativeInputContext::sendPreedit(const QString &text, int cursor)
+bool operator==(const QInputMethodEvent::Attribute &attribute1, const QInputMethodEvent::Attribute &attribute2)
+{
+ return attribute1.start == attribute2.start &&
+ attribute1.length == attribute2.length &&
+ attribute1.type == attribute2.type &&
+ attribute1.value == attribute2.value;
+}
+
+void DeclarativeInputContext::sendPreedit(const QString &text, const QList<QInputMethodEvent::Attribute> &attributes)
{
Q_D(DeclarativeInputContext);
- VIRTUALKEYBOARD_DEBUG() << "DeclarativeInputContext::sendPreedit():" << text << cursor;
- const QString preedit = d->preeditText;
- d->preeditText = text;
+ VIRTUALKEYBOARD_DEBUG() << "DeclarativeInputContext::sendPreedit():" << text;
- if (d->inputContext) {
- QList<QInputMethodEvent::Attribute> attributes;
+ bool textChanged = d->preeditText != text;
+ bool attributesChanged = d->preeditTextAttributes != attributes;
- if (cursor >= 0 && cursor <= text.length()) {
- attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, cursor, text.length(), QVariant()));
- }
+ if (textChanged || attributesChanged) {
+ d->preeditText = text;
+ d->preeditTextAttributes = attributes;
- if (!d->preeditText.isEmpty()) {
- QTextCharFormat textFormat;
- textFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline);
- attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, text.length(), textFormat));
+ if (d->inputContext) {
+ QInputMethodEvent event(text, attributes);
+ d->inputContext->sendEvent(&event);
}
- QInputMethodEvent event(text, attributes);
- d->inputContext->sendEvent(&event);
+ if (textChanged)
+ emit preeditTextChanged();
}
-
- if (d->preeditText != preedit)
- emit preeditTextChanged();
}
void DeclarativeInputContext::reset()
diff --git a/src/virtualkeyboard/declarativeinputcontext.h b/src/virtualkeyboard/declarativeinputcontext.h
index 341bbe03..92be12d4 100644
--- a/src/virtualkeyboard/declarativeinputcontext.h
+++ b/src/virtualkeyboard/declarativeinputcontext.h
@@ -22,6 +22,7 @@
#include <QObject>
#include <QRectF>
#include <QLocale>
+#include <QInputMethodEvent>
class PlatformInputContext;
class DeclarativeInputEngine;
@@ -63,7 +64,7 @@ public:
int cursorPosition() const;
Qt::InputMethodHints inputMethodHints() const;
QString preeditText() const;
- void setPreeditText(const QString &text);
+ void setPreeditText(const QString &text, QList<QInputMethodEvent::Attribute> attributes = QList<QInputMethodEvent::Attribute>());
QString surroundingText() const;
QString selectedText() const;
QRectF cursorRectangle() const;
@@ -113,7 +114,7 @@ signals:
private:
void setFocus(bool enable);
- void sendPreedit(const QString &text, int cursor = -1);
+ void sendPreedit(const QString &text, const QList<QInputMethodEvent::Attribute> &attributes);
void reset();
void externalCommit();
void update(Qt::InputMethodQueries queries);