From 803727395b3e737cccce530f3aed4564c60a85c5 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 13 Aug 2022 10:40:28 +0200 Subject: [PATCH] desktop: improve composition on TagWidgets The TagWidgets hook into the textChanged() signal to invoke the word-completer. However, that signal is also emitted for composition-keys, making composition impossible if the completer decides that it should show some entries. Instead, hook into the inputMethodEvent() function, where one can test whether a real character was input. Also, don't hook into cursorPositionChanged(), since that led to an uncanny cascade of reparse() calls when editing text. The UI experience is still rough as sometimes the completer popup steals the focus and hinders further entry. Also, this doesn't fix the location field, which is its own class. Signed-off-by: Berthold Stoeger --- desktop-widgets/tagwidget.cpp | 15 ++++++++++----- desktop-widgets/tagwidget.h | 8 ++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/desktop-widgets/tagwidget.cpp b/desktop-widgets/tagwidget.cpp index ad5ff7c38..093317d3b 100644 --- a/desktop-widgets/tagwidget.cpp +++ b/desktop-widgets/tagwidget.cpp @@ -7,9 +7,6 @@ TagWidget::TagWidget(QWidget *parent) : GroupedLineEdit(parent), m_completer(NULL), lastFinishedTag(false) { - connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(reparse())); - connect(this, SIGNAL(textChanged()), this, SLOT(reparse())); - QColor textColor = palette().color(QPalette::Text); #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) float h, s, l, a; @@ -39,8 +36,8 @@ void TagWidget::setCompleter(QCompleter *completer) { m_completer = completer; m_completer->setWidget(this); - connect(m_completer, SIGNAL(activated(QString)), this, SLOT(completionSelected(QString))); - connect(m_completer, SIGNAL(highlighted(QString)), this, SLOT(completionHighlighted(QString))); + connect(m_completer, QOverload::of(&QCompleter::activated), this, &TagWidget::completionSelected); + connect(m_completer, QOverload::of(&QCompleter::highlighted), this, &TagWidget::completionHighlighted); } #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) @@ -92,6 +89,13 @@ void TagWidget::highlight() } } +void TagWidget::inputMethodEvent(QInputMethodEvent *e) +{ + GroupedLineEdit::inputMethodEvent(e); + if (!e->commitString().isEmpty()) + reparse(); +} + void TagWidget::reparse() { highlight(); @@ -196,6 +200,7 @@ void TagWidget::keyPressEvent(QKeyEvent *e) keyPressEvent(&fakeEvent); } else { GroupedLineEdit::keyPressEvent(e); + reparse(); } lastFinishedTag = finishedTag; } diff --git a/desktop-widgets/tagwidget.h b/desktop-widgets/tagwidget.h index 83e8b86b5..ea8a7ed93 100644 --- a/desktop-widgets/tagwidget.h +++ b/desktop-widgets/tagwidget.h @@ -18,20 +18,20 @@ public: void clear(); void setCursorPosition(int position); void wheelEvent(QWheelEvent *event); -public +private slots: - void reparse(); void completionSelected(const QString &text); void completionHighlighted(const QString &text); -protected: +private: void keyPressEvent(QKeyEvent *e) override; void dragEnterEvent(QDragEnterEvent *e) override; void dragLeaveEvent(QDragLeaveEvent *e) override; void dragMoveEvent(QDragMoveEvent *e) override; void dropEvent(QDropEvent *e) override; -private: void focusOutEvent(QFocusEvent *ev) override; + void inputMethodEvent(QInputMethodEvent *e) override; + void reparse(); QCompleter *m_completer; bool lastFinishedTag; };