1
0
Fork 0
mirror of https://github.com/borgbase/vorta synced 2024-12-22 07:43:09 +00:00

Keep the profile list sorted when adding profiles. By @Parnassius (#1986)

This commit is contained in:
Parnassius 2024-05-25 15:00:51 +02:00 committed by GitHub
parent 9cabbbd193
commit 6b5f7a7aac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 7 deletions

View file

@ -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:

View file

@ -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,12 +286,21 @@ 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)
# 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):