From 30d8b0d5c846d134ccc5de3e289242229fcc41b6 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 19 Jul 2024 15:58:58 +0100 Subject: [PATCH] Rework track hiding logic Fixes #248 --- app/playlistmodel.py | 114 +++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 59 deletions(-) diff --git a/app/playlistmodel.py b/app/playlistmodel.py index 456197d..f0817b7 100644 --- a/app/playlistmodel.py +++ b/app/playlistmodel.py @@ -1516,71 +1516,67 @@ class PlaylistProxyModel(QSortFilterProxyModel): def filterAcceptsRow(self, source_row: int, source_parent: QModelIndex) -> bool: """ - Subclass to filter by played status + Subclass to filter by played status. Return True to show this row, False to hide it. """ if self.source_model.played_tracks_hidden: if self.source_model.is_played_row(source_row): - # Don't hide current or next track - with db.Session() as session: - if track_sequence.next: - next_plr = session.get(PlaylistRows, track_sequence.next.plr_id) - if ( - next_plr - and next_plr.plr_rownum == source_row - and next_plr.playlist_id == self.source_model.playlist_id - ): - return True - if track_sequence.current: - now_plr = session.get( - PlaylistRows, track_sequence.current.plr_id - ) - if ( - now_plr - and now_plr.plr_rownum == source_row - and now_plr.playlist_id == self.source_model.playlist_id - ): - return True - # Don't hide previous track until - # HIDE_AFTER_PLAYING_OFFSET milliseconds after - # current track has started - if track_sequence.previous: - previous_plr = session.get( - PlaylistRows, track_sequence.previous.plr_id - ) - if ( - track_sequence.current - and previous_plr - and previous_plr.plr_rownum == source_row - and previous_plr.playlist_id - == self.source_model.playlist_id - ): - if track_sequence.current.start_time: - if dt.datetime.now() > ( - track_sequence.current.start_time - + dt.timedelta( - milliseconds=Config.HIDE_AFTER_PLAYING_OFFSET - ) - ): - return False - else: - # Invalidate this row in - # HIDE_AFTER_PLAYING_OFFSET and a - # bit milliseconds - # so that it hides then - add 100mS - # on so that it if clause above it - # true next time through. - QTimer.singleShot( - Config.HIDE_AFTER_PLAYING_OFFSET + 100, - lambda: self.source_model.invalidate_row( - source_row - ), - ) - return True - else: - return True + # Don't hide current track + if ( + track_sequence.current + and track_sequence.current.playlist_id == self.source_model.playlist_id + and track_sequence.current.row_number == source_row + ): + return True + # Don't hide next track + if ( + track_sequence.next + and track_sequence.next.playlist_id == self.source_model.playlist_id + and track_sequence.next.row_number == source_row + ): + return True + + # Handle previous track + if track_sequence.previous: + if ( + track_sequence.previous.playlist_id != self.source_model.playlist_id + or track_sequence.previous.row_number != source_row + ): + # This row isn't our previous track: hide it + return False + if track_sequence.current and track_sequence.current.start_time: + # This row is our previous track. Don't hide it + # until HIDE_AFTER_PLAYING_OFFSET milliseconds + # after current track has started + if ( + track_sequence.current.start_time + and dt.datetime.now() > ( + track_sequence.current.start_time + + dt.timedelta(milliseconds=Config.HIDE_AFTER_PLAYING_OFFSET) + ) + ): + return False + else: + # Invalidate this row in + # HIDE_AFTER_PLAYING_OFFSET and a bit + # milliseconds so that it hides then. We add + # 100mS on so that the if clause above is + # true next time through. + QTimer.singleShot( + Config.HIDE_AFTER_PLAYING_OFFSET + 100, + lambda: self.source_model.invalidate_row( + source_row + ), + ) + return True + # Next track not playing yet so don't hide previous + else: + return True + + # No previous track so hide this played track immediately return False + return super().filterAcceptsRow(source_row, source_parent) def set_incremental_search(self, search_string: str) -> None: