Skip to content
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

Keep the profile list sorted when creating/renaming profiles #1986

Merged
merged 3 commits into from
May 25, 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
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
Loading