Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions Orange/widgets/utils/slidergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@
from Orange.widgets.visualize.utils.plotutils import PlotWidget


class InteractiveInfiniteLine(InfiniteLine): # pylint: disable=abstract-method
"""
A subclass of InfiniteLine that provides custom hover behavior.
"""

def __init__(self, angle=90, pos=None, movable=False, bounds=None,
normal_pen=None, highlight_pen=None, **kwargs):
super().__init__(angle=angle, pos=pos, movable=movable, bounds=bounds, **kwargs)
self._normal_pen = normal_pen
self._highlight_pen = highlight_pen
self.setPen(normal_pen)

def hoverEvent(self, ev):
"""
Override hoverEvent to provide custom hover behavior.

Parameters
----------
ev : HoverEvent
The hover event from pyqtgraph
"""

if ev.isEnter() and self._highlight_pen is not None:
self.setPen(self._highlight_pen)
elif ev.isExit() and self._normal_pen is not None:
self.setPen(self._normal_pen)


class SliderGraph(PlotWidget):
"""
An widget graph element that shows a line plot with more sequences. It
Expand Down Expand Up @@ -136,14 +164,23 @@ def _plot_cutpoint(self, x):
self._line = None
return
if self._line is None:
# plot interactive vertical line
self._line = InfiniteLine(
normal_pen = mkPen(
self.palette().text().color(), width=4,
style=Qt.SolidLine, capStyle=Qt.RoundCap
)
highlight_pen = mkPen(
self.palette().link().color(), width=4,
style=Qt.SolidLine, capStyle=Qt.RoundCap
)

self._line = InteractiveInfiniteLine(
angle=90, pos=x, movable=True,
bounds=self.selection_limit if self.selection_limit is not None
else (self.x.min(), self.x.max())
else (self.x.min(), self.x.max()),
normal_pen=normal_pen,
highlight_pen=highlight_pen
)
self._line.setCursor(Qt.SizeHorCursor)
self._line.setPen(mkPen(self.palette().text().color(), width=2))
self._line.sigPositionChanged.connect(self._on_cut_changed)
self.addItem(self._line)
else:
Expand Down
24 changes: 24 additions & 0 deletions Orange/widgets/utils/tests/test_slidergraph.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import Mock

import numpy as np
from AnyQt.QtCore import Qt

Expand Down Expand Up @@ -105,3 +107,25 @@ def test_plot_no_cutpoint(self):
p.update(x, self.data, [Qt.red])
# pylint: disable=protected-access
self.assertIsNone(p._line)

def test_hover_event(self):
p = self.widget.plot
x = np.arange(len(self.data[0]))
p.update(x, self.data, [Qt.red], cutpoint_x=1)

# pylint: disable=protected-access
self.assertIsNotNone(p._line)

enter_event = Mock()
enter_event.isEnter.return_value = True
enter_event.isExit.return_value = False

p._line.hoverEvent(enter_event)
self.assertEqual(p._line.pen, p._line._highlight_pen)

exit_event = Mock()
exit_event.isEnter.return_value = False
exit_event.isExit.return_value = True

p._line.hoverEvent(exit_event)
self.assertEqual(p._line.pen, p._line._normal_pen)
Loading