From 4d1c7a4f2f8f451d13d9a5bffc4c44c3221c3ca1 Mon Sep 17 00:00:00 2001 From: axis Date: Wed, 12 Aug 2009 10:09:50 +0200 Subject: Redid 7b95b70bc307 for QLineControl. --- src/gui/widgets/qlinecontrol.cpp | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/gui/widgets/qlinecontrol.cpp b/src/gui/widgets/qlinecontrol.cpp index f4a2348..80215e1 100644 --- a/src/gui/widgets/qlinecontrol.cpp +++ b/src/gui/widgets/qlinecontrol.cpp @@ -400,8 +400,17 @@ void QLineControl::moveCursor(int pos, bool mark) */ void QLineControl::processInputMethodEvent(QInputMethodEvent *event) { - int priorState = m_undoState; - removeSelectedText(); + int priorState = 0; + bool isGettingInput = !event->commitString().isEmpty() || !event->preeditString().isEmpty() + || event->replacementLength() > 0; + bool cursorPositionChanged = false; + + if (isGettingInput) { + // If any text is being input, remove selected text. + priorState = m_undoState; + removeSelectedText(); + } + int c = m_cursor; // cursor position after insertion of commit string if (event->replacementStart() <= 0) @@ -415,11 +424,30 @@ void QLineControl::processInputMethodEvent(QInputMethodEvent *event) m_selend = m_selstart + event->replacementLength(); removeSelectedText(); } - if (!event->commitString().isEmpty()) + if (!event->commitString().isEmpty()) { insert(event->commitString()); + cursorPositionChanged = true; + } m_cursor = qMin(c, m_text.length()); + for (int i = 0; i < event->attributes().size(); ++i) { + const QInputMethodEvent::Attribute &a = event->attributes().at(i); + if (a.type == QInputMethodEvent::Selection) { + m_cursor = qBound(0, a.start + a.length, m_text.length()); + if (a.length) { + m_selstart = qMax(0, qMin(a.start, m_text.length())); + m_selend = m_cursor; + if (m_selend < m_selstart) { + qSwap(m_selstart, m_selend); + } + } else { + m_selstart = m_selend = 0; + } + cursorPositionChanged = true; + } + } + setPreeditArea(m_cursor, event->preeditString()); m_preeditCursor = event->preeditString().length(); m_hideCursor = false; @@ -442,9 +470,10 @@ void QLineControl::processInputMethodEvent(QInputMethodEvent *event) } m_textLayout.setAdditionalFormats(formats); updateDisplayText(); - if (!event->commitString().isEmpty()) + if (cursorPositionChanged) emitCursorPositionChanged(); - finishChange(priorState); + if (isGettingInput) + finishChange(priorState); } /*! -- cgit v0.12