Move view mode mouse click into async handler

fsp_feeds
Tyler Goodlet 2021-08-13 12:02:07 -04:00
parent 5ce6dcf3cb
commit 4f4f9f66b4
1 changed files with 52 additions and 30 deletions

View File

@ -23,6 +23,7 @@ import time
from typing import Optional, Callable from typing import Optional, Callable
import pyqtgraph as pg import pyqtgraph as pg
from PyQt5.QtWidgets import QGraphicsSceneMouseEvent as gs_mouse
from PyQt5.QtCore import Qt, QEvent from PyQt5.QtCore import Qt, QEvent
from pyqtgraph import ViewBox, Point, QtCore from pyqtgraph import ViewBox, Point, QtCore
from pyqtgraph import functions as fn from pyqtgraph import functions as fn
@ -32,12 +33,13 @@ import trio
from ..log import get_logger from ..log import get_logger
from ._style import _min_points_to_show from ._style import _min_points_to_show
from ._editors import SelectRect from ._editors import SelectRect
from . import _event
log = get_logger(__name__) log = get_logger(__name__)
async def handle_viewmode_inputs( async def handle_viewmode_kb_inputs(
view: 'ChartView', view: 'ChartView',
recv_chan: trio.abc.ReceiveChannel, recv_chan: trio.abc.ReceiveChannel,
@ -185,7 +187,7 @@ async def handle_viewmode_inputs(
if order_keys_pressed: if order_keys_pressed:
# show the pp label # show the pp size label
mode.pp.show() mode.pp.show()
# TODO: show pp config mini-params in status bar widget # TODO: show pp config mini-params in status bar widget
@ -196,10 +198,10 @@ async def handle_viewmode_inputs(
Qt.Key_S in pressed or Qt.Key_S in pressed or
ctrl ctrl
): ):
trigger_mode: str = 'live' trigger_type: str = 'live'
else: else:
trigger_mode: str = 'dark' trigger_type: str = 'dark'
# order mode trigger "actions" # order mode trigger "actions"
if Qt.Key_D in pressed: # for "damp eet" if Qt.Key_D in pressed: # for "damp eet"
@ -210,22 +212,21 @@ async def handle_viewmode_inputs(
elif Qt.Key_A in pressed: elif Qt.Key_A in pressed:
action = 'alert' action = 'alert'
trigger_mode = 'live' trigger_type = 'live'
view.order_mode = True view.order_mode = True
# XXX: order matters here for line style! # XXX: order matters here for line style!
view.mode._exec_mode = trigger_mode view.mode._trigger_type = trigger_type
view.mode.set_exec(action) view.mode.stage_order(action, trigger_type=trigger_type)
prefix = trigger_mode + '-' if action != 'alert' else '' prefix = trigger_type + '-' if action != 'alert' else ''
view._chart.window().set_mode_name(f'{prefix}{action}') view._chart.window().set_mode_name(f'{prefix}{action}')
else: # none active else: # none active
# hide pp label # hide pp label
mode.pp.hide_info() mode.pp.hide_info()
# mode.pp_config.hide()
# if none are pressed, remove "staged" level # if none are pressed, remove "staged" level
# line under cursor position # line under cursor position
@ -240,6 +241,28 @@ async def handle_viewmode_inputs(
last = time.time() last = time.time()
async def handle_viewmode_mouse(
view: 'ChartView',
recv_chan: trio.abc.ReceiveChannel,
) -> None:
async for msg in recv_chan:
button = msg.button
# pos = ev.pos()
if button == QtCore.Qt.RightButton and view.menuEnabled():
# ev.accept()
view.raiseContextMenu(msg.event)
elif button == QtCore.Qt.LeftButton:
# when in order mode, submit execution
if view.order_mode:
# ev.accept()
view.mode.submit_order()
class ChartView(ViewBox): class ChartView(ViewBox):
''' '''
Price chart view box with interaction behaviors you'd expect from Price chart view box with interaction behaviors you'd expect from
@ -284,12 +307,23 @@ class ChartView(ViewBox):
self, self,
) -> 'ChartView': ) -> 'ChartView':
from . import _event
async with _event.open_handlers( async with (
[self], _event.open_handlers(
event_types={QEvent.KeyPress, QEvent.KeyRelease}, [self],
async_handler=handle_viewmode_inputs, event_types={
QEvent.KeyPress,
QEvent.KeyRelease,
},
async_handler=handle_viewmode_kb_inputs,
),
_event.open_handlers(
[self],
event_types={
gs_mouse.GraphicsSceneMousePress,
},
async_handler=handle_viewmode_mouse,
),
): ):
yield self yield self
@ -481,22 +515,10 @@ class ChartView(ViewBox):
self.scaleBy(x=x, y=y, center=center) self.scaleBy(x=x, y=y, center=center)
self.sigRangeChangedManually.emit(self.state['mouseEnabled']) self.sigRangeChangedManually.emit(self.state['mouseEnabled'])
def mouseClickEvent(self, ev): def mouseClickEvent(self, event: QtCore.QEvent) -> None:
"""Full-click callback. '''This routine is rerouted to an async handler.
'''
""" pass
button = ev.button()
# pos = ev.pos()
if button == QtCore.Qt.RightButton and self.menuEnabled():
ev.accept()
self.raiseContextMenu(ev)
elif button == QtCore.Qt.LeftButton:
# when in order mode, submit execution
if self.order_mode:
ev.accept()
self.mode.submit_exec()
def keyReleaseEvent(self, event: QtCore.QEvent) -> None: def keyReleaseEvent(self, event: QtCore.QEvent) -> None:
'''This routine is rerouted to an async handler. '''This routine is rerouted to an async handler.