From 2bfb3b7741a823eefa38f881cb3c70392878218f 2012-10-18 06:44:56 From: Diane Trout Date: 2012-10-18 06:44:56 Subject: [PATCH] Remove self._anchor Bradley Froehle recommended removing self._anchor and just requesting the anchor text for a position as it was needed. He also pointed out that Qt handles hiding the tooltip for the empty string and suppressing redraw messages which simplified the event handler code. The only problem I found is when updating my test code the tooltip takes a moment to disappear and caches the text value for a time longer than a function call. So I can't verify that the tooltip is cleared when the mouse moves off the anchor. --- diff --git a/IPython/frontend/qt/console/console_widget.py b/IPython/frontend/qt/console/console_widget.py index bd7e99d..1cfa695 100644 --- a/IPython/frontend/qt/console/console_widget.py +++ b/IPython/frontend/qt/console/console_widget.py @@ -265,7 +265,6 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): elif self.gui_completion == 'plain': self._completion_widget = CompletionPlain(self) - self._anchor = None self._continuation_prompt = '> ' self._continuation_prompt_html = None self._executing = False @@ -437,12 +436,7 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): elif etype == QtCore.QEvent.MouseMove: anchor = self._control.anchorAt(event.pos()) - if len(anchor) == 0: - self._anchor = None - QtGui.QToolTip.hideText() - elif anchor != self._anchor: - self._anchor = anchor - QtGui.QToolTip.showText(event.globalPos(), self._anchor) + QtGui.QToolTip.showText(event.globalPos(), anchor) return super(ConsoleWidget, self).eventFilter(obj, event) @@ -523,11 +517,10 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): """ self.layout().currentWidget().copy() - def copy_anchor(self): + def copy_anchor(self, anchor): """ Copy anchor text to the clipboard """ - if self._anchor: - QtGui.QApplication.clipboard().setText(self._anchor) + QtGui.QApplication.clipboard().setText(anchor) def cut(self): """ Copy the currently selected text to the clipboard and delete it @@ -696,11 +689,10 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): font = property(_get_font, _set_font) - def open_anchor(self): + def open_anchor(self, anchor): """ Open selected anchor in the default webbrowser """ - if self._anchor: - webbrowser.open( self._anchor ) + webbrowser.open( anchor ) def paste(self, mode=QtGui.QClipboard.Clipboard): """ Paste the contents of the clipboard into the input region. @@ -995,12 +987,13 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): self.paste_action.setEnabled(self.can_paste()) self.paste_action.setShortcut(QtGui.QKeySequence.Paste) - if self._anchor: + anchor = self._control.anchorAt(pos) + if 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) + self.copy_link_action = menu.addAction( + 'Copy Link Address', lambda: self.copy_anchor(anchor=anchor)) + self.open_link_action = menu.addAction( + 'Open Link', lambda: self.open_anchor(anchor=anchor)) menu.addSeparator() menu.addAction(self.select_all_action) diff --git a/IPython/frontend/qt/console/tests/test_console_widget.py b/IPython/frontend/qt/console/tests/test_console_widget.py index ba2f75b..9f24820 100644 --- a/IPython/frontend/qt/console/tests/test_console_widget.py +++ b/IPython/frontend/qt/console/tests/test_console_widget.py @@ -53,21 +53,28 @@ class TestConsoleWidget(unittest.TestCase): cursor = w._get_prompt_cursor() w._insert_html(cursor, 'written in') obj = w._control - self.assertEqual(w._anchor, None) + tip = QtGui.QToolTip + self.assertEqual(tip.text(), u'') + # should be somewhere else + elsewhereEvent = QMouseEvent(MouseMove, QtCore.QPoint(50,50), + noButton, noButtons, noModifiers) + w.eventFilter(obj, elsewhereEvent) + self.assertEqual(tip.isVisible(), False) + self.assertEqual(tip.text(), u'') + + #self.assertEqual(tip.text(), u'') # 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") + self.assertEqual(tip.isVisible(), True) + self.assertEqual(tip.text(), "http://python.org") # should still be over text stillOverTextEvent = QMouseEvent(MouseMove, QtCore.QPoint(1,5), noButton, noButtons, noModifiers) w.eventFilter(obj, stillOverTextEvent) + self.assertEqual(tip.isVisible(), True) + self.assertEqual(tip.text(), "http://python.org") - # should be somewhere else - elsewhereEvent = QMouseEvent(MouseMove, QtCore.QPoint(50,50), - noButton, noButtons, noModifiers) - w.eventFilter(obj, elsewhereEvent) - self.assertEqual(w._anchor, None)