forked from qt-creator/qt-creator
Editor: Avoid setting text cursor when clearing block selection
Task-number: QTCREATORBUG-17475 Change-Id: I0266530c8c2ba10bd14dcb26b946cc51a825600f Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -423,7 +423,13 @@ public:
|
|||||||
void enableBlockSelection(const QTextCursor &cursor);
|
void enableBlockSelection(const QTextCursor &cursor);
|
||||||
void enableBlockSelection(int positionBlock, int positionColumn,
|
void enableBlockSelection(int positionBlock, int positionColumn,
|
||||||
int anchorBlock, int anchorColumn);
|
int anchorBlock, int anchorColumn);
|
||||||
void disableBlockSelection(bool keepSelection = true);
|
|
||||||
|
enum BlockSelectionUpdateKind {
|
||||||
|
NoCursorUpdate,
|
||||||
|
CursorUpdateKeepSelection,
|
||||||
|
CursorUpdateClearSelection,
|
||||||
|
};
|
||||||
|
void disableBlockSelection(BlockSelectionUpdateKind kind);
|
||||||
void resetCursorFlashTimer();
|
void resetCursorFlashTimer();
|
||||||
QBasicTimer m_cursorFlashTimer;
|
QBasicTimer m_cursorFlashTimer;
|
||||||
bool m_cursorVisible;
|
bool m_cursorVisible;
|
||||||
@@ -1469,7 +1475,7 @@ void TextEditorWidget::insertLineAbove()
|
|||||||
void TextEditorWidget::insertLineBelow()
|
void TextEditorWidget::insertLineBelow()
|
||||||
{
|
{
|
||||||
if (d->m_inBlockSelectionMode)
|
if (d->m_inBlockSelectionMode)
|
||||||
d->disableBlockSelection(false);
|
d->disableBlockSelection(TextEditorWidgetPrivate::NoCursorUpdate);
|
||||||
QTextCursor cursor = textCursor();
|
QTextCursor cursor = textCursor();
|
||||||
cursor.beginEditBlock();
|
cursor.beginEditBlock();
|
||||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::MoveAnchor);
|
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::MoveAnchor);
|
||||||
@@ -1526,14 +1532,14 @@ void TextEditorWidget::unindent()
|
|||||||
void TextEditorWidget::undo()
|
void TextEditorWidget::undo()
|
||||||
{
|
{
|
||||||
if (d->m_inBlockSelectionMode)
|
if (d->m_inBlockSelectionMode)
|
||||||
d->disableBlockSelection(false);
|
d->disableBlockSelection(TextEditorWidgetPrivate::CursorUpdateClearSelection);
|
||||||
QPlainTextEdit::undo();
|
QPlainTextEdit::undo();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidget::redo()
|
void TextEditorWidget::redo()
|
||||||
{
|
{
|
||||||
if (d->m_inBlockSelectionMode)
|
if (d->m_inBlockSelectionMode)
|
||||||
d->disableBlockSelection(false);
|
d->disableBlockSelection(TextEditorWidgetPrivate::CursorUpdateClearSelection);
|
||||||
QPlainTextEdit::redo();
|
QPlainTextEdit::redo();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1572,7 +1578,7 @@ void TextEditorWidgetPrivate::moveLineUpDown(bool up)
|
|||||||
|
|
||||||
if (hasSelection) {
|
if (hasSelection) {
|
||||||
if (m_inBlockSelectionMode)
|
if (m_inBlockSelectionMode)
|
||||||
disableBlockSelection(true);
|
disableBlockSelection(NoCursorUpdate);
|
||||||
move.setPosition(cursor.selectionStart());
|
move.setPosition(cursor.selectionStart());
|
||||||
move.movePosition(QTextCursor::StartOfBlock);
|
move.movePosition(QTextCursor::StartOfBlock);
|
||||||
move.setPosition(cursor.selectionEnd(), QTextCursor::KeepAnchor);
|
move.setPosition(cursor.selectionEnd(), QTextCursor::KeepAnchor);
|
||||||
@@ -2114,7 +2120,7 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e)
|
|||||||
&& (e == QKeySequence::InsertParagraphSeparator
|
&& (e == QKeySequence::InsertParagraphSeparator
|
||||||
|| (!d->m_lineSeparatorsAllowed && e == QKeySequence::InsertLineSeparator))) {
|
|| (!d->m_lineSeparatorsAllowed && e == QKeySequence::InsertLineSeparator))) {
|
||||||
if (d->m_inBlockSelectionMode) {
|
if (d->m_inBlockSelectionMode) {
|
||||||
d->disableBlockSelection(false);
|
d->disableBlockSelection(TextEditorWidgetPrivate::CursorUpdateClearSelection);
|
||||||
e->accept();
|
e->accept();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -2241,7 +2247,7 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e)
|
|||||||
return;
|
return;
|
||||||
} else if (!ro && (e == QKeySequence::MoveToNextPage || e == QKeySequence::MoveToPreviousPage)
|
} else if (!ro && (e == QKeySequence::MoveToNextPage || e == QKeySequence::MoveToPreviousPage)
|
||||||
&& d->m_inBlockSelectionMode) {
|
&& d->m_inBlockSelectionMode) {
|
||||||
d->disableBlockSelection(false);
|
d->disableBlockSelection(TextEditorWidgetPrivate::CursorUpdateClearSelection);
|
||||||
QPlainTextEdit::keyPressEvent(e);
|
QPlainTextEdit::keyPressEvent(e);
|
||||||
return;
|
return;
|
||||||
} else if (!ro && (e == QKeySequence::SelectNextPage || e == QKeySequence::SelectPreviousPage)
|
} else if (!ro && (e == QKeySequence::SelectNextPage || e == QKeySequence::SelectPreviousPage)
|
||||||
@@ -2341,7 +2347,7 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e)
|
|||||||
e->accept();
|
e->accept();
|
||||||
return;
|
return;
|
||||||
} else if (d->m_inBlockSelectionMode) { // leave block selection mode
|
} else if (d->m_inBlockSelectionMode) { // leave block selection mode
|
||||||
d->disableBlockSelection();
|
d->disableBlockSelection(TextEditorWidgetPrivate::NoCursorUpdate);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Qt::Key_Insert:
|
case Qt::Key_Insert:
|
||||||
@@ -2528,7 +2534,7 @@ void TextEditorWidget::doSetTextCursor(const QTextCursor &cursor, bool keepBlock
|
|||||||
// workaround for QTextControl bug
|
// workaround for QTextControl bug
|
||||||
bool selectionChange = cursor.hasSelection() || textCursor().hasSelection();
|
bool selectionChange = cursor.hasSelection() || textCursor().hasSelection();
|
||||||
if (!keepBlockSelection && d->m_inBlockSelectionMode)
|
if (!keepBlockSelection && d->m_inBlockSelectionMode)
|
||||||
d->disableBlockSelection(false);
|
d->disableBlockSelection(TextEditorWidgetPrivate::NoCursorUpdate);
|
||||||
QTextCursor c = cursor;
|
QTextCursor c = cursor;
|
||||||
c.setVisualNavigation(true);
|
c.setVisualNavigation(true);
|
||||||
QPlainTextEdit::doSetTextCursor(c);
|
QPlainTextEdit::doSetTextCursor(c);
|
||||||
@@ -3502,15 +3508,17 @@ void TextEditorWidgetPrivate::enableBlockSelection(int positionBlock, int positi
|
|||||||
q->viewport()->update();
|
q->viewport()->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidgetPrivate::disableBlockSelection(bool keepSelection)
|
void TextEditorWidgetPrivate::disableBlockSelection(BlockSelectionUpdateKind kind)
|
||||||
{
|
{
|
||||||
m_inBlockSelectionMode = false;
|
m_inBlockSelectionMode = false;
|
||||||
m_cursorFlashTimer.stop();
|
m_cursorFlashTimer.stop();
|
||||||
QTextCursor cursor = m_blockSelection.selection(m_document.data());
|
if (kind != NoCursorUpdate) {
|
||||||
|
QTextCursor cursor = m_blockSelection.selection(m_document.data());
|
||||||
|
if (kind == CursorUpdateClearSelection)
|
||||||
|
cursor.clearSelection();
|
||||||
|
q->setTextCursor(cursor);
|
||||||
|
}
|
||||||
m_blockSelection.clear();
|
m_blockSelection.clear();
|
||||||
if (!keepSelection)
|
|
||||||
cursor.clearSelection();
|
|
||||||
q->setTextCursor(cursor);
|
|
||||||
q->viewport()->update();
|
q->viewport()->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4879,7 +4887,7 @@ void TextEditorWidget::mouseMoveEvent(QMouseEvent *e)
|
|||||||
viewport()->update();
|
viewport()->update();
|
||||||
}
|
}
|
||||||
} else if (d->m_inBlockSelectionMode) {
|
} else if (d->m_inBlockSelectionMode) {
|
||||||
d->disableBlockSelection();
|
d->disableBlockSelection(TextEditorWidgetPrivate::CursorUpdateKeepSelection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (viewport()->cursor().shape() == Qt::BlankCursor)
|
if (viewport()->cursor().shape() == Qt::BlankCursor)
|
||||||
@@ -4923,7 +4931,7 @@ void TextEditorWidget::mousePressEvent(QMouseEvent *e)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (d->m_inBlockSelectionMode)
|
if (d->m_inBlockSelectionMode)
|
||||||
d->disableBlockSelection(false); // just in case, otherwise we might get strange drag and drop
|
d->disableBlockSelection(TextEditorWidgetPrivate::NoCursorUpdate);
|
||||||
|
|
||||||
QTextBlock foldedBlock = d->foldedBlockAt(e->pos());
|
QTextBlock foldedBlock = d->foldedBlockAt(e->pos());
|
||||||
if (foldedBlock.isValid()) {
|
if (foldedBlock.isValid()) {
|
||||||
@@ -6680,7 +6688,7 @@ void TextEditorWidget::cut()
|
|||||||
void TextEditorWidget::selectAll()
|
void TextEditorWidget::selectAll()
|
||||||
{
|
{
|
||||||
if (d->m_inBlockSelectionMode)
|
if (d->m_inBlockSelectionMode)
|
||||||
d->disableBlockSelection();
|
d->disableBlockSelection(TextEditorWidgetPrivate::NoCursorUpdate);
|
||||||
QPlainTextEdit::selectAll();
|
QPlainTextEdit::selectAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -7139,7 +7147,8 @@ void BaseTextEditor::setCursorPosition(int pos)
|
|||||||
|
|
||||||
void TextEditorWidget::setCursorPosition(int pos)
|
void TextEditorWidget::setCursorPosition(int pos)
|
||||||
{
|
{
|
||||||
setBlockSelection(false);
|
if (d->m_inBlockSelectionMode)
|
||||||
|
d->disableBlockSelection(TextEditorWidgetPrivate::NoCursorUpdate);
|
||||||
QTextCursor tc = textCursor();
|
QTextCursor tc = textCursor();
|
||||||
tc.setPosition(pos);
|
tc.setPosition(pos);
|
||||||
setTextCursor(tc);
|
setTextCursor(tc);
|
||||||
@@ -7316,7 +7325,7 @@ void TextEditorWidget::setBlockSelection(bool on)
|
|||||||
if (on)
|
if (on)
|
||||||
d->enableBlockSelection(textCursor());
|
d->enableBlockSelection(textCursor());
|
||||||
else
|
else
|
||||||
d->disableBlockSelection(false);
|
d->disableBlockSelection(TextEditorWidgetPrivate::CursorUpdateClearSelection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidget::setBlockSelection(int positionBlock, int positionColumn,
|
void TextEditorWidget::setBlockSelection(int positionBlock, int positionColumn,
|
||||||
|
Reference in New Issue
Block a user