From d0693e2967f847ac2825c1c565ee8fc146e4ffc1 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 11 Jan 2022 16:06:51 -0500 Subject: [PATCH] Handle left axis case for x-axis label placement For wtv cucked reason all the viewbox/scene coordinate calcs do **not** include a left axis in the geo (likely because it's a hacked in widget + layout thing managed by `PlotItem`). Detect if there's a left axis and if so use it in the label placement scene coords calc. ToDo: probably make this a non-move calc and only recompute any time the axis changes. Other: - rate limit mouse events down to the 60 (ish) Hz for now - change one last lingering `'ohlc'` array lookup - fix `.mouseMoved()` "event" type annot --- piker/ui/_cursor.py | 62 ++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/piker/ui/_cursor.py b/piker/ui/_cursor.py index d9a4e45a..3833a123 100644 --- a/piker/ui/_cursor.py +++ b/piker/ui/_cursor.py @@ -24,7 +24,7 @@ from typing import Optional, Callable import inspect import numpy as np import pyqtgraph as pg -from PyQt5 import QtCore, QtGui, QtWidgets +from PyQt5 import QtCore, QtWidgets from PyQt5.QtCore import QPointF, QRectF from ._style import ( @@ -43,8 +43,8 @@ log = get_logger(__name__) # latency (in terms of perceived lag in cross hair) so really be sure # there's an improvement if you want to change it! -_mouse_rate_limit = 120 # TODO; should we calc current screen refresh rate? -_debounce_delay = 1 / 40 +_mouse_rate_limit = 58 # TODO; should we calc current screen refresh rate? +_debounce_delay = 1 / 60 _ch_label_opac = 1 @@ -64,7 +64,7 @@ class LineDot(pg.CurvePoint): ) -> None: # scale from dpi aware font size - size = int(_font.px_size * 0.375) + size = int(_font.px_size * 0.375) pg.CurvePoint.__init__( self, @@ -246,12 +246,16 @@ class ContentsLabels: # for name, (label, update) in self._labels.items(): for chart, name, label, update in self._labels: - if not (index >= 0 and index < chart._arrays['ohlc'][-1]['index']): + array = chart._arrays[name] + if not ( + index >= 0 + and index < array[-1]['index'] + ): # out of range print('out of range?') continue - array = chart._arrays[name] + # array = chart._arrays[name] # call provided update func with data point try: @@ -462,12 +466,15 @@ class Cursor(pg.GraphicsObject): def mouseMoved( self, - evt: 'tuple[QMouseEvent]', # noqa - ) -> None: # noqa - """Update horizonal and vertical lines when mouse moves inside + coords: tuple[QPointF], # noqa + + ) -> None: + ''' + Update horizonal and vertical lines when mouse moves inside either the main chart or any indicator subplot. - """ - pos = evt[0] + + ''' + pos = coords[0] # find position inside active plot try: @@ -516,28 +523,37 @@ class Cursor(pg.GraphicsObject): # with cursor movement self.contents_labels.update_labels(ix) + vl_x = ix + line_offset for plot, opts in self.graphics.items(): - # update the chart's "contents" label - # plot.update_contents_labels(ix) - # move the vertical line to the current "center of bar" - opts['vl'].setX(ix + line_offset) + opts['vl'].setX(vl_x) # update all subscribed curve dots for cursor in opts.get('cursors', ()): cursor.setIndex(ix) # update the label on the bottom of the crosshair - if 'bottom' in plot.plotItem.axes: + axes = plot.plotItem.axes + + # TODO: make this an up-front calc that we update + # on axis-widget resize events. + # left axis offset width for calcuating + # absolute x-axis label placement. + left_axis_width = 0 + + if 'bottom' in axes: + + left = axes.get('left') + if left: + left_axis_width = left['item'].width() + + # map back to abs (label-local) coordinates self.xaxis_label.update_label( - - # XXX: requires: - # https://github.com/pyqtgraph/pyqtgraph/pull/1418 - # otherwise gobbles tons of CPU.. - - # map back to abs (label-local) coordinates - abs_pos=plot.mapFromView(QPointF(ix + line_offset, iy)), + abs_pos=( + plot.mapFromView(QPointF(vl_x, iy)) - + QPointF(left_axis_width, 0) + ), value=ix, )