Skip to content

Commit

Permalink
Keep the profile list sorted when adding profiles. By @Parnassius (bo…
Browse files Browse the repository at this point in the history
  • Loading branch information
Parnassius authored and shivansh02 committed Jun 7, 2024
1 parent 4bb3e38 commit f1d6d57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/vorta/tray_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 16 additions & 5 deletions src/vorta/views/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit f1d6d57

Please sign in to comment.