Skip to content

Commit

Permalink
some GUI experimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed May 16, 2024
1 parent 1c2965b commit 353ab27
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 34 deletions.
78 changes: 58 additions & 20 deletions tmtccmd/gui/buttons.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from dataclasses import dataclass
from typing import Callable, Optional

from PyQt6.QtCore import QThreadPool, QRunnable
Expand All @@ -16,7 +17,7 @@
from tmtccmd.gui.defs import FrontendState
from tmtccmd.gui.worker import FrontendWorker

LOGGER = logging.getLogger(__name__)
_LOGGER = logging.getLogger(__name__)


class ButtonArgs:
Expand Down Expand Up @@ -64,7 +65,7 @@ def _button_op(self):
self._disconnect_button_pressed()

def _connect_button_pressed(self):
LOGGER.info("Opening COM Interface")
_LOGGER.info("Opening COM Interface")
self._com_if_needs_switch = False
# Build and assign new communication interface
if self._args.state.current_com_if != self._args.state.last_com_if:
Expand Down Expand Up @@ -100,7 +101,7 @@ def _connect_button_finished(self):
and self._bttn_params.tm_listener_bttn is not None
):
self._bttn_params.tm_listener_bttn.click()
LOGGER.info("Connected")
_LOGGER.info("Connected")

def _disconnect_button_pressed(self):
self.button.setEnabled(False)
Expand All @@ -116,22 +117,60 @@ def _disconnect_button_finished(self):
self.button.setStyleSheet(CONNECT_BTTN_STYLE)
self.button.setText("Connect")
self._bttn_params.disconnect_cb()
LOGGER.info("Disconnected")
_LOGGER.info("Disconnected")


class TmButtonWrapper:
def __init__(self, button: QPushButton, args: ButtonArgs, conn_button: QPushButton):
@dataclass
class TmButtonModel:
style_sheet: str
text: str
enabled: bool

@classmethod
def disconnected(cls):
return cls(DISCONNECT_BTTN_STYLE, "Start TM listener", False)

def set_connected(self):
self.style_sheet = DISCONNECT_BTTN_STYLE
self.text = "Stop TM listener"
self.enabled = True

def set_disconnected(self):
self.style_sheet = CONNECT_BTTN_STYLE
self.text = "Start TM listener"
self.enabled = False


class TmButtonView:
def __init__(self, button: QPushButton):
self.button = button

def render(self, model: TmButtonModel):
self.button.setText(model.text)
self.button.setEnabled(model.enabled)
self.button.setStyleSheet(model.style_sheet)


class TmButtonController:
def __init__(
self,
view: TmButtonView,
args: ButtonArgs,
conn_button: QPushButton,
):
self.view = view
self.model = TmButtonModel.disconnected()
self.view.render(self.model)
self.args = args
self.worker: Optional[QRunnable] = None
self._listening = False
self._next_listener_state = False
self.button.setStyleSheet(CONNECT_BTTN_STYLE)
self.button.setText("Start TM listener")
self.button.setEnabled(False)
self.button.clicked.connect(self.button_op)
self.view.button.clicked.connect(self.button_op)
self._conn_button = conn_button

def render_view(self):
self.view.render(self.model)

def is_listening(self):
return self._listening

Expand All @@ -144,7 +183,7 @@ def abort_thread(self):
self.worker.signals.abort.emit(None)

def start_listener(self):
LOGGER.info("Starting TM listener")
_LOGGER.info("Starting TM listener")
self.worker = FrontendWorker(
LocalArgs(WorkerOperationsCode.LISTEN_FOR_TM, 0.4), self.args.shared
)
Expand All @@ -154,12 +193,12 @@ def start_listener(self):
self.button_op_done()

def stop_listener(self):
LOGGER.info("Stopping TM listener")
_LOGGER.info("Stopping TM listener")
self._next_listener_state = False
if self.worker is not None:
self.worker.signals.finished.connect(self.button_op_done)
self.worker.signals.stop.emit(None)
self.button.setEnabled(False)
self.view.button.setEnabled(False)

def button_op(self):
if not self._listening:
Expand All @@ -169,17 +208,16 @@ def button_op(self):

def button_op_done(self):
if self._next_listener_state:
self.button.setStyleSheet(DISCONNECT_BTTN_STYLE)
self.button.setText("Stop TM listener")
self.model.set_connected()
self._listening = True
self.button.setEnabled(True)
else:
self.button.setStyleSheet(CONNECT_BTTN_STYLE)
self.model.style_sheet = CONNECT_BTTN_STYLE
if not self.args.shared.com_if_ref_tracker.is_used():
self._conn_button.setEnabled(True)
self.button.setText("Start TM listener")
self.model.text = "Start TM listener"
self.model.set_disconnected()
self._listening = False
self.button.setEnabled(True)
self.view.render(self.model)


