Skip to content

Use events instead of calling child widgets #2161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/vorta/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions src/vorta/views/archive_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
15 changes: 12 additions & 3 deletions src/vorta/views/log_page.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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)
Expand All @@ -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)
Expand Down
22 changes: 4 additions & 18 deletions src/vorta/views/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/vorta/views/misc_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
10 changes: 5 additions & 5 deletions src/vorta/views/networks_page.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions src/vorta/views/repo_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ def __init__(self, parent=None):
self.bAddSSHKey.clicked.connect(self.create_ssh_key)

self.set_icons()
self.populate_from_profile() # needs init of ssh and compression items

# Connect to palette change
# Connect to events
QApplication.instance().paletteChanged.connect(lambda p: self.set_icons())

self.populate_from_profile() # needs init of ssh and compression items
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"))
Expand Down
3 changes: 3 additions & 0 deletions src/vorta/views/schedule_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
25 changes: 12 additions & 13 deletions src/vorta/views/shell_commands_page.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/vorta/views/source_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
Loading