From 4eef30595d447348af2831fbc40d47b7f5e6bce8 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom <rmc@carlstrom.com> Date: Wed, 20 Sep 2023 22:41:31 -0400 Subject: [PATCH 1/6] atexit purge --- src/surface/gui/gui/app.py | 27 ++++++++++------------- src/surface/gui/gui/event_nodes/server.py | 2 +- src/surface/gui/gui/operator_app.py | 3 ++- src/surface/gui/gui/pilot_app.py | 3 ++- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/surface/gui/gui/app.py b/src/surface/gui/gui/app.py index d6b9aef6..dde6a79b 100644 --- a/src/surface/gui/gui/app.py +++ b/src/surface/gui/gui/app.py @@ -1,4 +1,3 @@ -import atexit import signal import sys @@ -8,38 +7,36 @@ from rclpy.node import Node -class App(Node, QWidget): +class App(QWidget): """Main app window.""" def __init__(self, node_name: str): self.app: QApplication = QApplication(sys.argv) rclpy.init() + super().__init__() + self.node = Node(node_name, parameter_overrides=[]) - super().__init__( - node_name=node_name, - parameter_overrides=[]) - super(QWidget, self).__init__() - - self.declare_parameter('theme', '') + self.node.declare_parameter('theme', '') self.resize(1850, 720) - def kill(): - self.destroy_node() - rclpy.shutdown() + def __enter__(self): + return self - atexit.register(kill) + def __exit__(self, *_): + rclpy.shutdown() def run_gui(self): # Kills with Control + C signal.signal(signal.SIGINT, signal.SIG_DFL) - if self.get_parameter('theme').get_parameter_value().string_value == "dark": + if self.node.get_parameter('theme').get_parameter_value().string_value == "dark": qdarktheme.setup_theme() - elif self.get_parameter('theme').get_parameter_value().string_value == "watermelon": + elif self.node.get_parameter('theme').get_parameter_value().string_value == "watermelon": # UGLY But WORKS self.app.setStyleSheet("QWidget { background-color: green; color: pink; }") else: qdarktheme.setup_theme("light") - + # Clean up resources + self.node.destroy_node() self.show() sys.exit(self.app.exec()) diff --git a/src/surface/gui/gui/event_nodes/server.py b/src/surface/gui/gui/event_nodes/server.py index 189fdce3..d3c69b22 100644 --- a/src/surface/gui/gui/event_nodes/server.py +++ b/src/surface/gui/gui/event_nodes/server.py @@ -12,7 +12,7 @@ class GUIEventServer(Node): """Multithreaded server for processing server requests to update GUI.""" def __init__(self, srv_type: SrvType, topic: str, - callback: Callable[[SrvTypeRequest, SrvTypeResponse], SrvTypeResponse]): + callback: Callable[[SrvTypeRequest, SrvTypeResponse], SrvTypeResponse]): """ Initialize this server with a CALLBACK for processing requests. diff --git a/src/surface/gui/gui/operator_app.py b/src/surface/gui/gui/operator_app.py index a66b18e7..78be04d7 100644 --- a/src/surface/gui/gui/operator_app.py +++ b/src/surface/gui/gui/operator_app.py @@ -39,4 +39,5 @@ def __init__(self): def run_gui_operator(): - OperatorApp().run_gui() + with OperatorApp() as app: + app.run_gui() diff --git a/src/surface/gui/gui/pilot_app.py b/src/surface/gui/gui/pilot_app.py index 8c077026..37a8a6b2 100644 --- a/src/surface/gui/gui/pilot_app.py +++ b/src/surface/gui/gui/pilot_app.py @@ -29,4 +29,5 @@ def __init__(self): def run_gui_pilot(): - PilotApp().run_gui() + with PilotApp() as app: + app.run_gui() From b737f405c51b88f0fa9058436dee59b3e6ce7f0c Mon Sep 17 00:00:00 2001 From: Michael Carlstrom <rmc@carlstrom.com> Date: Wed, 20 Sep 2023 22:45:31 -0400 Subject: [PATCH 2/6] missed an indent --- src/surface/gui/gui/event_nodes/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/surface/gui/gui/event_nodes/server.py b/src/surface/gui/gui/event_nodes/server.py index d3c69b22..189fdce3 100644 --- a/src/surface/gui/gui/event_nodes/server.py +++ b/src/surface/gui/gui/event_nodes/server.py @@ -12,7 +12,7 @@ class GUIEventServer(Node): """Multithreaded server for processing server requests to update GUI.""" def __init__(self, srv_type: SrvType, topic: str, - callback: Callable[[SrvTypeRequest, SrvTypeResponse], SrvTypeResponse]): + callback: Callable[[SrvTypeRequest, SrvTypeResponse], SrvTypeResponse]): """ Initialize this server with a CALLBACK for processing requests. From c11f0fc9b8977e9e6759d24c15cbc3fee7e43e50 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom <rmc@carlstrom.com> Date: Thu, 21 Sep 2023 09:46:38 -0400 Subject: [PATCH 3/6] switch from module to widgets --- src/surface/gui/gui/operator_app.py | 8 ++++---- src/surface/gui/gui/pilot_app.py | 4 ++-- src/surface/gui/gui/{modules => widgets}/__init__.py | 0 src/surface/gui/gui/{modules => widgets}/arm.py | 0 src/surface/gui/gui/{modules => widgets}/logger.py | 0 src/surface/gui/gui/{modules => widgets}/seagrass.py | 2 +- src/surface/gui/gui/{modules => widgets}/task_selector.py | 0 src/surface/gui/gui/{modules => widgets}/timer.py | 0 src/surface/gui/gui/{modules => widgets}/video_widget.py | 0 src/surface/gui/setup.py | 2 +- 10 files changed, 8 insertions(+), 8 deletions(-) rename src/surface/gui/gui/{modules => widgets}/__init__.py (100%) rename src/surface/gui/gui/{modules => widgets}/arm.py (100%) rename src/surface/gui/gui/{modules => widgets}/logger.py (100%) rename src/surface/gui/gui/{modules => widgets}/seagrass.py (99%) rename src/surface/gui/gui/{modules => widgets}/task_selector.py (100%) rename src/surface/gui/gui/{modules => widgets}/timer.py (100%) rename src/surface/gui/gui/{modules => widgets}/video_widget.py (100%) diff --git a/src/surface/gui/gui/operator_app.py b/src/surface/gui/gui/operator_app.py index 78be04d7..8dd68ed4 100644 --- a/src/surface/gui/gui/operator_app.py +++ b/src/surface/gui/gui/operator_app.py @@ -1,8 +1,8 @@ from gui.app import App -from gui.modules.logger import Logger -from gui.modules.seagrass import SeagrassWidget -from gui.modules.task_selector import TaskSelector -from gui.modules.timer import Timer +from gui.widgets.logger import Logger +from gui.widgets.seagrass import SeagrassWidget +from gui.widgets.task_selector import TaskSelector +from gui.widgets.timer import Timer from PyQt6.QtWidgets import QGridLayout, QTabWidget, QWidget diff --git a/src/surface/gui/gui/pilot_app.py b/src/surface/gui/gui/pilot_app.py index 37a8a6b2..03fe39ea 100644 --- a/src/surface/gui/gui/pilot_app.py +++ b/src/surface/gui/gui/pilot_app.py @@ -1,6 +1,6 @@ from gui.app import App -from gui.modules.arm import Arm -from gui.modules.video_widget import SwitchableVideoWidget +from gui.widgets.arm import Arm +from gui.widgets.video_widget import SwitchableVideoWidget from PyQt6.QtCore import Qt from PyQt6.QtWidgets import QHBoxLayout diff --git a/src/surface/gui/gui/modules/__init__.py b/src/surface/gui/gui/widgets/__init__.py similarity index 100% rename from src/surface/gui/gui/modules/__init__.py rename to src/surface/gui/gui/widgets/__init__.py diff --git a/src/surface/gui/gui/modules/arm.py b/src/surface/gui/gui/widgets/arm.py similarity index 100% rename from src/surface/gui/gui/modules/arm.py rename to src/surface/gui/gui/widgets/arm.py diff --git a/src/surface/gui/gui/modules/logger.py b/src/surface/gui/gui/widgets/logger.py similarity index 100% rename from src/surface/gui/gui/modules/logger.py rename to src/surface/gui/gui/widgets/logger.py diff --git a/src/surface/gui/gui/modules/seagrass.py b/src/surface/gui/gui/widgets/seagrass.py similarity index 99% rename from src/surface/gui/gui/modules/seagrass.py rename to src/surface/gui/gui/widgets/seagrass.py index b912ed0c..70fad501 100644 --- a/src/surface/gui/gui/modules/seagrass.py +++ b/src/surface/gui/gui/widgets/seagrass.py @@ -1,6 +1,6 @@ from typing import Callable, Optional -from gui.modules.video_widget import PauseableVideoWidget +from gui.widgets.video_widget import PauseableVideoWidget from PyQt6.QtCore import Qt from PyQt6.QtWidgets import (QFrame, QGridLayout, QHBoxLayout, QLabel, QPushButton, QVBoxLayout, QWidget) diff --git a/src/surface/gui/gui/modules/task_selector.py b/src/surface/gui/gui/widgets/task_selector.py similarity index 100% rename from src/surface/gui/gui/modules/task_selector.py rename to src/surface/gui/gui/widgets/task_selector.py diff --git a/src/surface/gui/gui/modules/timer.py b/src/surface/gui/gui/widgets/timer.py similarity index 100% rename from src/surface/gui/gui/modules/timer.py rename to src/surface/gui/gui/widgets/timer.py diff --git a/src/surface/gui/gui/modules/video_widget.py b/src/surface/gui/gui/widgets/video_widget.py similarity index 100% rename from src/surface/gui/gui/modules/video_widget.py rename to src/surface/gui/gui/widgets/video_widget.py diff --git a/src/surface/gui/setup.py b/src/surface/gui/setup.py index ae6ca028..40aa01de 100644 --- a/src/surface/gui/setup.py +++ b/src/surface/gui/setup.py @@ -7,7 +7,7 @@ setup( name=package_name, version='0.0.0', - packages=[package_name, os.path.join(package_name, 'modules'), + packages=[package_name, os.path.join(package_name, 'widgets'), os.path.join(package_name, 'event_nodes')], data_files=[ ('share/ament_index/resource_index/packages', From fa1ba37e6328cb563590cbae2a2302e33fd2f19a Mon Sep 17 00:00:00 2001 From: Michael Carlstrom <rmc@carlstrom.com> Date: Sat, 23 Sep 2023 16:05:35 -0400 Subject: [PATCH 4/6] bring back atexit --- src/surface/gui/gui/app.py | 9 +++------ src/surface/gui/gui/operator_app.py | 3 +-- src/surface/gui/gui/pilot_app.py | 3 +-- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/surface/gui/gui/app.py b/src/surface/gui/gui/app.py index dde6a79b..686ed4f9 100644 --- a/src/surface/gui/gui/app.py +++ b/src/surface/gui/gui/app.py @@ -1,3 +1,4 @@ +import atexit import signal import sys @@ -19,11 +20,7 @@ def __init__(self, node_name: str): self.node.declare_parameter('theme', '') self.resize(1850, 720) - def __enter__(self): - return self - - def __exit__(self, *_): - rclpy.shutdown() + atexit.register(rclpy.shutdown) def run_gui(self): # Kills with Control + C @@ -39,4 +36,4 @@ def run_gui(self): # Clean up resources self.node.destroy_node() self.show() - sys.exit(self.app.exec()) + self.app.exec() diff --git a/src/surface/gui/gui/operator_app.py b/src/surface/gui/gui/operator_app.py index 8dd68ed4..883b8e7a 100644 --- a/src/surface/gui/gui/operator_app.py +++ b/src/surface/gui/gui/operator_app.py @@ -39,5 +39,4 @@ def __init__(self): def run_gui_operator(): - with OperatorApp() as app: - app.run_gui() + OperatorApp().run_gui() diff --git a/src/surface/gui/gui/pilot_app.py b/src/surface/gui/gui/pilot_app.py index 03fe39ea..92723d26 100644 --- a/src/surface/gui/gui/pilot_app.py +++ b/src/surface/gui/gui/pilot_app.py @@ -29,5 +29,4 @@ def __init__(self): def run_gui_pilot(): - with PilotApp() as app: - app.run_gui() + PilotApp().run_gui() From adf5aaa175e981f2446afb89ed96dcefb03c0bba Mon Sep 17 00:00:00 2001 From: Benjamin Poulin <90342856+benjaminwp18@users.noreply.github.com> Date: Sat, 23 Sep 2023 16:30:32 -0400 Subject: [PATCH 5/6] Edit node destroy comment --- src/surface/gui/gui/app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/surface/gui/gui/app.py b/src/surface/gui/gui/app.py index 686ed4f9..af277520 100644 --- a/src/surface/gui/gui/app.py +++ b/src/surface/gui/gui/app.py @@ -33,7 +33,8 @@ def run_gui(self): self.app.setStyleSheet("QWidget { background-color: green; color: pink; }") else: qdarktheme.setup_theme("light") - # Clean up resources + # Delete node now that we've used it to get params self.node.destroy_node() + self.show() self.app.exec() From 9841302256f9b2305c45dd1536c5d4d57c72b4aa Mon Sep 17 00:00:00 2001 From: Benjamin Poulin <90342856+benjaminwp18@users.noreply.github.com> Date: Sat, 23 Sep 2023 16:33:13 -0400 Subject: [PATCH 6/6] Add TODO --- src/surface/gui/gui/app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/surface/gui/gui/app.py b/src/surface/gui/gui/app.py index af277520..4c6f2786 100644 --- a/src/surface/gui/gui/app.py +++ b/src/surface/gui/gui/app.py @@ -37,4 +37,6 @@ def run_gui(self): self.node.destroy_node() self.show() + + # TODO: when the app closes it causes an error. Make not cause error? self.app.exec()