Import LinuxCNC source tree
This commit is contained in:
19
configs/apps/gladevcp/class-callback/README
Normal file
19
configs/apps/gladevcp/class-callback/README
Normal file
@@ -0,0 +1,19 @@
|
||||
example with callback class methods
|
||||
-----------------------------------
|
||||
|
||||
This shows how to use class methods as callbacks.
|
||||
|
||||
Note:
|
||||
|
||||
There are two class definitions in this example. Both are instantiated - OtherClass around line 84 during
|
||||
import, and HandlerClass by the get_handlers() function.
|
||||
|
||||
HandlerClass.__init__() shows how to define a hal pin which is not associated to any HAL widget.
|
||||
|
||||
Passing the builder enables referring to any widget in the tree (see for instance line 33).
|
||||
|
||||
Also, a slow (1-second) timer callback is shown which updates the 'value' HAL pin - this pin is
|
||||
linked to the hal_hbar Horizontal Bar widget through class_callback.hal.
|
||||
------------------------------------------
|
||||
To run independently:
|
||||
$ gladevcp -u class_callback.py -H class_callback.hal class_callback.ui
|
||||
7
configs/apps/gladevcp/class-callback/class_callback.demo
Executable file
7
configs/apps/gladevcp/class-callback/class_callback.demo
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
thisfile=$(readlink -f "$0")
|
||||
thisdir=$(dirname "$thisfile")
|
||||
cd "$thisdir" || { echo "E: Could not change directory to '$thisdir'"; exit 1; }
|
||||
|
||||
gladevcp_demo -u ./class_callback.py -H ./class_callback.hal ./class_callback.ui
|
||||
1
configs/apps/gladevcp/class-callback/class_callback.hal
Normal file
1
configs/apps/gladevcp/class-callback/class_callback.hal
Normal file
@@ -0,0 +1 @@
|
||||
net count class_callback.value => class_callback.hal_hbar1
|
||||
93
configs/apps/gladevcp/class-callback/class_callback.py
Normal file
93
configs/apps/gladevcp/class-callback/class_callback.py
Normal file
@@ -0,0 +1,93 @@
|
||||
import hal
|
||||
import time
|
||||
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import Gdk
|
||||
from gi.repository import GObject
|
||||
from gi.repository import Pango
|
||||
from gi.repository import GLib
|
||||
|
||||
from inspect import stack
|
||||
|
||||
class OtherClass:
|
||||
'''
|
||||
methods in this class are not exposed as callback handlers
|
||||
since the object instance is not passed to gladevcp in get_handlers()
|
||||
'''
|
||||
def invisible(selfself):
|
||||
pass
|
||||
|
||||
|
||||
class HandlerClass:
|
||||
'''
|
||||
class with gladevcp callback handlers
|
||||
'''
|
||||
|
||||
def on_button_press(self,widget,data=None):
|
||||
'''
|
||||
a callback method
|
||||
parameters are:
|
||||
the generating object instance, likte a GtkButton instance
|
||||
user data passed if any - this is currently unused but
|
||||
the convention should be retained just in case
|
||||
'''
|
||||
self.nhits += 1
|
||||
laptime = time.time() - self.start
|
||||
self.builder.get_object('message').set_label("time lap#%d: %.2f seconds" % (self.nhits,laptime))
|
||||
|
||||
def _on_timer_tick(self,userdata=None):
|
||||
'''
|
||||
the full glib functionality is available if needed.
|
||||
here's a timer function which will be called periodically
|
||||
returning True restarts the timer
|
||||
returning False makes it a one-shot
|
||||
'''
|
||||
self.ticks += 1
|
||||
self.halcomp['value'] = self.ticks
|
||||
return True
|
||||
|
||||
|
||||
def __init__(self, halcomp,builder,useropts):
|
||||
'''
|
||||
Handler classes are instantiated in the following state:
|
||||
- the widget tree is created, but not yet realized (no toplevel window.show() executed yet)
|
||||
- the halcomp HAL component is set up and the widhget tree's HAL pins have already been added to it
|
||||
- it is safe to add more hal pins because halcomp.ready() has not yet been called at this point.
|
||||
|
||||
after all handlers are instantiated in command line and get_handlers() order, callbacks will be
|
||||
connected with connect_signals()/signal_autoconnect()
|
||||
|
||||
The builder may be either of libglade or GtkBuilder type depending on the glade file format.
|
||||
'''
|
||||
|
||||
self.halcomp = halcomp
|
||||
self.builder = builder
|
||||
|
||||
self.halcomp.newpin("value", hal.HAL_FLOAT, hal.HAL_OUT)
|
||||
self.start = time.time()
|
||||
self.nhits = 0
|
||||
self.ticks = 0
|
||||
|
||||
# demonstrate a slow background timer - granularity is one second
|
||||
# for a faster timer, use this:
|
||||
# glib.timeout_add(5000, self._on_timer_tick)
|
||||
GLib.timeout_add_seconds(1, self._on_timer_tick)
|
||||
|
||||
|
||||
|
||||
|
||||
other = OtherClass() # executed at import time
|
||||
|
||||
|
||||
def get_handlers(halcomp,builder,useropts):
|
||||
'''
|
||||
this function is called by gladevcp at import time (when this module is passed with '-u <modname>.py')
|
||||
|
||||
return a list of object instances whose methods should be connected as callback handlers
|
||||
any method whose name does not begin with an underscore ('_') is a callback candidate
|
||||
|
||||
the 'get_handlers' name is reserved - gladevcp expects it, so do not change
|
||||
'''
|
||||
return [HandlerClass(halcomp,builder,useropts)]
|
||||
60
configs/apps/gladevcp/class-callback/class_callback.ui
Normal file
60
configs/apps/gladevcp/class-callback/class_callback.ui
Normal file
@@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- Generated with glade 3.40.0 -->
|
||||
<interface>
|
||||
<requires lib="gtk+" version="3.0"/>
|
||||
<requires lib="gladevcp" version="0.0"/>
|
||||
<object class="GtkWindow" id="window1">
|
||||
<property name="can-focus">False</property>
|
||||
<property name="default-width">200</property>
|
||||
<property name="default-height">150</property>
|
||||
<child>
|
||||
<object class="GtkVBox" id="vbox1">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<child>
|
||||
<object class="HAL_Button" id="button">
|
||||
<property name="label" translatable="yes">Take lap time</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="receives-default">True</property>
|
||||
<signal name="pressed" handler="on_button_press" swapped="no"/>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">True</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkLabel" id="message">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">True</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="HAL_HBar" id="hal_hbar1">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="bg-color">#bebebebebebe</property>
|
||||
<property name="max">60</property>
|
||||
<property name="z0-border">0.6000000238418579</property>
|
||||
<property name="z0-color">#0000ffff0000</property>
|
||||
<property name="z1-border">0.8000000119209289</property>
|
||||
<property name="z1-color">#ffffffff0000</property>
|
||||
<property name="z2-color">#ffff00000000</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">True</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</interface>
|
||||
Reference in New Issue
Block a user