Move all anchor funcs to new mod

ordermodepps_backup
Tyler Goodlet 2021-07-16 14:50:59 -04:00
parent ee5d5b0cef
commit 0ddeded03d
3 changed files with 127 additions and 102 deletions

View File

@ -18,6 +18,11 @@
Anchor funtions for UI placement of annotions. Anchor funtions for UI placement of annotions.
''' '''
from typing import Callable
from PyQt5.QtCore import QPointF
from ._label import Label
def marker_right_points( def marker_right_points(
@ -26,8 +31,13 @@ def marker_right_points(
marker_size: int = 20, marker_size: int = 20,
) -> (float, float, float): ) -> (float, float, float):
'''Return x-dimension, y-axis-aware, level-line marker oriented scene values.
# chart = self._chart X values correspond to set the end of a level line, end of
a paried level line marker, and the right most side of the "right"
axis respectively.
'''
l1_len = chart._max_l1_line_len l1_len = chart._max_l1_line_len
ryaxis = chart.getAxis('right') ryaxis = chart.getAxis('right')
@ -38,3 +48,110 @@ def marker_right_points(
line_end = marker_right - (6/16 * marker_size) line_end = marker_right - (6/16 * marker_size)
return line_end, marker_right, r_axis_x return line_end, marker_right, r_axis_x
def vbr_left(
label: Label,
) -> Callable[..., float]:
"""Return a closure which gives the scene x-coordinate for the
leftmost point of the containing view box.
"""
return label.vbr().left
def right_axis(
chart: 'ChartPlotWidget', # noqa
label: Label,
side: str = 'left',
offset: float = 10,
avoid_book: bool = True,
# width: float = None,
) -> Callable[..., float]:
'''Return a position closure which gives the scene x-coordinate for
the x point on the right y-axis minus the width of the label given
it's contents.
'''
ryaxis = chart.getAxis('right')
if side == 'left':
if avoid_book:
def right_axis_offset_by_w() -> float:
# l1 spread graphics x-size
l1_len = chart._max_l1_line_len
# sum of all distances "from" the y-axis
right_offset = l1_len + label.w + offset
return ryaxis.pos().x() - right_offset
else:
def right_axis_offset_by_w() -> float:
return ryaxis.pos().x() - (label.w + offset)
return right_axis_offset_by_w
elif 'right':
# axis_offset = ryaxis.style['tickTextOffset'][0]
def on_axis() -> float:
return ryaxis.pos().x() # + axis_offset - 2
return on_axis
def update_pp_nav(
chartview: 'ChartView', # noqa
line: 'LevelLine', # noqa
) -> None:
'''Show a pp off-screen indicator for a level label.
This is like in fps games where you have a gps "nav" indicator
but your teammate is outside the range of view, except in 2D, on
the y-dimension.
'''
vr = chartview.state['viewRange']
ymn, ymx = vr[1]
level = line.value()
marker = line._marker
label = marker.label
_, marker_right, _ = marker_right_points(line._chart)
if level > ymx: # pin to top of view
marker.setPos(
QPointF(
marker_right,
marker._height/3,
)
)
elif level < ymn: # pin to bottom of view
marker.setPos(
QPointF(
marker_right,
chartview.height() - 4/3*marker._height,
)
)
else:
# pp line is viewable so show marker normally
marker.update()
# re-anchor label (i.e. trigger call of ``arrow_tr()`` from above
label.update()

View File

@ -223,61 +223,3 @@ class Label:
def delete(self) -> None: def delete(self) -> None:
self.vb.scene().removeItem(self.txt) self.vb.scene().removeItem(self.txt)
# anchoring helper funcs
def vbr_left(label) -> Callable[..., float]:
"""Return a closure which gives the scene x-coordinate for the
leftmost point of the containing view box.
"""
return label.vbr().left
def right_axis(
chart: 'ChartPlotWidget', # noqa
label: 'Label', # noqa
side: str = 'left',
offset: float = 10,
avoid_book: bool = True,
width: float = None,
) -> Callable[..., float]:
"""Return a position closure which gives the scene x-coordinate for
the x point on the right y-axis minus the width of the label given
it's contents.
"""
ryaxis = chart.getAxis('right')
if side == 'left':
if avoid_book:
def right_axis_offset_by_w() -> float:
# l1 spread graphics x-size
l1_len = chart._max_l1_line_len
# sum of all distances "from" the y-axis
right_offset = l1_len + label.w + offset
return ryaxis.pos().x() - right_offset
else:
def right_axis_offset_by_w() -> float:
return ryaxis.pos().x() - (label.w + offset)
return right_axis_offset_by_w
elif 'right':
# axis_offset = ryaxis.style['tickTextOffset'][0]
def on_axis() -> float:
return ryaxis.pos().x() # + axis_offset - 2
return on_axis

View File

@ -18,6 +18,7 @@
Lines for orders, alerts, L2. Lines for orders, alerts, L2.
""" """
from functools import partial
from math import floor from math import floor
from typing import Tuple, Optional, List from typing import Tuple, Optional, List
@ -27,8 +28,13 @@ from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QPointF from PyQt5.QtCore import QPointF
from ._annotate import mk_marker, qgo_draw_markers from ._annotate import mk_marker, qgo_draw_markers
from ._anchors import marker_right_points from ._anchors import (
from ._label import Label, vbr_left, right_axis marker_right_points,
vbr_left,
right_axis,
update_pp_nav,
)
from ._label import Label
from ._style import hcolor, _font from ._style import hcolor, _font
@ -785,46 +791,6 @@ def position_line(
# sanity check # sanity check
line.update_labels({'level': level}) line.update_labels({'level': level})
def update_pp_nav(chartview): vb.sigRangeChanged.connect(partial(update_pp_nav, chartview=vb, line=line))
'''Show a pp off-screen indicator when order mode is activated.
This is like in fps games where you have a gps "nav" indicator
but your teammate is outside the range of view, except in 2D, on
the y-dimension.
'''
vr = vb.state['viewRange']
ymn, ymx = vr[1]
level = line.value()
marker = line._marker
label = marker.label
_, marker_right, _ = marker_right_points(line._chart)
if level > ymx: # pin to top of view
marker.setPos(
QPointF(
marker_right,
marker._height/3,
)
)
elif level < ymn: # pin to bottom of view
marker.setPos(
QPointF(
marker_right,
vb.height() - 4/3*marker._height,
)
)
else:
# pp line is viewable so show marker normally
marker.update()
# re-anchor label (i.e. trigger call of ``arrow_tr()`` from above
label.update()
vb.sigRangeChanged.connect(update_pp_nav)
return line return line