Files
Muse 2d52b55743 Examples + shots: XHistoryGraph, XCoreGrid, XNetGraph, XTempHistory, XUsageBar
Designer-pattern examples (xgraph.py harness + live-demo GLib timer
feeding seeded/live sine data). Screenshots under Xvfb :107 via
tools/shoot_monitor.py (xdotool + import, PIL non-blank check).
2026-12-05 17:07:40 +00:00

196 lines
5.6 KiB
Python

#!/usr/bin/env python3
# Generated by LightGUI (Python/XFrame).
# A plain Python/GTK program: no CMake. Run with:
# python3 xcoregrid.py
# with the LightGUI Python package on PYTHONPATH
# (the designer sets it when it runs this script).
#
# The window is 'XCoreGrid example' (352x232) with 1 widget(s).
import math
import sys
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk, GLib, Gtk # noqa: E402
from lightgui.xframe.core import create, parse_color # noqa: E402
def on_XCoreGrid_001_OnClick(widget):
print("EVENT %s %s" % (widget.name, 'OnClick'))
# Event handlers, wired to the widgets in main():
_HANDLERS = {
('XCoreGrid_001', 'OnClick'): on_XCoreGrid_001_OnClick,
}
# Design-time geometry + effective anchors per widget:
# name -> (top, left, right, bottom, x, y, width, height)
_ANCHORS = {
'XCoreGrid_001': (True, True, False, False, 16, 16, 320, 200),
}
def _apply_anchors(widgets, width, height):
"""Reposition anchored widgets for a drawing area of
*width* x *height* (Flag 1: minimal anchor layout).
Computed from the design geometry every time, so a
resize is idempotent — no drift across resizes."""
design_w, design_h = _DESIGN_SIZE
dx = width - design_w
dy = height - design_h
for widget in widgets:
top, left, right, bottom, ox, oy, ow, oh = \
_ANCHORS[widget.name]
x, y, w, h = ox, oy, ow, oh
if right and not left:
x = ox + dx
elif right and left:
w = ow + dx
if bottom and not top:
y = oy + dy
elif bottom and top:
h = oh + dy
widget.move_to(x, y)
widget.resize_to(w, h)
def _build_widgets():
"""Create every placed widget with its props."""
widgets = []
widget = create('XCoreGrid', 'XCoreGrid_001', 16, 16, 320, 200)
widget.set_prop("label", "CPU cores")
widget.set_prop("cores", 8)
# Seeded demo data: 60 samples per core.
for i in range(60):
for c in range(8):
widget.push_core(c, 45 + 35 * math.sin(i / (5.0 + c) + c))
widgets.append(widget)
return widgets
_DESIGN_SIZE = (352, 232)
_BACKGROUND = ''
_WIDGETS = []
_LAST_SIZE = [None]
_PRESSED = [None] # Task 4: widget that got the ButtonPress
def _hit_widget(widgets, x, y):
"""Topmost visible widget containing (*x*, *y*)."""
for widget in reversed(widgets):
if widget.get_prop("visible", "true") in ("false", "0"):
continue
if widget.contains(x, y):
return widget
return None
def _dispatch_to(widget, event):
"""Call the *event* handler of *widget* (already chosen)."""
if widget is None:
return
if widget.get_prop("enabled", "true") in ("false", "0"):
return
handler = _HANDLERS.get((widget.name, event))
if handler is not None:
handler(widget)
def _dispatch(widgets, x, y, event):
"""Call the *event* handler of the widget at (*x*, *y*),
if it has one wired."""
_dispatch_to(_hit_widget(widgets, x, y), event)
def _on_draw(area, cr):
bg = parse_color(_BACKGROUND, None)
if bg is None:
bg = (1.0, 1.0, 1.0)
cr.set_source_rgb(*bg)
cr.paint()
for widget in _WIDGETS:
widget.draw(cr)
return False
def _on_button_press(area, event):
if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS:
_dispatch(_WIDGETS, event.x, event.y, "OnDblClick")
elif event.type == Gdk.EventType.BUTTON_PRESS:
_dispatch(_WIDGETS, event.x, event.y, "OnMouseDown")
_dispatch(_WIDGETS, event.x, event.y, "OnClick")
# Task 4: remember the pressed widget; release goes there.
_PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y)
return False
def _on_button_release(area, event):
# Task 4: release goes to the widget that got the press,
# even if the pointer moved off it.
widget = _PRESSED[0]
_PRESSED[0] = None
_dispatch_to(widget, "OnMouseUp")
return False
def _on_area_resize(area, allocation):
size = (allocation.width, allocation.height)
if size == _LAST_SIZE[0]:
return False
_LAST_SIZE[0] = size
_apply_anchors(_WIDGETS, size[0], size[1])
area.queue_draw()
return False
# --- live demo (hand-added, not designer codegen) ----------------------------
# The widget shows live data: _tick() feeds a new sample
# every 1000 ms and redraws the drawing area. _TICK starts
# at 60 so the timer continues the seeded sine waves
# from _build_widgets() without a phase jump.
_TICK = [60]
_AREA = []
def _tick():
_TICK[0] += 1
t = _TICK[0]
widget = _WIDGETS[0]
for c in range(8):
widget.push_core(c, 45 + 35 * math.sin(t / (5.0 + c) + c))
if _AREA:
_AREA[0].queue_draw()
return True
def main():
window = Gtk.Window(title='XCoreGrid example')
window.set_default_size(352, 232)
window.connect("destroy", Gtk.main_quit)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
spacing=0)
window.add(vbox)
area = Gtk.DrawingArea()
area.set_hexpand(True)
area.set_vexpand(True)
area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
Gdk.EventMask.BUTTON_RELEASE_MASK)
area.connect("draw", _on_draw)
area.connect("button-press-event", _on_button_press)
area.connect("button-release-event", _on_button_release)
area.connect("size-allocate", _on_area_resize)
vbox.pack_start(area, True, True, 0)
_WIDGETS.extend(_build_widgets())
_AREA.append(area)
GLib.timeout_add(1000, _tick)
window.show_all()
Gtk.main()
return 0
if __name__ == "__main__":
sys.exit(main())