2020-11-06 17:23:14 +00:00
|
|
|
# piker: trading gear for hackers
|
2021-01-05 18:37:03 +00:00
|
|
|
# Copyright (C) Tyler Goodlet (in stewardship for piker0)
|
2020-11-06 17:23:14 +00:00
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-08-15 02:17:57 +00:00
|
|
|
"""
|
2021-03-18 17:33:10 +00:00
|
|
|
Chart view box primitives
|
|
|
|
|
2020-08-15 02:17:57 +00:00
|
|
|
"""
|
2022-01-24 20:19:45 +00:00
|
|
|
from __future__ import annotations
|
2021-06-15 22:19:59 +00:00
|
|
|
from contextlib import asynccontextmanager
|
2022-04-04 04:35:32 +00:00
|
|
|
# import itertools
|
2021-06-17 20:52:54 +00:00
|
|
|
import time
|
|
|
|
from typing import Optional, Callable
|
2020-12-30 17:55:02 +00:00
|
|
|
|
2020-08-15 02:17:57 +00:00
|
|
|
import pyqtgraph as pg
|
2021-08-18 12:57:08 +00:00
|
|
|
# from pyqtgraph.GraphicsScene import mouseEvents
|
2021-08-13 16:02:07 +00:00
|
|
|
from PyQt5.QtWidgets import QGraphicsSceneMouseEvent as gs_mouse
|
2021-06-15 23:02:46 +00:00
|
|
|
from PyQt5.QtCore import Qt, QEvent
|
|
|
|
from pyqtgraph import ViewBox, Point, QtCore
|
2020-08-15 02:17:57 +00:00
|
|
|
from pyqtgraph import functions as fn
|
2020-11-08 21:15:34 +00:00
|
|
|
import numpy as np
|
2021-06-15 22:19:59 +00:00
|
|
|
import trio
|
2020-08-15 02:17:57 +00:00
|
|
|
|
|
|
|
from ..log import get_logger
|
2022-04-04 21:29:33 +00:00
|
|
|
from .._profile import pg_profile_enabled, ms_slower_then
|
2021-06-15 23:02:46 +00:00
|
|
|
from ._style import _min_points_to_show
|
|
|
|
from ._editors import SelectRect
|
2021-08-13 16:02:07 +00:00
|
|
|
from . import _event
|
2022-04-04 19:58:06 +00:00
|
|
|
from ._ohlc import BarItems
|
2020-08-15 02:17:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = get_logger(__name__)
|
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
NUMBER_LINE = {
|
|
|
|
Qt.Key_1,
|
|
|
|
Qt.Key_2,
|
|
|
|
Qt.Key_3,
|
|
|
|
Qt.Key_4,
|
|
|
|
Qt.Key_5,
|
|
|
|
Qt.Key_6,
|
|
|
|
Qt.Key_7,
|
|
|
|
Qt.Key_8,
|
|
|
|
Qt.Key_9,
|
|
|
|
Qt.Key_0,
|
|
|
|
}
|
|
|
|
|
|
|
|
ORDER_MODE = {
|
|
|
|
Qt.Key_A,
|
|
|
|
Qt.Key_F,
|
|
|
|
Qt.Key_D,
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:17:57 +00:00
|
|
|
|
2021-08-13 16:02:07 +00:00
|
|
|
async def handle_viewmode_kb_inputs(
|
2021-06-15 22:19:59 +00:00
|
|
|
|
|
|
|
view: 'ChartView',
|
|
|
|
recv_chan: trio.abc.ReceiveChannel,
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
order_mode = view.order_mode
|
2021-06-17 20:52:54 +00:00
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
# track edge triggered keys
|
|
|
|
# (https://en.wikipedia.org/wiki/Interrupt#Triggering_methods)
|
|
|
|
pressed: set[str] = set()
|
|
|
|
|
2021-06-17 20:52:54 +00:00
|
|
|
last = time.time()
|
|
|
|
trigger_mode: str
|
|
|
|
action: str
|
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
on_next_release: Optional[Callable] = None
|
|
|
|
|
2021-06-17 20:52:54 +00:00
|
|
|
# for quick key sequence-combo pattern matching
|
|
|
|
# we have a min_tap period and these should not
|
|
|
|
# ever be auto-repeats since we filter those at the
|
|
|
|
# event filter level prior to the above mem chan.
|
|
|
|
min_tap = 1/6
|
|
|
|
fast_key_seq: list[str] = []
|
|
|
|
fast_taps: dict[str, Callable] = {
|
2021-08-18 12:57:08 +00:00
|
|
|
'cc': order_mode.cancel_all_orders,
|
2021-06-17 20:52:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 20:57:19 +00:00
|
|
|
async for kbmsg in recv_chan:
|
|
|
|
event, etype, key, mods, text = kbmsg.to_tuple()
|
2021-06-15 22:19:59 +00:00
|
|
|
log.debug(f'key: {key}, mods: {mods}, text: {text}')
|
2021-06-17 20:52:54 +00:00
|
|
|
now = time.time()
|
|
|
|
period = now - last
|
2021-06-15 22:19:59 +00:00
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
# reset mods
|
|
|
|
ctrl: bool = False
|
|
|
|
shift: bool = False
|
|
|
|
|
|
|
|
# press branch
|
2021-06-15 22:19:59 +00:00
|
|
|
if etype in {QEvent.KeyPress}:
|
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
pressed.add(key)
|
2021-06-16 09:24:04 +00:00
|
|
|
|
2021-06-17 20:52:54 +00:00
|
|
|
if (
|
|
|
|
# clear any old values not part of a "fast" tap sequence:
|
|
|
|
# presumes the period since last tap is longer then our
|
|
|
|
# min_tap period
|
|
|
|
fast_key_seq and period >= min_tap or
|
|
|
|
|
|
|
|
# don't support more then 2 key sequences for now
|
|
|
|
len(fast_key_seq) > 2
|
|
|
|
):
|
|
|
|
fast_key_seq.clear()
|
|
|
|
|
|
|
|
# capture key to fast tap sequence if we either
|
|
|
|
# have no previous keys or we do and the min_tap period is
|
|
|
|
# met
|
|
|
|
if (
|
|
|
|
not fast_key_seq or
|
|
|
|
period <= min_tap and fast_key_seq
|
|
|
|
):
|
|
|
|
fast_key_seq.append(text)
|
|
|
|
log.debug(f'fast keys seqs {fast_key_seq}')
|
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
# mods run through
|
2021-06-16 09:24:04 +00:00
|
|
|
if mods == Qt.ShiftModifier:
|
2021-06-16 12:27:34 +00:00
|
|
|
shift = True
|
2021-06-16 09:24:04 +00:00
|
|
|
|
|
|
|
if mods == Qt.ControlModifier:
|
|
|
|
ctrl = True
|
|
|
|
|
2021-06-17 20:52:54 +00:00
|
|
|
# SEARCH MODE #
|
2021-06-16 09:24:04 +00:00
|
|
|
# ctlr-<space>/<l> for "lookup", "search" -> open search tree
|
2021-06-16 12:27:34 +00:00
|
|
|
if (
|
|
|
|
ctrl and key in {
|
|
|
|
Qt.Key_L,
|
|
|
|
Qt.Key_Space,
|
|
|
|
}
|
|
|
|
):
|
2021-07-22 15:23:41 +00:00
|
|
|
view._chart.linked.godwidget.search.focus()
|
2021-06-16 09:24:04 +00:00
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
# esc and ctrl-c
|
2021-06-16 09:24:04 +00:00
|
|
|
if key == Qt.Key_Escape or (ctrl and key == Qt.Key_C):
|
|
|
|
# ctrl-c as cancel
|
|
|
|
# https://forum.qt.io/topic/532/how-to-catch-ctrl-c-on-a-widget/9
|
|
|
|
view.select_box.clear()
|
|
|
|
|
|
|
|
# cancel order or clear graphics
|
|
|
|
if key == Qt.Key_C or key == Qt.Key_Delete:
|
2021-06-16 12:27:34 +00:00
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
order_mode.cancel_orders_under_cursor()
|
2021-06-16 09:24:04 +00:00
|
|
|
|
|
|
|
# View modes
|
|
|
|
if key == Qt.Key_R:
|
2021-06-16 12:27:34 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# TODO: set this for all subplots
|
2021-06-16 12:27:34 +00:00
|
|
|
# edge triggered default view activation
|
2021-06-16 09:24:04 +00:00
|
|
|
view.chart.default_view()
|
|
|
|
|
2021-06-17 20:52:54 +00:00
|
|
|
if len(fast_key_seq) > 1:
|
|
|
|
# begin matches against sequences
|
|
|
|
func: Callable = fast_taps.get(''.join(fast_key_seq))
|
|
|
|
if func:
|
|
|
|
func()
|
|
|
|
fast_key_seq.clear()
|
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
# release branch
|
|
|
|
elif etype in {QEvent.KeyRelease}:
|
2021-06-16 09:24:04 +00:00
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
if on_next_release:
|
|
|
|
on_next_release()
|
|
|
|
on_next_release = None
|
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
if key in pressed:
|
|
|
|
pressed.remove(key)
|
2021-06-16 09:24:04 +00:00
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
# QUERY/QUOTE MODE #
|
2021-06-22 11:17:49 +00:00
|
|
|
if {Qt.Key_Q}.intersection(pressed):
|
|
|
|
|
|
|
|
view.linkedsplits.cursor.in_query_mode = True
|
|
|
|
|
|
|
|
else:
|
|
|
|
view.linkedsplits.cursor.in_query_mode = False
|
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
# SELECTION MODE
|
|
|
|
# --------------
|
2021-06-16 09:24:04 +00:00
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
if shift:
|
|
|
|
if view.state['mouseMode'] == ViewBox.PanMode:
|
|
|
|
view.setMouseMode(ViewBox.RectMode)
|
|
|
|
else:
|
|
|
|
view.setMouseMode(ViewBox.PanMode)
|
2021-06-16 09:24:04 +00:00
|
|
|
|
2021-07-26 15:33:14 +00:00
|
|
|
# Toggle position config pane
|
|
|
|
if (
|
|
|
|
ctrl and key in {
|
|
|
|
Qt.Key_P,
|
|
|
|
}
|
|
|
|
):
|
2021-09-10 15:50:24 +00:00
|
|
|
pp_pane = order_mode.current_pp.pane
|
2021-08-18 12:57:08 +00:00
|
|
|
if pp_pane.isHidden():
|
|
|
|
pp_pane.show()
|
2021-07-26 15:33:14 +00:00
|
|
|
else:
|
2021-08-18 12:57:08 +00:00
|
|
|
pp_pane.hide()
|
|
|
|
|
|
|
|
# ORDER MODE
|
|
|
|
# ----------
|
2021-07-26 15:33:14 +00:00
|
|
|
|
2021-06-17 20:52:54 +00:00
|
|
|
# live vs. dark trigger + an action {buy, sell, alert}
|
2021-08-18 12:57:08 +00:00
|
|
|
order_keys_pressed = ORDER_MODE.intersection(pressed)
|
2021-06-16 09:24:04 +00:00
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
if order_keys_pressed:
|
2021-07-22 00:00:57 +00:00
|
|
|
|
2021-08-13 16:02:07 +00:00
|
|
|
# show the pp size label
|
2021-09-10 15:50:24 +00:00
|
|
|
order_mode.current_pp.show()
|
2021-07-26 15:33:14 +00:00
|
|
|
|
|
|
|
# TODO: show pp config mini-params in status bar widget
|
|
|
|
# mode.pp_config.show()
|
2021-07-22 00:00:57 +00:00
|
|
|
|
2021-06-17 20:52:54 +00:00
|
|
|
if (
|
|
|
|
# 's' for "submit" to activate "live" order
|
|
|
|
Qt.Key_S in pressed or
|
|
|
|
ctrl
|
|
|
|
):
|
2021-08-13 16:02:07 +00:00
|
|
|
trigger_type: str = 'live'
|
2021-06-17 20:52:54 +00:00
|
|
|
|
|
|
|
else:
|
2021-08-13 16:02:07 +00:00
|
|
|
trigger_type: str = 'dark'
|
2021-06-16 09:24:04 +00:00
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
# order mode trigger "actions"
|
|
|
|
if Qt.Key_D in pressed: # for "damp eet"
|
2021-06-17 20:52:54 +00:00
|
|
|
action = 'sell'
|
2021-06-16 09:24:04 +00:00
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
elif Qt.Key_F in pressed: # for "fillz eet"
|
2021-06-17 20:52:54 +00:00
|
|
|
action = 'buy'
|
2021-06-16 12:27:34 +00:00
|
|
|
|
|
|
|
elif Qt.Key_A in pressed:
|
2021-06-17 20:52:54 +00:00
|
|
|
action = 'alert'
|
2021-08-13 16:02:07 +00:00
|
|
|
trigger_type = 'live'
|
2021-06-17 20:52:54 +00:00
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
order_mode.active = True
|
2021-06-17 20:52:54 +00:00
|
|
|
|
|
|
|
# XXX: order matters here for line style!
|
2021-08-18 12:57:08 +00:00
|
|
|
order_mode._trigger_type = trigger_type
|
|
|
|
order_mode.stage_order(
|
2021-08-15 17:38:46 +00:00
|
|
|
action,
|
|
|
|
trigger_type=trigger_type,
|
|
|
|
)
|
2021-06-17 20:52:54 +00:00
|
|
|
|
2021-08-13 16:02:07 +00:00
|
|
|
prefix = trigger_type + '-' if action != 'alert' else ''
|
2021-07-24 20:07:04 +00:00
|
|
|
view._chart.window().set_mode_name(f'{prefix}{action}')
|
2021-06-16 12:27:34 +00:00
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
elif (
|
|
|
|
(
|
|
|
|
Qt.Key_S in pressed or
|
|
|
|
order_keys_pressed or
|
|
|
|
Qt.Key_O in pressed
|
|
|
|
) and
|
|
|
|
key in NUMBER_LINE
|
|
|
|
):
|
2021-09-10 15:50:24 +00:00
|
|
|
# hot key to set order slots size.
|
|
|
|
# change edit field to current number line value,
|
|
|
|
# update the pp allocator bar, unhighlight the
|
|
|
|
# field when ctrl is released.
|
2021-08-18 12:57:08 +00:00
|
|
|
num = int(text)
|
2021-08-23 18:48:20 +00:00
|
|
|
pp_pane = order_mode.pane
|
2021-08-26 12:40:14 +00:00
|
|
|
pp_pane.on_ui_settings_change('slots', num)
|
2021-08-23 18:48:20 +00:00
|
|
|
edit = pp_pane.form.fields['slots']
|
2021-08-18 12:57:08 +00:00
|
|
|
edit.selectAll()
|
2021-09-10 15:50:24 +00:00
|
|
|
# un-highlight on ctrl release
|
2021-08-18 12:57:08 +00:00
|
|
|
on_next_release = edit.deselect
|
2021-09-13 23:08:30 +00:00
|
|
|
pp_pane.update_status_ui(pp_pane.order_mode.current_pp)
|
2021-08-18 12:57:08 +00:00
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
else: # none active
|
2021-07-22 00:00:57 +00:00
|
|
|
|
|
|
|
# hide pp label
|
2021-09-10 15:50:24 +00:00
|
|
|
order_mode.current_pp.hide_info()
|
2021-07-22 00:00:57 +00:00
|
|
|
|
2021-06-16 12:27:34 +00:00
|
|
|
# if none are pressed, remove "staged" level
|
|
|
|
# line under cursor position
|
2021-08-18 12:57:08 +00:00
|
|
|
order_mode.lines.unstage_line()
|
2021-06-17 20:52:54 +00:00
|
|
|
|
|
|
|
if view.hasFocus():
|
|
|
|
# update mode label
|
2021-07-24 20:07:04 +00:00
|
|
|
view._chart.window().set_mode_name('view')
|
2021-06-17 20:52:54 +00:00
|
|
|
|
2021-08-18 12:57:08 +00:00
|
|
|
order_mode.active = False
|
2021-06-17 20:52:54 +00:00
|
|
|
|
|
|
|
last = time.time()
|
2021-06-15 22:19:59 +00:00
|
|
|
|
|
|
|
|
2021-08-13 16:02:07 +00:00
|
|
|
async def handle_viewmode_mouse(
|
|
|
|
|
|
|
|
view: 'ChartView',
|
|
|
|
recv_chan: trio.abc.ReceiveChannel,
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
async for msg in recv_chan:
|
|
|
|
button = msg.button
|
|
|
|
|
2021-08-13 17:49:07 +00:00
|
|
|
# XXX: ugggh ``pyqtgraph`` has its own mouse events..
|
|
|
|
# so we can't overried this easily.
|
|
|
|
# it's going to take probably some decent
|
|
|
|
# reworking of the mouseClickEvent() handler.
|
2021-08-13 16:02:07 +00:00
|
|
|
|
2021-08-13 17:49:07 +00:00
|
|
|
# if button == QtCore.Qt.RightButton and view.menuEnabled():
|
|
|
|
# event = mouseEvents.MouseClickEvent(msg.event)
|
|
|
|
# # event.accept()
|
|
|
|
# view.raiseContextMenu(event)
|
|
|
|
|
|
|
|
if (
|
2021-08-18 12:57:08 +00:00
|
|
|
view.order_mode.active and
|
2021-08-13 17:49:07 +00:00
|
|
|
button == QtCore.Qt.LeftButton
|
|
|
|
):
|
2021-08-13 16:02:07 +00:00
|
|
|
# when in order mode, submit execution
|
2021-08-13 17:49:07 +00:00
|
|
|
# msg.event.accept()
|
2022-04-04 04:35:32 +00:00
|
|
|
# breakpoint()
|
2021-08-18 12:57:08 +00:00
|
|
|
view.order_mode.submit_order()
|
2021-08-13 16:02:07 +00:00
|
|
|
|
|
|
|
|
2020-11-08 21:15:34 +00:00
|
|
|
class ChartView(ViewBox):
|
2021-06-15 22:19:59 +00:00
|
|
|
'''
|
|
|
|
Price chart view box with interaction behaviors you'd expect from
|
2020-08-15 02:17:57 +00:00
|
|
|
any interactive platform:
|
|
|
|
|
|
|
|
- zoom on mouse scroll that auto fits y-axis
|
2021-01-05 13:02:14 +00:00
|
|
|
- vertical scrolling on y-axis
|
|
|
|
- zoom on x to most recent in view datum
|
|
|
|
- zoom on right-click-n-drag to cursor position
|
2021-01-07 17:03:18 +00:00
|
|
|
|
2021-06-15 22:19:59 +00:00
|
|
|
'''
|
2021-07-24 20:07:04 +00:00
|
|
|
mode_name: str = 'view'
|
2021-05-30 12:45:55 +00:00
|
|
|
|
2022-01-21 22:02:08 +00:00
|
|
|
# "relay events" for making overlaid views work.
|
|
|
|
# NOTE: these MUST be defined here (and can't be monkey patched
|
|
|
|
# on later) due to signal construction requiring refs to be
|
|
|
|
# in place during the run of meta-class machinery.
|
|
|
|
mouseDragEventRelay = QtCore.Signal(object, object, object)
|
|
|
|
wheelEventRelay = QtCore.Signal(object, object, object)
|
|
|
|
|
|
|
|
event_relay_source: 'Optional[ViewBox]' = None
|
2022-02-08 20:57:32 +00:00
|
|
|
relays: dict[str, QtCore.Signal] = {}
|
2022-01-21 22:02:08 +00:00
|
|
|
|
2020-08-15 02:17:57 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-07-22 00:00:57 +00:00
|
|
|
|
2021-06-15 22:19:59 +00:00
|
|
|
name: str,
|
2021-07-22 00:00:57 +00:00
|
|
|
|
2020-11-08 21:15:34 +00:00
|
|
|
parent: pg.PlotItem = None,
|
2022-01-08 22:52:16 +00:00
|
|
|
static_yrange: Optional[tuple[float, float]] = None,
|
2020-08-15 02:17:57 +00:00
|
|
|
**kwargs,
|
2021-06-15 22:19:59 +00:00
|
|
|
|
2020-08-15 02:17:57 +00:00
|
|
|
):
|
2021-09-18 21:09:30 +00:00
|
|
|
super().__init__(
|
|
|
|
parent=parent,
|
2022-04-06 15:11:28 +00:00
|
|
|
name=name,
|
2021-09-18 21:09:30 +00:00
|
|
|
# TODO: look into the default view padding
|
|
|
|
# support that might replace somem of our
|
|
|
|
# ``ChartPlotWidget._set_yrange()`
|
|
|
|
# defaultPadding=0.,
|
|
|
|
**kwargs
|
|
|
|
)
|
2022-01-08 22:52:16 +00:00
|
|
|
# for "known y-range style"
|
|
|
|
self._static_yrange = static_yrange
|
|
|
|
self._maxmin = None
|
|
|
|
|
2020-08-15 02:17:57 +00:00
|
|
|
# disable vertical scrolling
|
2022-01-08 22:52:16 +00:00
|
|
|
self.setMouseEnabled(
|
|
|
|
x=True,
|
|
|
|
y=True,
|
|
|
|
)
|
2021-06-15 22:19:59 +00:00
|
|
|
|
|
|
|
self.linkedsplits = None
|
|
|
|
self._chart: 'ChartPlotWidget' = None # noqa
|
|
|
|
|
|
|
|
# add our selection box annotator
|
2020-11-08 21:15:34 +00:00
|
|
|
self.select_box = SelectRect(self)
|
|
|
|
self.addItem(self.select_box, ignoreBounds=True)
|
|
|
|
|
2021-01-07 17:03:18 +00:00
|
|
|
self.mode = None
|
2021-06-17 20:52:54 +00:00
|
|
|
self.order_mode: bool = False
|
2021-01-07 17:03:18 +00:00
|
|
|
|
2021-04-12 22:04:26 +00:00
|
|
|
self.setFocusPolicy(QtCore.Qt.StrongFocus)
|
2022-04-04 04:35:32 +00:00
|
|
|
self._ic = None
|
|
|
|
|
|
|
|
def start_ic(
|
|
|
|
self,
|
|
|
|
) -> None:
|
2022-04-07 18:15:18 +00:00
|
|
|
'''
|
|
|
|
Signal the beginning of a click-drag interaction
|
|
|
|
to any interested task waiters.
|
|
|
|
|
|
|
|
'''
|
2022-04-04 04:35:32 +00:00
|
|
|
if self._ic is None:
|
|
|
|
self.chart.pause_all_feeds()
|
|
|
|
self._ic = trio.Event()
|
|
|
|
|
|
|
|
def signal_ic(
|
|
|
|
self,
|
|
|
|
*args,
|
2022-04-07 18:15:18 +00:00
|
|
|
|
2022-04-04 04:35:32 +00:00
|
|
|
) -> None:
|
2022-04-07 18:15:18 +00:00
|
|
|
'''
|
|
|
|
Signal the end of a click-drag interaction
|
|
|
|
to any waiters.
|
2022-04-04 04:35:32 +00:00
|
|
|
|
2022-04-07 18:15:18 +00:00
|
|
|
'''
|
2022-04-04 04:35:32 +00:00
|
|
|
if self._ic:
|
|
|
|
self._ic.set()
|
|
|
|
self._ic = None
|
|
|
|
self.chart.resume_all_feeds()
|
2021-04-12 22:04:26 +00:00
|
|
|
|
2021-06-15 22:19:59 +00:00
|
|
|
@asynccontextmanager
|
|
|
|
async def open_async_input_handler(
|
|
|
|
self,
|
2021-07-22 00:00:57 +00:00
|
|
|
|
2021-06-15 22:19:59 +00:00
|
|
|
) -> 'ChartView':
|
|
|
|
|
2021-08-13 16:02:07 +00:00
|
|
|
async with (
|
|
|
|
_event.open_handlers(
|
|
|
|
[self],
|
|
|
|
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,
|
|
|
|
),
|
2021-06-15 22:19:59 +00:00
|
|
|
):
|
|
|
|
yield self
|
|
|
|
|
2020-11-08 21:15:34 +00:00
|
|
|
@property
|
2021-01-01 18:05:50 +00:00
|
|
|
def chart(self) -> 'ChartPlotWidget': # type: ignore # noqa
|
2020-11-08 21:15:34 +00:00
|
|
|
return self._chart
|
|
|
|
|
|
|
|
@chart.setter
|
2021-01-01 18:05:50 +00:00
|
|
|
def chart(self, chart: 'ChartPlotWidget') -> None: # type: ignore # noqa
|
2020-11-08 21:15:34 +00:00
|
|
|
self._chart = chart
|
|
|
|
self.select_box.chart = chart
|
2022-01-08 22:52:16 +00:00
|
|
|
if self._maxmin is None:
|
|
|
|
self._maxmin = chart.maxmin
|
2020-08-15 02:17:57 +00:00
|
|
|
|
2022-02-10 03:15:57 +00:00
|
|
|
@property
|
|
|
|
def maxmin(self) -> Callable:
|
|
|
|
return self._maxmin
|
|
|
|
|
|
|
|
@maxmin.setter
|
|
|
|
def maxmin(self, callback: Callable) -> None:
|
|
|
|
self._maxmin = callback
|
|
|
|
|
2022-01-24 20:19:45 +00:00
|
|
|
def wheelEvent(
|
|
|
|
self,
|
|
|
|
ev,
|
|
|
|
axis=None,
|
|
|
|
relayed_from: ChartView = None,
|
|
|
|
):
|
2022-03-11 19:49:34 +00:00
|
|
|
'''
|
|
|
|
Override "center-point" location for scrolling.
|
2020-08-15 02:17:57 +00:00
|
|
|
|
|
|
|
This is an override of the ``ViewBox`` method simply changing
|
|
|
|
the center of the zoom to be the y-axis.
|
|
|
|
|
|
|
|
TODO: PR a method into ``pyqtgraph`` to make this configurable
|
|
|
|
|
2021-06-15 22:19:59 +00:00
|
|
|
'''
|
2020-08-15 02:17:57 +00:00
|
|
|
if axis in (0, 1):
|
|
|
|
mask = [False, False]
|
|
|
|
mask[axis] = self.state['mouseEnabled'][axis]
|
|
|
|
else:
|
|
|
|
mask = self.state['mouseEnabled'][:]
|
|
|
|
|
2021-06-15 22:19:59 +00:00
|
|
|
chart = self.linkedsplits.chart
|
2021-03-17 12:25:58 +00:00
|
|
|
|
2020-08-15 02:17:57 +00:00
|
|
|
# don't zoom more then the min points setting
|
2021-03-17 12:25:58 +00:00
|
|
|
l, lbar, rbar, r = chart.bars_range()
|
2020-08-15 02:17:57 +00:00
|
|
|
vl = r - l
|
|
|
|
|
|
|
|
if ev.delta() > 0 and vl <= _min_points_to_show:
|
2020-10-20 01:32:50 +00:00
|
|
|
log.debug("Max zoom bruh...")
|
2020-08-15 02:17:57 +00:00
|
|
|
return
|
2020-10-25 14:49:31 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
if ev.delta() < 0 and vl >= len(chart._arrays[chart.name]) + 666:
|
2020-10-20 01:32:50 +00:00
|
|
|
log.debug("Min zoom bruh...")
|
2020-08-15 02:17:57 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# actual scaling factor
|
|
|
|
s = 1.015 ** (ev.delta() * -1 / 20) # self.state['wheelScaleFactor'])
|
|
|
|
s = [(None if m is False else s) for m in mask]
|
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
if (
|
|
|
|
# zoom happened on axis
|
|
|
|
axis == 1
|
2020-08-15 02:17:57 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# if already in axis zoom mode then keep it
|
|
|
|
or self.chart._static_yrange == 'axis'
|
|
|
|
):
|
|
|
|
self.chart._static_yrange = 'axis'
|
|
|
|
self.setLimits(yMin=None, yMax=None)
|
|
|
|
|
|
|
|
# print(scale_y)
|
|
|
|
# pos = ev.pos()
|
|
|
|
# lastPos = ev.lastPos()
|
|
|
|
# dif = pos - lastPos
|
|
|
|
# dif = dif * -1
|
2022-02-08 20:57:32 +00:00
|
|
|
center = Point(
|
|
|
|
fn.invertQTransform(
|
|
|
|
self.childGroup.transform()
|
|
|
|
).map(ev.pos())
|
|
|
|
)
|
2022-01-08 22:52:16 +00:00
|
|
|
# scale_y = 1.3 ** (center.y() * -1 / 20)
|
|
|
|
self.scaleBy(s, center)
|
2020-10-20 01:32:50 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
else:
|
2020-10-20 01:32:50 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# center = pg.Point(
|
|
|
|
# fn.invertQTransform(self.childGroup.transform()).map(ev.pos())
|
|
|
|
# )
|
2020-10-20 01:32:50 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# XXX: scroll "around" the right most element in the view
|
|
|
|
# which stays "pinned" in place.
|
2021-03-17 12:25:58 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# furthest_right_coord = self.boundingRect().topRight()
|
2021-03-17 12:25:58 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# yaxis = pg.Point(
|
|
|
|
# fn.invertQTransform(
|
|
|
|
# self.childGroup.transform()
|
|
|
|
# ).map(furthest_right_coord)
|
|
|
|
# )
|
2021-03-17 12:25:58 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# This seems like the most "intuitive option, a hybrid of
|
|
|
|
# tws and tv styles
|
|
|
|
last_bar = pg.Point(int(rbar)) + 1
|
2021-03-17 12:25:58 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
ryaxis = chart.getAxis('right')
|
|
|
|
r_axis_x = ryaxis.pos().x()
|
2021-03-17 12:25:58 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
end_of_l1 = pg.Point(
|
|
|
|
round(
|
|
|
|
chart.cv.mapToView(
|
|
|
|
pg.Point(r_axis_x - chart._max_l1_line_len)
|
|
|
|
# QPointF(chart._max_l1_line_len, 0)
|
|
|
|
).x()
|
|
|
|
)
|
|
|
|
) # .x()
|
|
|
|
|
|
|
|
# self.state['viewRange'][0][1] = end_of_l1
|
|
|
|
# focal = pg.Point((last_bar.x() + end_of_l1)/2)
|
|
|
|
|
|
|
|
focal = min(
|
|
|
|
last_bar,
|
|
|
|
end_of_l1,
|
|
|
|
key=lambda p: p.x()
|
|
|
|
)
|
|
|
|
# focal = pg.Point(last_bar.x() + end_of_l1)
|
2020-08-15 02:17:57 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
self._resetTarget()
|
|
|
|
self.scaleBy(s, focal)
|
|
|
|
self.sigRangeChangedManually.emit(mask)
|
2022-04-04 04:35:32 +00:00
|
|
|
|
|
|
|
# self._ic.set()
|
|
|
|
# self._ic = None
|
|
|
|
# self.chart.resume_all_feeds()
|
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
ev.accept()
|
2020-11-08 21:15:34 +00:00
|
|
|
|
2020-12-30 17:55:02 +00:00
|
|
|
def mouseDragEvent(
|
|
|
|
self,
|
|
|
|
ev,
|
|
|
|
axis: Optional[int] = None,
|
2022-01-24 20:19:45 +00:00
|
|
|
relayed_from: ChartView = None,
|
2022-01-08 22:52:16 +00:00
|
|
|
|
2020-12-30 17:55:02 +00:00
|
|
|
) -> None:
|
2020-11-08 21:15:34 +00:00
|
|
|
|
|
|
|
pos = ev.pos()
|
|
|
|
lastPos = ev.lastPos()
|
|
|
|
dif = pos - lastPos
|
|
|
|
dif = dif * -1
|
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# NOTE: if axis is specified, event will only affect that axis.
|
|
|
|
button = ev.button()
|
|
|
|
|
2020-11-08 21:15:34 +00:00
|
|
|
# Ignore axes if mouse is disabled
|
|
|
|
mouseEnabled = np.array(self.state['mouseEnabled'], dtype=np.float)
|
|
|
|
mask = mouseEnabled.copy()
|
|
|
|
if axis is not None:
|
|
|
|
mask[1-axis] = 0.0
|
|
|
|
|
|
|
|
# Scale or translate based on mouse button
|
2022-01-08 22:52:16 +00:00
|
|
|
if button & (
|
|
|
|
QtCore.Qt.LeftButton | QtCore.Qt.MidButton
|
|
|
|
):
|
2021-01-05 13:02:14 +00:00
|
|
|
# zoom y-axis ONLY when click-n-drag on it
|
2022-01-08 22:52:16 +00:00
|
|
|
# if axis == 1:
|
|
|
|
# # set a static y range special value on chart widget to
|
|
|
|
# # prevent sizing to data in view.
|
|
|
|
# self.chart._static_yrange = 'axis'
|
2020-12-30 17:55:02 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# scale_y = 1.3 ** (dif.y() * -1 / 20)
|
|
|
|
# self.setLimits(yMin=None, yMax=None)
|
2020-12-30 17:55:02 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# # print(scale_y)
|
|
|
|
# self.scaleBy((0, scale_y))
|
2020-12-30 17:55:02 +00:00
|
|
|
|
2022-01-24 20:19:45 +00:00
|
|
|
# SELECTION MODE
|
2022-01-08 22:52:16 +00:00
|
|
|
if (
|
|
|
|
self.state['mouseMode'] == ViewBox.RectMode
|
|
|
|
and axis is None
|
|
|
|
):
|
2022-01-24 20:19:45 +00:00
|
|
|
# XXX: WHY
|
|
|
|
ev.accept()
|
2020-11-08 21:15:34 +00:00
|
|
|
|
|
|
|
down_pos = ev.buttonDownPos()
|
|
|
|
|
|
|
|
# This is the final position in the drag
|
|
|
|
if ev.isFinish():
|
|
|
|
|
|
|
|
self.select_box.mouse_drag_released(down_pos, pos)
|
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
ax = QtCore.QRectF(down_pos, pos)
|
|
|
|
ax = self.childGroup.mapRectFromParent(ax)
|
2020-11-08 21:15:34 +00:00
|
|
|
|
|
|
|
# this is the zoom transform cmd
|
2022-01-08 22:52:16 +00:00
|
|
|
self.showAxRect(ax)
|
|
|
|
|
|
|
|
# axis history tracking
|
|
|
|
self.axHistoryPointer += 1
|
|
|
|
self.axHistory = self.axHistory[
|
|
|
|
:self.axHistoryPointer] + [ax]
|
2020-11-08 21:15:34 +00:00
|
|
|
|
|
|
|
else:
|
2022-01-08 22:52:16 +00:00
|
|
|
print('drag finish?')
|
2020-11-08 21:15:34 +00:00
|
|
|
self.select_box.set_pos(down_pos, pos)
|
|
|
|
|
|
|
|
# update shape of scale box
|
|
|
|
# self.updateScaleBox(ev.buttonDownPos(), ev.pos())
|
2022-01-08 22:52:16 +00:00
|
|
|
self.updateScaleBox(
|
|
|
|
down_pos,
|
|
|
|
ev.pos(),
|
|
|
|
)
|
2022-01-24 20:19:45 +00:00
|
|
|
|
|
|
|
# PANNING MODE
|
2020-11-08 21:15:34 +00:00
|
|
|
else:
|
2022-01-24 20:19:45 +00:00
|
|
|
# XXX: WHY
|
|
|
|
ev.accept()
|
2022-01-08 22:52:16 +00:00
|
|
|
|
2022-04-04 04:35:32 +00:00
|
|
|
self.start_ic()
|
|
|
|
# if self._ic is None:
|
|
|
|
# self.chart.pause_all_feeds()
|
|
|
|
# self._ic = trio.Event()
|
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
if axis == 1:
|
|
|
|
self.chart._static_yrange = 'axis'
|
|
|
|
|
2020-11-08 21:15:34 +00:00
|
|
|
tr = self.childGroup.transform()
|
|
|
|
tr = fn.invertQTransform(tr)
|
|
|
|
tr = tr.map(dif*mask) - tr.map(Point(0, 0))
|
|
|
|
|
|
|
|
x = tr.x() if mask[0] == 1 else None
|
|
|
|
y = tr.y() if mask[1] == 1 else None
|
|
|
|
|
|
|
|
self._resetTarget()
|
2021-01-01 22:49:23 +00:00
|
|
|
|
2020-11-08 21:15:34 +00:00
|
|
|
if x is not None or y is not None:
|
|
|
|
self.translateBy(x=x, y=y)
|
2021-01-01 22:49:23 +00:00
|
|
|
|
2020-11-08 21:15:34 +00:00
|
|
|
self.sigRangeChangedManually.emit(self.state['mouseEnabled'])
|
|
|
|
|
2022-04-04 04:35:32 +00:00
|
|
|
if ev.isFinish():
|
|
|
|
self.signal_ic()
|
|
|
|
# self._ic.set()
|
|
|
|
# self._ic = None
|
|
|
|
# self.chart.resume_all_feeds()
|
|
|
|
|
2022-01-24 20:19:45 +00:00
|
|
|
# WEIRD "RIGHT-CLICK CENTER ZOOM" MODE
|
2021-01-01 22:49:23 +00:00
|
|
|
elif button & QtCore.Qt.RightButton:
|
|
|
|
|
2020-11-08 21:15:34 +00:00
|
|
|
if self.state['aspectLocked'] is not False:
|
|
|
|
mask[0] = 0
|
|
|
|
|
|
|
|
dif = ev.screenPos() - ev.lastScreenPos()
|
|
|
|
dif = np.array([dif.x(), dif.y()])
|
|
|
|
dif[0] *= -1
|
|
|
|
s = ((mask * 0.02) + 1) ** dif
|
|
|
|
|
|
|
|
tr = self.childGroup.transform()
|
|
|
|
tr = fn.invertQTransform(tr)
|
|
|
|
|
|
|
|
x = s[0] if mouseEnabled[0] == 1 else None
|
|
|
|
y = s[1] if mouseEnabled[1] == 1 else None
|
|
|
|
|
|
|
|
center = Point(tr.map(ev.buttonDownPos(QtCore.Qt.RightButton)))
|
|
|
|
self._resetTarget()
|
|
|
|
self.scaleBy(x=x, y=y, center=center)
|
|
|
|
self.sigRangeChangedManually.emit(self.state['mouseEnabled'])
|
|
|
|
|
2022-01-24 20:19:45 +00:00
|
|
|
# XXX: WHY
|
|
|
|
ev.accept()
|
|
|
|
|
2021-08-13 17:49:07 +00:00
|
|
|
# def mouseClickEvent(self, event: QtCore.QEvent) -> None:
|
|
|
|
# '''This routine is rerouted to an async handler.
|
|
|
|
# '''
|
|
|
|
# pass
|
2021-01-07 17:03:18 +00:00
|
|
|
|
2021-06-15 22:19:59 +00:00
|
|
|
def keyReleaseEvent(self, event: QtCore.QEvent) -> None:
|
|
|
|
'''This routine is rerouted to an async handler.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
|
|
|
def keyPressEvent(self, event: QtCore.QEvent) -> None:
|
|
|
|
'''This routine is rerouted to an async handler.
|
|
|
|
'''
|
|
|
|
pass
|
2022-01-08 22:52:16 +00:00
|
|
|
|
|
|
|
def _set_yrange(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
|
|
|
|
yrange: Optional[tuple[float, float]] = None,
|
|
|
|
range_margin: float = 0.06,
|
|
|
|
bars_range: Optional[tuple[int, int, int, int]] = None,
|
|
|
|
|
|
|
|
# flag to prevent triggering sibling charts from the same linked
|
|
|
|
# set from recursion errors.
|
|
|
|
autoscale_linked_plots: bool = True,
|
2022-02-10 03:15:57 +00:00
|
|
|
name: Optional[str] = None,
|
2022-02-08 20:57:32 +00:00
|
|
|
# autoscale_overlays: bool = False,
|
2022-01-08 22:52:16 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
'''
|
|
|
|
Set the viewable y-range based on embedded data.
|
|
|
|
|
|
|
|
This adds auto-scaling like zoom on the scroll wheel such
|
|
|
|
that data always fits nicely inside the current view of the
|
|
|
|
data set.
|
|
|
|
|
|
|
|
'''
|
2022-04-06 15:11:28 +00:00
|
|
|
profiler = pg.debug.Profiler(
|
|
|
|
disabled=not pg_profile_enabled(),
|
|
|
|
gt=ms_slower_then,
|
|
|
|
delayed=True,
|
|
|
|
)
|
2022-01-08 22:52:16 +00:00
|
|
|
set_range = True
|
|
|
|
chart = self._chart
|
|
|
|
|
|
|
|
# view has been set in 'axis' mode
|
|
|
|
# meaning it can be panned and zoomed
|
|
|
|
# arbitrarily on the y-axis:
|
|
|
|
# - disable autoranging
|
|
|
|
# - remove any y range limits
|
|
|
|
if chart._static_yrange == 'axis':
|
|
|
|
set_range = False
|
|
|
|
self.setLimits(yMin=None, yMax=None)
|
|
|
|
|
|
|
|
# static y-range has been set likely by
|
|
|
|
# a specialized FSP configuration.
|
|
|
|
elif chart._static_yrange is not None:
|
|
|
|
ylow, yhigh = chart._static_yrange
|
|
|
|
|
|
|
|
# range passed in by caller, usually a
|
|
|
|
# maxmin detection algos inside the
|
|
|
|
# display loop for re-draw efficiency.
|
|
|
|
elif yrange is not None:
|
|
|
|
ylow, yhigh = yrange
|
|
|
|
|
|
|
|
# calculate max, min y values in viewable x-range from data.
|
|
|
|
# Make sure min bars/datums on screen is adhered.
|
|
|
|
else:
|
|
|
|
br = bars_range or chart.bars_range()
|
2022-04-06 15:11:28 +00:00
|
|
|
profiler(f'got bars range: {br}')
|
2022-01-08 22:52:16 +00:00
|
|
|
|
|
|
|
# TODO: maybe should be a method on the
|
|
|
|
# chart widget/item?
|
2022-04-06 15:11:28 +00:00
|
|
|
# if False:
|
2022-04-07 14:58:09 +00:00
|
|
|
# if autoscale_linked_plots:
|
|
|
|
# # avoid recursion by sibling plots
|
|
|
|
# linked = self.linkedsplits
|
|
|
|
# plots = list(linked.subplots.copy().values())
|
|
|
|
# main = linked.chart
|
|
|
|
# if main:
|
|
|
|
# plots.append(main)
|
|
|
|
|
|
|
|
# for chart in plots:
|
|
|
|
# if chart and not chart._static_yrange:
|
|
|
|
# chart.cv._set_yrange(
|
|
|
|
# bars_range=br,
|
|
|
|
# autoscale_linked_plots=False,
|
|
|
|
# )
|
|
|
|
# profiler('autoscaled linked plots')
|
2022-01-08 22:52:16 +00:00
|
|
|
|
|
|
|
if set_range:
|
2022-02-10 03:15:57 +00:00
|
|
|
|
2022-04-06 21:11:15 +00:00
|
|
|
if not yrange:
|
|
|
|
# XXX: only compute the mxmn range
|
|
|
|
# if none is provided as input!
|
|
|
|
yrange = self._maxmin()
|
|
|
|
|
|
|
|
if yrange is None:
|
|
|
|
log.warning(f'No yrange provided for {self.name}!?')
|
|
|
|
return
|
2022-02-04 16:55:53 +00:00
|
|
|
|
|
|
|
ylow, yhigh = yrange
|
2022-01-08 22:52:16 +00:00
|
|
|
|
2022-04-06 15:11:28 +00:00
|
|
|
profiler(f'maxmin(): {yrange}')
|
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
# view margins: stay within a % of the "true range"
|
|
|
|
diff = yhigh - ylow
|
|
|
|
ylow = ylow - (diff * range_margin)
|
|
|
|
yhigh = yhigh + (diff * range_margin)
|
|
|
|
|
|
|
|
# XXX: this often needs to be unset
|
|
|
|
# to get different view modes to operate
|
|
|
|
# correctly!
|
|
|
|
self.setLimits(
|
|
|
|
yMin=ylow,
|
|
|
|
yMax=yhigh,
|
|
|
|
)
|
|
|
|
self.setYRange(ylow, yhigh)
|
2022-04-06 15:11:28 +00:00
|
|
|
profiler(f'set limits: {(ylow, yhigh)}')
|
2022-01-08 22:52:16 +00:00
|
|
|
|
|
|
|
def enable_auto_yrange(
|
2022-04-04 19:58:06 +00:00
|
|
|
self,
|
|
|
|
src_vb: Optional[ChartView] = None,
|
2022-01-08 22:52:16 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
'''
|
|
|
|
Assign callback for rescaling y-axis automatically
|
|
|
|
based on data contents and ``ViewBox`` state.
|
|
|
|
|
|
|
|
'''
|
2022-04-04 19:58:06 +00:00
|
|
|
if src_vb is None:
|
|
|
|
src_vb = self
|
|
|
|
|
2022-04-07 14:58:09 +00:00
|
|
|
# such that when a linked chart changes its range
|
|
|
|
# this local view is also automatically changed and
|
|
|
|
# resized to data.
|
2022-04-04 19:58:06 +00:00
|
|
|
src_vb.sigXRangeChanged.connect(self._set_yrange)
|
2022-03-09 16:01:01 +00:00
|
|
|
|
2022-04-07 14:58:09 +00:00
|
|
|
# splitter(s) resizing
|
|
|
|
src_vb.sigResized.connect(self._set_yrange)
|
|
|
|
|
|
|
|
# mouse wheel doesn't emit XRangeChanged
|
|
|
|
src_vb.sigRangeChangedManually.connect(self._set_yrange)
|
|
|
|
|
2022-03-09 16:01:01 +00:00
|
|
|
# TODO: a smarter way to avoid calling this needlessly?
|
|
|
|
# 2 things i can think of:
|
|
|
|
# - register downsample-able graphics specially and only
|
|
|
|
# iterate those.
|
|
|
|
# - only register this when certain downsampleable graphics are
|
|
|
|
# "added to scene".
|
2022-04-06 15:11:28 +00:00
|
|
|
src_vb.sigRangeChangedManually.connect(
|
|
|
|
self.maybe_downsample_graphics
|
|
|
|
)
|
2022-03-09 16:01:01 +00:00
|
|
|
|
2022-01-08 22:52:16 +00:00
|
|
|
def disable_auto_yrange(
|
|
|
|
self,
|
|
|
|
) -> None:
|
|
|
|
|
2022-04-07 14:58:09 +00:00
|
|
|
# self._chart._static_yrange = 'axis'
|
|
|
|
|
|
|
|
self.sigXRangeChanged.disconnect(
|
|
|
|
self._set_yrange,
|
|
|
|
)
|
|
|
|
self.sigResized.disconnect(
|
|
|
|
self._set_yrange,
|
|
|
|
)
|
|
|
|
self.sigRangeChangedManually.disconnect(
|
|
|
|
self.maybe_downsample_graphics
|
|
|
|
)
|
|
|
|
self.sigRangeChangedManually.disconnect(
|
|
|
|
self._set_yrange,
|
|
|
|
)
|
2022-03-11 19:49:34 +00:00
|
|
|
|
2022-04-06 15:11:28 +00:00
|
|
|
def x_uppx(self) -> float:
|
2022-03-11 19:49:34 +00:00
|
|
|
'''
|
|
|
|
Return the "number of x units" within a single
|
|
|
|
pixel currently being displayed for relevant
|
|
|
|
graphics items which are our children.
|
|
|
|
|
|
|
|
'''
|
2022-04-06 15:11:28 +00:00
|
|
|
graphics = list(self._chart._graphics.values())
|
|
|
|
if not graphics:
|
|
|
|
return 0
|
|
|
|
|
2022-04-06 16:13:05 +00:00
|
|
|
for graphic in graphics:
|
|
|
|
xvec = graphic.pixelVectors()[0]
|
|
|
|
if xvec:
|
|
|
|
return xvec.x()
|
2022-04-06 15:11:28 +00:00
|
|
|
else:
|
|
|
|
return 0
|
2022-04-04 04:35:32 +00:00
|
|
|
|
|
|
|
def maybe_downsample_graphics(self):
|
|
|
|
|
2022-04-06 15:11:28 +00:00
|
|
|
uppx = self.x_uppx()
|
|
|
|
if (
|
|
|
|
# we probably want to drop this once we are "drawing in
|
|
|
|
# view" for downsampled flows..
|
|
|
|
uppx and uppx > 16
|
|
|
|
and self._ic is not None
|
|
|
|
):
|
|
|
|
# don't bother updating since we're zoomed out bigly and
|
|
|
|
# in a pan-interaction, in which case we shouldn't be
|
|
|
|
# doing view-range based rendering (at least not yet).
|
|
|
|
# print(f'{uppx} exiting early!')
|
|
|
|
return
|
|
|
|
|
2022-04-04 21:29:33 +00:00
|
|
|
profiler = pg.debug.Profiler(
|
|
|
|
disabled=not pg_profile_enabled(),
|
|
|
|
gt=3,
|
|
|
|
delayed=True,
|
|
|
|
)
|
|
|
|
|
2022-04-04 04:35:32 +00:00
|
|
|
# TODO: a faster single-loop-iterator way of doing this XD
|
|
|
|
chart = self._chart
|
2022-04-06 15:11:28 +00:00
|
|
|
linked = self.linkedsplits
|
|
|
|
plots = linked.subplots | {chart.name: chart}
|
|
|
|
for chart_name, chart in plots.items():
|
2022-04-07 14:58:09 +00:00
|
|
|
for name, flow in chart._flows.items():
|
|
|
|
graphics = flow.graphics
|
2022-04-06 15:11:28 +00:00
|
|
|
|
|
|
|
use_vr = False
|
|
|
|
if isinstance(graphics, BarItems):
|
|
|
|
use_vr = True
|
|
|
|
|
|
|
|
# pass in no array which will read and render from the last
|
|
|
|
# passed array (normally provided by the display loop.)
|
|
|
|
chart.update_graphics_from_array(
|
|
|
|
name,
|
|
|
|
use_vr=use_vr,
|
|
|
|
profiler=profiler,
|
|
|
|
)
|
|
|
|
profiler(f'range change updated {chart_name}:{name}')
|
2022-04-04 04:35:32 +00:00
|
|
|
|
2022-04-04 21:29:33 +00:00
|
|
|
profiler.finish()
|