forked from trav/lintunes
195 lines
6.3 KiB
Python
195 lines
6.3 KiB
Python
"""Hand-painted transport glyphs (prev/play/pause/next/shuffle) in any color,
|
|
so buttons can flash the theme highlight while pressed."""
|
|
|
|
from PyQt6.QtCore import Qt, QPointF, QRectF
|
|
from PyQt6.QtGui import (QIcon, QPixmap, QPainter, QPainterPath, QPen, QColor,
|
|
QPolygonF)
|
|
|
|
|
|
_cache: dict[tuple, QIcon] = {}
|
|
|
|
SIZE = 20
|
|
|
|
|
|
def transport_icon(kind: str, color: QColor, size: int = SIZE) -> QIcon:
|
|
"""A glyph painted at `size` px. The drawers work in a fixed SIZE-square
|
|
coordinate space and the painter is scaled to fit, so a bigger icon is
|
|
re-rendered crisply rather than being a blown-up 20px pixmap."""
|
|
key = (kind, color.name(), size)
|
|
if key in _cache:
|
|
return _cache[key]
|
|
pixmap = QPixmap(size, size)
|
|
pixmap.fill(Qt.GlobalColor.transparent)
|
|
painter = QPainter(pixmap)
|
|
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
|
if size != SIZE:
|
|
painter.scale(size / SIZE, size / SIZE)
|
|
painter.setPen(Qt.PenStyle.NoPen)
|
|
painter.setBrush(color)
|
|
_DRAWERS[kind](painter, color)
|
|
painter.end()
|
|
icon = QIcon(pixmap)
|
|
_cache[key] = icon
|
|
return icon
|
|
|
|
|
|
def _play(p, _color):
|
|
p.drawPolygon(QPolygonF([QPointF(5, 3), QPointF(17, 10), QPointF(5, 17)]))
|
|
|
|
|
|
def _pause(p, _color):
|
|
p.drawRect(QRectF(5, 3.5, 3.6, 13))
|
|
p.drawRect(QRectF(11.4, 3.5, 3.6, 13))
|
|
|
|
|
|
def _next(p, _color):
|
|
# Skip-to-next: two triangles plus a trailing bar (bare triangles = ffwd).
|
|
p.drawPolygon(QPolygonF([QPointF(2, 4), QPointF(9, 10), QPointF(2, 16)]))
|
|
p.drawPolygon(QPolygonF([QPointF(9, 4), QPointF(16, 10), QPointF(9, 16)]))
|
|
p.drawRect(QRectF(16.5, 4, 2, 12))
|
|
|
|
|
|
def _previous(p, _color):
|
|
# Skip-to-previous: a leading bar plus two triangles (mirror of _next).
|
|
p.drawRect(QRectF(1.5, 4, 2, 12))
|
|
p.drawPolygon(QPolygonF([QPointF(11, 4), QPointF(4, 10), QPointF(11, 16)]))
|
|
p.drawPolygon(QPolygonF([QPointF(18, 4), QPointF(11, 10), QPointF(18, 16)]))
|
|
|
|
|
|
def _shuffle(p, color):
|
|
pen = QPen(color, 1.8)
|
|
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
|
p.setPen(pen)
|
|
p.setBrush(Qt.BrushStyle.NoBrush)
|
|
# Two crossing strands
|
|
p.drawLine(QPointF(2, 6), QPointF(6, 6))
|
|
p.drawLine(QPointF(6, 6), QPointF(13, 14))
|
|
p.drawLine(QPointF(13, 14), QPointF(16, 14))
|
|
p.drawLine(QPointF(2, 14), QPointF(6, 14))
|
|
p.drawLine(QPointF(6, 14), QPointF(13, 6))
|
|
p.drawLine(QPointF(13, 6), QPointF(16, 6))
|
|
# Arrowheads
|
|
p.setPen(Qt.PenStyle.NoPen)
|
|
p.setBrush(color)
|
|
p.drawPolygon(QPolygonF([QPointF(15, 3), QPointF(19, 6), QPointF(15, 9)]))
|
|
p.drawPolygon(QPolygonF([QPointF(15, 11), QPointF(19, 14), QPointF(15, 17)]))
|
|
|
|
|
|
def _repeat(p, color):
|
|
"""iTunes' repeat: a loop of two strands with rounded far corners, the
|
|
top one arrowed to the right, the bottom one back to the left."""
|
|
pen = QPen(color, 1.8)
|
|
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
|
p.setPen(pen)
|
|
p.setBrush(Qt.BrushStyle.NoBrush)
|
|
top = QPainterPath(QPointF(3, 11.5))
|
|
top.lineTo(3, 9)
|
|
top.quadTo(3, 6, 6, 6)
|
|
top.lineTo(14, 6)
|
|
bottom = QPainterPath(QPointF(17, 8.5))
|
|
bottom.lineTo(17, 11)
|
|
bottom.quadTo(17, 14, 14, 14)
|
|
bottom.lineTo(6, 14)
|
|
p.drawPath(top)
|
|
p.drawPath(bottom)
|
|
p.setPen(Qt.PenStyle.NoPen)
|
|
p.setBrush(color)
|
|
p.drawPolygon(QPolygonF([QPointF(13.5, 2.8), QPointF(18, 6), QPointF(13.5, 9.2)]))
|
|
p.drawPolygon(QPolygonF([QPointF(6.5, 10.8), QPointF(2, 14), QPointF(6.5, 17.2)]))
|
|
|
|
|
|
def _repeat_one(p, color):
|
|
"""Repeat with a "1" inside the loop."""
|
|
_repeat(p, color)
|
|
pen = QPen(color, 1.9)
|
|
pen.setCapStyle(Qt.PenCapStyle.FlatCap)
|
|
pen.setJoinStyle(Qt.PenJoinStyle.MiterJoin)
|
|
p.setPen(pen)
|
|
p.setBrush(Qt.BrushStyle.NoBrush)
|
|
p.drawPolyline(QPolygonF([QPointF(8.6, 8.9), QPointF(10.6, 7.4),
|
|
QPointF(10.6, 12.6)]))
|
|
|
|
|
|
def _cast(p, color):
|
|
"""The standard cast glyph: a screen with signal arcs at its lower left.
|
|
The screen is lit rather than empty — it is only ever drawn while a device
|
|
is actually receiving."""
|
|
pen = QPen(color, 1.6)
|
|
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
|
p.setPen(pen)
|
|
p.setBrush(Qt.BrushStyle.NoBrush)
|
|
# Screen, its lower-left corner left open for the arcs.
|
|
p.drawPolyline(QPolygonF([
|
|
QPointF(6.5, 15.5), QPointF(17.5, 15.5), QPointF(17.5, 4.5),
|
|
QPointF(3.5, 4.5), QPointF(3.5, 7.0),
|
|
]))
|
|
p.setPen(Qt.PenStyle.NoPen)
|
|
p.setBrush(color)
|
|
p.drawRect(QRectF(8.5, 8.0, 7.2, 5.8))
|
|
# Two arcs radiating from the corner (16ths of a degree, 0..90).
|
|
p.setPen(pen)
|
|
p.setBrush(Qt.BrushStyle.NoBrush)
|
|
for radius in (4.5, 8.0):
|
|
p.drawArc(QRectF(3.5 - radius, 15.5 - radius, radius * 2, radius * 2),
|
|
0, 90 * 16)
|
|
# The corner dot the arcs radiate from.
|
|
p.setPen(Qt.PenStyle.NoPen)
|
|
p.setBrush(color)
|
|
p.drawEllipse(QPointF(3.8, 15.2), 1.5, 1.5)
|
|
|
|
|
|
def _airplay(p, color):
|
|
"""The AirPlay glyph: a screen whose bottom edge opens around a solid
|
|
triangle rising into it."""
|
|
pen = QPen(color, 1.6)
|
|
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
|
pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
|
|
p.setPen(pen)
|
|
p.setBrush(Qt.BrushStyle.NoBrush)
|
|
p.drawPolyline(QPolygonF([
|
|
QPointF(6.0, 14.0), QPointF(3.5, 14.0), QPointF(3.5, 3.5),
|
|
QPointF(16.5, 3.5), QPointF(16.5, 14.0), QPointF(14.0, 14.0),
|
|
]))
|
|
p.setPen(Qt.PenStyle.NoPen)
|
|
p.setBrush(color)
|
|
p.drawPolygon(QPolygonF([
|
|
QPointF(10.0, 11.0), QPointF(14.5, 17.0), QPointF(5.5, 17.0),
|
|
]))
|
|
|
|
|
|
def _sun(p, color):
|
|
"""Light mode's glyph: a disc and eight rays."""
|
|
import math
|
|
p.drawEllipse(QPointF(10, 10), 3.6, 3.6)
|
|
pen = QPen(color, 1.8)
|
|
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
|
p.setPen(pen)
|
|
for i in range(8):
|
|
a = i * math.pi / 4
|
|
p.drawLine(QPointF(10 + 6 * math.cos(a), 10 + 6 * math.sin(a)),
|
|
QPointF(10 + 8.4 * math.cos(a), 10 + 8.4 * math.sin(a)))
|
|
|
|
|
|
def _moon(p, _color):
|
|
"""Dark mode's glyph: a crescent."""
|
|
disc = QPainterPath()
|
|
disc.addEllipse(QPointF(10, 10), 7.5, 7.5)
|
|
bite = QPainterPath()
|
|
bite.addEllipse(QPointF(13.5, 7), 6.5, 6.5)
|
|
p.drawPath(disc.subtracted(bite))
|
|
|
|
|
|
_DRAWERS = {
|
|
"play": _play,
|
|
"pause": _pause,
|
|
"next": _next,
|
|
"previous": _previous,
|
|
"shuffle": _shuffle,
|
|
"repeat": _repeat,
|
|
"repeat_one": _repeat_one,
|
|
"cast": _cast,
|
|
"airplay": _airplay,
|
|
"sun": _sun,
|
|
"moon": _moon,
|
|
}
|