From 0fb7d7d761397d6240b76faaaafaa8bf6661280c Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 20 Oct 2021 19:03:24 -0700 Subject: [PATCH] Refactor TorSettingsDialog into TorSettingsTab --- desktop/src/onionshare/main_window.py | 7 +--- desktop/src/onionshare/tab_widget.py | 37 +++++++++++++++++---- desktop/src/onionshare/tor_settings_tab.py | 38 ++++++++++------------ 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/desktop/src/onionshare/main_window.py b/desktop/src/onionshare/main_window.py index 0f11cf8e..4a9d0c7e 100644 --- a/desktop/src/onionshare/main_window.py +++ b/desktop/src/onionshare/main_window.py @@ -246,9 +246,7 @@ class MainWindow(QtWidgets.QMainWindow): Open the TorSettingsTab """ self.common.log("MainWindow", "open_tor_settings") - # d = TorSettingsDialog(self.common) - # d.settings_saved.connect(self.settings_have_changed) - # d.exec_() + self.tabs.open_tor_settings_tab() def open_settings(self): """ @@ -256,9 +254,6 @@ class MainWindow(QtWidgets.QMainWindow): """ self.common.log("MainWindow", "open_settings") self.tabs.open_settings_tab() - # d = SettingsDialog(self.common) - # d.settings_saved.connect(self.settings_have_changed) - # d.exec_() def settings_have_changed(self): self.common.log("OnionShareGui", "settings_have_changed") diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index daf878d7..fe6d08dc 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -92,8 +92,9 @@ class TabWidget(QtWidgets.QTabWidget): # Clean up each tab for index in range(self.count()): - tab = self.widget(index) - tab.cleanup() + if not self.is_settings_tab(index): + tab = self.widget(index) + tab.cleanup() def move_new_tab_button(self): # Find the width of all tabs @@ -186,7 +187,7 @@ class TabWidget(QtWidgets.QTabWidget): # See if a settings tab is already open, and if so switch to it for index in range(self.count()): - if self.is_settings_tab(index): + if type(self.tabs[index]) is SettingsTab: self.setCurrentIndex(index) return @@ -200,6 +201,27 @@ class TabWidget(QtWidgets.QTabWidget): if self.common.platform == "Darwin": self.macos_create_close_button(settings_tab, index) + def open_tor_settings_tab(self): + self.common.log("TabWidget", "open_tor_settings_tab") + + # See if a settings tab is already open, and if so switch to it + for index in range(self.count()): + if type(self.tabs[index]) is TorSettingsTab: + self.setCurrentIndex(index) + return + + tor_settings_tab = TorSettingsTab(self.common, self.current_tab_id) + self.tabs[self.current_tab_id] = tor_settings_tab + self.current_tab_id += 1 + index = self.addTab( + tor_settings_tab, strings._("gui_tor_settings_window_title") + ) + self.setCurrentIndex(index) + + # In macOS, manually create a close button because tabs don't seem to have them otherwise + if self.common.platform == "Darwin": + self.macos_create_close_button(tor_settings_tab, index) + def change_title(self, tab_id, title): shortened_title = title if len(shortened_title) > 11: @@ -280,10 +302,11 @@ class TabWidget(QtWidgets.QTabWidget): See if there are active servers in any open tabs """ for tab_id in self.tabs: - mode = self.tabs[tab_id].get_mode() - if mode: - if mode.server_status.status != mode.server_status.STATUS_STOPPED: - return True + if not self.is_settings_tab(tab_id): + mode = self.tabs[tab_id].get_mode() + if mode: + if mode.server_status.status != mode.server_status.STATUS_STOPPED: + return True return False def paintEvent(self, event): diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index 279469df..e46fa729 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -319,15 +319,10 @@ class TorSettingsTab(QtWidgets.QWidget): self.test_tor_button.clicked.connect(self.test_tor_clicked) self.save_button = QtWidgets.QPushButton(strings._("gui_settings_button_save")) self.save_button.clicked.connect(self.save_clicked) - self.cancel_button = QtWidgets.QPushButton( - strings._("gui_settings_button_cancel") - ) - self.cancel_button.clicked.connect(self.cancel_clicked) buttons_layout = QtWidgets.QHBoxLayout() buttons_layout.addWidget(self.test_tor_button) buttons_layout.addStretch() buttons_layout.addWidget(self.save_button) - buttons_layout.addWidget(self.cancel_button) # Layout layout = QtWidgets.QVBoxLayout() @@ -337,7 +332,6 @@ class TorSettingsTab(QtWidgets.QWidget): layout.addLayout(buttons_layout) self.setLayout(layout) - self.cancel_button.setFocus() self.reload_settings() @@ -686,23 +680,27 @@ class TorSettingsTab(QtWidgets.QWidget): self.settings_saved.emit() self.close() - def cancel_clicked(self): + def close_tab(self): """ - Cancel button clicked. + Tab is closed """ self.common.log("TorSettingsTab", "cancel_clicked") - if ( - not self.common.gui.local_only - and not self.common.gui.onion.is_authenticated() - ): - Alert( - self.common, - strings._("gui_tor_connection_canceled"), - QtWidgets.QMessageBox.Warning, - ) - sys.exit() - else: - self.close() + return True + + # TODO: Figure out flow for first connecting, when closing settings when not connected + + # if ( + # not self.common.gui.local_only + # and not self.common.gui.onion.is_authenticated() + # ): + # Alert( + # self.common, + # strings._("gui_tor_connection_canceled"), + # QtWidgets.QMessageBox.Warning, + # ) + # sys.exit() + # else: + # self.close() def settings_from_fields(self): """