From 6b5f7a7aac93dbb6b63d8642359083db62e7564e Mon Sep 17 00:00:00 2001 From: Parnassius Date: Sat, 25 May 2024 15:00:51 +0200 Subject: [PATCH] Keep the profile list sorted when adding profiles. By @Parnassius (#1986) --- src/vorta/tray_menu.py | 4 ++-- src/vorta/views/main_window.py | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/vorta/tray_menu.py b/src/vorta/tray_menu.py index b9d5ac74..eef97b50 100644 --- a/src/vorta/tray_menu.py +++ b/src/vorta/tray_menu.py @@ -59,10 +59,10 @@ def build_menu(self): cancel_action.triggered.connect(self.app.backup_cancelled_event.emit) else: status.setText(self.tr('Next Task: %s') % next_task_time) - profiles = BackupProfileModel.select().order_by(BackupProfileModel.name) + profiles = BackupProfileModel.select() if profiles.count() > 1: profile_menu = menu.addMenu(self.tr('Backup Now')) - for profile in profiles: + for profile in sorted(profiles, key=lambda p: (p.name.casefold(), p.name)): new_item = profile_menu.addAction(profile.name) new_item.triggered.connect(lambda state, i=profile.id: self.app.create_backup_action(i)) else: diff --git a/src/vorta/views/main_window.py b/src/vorta/views/main_window.py index 78cf40f4..f42e04c4 100644 --- a/src/vorta/views/main_window.py +++ b/src/vorta/views/main_window.py @@ -65,7 +65,8 @@ def __init__(self, parent=None): prev_profile_id = SettingsModel.get(key='previous_profile_id') self.current_profile = BackupProfileModel.get_or_none(id=prev_profile_id.str_value) if self.current_profile is None: - self.current_profile = BackupProfileModel.select().order_by('name').first() + profiles = BackupProfileModel.select() + self.current_profile = min(profiles, key=lambda p: (p.name.casefold(), p.name)) # Load tab models self.repoTab = RepoTab(self.repoTabSlot) @@ -161,7 +162,8 @@ def populate_profile_selector(self): current_item = None # Add items to the QListWidget - for profile in BackupProfileModel.select().order_by(BackupProfileModel.name): + profiles = BackupProfileModel.select() + for profile in sorted(profiles, key=lambda p: (p.name.casefold(), p.name)): item = QListWidgetItem(profile.name) item.setData(Qt.ItemDataRole.UserRole, profile.id) @@ -284,13 +286,22 @@ def profile_imported_event(profile): def profile_add_edit_result(self, profile_name, profile_id): # Profile is renamed if self.profileSelector.currentItem().data(Qt.ItemDataRole.UserRole) == profile_id: - self.profileSelector.currentItem().setText(profile_name) + profile = self.profileSelector.takeItem(self.profileSelector.currentRow()) + profile.setText(profile_name) # Profile is added else: profile = QListWidgetItem(profile_name) profile.setData(Qt.ItemDataRole.UserRole, profile_id) - self.profileSelector.addItem(profile) - self.profileSelector.setCurrentItem(profile) + # Insert the profile at the correct position + # Both the casefolded and the original name are used as the key to keep the order stable + profile_key = (profile.text().casefold(), profile.text()) + row = 0 + for i in range(self.profileSelector.count()): + item_name = self.profileSelector.item(i).text() + if (item_name.casefold(), item_name) < profile_key: + row += 1 + self.profileSelector.insertItem(row, profile) + self.profileSelector.setCurrentItem(profile) def toggle_misc_visibility(self): if self.miscWidget.isVisible():