From f0d0fa07fd2d56ff6ac011f89b6645a24196a39f Mon Sep 17 00:00:00 2001 From: Manu <3916435+m3nu@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:28:16 +0000 Subject: [PATCH] Use Qt signals instead of calling child widgets (#2161) --- src/vorta/application.py | 1 + src/vorta/views/archive_tab.py | 9 +++++++-- src/vorta/views/log_page.py | 15 ++++++++++++--- src/vorta/views/main_window.py | 22 ++++------------------ src/vorta/views/misc_tab.py | 3 ++- src/vorta/views/networks_page.py | 10 +++++----- src/vorta/views/repo_tab.py | 9 +++++---- src/vorta/views/schedule_page.py | 3 +++ src/vorta/views/shell_commands_page.py | 25 ++++++++++++------------- src/vorta/views/source_tab.py | 7 ++++--- 10 files changed, 55 insertions(+), 49 deletions(-) diff --git a/src/vorta/application.py b/src/vorta/application.py index 17ec6752..d87830d9 100644 --- a/src/vorta/application.py +++ b/src/vorta/application.py @@ -42,6 +42,7 @@ class VortaApp(QtSingleApplication): backup_log_event = QtCore.pyqtSignal(str, dict) backup_progress_event = QtCore.pyqtSignal(str) check_failed_event = QtCore.pyqtSignal(dict) + profile_changed_event = QtCore.pyqtSignal() def __init__(self, args_raw, single_app=False): super().__init__(str(APP_ID), args_raw) diff --git a/src/vorta/views/archive_tab.py b/src/vorta/views/archive_tab.py index cab45285..4e59b0e6 100644 --- a/src/vorta/views/archive_tab.py +++ b/src/vorta/views/archive_tab.py @@ -150,8 +150,13 @@ def __init__(self, parent=None, app=None): self.selected_archives = None # TODO: remove unused variable self.set_icons() - # Connect to palette change - self.app.paletteChanged.connect(lambda p: self.set_icons()) + # Connect to events + self.app.paletteChanged.connect(self.set_icons) + self.app.paletteChanged.connect(self.populate_from_profile) + self.app.backup_finished_event.connect(self.populate_from_profile) + self.app.profile_changed_event.connect(self.populate_from_profile) + self.app.profile_changed_event.connect(self.toggle_compact_button_visibility) + self.app.backup_cancelled_event.connect(self.cancel_action) def set_icons(self): """Used when changing between light- and dark mode""" diff --git a/src/vorta/views/log_page.py b/src/vorta/views/log_page.py index 0ae3f12c..86c35a30 100644 --- a/src/vorta/views/log_page.py +++ b/src/vorta/views/log_page.py @@ -1,12 +1,13 @@ from PyQt6 import uic from PyQt6.QtWidgets import ( QAbstractItemView, + QApplication, QHeaderView, QTableWidgetItem, ) from vorta import config -from vorta.store.models import EventLogModel +from vorta.store.models import BackupProfileMixin, EventLogModel from vorta.utils import get_asset uifile = get_asset('UI/log_page.ui') @@ -21,11 +22,13 @@ class LogTableColumn: ReturnCode = 4 -class LogPage(LogTableBase, LogTableUI): +class LogPage(LogTableBase, LogTableUI, BackupProfileMixin): def __init__(self, parent=None): super().__init__(parent) self.setupUi(self) self.init_ui() + QApplication.instance().backup_finished_event.connect(self.populate_logs) + QApplication.instance().profile_changed_event.connect(self.populate_logs) def init_ui(self): self.logPage.setAlternatingRowColors(True) @@ -44,7 +47,13 @@ def init_ui(self): self.populate_logs() def populate_logs(self): - event_logs = [s for s in EventLogModel.select().order_by(EventLogModel.start_time.desc())] + profile = self.profile() + event_logs = [ + s + for s in EventLogModel.select() + .where(EventLogModel.profile == profile.id) + .order_by(EventLogModel.start_time.desc()) + ] sorting = self.logPage.isSortingEnabled() self.logPage.setSortingEnabled(False) diff --git a/src/vorta/views/main_window.py b/src/vorta/views/main_window.py index 7b3e21d1..0a017a27 100644 --- a/src/vorta/views/main_window.py +++ b/src/vorta/views/main_window.py @@ -184,19 +184,13 @@ def profile_selection_changed_action(self, index): backup_profile_id = profile.data(Qt.ItemDataRole.UserRole) if profile else None if not backup_profile_id: return - self.current_profile = BackupProfileModel.get(id=backup_profile_id) - self.archiveTab.populate_from_profile() - self.repoTab.populate_from_profile() - self.sourceTab.populate_from_profile() - self.scheduleTab.schedulePage.populate_from_profile() - self.scheduleTab.networksPage.populate_wifi() - self.scheduleTab.networksPage.setup_connections() - self.scheduleTab.shellCommandsPage.populate_from_profile() + self.current_profile = BackupProfileModel.get(id=backup_profile_id) SettingsModel.update({SettingsModel.str_value: self.current_profile.id}).where( SettingsModel.key == 'previous_profile_id' ).execute() - self.archiveTab.toggle_compact_button_visibility() + + self.app.profile_changed_event.emit() def profile_clicked_action(self): if self.miscWidget.isVisible(): @@ -266,11 +260,8 @@ def profile_imported_event(profile): self.tr('Profile import successful!'), self.tr('Profile {} imported.').format(profile.name), ) - self.repoTab.populate_from_profile() - self.scheduleTab.logPage.populate_logs() - self.scheduleTab.networksPage.populate_wifi() - self.miscTab.populate() self.populate_profile_selector() + self.app.profile_changed_event.emit() filename = QFileDialog.getOpenFileName( self, @@ -328,10 +319,6 @@ def backup_started_event(self): self.set_log('') def backup_finished_event(self): - self.archiveTab.populate_from_profile() - self.repoTab.init_repo_stats() - self.scheduleTab.logPage.populate_logs() - if not self.app.jobs_manager.is_worker_running() and ( self.archiveTab.remaining_refresh_archives == 0 or self.archiveTab.remaining_refresh_archives == 1 ): # Either the refresh is done or this is the last archive to refresh. @@ -341,7 +328,6 @@ def backup_finished_event(self): def backup_cancelled_event(self): self._toggle_buttons(create_enabled=True) self.set_log(self.tr('Task cancelled')) - self.archiveTab.cancel_action() def closeEvent(self, event): # Save window state in SettingsModel diff --git a/src/vorta/views/misc_tab.py b/src/vorta/views/misc_tab.py index 8cde5321..bc8bfe20 100644 --- a/src/vorta/views/misc_tab.py +++ b/src/vorta/views/misc_tab.py @@ -44,8 +44,9 @@ def __init__(self, parent=None): self.populate() - # Connect to palette change + # Connect to events QApplication.instance().paletteChanged.connect(lambda p: self.set_icons()) + QApplication.instance().profile_changed_event.connect(self.populate) def populate(self): """ diff --git a/src/vorta/views/networks_page.py b/src/vorta/views/networks_page.py index a9ee4a1e..6b262f19 100644 --- a/src/vorta/views/networks_page.py +++ b/src/vorta/views/networks_page.py @@ -1,6 +1,6 @@ from PyQt6 import uic from PyQt6.QtCore import Qt -from PyQt6.QtWidgets import QCheckBox, QLabel, QListWidget, QListWidgetItem +from PyQt6.QtWidgets import QApplication, QCheckBox, QLabel, QListWidget, QListWidgetItem from vorta.store.models import BackupProfileMixin, WifiSettingModel from vorta.utils import get_asset, get_sorted_wifis @@ -18,12 +18,12 @@ def __init__(self, parent=None): self.meteredNetworksCheckBox: QCheckBox = self.findChild(QCheckBox, 'meteredNetworksCheckBox') self.wifiListWidget: QListWidget = self.findChild(QListWidget, 'wifiListWidget') - self.populate_wifi() - self.setup_connections() - - def setup_connections(self): + # Connect signals self.meteredNetworksCheckBox.stateChanged.connect(self.on_metered_networks_state_changed) self.wifiListWidget.itemChanged.connect(self.save_wifi_item) + QApplication.instance().profile_changed_event.connect(self.populate_wifi) + + self.populate_wifi() def on_metered_networks_state_changed(self, state): profile = self.profile() diff --git a/src/vorta/views/repo_tab.py b/src/vorta/views/repo_tab.py index 8c6015e9..79cc9aa8 100644 --- a/src/vorta/views/repo_tab.py +++ b/src/vorta/views/repo_tab.py @@ -63,12 +63,13 @@ def __init__(self, parent=None): self.bAddSSHKey.clicked.connect(self.create_ssh_key) self.set_icons() - - # Connect to palette change - QApplication.instance().paletteChanged.connect(lambda p: self.set_icons()) - self.populate_from_profile() # needs init of ssh and compression items + # Connect to events + QApplication.instance().paletteChanged.connect(lambda p: self.set_icons()) + QApplication.instance().profile_changed_event.connect(self.populate_from_profile) + QApplication.instance().backup_finished_event.connect(self.init_repo_stats) + def set_icons(self): self.bAddSSHKey.setIcon(get_colored_icon("plus")) self.bAddRepo.setIcon(get_colored_icon("plus")) diff --git a/src/vorta/views/schedule_page.py b/src/vorta/views/schedule_page.py index fee539d1..d020a012 100644 --- a/src/vorta/views/schedule_page.py +++ b/src/vorta/views/schedule_page.py @@ -64,6 +64,9 @@ def __init__(self, parent=None): self.app.scheduler.schedule_changed.connect(lambda pid: self.draw_next_scheduled_backup()) self.populate_from_profile() + # Listen for events + self.app.profile_changed_event.connect(self.populate_from_profile) + def on_scheduler_change(self, _): profile = self.profile() for label, obj in self.schedulerRadioMapping.items(): diff --git a/src/vorta/views/shell_commands_page.py b/src/vorta/views/shell_commands_page.py index 54dfddaf..707f0f7e 100644 --- a/src/vorta/views/shell_commands_page.py +++ b/src/vorta/views/shell_commands_page.py @@ -1,5 +1,5 @@ from PyQt6 import uic -from PyQt6.QtWidgets import QLineEdit, QWidget +from PyQt6.QtWidgets import QApplication, QLineEdit, QWidget from vorta.store.models import BackupProfileMixin from vorta.utils import get_asset @@ -15,7 +15,17 @@ def __init__(self, parent=None): self.postBackupCmdLineEdit: QLineEdit = self.findChild(QLineEdit, 'postBackupCmdLineEdit') self.createCmdLineEdit: QLineEdit = self.findChild(QLineEdit, 'createCmdLineEdit') self.populate_from_profile() - self.setup_connections() + + self.preBackupCmdLineEdit.textEdited.connect( + lambda new_val, attr='pre_backup_cmd': self.save_profile_attr(attr, new_val) + ) + self.postBackupCmdLineEdit.textEdited.connect( + lambda new_val, attr='post_backup_cmd': self.save_profile_attr(attr, new_val) + ) + self.createCmdLineEdit.textEdited.connect( + lambda new_val, attr='create_backup_cmd': self.save_repo_attr(attr, new_val) + ) + QApplication.instance().profile_changed_event.connect(self.populate_from_profile) def populate_from_profile(self): profile = self.profile() @@ -33,17 +43,6 @@ def populate_from_profile(self): self.preBackupCmdLineEdit.setEnabled(False) self.postBackupCmdLineEdit.setEnabled(False) - def setup_connections(self): - self.preBackupCmdLineEdit.textEdited.connect( - lambda new_val, attr='pre_backup_cmd': self.save_profile_attr(attr, new_val) - ) - self.postBackupCmdLineEdit.textEdited.connect( - lambda new_val, attr='post_backup_cmd': self.save_profile_attr(attr, new_val) - ) - self.createCmdLineEdit.textEdited.connect( - lambda new_val, attr='create_backup_cmd': self.save_repo_attr(attr, new_val) - ) - def save_profile_attr(self, attr, new_value): profile = self.profile() setattr(profile, attr, new_value) diff --git a/src/vorta/views/source_tab.py b/src/vorta/views/source_tab.py index 20f0a264..66dc7b3f 100644 --- a/src/vorta/views/source_tab.py +++ b/src/vorta/views/source_tab.py @@ -106,13 +106,14 @@ def __init__(self, parent=None): self.bExclude.clicked.connect(self.show_exclude_dialog) header.sortIndicatorChanged.connect(self.update_sort_order) - # Connect to palette change - QApplication.instance().paletteChanged.connect(lambda p: self.set_icons()) - # Populate self.populate_from_profile() self.set_icons() + # Listen for events + QApplication.instance().paletteChanged.connect(lambda p: self.set_icons()) + QApplication.instance().profile_changed_event.connect(self.populate_from_profile) + def set_icons(self): "Used when changing between light- and dark mode" self.addButton.setIcon(get_colored_icon('plus'))