Commit e0ba90b4 authored by Sebastian Gottfried's avatar Sebastian Gottfried
Browse files

training screen: fix key event handling wrt to CJK input methods

I must confess that I as an European citizin have only little insights
into the nature of those input methods but at least the behavior of
KTouch is now consistent to the normal Qt input widgets when used with
IBUS: We show the preedit text (which we don't check for errors,
obviously) and the suggestions popup, if there is any, is positioned
right next to the cursor caret there it is supposed to be.

BUG: 194819
FIXED-IN: 2.0
parent f3c5ed6a
......@@ -47,6 +47,20 @@ void TrainingLineCore::setActive(bool active)
}
}
QDeclarativeItem* TrainingLineCore::cursorItem() const
{
return m_cursorItem;
}
void TrainingLineCore::setCursorItem(QDeclarativeItem* cursorPosition)
{
if (cursorPosition != m_cursorItem)
{
m_cursorItem = cursorPosition;
emit cursorItemChanged();
}
}
TrainingStats* TrainingLineCore::trainingStats() const
{
return m_trainingStats;
......@@ -82,6 +96,11 @@ QString TrainingLineCore::actualLine() const
return m_actualLine;
}
QString TrainingLineCore::preeditString() const
{
return m_preeditString;
}
bool TrainingLineCore::isCorrect() const
{
return m_actualLine == m_referenceLine.left(m_actualLine.length());
......@@ -206,12 +225,19 @@ void TrainingLineCore::inputMethodEvent(QInputMethodEvent *event)
}
const QString commitString = event->commitString();
const QString preeditString = event->preeditString();
if (!commitString.isEmpty())
{
add(commitString);
}
if (preeditString != m_preeditString)
{
m_preeditString = preeditString;
emit preeditStringChanged();
}
event->accept();
}
......@@ -219,6 +245,10 @@ QVariant TrainingLineCore::inputMethodQuery(Qt::InputMethodQuery query) const
{
switch (query)
{
case Qt::ImMicroFocus:
if (!m_cursorItem)
return QVariant();
return QVariant(m_cursorItem->mapRectToItem(this, m_cursorItem->boundingRect().toRect()));
case Qt::ImCursorPosition:
return QVariant(m_actualLine.length());
case Qt::ImSurroundingText:
......
......@@ -20,15 +20,19 @@
#include <QDeclarativeItem>
#include <QPointer>
class TrainingStats;
class TrainingLineCore : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
Q_PROPERTY(QDeclarativeItem* cursorItem READ cursorItem WRITE setCursorItem NOTIFY cursorItemChanged)
Q_PROPERTY(TrainingStats* trainingStats READ trainingStats WRITE setTrainingStats NOTIFY trainingStatsChanged)
Q_PROPERTY(QString referenceLine READ referenceLine WRITE setReferenceLine NOTIFY referenceLineChanged)
Q_PROPERTY(QString actualLine READ actualLine NOTIFY actualLineChanged)
Q_PROPERTY(QString preeditString READ preeditString NOTIFY preeditStringChanged)
Q_PROPERTY(bool isCorrect READ isCorrect NOTIFY actualLineChanged)
Q_PROPERTY(QString nextCharacter READ nextCharacter NOTIFY actualLineChanged)
Q_PROPERTY(int hintKey READ hintKey NOTIFY hintKeyChanged)
......@@ -36,19 +40,24 @@ public:
explicit TrainingLineCore(QDeclarativeItem* parent = 0);
bool active() const;
void setActive(bool active);
QDeclarativeItem* cursorItem() const;
void setCursorItem(QDeclarativeItem* cursorItem);
TrainingStats* trainingStats() const;
void setTrainingStats(TrainingStats* trainingStats);
QString referenceLine() const;
void setReferenceLine(const QString& referenceLine);
QString actualLine() const;
QString preeditString() const;
bool isCorrect() const;
QString nextCharacter() const;
int hintKey() const;
signals:
void activeChanged();
void cursorItemChanged();
void trainingStatsChanged();
void referenceLineChanged();
void actualLineChanged();
void preeditStringChanged();
void hintKeyChanged();
void done();
protected:
......@@ -66,8 +75,10 @@ private:
TrainingStats* m_trainingStats;
QString m_referenceLine;
QString m_actualLine;
QString m_preeditString;
int m_hintKey;
int m_keyHintOccurrenceCount;
QPointer<QDeclarativeItem> m_cursorItem;
};
#endif // TRAININGLINECORE_H
......@@ -26,6 +26,8 @@ TrainingLineCore {
signal keyPressed(variant event)
signal keyReleased(variant event)
cursorItem: cursor
height: line.fontScale * LessonFontSizeCalculater.BasePixelSize
focus: true
......@@ -69,7 +71,7 @@ TrainingLineCore {
id: lineChars
model: referenceLine.length
Item {
Rectangle {
id: lineCharWrapper
property alias text: lineChar.text
transformOrigin: Item.Center
......@@ -78,16 +80,21 @@ TrainingLineCore {
width: lineChar.width * line.fontScale
height: lineChar.height * line.fontScale
state: {
if (index < line.actualLine.length)
var actualLength = line.actualLine.length
if (index < actualLength)
{
var charCorrect = line.actualLine[index] === line.referenceLine[index]
var previousCorrect = index == 0 || lineChars.itemAt(index - 1).state === "done"
return charCorrect && previousCorrect? "done": "error"
}
else
if (index - actualLength < line.preeditString.length)
{
return "placeholder"
return "preedit"
}
return "placeholder"
}
onStateChanged: {
......@@ -100,7 +107,12 @@ TrainingLineCore {
font.family: "monospace"
font.pixelSize: LessonFontSizeCalculater.BasePixelSize
text: {
var character = index < line.actualLine.length? line.actualLine[index]: line.referenceLine[index]
var actualLength = line.actualLine.length
var character = index < actualLength?
line.actualLine[index]:
index - actualLength < line.preeditString.length?
line.preeditString[index - actualLength]:
line.referenceLine[index]
if (character === " " && lineCharWrapper.state === "error")
return "\u2423"
......@@ -135,13 +147,35 @@ TrainingLineCore {
states: [
State {
name: "placeholder"
PropertyChanges {
target: lineCharWrapper
color: "#00000000"
}
PropertyChanges {
target: lineChar
color: "#888"
}
},
State {
name: "preedit"
PropertyChanges {
target: lineCharWrapper
color: "#d0d0d0"
}
PropertyChanges {
target: lineChar
color: "#000"
}
},
State {
name: "done"
PropertyChanges {
target: lineCharWrapper
color: "#00000000"
}
PropertyChanges {
target: lineChar
color: "#000"
......@@ -149,6 +183,11 @@ TrainingLineCore {
},
State {
name: "error"
PropertyChanges {
target: lineCharWrapper
color: "#00000000"
}
PropertyChanges {
target: lineChar
color: "#f00"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment