Skip to content

Commit

Permalink
Implements BetaRavener#59.
Browse files Browse the repository at this point in the history
Improves port selection when ports list is
refreshed. Last user selection is preserved
and preferred port is used on start-up.
  • Loading branch information
BetaRavener committed Aug 28, 2018
1 parent 971aaaf commit a5241f7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self):
self.actionSettings.triggered.connect(self.open_settings_dialog)
self.actionAbout.triggered.connect(self.open_about_dialog)

self.lastSelectedConnection = None
self.connectionComboBox.currentIndexChanged.connect(self.connection_changed)
self.refreshButton.clicked.connect(self.refresh_ports)

Expand Down Expand Up @@ -108,21 +109,31 @@ def closeEvent(self, event):
def connection_changed(self):
connection = self._connection_scanner.port_list[self.connectionComboBox.currentIndex()]
self.connectionStackedWidget.setCurrentIndex(1 if connection == "wifi" else 0)
self.lastSelectedConnection = connection

def refresh_ports(self):
# Cache value of last selected connection because it might change when manipulating combobox
last_selected_connection = self.lastSelectedConnection

self._connection_scanner.scan_connections(with_wifi=True)
# Populate port combo box and select default
self.connectionComboBox.clear()

# Test if there are any available ports
if self._connection_scanner.port_list:
selected_port_idx = 0
selected_port_idx = -1
pref_port = Settings().preferred_port

# Populate port combo box and get index of preferred port if available
for i, port in enumerate(self._connection_scanner.port_list):
self.connectionComboBox.addItem(port)
if pref_port and port.upper() == pref_port.upper():
selected_port_idx = i

self.connectionComboBox.setCurrentIndex(selected_port_idx)
# Override preferred port if user made selection and this port is still available
if last_selected_connection and last_selected_connection in self._connection_scanner.port_list:
selected_port_idx = self._connection_scanner.port_list.index(last_selected_connection)
# Set current port
self.connectionComboBox.setCurrentIndex(selected_port_idx if selected_port_idx >= 0 else 0)
self.connectButton.setEnabled(True)
else:
self.connectButton.setEnabled(False)
Expand Down

0 comments on commit a5241f7

Please sign in to comment.