1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
# vim: sts=4 sw=4 et
# GladeVcp Widgets
#
# Copyright (c) 2010 Chris Morley, Pavel Shramov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import gi
gi.require_version("Gtk","3.0")
gi.require_version("Gdk","3.0")
from gi.repository import Gtk, Gdk
from gi.repository import GObject
import hal
hal_pin_changed_signal = ('hal-pin-changed', (GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE, (GObject.TYPE_OBJECT,)))
""" Set of base classes """
class _HalWidgetBase:
def hal_init(self, comp, name, panel_instance):
self.hal, self.hal_name = comp, name
self._panel_instance = panel_instance
self._hal_init()
def _hal_init(self):
""" Child HAL initialization functions """
pass
def hal_update(self):
""" Update HAL state """
pass
class _HalToggleBase(_HalWidgetBase):
def _hal_init(self):
self.set_active(False)
self.hal_pin = self.hal.newpin(self.hal_name, hal.HAL_BIT, hal.HAL_OUT)
self.hal_pin_not = self.hal.newpin(self.hal_name + "-not", hal.HAL_BIT, hal.HAL_OUT)
self.connect("toggled", self.hal_update)
def hal_update(self, *a):
active = bool(self.get_active())
self.hal_pin.set(active)
self.hal_pin_not.set(not active)
class _HalScaleBase(_HalWidgetBase):
def _hal_init(self):
self.hal_pin = self.hal.newpin(self.hal_name, hal.HAL_FLOAT, hal.HAL_OUT)
self.hal_pin_s = self.hal.newpin(self.hal_name+"-s", hal.HAL_S32, hal.HAL_OUT)
self.connect("value-changed", self.hal_update)
def hal_update(self, *a):
self.hal_pin.set(self.get_value())
self.hal_pin_s.set(int(self.get_value()))
class _HalIOScaleBase(_HalWidgetBase):
def _hal_init(self):
self.hal_pin = self.hal.newpin(self.hal_name, hal.HAL_FLOAT, hal.HAL_IO)
self.connect("value-changed", self.hal_update)
self.hal_pin.connect('value-changed', lambda s: self.emit('hal-pin-changed', s))
self.hal_current = self.hal_pin.get()
def hal_update(self, *a):
hval = self.hal_pin.get()
if self.hal_current != hval:
self.hal_current = hval
self.set_value(hval)
return
wval = self.get_value()
if wval != hval:
self.hal_pin.set(wval)
class _HalSensitiveBase(_HalWidgetBase):
def _hal_init(self):
self.hal_pin = self.hal.newpin(self.hal_name, hal.HAL_BIT, hal.HAL_IN)
self.hal_pin.connect('value-changed', lambda s: self.set_sensitive(s.value))
self.hal_pin.connect('value-changed', lambda s: self.emit('hal-pin-changed', s))
class _HalJogWheelBase(_HalWidgetBase):
def _hal_init(self):
self.hal_pin = self.hal.newpin(self.hal_name, hal.HAL_S32, hal.HAL_OUT)
try:
self.get_scaled_value()
self.hal_pin_scaled = self.hal.newpin(self.hal_name+'-scaled', hal.HAL_FLOAT, hal.HAL_OUT)
except:
pass
try:
self.get_delta_scaled_value()
self.hal_pin_delta_scaled = self.hal.newpin(self.hal_name+'-delta-scaled', hal.HAL_FLOAT, hal.HAL_OUT)
except:
pass
def hal_update(self, *a):
data = self.get_value()
self.hal_pin.set(int(data))
try:
data = self.get_scaled_value()
self.hal_pin_scaled.set(float(data))
except:
pass
try:
data = self.get_delta_scaled_value()
self.hal_pin_delta_scaled.set(float(data))
except:
pass
class _HalSpeedControlBase(_HalWidgetBase):
def _hal_init(self):
self.hal_pin = self.hal.newpin(self.hal_name + '.value', hal.HAL_FLOAT, hal.HAL_OUT)
self.connect("value-changed", self.hal_update)
def hal_update(self, *a):
self.hal_pin.set(self.get_value())
""" Real widgets """
class HAL_HBox(Gtk.Box, _HalSensitiveBase):
__gtype_name__ = "HAL_HBox"
__gsignals__ = dict([hal_pin_changed_signal])
class HAL_Table(Gtk.Grid, _HalSensitiveBase):
__gtype_name__ = "HAL_Table"
__gsignals__ = dict([hal_pin_changed_signal])
class HAL_HideTable(Gtk.Grid, _HalWidgetBase):
__gtype_name__ = "HAL_HideTable"
__gsignals__ = dict([hal_pin_changed_signal])
def _hal_init(self):
self.hal_pin = self.hal.newpin(self.hal_name, hal.HAL_BIT, hal.HAL_IN)
self.hal_pin.connect('value-changed',self.update)
def update(self,*a):
value = self.hal_pin.get()
if value:
self.hide()
else:
self.show()
class HAL_ComboBox(Gtk.ComboBox, _HalWidgetBase):
__gtype_name__ = "HAL_ComboBox"
__gproperties__ = {
'column' : ( GObject.TYPE_INT, 'Column', '-1:return value of index, other: column index of value in ListStore',
-1, 100, -1, GObject.ParamFlags.READWRITE|GObject.ParamFlags.CONSTRUCT),
}
def do_get_property(self, property):
name = property.name.replace('-', '_')
if name in ['column']:
return getattr(self, name)
else:
raise AttributeError('unknown property %s' % property.name)
def do_set_property(self, property, value):
name = property.name.replace('-', '_')
if name in ['column']:
return setattr(self, name, value)
else:
raise AttributeError('unknown property %s' % property.name)
def _hal_init(self):
self.hal_pin_f = self.hal.newpin(self.hal_name+"-f", hal.HAL_FLOAT, hal.HAL_OUT)
self.hal_pin_s = self.hal.newpin(self.hal_name+"-s", hal.HAL_S32, hal.HAL_OUT)
self.connect("changed", self.hal_update)
def hal_update(self, *a):
index = self.get_active()
if self.column == -1: # just use index
v = index
else:
model = self.get_model()
v = model[index][self.column]
self.hal_pin_s.set(int(v))
self.hal_pin_f.set(float(v))
class HAL_Button(Gtk.Button, _HalWidgetBase):
__gtype_name__ = "HAL_Button"
def _hal_init(self):
self.hal_pin = self.hal.newpin(self.hal_name, hal.HAL_BIT, hal.HAL_OUT)
def _f(w, data):
self.hal_pin.set(data)
self.connect("pressed", _f, True)
self.connect("released", _f, False)
self.emit("released")
class HALIO_Button(Gtk.ToggleButton, _HalWidgetBase):
__gtype_name__ = "HALIO_Button"
def _hal_init(self):
self.set_active(False)
self.hal_pin = self.hal.newpin(self.hal_name, hal.HAL_BIT, hal.HAL_IO)
def _f(w, data):
self.set_active(True)
self.hal_pin.set(data)
self.connect("pressed", _f, True)
self.hal_pin.connect('value-changed', self.hal_update)
def hal_update(self, *a):
active = bool(self.hal_pin.get())
self.set_active(active)
class HAL_CheckButton(Gtk.CheckButton, _HalToggleBase):
__gtype_name__ = "HAL_CheckButton"
class HAL_SpinButton(Gtk.SpinButton, _HalWidgetBase):
__gtype_name__ = "HAL_SpinButton"
def hal_update(self, *a):
data = self.get_value()
self.hal_pin_f.set(float(data))
self.hal_pin_s.set(int(data))
def _hal_init(self):
self.hal_pin_f = self.hal.newpin(self.hal_name+"-f", hal.HAL_FLOAT, hal.HAL_OUT)
self.hal_pin_s = self.hal.newpin(self.hal_name+"-s", hal.HAL_S32, hal.HAL_OUT)
self.connect("value-changed", self.hal_update)
self.emit("value-changed")
class HAL_RadioButton(Gtk.RadioButton, _HalToggleBase):
__gtype_name__ = "HAL_RadioButton"
class HAL_ToggleButton(Gtk.ToggleButton, _HalToggleBase):
__gtype_name__ = "HAL_ToggleButton"
class HAL_HScale(Gtk.Scale, _HalScaleBase):
__gtype_name__ = "HAL_HScale"
class HALIO_HScale(Gtk.Scale, _HalIOScaleBase):
__gtype_name__ = "HALIO_HScale"
__gsignals__ = dict([hal_pin_changed_signal])
class HAL_VScale(Gtk.Scale, _HalScaleBase):
__gtype_name__ = "HAL_VScale"
class HAL_ProgressBar(Gtk.ProgressBar, _HalWidgetBase):
__gtype_name__ = "HAL_ProgressBar"
__gproperties__ = {
'scale' : ( GObject.TYPE_FLOAT, 'Value Scale',
'Set maximum absolute value of input', -2**24, 2**24, 0,
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT),
'green_limit' : ( GObject.TYPE_FLOAT, 'green zone limit',
'lower limit of green zone', 0, 1, 0,
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT),
'yellow_limit' : ( GObject.TYPE_FLOAT, 'yellow zone limit',
'lower limit of yellow zone', 0, 1, 0,
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT),
'red_limit' : ( GObject.TYPE_FLOAT, 'red zone limit',
'lower limit of red zone', 0, 1, 0,
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT),
'text_template' : ( GObject.TYPE_STRING, 'text template',
'Text template to display. Python formatting may be used for dict {"value":value}',
"", GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT),
}
__gproperties = __gproperties__
def do_get_property(self, property):
name = property.name.replace('-', '_')
if name in self.__gproperties.keys():
return getattr(self, name)
else:
raise AttributeError('unknown property %s' % property.name)
def do_set_property(self, property, value):
name = property.name.replace('-', '_')
if name in self.__gproperties.keys():
return setattr(self, name, value)
else:
raise AttributeError('unknown property %s' % property.name)
def _hal_init(self):
self.hal_pin = self.hal.newpin(self.hal_name, hal.HAL_FLOAT, hal.HAL_IN)
self.hal_pin_scale = self.hal.newpin(self.hal_name+".scale", hal.HAL_FLOAT, hal.HAL_IN)
if self.yellow_limit or self.red_limit:
self.set_fraction(0)
self.modify_bg(Gtk.STATE_PRELIGHT, Gdk.Color.parse('#0f0')[1])
if self.text_template:
self.set_text(self.text_template % {'value':0})
def hal_update(self):
scale = self.hal_pin_scale.get() or self.scale
setting = self.hal_pin.get()
if scale <= 0 : scale = 1
if setting < 0 : setting = 0
if (setting/scale) >1:
setting = 1
scale = 1
old = self.get_fraction()
new = setting/scale
self.set_fraction(setting/scale)
if old == new:
return
if self.text_template:
self.set_text(self.text_template % {'value':setting})
colors = []
if self.yellow_limit:
colors.append((self.yellow_limit, 'yellow'))
if self.red_limit:
colors.append((self.red_limit, 'red'))
if colors:
colors.insert(0, (0, 'green'))
color = None
for (l,c), (h, _) in zip(colors, colors[1:] + [(1, None)]):
if new < l or new >= h:
pass
elif old < l or old >= h:
color = c
break
if color:
self.modify_bg(Gtk.STATE_PRELIGHT, Gdk.color_parse(color))
class HAL_Label(Gtk.Label, _HalWidgetBase):
__gtype_name__ = "HAL_Label"
__gsignals__ = dict([hal_pin_changed_signal])
__gproperties__ = {
'label_pin_type' : ( GObject.TYPE_INT, 'HAL pin type', '0:S32 1:Float 2:U32',
0, 2, 0, GObject.ParamFlags.READWRITE|GObject.ParamFlags.CONSTRUCT),
'text_template' : ( GObject.TYPE_STRING, 'text template',
'Text template to display. Python formatting may be used for one variable',
"%s", GObject.ParamFlags.READWRITE|GObject.ParamFlags.CONSTRUCT),
}
def do_get_property(self, property):
name = property.name.replace('-', '_')
if name in ['label_pin_type', 'text_template']:
return getattr(self, name)
else:
raise AttributeError('unknown property %s' % property.name)
def do_set_property(self, property, value):
name = property.name.replace('-', '_')
if name in ['label_pin_type', 'text_template']:
return setattr(self, name, value)
else:
raise AttributeError('unknown property %s' % property.name)
def _hal_init(self):
types = {0:hal.HAL_S32
,1:hal.HAL_FLOAT
,2:hal.HAL_U32
}
pin_type = types.get(self.label_pin_type, None)
if pin_type is None:
raise TypeError("%s: Invalid pin type: %s" % (self.hal_name, self.label_pin_type))
self.hal_pin = self.hal.newpin(self.hal_name, pin_type, hal.HAL_IN)
self.hal_pin.connect('value-changed',
lambda p: self.set_text(self.text_template % p.value))
self.hal_pin.connect('value-changed', lambda s: self.emit('hal-pin-changed', s))
|