/ lib / python / gladevcp / status_label.py
status_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 Status_Label(gtk.Label, _HalWidgetBase):
26      __gtype_name__ = "Status_Label"
27      __gproperties__ = {
28          'label_type'  : ( gobject.TYPE_INT, 'Label type',
29                   '0:user system 1:loaded file path 2:feed override 3:rapid override 4:spindle override',
30                  0, 4, 0, gobject.PARAM_READWRITE|gobject.PARAM_CONSTRUCT),
31          'text_template' : ( gobject.TYPE_STRING, 'text template',
32                  'Text template to display. Python formatting may be used for one variable',
33                  "%s", gobject.PARAM_READWRITE|gobject.PARAM_CONSTRUCT),
34      }
35      __gproperties = __gproperties__
36  
37      def __init__(self, *a, **kw):
38          gobject.GObject.__init__(self)
39          self.label_type = 0
40  
41      def _hal_init(self):
42          if self.label_type == 0:
43              GSTAT.connect('user-system-changed', lambda w,data: self._set_user_system_text(data))
44          elif self.label_type == 1:
45              GSTAT.connect('file-loaded', lambda w,data: self.update(data))
46          elif self.label_type == 2:
47              GSTAT.connect('feed-override-changed', lambda w,data: self.update(data))
48          elif self.label_type == 3:
49              GSTAT.connect('rapid-override-changed', lambda w,data: self.update(data))
50          elif self.label_type == 4:
51              GSTAT.connect('spindle-override-changed', lambda w,data: self.update(data))
52  
53      def update(self, data):
54          try:
55              self.set_text(self.text_template % data)
56          except:
57              print 'GLADEVCP: Error converting text template in status widget'
58  
59      def _set_user_system_text(self, data):
60          convert = { 1:"54", 2:"55", 3:"56", 4:"57", 5:"58", 6:"59", 7:"59.1", 8:"59.2", 9:"59.3"}
61          self.update(convert[int(data)])
62  
63      def do_get_property(self, property):
64          name = property.name.replace('-', '_')
65          if name in self.__gproperties.keys():
66              return getattr(self, name)
67          else:
68              raise AttributeError('unknown property %s' % property.name)
69  
70      def do_set_property(self, property, value):
71          name = property.name.replace('-', '_')
72          if name in self.__gproperties.keys():
73              return setattr(self, name, value)
74          else:
75              raise AttributeError('unknown property %s' % property.name)
76  
77  
78  # for testing without glade editor:
79  def main():
80      window = gtk.Dialog("My dialog",
81                     None,
82                     gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
83                     (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
84                      gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
85      widget = Status_Label()
86      widget._hal_init()
87      window.vbox.add(widget)
88      window.connect("destroy", gtk.main_quit)
89  
90      window.show_all()
91      response = window.run()
92      if response == gtk.RESPONSE_ACCEPT:
93         print "ok"
94      else:
95         print "cancel"
96  
97  if __name__ == "__main__":	
98      main()