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).
This commit is contained in:
1 parent
cff0c74605
commit
2d52b55743
11 files changed
+1123
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,195 @@
|
||||
#!/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())
|
||||
@@ -0,0 +1,198 @@
|
||||
#!/usr/bin/env python3
|
||||
# Generated by LightGUI (Python/XFrame).
|
||||
# A plain Python/GTK program: no CMake. Run with:
|
||||
# python3 xhistorygraph.py
|
||||
# with the LightGUI Python package on PYTHONPATH
|
||||
# (the designer sets it when it runs this script).
|
||||
#
|
||||
# The window is 'XHistoryGraph 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_XHistoryGraph_001_OnClick(widget):
|
||||
print("EVENT %s %s" % (widget.name, 'OnClick'))
|
||||
|
||||
# Event handlers, wired to the widgets in main():
|
||||
_HANDLERS = {
|
||||
('XHistoryGraph_001', 'OnClick'): on_XHistoryGraph_001_OnClick,
|
||||
}
|
||||
|
||||
|
||||
# Design-time geometry + effective anchors per widget:
|
||||
# name -> (top, left, right, bottom, x, y, width, height)
|
||||
_ANCHORS = {
|
||||
'XHistoryGraph_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('XHistoryGraph', 'XHistoryGraph_001', 16, 16, 320, 200)
|
||||
widget.set_prop("label", "CPU / MEM / IO")
|
||||
widget.set_prop("series-count", 3)
|
||||
widget.set_prop("series-names", "CPU|MEM|IO")
|
||||
# Seeded demo data: 60 samples so the graph is full at launch.
|
||||
for i in range(60):
|
||||
widget.push(55 + 25 * math.sin(i / 6.0), series=0)
|
||||
widget.push(40 + 18 * math.sin(i / 8.0 + 2.0), series=1)
|
||||
widget.push(25 + 12 * math.sin(i / 5.0 + 4.0), series=2)
|
||||
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]
|
||||
widget.push(55 + 25 * math.sin(t / 6.0), series=0)
|
||||
widget.push(40 + 18 * math.sin(t / 8.0 + 2.0), series=1)
|
||||
widget.push(25 + 12 * math.sin(t / 5.0 + 4.0), series=2)
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title='XHistoryGraph 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())
|
||||
@@ -0,0 +1,194 @@
|
||||
#!/usr/bin/env python3
|
||||
# Generated by LightGUI (Python/XFrame).
|
||||
# A plain Python/GTK program: no CMake. Run with:
|
||||
# python3 xnetgraph.py
|
||||
# with the LightGUI Python package on PYTHONPATH
|
||||
# (the designer sets it when it runs this script).
|
||||
#
|
||||
# The window is 'XNetGraph 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_XNetGraph_001_OnClick(widget):
|
||||
print("EVENT %s %s" % (widget.name, 'OnClick'))
|
||||
|
||||
# Event handlers, wired to the widgets in main():
|
||||
_HANDLERS = {
|
||||
('XNetGraph_001', 'OnClick'): on_XNetGraph_001_OnClick,
|
||||
}
|
||||
|
||||
|
||||
# Design-time geometry + effective anchors per widget:
|
||||
# name -> (top, left, right, bottom, x, y, width, height)
|
||||
_ANCHORS = {
|
||||
'XNetGraph_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('XNetGraph', 'XNetGraph_001', 16, 16, 320, 200)
|
||||
widget.set_prop("label", "Network enp3s0")
|
||||
# Seeded demo data: 60 down/up samples.
|
||||
for i in range(60):
|
||||
widget.push_net(18000 + 12000 * math.sin(i / 7.0),
|
||||
3000 + 2000 * math.sin(i / 4.0 + 1.0))
|
||||
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]
|
||||
widget.push_net(18000 + 12000 * math.sin(t / 7.0),
|
||||
3000 + 2000 * math.sin(t / 4.0 + 1.0))
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title='XNetGraph 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())
|
||||
@@ -0,0 +1,193 @@
|
||||
#!/usr/bin/env python3
|
||||
# Generated by LightGUI (Python/XFrame).
|
||||
# A plain Python/GTK program: no CMake. Run with:
|
||||
# python3 xtemphistory.py
|
||||
# with the LightGUI Python package on PYTHONPATH
|
||||
# (the designer sets it when it runs this script).
|
||||
#
|
||||
# The window is 'XTempHistory 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_XTempHistory_001_OnClick(widget):
|
||||
print("EVENT %s %s" % (widget.name, 'OnClick'))
|
||||
|
||||
# Event handlers, wired to the widgets in main():
|
||||
_HANDLERS = {
|
||||
('XTempHistory_001', 'OnClick'): on_XTempHistory_001_OnClick,
|
||||
}
|
||||
|
||||
|
||||
# Design-time geometry + effective anchors per widget:
|
||||
# name -> (top, left, right, bottom, x, y, width, height)
|
||||
_ANCHORS = {
|
||||
'XTempHistory_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('XTempHistory', 'XTempHistory_001', 16, 16, 320, 200)
|
||||
widget.set_prop("label", "CPU temp")
|
||||
widget.set_prop("threshold", "75")
|
||||
# Seeded demo data: 60 samples, crossing the 75C threshold.
|
||||
for i in range(60):
|
||||
widget.push(58 + 24 * math.sin(i / 6.0))
|
||||
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]
|
||||
widget.push(58 + 24 * math.sin(t / 6.0))
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title='XTempHistory 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())
|
||||
@@ -0,0 +1,190 @@
|
||||
#!/usr/bin/env python3
|
||||
# Generated by LightGUI (Python/XFrame).
|
||||
# A plain Python/GTK program: no CMake. Run with:
|
||||
# python3 xusagebar.py
|
||||
# with the LightGUI Python package on PYTHONPATH
|
||||
# (the designer sets it when it runs this script).
|
||||
#
|
||||
# The window is 'XUsageBar example' (352x68) 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_XUsageBar_001_OnClick(widget):
|
||||
print("EVENT %s %s" % (widget.name, 'OnClick'))
|
||||
|
||||
# Event handlers, wired to the widgets in main():
|
||||
_HANDLERS = {
|
||||
('XUsageBar_001', 'OnClick'): on_XUsageBar_001_OnClick,
|
||||
}
|
||||
|
||||
|
||||
# Design-time geometry + effective anchors per widget:
|
||||
# name -> (top, left, right, bottom, x, y, width, height)
|
||||
_ANCHORS = {
|
||||
'XUsageBar_001': (True, True, False, False, 16, 16, 320, 36),
|
||||
}
|
||||
|
||||
|
||||
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('XUsageBar', 'XUsageBar_001', 16, 16, 320, 36)
|
||||
widget.set_prop("label", "Memory")
|
||||
widget.set_prop("value", "65")
|
||||
widgets.append(widget)
|
||||
return widgets
|
||||
|
||||
|
||||
_DESIGN_SIZE = (352, 68)
|
||||
_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]
|
||||
widget.set_value(65 + 10 * math.sin(t / 8.0))
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title='XUsageBar example')
|
||||
window.set_default_size(352, 68)
|
||||
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())
|
||||
@@ -0,0 +1,153 @@
|
||||
"""Shoot one screenshot per X* monitor example on hot_dallas.
|
||||
|
||||
Recipe (from /opt/lightgui-docs/shoot.py, adapted): Xvfb :107,
|
||||
launch the example, find the window by title with xdotool,
|
||||
ImageMagick `import -window`, root-crop fallback if the shot comes
|
||||
back pure black. Every shot is then verified non-blank with PIL
|
||||
(distinct colors + grayscale stddev) -- a file present is not
|
||||
proof it rendered.
|
||||
|
||||
Usage: python3 shoot_monitor.py [name ...]
|
||||
With no args, shoots all 15. Saves to examples/shots/<name>.png.
|
||||
"""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
REPO = "/root/lg-review/lightgui_python"
|
||||
DISPLAY = ":107"
|
||||
SHOTS = os.path.join(REPO, "examples", "shots")
|
||||
|
||||
NAMES = [
|
||||
"XHistoryGraph", "XCoreGrid", "XNetGraph", "XTempHistory",
|
||||
"XUsageBar", "XStackBar", "XVerticalBar", "XTachometer",
|
||||
"XArcGauge", "XDonutGauge", "XProcessList", "XLoadBadge",
|
||||
"XMonitorButton", "XMonitorGrid", "XSysBox",
|
||||
]
|
||||
|
||||
|
||||
def run(cmd, env=None, timeout=30):
|
||||
full_env = dict(os.environ)
|
||||
full_env["DISPLAY"] = DISPLAY
|
||||
if env:
|
||||
full_env.update(env)
|
||||
return subprocess.run(cmd, env=full_env, capture_output=True,
|
||||
text=True, timeout=timeout)
|
||||
|
||||
|
||||
def ensure_xvfb():
|
||||
if os.path.exists("/tmp/.X11-unix/X107"):
|
||||
return
|
||||
subprocess.Popen(
|
||||
["Xvfb", DISPLAY, "-screen", "0", "1024x768x24"],
|
||||
stdout=open("/tmp/xvfb107.log", "w"),
|
||||
stderr=subprocess.STDOUT)
|
||||
for _ in range(20):
|
||||
if os.path.exists("/tmp/.X11-unix/X107"):
|
||||
return
|
||||
time.sleep(0.5)
|
||||
raise RuntimeError("Xvfb :107 did not start")
|
||||
|
||||
|
||||
def find_window(title, timeout=15.0):
|
||||
# 15s: cold GTK startup on a loaded box can take ~5s, and each
|
||||
# xdotool poll costs ~0.5s, so a 6s window only gets ~7 polls.
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
proc = run(["xdotool", "search", "--name", title])
|
||||
wids = proc.stdout.split()
|
||||
if wids:
|
||||
return wids[0]
|
||||
time.sleep(0.25)
|
||||
return None
|
||||
|
||||
|
||||
def black_fraction(path):
|
||||
from PIL import Image
|
||||
img = Image.open(path).convert("RGB")
|
||||
pixels = list(img.getdata())
|
||||
black = sum(1 for r, g, b in pixels if r < 8 and g < 8 and b < 8)
|
||||
return black / max(1, len(pixels))
|
||||
|
||||
|
||||
def verify(path):
|
||||
"""(distinct_colors, stddev) -- non-blank means colors > 50
|
||||
and stddev > 3 (dark panels still have text + graph lines)."""
|
||||
from PIL import Image, ImageStat
|
||||
img = Image.open(path).convert("RGB")
|
||||
colors = len(img.getcolors(maxcolors=1000000) or [])
|
||||
stddev = sum(ImageStat.Stat(img.convert("L")).stddev)
|
||||
return colors, stddev
|
||||
|
||||
|
||||
def shoot(name):
|
||||
lower = name.lower()
|
||||
example = os.path.join(REPO, "examples", lower + ".py")
|
||||
out = os.path.join(SHOTS, lower + ".png")
|
||||
title = "%s example" % name
|
||||
env = {"PYTHONPATH": REPO}
|
||||
proc = subprocess.Popen(
|
||||
["python3", example],
|
||||
env={**os.environ, "DISPLAY": DISPLAY, "PYTHONPATH": REPO},
|
||||
stdout=open("/tmp/shot-%s.log" % lower, "w"),
|
||||
stderr=subprocess.STDOUT)
|
||||
try:
|
||||
wid = find_window(title)
|
||||
if wid is None:
|
||||
return "NOWINDOW"
|
||||
time.sleep(3.0) # let live data flow (process CPU% needs 2+ ticks)
|
||||
run(["import", "-window", wid, out])
|
||||
if not os.path.exists(out) or black_fraction(out) > 0.5:
|
||||
# Root-crop fallback (shoot.py recipe).
|
||||
geo = run(["xdotool", "getwindowgeometry", wid]).stdout
|
||||
# "Window 123\n Position: 100,100 (screen: 0)\n Geometry: 352x232"
|
||||
geom = None
|
||||
for line in geo.splitlines():
|
||||
if "Geometry:" in line:
|
||||
geom = line.split("Geometry:")[1].strip()
|
||||
if geom:
|
||||
w, h = geom.split("x")
|
||||
pos = None
|
||||
for line in geo.splitlines():
|
||||
if "Position:" in line:
|
||||
pos = line.split("Position:")[1].split("(")[0].strip()
|
||||
x, y = pos.split(",") if pos else ("0", "0")
|
||||
run(["import", "-window", "root", "-crop",
|
||||
"%sx%s+%s+%s" % (w, h, x, y), out])
|
||||
if not os.path.exists(out):
|
||||
return "SHOTFAIL"
|
||||
colors, stddev = verify(out)
|
||||
if colors <= 50 or stddev <= 3:
|
||||
return "BLANK(colors=%d stddev=%.1f)" % (colors, stddev)
|
||||
return "OK(colors=%d stddev=%.1f)" % (colors, stddev)
|
||||
finally:
|
||||
proc.terminate()
|
||||
try:
|
||||
proc.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
|
||||
|
||||
def main():
|
||||
names = sys.argv[1:] or NAMES
|
||||
ensure_xvfb()
|
||||
os.makedirs(SHOTS, exist_ok=True)
|
||||
results = {}
|
||||
for name in names:
|
||||
try:
|
||||
results[name] = shoot(name)
|
||||
except Exception as exc: # noqa: BLE001 - report per widget
|
||||
results[name] = "ERROR %r" % (exc,)
|
||||
print("%-16s %s" % (name, results[name]), flush=True)
|
||||
failed = [n for n, r in results.items() if not r.startswith("OK")]
|
||||
print("shot %d/%d OK" % (len(results) - len(failed), len(results)))
|
||||
if failed:
|
||||
print("FAILED: %s" % ", ".join(failed))
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user