diff options
author | Andrew den Exter <andrew.den-exter@nokia.com> | 2011-02-28 06:54:08 (GMT) |
---|---|---|
committer | Andrew den Exter <andrew.den-exter@nokia.com> | 2011-03-01 06:56:46 (GMT) |
commit | 119a8ddcd7c8de0607309b37f9ef83439b607f17 (patch) | |
tree | 83eff83c432946235437466fa15a6233a50aaa50 /src/declarative/graphicsitems | |
parent | 70be048712ba3516b79dbf2b1b0b4677fdc7ee0f (diff) | |
download | Qt-119a8ddcd7c8de0607309b37f9ef83439b607f17.zip Qt-119a8ddcd7c8de0607309b37f9ef83439b607f17.tar.gz Qt-119a8ddcd7c8de0607309b37f9ef83439b607f17.tar.bz2 |
Don't return positions within preedit text from positionAt().
Preedit text isn't visible outside the inner workings of the Text
elements so any position returned that is greater than the current
cursor position is invalid.
Change-Id: I433fc635dcd6d087bde13fc47a1a92e4222a3b2c
Task-number: QTBUG-17834
Reviewed-by: Martin Jones
Diffstat (limited to 'src/declarative/graphicsitems')
3 files changed, 31 insertions, 6 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 9148e88..c28d7f4 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -606,6 +606,22 @@ int QDeclarativeTextEdit::positionAt(int x, int y) const { Q_D(const QDeclarativeTextEdit); int r = d->document->documentLayout()->hitTest(QPoint(x,y-d->yoff), Qt::FuzzyHit); + QTextCursor cursor = d->control->textCursor(); + if (r > cursor.position()) { + // The cursor position includes positions within the preedit text, but only positions in the + // same text block are offset so it is possible to get a position that is either part of the + // preedit or the next text block. + QTextLayout *layout = cursor.block().layout(); + const int preeditLength = layout + ? layout->preeditAreaText().length() + : 0; + if (preeditLength > 0 + && d->document->documentLayout()->blockBoundingRect(cursor.block()).contains(x,y-d->yoff)) { + r = r > cursor.position() + preeditLength + ? r - preeditLength + : cursor.position(); + } + } return r; } diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 88b55df..c873172 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -921,6 +921,8 @@ void QDeclarativeTextInput::moveCursor() QRectF QDeclarativeTextInput::positionToRectangle(int pos) const { Q_D(const QDeclarativeTextInput); + if (pos > d->control->cursorPosition()) + pos += d->control->preeditAreaText().length(); return QRectF(d->control->cursorToX(pos)-d->hscroll, 0.0, d->control->cursorWidth(), @@ -929,8 +931,7 @@ QRectF QDeclarativeTextInput::positionToRectangle(int pos) const int QDeclarativeTextInput::positionAt(int x) const { - Q_D(const QDeclarativeTextInput); - return d->control->xToPos(x + d->hscroll); + return positionAt(x, CursorBetweenCharacters); } /*! @@ -952,10 +953,18 @@ int QDeclarativeTextInput::positionAt(int x) const \o TextInput.CursorOnCharacter - Returns the position before the character that is nearest x. \endlist */ -int QDeclarativeTextInput::positionAt(int x, CursorPosition position) +int QDeclarativeTextInput::positionAt(int x, CursorPosition position) const { Q_D(const QDeclarativeTextInput); - return d->control->xToPos(x + d->hscroll, QTextLine::CursorPosition(position)); + int pos = d->control->xToPos(x + d->hscroll, QTextLine::CursorPosition(position)); + const int cursor = d->control->cursor(); + if (pos > cursor) { + const int preeditLength = d->control->preeditAreaText().length(); + pos = pos > cursor + preeditLength + ? pos - preeditLength + : cursor; + } + return pos; } void QDeclarativeTextInputPrivate::focusChanged(bool hasFocus) @@ -1065,7 +1074,7 @@ void QDeclarativeTextInput::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { Q_D(QDeclarativeTextInput); if (d->sendMouseEventToInputContext(event, QEvent::MouseMove)) - event->setAccepted(true); + return; if (d->selectByMouse) { if (qAbs(int(event->pos().x() - d->pressPos.x())) > QApplication::startDragDistance()) setKeepMouseGrab(true); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h index 79f5230..822d5e2 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h @@ -128,7 +128,7 @@ public: //Auxilliary functions needed to control the TextInput from QML Q_INVOKABLE int positionAt(int x) const; - Q_INVOKABLE Q_REVISION(1) int positionAt(int x, CursorPosition position); + Q_INVOKABLE Q_REVISION(1) int positionAt(int x, CursorPosition position) const; Q_INVOKABLE QRectF positionToRectangle(int pos) const; Q_INVOKABLE void moveCursorSelection(int pos); Q_INVOKABLE Q_REVISION(1) void moveCursorSelection(int pos, SelectionMode mode); |