From 5c79fd8195038a9e5a4a4fef9c75e3c2ed854f4a Mon Sep 17 00:00:00 2001 From: LoSk-p Date: Fri, 22 Mar 2024 23:18:36 +0300 Subject: [PATCH] ui changes, handle same sensors in main screen --- app/boards.py | 36 +++++-- app/gui/label_color_utils.py | 17 +++ app/gui/mainwindow.ui | 194 ++++++++++++++++++----------------- app/loading_window.py | 6 +- app/main.py | 53 ++++++++-- 5 files changed, 196 insertions(+), 110 deletions(-) create mode 100644 app/gui/label_color_utils.py diff --git a/app/boards.py b/app/boards.py index 874b960..39a7590 100644 --- a/app/boards.py +++ b/app/boards.py @@ -31,7 +31,8 @@ def __init__(self, sensors): self._sensor_objects = sensors self._board_data = BoardData() self._parser_strategy = None - self._connected_sockets: tp.Dict[str, int] = {} + # self._connected_sockets: tp.Dict[str, int] = {} + self._connected_sockets: tp.Dict[int, str] = {} self._sensors = {} self._sockets = {} for sensor in self._sensor_objects: @@ -59,20 +60,38 @@ def parser(self, data: str) -> bool: return self._parser_strategy.get_parser(data, self._message_id).parse( self._board_data ) + + def get_current_sensor_for_socket(self, socket: int) -> tp.Optional[str]: + return self._connected_sockets.get(socket, None) + # for sensor_name in self._connected_sockets: + # if self._connected_sockets[sensor_name] == socket: + # return sensor_name def update_connected_sockets(self, connected_sockets: tp.Dict): self._connected_sockets = connected_sockets + _LOGGER.debug(f"Update connected sockets: {connected_sockets}") def get_sensors_data(self) -> tp.Dict: sensors_data = {} - for sensor_name in self._connected_sockets: - sensors_data[sensor_name] = self._board_data.sensors_data[ - self._connected_sockets[sensor_name] - ] + for socket in self._connected_sockets: + sensors_data[self._connected_sockets[socket]] = self._board_data.sensors_data[socket] return sensors_data + # sensors_data = {} + # for sensor_name in self._connected_sockets: + # sensors_data[sensor_name] = self._board_data.sensors_data[ + # self._connected_sockets[sensor_name] + # ] + # return sensors_data def get_calibration_coeffs(self, sensor_name: str) -> tp.Dict: - return self._board_data.calibration_coeffs[self._connected_sockets[sensor_name]] + socket = self._get_socket_for_sensor_name(sensor_name) + return self._board_data.calibration_coeffs[socket] + # return self._board_data.calibration_coeffs[self._connected_sockets[sensor_name]] + + def _get_socket_for_sensor_name(self, sensor_name: str) -> int: + for socket in self._connected_sockets: + if self._connected_sockets[socket] == sensor_name: + return socket def get_board_info(self) -> tp.Dict: return self._board_data.board_info @@ -174,8 +193,11 @@ def get_calibration_command( consentration, solution_number = self._sensors[sensor_name].get_consentration( calibration_solution ) + # socket_command = self._socket_calibration_commands[ + # self._connected_sockets[sensor_name] + # ][solution_number] socket_command = self._socket_calibration_commands[ - self._connected_sockets[sensor_name] + self._get_socket_for_sensor_name(sensor_name) ][solution_number] return f"{socket_command}{consentration}".encode(), "#?" diff --git a/app/gui/label_color_utils.py b/app/gui/label_color_utils.py new file mode 100644 index 0000000..779d1af --- /dev/null +++ b/app/gui/label_color_utils.py @@ -0,0 +1,17 @@ +from PyQt5.QtWidgets import QLabel, QGraphicsColorizeEffect +from PyQt5.QtGui import QColor +from PyQt5.QtCore import Qt + +def set_green_label_color(label: QLabel): + _set_label_color(label, Qt.darkGreen) + +def set_red_label_color(label: QLabel): + _set_label_color(label, Qt.darkRed) + +def set_yellow_label_color(label: QLabel): + _set_label_color(label, Qt.darkYellow) + +def _set_label_color(label: QLabel, color: QColor): + color_effect = QGraphicsColorizeEffect() + color_effect.setColor(color) + label.setGraphicsEffect(color_effect) \ No newline at end of file diff --git a/app/gui/mainwindow.ui b/app/gui/mainwindow.ui index 63f937d..06f0469 100644 --- a/app/gui/mainwindow.ui +++ b/app/gui/mainwindow.ui @@ -6,7 +6,7 @@ 0 0 - 521 + 585 631 @@ -22,7 +22,7 @@ 10 60 - 501 + 561 521 @@ -36,10 +36,10 @@ - 10 - 50 - 421 - 403 + 30 + 40 + 471 + 431 @@ -88,7 +88,7 @@ - 130 + 150 30 @@ -147,7 +147,7 @@ - 130 + 150 30 @@ -200,7 +200,7 @@ - 130 + 150 30 @@ -253,7 +253,7 @@ - 130 + 150 30 @@ -306,7 +306,7 @@ - 130 + 150 30 @@ -359,7 +359,7 @@ - 130 + 150 30 @@ -387,7 +387,7 @@ - 10 + 40 10 111 31 @@ -403,7 +403,7 @@ - 140 + 170 10 141 31 @@ -421,9 +421,9 @@ - 270 + 320 10 - 211 + 231 31 @@ -457,9 +457,9 @@ - 270 + 320 50 - 211 + 231 31 @@ -467,7 +467,7 @@ - 150 + 180 140 211 41 @@ -493,9 +493,9 @@ - 270 + 320 90 - 211 + 231 31 @@ -530,7 +530,7 @@ 10 440 - 471 + 541 31 @@ -579,7 +579,7 @@ 179 209 - 301 + 371 221 @@ -742,36 +742,13 @@ - - - - 10 - 20 - 81 - 31 - - - - USB Порт - - - - - - 90 - 20 - 211 - 31 - - - - 340 - 574 - 171 - 31 + 350 + 570 + 181 + 41 @@ -783,78 +760,111 @@ SmartWaterGUI версия ПО 2.0 - + - 410 - 20 - 51 - 31 + 10 + 10 + 561 + 41 - - Заряд: - + + + QLayout::SetDefaultConstraint + + + + + не подключено + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + USB Порт + + + + + + + + 30 + 30 + + + + + + + + Статус: + + + + - + - 460 - 20 - 31 - 31 + 110 + 570 + 16 + 41 - - Qt::LeftToRight - - ... + % Qt::AlignCenter - + - 490 - 20 - 21 - 31 + 75 + 570 + 31 + 41 + + Qt::LeftToRight + - % + ... Qt::AlignCenter - - - - 10 - 580 - 61 - 21 - - - - Статус: - - - + - 70 - 580 - 121 - 21 + 19 + 570 + 47 + 41 - не подключено + Заряд: diff --git a/app/loading_window.py b/app/loading_window.py index 8138db2..81ea1f9 100644 --- a/app/loading_window.py +++ b/app/loading_window.py @@ -12,11 +12,11 @@ def __init__(self, main_window): self.main_window = main_window def show_usb_window(self): - message = "Connecting to the board..." + message = "Подключаемся к плате..." self._show_window(message) def show_calibration_window(self): - message = "Wait for the board response..." + message = "Начинаем калибровку..." self._show_window(message) def _show_window(self, message: str): @@ -45,7 +45,7 @@ def __init__(self, parent, message: str): self.parent_window = parent self.setAttribute(Qt.WA_StyledBackground, True) self.setStyleSheet("background-color: lightgrey;") - self.setFixedSize(200, 100) + self.setFixedSize(250, 100) layout = QVBoxLayout() self.setLayout(layout) diff --git a/app/main.py b/app/main.py index c5684d8..9e8e555 100644 --- a/app/main.py +++ b/app/main.py @@ -9,8 +9,10 @@ from serial.tools.list_ports_common import ListPortInfo from workers import BoardSerial, PortDetectThread from logger import get_logger + from gui.mainwindow import Ui_MainWindow from loading_window import LoadingWindowManager +from gui.label_color_utils import set_green_label_color, set_red_label_color, set_yellow_label_color _LOGGER = get_logger(__name__) @@ -79,6 +81,7 @@ def __init__(self, parent=None, app=None): self.loading_window_manager = LoadingWindowManager(self) self.board_serial = None self.board_status = BoardStatus.Disconnected + self._update_board_status(self.board_status) self.current_board_type: str = SW_BOARD_TYPE self.boards = {SW_BOARD_TYPE: SWBoard(), SWIONS_BOARD_TYPE: SWIonsBoard()} self.current_board: str = self.boards[SW_BOARD_TYPE] @@ -137,6 +140,7 @@ def __init__(self, parent=None, app=None): self.sensors_enabled: tp.List[str] = [] self.populate_sensors_on_calibration() self._setup_graphic() + self._handle_disabled_sensors() self.show() def _setup_graphic(self): @@ -148,13 +152,35 @@ def handle_calibration_button(self): self.calibration = Calibration(self) def sensors_sockets_changed(self, data): + self._set_not_equal_sensors_on_sockets() self._update_connected_sockets() + if self.board_status == BoardStatus.Connected: + self._update_calibration_coeffs() + + def _set_not_equal_sensors_on_sockets(self): + socket_number = 1 + changed_new_sensor_name = None + changed_old_sensor_name = None + changed_socket = None + for sensor in self.sensors_gui: + if sensor[0].currentText() != self.current_board.get_current_sensor_for_socket(socket_number): + changed_new_sensor_name = sensor[0].currentText() + changed_old_sensor_name = self.current_board.get_current_sensor_for_socket(socket_number) + changed_socket = socket_number + socket_number += 1 + if changed_new_sensor_name is not None and changed_old_sensor_name is not None: + socket_number = 1 + for sensor in self.sensors_gui: + if sensor[0].currentText() == changed_new_sensor_name and socket_number != changed_socket: + sensor[0].setCurrentText(changed_old_sensor_name) + socket_number += 1 def _update_connected_sockets(self): i = 1 connected_sockets = {} for sensor in self.sensors_gui: - connected_sockets[sensor[0].currentText()] = i + # connected_sockets[sensor[0].currentText()] = i + connected_sockets[i] = sensor[0].currentText() i += 1 self.current_board.update_connected_sockets(connected_sockets) @@ -167,8 +193,8 @@ def populate_sensors_on_calibration(self): self.boxSensors.addItems(self.sensors_enabled) if self.board_status == BoardStatus.Connected: self._update_sensors_meas() - if self.radioButtonSWIons.isChecked(): - self.boxSensors.addItem("Multi Ions (NO3, NH4, Cl)") + # if self.radioButtonSWIons.isChecked(): + # self.boxSensors.addItem("Multi Ions (NO3, NH4, Cl)") def choose_sensor_calibration(self, sensor: str): self.current_sensor_calibration = sensor @@ -190,6 +216,7 @@ def sw_swions_switched(self): SW_BOARD_TYPE if self.radioButtonSW.isChecked() else SWIONS_BOARD_TYPE ) self.populate_sensors_on_calibration() + self._update_connected_sockets() def set_sensors_units(self): for sensor in self.sensors_gui: @@ -216,6 +243,15 @@ def change_sensor_board(self, board_type: str): def sensor_enabled_changed(self, state: int): _LOGGER.debug(f"Checkbox state changed: {state}") self.populate_sensors_on_calibration() + self._handle_disabled_sensors() + + def _handle_disabled_sensors(self): + for sensor in self.sensors_gui: + self._set_enabled_sensor(sensor, sensor[1].checkState()) + + def _set_enabled_sensor(self, sensor: list, enable: bool): + sensor[0].setEnabled(enable) + sensor[2].setEnabled(enable) def _update_sensors_meas(self) -> None: sensors_data = self.current_board.get_sensors_data() @@ -254,14 +290,15 @@ def _update_board_status(self, board_status: str): self.board_status = board_status self.dataStatus.setText(board_status) if self.board_status == BoardStatus.Connection: + set_yellow_label_color(self.dataStatus) self.loading_window_manager.show_usb_window() - # self.w = AnotherWindow(self) - # self.w.show() else: + if self.board_status == BoardStatus.Connected: + set_green_label_color(self.dataStatus) + else: + set_red_label_color(self.dataStatus) self.loading_window_manager.close_window() - # if self.w is not None: - # self.w.close() - # self.w = None + self.pushButtonStartCalibration.setEnabled(True) def populate_boards(self, ports: tp.List[ListPortInfo]): self.detected_ports = ports