From 76a50ac0829ca2963cada1a2d3c65d19a68e1423 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 28 Dec 2022 01:20:55 -0500 Subject: [PATCH] Make `FlowGraphic.x_last()` be optionally `None` In the case where the last-datum-graphic hasn't been created yet, simply return a `None` from this method so the caller can choose to ignore the output. Further, drop `.px_width()` since it makes more sense defined on `Viz` as well as the previously commented `BarItems.x_uppx()` method. Also, don't round the `.x_uppx()` output since it can then be used when < 1 to do x-domain scaling during high zoom usage. --- piker/ui/_curve.py | 30 +++++------------------------- piker/ui/_ohlc.py | 15 ++++++++------- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/piker/ui/_curve.py b/piker/ui/_curve.py index f22dcd14..a3287341 100644 --- a/piker/ui/_curve.py +++ b/piker/ui/_curve.py @@ -70,37 +70,17 @@ class FlowGraphic(pg.GraphicsObject): px_vecs = self.pixelVectors()[0] if px_vecs: - xs_in_px = px_vecs.x() - return round(xs_in_px) + return px_vecs.x() else: return 0 - def x_last(self) -> float: + def x_last(self) -> float | None: ''' - Return the last most x value of the last line segment. + Return the last most x value of the last line segment or if not + drawn yet, ``None``. ''' - return self._last_line.x1() - - def px_width(self) -> float: - ''' - Return the width of the view box containing - this graphic in pixel units. - - ''' - vb = self.getViewBox() - if not vb: - return 0 - - vr = self.viewRect() - vl, vr = int(vr.left()), int(vr.right()) - - return vb.mapViewToDevice( - QLineF( - vl, 0, - vr, 0, - ) - ).length() + return self._last_line.x1() if self._last_line else None class Curve(FlowGraphic): diff --git a/piker/ui/_ohlc.py b/piker/ui/_ohlc.py index 9712bb9d..de421cd2 100644 --- a/piker/ui/_ohlc.py +++ b/piker/ui/_ohlc.py @@ -126,16 +126,17 @@ class BarItems(FlowGraphic): self.path = QPainterPath() self._last_bar_lines: tuple[QLineF, ...] | None = None - # def x_uppx(self) -> int: - # # we expect the downsample curve report this. - # return 0 - - def x_last(self) -> float: + def x_last(self) -> None | float: ''' - Return the last most x value of the close line segment. + Return the last most x value of the close line segment + or if not drawn yet, ``None``. ''' - return self._last_bar_lines[-1].x2() + if self._last_bar_lines: + close_arm_line = self._last_bar_lines[-1] + return close_arm_line.x2() if close_arm_line else None + else: + return None # Qt docs: https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect def boundingRect(self):