From d89eb526fad0fc6d52cb1516e2ed56314c02a257 Mon Sep 17 00:00:00 2001 From: Kristoffer Richardsson Date: Wed, 3 Mar 2021 17:10:36 +0100 Subject: [PATCH] #473 Moved interface selection into connectivity manager --- src/cfclient/ui/connectivity_manager.py | 45 ++++++++++++++++++++++--- src/cfclient/ui/main.py | 35 ++++++------------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/src/cfclient/ui/connectivity_manager.py b/src/cfclient/ui/connectivity_manager.py index a49baef51b..7e8f0de7d9 100644 --- a/src/cfclient/ui/connectivity_manager.py +++ b/src/cfclient/ui/connectivity_manager.py @@ -39,9 +39,16 @@ class ConnectivityManager(QObject): 'connect_button', 'scan_button']) + CONNECTION_STATE_DISCONNECTED = 0 + CONNECTION_STATE_CONNECTING = 1 + CONNECTION_STATE_CONNECTED = 2 + CONNECTION_STATE_SCANNING = 3 + + INTERFACE_PROMPT_TEXT = 'Select an interface' + connect_button_clicked = pyqtSignal() scan_button_clicked = pyqtSignal(object) - interface_combo_current_index_changed = pyqtSignal(object) + connection_state_changed = pyqtSignal(object) def __init__(self): QObject.__init__(self) @@ -59,7 +66,10 @@ def register_ui_elements(self, ui_elements): ui_elements.interface_combo.currentIndexChanged['QString'].connect( self._interface_combo_current_index_changed_handler) - def set_state_disconnected(self, can_connect): + def set_state_disconnected(self): + self.connection_state_changed.emit(self.CONNECTION_STATE_DISCONNECTED) + + can_connect = self.get_interface() is not None for ui_elements in self._ui_elements: ui_elements.connect_button.setText("Connect") ui_elements.connect_button.setToolTip("Connect to the Crazyflie on the selected interface (Ctrl+I)") @@ -70,6 +80,7 @@ def set_state_disconnected(self, can_connect): ui_elements.interface_combo.setEnabled(True) def set_state_connected(self): + self.connection_state_changed.emit(self.CONNECTION_STATE_CONNECTED) for ui_elements in self._ui_elements: ui_elements.connect_button.setText("Disconnect") ui_elements.connect_button.setToolTip("Disconnect from the Crazyflie (Ctrl+I)") @@ -78,6 +89,7 @@ def set_state_connected(self): ui_elements.interface_combo.setEnabled(False) def set_state_connecting(self): + self.connection_state_changed.emit(self.CONNECTION_STATE_CONNECTING) for ui_elements in self._ui_elements: ui_elements.connect_button.setText("Cancel") ui_elements.connect_button.setToolTip("Cancel connecting to the Crazyflie") @@ -86,6 +98,7 @@ def set_state_connecting(self): ui_elements.interface_combo.setEnabled(False) def set_state_scanning(self): + self.connection_state_changed.emit(self.CONNECTION_STATE_SCANNING) for ui_elements in self._ui_elements: ui_elements.connect_button.setText("Connect") ui_elements.connect_button.setEnabled(False) @@ -94,6 +107,13 @@ def set_state_scanning(self): ui_elements.address_spinner.setEnabled(False) ui_elements.interface_combo.setEnabled(False) + def set_enable(self, enable): + for ui_elements in self._ui_elements: + ui_elements.connect_button.setEnabled(enable) + ui_elements.scan_button.setEnabled(enable) + ui_elements.address_spinner.setEnabled(enable) + ui_elements.interface_combo.setEnabled(enable) + def set_address(self, address): for ui_elements in self._ui_elements: ui_elements.address_spinner.setValue(address) @@ -105,12 +125,27 @@ def get_address(self): return 0 def set_interfaces(self, interface_items, index): + new_index = 0 + if index is not None: + new_index = index + 1 + for ui_elements in self._ui_elements: combo = ui_elements.interface_combo combo.clear() + combo.addItem(self.INTERFACE_PROMPT_TEXT) combo.addItems(interface_items) - combo.setCurrentIndex(index) + combo.setCurrentIndex(new_index) + + def get_interface(self): + if len(self._ui_elements) > 0: + interface = self._ui_elements[0].interface_combo.currentText() + if interface == self.INTERFACE_PROMPT_TEXT: + self._selected_interface = None + else: + return interface + else: + return None def _connect_button_click_handler(self): self.connect_button_clicked.emit() @@ -140,9 +175,9 @@ def _address_edited_handler(self): ui_elements.address_spinner.setValue(value) def _interface_combo_current_index_changed_handler(self, interface): + can_connect = interface != self.INTERFACE_PROMPT_TEXT for ui_elements in self._ui_elements: combo = ui_elements.interface_combo if combo.currentText != interface: combo.setCurrentText(interface) - - self.interface_combo_current_index_changed.emit(interface) + ui_elements.connect_button.setEnabled(can_connect) diff --git a/src/cfclient/ui/main.py b/src/cfclient/ui/main.py index c812d25284..ce04d4f8fa 100644 --- a/src/cfclient/ui/main.py +++ b/src/cfclient/ui/main.py @@ -72,8 +72,6 @@ logger = logging.getLogger(__name__) -INTERFACE_PROMPT_TEXT = 'Select an interface' - (main_window_class, main_windows_base_class) = (uic.loadUiType(cfclient.module_path + '/ui/main.ui')) @@ -187,7 +185,6 @@ def __init__(self, *args): self._connectivity_manager.connect_button_clicked.connect(self._connect) self._connectivity_manager.scan_button_clicked.connect(self._scan) - self._connectivity_manager.interface_combo_current_index_changed.connect(self.interfaceChanged) self._auto_reconnect_enabled = Config().get("auto_reconnect") self.autoReconnectCheckBox.toggled.connect( @@ -231,8 +228,6 @@ def __init__(self, *args): self.linkQualitySignal.connect( lambda percentage: self.linkQualityBar.setValue(percentage)) - self._selected_interface = None - # Parse the log configuration files self.logConfigReader = LogConfigReader(self.cf) @@ -394,17 +389,8 @@ def disable_input(self, disable): """ self._disable_input = disable - def interfaceChanged(self, interface): - if interface == INTERFACE_PROMPT_TEXT: - self._selected_interface = None - else: - self._selected_interface = interface - self._update_ui_state() - def foundInterfaces(self, interfaces): - selected_interface = self._selected_interface - - interface_items = [INTERFACE_PROMPT_TEXT] + selected_interface = self._connectivity_manager.get_interface() formatted_interfaces = [] for i in interfaces: @@ -413,7 +399,6 @@ def foundInterfaces(self, interfaces): else: interface = i[0] formatted_interfaces.append(interface) - interface_items += formatted_interfaces if self._initial_scan: self._initial_scan = False @@ -432,14 +417,14 @@ def foundInterfaces(self, interfaces): if len(interfaces) == 1 and selected_interface is None: selected_interface = interfaces[0][0] - newIndex = 0 + newIndex = None if selected_interface is not None: try: - newIndex = formatted_interfaces.index(selected_interface) + 1 + newIndex = formatted_interfaces.index(selected_interface) except ValueError: pass - self._connectivity_manager.set_interfaces(interface_items, newIndex) + self._connectivity_manager.set_interfaces(formatted_interfaces, newIndex) self.uiState = UIState.DISCONNECTED self._update_ui_state() @@ -447,16 +432,16 @@ def foundInterfaces(self, interfaces): def _update_ui_state(self): if self.uiState == UIState.DISCONNECTED: self.setWindowTitle("Not connected") - canConnect = self._selected_interface is not None + canConnect = self._connectivity_manager.get_interface() is not None self.menuItemConnect.setText("Connect to Crazyflie") self.menuItemConnect.setEnabled(canConnect) - self._connectivity_manager.set_state_disconnected(canConnect) + self._connectivity_manager.set_state_disconnected() self.batteryBar.setValue(3000) self._menu_cf2_config.setEnabled(False) self.linkQualityBar.setValue(0) self.logConfigAction.setEnabled(False) elif self.uiState == UIState.CONNECTED: - s = "Connected on %s" % self._selected_interface + s = "Connected on %s" % self._connectivity_manager.get_interface() self.setWindowTitle(s) self.menuItemConnect.setText("Disconnect") self.menuItemConnect.setEnabled(True) @@ -467,7 +452,7 @@ def _update_ui_state(self): if len(self.cf.mem.get_mems(MemoryElement.TYPE_I2C)) > 0: self._menu_cf2_config.setEnabled(True) elif self.uiState == UIState.CONNECTING: - s = "Connecting to {} ...".format(self._selected_interface) + s = "Connecting to {} ...".format(self._connectivity_manager.get_interface()) self.setWindowTitle(s) self.menuItemConnect.setText("Cancel") self.menuItemConnect.setEnabled(True) @@ -535,7 +520,7 @@ def _connected(self): self.uiState = UIState.CONNECTED self._update_ui_state() - Config().set("link_uri", str(self._selected_interface)) + Config().set("link_uri", str(self._connectivity_manager.get_interface())) lg = LogConfig("Battery", 1000) lg.add_variable("pm.vbat", "float") @@ -606,7 +591,7 @@ def _connect(self): self.uiState = UIState.DISCONNECTED self._update_ui_state() else: - self.cf.open_link(self._selected_interface) + self.cf.open_link(self._connectivity_manager.get_interface()) def _scan(self, address): self.uiState = UIState.SCANNING