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 <jiang.jiang@nokia.com>
(cherry picked from commit 1b031759ddfdab9703dfecac13f1ed318da3dafe)
This commit is contained in:
Pasi Matilainen 2012-01-16 09:10:18 +02:00 committed by Qt by Nokia
parent 0a8671fe50
commit 6dc85408cd
3 changed files with 24 additions and 10 deletions

View File

@ -414,11 +414,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)
@ -530,11 +537,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:

View File

@ -1474,11 +1474,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);
}

View File

@ -1840,11 +1840,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);
}