From b3262b2edec7b1ee36fb4860da7c3b217984df3d Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Wed, 8 Nov 2023 18:15:57 +0000 Subject: [PATCH] WIP V3: track start/end times working --- app/playlistmodel.py | 87 ++++++++++++++++++++++++++++++++------------ 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/app/playlistmodel.py b/app/playlistmodel.py index 713df1f..482e8a2 100644 --- a/app/playlistmodel.py +++ b/app/playlistmodel.py @@ -17,7 +17,7 @@ from PyQt6.QtGui import ( from classes import track_sequence, MusicMusterSignals, PlaylistTrack from config import Config from dbconfig import scoped_session, Session -from helpers import file_is_unreadable, get_embedded_time +from helpers import file_is_unreadable, get_embedded_time, ms_to_mmss from log import log from models import Playdates, PlaylistRows, Tracks @@ -250,16 +250,6 @@ class PlaylistModel(QAbstractTableModel): ) return - # Update display - self.invalidate_row(row_number) - - # Update track times - self.playlist_rows[row_number].start_time = datetime.now() - self.playlist_rows[row_number].end_time = datetime.now() + timedelta( - milliseconds=self.playlist_rows[row_number].duration - ) - self.update_track_times() - # Update Playdates in database with Session() as session: Playdates(session, track_sequence.now.track_id) @@ -268,6 +258,16 @@ class PlaylistModel(QAbstractTableModel): plr.played = True self.refresh_row(session, plr.plr_rownum) + # Update track times + self.playlist_rows[row_number].start_time = datetime.now() + self.playlist_rows[row_number].end_time = datetime.now() + timedelta( + milliseconds=self.playlist_rows[row_number].duration + ) + # Update colour and times for current row + self.invalidate_row(row_number) + # Update all other track times + self.update_track_times() + # Find next track # Get all unplayed track rows next_row = None @@ -279,9 +279,7 @@ class PlaylistModel(QAbstractTableModel): if unplayed_rows: try: # Find next row after current track - next_row = min( - [a for a in unplayed_rows if a > track_sequence.now.plr_rownum] - ) + next_row = min([a for a in unplayed_rows if a > row_number]) except ValueError: # Find first unplayed track next_row = min(unplayed_rows) @@ -352,7 +350,7 @@ class PlaylistModel(QAbstractTableModel): if column == Col.ARTIST.value: return QVariant(prd.artist) if column == Col.DURATION.value: - return QVariant(prd.duration) + return QVariant(ms_to_mmss(prd.duration)) if column == Col.START_TIME.value: if prd.start_time: return QVariant(prd.start_time.strftime(Config.TRACK_TIME_FORMAT)) @@ -666,6 +664,7 @@ class PlaylistModel(QAbstractTableModel): track_sequence.next.set_plr(session, plr) self.signals.next_track_changed_signal.emit() self.invalidate_row(row_number) + self.update_track_times() def setData( self, index: QModelIndex, value: QVariant, role: int = Qt.ItemDataRole.EditRole @@ -719,23 +718,53 @@ class PlaylistModel(QAbstractTableModel): """ next_start_time: Optional[datetime] = None + update_rows: List[int] = [] for row_number in range(len(self.playlist_rows)): - plr = self.playlist_rows[row_number] + prd = self.playlist_rows[row_number] # Reset start_time if this is the current row if row_number == track_sequence.now.plr_rownum: - next_start_time = plr.end_time = track_sequence.now.end_time + # Start/end times for current track are set in current_track_started + if not next_start_time: + next_start_time = prd.end_time + continue + + # Set start time for next row if we have a current track + current_end_time = track_sequence.now.end_time + if row_number == track_sequence.next.plr_rownum and current_end_time: + prd.start_time = current_end_time + prd.end_time = current_end_time + timedelta(milliseconds=prd.duration) + next_start_time = prd.end_time + update_rows.append(row_number) continue # Don't update times for tracks that have been played - if plr.played: + if prd.played: continue - # Reset start time if timing in hearer or at current track - if not plr.path: + # Don't schedule unplayable tracks + if file_is_unreadable(prd.path): + continue + + # If we're between the current and next row, zero out + # times + if ( + track_sequence.now.plr_rownum is not None + and track_sequence.next.plr_rownum is not None + and track_sequence.now.plr_rownum + < row_number + < track_sequence.next.plr_rownum + ): + prd.start_time = None + prd.end_time = None + update_rows.append(row_number) + continue + + # Reset start time if timing in header or at current track + if not prd.path: # This is a header row - header_time = get_embedded_time(plr.note) + header_time = get_embedded_time(prd.note) if header_time: next_start_time = header_time else: @@ -743,7 +772,19 @@ class PlaylistModel(QAbstractTableModel): # start time if next_start_time is None: continue - plr.start_time = next_start_time - next_start_time = plr.end_time = next_start_time + timedelta( + if prd.start_time != next_start_time: + prd.start_time = next_start_time + update_rows.append(row_number) + next_start_time += timedelta( milliseconds=self.playlist_rows[row_number].duration ) + if prd.end_time != next_start_time: + prd.end_time = next_start_time + update_rows.append(row_number) + + # Update start/stop times of rows that have changed + for updated_row in update_rows: + self.dataChanged.emit( + self.index(updated_row, Col.START_TIME.value), + self.index(updated_row, Col.END_TIME.value), + )