2021-06-14 03:47:52 +00:00
|
|
|
# piker: trading gear for hackers
|
2022-01-13 18:38:36 +00:00
|
|
|
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
2021-06-14 03:47:52 +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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Qt main window singletons and stuff.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import time
|
2022-09-09 00:00:50 +00:00
|
|
|
from typing import (
|
|
|
|
Callable,
|
|
|
|
Optional,
|
|
|
|
Union,
|
|
|
|
)
|
2021-06-17 20:53:19 +00:00
|
|
|
import uuid
|
2021-06-14 03:47:52 +00:00
|
|
|
|
2022-01-13 18:38:36 +00:00
|
|
|
from PyQt5 import QtCore
|
2022-10-31 01:11:14 +00:00
|
|
|
from PyQt5.QtWidgets import (
|
|
|
|
QWidget,
|
|
|
|
QMainWindow,
|
|
|
|
QApplication,
|
|
|
|
QLabel,
|
|
|
|
QStatusBar,
|
|
|
|
)
|
2021-06-14 03:47:52 +00:00
|
|
|
|
2022-10-31 01:11:14 +00:00
|
|
|
from PyQt5.QtGui import (
|
|
|
|
QScreen,
|
|
|
|
QCloseEvent,
|
|
|
|
)
|
2021-06-14 03:47:52 +00:00
|
|
|
from ..log import get_logger
|
|
|
|
from ._style import _font_small, hcolor
|
2022-09-09 00:00:50 +00:00
|
|
|
from ._chart import GodWidget
|
2021-06-14 03:47:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class MultiStatus:
|
|
|
|
|
|
|
|
bar: QStatusBar
|
|
|
|
statuses: list[str]
|
|
|
|
|
|
|
|
def __init__(self, bar, statuses) -> None:
|
|
|
|
self.bar = bar
|
|
|
|
self.statuses = statuses
|
2021-06-17 20:53:19 +00:00
|
|
|
self._to_clear: set = set()
|
|
|
|
self._status_groups: dict[str, (set, Callable)] = {}
|
2021-06-14 03:47:52 +00:00
|
|
|
|
|
|
|
def open_status(
|
2021-06-17 20:53:19 +00:00
|
|
|
|
2021-06-14 03:47:52 +00:00
|
|
|
self,
|
|
|
|
msg: str,
|
2021-06-17 20:53:19 +00:00
|
|
|
final_msg: Optional[str] = None,
|
|
|
|
clear_on_next: bool = False,
|
|
|
|
group_key: Optional[Union[bool, str]] = False,
|
|
|
|
|
|
|
|
) -> Union[Callable[..., None], str]:
|
2022-01-13 18:38:36 +00:00
|
|
|
'''
|
|
|
|
Add a status to the status bar and return a close callback which
|
2021-06-14 03:47:52 +00:00
|
|
|
when called will remove the status ``msg``.
|
|
|
|
|
|
|
|
'''
|
2021-06-18 13:37:55 +00:00
|
|
|
for old_msg in self._to_clear:
|
2021-06-17 20:53:19 +00:00
|
|
|
try:
|
2021-06-18 13:37:55 +00:00
|
|
|
self.statuses.remove(old_msg)
|
2021-06-17 20:53:19 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2021-06-14 03:47:52 +00:00
|
|
|
self.statuses.append(msg)
|
|
|
|
|
|
|
|
def remove_msg() -> None:
|
2021-06-17 20:53:19 +00:00
|
|
|
try:
|
|
|
|
self.statuses.remove(msg)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2021-06-18 13:37:55 +00:00
|
|
|
self.render()
|
|
|
|
|
2021-06-17 20:53:19 +00:00
|
|
|
if final_msg is not None:
|
|
|
|
self.statuses.append(final_msg)
|
|
|
|
self.render()
|
|
|
|
self._to_clear.add(final_msg)
|
|
|
|
|
|
|
|
ret = remove_msg
|
|
|
|
|
|
|
|
# create a "status group" such that new `.open_status()`
|
|
|
|
# calls can be made passing in the returned group key.
|
|
|
|
# once all clear callbacks have been called from all statuses
|
|
|
|
# in the group the final status msg to be removed will be the one
|
|
|
|
# the one provided when `group_key=True`, this way you can
|
|
|
|
# create a long living status that completes once all
|
|
|
|
# sub-statuses have finished.
|
|
|
|
if group_key is True:
|
|
|
|
if clear_on_next:
|
|
|
|
ValueError("Can't create group status and clear it on next?")
|
|
|
|
|
|
|
|
# generate a key for a new "status group"
|
|
|
|
new_group_key = str(uuid.uuid4())
|
|
|
|
|
|
|
|
def pop_group_and_clear():
|
|
|
|
|
|
|
|
subs, final_clear = self._status_groups.pop(new_group_key)
|
|
|
|
assert not subs
|
|
|
|
return remove_msg()
|
|
|
|
|
|
|
|
self._status_groups[new_group_key] = (set(), pop_group_and_clear)
|
|
|
|
ret = new_group_key
|
|
|
|
|
|
|
|
elif group_key:
|
|
|
|
|
|
|
|
def pop_from_group_and_maybe_clear_group():
|
|
|
|
# remove the message for this sub-status
|
|
|
|
remove_msg()
|
|
|
|
|
|
|
|
# check to see if all other substatuses have cleared
|
|
|
|
group_tup = self._status_groups.get(group_key)
|
|
|
|
|
|
|
|
if group_tup:
|
|
|
|
subs, group_clear = group_tup
|
|
|
|
try:
|
|
|
|
subs.remove(msg)
|
|
|
|
except KeyError:
|
|
|
|
raise KeyError(f'no msg {msg} for group {group_key}!?')
|
|
|
|
|
|
|
|
if not subs:
|
|
|
|
group_clear()
|
|
|
|
|
2021-08-26 12:58:55 +00:00
|
|
|
group = self._status_groups.get(group_key)
|
|
|
|
if group:
|
|
|
|
group[0].add(msg)
|
|
|
|
ret = pop_from_group_and_maybe_clear_group
|
2021-06-17 20:53:19 +00:00
|
|
|
|
2021-06-18 13:37:55 +00:00
|
|
|
self.render()
|
|
|
|
|
2021-06-17 20:53:19 +00:00
|
|
|
if clear_on_next:
|
|
|
|
self._to_clear.add(msg)
|
2021-06-14 03:47:52 +00:00
|
|
|
|
2021-06-17 20:53:19 +00:00
|
|
|
return ret
|
2021-06-14 03:47:52 +00:00
|
|
|
|
|
|
|
def render(self) -> None:
|
2022-01-13 18:38:36 +00:00
|
|
|
'''
|
|
|
|
Display all open statuses to bar.
|
2021-06-18 13:37:55 +00:00
|
|
|
|
|
|
|
'''
|
2021-06-14 03:47:52 +00:00
|
|
|
if self.statuses:
|
|
|
|
self.bar.showMessage(f'{" ".join(self.statuses)}')
|
|
|
|
else:
|
|
|
|
self.bar.clearMessage()
|
|
|
|
|
|
|
|
|
2022-10-31 01:11:14 +00:00
|
|
|
class MainWindow(QMainWindow):
|
2021-06-14 03:47:52 +00:00
|
|
|
|
2021-09-05 17:19:27 +00:00
|
|
|
# XXX: for tiling wms this should scale
|
|
|
|
# with the alloted window size.
|
|
|
|
# TODO: detect for tiling and if untrue set some size?
|
2022-09-10 02:07:51 +00:00
|
|
|
# size = (300, 500)
|
2022-09-09 00:00:50 +00:00
|
|
|
godwidget: GodWidget
|
2021-09-05 17:19:27 +00:00
|
|
|
|
2021-06-14 03:47:52 +00:00
|
|
|
title = 'piker chart (ur symbol is loading bby)'
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
2021-09-05 17:19:27 +00:00
|
|
|
# self.setMinimumSize(*self.size)
|
2021-06-14 03:47:52 +00:00
|
|
|
self.setWindowTitle(self.title)
|
|
|
|
|
2022-09-09 00:00:50 +00:00
|
|
|
# set by runtime after `trio` is engaged.
|
|
|
|
self.godwidget: Optional[GodWidget] = None
|
|
|
|
|
2021-06-14 03:47:52 +00:00
|
|
|
self._status_bar: QStatusBar = None
|
|
|
|
self._status_label: QLabel = None
|
2021-09-19 22:34:17 +00:00
|
|
|
self._size: Optional[tuple[int, int]] = None
|
2022-02-10 19:35:28 +00:00
|
|
|
|
2021-06-14 03:47:52 +00:00
|
|
|
@property
|
2022-10-31 01:11:14 +00:00
|
|
|
def mode_label(self) -> QLabel:
|
2021-06-14 03:47:52 +00:00
|
|
|
|
|
|
|
# init mode label
|
|
|
|
if not self._status_label:
|
|
|
|
|
2022-10-31 01:11:14 +00:00
|
|
|
self._status_label = label = QLabel()
|
2021-06-14 03:47:52 +00:00
|
|
|
label.setStyleSheet(
|
2021-07-27 10:12:19 +00:00
|
|
|
f"""QLabel {{
|
|
|
|
color : {hcolor('gunmetal')};
|
|
|
|
}}
|
|
|
|
"""
|
|
|
|
# font-size : {font_size}px;
|
2021-06-14 03:47:52 +00:00
|
|
|
)
|
|
|
|
label.setTextFormat(3) # markdown
|
|
|
|
label.setFont(_font_small.font)
|
|
|
|
label.setMargin(2)
|
|
|
|
label.setAlignment(
|
|
|
|
QtCore.Qt.AlignVCenter
|
|
|
|
| QtCore.Qt.AlignRight
|
|
|
|
)
|
|
|
|
self.statusBar().addPermanentWidget(label)
|
|
|
|
label.show()
|
|
|
|
|
|
|
|
return self._status_label
|
|
|
|
|
|
|
|
def closeEvent(
|
|
|
|
self,
|
2022-10-31 01:11:14 +00:00
|
|
|
event: QCloseEvent,
|
2021-06-14 03:47:52 +00:00
|
|
|
|
2021-08-03 13:45:33 +00:00
|
|
|
) -> None:
|
|
|
|
'''Cancel the root actor asap.
|
2021-07-24 20:06:05 +00:00
|
|
|
|
2021-08-03 13:45:33 +00:00
|
|
|
'''
|
2021-06-14 03:47:52 +00:00
|
|
|
# raising KBI seems to get intercepted by by Qt so just use the system.
|
|
|
|
os.kill(os.getpid(), signal.SIGINT)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def status_bar(self) -> QStatusBar:
|
|
|
|
|
|
|
|
# style and cached the status bar on first access
|
|
|
|
if not self._status_bar:
|
|
|
|
|
|
|
|
sb = self.statusBar()
|
|
|
|
sb.setStyleSheet((
|
|
|
|
f"color : {hcolor('gunmetal')};"
|
|
|
|
f"background : {hcolor('default_dark')};"
|
|
|
|
f"font-size : {_font_small.px_size}px;"
|
|
|
|
"padding : 0px;"
|
|
|
|
# "min-height : 19px;"
|
|
|
|
# "qproperty-alignment: AlignVCenter;"
|
|
|
|
))
|
|
|
|
self.setStatusBar(sb)
|
|
|
|
self._status_bar = MultiStatus(sb, [])
|
|
|
|
|
|
|
|
return self._status_bar
|
|
|
|
|
2021-07-24 20:06:05 +00:00
|
|
|
def set_mode_name(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
self.mode_label.setText(f'mode:{name}')
|
|
|
|
|
2021-06-14 03:47:52 +00:00
|
|
|
def on_focus_change(
|
|
|
|
self,
|
2021-07-24 20:06:05 +00:00
|
|
|
|
2022-10-31 01:11:14 +00:00
|
|
|
last: QWidget,
|
|
|
|
current: QWidget,
|
2021-07-24 20:06:05 +00:00
|
|
|
|
2021-06-14 03:47:52 +00:00
|
|
|
) -> None:
|
|
|
|
|
2021-08-03 13:45:33 +00:00
|
|
|
log.info(f'widget focus changed from {last} -> {current}')
|
2021-06-14 03:47:52 +00:00
|
|
|
|
2021-08-03 13:45:33 +00:00
|
|
|
if current is not None:
|
2021-06-14 03:47:52 +00:00
|
|
|
# cursor left window?
|
2021-08-03 13:45:33 +00:00
|
|
|
name = getattr(current, 'mode_name', '')
|
2021-07-24 20:06:05 +00:00
|
|
|
self.set_mode_name(name)
|
2021-06-14 03:47:52 +00:00
|
|
|
|
2022-10-31 01:11:14 +00:00
|
|
|
def current_screen(self) -> QScreen:
|
2022-09-09 00:00:50 +00:00
|
|
|
'''
|
|
|
|
Get a frickin screen (if we can, gawd).
|
2021-06-14 03:47:52 +00:00
|
|
|
|
2022-09-09 00:00:50 +00:00
|
|
|
'''
|
2022-10-31 01:11:14 +00:00
|
|
|
app = QApplication.instance()
|
2021-06-14 03:47:52 +00:00
|
|
|
|
|
|
|
for _ in range(3):
|
|
|
|
screen = app.screenAt(self.pos())
|
2021-07-22 15:43:18 +00:00
|
|
|
log.debug('trying to access QScreen...')
|
2021-06-14 03:47:52 +00:00
|
|
|
if screen is None:
|
|
|
|
time.sleep(0.5)
|
|
|
|
continue
|
|
|
|
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if screen is None:
|
|
|
|
# try for the first one we can find
|
|
|
|
screen = app.screens()[0]
|
|
|
|
|
|
|
|
assert screen, "Wow Qt is dumb as shit and has no screen..."
|
|
|
|
return screen
|
|
|
|
|
2021-09-16 20:33:48 +00:00
|
|
|
def configure_to_desktop(
|
|
|
|
self,
|
2021-09-19 22:34:17 +00:00
|
|
|
size: Optional[tuple[int, int]] = None,
|
2022-02-10 19:20:15 +00:00
|
|
|
|
2021-09-16 20:33:48 +00:00
|
|
|
) -> None:
|
2022-02-10 19:20:15 +00:00
|
|
|
'''
|
|
|
|
Explicitly size the window dimensions (for stacked window
|
|
|
|
managers).
|
|
|
|
|
|
|
|
For tina systems (like windoze) try to do a sane window size on
|
|
|
|
startup.
|
|
|
|
|
|
|
|
'''
|
2021-09-16 20:33:48 +00:00
|
|
|
# https://stackoverflow.com/a/18975846
|
2021-09-19 22:34:17 +00:00
|
|
|
if not size and not self._size:
|
2022-10-31 01:11:14 +00:00
|
|
|
# app = QApplication.instance()
|
2021-09-19 22:34:17 +00:00
|
|
|
geo = self.current_screen().geometry()
|
|
|
|
h, w = geo.height(), geo.width()
|
|
|
|
# use approx 1/3 of the area of the screen by default
|
|
|
|
self._size = round(w * .666), round(h * .666)
|
2022-02-10 19:20:15 +00:00
|
|
|
|
2021-09-19 22:34:17 +00:00
|
|
|
self.resize(*size or self._size)
|
2021-09-16 20:33:48 +00:00
|
|
|
|
2022-09-09 00:00:50 +00:00
|
|
|
def resizeEvent(self, event: QtCore.QEvent) -> None:
|
2022-09-10 02:07:51 +00:00
|
|
|
if (
|
|
|
|
# event.spontaneous()
|
|
|
|
event.oldSize().height == event.size().height
|
|
|
|
):
|
|
|
|
event.ignore()
|
|
|
|
return
|
|
|
|
|
|
|
|
# XXX: uncomment for debugging..
|
|
|
|
# attrs = {}
|
|
|
|
# for key in dir(event):
|
|
|
|
# if key == '__dir__':
|
|
|
|
# continue
|
|
|
|
# attr = getattr(event, key)
|
|
|
|
# try:
|
|
|
|
# attrs[key] = attr()
|
|
|
|
# except TypeError:
|
|
|
|
# attrs[key] = attr
|
|
|
|
|
|
|
|
# from pprint import pformat
|
|
|
|
# print(
|
|
|
|
# f'{pformat(attrs)}\n'
|
|
|
|
# f'WINDOW RESIZE: {self.size()}\n\n'
|
|
|
|
# )
|
2022-09-09 00:00:50 +00:00
|
|
|
self.godwidget.on_win_resize(event)
|
2022-09-10 02:07:51 +00:00
|
|
|
event.accept()
|
2022-09-09 00:00:50 +00:00
|
|
|
|
2021-09-16 20:33:48 +00:00
|
|
|
|
2021-06-14 03:47:52 +00:00
|
|
|
# singleton app per actor
|
2022-10-31 01:11:14 +00:00
|
|
|
_qt_win: QMainWindow = None
|
2021-06-14 03:47:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main_window() -> MainWindow:
|
|
|
|
'Return the actor-global Qt window.'
|
|
|
|
|
|
|
|
global _qt_win
|
|
|
|
assert _qt_win
|
|
|
|
return _qt_win
|