class SendButtonWrapper:
Expand All @@ -195,7 +233,7 @@ def __init__(self, button: QPushButton, args: ButtonArgs, conn_button: QPushButt

def _button_op(self):
if self.debug_mode:
LOGGER.info("Send command button pressed.")
_LOGGER.info("Send command button pressed.")
self.button.setDisabled(True)
if self._args.state.current_cmd_path is None:
return
Expand Down
21 changes: 12 additions & 9 deletions tmtccmd/gui/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
ConnectButtonParams,
ButtonArgs,
SendButtonWrapper,
TmButtonWrapper,
TmButtonController,
TmButtonView,
ConnectButtonWrapper,
)
from tmtccmd.gui.cmd_select import CommandPathSelectWidget
Expand Down Expand Up @@ -137,12 +138,12 @@ def _start_ui(self):
grid.addWidget(self.__send_bttn_wrapper.button, row, 0, 1, 2)
row += 1

self.__tm_button_wrapper = TmButtonWrapper(
button=tm_listener_button,
self.__tm_button_ctrl = TmButtonController(
view=TmButtonView(tm_listener_button),
args=button_args,
conn_button=self.__connect_button_wrapper.button,
)
grid.addWidget(self.__tm_button_wrapper.button, row, 0, 1, 2)
grid.addWidget(self.__tm_button_ctrl.view.button, row, 0, 1, 2)
row += 1
self.show()
# self.destroyed.connect(self.__tm_button_wrapper.stop_thread)
Expand All @@ -156,14 +157,14 @@ def set_gui_logo(self, logo_total_path: str):
def closeEvent(self, event):
try:
pass
if self.__tm_button_wrapper.is_listening():
if self.__tm_button_ctrl.is_listening():
LOGGER.warning("TM listener still active. Stopping it first..")
self.__tm_button_wrapper.stop_thread()
self.__tm_button_ctrl.stop_thread()
event.ignore()
else:
pass
except KeyboardInterrupt:
self.__tm_button_wrapper.abort_thread()
self.__tm_button_ctrl.abort_thread()

def _create_menu_bar(self):
menu_bar = self.menuBar()
Expand Down Expand Up @@ -269,11 +270,13 @@ def __enable_conn_bttn(self):

def __connected_com_if_cb(self):
self.__send_bttn_wrapper.button.setEnabled(True)
self.__tm_button_wrapper.button.setEnabled(True)
self.__tm_button_ctrl.model.set_connected()
self.__tm_button_ctrl.render_view()

def __disconnect_com_if_cb(self):
self.__send_bttn_wrapper.button.setDisabled(True)
self.__tm_button_wrapper.button.setDisabled(True)
self.__tm_button_ctrl.model.set_disconnected()
self.__tm_button_ctrl.render_view()

def _set_up_cmd_path_ui(self, grid: QGridLayout, row: int):
font = QFont()
Expand Down
10 changes: 5 additions & 5 deletions tmtccmd/gui/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from tmtccmd.gui.defs import LocalArgs, SharedArgs, WorkerOperationsCode
from tmtccmd.tmtc.procedure import TreeCommandingProcedure

LOGGER = logging.getLogger(__name__)
_LOGGER = logging.getLogger(__name__)


class WorkerSignalWrapper(QObject):
Expand Down Expand Up @@ -56,7 +56,7 @@ def spawn_for_cmd_path(

def __setup(self, op_code: WorkerOperationsCode) -> bool:
if op_code == WorkerOperationsCode.OPEN_COM_IF:
LOGGER.info("Switching COM Interface")
_LOGGER.info("Switching COM Interface")

assert isinstance(self._locals.op_args, tuple)
assert isinstance(self._locals.op_args[0], bool)
Expand All @@ -66,12 +66,11 @@ def __setup(self, op_code: WorkerOperationsCode) -> bool:
new_com_if = self._locals.op_args[2].get_communication_interface(
com_if_key=self._locals.op_args[1]
)
# self._args.state.last_com_if = self._args.state.current_com_if
set_success = False
if new_com_if is not None and self._locals.op_args[0]:
set_success = self._shared.backend.try_set_com_if(new_com_if)
if not set_success:
LOGGER.warning(
_LOGGER.warning(
f"Could not set new communication interface {new_com_if}"
)
if self._shared.backend.com_if_active():
Expand All @@ -86,6 +85,7 @@ def __setup(self, op_code: WorkerOperationsCode) -> bool:
elif self._shared.com_if_ref_tracker.is_used():
self._failure_with_info("Can not close COM interface which is used")
else:
_LOGGER.info("Closing COM Interface")
self._shared.backend.close_com_if()
self._finish_success()
return False
Expand Down Expand Up @@ -148,7 +148,7 @@ def __loop(self, op_code: WorkerOperationsCode) -> bool:
return False
else:
# This must be a programming error
LOGGER.error(f"Unknown worker operation code {self._locals.op_code}")
_LOGGER.error(f"Unknown worker operation code {self._locals.op_code}")
return True

@pyqtSlot()
Expand Down

0 comments on commit 353ab27

Please sign in to comment.