From df42e7acc41d65686165a838681ceae62d373327 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 13 Sep 2022 18:26:06 -0400 Subject: [PATCH] Add `LevelLine.get_cursor()` to get any currently hovering mouse-cursor --- piker/ui/_lines.py | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/piker/ui/_lines.py b/piker/ui/_lines.py index 033464c3..7756f1a7 100644 --- a/piker/ui/_lines.py +++ b/piker/ui/_lines.py @@ -18,9 +18,14 @@ Lines for orders, alerts, L2. """ +from __future__ import annotations from functools import partial from math import floor -from typing import Optional, Callable +from typing import ( + Optional, + Callable, + TYPE_CHECKING, +) import pyqtgraph as pg from pyqtgraph import Point, functions as fn @@ -37,6 +42,9 @@ from ..calc import humanize from ._label import Label from ._style import hcolor, _font +if TYPE_CHECKING: + from ._cursor import Cursor + # TODO: probably worth investigating if we can # make .boundingRect() faster: @@ -220,20 +228,23 @@ class LevelLine(pg.InfiniteLine): y: float ) -> None: - '''Chart coordinates cursor tracking callback. + ''' + Chart coordinates cursor tracking callback. this is called by our ``Cursor`` type once this line is set to track the cursor: for every movement this callback is invoked to reposition the line with the current view coordinates. + ''' self.movable = True self.set_level(y) # implictly calls reposition handler def mouseDragEvent(self, ev): - """Override the ``InfiniteLine`` handler since we need more + ''' + Override the ``InfiniteLine`` handler since we need more detailed control and start end signalling. - """ + ''' cursor = self._chart.linked.cursor # hide y-crosshair @@ -285,10 +296,20 @@ class LevelLine(pg.InfiniteLine): # show y-crosshair again cursor.show_xhair() - def delete(self) -> None: - """Remove this line from containing chart/view/scene. + def get_cursor(self) -> Optional[Cursor]: - """ + chart = self._chart + cur = chart.linked.cursor + if self in cur._hovered: + return cur + + return None + + def delete(self) -> None: + ''' + Remove this line from containing chart/view/scene. + + ''' scene = self.scene() if scene: for label in self._labels: @@ -302,9 +323,8 @@ class LevelLine(pg.InfiniteLine): # remove from chart/cursor states chart = self._chart - cur = chart.linked.cursor - - if self in cur._hovered: + cur = self.get_cursor() + if cur: cur._hovered.remove(self) chart.plotItem.removeItem(self) @@ -312,8 +332,8 @@ class LevelLine(pg.InfiniteLine): def mouseDoubleClickEvent( self, ev: QtGui.QMouseEvent, - ) -> None: + ) -> None: # TODO: enter labels edit mode print(f'double click {ev}')