WIP V3: move selected tracks works

This commit is contained in:
Keith Edmunds 2023-11-22 19:57:14 +00:00
parent 223fb3bdec
commit 48b180e280
2 changed files with 36 additions and 72 deletions

View File

@ -924,93 +924,49 @@ class Window(QMainWindow, Ui_MainWindow):
self.signals.search_wikipedia_signal.emit(track_info.title) self.signals.search_wikipedia_signal.emit(track_info.title)
def move_playlist_rows( def move_playlist_rows(self, row_numbers: List[int]) -> None:
self, session: scoped_session, playlistrows: Sequence[PlaylistRows]
) -> None:
""" """
Move passed playlist rows to another playlist Move passed playlist rows to another playlist
Actions required:
- exclude current/next tracks from being moved
- identify destination playlist
- update playlist for the rows in the database
- remove them from the display
- update destination playlist display if loaded
""" """
# Remove current/next rows from list
plrs_to_move = [
plr
for plr in playlistrows
if plr.id not in [track_sequence.now.plr_id, track_sequence.next.plr_id]
]
rows_to_delete = [
plr.plr_rownum for plr in plrs_to_move if plr.plr_rownum is not None
]
if not rows_to_delete:
return
# Identify destination playlist # Identify destination playlist
playlists = [] playlists = []
visible_tab = self.active_tab() visible_tab = self.active_tab()
source_playlist_id = visible_tab.playlist_id source_playlist_id = visible_tab.playlist_id
for playlist in Playlists.get_all(session): with Session() as session:
if playlist.id == source_playlist_id: for playlist in Playlists.get_all(session):
continue if playlist.id == source_playlist_id:
continue
else:
playlists.append(playlist)
dlg = SelectPlaylistDialog(self, playlists=playlists, session=session)
dlg.exec()
if not dlg.playlist:
return
to_playlist_id = dlg.playlist.id
# Get row number in destination playlist
last_row = PlaylistRows.get_last_used_row(session, to_playlist_id)
if last_row is not None:
to_row = last_row + 1
else: else:
playlists.append(playlist) to_row = 0
dlg = SelectPlaylistDialog(self, playlists=playlists, session=session) # Move rows
dlg.exec() self.active_model().move_rows_between_playlists(row_numbers, to_row, to_playlist_id)
if not dlg.playlist:
return
destination_playlist_id = dlg.playlist.id
# Update destination playlist in the database
last_row = PlaylistRows.get_last_used_row(session, destination_playlist_id)
if last_row is not None:
next_row = last_row + 1
else:
next_row = 0
for plr in plrs_to_move:
plr.plr_rownum = next_row
next_row += 1
plr.playlist_id = destination_playlist_id
# Reset played as it's not been played on this playlist
plr.played = False
session.commit()
# Remove moved rows from display and save visible playlist
visible_tab.remove_rows(rows_to_delete)
visible_tab.save_playlist(session)
# Disable sort undo
self.sort_undo: List[int] = []
# Update destination playlist_tab if visible (if not visible, it
# will be re-populated when it is opened)
destination_playlist_tab = None
for tab in range(self.tabPlaylist.count()):
if self.tabPlaylist.widget(tab).playlist_id == dlg.playlist.id:
destination_playlist_tab = self.tabPlaylist.widget(tab)
break
if destination_playlist_tab:
destination_playlist_tab.populate_display(session, dlg.playlist.id)
def move_selected(self) -> None: def move_selected(self) -> None:
""" """
Move selected rows to another playlist Move selected rows to another playlist
""" """
with Session() as session: selected_rows = self.active_tab().get_selected_rows()
selected_plrs = self.active_tab().get_selected_playlistrows(session) if not selected_rows:
if not selected_plrs: return
return
self.move_playlist_rows(session, selected_plrs) self.move_playlist_rows(selected_rows)
def move_unplayed(self) -> None: def move_unplayed(self) -> None:
""" """

View File

@ -422,6 +422,8 @@ class PlaylistModel(QAbstractTableModel):
if playlist_id != self.playlist_id: if playlist_id != self.playlist_id:
return return
with Session() as session:
self.refresh_data(session)
super().endResetModel() super().endResetModel()
def get_duplicate_rows(self) -> List[int]: def get_duplicate_rows(self) -> List[int]:
@ -809,6 +811,10 @@ class PlaylistModel(QAbstractTableModel):
# the highest rows first so the lower row numbers are unchanged # the highest rows first so the lower row numbers are unchanged
row_groups = self._reversed_contiguous_row_groups(from_rows) row_groups = self._reversed_contiguous_row_groups(from_rows)
next_to_row = to_row_number next_to_row = to_row_number
# Prepare destination playlist for a reset
self.signals.begin_reset_model_signal.emit(to_playlist_id)
with Session() as session: with Session() as session:
# Make room in destination playlist # Make room in destination playlist
max_destination_row_number = PlaylistRows.get_last_used_row( max_destination_row_number = PlaylistRows.get_last_used_row(
@ -822,8 +828,6 @@ class PlaylistModel(QAbstractTableModel):
session, to_playlist_id, to_row_number, len(from_rows) session, to_playlist_id, to_row_number, len(from_rows)
) )
# Prepare destination playlist for a reset
self.signals.begin_reset_model_signal.emit(to_playlist_id)
for row_group in row_groups: for row_group in row_groups:
super().beginRemoveRows(QModelIndex(), min(row_group), max(row_group)) super().beginRemoveRows(QModelIndex(), min(row_group), max(row_group))
for plr in PlaylistRows.plrids_to_plrs( for plr in PlaylistRows.plrids_to_plrs(
@ -831,17 +835,21 @@ class PlaylistModel(QAbstractTableModel):
self.playlist_id, self.playlist_id,
[self.playlist_rows[a].plrid for a in row_group], [self.playlist_rows[a].plrid for a in row_group],
): ):
if plr.id == track_sequence.now.plr_id:
# Don't move current track
continue
plr.playlist_id = to_playlist_id plr.playlist_id = to_playlist_id
plr.plr_rownum = next_to_row plr.plr_rownum = next_to_row
next_to_row += 1 next_to_row += 1
self.refresh_data(session) self.refresh_data(session)
super().endRemoveRows() super().endRemoveRows()
self.signals.end_reset_model_signal.emit(to_playlist_id)
# We need to remove gaps in row numbers after tracks have # We need to remove gaps in row numbers after tracks have
# moved. # moved.
PlaylistRows.fixup_rownumbers(session, self.playlist_id) PlaylistRows.fixup_rownumbers(session, self.playlist_id)
self.refresh_data(session) self.refresh_data(session)
# Reset of model must come after session has been closed
self.signals.end_reset_model_signal.emit(to_playlist_id)
self.update_track_times() self.update_track_times()
def open_in_audacity(self, row_number: int) -> None: def open_in_audacity(self, row_number: int) -> None: