Implement header row foreground colour

This commit is contained in:
Keith Edmunds 2024-12-14 12:01:41 +00:00
parent b7b825f0ef
commit f7f4cdc622
3 changed files with 32 additions and 12 deletions

View File

@ -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)

View File

@ -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

View File

@ -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