Commit 2c115e93 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

Make accidentally activating the link navigation more difficult

Only checking Ctrl and link availability on mouse release was triggering
the navigation too easily.

Now you have to press and release the mouse on the same link, having
Ctrl pressed all the time. In addition, when changing the text selection
the link is also cleared.

Task-number: QTCREATORBUG-134
Reviewed-by: con
parent a4d1eb48
...@@ -648,6 +648,9 @@ void BaseTextEditor::slotSelectionChanged() ...@@ -648,6 +648,9 @@ void BaseTextEditor::slotSelectionChanged()
d->m_blockSelectionExtraX = 0; d->m_blockSelectionExtraX = 0;
if (!d->m_selectBlockAnchor.isNull() && !textCursor().hasSelection()) if (!d->m_selectBlockAnchor.isNull() && !textCursor().hasSelection())
d->m_selectBlockAnchor = QTextCursor(); d->m_selectBlockAnchor = QTextCursor();
// Clear any link which might be showing when the selection changes
clearLink();
} }
void BaseTextEditor::gotoBlockStart() void BaseTextEditor::gotoBlockStart()
...@@ -1470,7 +1473,7 @@ BaseTextEditorPrivate::BaseTextEditorPrivate() ...@@ -1470,7 +1473,7 @@ BaseTextEditorPrivate::BaseTextEditorPrivate()
m_requestMarkEnabled(true), m_requestMarkEnabled(true),
m_lineSeparatorsAllowed(false), m_lineSeparatorsAllowed(false),
m_visibleWrapColumn(0), m_visibleWrapColumn(0),
m_showingLink(false), m_linkPressed(false),
m_editable(0), m_editable(0),
m_actionHack(0), m_actionHack(0),
m_inBlockSelectionMode(false), m_inBlockSelectionMode(false),
...@@ -2864,35 +2867,11 @@ void BaseTextEditorPrivate::clearVisibleCollapsedBlock() ...@@ -2864,35 +2867,11 @@ 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);
bool linkFound = false; updateLink(e);
if (d->m_mouseNavigationEnabled && e->modifiers() & Qt::ControlModifier) {
// Link emulation behaviour for 'go to definition'
const QTextCursor cursor = cursorForPosition(e->pos());
// Check that the mouse was actually on the text somewhere
bool onText = cursorRect(cursor).right() >= e->x();
if (!onText) {
QTextCursor nextPos = cursor;
nextPos.movePosition(QTextCursor::Right);
onText = cursorRect(nextPos).right() >= e->x();
}
const Link link = findLinkAt(cursor, false);
if (onText && link.isValid()) {
showLink(link);
linkFound = true;
}
}
if (!linkFound)
clearLink();
if (e->buttons() == Qt::NoButton) { if (e->buttons() == Qt::NoButton) {
const QTextBlock collapsedBlock = collapsedBlockAt(e->pos()); const QTextBlock collapsedBlock = collapsedBlockAt(e->pos());
...@@ -2936,16 +2915,23 @@ void BaseTextEditor::mousePressEvent(QMouseEvent *e) ...@@ -2936,16 +2915,23 @@ void BaseTextEditor::mousePressEvent(QMouseEvent *e)
toggleBlockVisible(collapsedBlock); toggleBlockVisible(collapsedBlock);
viewport()->setCursor(Qt::IBeamCursor); viewport()->setCursor(Qt::IBeamCursor);
} }
updateLink(e);
if (d->m_currentLink.isValid())
d->m_linkPressed = true;
} }
QPlainTextEdit::mousePressEvent(e); QPlainTextEdit::mousePressEvent(e);
} }
void BaseTextEditor::mouseReleaseEvent(QMouseEvent *e) void BaseTextEditor::mouseReleaseEvent(QMouseEvent *e)
{ {
if (d->m_mouseNavigationEnabled && e->modifiers() & Qt::ControlModifier if (d->m_mouseNavigationEnabled
&& d->m_linkPressed
&& e->modifiers() & Qt::ControlModifier
&& !(e->modifiers() & Qt::ShiftModifier) && !(e->modifiers() & Qt::ShiftModifier)
&& e->button() == Qt::LeftButton) { && e->button() == Qt::LeftButton
) {
const QTextCursor cursor = cursorForPosition(e->pos()); const QTextCursor cursor = cursorForPosition(e->pos());
if (openLink(findLinkAt(cursor))) { if (openLink(findLinkAt(cursor))) {
clearLink(); clearLink();
...@@ -3461,8 +3447,39 @@ bool BaseTextEditor::openLink(const Link &link) ...@@ -3461,8 +3447,39 @@ bool BaseTextEditor::openLink(const Link &link)
return openEditorAt(link.fileName, link.line, link.column); return openEditorAt(link.fileName, link.line, link.column);
} }
void BaseTextEditor::updateLink(QMouseEvent *e)
{
bool linkFound = false;
if (d->m_mouseNavigationEnabled && e->modifiers() & Qt::ControlModifier) {
// Link emulation behaviour for 'go to definition'
const QTextCursor cursor = cursorForPosition(e->pos());
// Check that the mouse was actually on the text somewhere
bool onText = cursorRect(cursor).right() >= e->x();
if (!onText) {
QTextCursor nextPos = cursor;
nextPos.movePosition(QTextCursor::Right);
onText = cursorRect(nextPos).right() >= e->x();
}
const Link link = findLinkAt(cursor, false);
if (onText && link.isValid()) {
showLink(link);
linkFound = true;
}
}
if (!linkFound)
clearLink();
}
void BaseTextEditor::showLink(const Link &link) void BaseTextEditor::showLink(const Link &link)
{ {
if (d->m_currentLink == link)
return;
QTextEdit::ExtraSelection sel; QTextEdit::ExtraSelection sel;
sel.cursor = textCursor(); sel.cursor = textCursor();
sel.cursor.setPosition(link.pos); sel.cursor.setPosition(link.pos);
...@@ -3471,25 +3488,26 @@ void BaseTextEditor::showLink(const Link &link) ...@@ -3471,25 +3488,26 @@ void BaseTextEditor::showLink(const Link &link)
sel.format.setFontUnderline(true); sel.format.setFontUnderline(true);
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>() << sel); setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>() << sel);
viewport()->setCursor(Qt::PointingHandCursor); viewport()->setCursor(Qt::PointingHandCursor);
d->m_showingLink = true; d->m_currentLink = link;
d->m_linkPressed = false;
} }
void BaseTextEditor::clearLink() void BaseTextEditor::clearLink()
{ {
if (!d->m_showingLink) if (!d->m_currentLink.isValid())
return; return;
setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>()); setExtraSelections(OtherSelection, QList<QTextEdit::ExtraSelection>());
viewport()->setCursor(Qt::IBeamCursor); viewport()->setCursor(Qt::IBeamCursor);
d->m_showingLink = false; d->m_currentLink = Link();
d->m_linkPressed = false;
} }
void BaseTextEditorPrivate::updateMarksBlock(const QTextBlock &block) void BaseTextEditorPrivate::updateMarksBlock(const QTextBlock &block)
{ {
if (const TextBlockUserData *userData = TextEditDocumentLayout::testUserData(block)) if (const TextBlockUserData *userData = TextEditDocumentLayout::testUserData(block))
foreach (ITextMark *mrk, userData->marks()) { foreach (ITextMark *mrk, userData->marks())
mrk->updateBlock(block); mrk->updateBlock(block);
}
} }
void BaseTextEditorPrivate::updateMarksLineNumber() void BaseTextEditorPrivate::updateMarksLineNumber()
......
...@@ -538,6 +538,9 @@ protected: ...@@ -538,6 +538,9 @@ protected:
bool isValid() const bool isValid() const
{ return !(pos == -1 || length == -1); } { return !(pos == -1 || length == -1); }
bool operator==(const Link &other) const
{ return pos == other.pos && length == other.length; }
int pos; // Link position int pos; // Link position
int length; // Link length int length; // Link length
...@@ -593,6 +596,7 @@ private: ...@@ -593,6 +596,7 @@ private:
QTextBlock collapsedBlockAt(const QPoint &pos, QRect *box = 0) const; QTextBlock collapsedBlockAt(const QPoint &pos, QRect *box = 0) const;
void updateLink(QMouseEvent *e);
void showLink(const Link &); void showLink(const Link &);
void clearLink(); void clearLink();
......
...@@ -204,7 +204,8 @@ public: ...@@ -204,7 +204,8 @@ public:
int m_visibleWrapColumn; int m_visibleWrapColumn;
QTextCharFormat m_linkFormat; QTextCharFormat m_linkFormat;
bool m_showingLink; BaseTextEditor::Link m_currentLink;
bool m_linkPressed;
QTextCharFormat m_ifdefedOutFormat; QTextCharFormat m_ifdefedOutFormat;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment