forked from qt-creator/qt-creator
Stop fighting over which mouse cursor to show on text editor
The BaseTextEditor was unconditionally setting the mouse cursor on each mouse move event, after which the CPPEditor would set it as well when the mouse was above a link. This caused some mouse cursor flickering, so now the cursor is only set when it's supposed to change. Also fixed an issue where the link wasn't removed when leaving the text editor with the mouse while Ctrl was pressed.
This commit is contained in:
@@ -183,13 +183,13 @@ CPPEditorEditable::CPPEditorEditable(CPPEditor *editor)
|
|||||||
|
|
||||||
CPPEditor::CPPEditor(QWidget *parent)
|
CPPEditor::CPPEditor(QWidget *parent)
|
||||||
: TextEditor::BaseTextEditor(parent)
|
: TextEditor::BaseTextEditor(parent)
|
||||||
|
, m_showingLink(false)
|
||||||
{
|
{
|
||||||
setParenthesesMatchingEnabled(true);
|
setParenthesesMatchingEnabled(true);
|
||||||
setMarksVisible(true);
|
setMarksVisible(true);
|
||||||
setCodeFoldingSupported(true);
|
setCodeFoldingSupported(true);
|
||||||
setCodeFoldingVisible(true);
|
setCodeFoldingVisible(true);
|
||||||
baseTextDocument()->setSyntaxHighlighter(new CppHighlighter);
|
baseTextDocument()->setSyntaxHighlighter(new CppHighlighter);
|
||||||
// new QShortcut(QKeySequence("Ctrl+Alt+M"), this, SLOT(foo()), 0, Qt::WidgetShortcut);
|
|
||||||
|
|
||||||
#ifdef WITH_TOKEN_MOVE_POSITION
|
#ifdef WITH_TOKEN_MOVE_POSITION
|
||||||
new QShortcut(QKeySequence::MoveToPreviousWord, this, SLOT(moveToPreviousToken()),
|
new QShortcut(QKeySequence::MoveToPreviousWord, this, SLOT(moveToPreviousToken()),
|
||||||
@@ -808,8 +808,7 @@ void CPPEditor::contextMenuEvent(QContextMenuEvent *e)
|
|||||||
|
|
||||||
void CPPEditor::mouseMoveEvent(QMouseEvent *e)
|
void CPPEditor::mouseMoveEvent(QMouseEvent *e)
|
||||||
{
|
{
|
||||||
bool hasDestination = false;
|
bool linkFound = false;
|
||||||
Qt::CursorShape cursorShape;
|
|
||||||
|
|
||||||
if (e->modifiers() & Qt::ControlModifier) {
|
if (e->modifiers() & Qt::ControlModifier) {
|
||||||
// Link emulation behaviour for 'go to definition'
|
// Link emulation behaviour for 'go to definition'
|
||||||
@@ -826,30 +825,15 @@ void CPPEditor::mouseMoveEvent(QMouseEvent *e)
|
|||||||
const Link link = findLinkAt(cursor, false);
|
const Link link = findLinkAt(cursor, false);
|
||||||
|
|
||||||
if (onText && !link.fileName.isEmpty()) {
|
if (onText && !link.fileName.isEmpty()) {
|
||||||
QTextEdit::ExtraSelection sel;
|
showLink(link);
|
||||||
sel.cursor = cursor;
|
linkFound = true;
|
||||||
if (link.pos >= 0) {
|
|
||||||
sel.cursor.setPosition(link.pos);
|
|
||||||
sel.cursor.setPosition(link.pos + link.length, QTextCursor::KeepAnchor);
|
|
||||||
} else {
|
|
||||||
sel.cursor.select(QTextCursor::WordUnderCursor);
|
|
||||||
}
|
|
||||||
sel.format = m_linkFormat;
|
|
||||||
sel.format.setFontUnderline(true);
|
|
||||||
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>() << sel);
|
|
||||||
hasDestination = true;
|
|
||||||
cursorShape = Qt::PointingHandCursor;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasDestination) {
|
if (!linkFound)
|
||||||
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>());
|
clearLink();
|
||||||
cursorShape = Qt::IBeamCursor;
|
|
||||||
}
|
|
||||||
|
|
||||||
TextEditor::BaseTextEditor::mouseMoveEvent(e);
|
TextEditor::BaseTextEditor::mouseMoveEvent(e);
|
||||||
|
|
||||||
viewport()->setCursor(cursorShape);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CPPEditor::mouseReleaseEvent(QMouseEvent *e)
|
void CPPEditor::mouseReleaseEvent(QMouseEvent *e)
|
||||||
@@ -859,8 +843,7 @@ void CPPEditor::mouseReleaseEvent(QMouseEvent *e)
|
|||||||
|
|
||||||
const QTextCursor cursor = cursorForPosition(e->pos());
|
const QTextCursor cursor = cursorForPosition(e->pos());
|
||||||
if (openCppEditorAt(findLinkAt(cursor))) {
|
if (openCppEditorAt(findLinkAt(cursor))) {
|
||||||
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>());
|
clearLink();
|
||||||
viewport()->setCursor(Qt::IBeamCursor);
|
|
||||||
e->accept();
|
e->accept();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -869,17 +852,44 @@ void CPPEditor::mouseReleaseEvent(QMouseEvent *e)
|
|||||||
TextEditor::BaseTextEditor::mouseReleaseEvent(e);
|
TextEditor::BaseTextEditor::mouseReleaseEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CPPEditor::leaveEvent(QEvent *e)
|
||||||
|
{
|
||||||
|
clearLink();
|
||||||
|
TextEditor::BaseTextEditor::leaveEvent(e);
|
||||||
|
}
|
||||||
|
|
||||||
void CPPEditor::keyReleaseEvent(QKeyEvent *e)
|
void CPPEditor::keyReleaseEvent(QKeyEvent *e)
|
||||||
{
|
{
|
||||||
// Clear link emulation when Ctrl is released
|
// Clear link emulation when Ctrl is released
|
||||||
if (e->key() == Qt::Key_Control) {
|
if (e->key() == Qt::Key_Control)
|
||||||
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>());
|
clearLink();
|
||||||
viewport()->setCursor(Qt::IBeamCursor);
|
|
||||||
}
|
|
||||||
|
|
||||||
TextEditor::BaseTextEditor::keyReleaseEvent(e);
|
TextEditor::BaseTextEditor::keyReleaseEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CPPEditor::showLink(const Link &link)
|
||||||
|
{
|
||||||
|
QTextEdit::ExtraSelection sel;
|
||||||
|
sel.cursor = textCursor();
|
||||||
|
sel.cursor.setPosition(link.pos);
|
||||||
|
sel.cursor.setPosition(link.pos + link.length, QTextCursor::KeepAnchor);
|
||||||
|
sel.format = m_linkFormat;
|
||||||
|
sel.format.setFontUnderline(true);
|
||||||
|
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>() << sel);
|
||||||
|
viewport()->setCursor(Qt::PointingHandCursor);
|
||||||
|
m_showingLink = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPPEditor::clearLink()
|
||||||
|
{
|
||||||
|
if (!m_showingLink)
|
||||||
|
return;
|
||||||
|
|
||||||
|
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>());
|
||||||
|
viewport()->setCursor(Qt::IBeamCursor);
|
||||||
|
m_showingLink = false;
|
||||||
|
}
|
||||||
|
|
||||||
QList<int> CPPEditorEditable::context() const
|
QList<int> CPPEditorEditable::context() const
|
||||||
{
|
{
|
||||||
return m_context;
|
return m_context;
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ protected:
|
|||||||
void contextMenuEvent(QContextMenuEvent *);
|
void contextMenuEvent(QContextMenuEvent *);
|
||||||
void mouseMoveEvent(QMouseEvent *);
|
void mouseMoveEvent(QMouseEvent *);
|
||||||
void mouseReleaseEvent(QMouseEvent *);
|
void mouseReleaseEvent(QMouseEvent *);
|
||||||
|
void leaveEvent(QEvent *);
|
||||||
void keyReleaseEvent(QKeyEvent *);
|
void keyReleaseEvent(QKeyEvent *);
|
||||||
|
|
||||||
TextEditor::BaseTextEditorEditable *createEditableInterface();
|
TextEditor::BaseTextEditorEditable *createEditableInterface();
|
||||||
@@ -148,6 +149,10 @@ private:
|
|||||||
int column; // Target column
|
int column; // Target column
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void showLink(const Link &);
|
||||||
|
void clearLink();
|
||||||
|
bool m_showingLink;
|
||||||
|
|
||||||
Link findLinkAt(const QTextCursor &, bool lookupDefinition = true);
|
Link findLinkAt(const QTextCursor &, bool lookupDefinition = true);
|
||||||
static Link linkToSymbol(CPlusPlus::Symbol *symbol);
|
static Link linkToSymbol(CPlusPlus::Symbol *symbol);
|
||||||
bool openCppEditorAt(const Link &);
|
bool openCppEditorAt(const Link &);
|
||||||
|
|||||||
@@ -1255,6 +1255,7 @@ BaseTextEditorPrivate::BaseTextEditorPrivate()
|
|||||||
m_document(new BaseTextDocument()),
|
m_document(new BaseTextDocument()),
|
||||||
m_parenthesesMatchingEnabled(false),
|
m_parenthesesMatchingEnabled(false),
|
||||||
m_extraArea(0),
|
m_extraArea(0),
|
||||||
|
m_mouseOnCollapsedMarker(false),
|
||||||
m_marksVisible(false),
|
m_marksVisible(false),
|
||||||
m_codeFoldingVisible(false),
|
m_codeFoldingVisible(false),
|
||||||
m_codeFoldingSupported(false),
|
m_codeFoldingSupported(false),
|
||||||
@@ -2421,22 +2422,30 @@ void BaseTextEditorPrivate::clearVisibleCollapsedBlock()
|
|||||||
void BaseTextEditor::mouseMoveEvent(QMouseEvent *e)
|
void BaseTextEditor::mouseMoveEvent(QMouseEvent *e)
|
||||||
{
|
{
|
||||||
d->m_lastEventWasBlockSelectionEvent = (e->modifiers() & Qt::AltModifier);
|
d->m_lastEventWasBlockSelectionEvent = (e->modifiers() & Qt::AltModifier);
|
||||||
if (e->buttons() == 0) {
|
if (e->buttons() == Qt::NoButton) {
|
||||||
QTextBlock collapsedBlock = collapsedBlockAt(e->pos());
|
const QTextBlock collapsedBlock = collapsedBlockAt(e->pos());
|
||||||
int blockNumber = collapsedBlock.next().blockNumber();
|
const int blockNumber = collapsedBlock.next().blockNumber();
|
||||||
if (blockNumber < 0) {
|
if (blockNumber < 0) {
|
||||||
d->clearVisibleCollapsedBlock();
|
d->clearVisibleCollapsedBlock();
|
||||||
} else if (blockNumber != d->visibleCollapsedBlockNumber) {
|
} else if (blockNumber != d->visibleCollapsedBlockNumber) {
|
||||||
d->suggestedVisibleCollapsedBlockNumber = blockNumber;
|
d->suggestedVisibleCollapsedBlockNumber = blockNumber;
|
||||||
d->collapsedBlockTimer.start(40, this);
|
d->collapsedBlockTimer.start(40, this);
|
||||||
}
|
}
|
||||||
viewport()->setCursor(collapsedBlock.isValid() ? Qt::PointingHandCursor : Qt::IBeamCursor);
|
|
||||||
|
// Update the mouse cursor
|
||||||
|
if (collapsedBlock.isValid() && !d->m_mouseOnCollapsedMarker) {
|
||||||
|
d->m_mouseOnCollapsedMarker = true;
|
||||||
|
viewport()->setCursor(Qt::PointingHandCursor);
|
||||||
|
} else if (!collapsedBlock.isValid() && d->m_mouseOnCollapsedMarker) {
|
||||||
|
d->m_mouseOnCollapsedMarker = false;
|
||||||
|
viewport()->setCursor(Qt::IBeamCursor);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
QPlainTextEdit::mouseMoveEvent(e);
|
QPlainTextEdit::mouseMoveEvent(e);
|
||||||
}
|
}
|
||||||
if (d->m_lastEventWasBlockSelectionEvent && d->m_inBlockSelectionMode) {
|
if (d->m_lastEventWasBlockSelectionEvent && d->m_inBlockSelectionMode) {
|
||||||
if (textCursor().atBlockEnd()) {
|
if (textCursor().atBlockEnd()) {
|
||||||
d->m_blockSelectionExtraX = qMax(0, e->pos().x() - cursorRect().center().x()) / fontMetrics().width(QLatin1Char('x'));
|
d->m_blockSelectionExtraX = qMax(0, e->pos().x() - cursorRect().center().x()) / fontMetrics().averageCharWidth();
|
||||||
} else {
|
} else {
|
||||||
d->m_blockSelectionExtraX = 0;
|
d->m_blockSelectionExtraX = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -175,6 +175,7 @@ public:
|
|||||||
int visibleCollapsedBlockNumber;
|
int visibleCollapsedBlockNumber;
|
||||||
int suggestedVisibleCollapsedBlockNumber;
|
int suggestedVisibleCollapsedBlockNumber;
|
||||||
void clearVisibleCollapsedBlock();
|
void clearVisibleCollapsedBlock();
|
||||||
|
bool m_mouseOnCollapsedMarker;
|
||||||
|
|
||||||
QBasicTimer autoScrollTimer;
|
QBasicTimer autoScrollTimer;
|
||||||
void updateMarksLineNumber();
|
void updateMarksLineNumber();
|
||||||
|
|||||||
Reference in New Issue
Block a user