Files
lintunes/lintunes/gui/scope_drawing.py
T

88 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""A small, still line drawing of an oscilloscope beside the Oscilloscope
style's settings, in the manner of an old electronics textbook: thin
dark-gray lines, the screen's dot graticule, a Lissajous loop on it and two
knobs underneath. It shows what the style is; it doesn't follow the settings.
"""
import math
from PyQt6.QtWidgets import QWidget, QSizePolicy
from PyQt6.QtCore import Qt, QPointF, QRectF, QSize
from PyQt6.QtGui import QPainter, QPen, QColor, QPolygonF
INK = 0.72 # how far the line color is from the background to the text
class ScopeDrawing(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setSizePolicy(QSizePolicy.Policy.Preferred,
QSizePolicy.Policy.Preferred)
self.setMinimumSize(150, 116)
def sizeHint(self):
return QSize(170, 128)
def _ink(self) -> QColor:
text = self.palette().windowText().color()
back = self.palette().window().color()
return QColor(*(round(b + (t - b) * INK) for t, b in (
(text.red(), back.red()), (text.green(), back.green()),
(text.blue(), back.blue()))))
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
ink = self._ink()
pen = QPen(ink, 1.0)
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
painter.setPen(pen)
painter.setBrush(Qt.BrushStyle.NoBrush)
# The case, 4:3, centered.
w, h = self.width() - 2, self.height() - 2
case_w = min(w, h * 4 / 3)
case = QRectF((self.width() - case_w) / 2, (self.height() - case_w * 3 / 4) / 2,
case_w, case_w * 3 / 4)
painter.drawRoundedRect(case, 6, 6)
# The screen: the left three quarters, with the knobs to its right.
pad = case.height() * 0.1
screen = QRectF(case.left() + pad, case.top() + pad,
case.width() * 0.68, case.height() - 2 * pad)
painter.drawRoundedRect(screen, 4, 4)
# The graticule: a dot at every division, 8 × 6.
dots = QPen(ink, 2.2)
dots.setCapStyle(Qt.PenCapStyle.RoundCap)
painter.setPen(dots)
inner = screen.adjusted(4, 4, -4, -4)
painter.drawPoints([QPointF(inner.left() + i * inner.width() / 8,
inner.top() + j * inner.height() / 6)
for i in range(1, 8) for j in range(1, 6)])
# A 3:2 Lissajous figure.
cx, cy = inner.center().x(), inner.center().y()
rx, ry = inner.width() * 0.38, inner.height() * 0.38
trace = QPen(ink, 1.3)
trace.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
painter.setPen(trace)
steps = 240
painter.drawPolyline(QPolygonF([
QPointF(cx + rx * math.sin(3 * t + math.pi / 2),
cy + ry * math.sin(2 * t + math.pi / 4))
for t in (2 * math.pi * k / steps for k in range(steps + 1))]))
# Two knobs, each with a pointer.
painter.setPen(pen)
knob_x = (screen.right() + case.right()) / 2
radius = min(case.right() - screen.right(), case.height()) * 0.2
for share, turn in ((0.32, -40), (0.68, 70)):
center = QPointF(knob_x, case.top() + case.height() * share)
painter.drawEllipse(center, radius, radius)
angle = math.radians(turn - 90)
painter.drawLine(center, QPointF(
center.x() + radius * 0.8 * math.cos(angle),
center.y() + radius * 0.8 * math.sin(angle)))
painter.end()