WIP V3: gap and bitrate column background working

This commit is contained in:
Keith Edmunds 2023-10-19 15:05:30 +01:00
parent bec336d2a3
commit 1c8fb05ffa
3 changed files with 42 additions and 38 deletions

View File

@ -80,6 +80,7 @@ class Config(object):
ROOT = os.environ.get('ROOT') or "/home/kae/music" ROOT = os.environ.get('ROOT') or "/home/kae/music"
IMPORT_DESTINATION = os.path.join(ROOT, "Singles") IMPORT_DESTINATION = os.path.join(ROOT, "Singles")
SCROLL_TOP_MARGIN = 3 SCROLL_TOP_MARGIN = 3
START_GAP_WARNING_THRESHOLD = 500
TEXT_NO_TRACK_NO_NOTE = "[Section header]" TEXT_NO_TRACK_NO_NOTE = "[Section header]"
TOD_TIME_FORMAT = "%H:%M:%S" TOD_TIME_FORMAT = "%H:%M:%S"
TRACK_TIME_FORMAT = "%H:%M:%S" TRACK_TIME_FORMAT = "%H:%M:%S"

View File

@ -10,6 +10,7 @@ from PyQt6.QtCore import (
) )
from PyQt6.QtGui import ( from PyQt6.QtGui import (
QBrush,
QColor, QColor,
QFont, QFont,
) )
@ -40,7 +41,6 @@ class Col(Enum):
class PlaylistRowData: class PlaylistRowData:
def __init__(self, plr: PlaylistRows) -> None: def __init__(self, plr: PlaylistRows) -> None:
""" """
Populate PlaylistRowData from database PlaylistRows record Populate PlaylistRowData from database PlaylistRows record
@ -80,7 +80,7 @@ class PlaylistModel(QAbstractTableModel):
def __init__(self, playlist_id: int, *args, **kwargs): def __init__(self, playlist_id: int, *args, **kwargs):
self.playlist_id = playlist_id self.playlist_id = playlist_id
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.playlist_rows: dict[int, PlaylistRowData] = {} self.playlist_rows: dict[int, PlaylistRowData] = {}
# self.current_row = None # self.current_row = None
# self.next_row = None # self.next_row = None
@ -91,20 +91,18 @@ class PlaylistModel(QAbstractTableModel):
self.refresh_data() self.refresh_data()
def __repr__(self) -> str: def __repr__(self) -> str:
return ( return f"<PlaylistModel: playlist_id={self.playlist_id}>"
f"<PlaylistModel: playlist_id={self.playlist_id}>"
)
def columnCount(self, parent: QModelIndex) -> int: def columnCount(self, parent: QModelIndex = QModelIndex()) -> int:
"""Standard function for view""" """Standard function for view"""
return 9 return 9
def data(self, index: QModelIndex, role: Qt.ItemDataRole.DisplayRole): # def data(self, index: QModelIndex, role: Qt.ItemDataRole.DisplayRole):
def data(self, index: QModelIndex, role: int = Qt.ItemDataRole.DisplayRole):
"""Return data to view""" """Return data to view"""
if not index.isValid() or not ( if not index.isValid() or not (0 <= index.row() < len(self.playlist_rows)):
0 <= index.row() < len(self.playlist_rows)):
return QVariant() return QVariant()
row = index.row() row = index.row()
@ -132,7 +130,7 @@ class PlaylistModel(QAbstractTableModel):
elif role == Qt.ItemDataRole.TextAlignmentRole: elif role == Qt.ItemDataRole.TextAlignmentRole:
pass pass
elif role == Qt.ItemDataRole.BackgroundRole: elif role == Qt.ItemDataRole.BackgroundRole:
pass return self.background_role(row, column, prd)
elif role == Qt.ItemDataRole.ForegroundRole: elif role == Qt.ItemDataRole.ForegroundRole:
pass pass
elif role == Qt.ItemDataRole.CheckStateRole: elif role == Qt.ItemDataRole.CheckStateRole:
@ -169,43 +167,49 @@ class PlaylistModel(QAbstractTableModel):
return QVariant() return QVariant()
def background_role(self, row: int, column: int, prd: PlaylistRowData) -> QBrush:
""" Return background setting """
if role == Qt.ItemDataRole.BackgroundRole: if column == Col.START_GAP.value:
if rowdata.path and file_is_unreadable(rowdata.path): if prd.start_gap >= Config.START_GAP_WARNING_THRESHOLD:
return QVariant(QColor(Config.COLOUR_UNREADABLE)) return QBrush(QColor(Config.COLOUR_LONG_START))
elif row == self.current_row: if column == Col.BITRATE.value:
return QVariant(QColor(Config.COLOUR_CURRENT_PLAYLIST)) if prd.bitrate < Config.BITRATE_LOW_THRESHOLD:
elif row == self.next_row: return QBrush(QColor(Config.COLOUR_BITRATE_LOW))
return QVariant(QColor(Config.COLOUR_NEXT_PLAYLIST)) elif prd.bitrate < Config.BITRATE_OK_THRESHOLD:
elif column == BITRATE: return QBrush(QColor(Config.COLOUR_BITRATE_MEDIUM))
if rowdata.bitrate: else:
if rowdata.bitrate < Config.BITRATE_LOW_THRESHOLD: return QBrush(QColor(Config.COLOUR_BITRATE_OK))
cell_colour = Config.COLOUR_BITRATE_LOW
elif rowdata.bitrate < Config.BITRATE_OK_THRESHOLD:
cell_colour = Config.COLOUR_BITRATE_MEDIUM
else:
cell_colour = Config.COLOUR_BITRATE_OK
return QVariant(QColor(cell_colour))
# Return a QColor, QIcon or QPixmap. QColor puts a coloured return QBrush()
# block to left of text
return QVariant() # if rowdata.path and file_is_unreadable(rowdata.path):
# return QVariant(QColor(Config.COLOUR_UNREADABLE))
# elif row == self.current_row:
# return QVariant(QColor(Config.COLOUR_CURRENT_PLAYLIST))
# elif row == self.next_row:
# return QVariant(QColor(Config.COLOUR_NEXT_PLAYLIST))
# elif column == BITRATE:
# if rowdata.bitrate:
# if rowdata.bitrate < Config.BITRATE_LOW_THRESHOLD:
# cell_colour = Config.COLOUR_BITRATE_LOW
# elif rowdata.bitrate < Config.BITRATE_OK_THRESHOLD:
# cell_colour = Config.COLOUR_BITRATE_MEDIUM
# else:
# cell_colour = Config.COLOUR_BITRATE_OK
# return QVariant(QColor(cell_colour))
# Text colour, not currently used
return QVariant()
if not rowdata.played: # if not rowdata.played:
font = QFont() # font = QFont()
font.setBold(True) # font.setBold(True)
return QVariant(font) # return QVariant(font)
return QVariant() # return QVariant()
def refresh_data(self): def refresh_data(self):
"""Populate dicts for data calls""" """Populate dicts for data calls"""
# Populate self.playlist_rows with playlist data # Populate self.playlist_rows with playlist data
with Session() as session: with Session() as session:
for p in PlaylistRows.deep_rows(session, self.playlist_id): for p in PlaylistRows.deep_rows(session, self.playlist_id):

View File

@ -136,7 +136,6 @@ class EscapeDelegate(QStyledItemDelegate):
class PlaylistTab(QTableView): class PlaylistTab(QTableView):
def __init__( def __init__(
self, self,
musicmuster: "Window", musicmuster: "Window",