From 5cdefbf9f25b6add243537a43e8704087da0e42c Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Thu, 10 Oct 2024 23:04:34 -0500 Subject: [PATCH] Select newly inserted rows in Profile model/view. This fixes a bug when adding a new custom profile and the context menu not updating correctly. Also, fixed the Profile comparison to use "is" since we just modified the Swig bindings for Profile equality operators. --- src/windows/models/profiles_model.py | 4 ++-- src/windows/views/profiles_treeview.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/windows/models/profiles_model.py b/src/windows/models/profiles_model.py index 3e1b1ae8d..f7bc2ade6 100644 --- a/src/windows/models/profiles_model.py +++ b/src/windows/models/profiles_model.py @@ -91,7 +91,7 @@ def update_or_insert_row(self, profile): existing_index = None for row in range(self.model.rowCount()): index = self.model.index(row, 0) # Assuming key is in column 0 - if index.data(Qt.UserRole) == profile: + if index.data(Qt.UserRole) is profile: existing_index = index break @@ -109,7 +109,7 @@ def remove_row(self, profile): # Find if the profile already exists in the model by key for row in range(self.model.rowCount()): index = self.model.index(row, 0) # Assuming key is in column 0 - if index.data(Qt.UserRole) == profile: + if index.data(Qt.UserRole) is profile: # Remove the row from the model self.model.removeRow(row) break diff --git a/src/windows/views/profiles_treeview.py b/src/windows/views/profiles_treeview.py index 2d1e5d7cd..5922b021b 100644 --- a/src/windows/views/profiles_treeview.py +++ b/src/windows/views/profiles_treeview.py @@ -46,6 +46,17 @@ def selectionChanged(self, selected, deselected): self.selected_profile_object = selected.first().indexes()[0].data(Qt.UserRole) super().selectionChanged(selected, deselected) + def on_rows_inserted(self, parent, first, last): + """Handle row insertion and refresh view.""" + self.last_inserted_row_index = self.model().index(last, 0) + + # Select the newly inserted row + if self.last_inserted_row_index.isValid(): + self.selectionModel().clear() + self.select_profile(self.last_inserted_row_index) + self.selectionModel().select(self.last_inserted_row_index, QItemSelectionModel.Select) + self.scrollTo(self.last_inserted_row_index) + def refresh_view(self, filter_text=""): """Filter transitions with proxy class""" self.is_filter_running = True @@ -126,6 +137,8 @@ def __init__(self, dialog, profiles, *args): self.setStyleSheet('QTreeView::item { padding-top: 2px; }') self.columns = 6 self.selected_profile_object = None + self.last_inserted_row_index = None + self.model().rowsInserted.connect(self.on_rows_inserted) # Refresh view self.profiles_model.update_model()