Files
lintunes/lintunes/gui/cassette_art.py
T
trav 3a3d518092 v0.77.1: a cassetted song shows a little blue down-arrow instead of going grey
Asked for and waiting to download now reads as that: the button stays
pressed in, in its own face, with the tape nudged up-left and a blue
arrow in the corner (paler for a partly asked-for artist or album, faded
on hover to say a click lets it go). The guide's icons and friend-mode
screenshot are repainted with the same code.
2026-10-23 01:13:20 -07:00

172 lines
6.7 KiB
Python

"""Cassette's two little pictures, painted (no image files to ship or theme).
* The **cassette button**: a small embossed square with a tape glyph. Raised
when off; pressed in with a little blue down-arrow when on ("asked for,
on its way"); the arrow fades on hover over a pressed one, to say a click
would let it go. Partly asked for (an artist or album) is a paler
arrow. Functionally it's a checkbox — the tape is reserved for asking
friends for songs.
* The **UFO**: a friend's playlist. Light purple when followed, a grey
outline when not.
Plus friend mode's light purple, shared by everything that says "you're in
someone else's library".
"""
from __future__ import annotations
from PyQt6.QtCore import QPointF, QRectF, Qt
from PyQt6.QtGui import (
QBrush, QColor, QIcon, QLinearGradient, QPainter, QPainterPath, QPen,
QPixmap,
)
FRIEND_PURPLE = QColor("#cdb4f2") # button tint in friend mode
FRIEND_PURPLE_DARK = QColor("#9b76d6") # its border / pressed shade
UFO_PURPLE = QColor("#b38ee8")
OFF, ON, PARTIAL = "off", "on", "partial"
ASKED_BLUE = QColor("#2f7de1") # the "on its way" arrow
def friend_button_qss(selector: str = "QPushButton", padding: str = "3px 12px") -> str:
"""Light purple, with the same face as every other button."""
from lintunes import theme
return theme.button_face_qss(
selector, FRIEND_PURPLE,
extra=f"color: black; padding: {padding}; min-height: 0;")
def button_rect(cell: QRectF, margin: float = 2.0) -> QRectF:
"""The square the button occupies inside a cell (left-aligned)."""
side = max(8.0, min(cell.height(), cell.width()) - 2 * margin)
return QRectF(cell.left() + margin, cell.top() + (cell.height() - side) / 2,
side, side)
def paint_cassette_button(painter: QPainter, rect: QRectF, state: str,
hovered: bool, palette) -> None:
painter.save()
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
base = palette.button().color()
mid = palette.mid().color()
box = QRectF(rect).adjusted(0.5, 0.5, -0.5, -0.5)
radius = max(2.0, box.width() * 0.18)
gradient = QLinearGradient(box.topLeft(), box.bottomLeft())
if state == OFF:
top = base.lighter(112 if hovered else 106)
gradient.setColorAt(0, top)
gradient.setColorAt(1, base.darker(106))
else:
gradient.setColorAt(0, base.darker(118)) # pressed in: lit from below
gradient.setColorAt(1, base.darker(104))
painter.setBrush(QBrush(gradient))
painter.setPen(QPen(mid, 1))
painter.drawRoundedRect(box, radius, radius)
ink = QColor(palette.text().color())
ink.setAlpha(150)
if state == OFF:
_paint_tape(painter, box, ink)
else:
# the tape moves up and left to make room for the arrow
_paint_tape(painter, box.translated(-box.width() * 0.06,
-box.height() * 0.08), ink)
blue = QColor(ASKED_BLUE)
blue.setAlpha((255 if state == ON else 130) // (2 if hovered else 1))
_paint_arrow(painter, box, blue, base.darker(110))
painter.restore()
def _paint_arrow(painter: QPainter, box: QRectF, color: QColor,
halo: QColor):
"""A down-arrow in the bottom-right corner, ringed in the button's own
face so it stands off the tape behind it."""
w, h = box.width(), box.height()
cx = box.left() + w * 0.75
top, bottom = box.top() + h * 0.5, box.top() + h * 0.88
shaft, head = w * 0.08, w * 0.17
neck = bottom - h * 0.17
path = QPainterPath()
path.moveTo(cx - shaft, top)
path.lineTo(cx + shaft, top)
path.lineTo(cx + shaft, neck)
path.lineTo(cx + head, neck)
path.lineTo(cx, bottom)
path.lineTo(cx - head, neck)
path.lineTo(cx - shaft, neck)
path.closeSubpath()
line = max(1.0, w / 16)
painter.setBrush(Qt.BrushStyle.NoBrush)
painter.setPen(QPen(halo, line * 3, Qt.PenStyle.SolidLine,
Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin))
painter.drawPath(path)
painter.setPen(QPen(color, line, Qt.PenStyle.SolidLine,
Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin))
painter.setBrush(color)
painter.drawPath(path)
def _paint_tape(painter: QPainter, box: QRectF, ink: QColor):
"""A cassette: body, label window, two reels."""
w, h = box.width(), box.height()
body = QRectF(box.left() + w * 0.16, box.top() + h * 0.28,
w * 0.68, h * 0.46)
pen = QPen(ink, max(1.0, w / 16))
painter.setPen(pen)
painter.setBrush(Qt.BrushStyle.NoBrush)
painter.drawRoundedRect(body, body.height() * 0.15, body.height() * 0.15)
reel_r = body.height() * 0.17
cy = body.top() + body.height() * 0.45
for fx in (0.3, 0.7):
painter.drawEllipse(QPointF(body.left() + body.width() * fx, cy),
reel_r, reel_r)
# the bottom trapezoid where the tape shows
path = QPainterPath()
path.moveTo(body.left() + body.width() * 0.22, body.bottom())
path.lineTo(body.left() + body.width() * 0.3, body.bottom() - body.height() * 0.22)
path.lineTo(body.left() + body.width() * 0.7, body.bottom() - body.height() * 0.22)
path.lineTo(body.left() + body.width() * 0.78, body.bottom())
painter.drawPath(path)
def paint_ufo(painter: QPainter, rect: QRectF, color: QColor,
filled: bool = True) -> None:
painter.save()
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
w, h = rect.width(), rect.height()
pen = QPen(color.darker(135) if filled else color, max(1.0, w / 14))
painter.setPen(pen)
dome = QRectF(rect.left() + w * 0.32, rect.top() + h * 0.2,
w * 0.36, h * 0.4)
saucer = QRectF(rect.left() + w * 0.06, rect.top() + h * 0.38,
w * 0.88, h * 0.3)
if filled:
painter.setBrush(color.lighter(125))
else:
painter.setBrush(Qt.BrushStyle.NoBrush)
painter.drawChord(dome, 0, 180 * 16)
painter.setBrush(color if filled else Qt.BrushStyle.NoBrush)
painter.drawEllipse(saucer)
# three little lights
painter.setBrush(color.lighter(160) if filled else Qt.BrushStyle.NoBrush)
r = w * 0.045
for fx in (0.3, 0.5, 0.7):
painter.drawEllipse(QPointF(rect.left() + w * fx,
saucer.center().y() + saucer.height() * 0.1),
r, r)
painter.restore()
def ufo_icon(size: int = 32, followed: bool = True,
color: QColor | None = None) -> QIcon:
pixmap = QPixmap(size, size)
pixmap.fill(Qt.GlobalColor.transparent)
painter = QPainter(pixmap)
paint_ufo(painter, QRectF(1, 1, size - 2, size - 2),
color or (UFO_PURPLE if followed else QColor("#8a8a8a")),
filled=followed)
painter.end()
return QIcon(pixmap)