Examples + shots: XStackBar, XVerticalBar, XTachometer, XArcGauge, XDonutGauge
Designer-pattern examples + Xvfb :107 screenshots (see Batch B for the harness/shooter). Gauges re-shot after the _arc_stroke fix; tachometer readout moved clear of the 0/max labels.
This commit is contained in:
1 parent
2d52b55743
commit
2ecc09dd4d
10 files changed
+937
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 7.3 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 7.4 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.7 KiB |
@@ -0,0 +1,191 @@
|
||||
#!/usr/bin/env python3
|
||||
# Generated by LightGUI (Python/XFrame).
|
||||
# A plain Python/GTK program: no CMake. Run with:
|
||||
# python3 xarcgauge.py
|
||||
# with the LightGUI Python package on PYTHONPATH
|
||||
# (the designer sets it when it runs this script).
|
||||
#
|
||||
# The window is 'XArcGauge example' (232x172) 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_XArcGauge_001_OnClick(widget):
|
||||
print("EVENT %s %s" % (widget.name, 'OnClick'))
|
||||
|
||||
# Event handlers, wired to the widgets in main():
|
||||
_HANDLERS = {
|
||||
('XArcGauge_001', 'OnClick'): on_XArcGauge_001_OnClick,
|
||||
}
|
||||
|
||||
|
||||
# Design-time geometry + effective anchors per widget:
|
||||
# name -> (top, left, right, bottom, x, y, width, height)
|
||||
_ANCHORS = {
|
||||
'XArcGauge_001': (True, True, False, False, 16, 16, 200, 140),
|
||||
}
|
||||
|
||||
|
||||
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('XArcGauge', 'XArcGauge_001', 16, 16, 200, 140)
|
||||
widget.set_prop("label", "CPU load")
|
||||
widget.set_prop("value", "73")
|
||||
widget.set_prop("unit", "load")
|
||||
widgets.append(widget)
|
||||
return widgets
|
||||
|
||||
|
||||
_DESIGN_SIZE = (232, 172)
|
||||
_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(73 + 12 * math.sin(t / 7.0))
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title='XArcGauge example')
|
||||
window.set_default_size(232, 172)
|
||||
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,191 @@
|
||||
#!/usr/bin/env python3
|
||||
# Generated by LightGUI (Python/XFrame).
|
||||
# A plain Python/GTK program: no CMake. Run with:
|
||||
# python3 xdonutgauge.py
|
||||
# with the LightGUI Python package on PYTHONPATH
|
||||
# (the designer sets it when it runs this script).
|
||||
#
|
||||
# The window is 'XDonutGauge example' (192x192) 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_XDonutGauge_001_OnClick(widget):
|
||||
print("EVENT %s %s" % (widget.name, 'OnClick'))
|
||||
|
||||
# Event handlers, wired to the widgets in main():
|
||||
_HANDLERS = {
|
||||
('XDonutGauge_001', 'OnClick'): on_XDonutGauge_001_OnClick,
|
||||
}
|
||||
|
||||
|
||||
# Design-time geometry + effective anchors per widget:
|
||||
# name -> (top, left, right, bottom, x, y, width, height)
|
||||
_ANCHORS = {
|
||||
'XDonutGauge_001': (True, True, False, False, 16, 16, 160, 160),
|
||||
}
|
||||
|
||||
|
||||
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('XDonutGauge', 'XDonutGauge_001', 16, 16, 160, 160)
|
||||
widget.set_prop("label", "Disk")
|
||||
widget.set_prop("value", "58")
|
||||
widget.set_prop("unit", "used")
|
||||
widgets.append(widget)
|
||||
return widgets
|
||||
|
||||
|
||||
_DESIGN_SIZE = (192, 192)
|
||||
_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(58 + 7 * math.sin(t / 8.0))
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title='XDonutGauge example')
|
||||
window.set_default_size(192, 192)
|
||||
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,169 @@
|
||||
#!/usr/bin/env python3
|
||||
# Generated by LightGUI (Python/XFrame).
|
||||
# A plain Python/GTK program: no CMake. Run with:
|
||||
# python3 xstackbar.py
|
||||
# with the LightGUI Python package on PYTHONPATH
|
||||
# (the designer sets it when it runs this script).
|
||||
#
|
||||
# The window is 'XStackBar example' (352x80) with 1 widget(s).
|
||||
|
||||
import sys
|
||||
|
||||
import gi
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
gi.require_version("Gdk", "3.0")
|
||||
|
||||
from gi.repository import Gdk, Gtk # noqa: E402
|
||||
from lightgui.xframe.core import create, parse_color # noqa: E402
|
||||
|
||||
|
||||
def on_XStackBar_001_OnClick(widget):
|
||||
print("EVENT %s %s" % (widget.name, 'OnClick'))
|
||||
|
||||
# Event handlers, wired to the widgets in main():
|
||||
_HANDLERS = {
|
||||
('XStackBar_001', 'OnClick'): on_XStackBar_001_OnClick,
|
||||
}
|
||||
|
||||
|
||||
# Design-time geometry + effective anchors per widget:
|
||||
# name -> (top, left, right, bottom, x, y, width, height)
|
||||
_ANCHORS = {
|
||||
'XStackBar_001': (True, True, False, False, 16, 16, 320, 48),
|
||||
}
|
||||
|
||||
|
||||
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('XStackBar', 'XStackBar_001', 16, 16, 320, 48)
|
||||
widget.set_prop("label", "Memory")
|
||||
widget.set_prop("segments", "used=45|cached=25|buffers=10|free=20")
|
||||
widgets.append(widget)
|
||||
return widgets
|
||||
|
||||
|
||||
_DESIGN_SIZE = (352, 80)
|
||||
_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
|
||||
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title='XStackBar example')
|
||||
window.set_default_size(352, 80)
|
||||
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())
|
||||
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 xtachometer.py
|
||||
# with the LightGUI Python package on PYTHONPATH
|
||||
# (the designer sets it when it runs this script).
|
||||
#
|
||||
# The window is 'XTachometer example' (252x212) 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_XTachometer_001_OnClick(widget):
|
||||
print("EVENT %s %s" % (widget.name, 'OnClick'))
|
||||
|
||||
# Event handlers, wired to the widgets in main():
|
||||
_HANDLERS = {
|
||||
('XTachometer_001', 'OnClick'): on_XTachometer_001_OnClick,
|
||||
}
|
||||
|
||||
|
||||
# Design-time geometry + effective anchors per widget:
|
||||
# name -> (top, left, right, bottom, x, y, width, height)
|
||||
_ANCHORS = {
|
||||
'XTachometer_001': (True, True, False, False, 16, 16, 220, 180),
|
||||
}
|
||||
|
||||
|
||||
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('XTachometer', 'XTachometer_001', 16, 16, 220, 180)
|
||||
widget.set_prop("label", "Engine")
|
||||
widget.set_prop("value", "3200")
|
||||
widget.set_prop("maximum", "8000")
|
||||
widget.set_prop("redline", "6500")
|
||||
widget.set_prop("unit", "rpm")
|
||||
widgets.append(widget)
|
||||
return widgets
|
||||
|
||||
|
||||
_DESIGN_SIZE = (252, 212)
|
||||
_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(3200 + 900 * math.sin(t / 9.0))
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title='XTachometer example')
|
||||
window.set_default_size(252, 212)
|
||||
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 xverticalbar.py
|
||||
# with the LightGUI Python package on PYTHONPATH
|
||||
# (the designer sets it when it runs this script).
|
||||
#
|
||||
# The window is 'XVerticalBar example' (92x232) 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_XVerticalBar_001_OnClick(widget):
|
||||
print("EVENT %s %s" % (widget.name, 'OnClick'))
|
||||
|
||||
# Event handlers, wired to the widgets in main():
|
||||
_HANDLERS = {
|
||||
('XVerticalBar_001', 'OnClick'): on_XVerticalBar_001_OnClick,
|
||||
}
|
||||
|
||||
|
||||
# Design-time geometry + effective anchors per widget:
|
||||
# name -> (top, left, right, bottom, x, y, width, height)
|
||||
_ANCHORS = {
|
||||
'XVerticalBar_001': (True, True, False, False, 16, 16, 60, 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('XVerticalBar', 'XVerticalBar_001', 16, 16, 60, 200)
|
||||
widget.set_prop("label", "CPU")
|
||||
# Seed the peak-hold marker: 82, then back to 65.
|
||||
widget.set_value(65)
|
||||
widget.set_value(82)
|
||||
widget.set_value(65)
|
||||
widgets.append(widget)
|
||||
return widgets
|
||||
|
||||
|
||||
_DESIGN_SIZE = (92, 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.set_value(65 + 15 * math.sin(t / 8.0))
|
||||
if _AREA:
|
||||
_AREA[0].queue_draw()
|
||||
return True
|
||||
|
||||
def main():
|
||||
window = Gtk.Window(title='XVerticalBar example')
|
||||
window.set_default_size(92, 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())
|
||||
Reference in new issue
Block a user