Compare commits

..

No commits in common. "9d3743ceb5e6cbdbaec099d8482948f6156edb55" and "634637f42cbe77aca7a6ecc8a33ab55bd4501964" have entirely different histories.

2 changed files with 48 additions and 81 deletions

View File

@ -462,28 +462,6 @@ class PlaylistRows(Base):
# Ensure new row numbers are available to the caller # Ensure new row numbers are available to the caller
session.commit() session.commit()
@classmethod
def get_section_header_rows(cls, session: scoped_session,
playlist_id: int) -> List["PlaylistRows"]:
"""
Return a list of PlaylistRows that are section headers for this
playlist
"""
plrs = session.execute(
select(cls)
.where(
cls.playlist_id == playlist_id,
cls.track_id.is_(None),
(
cls.note.endswith("-") |
cls.note.endswith("+")
)
)
.order_by(cls.row_number)).scalars().all()
return plrs
@staticmethod @staticmethod
def get_track_plr(session: scoped_session, track_id: int, def get_track_plr(session: scoped_session, track_id: int,
playlist_id: int) -> Optional["PlaylistRows"]: playlist_id: int) -> Optional["PlaylistRows"]:
@ -528,25 +506,21 @@ class PlaylistRows(Base):
return plrs return plrs
@classmethod @classmethod
def get_rows_with_tracks( def get_rows_with_tracks(cls, session: scoped_session,
cls, session: scoped_session, playlist_id: int, playlist_id: int) -> List["PlaylistRows"]:
from_row: Optional[int] = None,
to_row: Optional[int] = None) -> List["PlaylistRows"]:
""" """
For passed playlist, return a list of rows that For passed playlist, return a list of rows that
contain tracks contain tracks
""" """
query = select(cls).where( plrs = session.execute(
cls.playlist_id == playlist_id, select(cls)
cls.track_id.is_not(None) .where(
) cls.playlist_id == playlist_id,
if from_row is not None: cls.track_id.is_not(None)
query = query.where(cls.row_number >= from_row) )
if to_row is not None: .order_by(cls.row_number)
query = query.where(cls.row_number <= to_row) ).scalars().all()
plrs = session.execute((query).order_by(cls.row_number)).scalars().all()
return plrs return plrs

View File

@ -1580,6 +1580,28 @@ class PlaylistTab(QTableWidget):
return userdata_item.data(role) return userdata_item.data(role)
def _get_section_start_time(self, session: scoped_session,
row: int) -> Optional[datetime]:
"""
Parse section header for a start time.
Return None if:
- it's not a section header row or
- we can't parse a time from it
Otherwise return datetime from header.
"""
# If we have a track_id, we're not a section header
if self._get_row_track_id(row):
return None
# Check for start time in note. Extract note text from database
# to ignore section timings.
plr = session.get(PlaylistRows, self._get_playlistrow_id(row))
if plr and plr.note:
return self._get_note_text_time(plr.note)
else:
return None
def _get_selected_row(self) -> Optional[int]: def _get_selected_row(self) -> Optional[int]:
"""Return row number of first selected row, or None if none selected""" """Return row number of first selected row, or None if none selected"""
@ -1883,7 +1905,7 @@ class PlaylistTab(QTableWidget):
# If only one row is selected and it's a track row, show # If only one row is selected and it's a track row, show
# Wikipedia page for that track # Wikipedia page for that track
if len(selected_rows) == 1: if len(selected_rows) == 1:
QTimer.singleShot(0, lambda: self._wikipedia(selected_rows[0])) self._wikipedia(selected_rows[0])
ms = 0 ms = 0
for row in selected_rows: for row in selected_rows:
@ -2198,9 +2220,13 @@ class PlaylistTab(QTableWidget):
self.musicmuster.tabInfolist.open_in_songfacts(title) self.musicmuster.tabInfolist.open_in_songfacts(title)
def _update_note_text(self, playlist_row: PlaylistRows, def _update_note_text(self, session: scoped_session,
new_text: str) -> None: playlist_row: PlaylistRows,
"""Update note text""" additional_text: str) -> None:
"""Append additional_text to row display"""
if not playlist_row.row_number:
return
# Column to update is either HEADER_NOTES_COLUMN for a section # Column to update is either HEADER_NOTES_COLUMN for a section
# header or the appropriate row_notes column for a track row # header or the appropriate row_notes column for a track row
@ -2209,7 +2235,13 @@ class PlaylistTab(QTableWidget):
else: else:
column = HEADER_NOTES_COLUMN column = HEADER_NOTES_COLUMN
_ = self._set_item_text(playlist_row.row_number, column, new_text) # Update text
if playlist_row.note:
new_text = playlist_row.note + additional_text
else:
new_text = additional_text
_ = self._set_row_note(session, playlist_row.row_number, new_text)
def _update_row(self, session, row: int, track: Tracks) -> None: def _update_row(self, session, row: int, track: Tracks) -> None:
""" """
@ -2224,46 +2256,10 @@ class PlaylistTab(QTableWidget):
self.update_display(session) self.update_display(session)
def _update_section_headers(self, session: scoped_session) -> None:
"""
Update section headers with run time of section
"""
header_rows = []
# Get section header PlaylistRows
plrs = PlaylistRows.get_section_header_rows(session, self.playlist_id)
for plr in plrs:
if plr.note.endswith("+"):
header_rows.append(plr)
else:
try:
from_plr = header_rows.pop()
except IndexError:
pass
# section runs from from_plr to plr
from_row = from_plr.row_number
plr_tracks = PlaylistRows.get_rows_with_tracks(
session, self.playlist_id, from_row, plr.row_number)
total_time = 0
total_time = sum([a.track.duration for a in plr_tracks])
time_str = self._get_section_timing_string(total_time)
self._update_note_text(from_plr, from_plr.note + time_str)
# Update section end
if plr.note.strip() == "-":
new_text = (
"[End " + from_plr.note.strip()[:-1].strip() + "]"
)
self._update_note_text(plr, new_text)
def _update_start_end_times(self) -> None: def _update_start_end_times(self) -> None:
""" Update track start and end times """ """ Update track start and end times """
with Session() as session: with Session() as session:
section_start_rows = []
current_track_end_time = self._get_current_track_end_time() current_track_end_time = self._get_current_track_end_time()
current_track_row = self._get_current_track_row_number() current_track_row = self._get_current_track_row_number()
current_track_start_time = self._get_current_track_start_time() current_track_start_time = self._get_current_track_start_time()
@ -2280,8 +2276,7 @@ class PlaylistTab(QTableWidget):
# Get any timing from header row (that's all we need) # Get any timing from header row (that's all we need)
if self._get_row_track_id(row) == 0: if self._get_row_track_id(row) == 0:
note_time = self._get_note_text_time( note_time = self._get_section_start_time(session, row)
self._get_row_note(row))
if note_time: if note_time:
next_start_time = note_time next_start_time = note_time
continue continue
@ -2326,8 +2321,6 @@ class PlaylistTab(QTableWidget):
next_start_time = self._set_row_times( next_start_time = self._set_row_times(
row, next_start_time, self._get_row_duration(row)) row, next_start_time, self._get_row_duration(row))
self._update_section_headers(session)
def _wikipedia(self, row_number: int) -> None: def _wikipedia(self, row_number: int) -> None:
"""Look up passed row title in Wikipedia and display info tab""" """Look up passed row title in Wikipedia and display info tab"""