WIP: playlist refacton: start/end times

Clean up function. If next track is above current track, flow start
times around current track rather than resetting them at current
track.
This commit is contained in:
Keith Edmunds 2023-02-26 22:04:39 +00:00
parent 613fa4343b
commit 634637f42c

View File

@ -1411,6 +1411,17 @@ class PlaylistTab(QTableWidget):
return self.musicmuster.current_track.end_time
def _get_current_track_start_time(self) -> Optional[datetime]:
"""
Return current track start time or None if no current track
"""
current_track_row = self._get_current_track_row_number()
if current_track_row is None:
return None
return self.musicmuster.current_track.start_time
def _get_current_track_row_number(self) -> Optional[int]:
"""Return current track row or None"""
@ -2151,6 +2162,18 @@ class PlaylistTab(QTableWidget):
return self._set_item_text(row, START_TIME, time_str)
def _set_row_times(self, row: int, start: datetime,
duration: int) -> datetime:
"""
Set row start and end times, return end time
"""
self._set_row_start_time(row, start)
end_time = self._calculate_end_time(start, duration)
self._set_row_end_time(row, end_time)
return end_time
def _set_row_title(self, row: int,
title: Optional[str]) -> QTableWidgetItem:
"""
@ -2236,11 +2259,11 @@ class PlaylistTab(QTableWidget):
def _update_start_end_times(self) -> None:
""" Update track start and end times """
next_start_time = None
with Session() as session:
current_track_end_time = self._get_current_track_end_time()
current_track_row = self._get_current_track_row_number()
current_track_start_time = self._get_current_track_start_time()
next_start_time = None
next_track_row = self._get_next_track_row_number()
played_rows = self._get_played_rows(session)
@ -2251,60 +2274,52 @@ class PlaylistTab(QTableWidget):
current_track_row, next_track_row]:
continue
a_track_row = self._get_row_track_id(row) > 0
if not a_track_row:
# Get any timing from header row (that's all we need)
if self._get_row_track_id(row) == 0:
note_time = self._get_section_start_time(session, row)
if note_time:
next_start_time = note_time
# We have any timings from note; there's no track; go to
# next row
continue
# Here means we have a track. Skip if track is
# unreadable
# We have a track. Skip if it is unreadable
if not file_is_readable(self._get_row_path(row)):
continue
# Set next track start from end of current track
if row == next_track_row:
# If we have a current track, set this next track start
# time from it
if current_track_end_time:
self._set_row_start_time(row, current_track_end_time)
next_start_time = self._calculate_end_time(
current_track_end_time,
next_start_time = self._set_row_times(
row, current_track_end_time,
self._get_row_duration(row))
self._set_row_end_time(row, next_start_time)
continue
# Fall through to set times if next_start_time is
# set
# Else set track times below
if row == current_track_row:
track_start = self.musicmuster.current_track.start_time
if not track_start:
if not current_track_start_time:
continue
self._set_row_start_time(row, track_start)
next_start_time = self._calculate_end_time(
track_start, self._get_row_duration(row))
self._set_row_end_time(row, next_start_time)
self._set_row_start_time(row, current_track_start_time)
self._set_row_end_time(row, current_track_end_time)
# Next track may be above us so only reset
# next_start_time if it's not set
if not next_start_time:
next_start_time = current_track_end_time
continue
if not next_start_time:
# Clear any existing times
self._set_row_start_time(row, None)
self._set_row_end_time(row, None)
continue
# If we're between the current and next row, zero out
# times
if (
current_track_row and
next_track_row and
current_track_row < row < next_track_row
):
if (current_track_row and next_track_row and
current_track_row < row < next_track_row):
self._set_row_start_time(row, None)
self._set_row_end_time(row, None)
else:
self._set_row_start_time(row, next_start_time)
next_start_time = self._calculate_end_time(
next_start_time, self._get_row_duration(row))
self._set_row_end_time(row, next_start_time)
next_start_time = self._set_row_times(
row, next_start_time, self._get_row_duration(row))
def _wikipedia(self, row_number: int) -> None:
"""Look up passed row title in Wikipedia and display info tab"""