Files
cnmcdee 6d2dae20a1 Phase 6 fix: 14/16-segment maps + geometry verbatim from C
The draft segment code used its own segment ids (h/i centre
verticals, j/k/l/m diagonals) and drew diagonals as full-length
round-capped strokes, so I/K/M/V/W were wrong and every glyph was
swamped by a dark X blob ("HELLO" unreadable in the showcase
screenshot). This mirrors the C widgets exactly: segment_map()
strings from InstSeg14/16Display5.h, C ids (i/l centre verticals,
h/j/k/m diagonals), C draw_segment geometry (inset bars, hh/hv
verticals) and C draw_diag polygons that stop short of the centre.
Adds per-segment probe tests: every 14-seg glyph vs C map, 16-seg
M/W clean forms, 7-seg 8 vs 1.
2026-12-01 04:03:26 +00:00

1935 lines
68 KiB
Python

"""Instrumentation widgets (66): Cairo drawings in the archetype structure.
Ports the C designer (widgets/components/Inst*, docs/instrumentation*.md)
to the Python XFrame archetype model: one InstWidget class,
parameterised by ``kind``, with kind-specific branches in _draw --
the same pattern as the other archetypes in lightgui/xframe/core.py.
Families: gauges (round dial + needle, bar meter, rotameter,
manometer), tanks (fill 0-100% + alarm bands hi-hi 95 / hi 80 /
lo 20 / lo-lo 5), lamps/LEDs (on/off + brightness 0-100), 7-segment
LED digits, 14/16-segment alphanumeric displays, switches, needles,
panel furniture. Draw fallbacks are the C member defaults from
tools/gen_phase4_props.py, so a freshly placed widget looks like
its C demo (e.g. VerticalTankLevel level 76.28, AnalogGauge value
62.0). ``set_level``/``set_value`` are live: editing level in the
Inspector repaints the fill immediately.
"""
import math
from lightgui.xframe.core import (
Widget,
parse_color,
_bg,
_box,
_prop_bool,
_prop_float,
_prop_int,
_rgb,
_rounded_box,
_text_width,
_wtext,
)
# Palette kinds (instruments.py covers the 66 Inst* registry names).
INST_KINDS = frozenset((
"AnalogGauge", "BarMeter", "Beacon", "Bezel", "CircuitBreaker",
"ControlPanel", "DigitalReadout", "EmergencyStop", "FlowMeter",
"HorizontalTankLevel", "KeySwitch", "LampTestButton", "LedBlue",
"LedDigits1", "LedDigits2", "LedDigits3", "LedDigits4",
"LedDigits5", "LedDigits6", "LedGreen", "LedRed",
"LevelColumnGauge", "LevelTransmitter", "Manometer", "Nameplate",
"NeedleEdgewise", "NeedleKnifeEdge", "NeedleTapered", "NeedleVu",
"OpenChannelLevel", "PanelLampAmber", "PanelLampBlue",
"PanelLampGreen", "PanelLampRed", "PanelLampWhite", "PanelPlate",
"PressureGauge", "PushButton", "PushButtonLatching", "RgbStatusLed",
"RockerSwitch", "RockerSwitchDouble", "RockerSwitchIlluminated",
"Rotary", "Seg14Display1", "Seg14Display2", "Seg14Display3",
"Seg14Display4", "Seg14Display5", "Seg14Display6",
"Seg16Display1", "Seg16Display2", "Seg16Display3",
"Seg16Display4", "Seg16Display5", "Seg16Display6",
"SightGlassLevel", "SlideSwitch", "SphericalTankLevel",
"StackLight", "TankLevelAlarms", "TemperatureGauge",
"ToggleSwitch", "ToggleSwitchIlluminated", "VerticalTankLevel",
"VuMeter",
))
# C member defaults (tools/gen_phase4_props.py report) used as draw
# fallbacks, so a fresh widget shows its demo state.
_VALUE_DEFAULT = {
"AnalogGauge": 62.0, "PressureGauge": 180.0,
"TemperatureGauge": 85.0, "VuMeter": -6.0, "BarMeter": 55.0,
"FlowMeter": 40.0, "Manometer": 12.0, "DigitalReadout": 123.4,
"NeedleEdgewise": 76.28, "NeedleKnifeEdge": 76.28,
"NeedleTapered": 76.28, "NeedleVu": -2.4556,
"LedDigits1": 6.8652, "LedDigits2": 7.55172,
"LedDigits3": 76.20372, "LedDigits4": 762.72372,
"LedDigits5": 7627.92372, "LedDigits6": 76279.92372,
}
_RANGE_DEFAULT = {
"AnalogGauge": (0.0, 100.0), "PressureGauge": (0.0, 300.0),
"TemperatureGauge": (-20.0, 200.0), "VuMeter": (-20.0, 3.0),
"BarMeter": (0.0, 100.0), "FlowMeter": (0.0, 100.0),
"Manometer": (-50.0, 50.0), "DigitalReadout": (0.0, 999.9),
"NeedleEdgewise": (0.0, 100.0), "NeedleKnifeEdge": (0.0, 100.0),
"NeedleTapered": (0.0, 100.0), "NeedleVu": (-20.0, 3.0),
}
_LEVEL_DEFAULT = {
"VerticalTankLevel": 76.28, "HorizontalTankLevel": 13.78,
"SphericalTankLevel": 14.63, "SightGlassLevel": 14.58,
"LevelColumnGauge": 58.45, "LevelTransmitter": 30.47,
"OpenChannelLevel": 87.83, "TankLevelAlarms": 56.24,
}
def _color(widget, name, default):
return parse_color(widget.get_prop(name, ""), default)
def _value(widget):
"""Gauge/digit value with the C demo default."""
default = _VALUE_DEFAULT.get(widget.pkind, 0.0)
return _prop_float(widget, "value", default)
def _range(widget):
"""(min, max) with the C demo default."""
lo, hi = _RANGE_DEFAULT.get(widget.pkind, (0.0, 100.0))
return (_prop_float(widget, "minimum", lo),
_prop_float(widget, "maximum", hi))
def _frac(widget):
"""Value as a 0..1 fraction of its range."""
lo, hi = _range(widget)
value = _value(widget)
if hi <= lo:
return 0.5
return max(0.0, min(1.0, (value - lo) / (hi - lo)))
def _level(widget):
"""Tank level 0..100 with the C demo default."""
default = _LEVEL_DEFAULT.get(widget.pkind, 50.0)
return max(0.0, min(100.0, _prop_float(widget, "level", default)))
def _alarms(widget):
"""(hi-hi, hi, lo, lo-lo) alarm bands (C defaults 95/80/20/5)."""
return (_prop_float(widget, "alarm-hi-hi", 95.0),
_prop_float(widget, "alarm-hi", 80.0),
_prop_float(widget, "alarm-lo", 20.0),
_prop_float(widget, "alarm-lo-lo", 5.0))
def _on(widget):
return _prop_bool(widget, "on", False)
def _brightness(widget):
return max(0, min(100, _prop_int(widget, "brightness", 100)))
def _screw(cr, x, y, r=4):
"""One panel screw: grey disc + dark slot."""
cr.save()
_rgb(cr, (150 / 255, 150 / 255, 150 / 255))
cr.arc(x, y, r, 0, 2 * math.pi)
cr.fill_preserve()
_rgb(cr, (40 / 255, 40 / 255, 40 / 255))
cr.set_line_width(1)
cr.stroke()
cr.move_to(x - r + 1, y)
cr.line_to(x + r - 1, y)
cr.stroke()
cr.restore()
def _panel_screws(cr, widget, margin=10):
w, h = widget.width, widget.height
for x, y in ((margin, margin), (w - margin, margin),
(margin, h - margin), (w - margin, h - margin)):
_screw(cr, x, y)
# --- gauges ------------------------------------------------------------------
def _dial_angle(frac):
"""Needle angle (radians, Cairo coords): 270-degree sweep from
lower-left (0.75 pi) through top (1.5 pi) to lower-right."""
return (0.75 + 1.5 * frac) * math.pi
def _draw_dial(cr, widget, red_from=None, unit="", fmt="%.0f"):
"""Round dial gauge: face, ticks, numbers, red zone, needle,
hub, value readout. *red_from* is the fraction where the red
zone starts (None = no red zone)."""
w, h = widget.width, widget.height
face = _color(widget, "face-color", (245 / 255, 245 / 255, 240 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
red = _color(widget, "red-color", (200 / 255, 30 / 255, 30 / 255))
lo, hi = _range(widget)
frac = _frac(widget)
cx, cy = w / 2, h / 2 + 4
r = min(w, h) / 2 - 10
if r < 4:
return
cr.save()
_rgb(cr, face)
cr.arc(cx, cy, r, 0, 2 * math.pi)
cr.fill_preserve()
_rgb(cr, ink)
cr.set_line_width(2)
cr.stroke()
if red_from is not None:
_rgb(cr, red)
cr.set_line_width(4)
cr.arc(cx, cy, r - 6, _dial_angle(red_from),
_dial_angle(1.0))
cr.stroke()
# Ticks + numbers.
for i in range(11):
f = i / 10.0
angle = _dial_angle(f)
major = i % 5 == 0
r0 = r - (12 if major else 7)
cr.save()
_rgb(cr, ink)
cr.set_line_width(2 if major else 1)
cr.move_to(cx + r0 * math.cos(angle), cy + r0 * math.sin(angle))
cr.line_to(cx + (r - 2) * math.cos(angle),
cy + (r - 2) * math.sin(angle))
cr.stroke()
cr.restore()
if major:
value = lo + f * (hi - lo)
label = fmt % value
tx = cx + (r - 24) * math.cos(angle)
ty = cy + (r - 24) * math.sin(angle)
_wtext(cr, widget, label, tx, ty + 4, size=11,
rgb=ink, align="center", max_width=44)
# Needle.
angle = _dial_angle(frac)
width = _prop_int(widget, "needle-line-width", 3)
cr.save()
_rgb(cr, ink)
cr.set_line_width(max(1, width))
cr.set_line_cap(1)
cr.move_to(cx, cy)
cr.line_to(cx + (r - 16) * math.cos(angle),
cy + (r - 16) * math.sin(angle))
cr.stroke()
_rgb(cr, ink)
cr.arc(cx, cy, 5, 0, 2 * math.pi)
cr.fill()
cr.restore()
label = (fmt % _value(widget)) + (" " + unit if unit else "")
_wtext(cr, widget, label, cx, cy + r * 0.55 + 6, size=12,
rgb=ink, align="center", bold=True, max_width=w - 16)
if widget.text != widget.name:
_wtext(cr, widget, widget.text, cx, cy - r * 0.55, size=11,
rgb=ink, align="center", max_width=w - 16)
cr.restore()
def _draw_analog_gauge(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
_draw_dial(cr, widget)
def _draw_pressure_gauge(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
_draw_dial(cr, widget, red_from=0.8, unit="psi")
def _draw_temperature_gauge(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
_draw_dial(cr, widget, red_from=170.0 / 220.0, unit="°C")
def _draw_vu_meter(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
_draw_dial(cr, widget, red_from=20.0 / 23.0, unit="VU", fmt="%.0f")
def _draw_bar_meter(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
face = _color(widget, "face-color", (245 / 255, 245 / 255, 240 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
green = _color(widget, "green-color", (0.0, 150 / 255, 0.0))
amber = _color(widget, "amber-color", (255 / 255, 176 / 255, 0.0))
red = _color(widget, "red-color", (200 / 255, 30 / 255, 30 / 255))
frac = _frac(widget)
horizontal = _prop_bool(widget, "horizontal", False)
w, h = widget.width, widget.height
x0, y0 = 30, 10
bw, bh = w - 44, h - 34
if bw <= 0 or bh <= 0:
return
_box(cr, x0, y0, bw, bh, face, ink)
colour = green if frac < 0.6 else amber if frac < 0.8 else red
marker_w = _prop_int(widget, "marker-line-width", 3)
if horizontal:
fill_w = max(0, bw * frac)
_rgb(cr, colour)
cr.rectangle(x0, y0, fill_w, bh)
cr.fill()
cr.save()
_rgb(cr, ink)
cr.set_line_width(marker_w)
cr.move_to(x0 + fill_w, y0)
cr.line_to(x0 + fill_w, y0 + bh)
cr.stroke()
cr.restore()
else:
fill_h = max(0, bh * frac)
_rgb(cr, colour)
cr.rectangle(x0, y0 + bh - fill_h, bw, fill_h)
cr.fill()
cr.save()
_rgb(cr, ink)
cr.set_line_width(marker_w)
cr.move_to(x0, y0 + bh - fill_h)
cr.line_to(x0 + bw, y0 + bh - fill_h)
cr.stroke()
cr.restore()
lo, hi = _range(widget)
for i in range(6):
f = i / 5.0
label = "%.0f" % (lo + f * (hi - lo))
if horizontal:
_wtext(cr, widget, label, x0 + f * bw, y0 + bh + 14,
size=10, rgb=ink, align="center", max_width=36)
else:
_wtext(cr, widget, label, x0 - 4, y0 + bh - f * bh + 4,
size=10, rgb=ink, align="right", max_width=26)
_wtext(cr, widget, "%.1f" % _value(widget), w / 2, h - 4, size=12,
rgb=ink, align="center", bold=True, max_width=w - 8)
def _draw_flow_meter(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
face = _color(widget, "face-color", (245 / 255, 245 / 255, 240 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
red = _color(widget, "red-color", (200 / 255, 30 / 255, 30 / 255))
frac = _frac(widget)
w, h = widget.width, widget.height
cx = w / 2
top_w, bot_w = 26, 52
top_y, bot_y = 12, h - 30
if bot_y - top_y < 10:
return
# Tapered tube (narrow at the top, wide at the bottom).
cr.save()
_rgb(cr, face)
cr.move_to(cx - top_w / 2, top_y)
cr.line_to(cx + top_w / 2, top_y)
cr.line_to(cx + bot_w / 2, bot_y)
cr.line_to(cx - bot_w / 2, bot_y)
cr.close_path()
cr.fill_preserve()
_rgb(cr, ink)
cr.set_line_width(1.5)
cr.stroke()
cr.restore()
# Float rides at the flow level.
fy = bot_y - frac * (bot_y - top_y)
fw = top_w + (bot_w - top_w) * (1 - frac)
cr.save()
_rgb(cr, red)
cr.arc(cx, fy, max(3, fw / 2 - 3), 0, 2 * math.pi)
cr.fill_preserve()
_rgb(cr, ink)
cr.set_line_width(1)
cr.stroke()
cr.restore()
lo, hi = _range(widget)
for i in range(6):
f = i / 5.0
y = bot_y - f * (bot_y - top_y)
_wtext(cr, widget, "%.0f" % (lo + f * (hi - lo)),
cx - bot_w / 2 - 4, y + 4, size=10, rgb=ink,
align="right", max_width=30)
_wtext(cr, widget, "%.0f L/min" % _value(widget), cx, h - 8,
size=12, rgb=ink, align="center", bold=True,
max_width=w - 8)
def _draw_manometer(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
face = _color(widget, "face-color", (245 / 255, 245 / 255, 240 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
blue = _color(widget, "blue-color", (30 / 255, 120 / 255, 200 / 255))
lo, hi = _range(widget)
value = _value(widget)
frac = (value - lo) / (hi - lo) if hi > lo else 0.5
frac = max(-1.0, min(1.0, frac))
w, h = widget.width, widget.height
mid_y = h / 2
amp = (h - 40) / 2 * 0.8
left_x, right_x = w * 0.3, w * 0.7
tube_w = 16
top = 14
bot = h - 26
if bot - top < 10:
return
cr.save()
_rgb(cr, face)
cr.rectangle(left_x - tube_w / 2, top, tube_w, bot - top)
cr.rectangle(right_x - tube_w / 2, top, tube_w, bot - top)
cr.rectangle(left_x - tube_w / 2, bot - tube_w, right_x - left_x
+ tube_w, tube_w)
cr.fill_preserve()
_rgb(cr, ink)
cr.set_line_width(1.5)
cr.stroke()
cr.restore()
# Liquid: levels move apart with differential pressure.
left_y = mid_y - frac * amp
right_y = mid_y + frac * amp
cr.save()
_rgb(cr, blue)
cr.rectangle(left_x - tube_w / 2 + 1, left_y, tube_w - 2,
bot - left_y)
cr.rectangle(right_x - tube_w / 2 + 1, right_y, tube_w - 2,
bot - right_y)
cr.rectangle(left_x - tube_w / 2 + 1, bot - tube_w + 1,
right_x - left_x + tube_w - 2, tube_w - 2)
cr.fill()
cr.restore()
for i in range(5):
f = i / 4.0
y = top + f * (bot - top)
_wtext(cr, widget, "%+.0f" % (lo + f * (hi - lo)),
left_x - tube_w / 2 - 4, y + 4, size=10, rgb=ink,
align="right", max_width=34)
_wtext(cr, widget, "%.1f mm" % value, w / 2, h - 8, size=12,
rgb=ink, align="center", bold=True, max_width=w - 8)
# --- needles -----------------------------------------------------------------
def _draw_needle_tapered(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
face = _color(widget, "face-color", (250 / 255, 246 / 255, 230 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
needle = _color(widget, "needle-color", (30 / 255, 30 / 255, 30 / 255))
red = _color(widget, "red-color", (200 / 255, 30 / 255, 30 / 255))
w, h = widget.width, widget.height
cx, cy = w / 2, h * 0.62
r = min(w, h) * 0.36
if r < 4:
return
cr.save()
_rgb(cr, face)
cr.arc(cx, cy, r, 0, 2 * math.pi)
cr.fill_preserve()
_rgb(cr, ink)
cr.set_line_width(2)
cr.stroke()
_rgb(cr, red)
cr.set_line_width(3)
cr.arc(cx, cy, r - 5, _dial_angle(0.8), _dial_angle(1.0))
cr.stroke()
frac = _frac(widget)
angle = _dial_angle(frac)
# Tapered needle: a triangle from the hub to the tip.
tip_x, tip_y = cx + (r - 8) * math.cos(angle), \
cy + (r - 8) * math.sin(angle)
perp = angle + math.pi / 2
base = 5
cr.move_to(tip_x, tip_y)
cr.line_to(cx + base * math.cos(perp), cy + base * math.sin(perp))
cr.line_to(cx - base * math.cos(perp), cy - base * math.sin(perp))
cr.close_path()
_rgb(cr, needle)
cr.fill()
_rgb(cr, ink)
cr.arc(cx, cy, 4, 0, 2 * math.pi)
cr.fill()
cr.restore()
_wtext(cr, widget, "%.1f" % _value(widget), cx, h - 8, size=12,
rgb=ink, align="center", bold=True, max_width=w - 8)
def _draw_needle_knife(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
face = _color(widget, "face-color", (252 / 255, 252 / 255, 252 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
needle = _color(widget, "needle-color", (15 / 255, 15 / 255, 15 / 255))
mirror = _color(widget, "mirror-color", (200 / 255, 200 / 255, 200 / 255))
red = _color(widget, "red-color", (200 / 255, 30 / 255, 30 / 255))
w, h = widget.width, widget.height
x0, y0 = 12, 12
bw, bh = w - 24, h - 40
if bw <= 0 or bh <= 0:
return
_box(cr, x0, y0, bw, bh, face, ink)
# Mirror strip under the scale arc.
_rgb(cr, mirror)
cr.rectangle(x0 + 6, y0 + bh * 0.42, bw - 12, 8)
cr.fill()
frac = _frac(widget)
# Knife-edge needle: a thin line pivoting at the bottom centre,
# sweeping from upper-left (frac 0, 1.25 pi) to upper-right
# (frac 1, 1.75 pi).
pivot_x, pivot_y = x0 + bw / 2, y0 + bh
angle = math.pi * (1.25 + 0.5 * frac)
tip_x = pivot_x + bh * 0.85 * math.cos(angle)
tip_y = pivot_y + bh * 0.85 * math.sin(angle)
cr.save()
_rgb(cr, red)
cr.set_line_width(2)
cr.arc(pivot_x, pivot_y, bh * 0.8, math.pi * 1.65, math.pi * 1.75)
cr.stroke()
_rgb(cr, needle)
cr.set_line_width(2)
cr.move_to(pivot_x, pivot_y)
cr.line_to(tip_x, tip_y)
cr.stroke()
_rgb(cr, ink)
cr.arc(pivot_x, pivot_y, 4, 0, 2 * math.pi)
cr.fill()
cr.restore()
lo, hi = _range(widget)
for i in range(6):
f = i / 5.0
x = x0 + 8 + f * (bw - 16)
_wtext(cr, widget, "%.0f" % (lo + f * (hi - lo)), x,
y0 + 14, size=10, rgb=ink, align="center", max_width=36)
_wtext(cr, widget, "%.1f" % _value(widget), w / 2, h - 8, size=12,
rgb=ink, align="center", bold=True, max_width=w - 8)
def _draw_needle_edgewise(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
face = _color(widget, "face-color", (250 / 255, 250 / 255, 248 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
needle = _color(widget, "needle-color", (15 / 255, 15 / 255, 15 / 255))
red = _color(widget, "red-color", (200 / 255, 30 / 255, 30 / 255))
tick = _color(widget, "tick-color", (20 / 255, 20 / 255, 20 / 255))
w, h = widget.width, widget.height
x0, y0 = 12, 12
bw, bh = w - 24, h - 40
if bw <= 0 or bh <= 0:
return
_box(cr, x0, y0, bw, bh, face, ink)
frac = _frac(widget)
pivot_x, pivot_y = x0 + bw / 2, y0 + bh + 6
# Edgewise: the needle sweeps a shallow arc; the scale is a
# horizontal band with a red zone at the right end.
cr.save()
_rgb(cr, red)
cr.set_line_width(3)
cr.move_to(x0 + bw * 0.85, y0 + 8)
cr.line_to(x0 + bw - 4, y0 + 8)
cr.stroke()
_rgb(cr, tick)
cr.set_line_width(1)
lo, hi = _range(widget)
for i in range(11):
f = i / 10.0
x = x0 + 6 + f * (bw - 12)
cr.move_to(x, y0 + 6)
cr.line_to(x, y0 + 14 if i % 5 == 0 else y0 + 10)
if i % 5 == 0:
_wtext(cr, widget, "%.0f" % (lo + f * (hi - lo)), x,
y0 + 28, size=10, rgb=ink, align="center",
max_width=36)
cr.stroke()
_rgb(cr, needle)
cr.set_line_width(2)
tip_x = x0 + 6 + frac * (bw - 12)
cr.move_to(pivot_x, pivot_y)
cr.line_to(tip_x, y0 + 8)
cr.stroke()
_rgb(cr, ink)
cr.arc(pivot_x, pivot_y, 4, 0, 2 * math.pi)
cr.fill()
cr.restore()
_wtext(cr, widget, "%.1f" % _value(widget), w / 2, h - 8, size=12,
rgb=ink, align="center", bold=True, max_width=w - 8)
def _draw_needle_vu(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
face = _color(widget, "face-color", (248 / 255, 238 / 255, 205 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
needle = _color(widget, "needle-color", (15 / 255, 15 / 255, 15 / 255))
red = _color(widget, "red-color", (200 / 255, 30 / 255, 30 / 255))
w, h = widget.width, widget.height
x0, y0 = 12, 10
bw, bh = w - 24, h - 36
if bw <= 0 or bh <= 0:
return
_box(cr, x0, y0, bw, bh, face, ink)
frac = _frac(widget)
pivot_x, pivot_y = x0 + bw / 2, y0 + bh + 4
cr.save()
_rgb(cr, red)
cr.set_line_width(2)
cr.arc(pivot_x, pivot_y, bh * 0.75, math.pi * 1.6, math.pi * 1.75)
cr.stroke()
_rgb(cr, ink)
cr.set_line_width(1)
lo, hi = _range(widget)
for i in range(11):
f = i / 10.0
angle = math.pi * (1.25 + 0.5 * f)
r0 = bh * 0.72
cr.move_to(pivot_x + r0 * math.cos(angle),
pivot_y + r0 * math.sin(angle))
cr.line_to(pivot_x + (r0 + (8 if i % 5 == 0 else 4))
* math.cos(angle),
pivot_y + (r0 + (8 if i % 5 == 0 else 4))
* math.sin(angle))
if i % 5 == 0:
tx = pivot_x + (r0 + 16) * math.cos(angle)
ty = pivot_y + (r0 + 16) * math.sin(angle)
_wtext(cr, widget, "%+.0f" % (lo + f * (hi - lo)), tx,
ty + 4, size=10, rgb=ink, align="center",
max_width=36)
cr.stroke()
angle = math.pi * (1.25 + 0.5 * frac)
_rgb(cr, needle)
cr.set_line_width(2)
cr.move_to(pivot_x, pivot_y)
cr.line_to(pivot_x + bh * 0.8 * math.cos(angle),
pivot_y + bh * 0.8 * math.sin(angle))
cr.stroke()
_rgb(cr, ink)
cr.arc(pivot_x, pivot_y, 4, 0, 2 * math.pi)
cr.fill()
cr.restore()
_wtext(cr, widget, "VU %.1f" % _value(widget), w / 2, h - 8,
size=12, rgb=ink, align="center", bold=True,
max_width=w - 8)
# --- tanks ---------------------------------------------------------------------
def _tank_colours(widget):
return {
"fill": _color(widget, "fill-color",
(40 / 255, 120 / 255, 200 / 255)),
"amber": _color(widget, "amber-color",
(220 / 255, 150 / 255, 0.0)),
"red": _color(widget, "red-color",
(200 / 255, 30 / 255, 30 / 255)),
"ink": _color(widget, "ink-color", (60 / 255, 60 / 255, 60 / 255)),
"pale": _color(widget, "pale-color",
(235 / 255, 242 / 255, 248 / 255)),
"white": _color(widget, "white-color", (1.0, 1.0, 1.0)),
"grid": _color(widget, "grid-color",
(210 / 255, 210 / 255, 210 / 255)),
}
def _alarm_lines(cr, widget, x, top, bottom, vertical=False):
"""Alarm markers: hi-hi/hi/lo/lo-lo (red/amber/amber/red)."""
cols = _tank_colours(widget)
hi_hi, hi, lo, lo_lo = _alarms(widget)
for level, colour in ((hi_hi, cols["red"]), (hi, cols["amber"]),
(lo, cols["amber"]), (lo_lo, cols["red"])):
y = bottom - level / 100.0 * (bottom - top)
cr.save()
_rgb(cr, colour)
cr.set_line_width(2)
if vertical:
cr.move_to(x, top)
cr.line_to(x, bottom)
else:
cr.move_to(x, y)
cr.line_to(x + 14, y)
cr.stroke()
cr.restore()
def _tank_readout(cr, widget, level, x, y, align="left"):
_wtext(cr, widget, "%.1f%%" % level, x, y, size=13, bold=True,
rgb=(0.12, 0.12, 0.12), align=align,
max_width=widget.width - 16)
def _draw_vertical_tank(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
cols = _tank_colours(widget)
level = _level(widget)
w, h = widget.width, widget.height
x0, y0 = 24, 16
bw, bh = w - 62, h - 36
if bw <= 0 or bh <= 0:
return
_box(cr, x0, y0, bw, bh, cols["pale"], cols["ink"])
fill_h = bh * level / 100.0
cr.save()
_rgb(cr, cols["fill"])
cr.rectangle(x0 + 1, y0 + bh - fill_h, bw - 2, fill_h)
cr.fill()
_rgb(cr, cols["grid"])
cr.set_line_width(0.5)
for i in range(1, 10):
y = y0 + bh * i / 10.0
cr.move_to(x0, y)
cr.line_to(x0 + bw, y)
cr.stroke()
cr.restore()
_alarm_lines(cr, widget, x0 + bw + 2, y0, y0 + bh)
_tank_readout(cr, widget, level, x0 + bw / 2, h - 6, align="center")
def _draw_horizontal_tank(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
cols = _tank_colours(widget)
level = _level(widget)
w, h = widget.width, widget.height
x0, y0 = 16, 22
bw, bh = w - 32, h - 48
if bw <= 0 or bh <= 0:
return
_rounded_box(cr, x0, y0, bw, bh, cols["pale"], cols["ink"],
radius=bh / 2)
fill_w = bw * level / 100.0
if fill_w > 0:
cr.save()
_rounded_box(cr, x0 + 1, y0 + 1, max(2, fill_w - 2), bh - 2,
cols["fill"], cols["fill"], radius=bh / 2 - 1)
cr.restore()
hi_hi, hi, lo, lo_lo = _alarms(widget)
for mark, colour in ((hi_hi, cols["red"]), (hi, cols["amber"]),
(lo, cols["amber"]), (lo_lo, cols["red"])):
x = x0 + bw * mark / 100.0
cr.save()
_rgb(cr, colour)
cr.set_line_width(2)
cr.move_to(x, y0)
cr.line_to(x, y0 + bh)
cr.stroke()
cr.restore()
_tank_readout(cr, widget, level, x0 + bw / 2, h - 6, align="center")
def _draw_spherical_tank(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
cols = _tank_colours(widget)
level = _level(widget)
w, h = widget.width, widget.height
cx, cy = w / 2, h / 2 - 4
r = min(w, h) / 2 - 14
if r < 4:
return
# Legs.
cr.save()
_rgb(cr, cols["ink"])
cr.set_line_width(2)
cr.move_to(cx - r * 0.6, cy + r * 0.7)
cr.line_to(cx - r * 0.8, h - 8)
cr.move_to(cx + r * 0.6, cy + r * 0.7)
cr.line_to(cx + r * 0.8, h - 8)
cr.stroke()
cr.restore()
cr.save()
_rgb(cr, cols["pale"])
cr.arc(cx, cy, r, 0, 2 * math.pi)
cr.fill_preserve()
cr.clip()
fill_h = 2 * r * level / 100.0
_rgb(cr, cols["fill"])
cr.rectangle(cx - r, cy + r - fill_h, 2 * r, fill_h)
cr.fill()
cr.restore()
cr.save()
_rgb(cr, cols["ink"])
cr.set_line_width(1.5)
cr.arc(cx, cy, r, 0, 2 * math.pi)
cr.stroke()
cr.restore()
_alarm_lines(cr, widget, cx + r + 2, cy - r, cy + r)
_tank_readout(cr, widget, level, cx, h - 4, align="center")
def _draw_sight_glass(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
cols = _tank_colours(widget)
steel = _color(widget, "steel-color",
(150 / 255, 150 / 255, 150 / 255))
level = _level(widget)
w, h = widget.width, widget.height
cx = w / 2
tube_w = 26
x0 = cx - tube_w / 2
top, bot = 20, h - 24
if bot - top < 10:
return
# Top/bottom pipes.
_box(cr, cx - 4, 6, 8, 14, steel, cols["ink"])
_box(cr, cx - 4, bot, 8, 14, steel, cols["ink"])
_box(cr, x0, top, tube_w, bot - top, cols["white"], steel)
fill_h = (bot - top) * level / 100.0
cr.save()
_rgb(cr, cols["fill"])
cr.rectangle(x0 + 2, bot - fill_h, tube_w - 4, fill_h)
cr.fill()
cr.restore()
for i in range(11):
y = bot - (bot - top) * i / 10.0
cr.save()
_rgb(cr, cols["ink"])
cr.set_line_width(1 if i % 5 else 2)
cr.move_to(x0 - (6 if i % 5 else 10), y)
cr.line_to(x0, y)
cr.stroke()
cr.restore()
if i % 5 == 0 and i:
_wtext(cr, widget, "%d" % (i * 10), x0 - 14, y + 4,
size=10, rgb=cols["ink"], align="right",
max_width=26)
_alarm_lines(cr, widget, x0 + tube_w + 2, top, bot)
_tank_readout(cr, widget, level, cx, h - 6, align="center")
def _draw_level_column(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
cols = _tank_colours(widget)
level = _level(widget)
w, h = widget.width, widget.height
cx = w / 2 - 10
col_w = 18
x0 = cx - col_w / 2
top, bot = 16, h - 24
if bot - top < 10:
return
_box(cr, x0, top, col_w, bot - top, cols["pale"], cols["ink"])
fill_h = (bot - top) * level / 100.0
cr.save()
_rgb(cr, cols["fill"])
cr.rectangle(x0 + 1, bot - fill_h, col_w - 2, fill_h)
cr.fill()
cr.restore()
# Float marker: a red bar at the level.
float_y = bot - fill_h
cr.save()
_rgb(cr, cols["red"])
cr.rectangle(x0 - 6, float_y - 3, col_w + 12, 6)
cr.fill()
cr.restore()
for i in range(11):
y = bot - (bot - top) * i / 10.0
_wtext(cr, widget, "%d" % (i * 10), x0 - 8, y + 4, size=10,
rgb=cols["ink"], align="right", max_width=26)
_alarm_lines(cr, widget, x0 + col_w + 2, top, bot)
_tank_readout(cr, widget, level, cx + 44, top + 12, align="center")
def _draw_level_transmitter(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
cols = _tank_colours(widget)
level = _level(widget)
ma = 4.0 + 16.0 * level / 100.0
w, h = widget.width, widget.height
x0, y0 = 16, 16
bw, bh = 26, h - 48
if bw <= 0 or bh <= 0:
return
_box(cr, x0, y0, bw, bh, cols["pale"], cols["ink"])
fill_h = bh * level / 100.0
cr.save()
_rgb(cr, cols["fill"])
cr.rectangle(x0 + 1, y0 + bh - fill_h, bw - 2, fill_h)
cr.fill()
cr.restore()
_alarm_lines(cr, widget, x0 + bw + 2, y0, y0 + bh)
_wtext(cr, widget, "%.1f%%" % level, x0 + bw + 22, y0 + 14,
size=13, bold=True, rgb=(0.12, 0.12, 0.12),
max_width=w - x0 - bw - 26)
_wtext(cr, widget, "%.2f mA" % ma, x0 + bw + 22, y0 + 32, size=12,
rgb=cols["ink"], max_width=w - x0 - bw - 26)
# Loop terminals.
_box(cr, x0 + bw + 22, y0 + 48, 14, 10, cols["white"], cols["ink"])
_box(cr, x0 + bw + 42, y0 + 48, 14, 10, cols["white"], cols["ink"])
_wtext(cr, widget, "+ -", x0 + bw + 22, y0 + 72, size=11,
rgb=cols["ink"], max_width=40)
def _draw_open_channel(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
cols = _tank_colours(widget)
surface = _color(widget, "surface-color",
(20 / 255, 80 / 255, 160 / 255))
level = _level(widget)
w, h = widget.width, widget.height
x0, y0 = 16, 16
bw, bh = w - 48, h - 40
if bw <= 0 or bh <= 0:
return
fill_h = bh * level / 100.0
cr.save()
_rgb(cr, cols["fill"], 0.75)
cr.rectangle(x0, y0 + bh - fill_h, bw, fill_h)
cr.fill()
_rgb(cr, surface)
cr.set_line_width(2)
cr.move_to(x0, y0 + bh - fill_h)
cr.line_to(x0 + bw, y0 + bh - fill_h)
cr.stroke()
_rgb(cr, cols["ink"])
cr.set_line_width(1.5)
cr.move_to(x0, y0)
cr.line_to(x0, y0 + bh)
cr.line_to(x0 + bw, y0 + bh)
cr.line_to(x0 + bw, y0)
cr.stroke()
cr.restore()
for i in range(6):
y = y0 + bh - bh * i / 5.0
_wtext(cr, widget, "%d" % (i * 20), x0 + bw + 4, y + 4,
size=10, rgb=cols["ink"], max_width=26)
_tank_readout(cr, widget, level, x0 + bw / 2, h - 6, align="center")
def _draw_tank_alarms(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
cols = _tank_colours(widget)
level = _level(widget)
hi_hi, hi, lo, lo_lo = _alarms(widget)
w, h = widget.width, widget.height
x0, y0 = 16, 16
bw, bh = w - 84, h - 36
if bw <= 0 or bh <= 0:
return
_box(cr, x0, y0, bw, bh, cols["pale"], cols["ink"])
fill_h = bh * level / 100.0
cr.save()
_rgb(cr, cols["fill"])
cr.rectangle(x0 + 1, y0 + bh - fill_h, bw - 2, fill_h)
cr.fill()
cr.restore()
_alarm_lines(cr, widget, x0 + bw + 2, y0, y0 + bh)
lamps = (("HI-HI", cols["red"], level >= hi_hi),
("HI", cols["amber"], level >= hi),
("LO", cols["amber"], level <= lo),
("LO-LO", cols["red"], level <= lo_lo))
lx = x0 + bw + 20
ly = y0 + 8
for label, colour, lit in lamps:
cr.save()
_rgb(cr, colour if lit else (60 / 255, 60 / 255, 60 / 255))
cr.arc(lx + 6, ly + 6, 6, 0, 2 * math.pi)
cr.fill_preserve()
_rgb(cr, cols["ink"])
cr.set_line_width(1)
cr.stroke()
cr.restore()
_wtext(cr, widget, label, lx + 16, ly + 10, size=10,
rgb=cols["ink"], max_width=40)
ly += 20
_tank_readout(cr, widget, level, x0 + bw / 2, h - 6, align="center")
# --- lamps / LEDs --------------------------------------------------------------
def _lens_rgb(lens, on, brightness):
"""Lens colour: dim tint when off, brighter with brightness."""
dim = tuple(0.3 * c + 0.7 * (40 / 255) for c in lens)
if not on:
return dim
f = brightness / 100.0
return tuple(dim[i] + (lens[i] - dim[i]) * (0.35 + 0.65 * f)
for i in range(3))
def _draw_panel_lamp(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
bezel = _color(widget, "bezel-color", (70 / 255, 70 / 255, 70 / 255))
ring = _color(widget, "ring-color", (160 / 255, 160 / 255, 160 / 255))
lens = _color(widget, "lens-color", (220 / 255, 30 / 255, 30 / 255))
on = _on(widget)
brightness = _brightness(widget)
w, h = widget.width, widget.height
cx, cy = w / 2, h / 2 - (6 if widget.text != widget.name else 0)
r = min(w, h) / 2 - 10
if r < 4:
return
cr.save()
_rgb(cr, bezel)
cr.arc(cx, cy, r, 0, 2 * math.pi)
cr.fill()
_rgb(cr, ring)
cr.arc(cx, cy, r - 3, 0, 2 * math.pi)
cr.fill()
_rgb(cr, _lens_rgb(lens, on, brightness))
cr.arc(cx, cy, r - 7, 0, 2 * math.pi)
cr.fill()
if on and brightness > 70:
_rgb(cr, (1.0, 1.0, 1.0), 0.35)
cr.arc(cx, cy, r - 2, 0, 2 * math.pi)
cr.set_line_width(2)
cr.stroke()
_rgb(cr, (1.0, 1.0, 1.0), 0.5)
cr.arc(cx - r * 0.3, cy - r * 0.35, r * 0.18, 0, 2 * math.pi)
cr.fill()
cr.restore()
if widget.text != widget.name:
_wtext(cr, widget, widget.text, cx, h - 6, size=11,
align="center", rgb=(0.25, 0.25, 0.25),
max_width=w - 8)
def _draw_led(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
dark = _color(widget, "dark-color", (40 / 255, 40 / 255, 40 / 255))
lens = _color(widget, "lens-color", (220 / 255, 30 / 255, 30 / 255))
lead = _color(widget, "lead-color", (120 / 255, 120 / 255, 120 / 255))
on = _on(widget)
brightness = _brightness(widget)
w, h = widget.width, widget.height
cx = w / 2
block_w, block_h = min(w - 16, 64), min(h - 30, 54)
bx, by = cx - block_w / 2, 10
_rounded_box(cr, bx, by, block_w, block_h, dark, dark, radius=6)
r = min(block_w, block_h) / 2 - 6
cr.save()
_rgb(cr, _lens_rgb(lens, on, brightness))
cr.arc(cx, by + block_h / 2, max(r, 3), 0, 2 * math.pi)
cr.fill()
_rgb(cr, (1.0, 1.0, 1.0), 0.5)
cr.arc(cx - r * 0.3, by + block_h / 2 - r * 0.3, max(r * 0.2, 1),
0, 2 * math.pi)
cr.fill()
cr.restore()
cr.save()
_rgb(cr, lead)
cr.set_line_width(3)
cr.move_to(cx - 10, by + block_h)
cr.line_to(cx - 10, h - 6)
cr.move_to(cx + 10, by + block_h)
cr.line_to(cx + 10, h - 6)
cr.stroke()
cr.restore()
if widget.text != widget.name:
_wtext(cr, widget, widget.text, cx, h - 4, size=11,
align="center", rgb=(0.25, 0.25, 0.25),
max_width=w - 8)
def _draw_rgb_led(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
dark = _color(widget, "dark-color", (40 / 255, 40 / 255, 40 / 255))
red_c = _color(widget, "red-channel-color",
(220 / 255, 30 / 255, 30 / 255))
green_c = _color(widget, "green-channel-color",
(30 / 255, 200 / 255, 60 / 255))
blue_c = _color(widget, "blue-channel-color",
(40 / 255, 120 / 255, 230 / 255))
outline = _color(widget, "swatch-outline-color",
(60 / 255, 60 / 255, 60 / 255))
brightness = _brightness(widget)
all_on = _prop_bool(widget, "all-on", False)
red_on = all_on or _prop_bool(widget, "red-on", False)
green_on = all_on or _prop_bool(widget, "green-on", False)
blue_on = all_on or _prop_bool(widget, "blue-on", False)
w, h = widget.width, widget.height
cx = w / 2
block_w, block_h = min(w - 16, 64), min(h - 34, 50)
bx, by = cx - block_w / 2, 8
_rounded_box(cr, bx, by, block_w, block_h, dark, dark, radius=6)
combined = (
red_c[0] if red_on else 0.12,
green_c[1] if green_on else 0.12,
blue_c[2] if blue_on else 0.12,
)
r = min(block_w, block_h) / 2 - 6
cr.save()
_rgb(cr, _lens_rgb(combined, red_on or green_on or blue_on,
brightness))
cr.arc(cx, by + block_h / 2, max(r, 3), 0, 2 * math.pi)
cr.fill()
cr.restore()
sw = 14
sx = cx - (3 * sw + 2 * 6) / 2
sy = by + block_h + 6
for on, colour in ((red_on, red_c), (green_on, green_c),
(blue_on, blue_c)):
_box(cr, sx, sy, sw, sw,
colour if on else (30 / 255, 30 / 255, 30 / 255), outline)
sx += sw + 6
def _draw_stack_light(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
dark = _color(widget, "dark-color", (40 / 255, 40 / 255, 40 / 255))
red = _color(widget, "red-color", (220 / 255, 30 / 255, 30 / 255))
amber = _color(widget, "amber-color", (230 / 255, 160 / 255, 20 / 255))
green = _color(widget, "green-color", (30 / 255, 200 / 255, 60 / 255))
outline = _color(widget, "tier-outline-color",
(60 / 255, 60 / 255, 60 / 255))
all_on = _prop_bool(widget, "all-on", False)
tiers = (
("red-on", "red-brightness", red),
("amber-on", "amber-brightness", amber),
("green-on", "green-brightness", green),
)
w, h = widget.width, widget.height
cx = w / 2
pole_h = 18
tier_h = max(14, (h - pole_h - 26) / 3)
tier_w = min(54, w - 20)
x0 = cx - tier_w / 2
y = 8
for prop, bright_prop, colour in tiers:
on = all_on or _prop_bool(widget, prop, False)
bright = _prop_int(widget, bright_prop, 100)
_rounded_box(cr, x0, y, tier_w, tier_h - 3,
_lens_rgb(colour, on, bright), outline, radius=6)
y += tier_h
# Pole + base.
_box(cr, cx - 3, y, 6, pole_h, dark, dark)
_box(cr, cx - tier_w / 2 - 6, y + pole_h, tier_w + 12, 10, dark,
dark)
def _draw_beacon(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
dark = _color(widget, "dark-color", (40 / 255, 40 / 255, 40 / 255))
beacon = _color(widget, "beacon-color",
(230 / 255, 160 / 255, 20 / 255))
on = _on(widget)
phase = _prop_bool(widget, "phase", True)
brightness = _brightness(widget)
lit = on and phase
w, h = widget.width, widget.height
cx = w / 2
base_y = h - 18
dome_r = min(w / 2 - 12, (h - 30) / 2)
if dome_r < 4:
return
cy = base_y - dome_r
if lit:
cr.save()
_rgb(cr, beacon, 0.25)
cr.move_to(cx, cy)
cr.line_to(4, cy - dome_r * 0.4)
cr.line_to(4, cy + dome_r * 0.4)
cr.close_path()
cr.fill()
cr.move_to(cx, cy)
cr.line_to(w - 4, cy - dome_r * 0.4)
cr.line_to(w - 4, cy + dome_r * 0.4)
cr.close_path()
cr.fill()
cr.restore()
cr.save()
_rgb(cr, _lens_rgb(beacon, lit, brightness))
cr.arc(cx, cy, dome_r, math.pi, 2 * math.pi)
cr.close_path()
cr.fill_preserve()
_rgb(cr, dark)
cr.set_line_width(2)
cr.stroke()
cr.restore()
_box(cr, cx - dome_r - 4, base_y, 2 * dome_r + 8, 10, dark, dark)
_wtext(cr, widget, "%.1f Hz" % _prop_float(widget, "flash-rate", 2.0),
cx, h - 4, size=11, align="center", rgb=(0.25, 0.25, 0.25),
max_width=w - 8)
def _draw_lamp_test_button(cr, widget):
face = _color(widget, "face-color", (200 / 255, 200 / 255, 200 / 255))
bezel = _color(widget, "bezel-color", (70 / 255, 70 / 255, 70 / 255))
w, h = widget.width, widget.height
_rounded_box(cr, 0, 0, w, h, bezel, bezel, radius=6)
_rounded_box(cr, 4, 4, w - 8, h - 8, face, bezel, radius=5)
_wtext(cr, widget, "TEST", w / 2, h / 2 + 5, size=12, bold=True,
align="center", rgb=(0.15, 0.15, 0.15), max_width=w - 16)
# --- segment displays ----------------------------------------------------------
# Segment maps are verbatim from the C widgets
# (InstSeg14Display5.h / InstSeg16Display5.h segment_map(),
# InstDigitalReadout.h draw_digit seg[10][7]). C ids: a top,
# b upper-right, c lower-right, d bottom, e lower-left,
# f upper-left, g1/g2 middle halves, i upper centre vertical,
# l lower centre vertical, h/j/k/m diagonals (top-left, top-right,
# bottom-left, bottom-right). 16-segment splits a -> a1/a2 and
# d -> d1/d2; its M ("b c e f h i j") and W ("b c e f k l m") are
# the clean 16-seg forms. (An earlier draft used different ids for
# the centre verticals/diagonals, which made I/K/M/V/W wrong and
# drew the diagonals as full-length round-capped strokes -- a dark
# X blob over every glyph. This block mirrors C exactly.)
_SEVEN_MAP = {
"0": "abcdef", "1": "bc", "2": "abdeg", "3": "abcdg",
"4": "bcfg", "5": "acdfg", "6": "acdefg", "7": "abc",
"8": "abcdefg", "9": "abcdfg", "-": "g", "_": "d", " ": "",
}
_FOURTEEN_MAP = {
" ": "",
'"': "h j",
"'": "j",
"+": "g1 g2 i l",
"-": "g1 g2",
"/": "j k",
"0": "a b c d e f",
"1": "b c",
"2": "a b d e g1 g2",
"3": "a b c d g1 g2",
"4": "b c f g1 g2",
"5": "a c d f g1 g2",
"6": "a c d e f g1 g2",
"7": "a b c",
"8": "a b c d e f g1 g2",
"9": "a b c d f g1 g2",
"=": "d g1 g2",
"A": "a b c e f g1 g2",
"B": "a b c d g1 g2 i l",
"C": "a d e f",
"D": "a b c d i l",
"E": "a d e f g1 g2",
"F": "a e f g1 g2",
"G": "a c d e f g1 g2",
"H": "b c e f g1 g2",
"I": "i l",
"J": "b c d e",
"K": "e f g1 j k",
"L": "d e f",
"M": "b c e f h j",
"N": "b c e f h m",
"O": "a b c d e f",
"P": "a b e f g1 g2",
"Q": "a b c d e f m",
"R": "a b e f g1 g2 m",
"S": "a c d f g1 g2",
"T": "a i l",
"U": "b c d e",
"V": "c e j k",
"W": "b c e f k m",
"X": "h j k m",
"Y": "b c f g1 g2 l",
"Z": "a d j k",
"_": "d",
}
_SIXTEEN_MAP = dict(_FOURTEEN_MAP)
_SIXTEEN_MAP.update({
"0": "a1 a2 b c d1 d2 e f",
"2": "a1 a2 b d1 d2 e g1 g2",
"3": "a1 a2 b c d1 d2 g1 g2",
"5": "a1 a2 c d1 d2 f g1 g2",
"6": "a1 a2 c d1 d2 e f g1 g2",
"7": "a1 a2 b c",
"8": "a1 a2 b c d1 d2 e f g1 g2",
"9": "a1 a2 b c d1 d2 f g1 g2",
"=": "d1 d2 g1 g2",
"A": "a1 a2 b c e f g1 g2",
"B": "a1 a2 b c d1 d2 g1 g2 i l",
"C": "a1 a2 d1 d2 e f",
"D": "a1 a2 b c d1 d2 i l",
"E": "a1 a2 d1 d2 e f g1 g2",
"F": "a1 a2 e f g1 g2",
"G": "a1 a2 c d1 d2 e f g1 g2",
"J": "b c d1 d2 e",
"L": "d1 d2 e f",
"M": "b c e f h i j",
"O": "a1 a2 b c d1 d2 e f",
"P": "a1 a2 b e f g1 g2",
"Q": "a1 a2 b c d1 d2 e f m",
"R": "a1 a2 b e f g1 g2 m",
"S": "a1 a2 c d1 d2 f g1 g2",
"T": "a1 a2 i l",
"U": "b c d1 d2 e",
"W": "b c e f k l m",
"Z": "a1 a2 d1 d2 j k",
"_": "d1 d2",
})
def _seg_thickness(w):
"""C: t = max(2, cell_w / 6) (Seg14 draw); the 7-seg readout
uses a fixed t = 8 at its own cell size, the same scale."""
return max(2.0, w / 6.0)
def _seg_rects(x, y, w, h, segs):
"""Segment name -> rect, C draw_segment() geometry verbatim
(InstSeg14Display5.h; DigitalReadout.h for the 7-seg set).
Diagonals are returned separately as endpoint pairs: C draws
them as polygons (draw_diag), not stroked lines, so they stop
short of the centre instead of bulging into a blob."""
t = _seg_thickness(w)
midx = x + w / 2
midy = y + h / 2
if segs == "seven":
hv = max(1.0, h / 2 - 2 * t)
return {
"a": (x + t, y, w - 2 * t, t),
"b": (x + w - t, y + t, t, hv),
"c": (x + w - t, y + h / 2 + t, t, hv),
"d": (x + t, y + h - t, w - 2 * t, t),
"e": (x, y + h / 2 + t, t, hv),
"f": (x, y + t, t, hv),
"g": (x + t, y + h / 2 - t / 2, w - 2 * t, t),
}, {}
hh = max(1.0, h / 2 - t)
hw = max(1.0, w / 2 - t)
cv = max(1.0, h / 2 - 2 * t)
rects = {
"a": (x + t, y, w - 2 * t, t),
"b": (x + w - t, y + t, t, hh),
"c": (x + w - t, midy, t, hh),
"d": (x + t, y + h - t, w - 2 * t, t),
"e": (x, midy, t, hh),
"f": (x, y + t, t, hh),
"g1": (x + t, midy - t / 2, hw, t),
"g2": (midx, midy - t / 2, hw, t),
"i": (midx - t / 2, y + t, t, cv),
"l": (midx - t / 2, midy + t, t, cv),
}
if segs == "sixteen":
rects["a1"] = (x + t, y, hw, t)
rects["a2"] = (midx, y, hw, t)
rects["d1"] = (x + t, y + h - t, hw, t)
rects["d2"] = (midx, y + h - t, hw, t)
lines = {
"h": (x + t, y + t, midx, midy - t),
"j": (x + w - t, y + t, midx, midy - t),
"k": (x + t, y + h - t, midx, midy + t),
"m": (x + w - t, y + h - t, midx, midy + t),
}
return rects, lines
def _draw_diag(cr, x0, y0, x1, y1, t):
"""C draw_diag: a t-wide parallelogram, not a stroked line."""
dx, dy = x1 - x0, y1 - y0
length = math.hypot(dx, dy)
if length < 1.0:
return
px, py = -dy / length * t / 2.0, dx / length * t / 2.0
cr.move_to(x0 + px, y0 + py)
cr.line_to(x1 + px, y1 + py)
cr.line_to(x1 - px, y1 - py)
cr.line_to(x0 - px, y0 - py)
cr.close_path()
cr.fill()
def _draw_seg_digit(cr, x, y, w, h, ch, on_rgb, off_rgb,
segs="fourteen"):
"""One 7/14/16-segment digit; off segments stay faintly visible.
Maps and geometry are verbatim from the C widgets."""
if segs == "seven":
lit = set(_SEVEN_MAP.get(ch, ""))
universe = ("a", "b", "c", "d", "e", "f", "g")
elif segs == "sixteen":
lit = set(_SIXTEEN_MAP.get(ch.upper(), "").split())
universe = ("a1", "a2", "b", "c", "d1", "d2", "e", "f",
"g1", "g2", "h", "i", "j", "k", "l", "m")
else:
lit = set(_FOURTEEN_MAP.get(ch.upper(), "").split())
universe = ("a", "b", "c", "d", "e", "f", "g1", "g2",
"h", "i", "j", "k", "l", "m")
rects, lines = _seg_rects(x, y, w, h, segs)
t = _seg_thickness(w)
for seg in universe:
cr.save()
_rgb(cr, on_rgb if seg in lit else off_rgb)
if seg in rects:
rx, ry, rw, rh = rects[seg]
cr.rectangle(rx, ry, max(1.0, rw), max(1.0, rh))
cr.fill()
else:
x0, y0, x1, y1 = lines[seg]
_draw_diag(cr, x0, y0, x1, y1, t)
cr.restore()
def _led_digits_text(value, digits, dp):
"""Zero-padded digit string + decimal-point index (C shown_digits
/ decimal_point_after). Returns (digits_str, point_after_index)
where point_after is -1 when there is no point."""
max_value = (10.0 ** digits - 1.0) / (10.0 ** dp) if dp else \
float(10 ** digits - 1)
value = max(0.0, min(max_value, value))
text = "%.*f" % (dp, value)
bare = text.replace(".", "").replace("-", "")
if len(bare) < digits:
bare = "0" * (digits - len(bare)) + bare
else:
bare = bare[-digits:]
point_after = digits - dp - 1 if dp > 0 else -1
return bare, point_after
def _draw_led_digits(cr, widget):
kind = widget.pkind
digits = int(kind.replace("LedDigits", ""))
dp = _prop_int(widget, "decimal-places", 0 if digits == 1 else 1)
bezel = _color(widget, "bezel-color", (58 / 255, 58 / 255, 62 / 255))
dim = _color(widget, "dim-color", (52 / 255, 14 / 255, 11 / 255))
lit = _color(widget, "lit-color", (255 / 255, 60 / 255, 35 / 255))
_rounded_box(cr, 0, 0, widget.width, widget.height, bezel, bezel,
radius=6)
text, point_after = _led_digits_text(_value(widget), digits, dp)
pad = 10
gap = 6
avail_w = widget.width - 2 * pad - gap * (digits - 1)
dw = avail_w / digits if digits else avail_w
dh = widget.height - 20
if dw <= 2 or dh <= 4:
return
for i, ch in enumerate(text):
x = pad + i * (dw + gap)
_draw_seg_digit(cr, x, 10, dw, dh, ch, lit, dim,
segs="seven")
if i == point_after:
cr.save()
_rgb(cr, lit)
cr.arc(x + dw + gap / 2, 10 + dh, 2.5, 0, 2 * math.pi)
cr.fill()
cr.restore()
def _draw_digital_readout(cr, widget):
dark = _color(widget, "dark-color", (30 / 255, 30 / 255, 30 / 255))
off = _color(widget, "segment-off-color",
(50 / 255, 45 / 255, 20 / 255))
on = _color(widget, "segment-on-color",
(255 / 255, 176 / 255, 0.0))
_rounded_box(cr, 0, 0, widget.width, widget.height, dark, dark,
radius=6)
digits = 4
dp = 1
text, point_after = _led_digits_text(_value(widget), digits, dp)
pad = 10
gap = 6
avail_w = widget.width - 2 * pad - gap * (digits - 1)
dw = avail_w / digits
dh = widget.height - 20
if dw <= 2 or dh <= 4:
return
for i, ch in enumerate(text):
x = pad + i * (dw + gap)
_draw_seg_digit(cr, x, 10, dw, dh, ch, on, off, segs="seven")
if i == point_after:
cr.save()
_rgb(cr, on)
cr.arc(x + dw + gap / 2, 10 + dh, 2.5, 0, 2 * math.pi)
cr.fill()
cr.restore()
def _draw_seg_display(cr, widget):
kind = widget.pkind
sixteen = kind.startswith("Seg16")
digits = int(kind.replace("Seg14Display", "")
.replace("Seg16Display", ""))
bezel = _color(widget, "bezel-color", (58 / 255, 58 / 255, 62 / 255))
dim = _color(widget, "dim-color", (60 / 255, 42 / 255, 8 / 255))
lit = _color(widget, "lit-color", (255 / 255, 176 / 255, 0.0))
_rounded_box(cr, 0, 0, widget.width, widget.height, bezel, bezel,
radius=6)
raw = widget.get_prop(
"value",
widget.prop_def("value").default
if widget.prop_def("value") is not None else "")
chars = []
point_after = -1
for ch in str(raw).upper():
if ch in ".:":
point_after = len(chars) - 1
continue
chars.append(ch)
if len(chars) >= digits:
break
while len(chars) < digits:
chars.append(" ")
pad = 10
gap = 5
avail_w = widget.width - 2 * pad - gap * (digits - 1)
dw = avail_w / digits if digits else avail_w
dh = widget.height - 20
if dw <= 2 or dh <= 4:
return
for i, ch in enumerate(chars):
x = pad + i * (dw + gap)
_draw_seg_digit(cr, x, 10, dw, dh, ch, lit, dim,
segs="sixteen" if sixteen else "fourteen")
if i == point_after:
cr.save()
_rgb(cr, lit)
cr.arc(x + dw + gap / 2, 10 + dh, 2.5, 0, 2 * math.pi)
cr.fill()
cr.restore()
# --- switches ------------------------------------------------------------------
def _draw_rocker(cr, widget, illuminated=False, double=False):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
bezel = _color(widget, "bezel-color", (38 / 255, 38 / 255, 42 / 255))
panel = _color(widget, "panel-color", (58 / 255, 58 / 255, 62 / 255))
rocker = _color(widget, "rocker-color",
(190 / 255, 190 / 255, 195 / 255))
green = _color(widget, "green-color", (0.0, 180 / 255, 0.0))
dimred = _color(widget, "dimred-color", (120 / 255, 30 / 255, 30 / 255))
greenlit = _color(widget, "greenlit-color",
(60 / 255, 255 / 255, 90 / 255))
w, h = widget.width, widget.height
_rounded_box(cr, 6, 6, w - 12, h - 12, panel, bezel, radius=8)
if double:
position = _prop_int(widget, "position", 0)
tilt = 8 if position > 0 else -8 if position < 0 else 0
else:
tilt = 8 if _on(widget) else -8
x0, y0 = 16, 14
rw, rh = w - 32, h - 28
if rw <= 0 or rh <= 0:
return
# Rocker quad: top edge raised when on (up is on).
cr.save()
_rgb(cr, rocker)
cr.move_to(x0, y0 - tilt if tilt > 0 else y0)
cr.line_to(x0 + rw, y0 + tilt if tilt < 0 else y0)
cr.line_to(x0 + rw, y0 + rh + (tilt if tilt > 0 else 0))
cr.line_to(x0, y0 + rh + (-tilt if tilt < 0 else 0))
cr.close_path()
cr.fill_preserve()
_rgb(cr, bezel)
cr.set_line_width(1.5)
cr.stroke()
cr.restore()
_wtext(cr, widget, "I", w / 2, y0 + 12, size=11, align="center",
rgb=(0.15, 0.15, 0.15), max_width=20)
_wtext(cr, widget, "O", w / 2, y0 + rh - 4, size=11,
align="center", rgb=(0.15, 0.15, 0.15), max_width=20)
if illuminated:
on = _on(widget)
_rounded_box(cr, w / 2 - 12, y0 + rh / 2 - 5, 24, 10,
greenlit if on else dimred,
green if on else dimred, radius=4)
def _draw_toggle_switch(cr, widget, illuminated=False):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
bezel = _color(widget, "bezel-color", (38 / 255, 38 / 255, 42 / 255))
panel = _color(widget, "panel-color", (58 / 255, 58 / 255, 62 / 255))
plate = _color(widget, "plate-color", (210 / 255, 210 / 255, 210 / 255))
greenlit = _color(widget, "greenlit-color",
(60 / 255, 255 / 255, 90 / 255))
redlit = _color(widget, "redlit-color",
(255 / 255, 70 / 255, 60 / 255))
dimred = _color(widget, "dimred-color", (120 / 255, 30 / 255, 30 / 255))
on = _on(widget)
w, h = widget.width, widget.height
cx = w / 2
_rounded_box(cr, 6, 6, w - 12, h - 12, panel, bezel, radius=8)
_rounded_box(cr, cx - 16, 10, 32, h - 20, plate, bezel, radius=6)
pivot_x, pivot_y = cx, h / 2
lever_w = max(2, _prop_int(widget, "lever-line-width", 10))
lever_len = h * 0.32
angle = -math.pi / 2 if on else math.pi / 2
tip_x = pivot_x + lever_len * math.cos(angle)
tip_y = pivot_y + lever_len * math.sin(angle)
cr.save()
_rgb(cr, bezel)
cr.set_line_width(lever_w)
cr.set_line_cap(1)
cr.move_to(pivot_x, pivot_y)
cr.line_to(tip_x, tip_y)
cr.stroke()
_rgb(cr, (230 / 255, 230 / 255, 230 / 255))
cr.arc(tip_x, tip_y, lever_w / 2 + 2, 0, 2 * math.pi)
cr.fill()
_rgb(cr, bezel)
cr.arc(pivot_x, pivot_y, 6, 0, 2 * math.pi)
cr.fill()
cr.restore()
if illuminated:
_rgb(cr, greenlit if on else dimred)
cr.arc(cx + 22, 18, 5, 0, 2 * math.pi)
cr.fill()
else:
_rgb(cr, greenlit if on else redlit)
cr.arc(cx + 22, 18, 4, 0, 2 * math.pi)
cr.fill()
_wtext(cr, widget, "ON" if on else "OFF", cx, h - 8, size=10,
align="center", rgb=(0.9, 0.9, 0.9), max_width=40)
def _draw_slide_switch(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
bezel = _color(widget, "bezel-color", (38 / 255, 38 / 255, 42 / 255))
panel = _color(widget, "panel-color", (58 / 255, 58 / 255, 62 / 255))
rocker = _color(widget, "rocker-color",
(190 / 255, 190 / 255, 195 / 255))
green = _color(widget, "green-color", (0.0, 180 / 255, 0.0))
on = _on(widget)
w, h = widget.width, widget.height
_rounded_box(cr, 6, 6, w - 12, h - 12, panel, bezel, radius=8)
track_h = 16
ty = h / 2 - track_h / 2
_rounded_box(cr, 14, ty, w - 28, track_h, bezel, bezel,
radius=track_h / 2)
knob_w = 30
kx = (w - 14 - knob_w) if on else 14
_rounded_box(cr, kx, ty - 5, knob_w, track_h + 10, rocker, bezel,
radius=6)
_wtext(cr, widget, "ON" if on else "OFF", w / 2, h - 8, size=10,
align="center", rgb=(0.9, 0.9, 0.9), max_width=40)
def _draw_key_switch(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
bezel = _color(widget, "bezel-color", (38 / 255, 38 / 255, 42 / 255))
panel = _color(widget, "panel-color", (58 / 255, 58 / 255, 62 / 255))
plate = _color(widget, "plate-color", (210 / 255, 210 / 255, 210 / 255))
amber = _color(widget, "amber-color", (255 / 255, 176 / 255, 0.0))
key_in = _prop_bool(widget, "key-in", False)
w, h = widget.width, widget.height
cx, cy = w / 2, h / 2
r = min(w, h) / 2 - 10
if r < 4:
return
_rounded_box(cr, 6, 6, w - 12, h - 12, panel, bezel, radius=8)
cr.save()
_rgb(cr, plate)
cr.arc(cx, cy, r, 0, 2 * math.pi)
cr.fill_preserve()
_rgb(cr, bezel)
cr.set_line_width(2)
cr.stroke()
# Key slot pointing up; positions around it.
_rgb(cr, bezel)
cr.set_line_width(max(2, _prop_int(widget, "pointer-line-width",
10) // 2))
cr.move_to(cx, cy)
cr.line_to(cx, cy - r + 8)
cr.stroke()
if key_in:
_rgb(cr, amber)
cr.set_line_width(4)
cr.move_to(cx, cy - r + 8)
cr.line_to(cx, cy - r - 6)
cr.stroke()
_rgb(cr, amber)
cr.arc(cx, cy - r - 6, 5, 0, 2 * math.pi)
cr.fill()
_rgb(cr, bezel)
cr.arc(cx, cy, 5, 0, 2 * math.pi)
cr.fill()
cr.restore()
_wtext(cr, widget, "KEY", cx, h - 8, size=10, align="center",
rgb=(0.9, 0.9, 0.9), max_width=40)
def _draw_rotary(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
bezel = _color(widget, "bezel-color", (38 / 255, 38 / 255, 42 / 255))
panel = _color(widget, "panel-color", (58 / 255, 58 / 255, 62 / 255))
plate = _color(widget, "plate-color", (210 / 255, 210 / 255, 210 / 255))
positions = max(2, _prop_int(widget, "positions", 3))
position = max(0, min(positions - 1,
_prop_int(widget, "position", 0)))
w, h = widget.width, widget.height
cx, cy = w / 2, h / 2
r = min(w, h) / 2 - 14
if r < 4:
return
_rounded_box(cr, 6, 6, w - 12, h - 12, panel, bezel, radius=8)
for i in range(positions):
angle = -math.pi * 0.75 + i * (math.pi * 1.5
/ (positions - 1))
tx = cx + (r + 8) * math.cos(angle)
ty = cy + (r + 8) * math.sin(angle)
_wtext(cr, widget, str(i), tx, ty + 4, size=10,
align="center", rgb=(0.9, 0.9, 0.9), max_width=16)
cr.save()
_rgb(cr, plate)
cr.arc(cx, cy, r, 0, 2 * math.pi)
cr.fill_preserve()
_rgb(cr, bezel)
cr.set_line_width(2)
cr.stroke()
angle = -math.pi * 0.75 + position * (math.pi * 1.5
/ (positions - 1))
_rgb(cr, bezel)
cr.set_line_width(max(2, _prop_int(widget, "pointer-line-width",
8)))
cr.set_line_cap(1)
cr.move_to(cx, cy)
cr.line_to(cx + (r - 6) * math.cos(angle),
cy + (r - 6) * math.sin(angle))
cr.stroke()
_rgb(cr, bezel)
cr.arc(cx, cy, 5, 0, 2 * math.pi)
cr.fill()
cr.restore()
def _draw_push_button(cr, widget, latching=False):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
bezel = _color(widget, "bezel-color", (38 / 255, 38 / 255, 42 / 255))
greenlit = _color(widget, "greenlit-color",
(60 / 255, 255 / 255, 90 / 255))
redlit = _color(widget, "redlit-color",
(255 / 255, 70 / 255, 60 / 255))
on = _on(widget)
w, h = widget.width, widget.height
cx, cy = w / 2, h / 2 + 4
r = min(w, h) / 2 - 12
if r < 4:
return
cr.save()
_rgb(cr, bezel)
cr.arc(cx, cy, r + 5, 0, 2 * math.pi)
cr.fill()
face = (0.75, 0.75, 0.78) if (on and latching) else \
(0.9, 0.9, 0.92)
_rgb(cr, face)
cr.arc(cx, cy, r, 0, 2 * math.pi)
cr.fill_preserve()
_rgb(cr, bezel)
cr.set_line_width(2)
cr.stroke()
_rgb(cr, (1.0, 1.0, 1.0), 0.5)
cr.arc(cx - r * 0.3, cy - r * 0.35, r * 0.2, 0, 2 * math.pi)
cr.fill()
cr.restore()
_rgb(cr, greenlit if on else redlit)
cr.arc(cx, cy - r - 8, 5, 0, 2 * math.pi)
cr.fill()
_wtext(cr, widget, widget.text if widget.text != widget.name
else ("PUSH" if not latching else "LATCH"), cx, h - 6,
size=11, align="center", rgb=(0.25, 0.25, 0.25),
max_width=w - 8)
def _draw_emergency_stop(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
surround = _color(widget, "surround-color",
(255 / 255, 220 / 255, 0.0))
red = _color(widget, "red-color", (200 / 255, 30 / 255, 30 / 255))
redlit = _color(widget, "redlit-color",
(255 / 255, 70 / 255, 60 / 255))
on = _on(widget)
w, h = widget.width, widget.height
cx, cy = w / 2, h / 2
r = min(w, h) / 2 - 10
if r < 4:
return
_rounded_box(cr, 8, 8, w - 16, h - 16, surround, surround, radius=8)
cr.save()
_rgb(cr, (40 / 255, 40 / 255, 40 / 255))
cr.rectangle(cx - 8, cy - 4, 16, r)
cr.fill()
_rgb(cr, redlit if on else red)
cr.arc(cx, cy - (2 if on else 6), r * 0.8, 0, 2 * math.pi)
cr.fill_preserve()
_rgb(cr, (120 / 255, 20 / 255, 20 / 255))
cr.set_line_width(2)
cr.stroke()
_rgb(cr, (1.0, 1.0, 1.0), 0.4)
cr.arc(cx - r * 0.25, cy - r * 0.35, r * 0.18, 0, 2 * math.pi)
cr.fill()
cr.restore()
_wtext(cr, widget, "STOP", cx, h - 6, size=11, bold=True,
align="center", rgb=(0.6, 0.1, 0.1), max_width=w - 8)
def _draw_circuit_breaker(cr, widget):
_bg(cr, widget, (1.0, 1.0, 1.0), stroke=(0.55, 0.55, 0.55))
bezel = _color(widget, "bezel-color", (38 / 255, 38 / 255, 42 / 255))
panel = _color(widget, "panel-color", (58 / 255, 58 / 255, 62 / 255))
plate = _color(widget, "plate-color", (210 / 255, 210 / 255, 210 / 255))
green = _color(widget, "green-color", (0.0, 180 / 255, 0.0))
red = _color(widget, "red-color", (200 / 255, 30 / 255, 30 / 255))
w, h = widget.width, widget.height
_rounded_box(cr, 6, 6, w - 12, h - 12, panel, bezel, radius=8)
bw, bh = min(64, w - 40), h - 36
bx, by = (w - bw) / 2, 14
if bw <= 0 or bh <= 0:
return
_rounded_box(cr, bx, by, bw, bh, plate, bezel, radius=6)
lever_w = max(4, _prop_int(widget, "lever-line-width", 12))
cr.save()
_rgb(cr, bezel)
cr.set_line_width(lever_w)
cr.set_line_cap(1)
cr.move_to(bx + bw / 2, by + bh / 2 + 12)
cr.line_to(bx + bw / 2, by + 12)
cr.stroke()
cr.restore()
_rgb(cr, green)
cr.arc(bx + bw - 10, by + 12, 5, 0, 2 * math.pi)
cr.fill()
_rgb(cr, red)
cr.arc(bx + bw - 10, by + bh - 12, 5, 0, 2 * math.pi)
cr.fill()
_wtext(cr, widget, "ON", bx + bw / 2, by + bh - 4, size=10,
align="center", rgb=(0.25, 0.25, 0.25), max_width=bw)
# --- panel furniture -----------------------------------------------------------
def _draw_bezel(cr, widget):
panel = _color(widget, "panel-color", (210 / 255, 210 / 255, 205 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
_bg(cr, widget, panel, stroke=ink)
margin = 14
cr.save()
_rgb(cr, (1.0, 1.0, 1.0))
cr.rectangle(margin, margin, widget.width - 2 * margin,
widget.height - 2 * margin)
cr.fill_preserve()
_rgb(cr, ink)
cr.set_line_width(1.5)
cr.stroke()
cr.restore()
_panel_screws(cr, widget)
if widget.text != widget.name:
_wtext(cr, widget, widget.text, widget.width / 2, 12, size=11,
align="center", rgb=ink, max_width=widget.width - 40)
def _draw_panel_plate(cr, widget):
panel = _color(widget, "panel-color", (210 / 255, 210 / 255, 205 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
amber = _color(widget, "amber-color", (255 / 255, 176 / 255, 0.0))
_bg(cr, widget, panel, stroke=ink)
_box(cr, 8, 8, widget.width - 16, 22, amber, amber)
_wtext(cr, widget, widget.text if widget.text != widget.name
else "PANEL", widget.width / 2, 24, size=12, bold=True,
align="center", rgb=(0.15, 0.15, 0.15),
max_width=widget.width - 32)
_panel_screws(cr, widget)
def _draw_nameplate(cr, widget):
dark = _color(widget, "dark-color", (30 / 255, 30 / 255, 30 / 255))
engraved = _color(widget, "engraved-color",
(240 / 255, 220 / 255, 160 / 255))
_rounded_box(cr, 0, 0, widget.width, widget.height, dark, dark,
radius=6)
_wtext(cr, widget, widget.text if widget.text != widget.name
else "NAMEPLATE", widget.width / 2, widget.height / 2 + 5,
size=13, bold=True, align="center", rgb=engraved,
max_width=widget.width - 40)
_screw(cr, 12, widget.height / 2)
_screw(cr, widget.width - 12, widget.height / 2)
def _draw_control_panel(cr, widget):
face = _color(widget, "face-color", (245 / 255, 245 / 255, 240 / 255))
ink = _color(widget, "ink-color", (40 / 255, 40 / 255, 40 / 255))
panel = _color(widget, "panel-color", (210 / 255, 210 / 255, 205 / 255))
amber = _color(widget, "amber-color", (255 / 255, 176 / 255, 0.0))
rows = max(1, _prop_int(widget, "grid-rows", 3))
cols = max(1, _prop_int(widget, "grid-cols", 2))
_bg(cr, widget, panel, stroke=ink)
_box(cr, 8, 8, widget.width - 16, 22, amber, amber)
_wtext(cr, widget, widget.text if widget.text != widget.name
else "CONTROL", widget.width / 2, 24, size=12, bold=True,
align="center", rgb=(0.15, 0.15, 0.15),
max_width=widget.width - 32)
_panel_screws(cr, widget)
grid_x, grid_y = 14, 38
grid_w, grid_h = widget.width - 28, widget.height - 52
if grid_w <= 0 or grid_h <= 0:
return
cell_w, cell_h = grid_w / cols, grid_h / rows
labels = ("PUMP", "VALVE", "MOTOR", "FAN", "LIGHT", "HEATER")
for r in range(rows):
for c in range(cols):
x, y = grid_x + c * cell_w, grid_y + r * cell_h
_box(cr, x + 2, y + 2, cell_w - 4, cell_h - 4, face, ink)
label = labels[(r * cols + c) % len(labels)]
_wtext(cr, widget, label, x + cell_w / 2,
y + cell_h / 2 + 4, size=11, align="center",
rgb=ink, max_width=cell_w - 8)
# --- widget class --------------------------------------------------------------
_INST_DRAW = {
"AnalogGauge": _draw_analog_gauge,
"BarMeter": _draw_bar_meter,
"Beacon": _draw_beacon,
"Bezel": _draw_bezel,
"CircuitBreaker": _draw_circuit_breaker,
"ControlPanel": _draw_control_panel,
"DigitalReadout": _draw_digital_readout,
"EmergencyStop": _draw_emergency_stop,
"FlowMeter": _draw_flow_meter,
"HorizontalTankLevel": _draw_horizontal_tank,
"KeySwitch": _draw_key_switch,
"LampTestButton": _draw_lamp_test_button,
"LedBlue": _draw_led,
"LedGreen": _draw_led,
"LedRed": _draw_led,
"LevelColumnGauge": _draw_level_column,
"LevelTransmitter": _draw_level_transmitter,
"Manometer": _draw_manometer,
"Nameplate": _draw_nameplate,
"NeedleEdgewise": _draw_needle_edgewise,
"NeedleKnifeEdge": _draw_needle_knife,
"NeedleTapered": _draw_needle_tapered,
"NeedleVu": _draw_needle_vu,
"OpenChannelLevel": _draw_open_channel,
"PanelLampAmber": _draw_panel_lamp,
"PanelLampBlue": _draw_panel_lamp,
"PanelLampGreen": _draw_panel_lamp,
"PanelLampRed": _draw_panel_lamp,
"PanelLampWhite": _draw_panel_lamp,
"PanelPlate": _draw_panel_plate,
"PressureGauge": _draw_pressure_gauge,
"PushButton": lambda cr, w: _draw_push_button(cr, w, False),
"PushButtonLatching": lambda cr, w: _draw_push_button(cr, w, True),
"RgbStatusLed": _draw_rgb_led,
"RockerSwitch": lambda cr, w: _draw_rocker(cr, w, False, False),
"RockerSwitchDouble": lambda cr, w: _draw_rocker(cr, w, False, True),
"RockerSwitchIlluminated": lambda cr, w: _draw_rocker(cr, w, True,
False),
"Rotary": _draw_rotary,
"SightGlassLevel": _draw_sight_glass,
"SlideSwitch": _draw_slide_switch,
"SphericalTankLevel": _draw_spherical_tank,
"StackLight": _draw_stack_light,
"TankLevelAlarms": _draw_tank_alarms,
"TemperatureGauge": _draw_temperature_gauge,
"ToggleSwitch": lambda cr, w: _draw_toggle_switch(cr, w, False),
"ToggleSwitchIlluminated": lambda cr, w: _draw_toggle_switch(cr, w,
True),
"VerticalTankLevel": _draw_vertical_tank,
"VuMeter": _draw_vu_meter,
}
def _draw_seg_kind(cr, widget):
kind = widget.pkind
if kind.startswith("LedDigits"):
_draw_led_digits(cr, widget)
else:
_draw_seg_display(cr, widget)
class InstWidget(Widget):
"""One of the 66 Instrumentation widgets (any palette archetype
in the Instrumentation section, drawn here instead of the generic
archetype drawing)."""
ARCHETYPE = "Instrument"
def _draw(self, cr):
kind = self.pkind
if kind.startswith(("LedDigits", "Seg14Display",
"Seg16Display")):
_draw_seg_kind(cr, self)
return
draw = _INST_DRAW.get(kind)
if draw is not None:
draw(cr, self)
return
_bg(cr, self, (0.96, 0.97, 0.99), stroke=(0.5, 0.55, 0.65))
_wtext(cr, self, kind, self.width / 2, self.height / 2 + 5,
size=12, align="center", rgb=(0.4, 0.4, 0.4),
max_width=self.width - 12)