From 75ce422cadebb3caeac0a4722a281542d17bb838 Mon Sep 17 00:00:00 2001 From: artem30801 <38689676+artem30801@users.noreply.github.com> Date: Tue, 31 Dec 2019 11:56:53 +0300 Subject: [PATCH] Refactored visual land dialog (YAY!) + shortcuts + not closing every time (#62) --- Server/server_qt.py | 54 ++--------------- Server/visual_land_dialog.py | 112 +++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 48 deletions(-) create mode 100644 Server/visual_land_dialog.py diff --git a/Server/server_qt.py b/Server/server_qt.py index d645cf2..f6fee60 100644 --- a/Server/server_qt.py +++ b/Server/server_qt.py @@ -18,7 +18,7 @@ from server_gui import Ui_MainWindow from server import * import messaging_lib as messaging from copter_table_models import * -from visual_land import * +from visual_land_dialog import VisualLandDialog import threading @@ -286,53 +286,6 @@ class MainWindow(QtWidgets.QMainWindow): def land_all(self): Client.broadcast_message("land") - @pyqtSlot() - def visual_land(self): - client_row_min = 0 - client_row_max = self.model.rowCount() - 1 - result = -1 - while (result != 0) and (result != 3) and (result != 4): - # light_green_red(min, max) - client_row_mid = int(math.ceil((client_row_max + client_row_min) / 2.0)) - print(client_row_min, client_row_mid, client_row_max) - for row_num in range(client_row_min, client_row_mid): - self.model.data_contents[row_num].client \ - .send_message("led_fill", {"green": 255}) - for row_num in range(client_row_mid, client_row_max + 1): - self.model.data_contents[row_num].client \ - .send_message("led_fill", {"red": 255}) - - Dialog = QtWidgets.QDialog() - ui = Ui_Dialog() - ui.setupUi(Dialog) - Dialog.show() - result = Dialog.exec() - print("Dialog result: {}".format(result)) - - if client_row_max != client_row_min: - if result == 1: - for row_num in range(client_row_mid, client_row_max + 1): - self.model.data_contents[row_num].client \ - .send_message("led_fill") - client_row_max = client_row_mid - 1 - - elif result == 2: - for row_num in range(client_row_min, client_row_mid): - self.model.data_contents[row_num].client \ - .send_message("led_fill") - client_row_min = client_row_mid - - if result == 0: - Client.broadcast_message("led_fill") - elif result == 3: - for row_num in range(client_row_min, client_row_max + 1): - self.model.data_contents[row_num].client \ - .send_message("land") - elif result == 4: - for row_num in range(client_row_min, client_row_max + 1): - self.model.data_contents[row_num].client \ - .send_message("disarm") - @pyqtSlot() def emergency_land_selected(self): for copter in self.model.user_selected(): @@ -602,6 +555,11 @@ class MainWindow(QtWidgets.QMainWindow): logging.info("Playing music") self.player.play() + @pyqtSlot() + def visual_land(self): + dialog = VisualLandDialog(self.model) + dialog.start() + @messaging.message_callback("telemetry") def get_telem_data(self, **kwargs): diff --git a/Server/visual_land_dialog.py b/Server/visual_land_dialog.py new file mode 100644 index 0000000..aed194c --- /dev/null +++ b/Server/visual_land_dialog.py @@ -0,0 +1,112 @@ +from PyQt5.QtCore import pyqtSlot +from PyQt5.QtGui import QKeySequence +from PyQt5 import QtWidgets + +import visual_land +import math +import logging +import sys +from functools import partial + + +# TODO: previous step and reset +class VisualLandDialog(QtWidgets.QDialog): + def __init__(self, model): + super(VisualLandDialog, self).__init__() + + self.ui = visual_land.Ui_Dialog() + self.setupUi() + + self.model = model + self.row_min = 0 + self.row_max = self.model.rowCount() - 1 + self._finished = False + + def setupUi(self): + self.ui.setupUi(self) + self.ui.one_button.clicked.connect(partial(self.selection_choice, 1)) + self.ui.two_button.clicked.connect(partial(self.selection_choice, 2)) + self.ui.land_emergency_button.clicked.connect(partial(self.send_to_selected, "land", None)) + self.ui.disarm_emergency_button.clicked.connect(partial(self.send_to_selected, "disarm", None)) + + self.ui.one_button.setShortcut(QKeySequence("1")) + self.ui.two_button.setShortcut(QKeySequence("2")) + self.ui.land_emergency_button.setShortcut(QKeySequence("L")) + self.ui.disarm_emergency_button.setShortcut(QKeySequence("D")) + + @property + def row_mid(self): + return int(math.ceil((self.row_min + self.row_max) / 2.0)) + + def send_to_row(self, row, message, args=None): + logging.debug(f"Send {message}: {args} to {row}") + self.model.data_contents[row].client.send_message(message, args) + # test[row] = args # for testing + # print(test) + + def clear_leds(self, rows): + for row in rows: + self.send_to_row(row, "led_fill") + + def start(self): + self.show() + self.send_led_indication() + + self.exec() + + def send_led_indication(self): + for row in range(self.row_min, self.row_mid): + self.send_to_row(row, "led_fill", {"green": 255}) + + for row in range(self.row_mid, self.row_max + 1): + self.send_to_row(row, "led_fill", {"red": 255}) + + @pyqtSlot() + def selection_choice(self, choice): + if self.row_min == self.row_max: + # self.ui.one_button.setDisabled(True) # maybe? + # self.ui.two_button.setDisabled(True) + return + + if choice == 1: + to_clear = range(self.row_mid, self.row_max + 1) + self.row_max = self.row_mid - 1 + + elif choice == 2: + to_clear = range(self.row_min, self.row_mid) + self.row_min = self.row_mid + + else: + return + + self.clear_leds(to_clear) + self.send_led_indication() + + @pyqtSlot() + def send_to_selected(self, message, args=None): + for row in range(self.row_min, self.row_max + 1): + self.send_to_row(row, message, args) + + self._finished = True + self.close() + + def closeEvent(self, event): + if not self._finished: + self.clear_leds(range(self.row_min, self.row_max + 1)) + + event.accept() + + +if __name__ == '__main__': + logging.basicConfig(level=logging.DEBUG) + app = QtWidgets.QApplication(sys.argv) + + import copter_table_models + model = copter_table_models.CopterDataModel() + for i in range(10): + model.add_client(copter_table_models.StatedCopterData()) + + dialog = VisualLandDialog(model) + test = list(range(10)) + + dialog.start()