class SpreadLogMonitor(QtWidgets.QTextEdit): """ Monitor for log data. """ signal = QtCore.pyqtSignal(Event) def __init__(self, main_engine: MainEngine, event_engine: EventEngine): """""" super().__init__() self.main_engine = main_engine self.event_engine = event_engine self.init_ui() self.register_event() def init_ui(self): """""" self.setReadOnly(True) def register_event(self): """""" self.signal.connect(self.process_log_event) self.event_engine.register(EVENT_SPREAD_LOG, self.signal.emit) def process_log_event(self, event: Event): """""" log = event.data msg = f"{log.time.strftime('%H:%M:%S')}\t{log.msg}" self.append(msg)
class SpreadStrategyMonitor(QtWidgets.QWidget): """""" signal_strategy = QtCore.pyqtSignal(Event) def __init__(self, spread_engine: SpreadEngine): super().__init__() self.strategy_engine = spread_engine.strategy_engine self.main_engine = spread_engine.main_engine self.event_engine = spread_engine.event_engine self.managers = {} self.init_ui() self.register_event() def init_ui(self): """""" self.scroll_layout = QtWidgets.QVBoxLayout() self.scroll_layout.addStretch() scroll_widget = QtWidgets.QWidget() scroll_widget.setLayout(self.scroll_layout) scroll_area = QtWidgets.QScrollArea() scroll_area.setWidgetResizable(True) scroll_area.setWidget(scroll_widget) vbox = QtWidgets.QVBoxLayout() vbox.addWidget(scroll_area) self.setLayout(vbox) def register_event(self): """""" self.signal_strategy.connect(self.process_strategy_event) self.event_engine.register(EVENT_SPREAD_STRATEGY, self.signal_strategy.emit) def process_strategy_event(self, event): """ Update strategy status onto its monitor. """ data = event.data strategy_name = data["strategy_name"] if strategy_name in self.managers: manager = self.managers[strategy_name] manager.update_data(data) else: manager = SpreadStrategyWidget(self, self.strategy_engine, data) self.scroll_layout.insertWidget(0, manager) self.managers[strategy_name] = manager def remove_strategy(self, strategy_name): """""" manager = self.managers.pop(strategy_name) manager.deleteLater()