state_label.py
1 # vim: sts=4 sw=4 et 2 # GladeVcp Widgets 3 # 4 # Copyright (c) 2018 Chris Morley 5 # 6 # This program is free software: you can redistribute it and/or modify 7 # it under the terms of the GNU General Public License as published by 8 # the Free Software Foundation, either version 2 of the License, or 9 # (at your option) any later version. 10 # 11 # This program is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 # GNU General Public License for more details. 15 16 import gobject 17 import gtk 18 19 import hal 20 from hal_widgets import _HalWidgetBase 21 from hal_glib import GStat 22 23 GSTAT = GStat() 24 25 class State_Label(gtk.Label, _HalWidgetBase): 26 __gtype_name__ = "State_Label" 27 __gproperties__ = { 28 'label_type' : ( gobject.TYPE_INT, 'Label type', '0:metric mode 1:Diameter mode 2:CSS Mode', 29 0, 2, 0, gobject.PARAM_READWRITE|gobject.PARAM_CONSTRUCT), 30 'true_text' : ( gobject.TYPE_STRING, 'True Text', 31 'Text to display when state is True', 32 "True", gobject.PARAM_READWRITE|gobject.PARAM_CONSTRUCT), 33 'false_text' : ( gobject.TYPE_STRING, 'False Text', 34 'Text to display when state is False', 35 "False", gobject.PARAM_READWRITE|gobject.PARAM_CONSTRUCT), 36 } 37 __gproperties = __gproperties__ 38 39 def __init__(self, *a, **kw): 40 gobject.GObject.__init__(self) 41 self.true_text = 'True' 42 self.false_text = 'False' 43 self.label_type = 0 44 45 def _hal_init(self): 46 if self.label_type == 0: 47 GSTAT.connect('metric-mode-changed', lambda w,state: self.update(state)) 48 elif self.label_type == 1: 49 GSTAT.connect('diameter-mode', lambda w,state: self.update(state)) 50 elif self.label_type == 2: 51 GSTAT.connect('css-mode', lambda w,state: self.update(state)) 52 53 def update(self, state): 54 if state: 55 self.set_text(self.true_text) 56 else: 57 self.set_text(self.false_text) 58 59 def do_get_property(self, property): 60 name = property.name.replace('-', '_') 61 if name in self.__gproperties.keys(): 62 return getattr(self, name) 63 else: 64 raise AttributeError('unknown property %s' % property.name) 65 66 def do_set_property(self, property, value): 67 name = property.name.replace('-', '_') 68 if name in self.__gproperties.keys(): 69 return setattr(self, name, value) 70 else: 71 raise AttributeError('unknown property %s' % property.name) 72 73 74 # for testing without glade editor: 75 def main(): 76 window = gtk.Dialog("My dialog", 77 None, 78 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, 79 (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, 80 gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)) 81 widget = State_Label() 82 widget._hal_init() 83 window.vbox.add(widget) 84 window.connect("destroy", gtk.main_quit) 85 86 window.show_all() 87 response = window.run() 88 if response == gtk.RESPONSE_ACCEPT: 89 print "ok" 90 else: 91 print "cancel" 92 93 if __name__ == "__main__": 94 main()