From 4027ad0b817f24ef746afc5069c7db4af9e0dc6a Mon Sep 17 00:00:00 2001 From: reionwong Date: Fri, 23 Jul 2021 16:35:28 +0800 Subject: [PATCH] Fix input method does not follow cursor --- qmltermwidget/lib/TerminalDisplay.cpp | 52 +++++++-------------------- qmltermwidget/lib/TerminalDisplay.h | 7 ++-- 2 files changed, 15 insertions(+), 44 deletions(-) diff --git a/qmltermwidget/lib/TerminalDisplay.cpp b/qmltermwidget/lib/TerminalDisplay.cpp index b753164..20675d5 100644 --- a/qmltermwidget/lib/TerminalDisplay.cpp +++ b/qmltermwidget/lib/TerminalDisplay.cpp @@ -1,6 +1,7 @@ /* This file is part of Konsole, a terminal emulator for KDE. + Copyright 2021 by Reion Wong Copyright 2006-2008 by Robert Knight Copyright 1997,1998 by Lars Doelle @@ -2817,9 +2818,9 @@ void TerminalDisplay::keyPressEvent( QKeyEvent* event ) event->accept(); } -void TerminalDisplay::inputMethodEvent( QInputMethodEvent* event ) +void TerminalDisplay::inputMethodEvent(QInputMethodEvent *event) { - QKeyEvent keyEvent(QEvent::KeyPress,0,Qt::NoModifier,event->commitString()); + QKeyEvent keyEvent(QEvent::KeyPress, 0, Qt::NoModifier, event->commitString()); emit keyPressedSignal(&keyEvent); _inputMethodData.preeditString = event->preeditString().toStdWString(); @@ -2830,46 +2831,19 @@ void TerminalDisplay::inputMethodEvent( QInputMethodEvent* event ) void TerminalDisplay::inputMethodQuery(QInputMethodQueryEvent *event) { + QPoint cursorPos = _screenWindow ? _screenWindow->cursorPosition() : QPoint(0, 0); + event->setValue(Qt::ImEnabled, true); - event->setValue(Qt::ImHints, QVariant(Qt::ImhNoPredictiveText | Qt::ImhNoAutoUppercase)); - event->accept(); -} -QVariant TerminalDisplay::inputMethodQuery( Qt::InputMethodQuery query ) const -{ - const QPoint cursorPos = _screenWindow ? _screenWindow->cursorPosition() : QPoint(0,0); - switch ( query ) - { - case Qt::ImMicroFocus: - return imageToWidget(QRect(cursorPos.x(),cursorPos.y(),1,1)); - break; - case Qt::ImFont: - return font(); - break; - case Qt::ImCursorPosition: - // return the cursor position within the current line - return cursorPos.x(); - break; - case Qt::ImSurroundingText: - { - // return the text from the current line - QString lineText; - QTextStream stream(&lineText); - PlainTextDecoder decoder; - decoder.begin(&stream); - decoder.decodeLine(&_image[loc(0,cursorPos.y())],_usedColumns,_lineProperties[cursorPos.y()]); - decoder.end(); - return lineText; - } - break; - case Qt::ImCurrentSelection: - return QString(); - break; - default: - break; - } + // Reion: Use ImCursorRectangle instead of ImMicroFocus. + event->setValue(Qt::ImCursorRectangle, imageToWidget(QRect(cursorPos.x(), cursorPos.y(), 1, 1))); + + // cursor position within the current line + event->setValue(Qt::ImCursorPosition, cursorPos.x()); - return QVariant(); + event->setValue(Qt::ImFont, font()); + event->setValue(Qt::ImHints, QVariant(Qt::ImhNoPredictiveText | Qt::ImhNoAutoUppercase)); + event->accept(); } bool TerminalDisplay::handleShortcutOverrideEvent(QKeyEvent* keyEvent) diff --git a/qmltermwidget/lib/TerminalDisplay.h b/qmltermwidget/lib/TerminalDisplay.h index 8f1ae6d..94d97cc 100644 --- a/qmltermwidget/lib/TerminalDisplay.h +++ b/qmltermwidget/lib/TerminalDisplay.h @@ -670,17 +670,15 @@ protected: void mouseTripleClickEvent(QMouseEvent* ev); // reimplemented - virtual void inputMethodEvent ( QInputMethodEvent* event ); - virtual QVariant inputMethodQuery( Qt::InputMethodQuery query ) const; + void inputMethodEvent(QInputMethodEvent *event); // QMLTermWidget void paint(QPainter * painter); - virtual void geometryChanged(const QRectF & newGeometry, const QRectF & oldGeometry); + void geometryChanged(const QRectF & newGeometry, const QRectF & oldGeometry); void inputMethodQuery(QInputMethodQueryEvent *event); void itemChange(ItemChange change, const ItemChangeData & value); protected slots: - void scrollBarPositionChanged(int value); void blinkEvent(); void blinkCursorEvent(); @@ -690,7 +688,6 @@ protected slots: void enableBell(); private slots: - void swapColorTable(); void tripleClickTimeout(); // resets possibleTripleClick