-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.py
More file actions
203 lines (163 loc) · 6.76 KB
/
Copy pathwidget.py
File metadata and controls
203 lines (163 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# This Python file uses the following encoding: utf-8
from __future__ import annotations
import asyncio
import os
import sys
from json import load
from typing import Any
from PySide6.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QLabel,
QStackedWidget,
)
from PySide6.QtGui import QIcon
from pathlib import Path
# Import view skeletons (high-level)
from src.views.console_view import DebugConsole
from src.views.rtsp_view import RTSPView
from src.views.layout_pannel import LayoutPanel
from src.controller.event_bus import EventBus
from src.views.components.header import Header
from src.views.components.nav_bar import NavBar
from src.clients.udp_client import UDPClient
from src.clients.ros2_client import ROS2Client
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Rove - UI")
self._header = Header(parent=self)
self._nav = NavBar(self)
self._central = QWidget(self)
self._central_layout = QVBoxLayout(self._central)
self._central_layout.setContentsMargins(0, 0, 0, 0)
# stacked widget for top-level pages (dashboard, logs, ...)
self._stack = QStackedWidget(self._central)
self._central_layout.addWidget(self._stack)
self.event_bus = EventBus()
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.addWidget(self._header)
layout.addWidget(self._nav)
layout.addWidget(self._central)
#layout.addWidget(self._console)
self._views: list[Any] = []
self._pages: dict[str, int] = {}
self._udp_clients: list[UDPClient] = []
self._ros2_clients: list[ROS2Client] = []
# Application-wide EventBus (can be shared or passed to orchestrator)
self.event_bus = EventBus()
def load_config(self, configFile):
"""Load JSON from a path and build the interface."""
if isinstance(configFile, str):
with open(configFile, "r", encoding="utf-8") as f:
data = load(f)
return data
def buildInterface(self, config: dict):
"""Construct the main interface from the config."""
config = self.load_config(config)
header_settings = config.get("header_settings", {})
header_index = self.layout().indexOf(self._header)
self.layout().removeWidget(self._header)
self._header.deleteLater()
self._header = Header(settings=header_settings, parent=self)
self.layout().insertWidget(header_index, self._header)
views_root = config.get("views", {})
# Clear previous stack pages
while self._stack.count():
w = self._stack.widget(0)
self._stack.removeWidget(w)
w.setParent(None)
self._nav.clear()
for view_name, view_cfg in views_root.items():
vtype = view_cfg.get("type")
if vtype == "layout":
panel = LayoutPanel(view_name, view_cfg, children=[], event_bus=self.event_bus)
panel.build()
page_widget = panel.get_widget()
self._views.append(panel)
else:
page_widget = QLabel(f"Page placeholder: {view_name} ({vtype})")
idx = self._stack.addWidget(page_widget)
self._pages[view_name] = idx
self._nav.add_page(view_name, lambda i=idx: self._stack.setCurrentIndex(i))
if self._stack.count() > 0:
self._stack.setCurrentIndex(0)
self._nav.activate_first()
self._restart_udp_clients(config.get("udp_clients", []))
self._restart_ros2_clients(config.get("ros2_clients", []))
def update_header_time(self, time_value: str):
self._header.update_time(time_value)
def update_header_battery(self, battery_level: int):
self._header.update_battery(battery_level)
def _restart_udp_clients(self, client_configs: list[dict[str, Any]]) -> None:
for client in self._udp_clients:
client.stop()
self._udp_clients = []
for client_config in client_configs:
client = UDPClient(client_config, self.event_bus)
client.start()
self._udp_clients.append(client)
def _restart_ros2_clients(self, client_configs: list[dict[str, Any]]) -> None:
for client in self._ros2_clients:
client.stop()
self._ros2_clients = []
for client_config in client_configs:
client = ROS2Client(client_config, self.event_bus)
client.start()
self._ros2_clients.append(client)
def closeEvent(self, event):
for client in self._udp_clients:
client.stop()
self._udp_clients = []
for client in self._ros2_clients:
client.stop()
self._ros2_clients = []
super().closeEvent(event)
if __name__ == "__main__":
app = QApplication([])
app.setApplicationName("Rove - UI")
app.setDesktopFileName("rove-ui")
_icon_dir = Path(__file__).resolve().parent / "src" / "media" / "icons"
_app_icon: QIcon | None = None
for _candidate in (_icon_dir / "app_icon.png", _icon_dir / "app_icons.png"):
if _candidate.exists():
_app_icon = QIcon(str(_candidate))
break
if _app_icon:
app.setWindowIcon(_app_icon)
window = Widget()
if _app_icon:
window.setWindowIcon(_app_icon)
window.buildInterface("./config/config_window1.json")
window.showMaximized()
# FORCER le bakcground en BLANC
window.setStyleSheet("background-color: #1c1c1b; color: #e0e0e0;")
screens = app.screens()
primary_screen = app.primaryScreen()
# Mettre window sur l'écran principal
window.move(primary_screen.geometry().topLeft())
# Chercher un écran secondaire
secondary_screens = [s for s in screens if s != primary_screen]
# Si il y a un config window 2 existe, on affiche un autre window pour le second écran
# (ex: config_window2.json)
if os.path.exists("./config/config_window2.json"):
window2 = Widget()
if _app_icon:
window2.setWindowIcon(_app_icon)
window2.buildInterface("./config/config_window2.json")
screens = app.screens()
#Si il y a un autre écran,
if len(screens) > 1:
second_screen = secondary_screens[0]
window2.move(second_screen.geometry().topLeft())
asyncio.run(window.event_bus.publish("log", "Window : Application has started on the second screen."))
window2.showMaximized()
window2.setStyleSheet("background-color: #1c1c1b; color: #e0e0e0;")
try:
asyncio.run(window.event_bus.publish("log", "Window : Application has started."))
except Exception:
pass
sys.exit(app.exec())