From 5391f3c72f884fdea0861ceca814d247ef552397 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Mon, 11 Apr 2016 14:23:22 +0200 Subject: QLineEdit: add support for Qt::ImAnchorRectangle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I52e15aa40af5ff77306c43209af87a3e727d2bdb Reviewed-by: Jan Arve Sæther --- src/widgets/widgets/qlineedit.cpp | 2 ++ src/widgets/widgets/qwidgetlinecontrol.cpp | 33 ++++++++++++++++++++++++------ src/widgets/widgets/qwidgetlinecontrol_p.h | 2 ++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/widgets/widgets/qlineedit.cpp b/src/widgets/widgets/qlineedit.cpp index 5e88c2034a..499710d18e 100644 --- a/src/widgets/widgets/qlineedit.cpp +++ b/src/widgets/widgets/qlineedit.cpp @@ -1758,6 +1758,8 @@ QVariant QLineEdit::inputMethodQuery(Qt::InputMethodQuery property, QVariant arg switch(property) { case Qt::ImCursorRectangle: return d->cursorRect(); + case Qt::ImAnchorRectangle: + return d->adjustedControlRect(d->control->anchorRect()); case Qt::ImFont: return font(); case Qt::ImCursorPosition: { diff --git a/src/widgets/widgets/qwidgetlinecontrol.cpp b/src/widgets/widgets/qwidgetlinecontrol.cpp index af8e5a8b42..24edca172b 100644 --- a/src/widgets/widgets/qwidgetlinecontrol.cpp +++ b/src/widgets/widgets/qwidgetlinecontrol.cpp @@ -407,22 +407,43 @@ int QWidgetLineControl::xToPos(int x, QTextLine::CursorPosition betweenOrOn) con /*! \internal - Returns the bounds of the current cursor, as defined as a - between characters cursor. + Returns the bounds of the given text position. */ -QRect QWidgetLineControl::cursorRect() const +QRect QWidgetLineControl::rectForPos(int pos) const { QTextLine l = textLayout()->lineAt(0); - int c = m_cursor; if (m_preeditCursor != -1) - c += m_preeditCursor; - int cix = qRound(l.cursorToX(c)); + pos += m_preeditCursor; + int cix = qRound(l.cursorToX(pos)); int w = m_cursorWidth; int ch = l.height() + 1; return QRect(cix-5, 0, w+9, ch); } +/*! + \internal + + Returns the bounds of the current cursor, as defined as a + between characters cursor. +*/ +QRect QWidgetLineControl::cursorRect() const +{ + return rectForPos(m_cursor); +} + +/*! + \internal + + Returns the bounds of the current anchor +*/ +QRect QWidgetLineControl::anchorRect() const +{ + if (!hasSelectedText()) + return cursorRect(); + return rectForPos(m_selstart < m_selend ? m_selstart : m_selend); +} + /*! \internal diff --git a/src/widgets/widgets/qwidgetlinecontrol_p.h b/src/widgets/widgets/qwidgetlinecontrol_p.h index 6fadb64e0c..8b723b0224 100644 --- a/src/widgets/widgets/qwidgetlinecontrol_p.h +++ b/src/widgets/widgets/qwidgetlinecontrol_p.h @@ -212,7 +212,9 @@ public: void end(bool mark) { moveCursor(text().length(), mark); } int xToPos(int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters) const; + QRect rectForPos(int pos) const; QRect cursorRect() const; + QRect anchorRect() const; qreal cursorToX(int cursor) const { return m_textLayout.lineAt(0).cursorToX(cursor); } qreal cursorToX() const -- cgit v1.2.3