From 1b031759ddfdab9703dfecac13f1ed318da3dafe Mon Sep 17 00:00:00 2001 From: Pasi Matilainen Date: Mon, 16 Jan 2012 09:10:18 +0200 Subject: QTextEdit cursor position fix when moving left/right with selection When text has been selected in a QTextEdit and the left or right arrow key is pressed, the cursor moves one character beyond the start or end of the selection, when it shouldn't move past the selection. Fixed by moving the cursor to the right place when a selection is active. Task-number: QTBUG-22853 Change-Id: I9ea1863436db98627a6fd041ce554cf10be26493 Reviewed-by: Jiang Jiang --- src/gui/text/qtextcursor.cpp | 26 ++++++++++++++++++------ tests/auto/qplaintextedit/tst_qplaintextedit.cpp | 4 ++-- tests/auto/qtextedit/tst_qtextedit.cpp | 4 ++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/gui/text/qtextcursor.cpp b/src/gui/text/qtextcursor.cpp index 51db578..e6af105 100644 --- a/src/gui/text/qtextcursor.cpp +++ b/src/gui/text/qtextcursor.cpp @@ -421,11 +421,18 @@ bool QTextCursorPrivate::movePosition(QTextCursor::MoveOperation op, QTextCursor break; } case QTextCursor::PreviousCharacter: - newPosition = priv->previousCursorPosition(position, QTextLayout::SkipCharacters); + if (mode == QTextCursor::MoveAnchor && position != adjusted_anchor) + newPosition = qMin(position, adjusted_anchor); + else + newPosition = priv->previousCursorPosition(position, QTextLayout::SkipCharacters); break; case QTextCursor::Left: - newPosition = visualMovement ? priv->leftCursorPosition(position) - : priv->previousCursorPosition(position, QTextLayout::SkipCharacters); + if (mode == QTextCursor::MoveAnchor && position != adjusted_anchor) + newPosition = visualMovement ? qMax(position, adjusted_anchor) + : qMin(position, adjusted_anchor); + else + newPosition = visualMovement ? priv->leftCursorPosition(position) + : priv->previousCursorPosition(position, QTextLayout::SkipCharacters); break; case QTextCursor::StartOfWord: { if (relativePos == 0) @@ -535,11 +542,18 @@ bool QTextCursorPrivate::movePosition(QTextCursor::MoveOperation op, QTextCursor break; } case QTextCursor::NextCharacter: - newPosition = priv->nextCursorPosition(position, QTextLayout::SkipCharacters); + if (mode == QTextCursor::MoveAnchor && position != adjusted_anchor) + newPosition = qMax(position, adjusted_anchor); + else + newPosition = priv->nextCursorPosition(position, QTextLayout::SkipCharacters); break; case QTextCursor::Right: - newPosition = visualMovement ? priv->rightCursorPosition(position) - : priv->nextCursorPosition(position, QTextLayout::SkipCharacters); + if (mode == QTextCursor::MoveAnchor && position != adjusted_anchor) + newPosition = visualMovement ? qMin(position, adjusted_anchor) + : qMax(position, adjusted_anchor); + else + newPosition = visualMovement ? priv->rightCursorPosition(position) + : priv->nextCursorPosition(position, QTextLayout::SkipCharacters); break; case QTextCursor::NextWord: case QTextCursor::WordRight: diff --git a/tests/auto/qplaintextedit/tst_qplaintextedit.cpp b/tests/auto/qplaintextedit/tst_qplaintextedit.cpp index dfb8ca0..3eeae7e 100644 --- a/tests/auto/qplaintextedit/tst_qplaintextedit.cpp +++ b/tests/auto/qplaintextedit/tst_qplaintextedit.cpp @@ -1479,11 +1479,11 @@ void tst_QPlainTextEdit::selectionChanged() QCOMPARE(selectionChangedSpy.count(), 3); QTest::keyClick(ed, Qt::Key_Right); - QCOMPARE(ed->textCursor().position(), 5); + QCOMPARE(ed->textCursor().position(), 4); QCOMPARE(selectionChangedSpy.count(), 4); QTest::keyClick(ed, Qt::Key_Right); - QCOMPARE(ed->textCursor().position(), 6); + QCOMPARE(ed->textCursor().position(), 5); QCOMPARE(selectionChangedSpy.count(), 4); } diff --git a/tests/auto/qtextedit/tst_qtextedit.cpp b/tests/auto/qtextedit/tst_qtextedit.cpp index cef7255..d308332 100644 --- a/tests/auto/qtextedit/tst_qtextedit.cpp +++ b/tests/auto/qtextedit/tst_qtextedit.cpp @@ -1902,11 +1902,11 @@ void tst_QTextEdit::selectionChanged() QCOMPARE(selectionChangedSpy.count(), 3); QTest::keyClick(ed, Qt::Key_Right); - QCOMPARE(ed->textCursor().position(), 5); + QCOMPARE(ed->textCursor().position(), 4); QCOMPARE(selectionChangedSpy.count(), 4); QTest::keyClick(ed, Qt::Key_Right); - QCOMPARE(ed->textCursor().position(), 6); + QCOMPARE(ed->textCursor().position(), 5); QCOMPARE(selectionChangedSpy.count(), 4); } -- cgit v0.12