From f7f4cdc62264c2eb42c96dec81f7064ff116f0fb Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sat, 14 Dec 2024 12:01:41 +0000 Subject: [PATCH] Implement header row foreground colour --- app/dbtables.py | 1 + app/models.py | 18 +++++++++++++----- app/playlistmodel.py | 25 ++++++++++++++++++------- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/app/dbtables.py b/app/dbtables.py index 95945c6..2e03826 100644 --- a/app/dbtables.py +++ b/app/dbtables.py @@ -30,6 +30,7 @@ class NoteColoursTable(Model): substring: Mapped[str] = mapped_column(String(256), index=False) colour: Mapped[str] = mapped_column(String(21), index=False) enabled: Mapped[bool] = mapped_column(default=True, index=True) + foreground: Mapped[Optional[str]] = mapped_column(String(21), index=False) is_regex: Mapped[bool] = mapped_column(default=False, index=False) is_casesensitive: Mapped[bool] = mapped_column(default=False, index=False) order: Mapped[Optional[int]] = mapped_column(index=True) diff --git a/app/models.py b/app/models.py index d46e5d5..000811f 100644 --- a/app/models.py +++ b/app/models.py @@ -70,14 +70,17 @@ class NoteColours(dbtables.NoteColoursTable): return result @staticmethod - def get_colour(session: Session, text: str) -> Optional[str]: + def get_colour(session: Session, text: str, foreground: bool = False) -> Optional[str]: """ - Parse text and return colour string if matched, else empty string + Parse text and return background (foreground if foreground==True) colour + string if matched, else None + """ if not text: return None + match = False for rec in session.scalars( select(NoteColours) .filter(NoteColours.enabled.is_(True)) @@ -89,15 +92,20 @@ class NoteColours(dbtables.NoteColoursTable): flags |= re.IGNORECASE p = re.compile(rec.substring, flags) if p.match(text): - return rec.colour + match = True else: if rec.is_casesensitive: if rec.substring in text: - return rec.colour + match = True else: if rec.substring.lower() in text.lower(): - return rec.colour + match = True + if match: + if foreground: + return rec.foreground + else: + return rec.colour return None diff --git a/app/playlistmodel.py b/app/playlistmodel.py index dfa4e06..02dfdc8 100644 --- a/app/playlistmodel.py +++ b/app/playlistmodel.py @@ -147,9 +147,9 @@ class PlaylistModel(QAbstractTableModel): if self.is_header_row(row): # Check for specific header colouring with db.Session() as session: - note_colour = NoteColours.get_colour(session, rat.note) - if note_colour: - return QBrush(QColor(note_colour)) + note_background = NoteColours.get_colour(session, rat.note) + if note_background: + return QBrush(QColor(note_background)) else: return QBrush(QColor(Config.COLOUR_NOTES_PLAYLIST)) # Unreadable track file @@ -184,9 +184,9 @@ class PlaylistModel(QAbstractTableModel): if column == Col.NOTE.value: if rat.note: with db.Session() as session: - note_colour = NoteColours.get_colour(session, rat.note) - if note_colour: - return QBrush(QColor(note_colour)) + note_background = NoteColours.get_colour(session, rat.note) + if note_background: + return QBrush(QColor(note_background)) return QBrush() @@ -303,6 +303,7 @@ class PlaylistModel(QAbstractTableModel): int(Qt.ItemDataRole.DisplayRole): self.display_role, int(Qt.ItemDataRole.EditRole): self.edit_role, int(Qt.ItemDataRole.FontRole): self.font_role, + int(Qt.ItemDataRole.ForegroundRole): self.foreground_role, int(Qt.ItemDataRole.ToolTipRole): self.tooltip_role, } @@ -316,7 +317,6 @@ class PlaylistModel(QAbstractTableModel): Qt.ItemDataRole.WhatsThisRole, Qt.ItemDataRole.SizeHintRole, Qt.ItemDataRole.TextAlignmentRole, - Qt.ItemDataRole.ForegroundRole, Qt.ItemDataRole.CheckStateRole, Qt.ItemDataRole.InitialSortOrderRole, ]: @@ -447,6 +447,17 @@ class PlaylistModel(QAbstractTableModel): return QVariant() + def foreground_role(self, row: int, column: int, rat: RowAndTrack) -> QBrush: + """Return header foreground colour or QBrush() if none""" + + if self.is_header_row(row): + with db.Session() as session: + note_foreground = NoteColours.get_colour(session, rat.note, foreground=True) + if note_foreground: + return QBrush(QColor(note_foreground)) + + return QBrush() + def flags(self, index: QModelIndex) -> Qt.ItemFlag: """ Standard model flags