Improve performance selecting multiple tracks

This commit is contained in:
Keith Edmunds 2022-04-04 21:30:49 +01:00
parent c5f33c437f
commit 805053b795

View File

@ -136,6 +136,7 @@ class PlaylistTab(QTableWidget):
self.itemSelectionChanged.connect(self._select_event)
self.editing_cell: bool = False
self.selecting_in_progress = False
self.cellChanged.connect(self._cell_changed)
self.doubleClicked.connect(self._edit_cell)
self.cellEditingStarted.connect(self._cell_edit_started)
@ -390,8 +391,13 @@ class PlaylistTab(QTableWidget):
# Row number will change as we delete rows so remove them in
# reverse order.
for row in sorted(rows, reverse=True):
self.removeRow(row)
try:
self.selecting_in_progress = True
for row in sorted(rows, reverse=True):
self.removeRow(row)
finally:
self.selecting_in_progress = False
self._select_event()
with Session() as session:
self.save_playlist(session)
@ -599,7 +605,12 @@ class PlaylistTab(QTableWidget):
def select_played_tracks(self) -> None:
"""Select all played tracks in playlist"""
self._select_tracks(played=True)
try:
self.selecting_in_progress = True
self._select_tracks(played=True)
finally:
self.selecting_in_progress = False
self._select_event()
def select_previous_row(self) -> None:
"""
@ -640,7 +651,12 @@ class PlaylistTab(QTableWidget):
def select_unplayed_tracks(self) -> None:
"""Select all unplayed tracks in playlist"""
self._select_tracks(played=False)
try:
self.selecting_in_progress = True
self._select_tracks(played=False)
finally:
self.selecting_in_progress = False
self._select_event()
def set_selected_as_next(self) -> None:
"""Sets the select track as next to play"""
@ -1411,11 +1427,16 @@ class PlaylistTab(QTableWidget):
If multiple rows are selected, display sum of durations in status bar.
"""
# If we are in the process of selecting multiple tracks, no-op here
if self.selecting_in_progress:
return
# Get the row number of all selected items and put into a set
# to deduplicate
sel_rows: Set[int] = set([item.row() for item in self.selectedItems()])
# If no rows are selected, we have nothing to do
if len(sel_rows) == 0:
self.musicmuster.lblSumPlaytime.setText("")
return
notes_rows: Set[int] = set(self._get_notes_rows())