Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clickable links #2467

Merged
merged 6 commits into from
Oct 25, 2012
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions IPython/frontend/qt/console/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
from textwrap import dedent
from unicodedata import category
import webbrowser

# System library imports
from IPython.external.qt import QtCore, QtGui
Expand Down Expand Up @@ -264,6 +265,7 @@ def __init__(self, parent=None, **kw):
elif self.gui_completion == 'plain':
self._completion_widget = CompletionPlain(self)

self._anchor = None
self._continuation_prompt = '> '
self._continuation_prompt_html = None
self._executing = False
Expand Down Expand Up @@ -432,6 +434,16 @@ def eventFilter(self, obj, event):
obj == self._page_control:
self._page_control.repaint()
return True

elif etype == QtCore.QEvent.MouseMove:
anchor = self._control.anchorAt(event.pos())
if len(anchor) == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as if not anchor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bet it'd work, I'm just not used to the "if not anchor:" idiom so tend to be uncertain if the empty string is false.

self._anchor = None
QtGui.QToolTip.hideText()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this in any way expensive? I.e., should this become:

if self._anchor:
    self._anchor = None
    QtGui.QToolTip.hideText()

No idea, it's certainly simpler as-is, but the other way might be more performant?

elif anchor != self._anchor:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test temporary variable anchor to see if the pointer moved over a different anchor.

self._anchor = anchor
QtGui.QToolTip.showText(event.globalPos(), self._anchor)

return super(ConsoleWidget, self).eventFilter(obj, event)

#---------------------------------------------------------------------------
Expand Down Expand Up @@ -511,6 +523,12 @@ def copy(self):
"""
self.layout().currentWidget().copy()

def copy_anchor(self):
""" Copy anchor text to the clipboard
"""
if self._anchor:
QtGui.QApplication.clipboard().setText(self._anchor)

def cut(self):
""" Copy the currently selected text to the clipboard and delete it
if it's inside the input buffer.
Expand Down Expand Up @@ -678,6 +696,12 @@ def _set_font(self, font):

font = property(_get_font, _set_font)

def open_anchor(self):
""" Open selected anchor in the default webbrowser
"""
if self._anchor:
webbrowser.open( self._anchor )

def paste(self, mode=QtGui.QClipboard.Clipboard):
""" Paste the contents of the clipboard into the input region.

Expand Down Expand Up @@ -971,6 +995,13 @@ def _context_menu_make(self, pos):
self.paste_action.setEnabled(self.can_paste())
self.paste_action.setShortcut(QtGui.QKeySequence.Paste)

if self._anchor:
menu.addSeparator()
self.copy_link_action = menu.addAction('Copy Link Address',
self.copy_anchor)
self.open_link_action = menu.addAction('Open Link',
self.open_anchor)

menu.addSeparator()
menu.addAction(self.select_all_action)

Expand Down Expand Up @@ -1009,6 +1040,7 @@ def _create_control(self):
elif self.kind == 'rich':
control = QtGui.QTextEdit()
control.setAcceptRichText(False)
control.setMouseTracking(True)

# Install event filters. The filter on the viewport is needed for
# mouse events and drag events.
Expand Down
33 changes: 32 additions & 1 deletion IPython/frontend/qt/console/tests/test_console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest

# System library imports
from IPython.external.qt import QtGui
from IPython.external.qt import QtCore, QtGui

# Local imports
from IPython.frontend.qt.console.console_widget import ConsoleWidget
Expand Down Expand Up @@ -40,3 +40,34 @@ def test_special_characters(self):
self.assertEqual(expected_outputs[i], selection)
# clear all the text
cursor.insertText('')

def test_link_handling(self):
noKeys = QtCore.Qt
noButton = QtCore.Qt.MouseButton(0)
noButtons = QtCore.Qt.MouseButtons(0)
noModifiers = QtCore.Qt.KeyboardModifiers(0)
MouseMove = QtCore.QEvent.MouseMove
QMouseEvent = QtGui.QMouseEvent

w = ConsoleWidget()
cursor = w._get_prompt_cursor()
w._insert_html(cursor, '<a href="http://python.org">written in</a>')
obj = w._control
self.assertEqual(w._anchor, None)

# should be over text
overTextEvent = QMouseEvent(MouseMove, QtCore.QPoint(1,5),
noButton, noButtons, noModifiers)
w.eventFilter(obj, overTextEvent)
self.assertEqual(w._anchor, "http://python.org")

# should still be over text
stillOverTextEvent = QMouseEvent(MouseMove, QtCore.QPoint(1,5),
noButton, noButtons, noModifiers)
w.eventFilter(obj, stillOverTextEvent)

# should be somewhere else
elsewhereEvent = QMouseEvent(MouseMove, QtCore.QPoint(50,50),
noButton, noButtons, noModifiers)
w.eventFilter(obj, elsewhereEvent)
self.assertEqual(w._anchor, None)