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

View File

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