Ejemplo n.º 1
0
class MILWidget(gtk.Entry,
                StateMixin,
                PropertyObject):
    __gtype_name__='MILWidget'
    gproperty('on', bool, False)
    gproperty('on-color', str, '#F7D30D')
    gproperty('off-color', str, '#AAAAAA')
    
    def __init__(self, app):
        gtk.Entry.__init__(self, 3)
        self.command = Sensor('0101', 1)
        PropertyObject.__init__(self)

        self._pref_cbs = []
        
        self.app = app
        self.set_text('MIL')
        self.set_property('editable', False)
        self.set_property('width-chars', 3)
        
    def __post_init__(self):
        self.on_color = self.app.prefs.get('mil.on-color')
        self.off_color = self.app.prefs.get('mil.off-color')
        cb_id = self.app.prefs.add_watch('mil.on-color',
                                         self._notify_prefs_cb)
        self._pref_cbs.append(('mil.on-color', cb_id))
        cb_id = self.app.prefs.add_watch('mil.off-color',
                                         self._notify_prefs_cb)
        self._pref_cbs.append(('mil.off-color', cb_id))
                                                     
        self.connect('notify::on', self._notify_cb)
        self.notify('on')
        self.command.connect('notify::data', self._data_changed_cb)
        
    def _notify_cb(self, o, pspec):
        if pspec.name == 'on':
            self._update_color()
        
    def _update_color(self):
        if self.on:
            self.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(self.on_color))
        else:
            self.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(self.off_color))


    def _notify_prefs_cb(self, pname, pvalue, args):
        if pname == 'mil.on-color':
            self.on_color = pvalue
        elif pname == 'mil.off-color':
            self.off_color = pvalue
        self._update_color()
    
    
    def _data_changed_cb(self, command, pspec):
        on = self.command.metric_value == 'On'
        self.on = on
Ejemplo n.º 2
0
class SensorView(BaseView, UnitMixin, PropertyObject):
    __gtype_name__ = 'SensorView'

    gproperty('units-widget', object)
    gproperty('update-name', bool, False)

    def __init__(self,
                 pid,
                 index=0,
                 units='Metric',
                 active_widget=None,
                 name_widget=None,
                 value_widget=None,
                 units_widget=None,
                 helper=None):

        self.command = Sensor(pid, index)
        self.command.connect('data-changed', self._data_changed_cb)

        BaseView.__init__(self, active_widget, name_widget, value_widget,
                          helper)
        PropertyObject.__init__(self,
                                active_widget=active_widget,
                                name_widget=name_widget,
                                value_widget=value_widget,
                                units_widget=units_widget,
                                unit_standard=units,
                                helper=helper)

    def __post_init__(self):
        self.connect('notify::unit-standard', self._notify_unit_standard_cb)
        BaseView.__post_init__(self)

    def _notify_unit_standard_cb(self, o, pspec):
        self._update_view()

    def _do_sensitize_widgets(self):
        if self.units_widget:
            self.units_widget.set_sensitive(self.supported and self.active)

    def _do_update_view(self):
        if self.unit_standard == 'Imperial':
            value = self.command.imperial_value
            units = self.command.imperial_units
        else:
            value = self.command.metric_value
            units = self.command.metric_units
        if not units: units = ''
        if not value: value = ''

        if self.name_widget and self.update_name:
            self.name_widget.set_text(self.command.name)
        if self.value_widget:
            self.value_widget.set_text(value)
        if self.units_widget:
            self.units_widget.set_text(units)
Ejemplo n.º 3
0
class SensorView(BaseView, UnitMixin, PropertyObject):
    __gtype_name__ = 'SensorView'
    
    gproperty('units-widget', object)
    gproperty('update-name', bool, False)
    
    def __init__(self, pid, index=0, units='Metric',
                       active_widget=None, name_widget=None,
                       value_widget=None, units_widget=None,
                       helper=None):
        
        self.command = Sensor(pid, index)
        self.command.connect('notify::data', self._data_changed_cb)
        
        BaseView.__init__(self, active_widget, name_widget,
                                value_widget, helper)
        PropertyObject.__init__(self, active_widget=active_widget,
                                      name_widget=name_widget,
                                      value_widget=value_widget,
                                      units_widget=units_widget,
                                      unit_standard=units,
                                      helper=helper)

    def __post_init__(self):
        self.connect('notify::unit-standard', self._notify_unit_standard_cb)
        BaseView.__post_init__(self)    
       
        
    def  _notify_unit_standard_cb(self, o, pspec):
        self._update_view()
                     
                     
    def _do_sensitize_widgets(self):
        if self.units_widget:
            self.units_widget.set_sensitive(self.supported and self.active)            
       
       
    def _do_update_view(self):
        if self.unit_standard == 'Imperial':
            value = self.command.imperial_value
            units = self.command.imperial_units
        else:
            value = self.command.metric_value
            units = self.command.metric_units
        if not units: units=''
        if not value: value=''
 
        if self.name_widget and self.update_name:
            self.name_widget.set_text(self.command.name)
        if self.value_widget:
            self.value_widget.set_text(value)
        if self.units_widget:
            self.units_widget.set_text(units)
Ejemplo n.º 4
0
class SensorProgressView(BaseView, PropertyObject):
    __gtype_name__ = 'SensorProgressView'

    gproperty('progress-widget', object)
    gproperty('min-value', float)
    gproperty('max-value', float)
    gproperty('update-name', bool, False)

    def __init__(self,
                 pid,
                 index=0,
                 min_value=0,
                 max_value=100,
                 active_widget=None,
                 name_widget=None,
                 value_widget=None,
                 helper=None,
                 progress_widget=None):

        self.command = Sensor(pid, index)
        self.command.connect('data-changed', self._data_changed_cb)

        BaseView.__init__(self, active_widget, name_widget, value_widget,
                          helper)
        PropertyObject.__init__(self,
                                active_widget=active_widget,
                                name_widget=name_widget,
                                value_widget=value_widget,
                                helper=helper,
                                progress_widget=progress_widget,
                                min_value=min_value,
                                max_value=max_value)

    def _do_sensitize_widgets(self):
        if self.progress_widget:
            self.progress_widget.set_sensitive(self.supported and self.active)

    def _do_update_view(self):
        value = self.command.metric_value
        if not value:
            value = ''
            fraction = 0
        else:
            fraction = eval(value) / (self.max_value - self.min_value)
            if fraction > 1: fraction = 1
            if fraction < 0: fraction = 0

        if self.name_widget and self.update_name:
            self.name_widget.set_text(self.command.name)
        if self.value_widget:
            self.value_widget.set_text(value)
        if self.progress_widget:
            self.progress_widget.set_fraction(fraction)
Ejemplo n.º 5
0
class MILWidget(gtk.Entry, StateMixin, PropertyObject):
    __gtype_name__ = 'MILWidget'
    gproperty('on', bool, False)
    gproperty('on-color', str, '#F7D30D')
    gproperty('off-color', str, '#AAAAAA')

    def __init__(self, app):
        gtk.Entry.__init__(self, 3)
        self.command = Sensor('0101', 1)
        PropertyObject.__init__(self)

        self._pref_cbs = []

        self.app = app
        self.set_text('MIL')
        self.set_property('editable', False)
        self.set_property('width-chars', 3)

    def __post_init__(self):
        self.on_color = self.app.prefs.get('mil.on-color')
        self.off_color = self.app.prefs.get('mil.off-color')
        cb_id = self.app.prefs.add_watch('mil.on-color', self._notify_prefs_cb)
        self._pref_cbs.append(('mil.on-color', cb_id))
        cb_id = self.app.prefs.add_watch('mil.off-color',
                                         self._notify_prefs_cb)
        self._pref_cbs.append(('mil.off-color', cb_id))

        self.connect('notify::on', self._notify_cb)
        self.connect('notify::on-color', self._notify_cb)
        self.connect('notify::off-color', self._notify_cb)
        self.notify('on')
        self.command.connect('data-changed', self._data_changed_cb)

    def _notify_cb(self, o, pspec):
        if self.on:
            self.modify_base(gtk.STATE_NORMAL,
                             gtk.gdk.color_parse(self.on_color))
        else:
            self.modify_base(gtk.STATE_NORMAL,
                             gtk.gdk.color_parse(self.off_color))

    def _notify_prefs_cb(self, pname, pvalue, args):
        if pname == 'mil.on-color':
            self.on_color = pvalue
        elif pname == 'mil.off-color':
            self.off_color = pvalue

    def _data_changed_cb(self, command, data):
        on = self.command.metric_value == 'On'
        self.on = on
Ejemplo n.º 6
0
class SensorProgressView(BaseView, PropertyObject):
    __gtype_name__ = 'SensorProgressView'
    
    gproperty('progress-widget', object)
    gproperty('min-value', float)
    gproperty('max-value', float)
    gproperty('update-name', bool, False)
        
    def __init__(self, pid, index=0,
                       min_value=0, max_value=100,
                       active_widget=None, name_widget=None,
                       value_widget=None,
                       helper=None, progress_widget=None):
        
        self.command = Sensor(pid, index)
        self.command.connect('notify::data', self._data_changed_cb)
        
        BaseView.__init__(self, active_widget, name_widget,
                                  value_widget, helper)
        PropertyObject.__init__(self, active_widget=active_widget,
                                      name_widget=name_widget,
                                      value_widget=value_widget,
                                      helper=helper,
                                      progress_widget=progress_widget,
                                      min_value=min_value,
                                      max_value=max_value)
                         
                     
    def _do_sensitize_widgets(self):
        if self.progress_widget:
            self.progress_widget.set_sensitive(self.supported and self.active)            
       
       
    def _do_update_view(self):
        value = self.command.metric_value
        if not value: 
            value=''
            fraction = 0
        else:
            fraction = eval(value) / (self.max_value - self.min_value)
            if fraction > 1: fraction = 1
            if fraction < 0: fraction = 0
 
        if self.name_widget and self.update_name:
            self.name_widget.set_text(self.command.name)
        if self.value_widget:
            self.value_widget.set_text(value)
        if self.progress_widget:
            self.progress_widget.set_fraction(fraction)
Ejemplo n.º 7
0
class Gauge (gtk.EventBox, StateMixin, UnitMixin,
                              PropertyObject):
    __gtype_name__="Gauge"
         
    def prop_get_value (self):
        return self._value
    
    # FIXME: min-angle and max-angle should be float, 
    # but set_property does not accept negative values for gdouble or gint.
    # Warning: value "-50" of type `gint' is invalid or out of range for property `min-angle' of type `gint'
    gproperty('min-angle', object) 
    gproperty('max-angle', object)
    gproperty('min-value', float)
    gproperty('max-value', float)
    gproperty('idle-value', float)
    gproperty('value', float, flags=gobject.PARAM_READABLE)
    gproperty('needle-color', str, '#FDC62D')
    gproperty('needle-length', int)
    gproperty('needle-width', int)
    gproperty('metric-overlay', object)
    gproperty('imperial-overlay', object)
    
    
    def __init__ (self, pid, metric, imperial=None, index=0):
        if not imperial:
            imperial=metric
        gtk.EventBox.__init__(self)
        PropertyObject.__init__(self, command=pid, index=index, 
                                      metric_overlay=metric,
                                      imperial_overlay=imperial)
        self.sensor = Sensor(pid, index)
        
        width = self.metric_overlay.get_width()
        height = self.metric_overlay.get_height()    
        self.set_size_request(width, height)    
        
        self._needle_gc = None
        
        self._set_default_values()
        
        self._value = self.idle_value
        self._pixmap = None
        self._area = gtk.DrawingArea()
        self.add(self._area)

        self.connect('button-press-event', self._button_press_cb)
        self._area.connect("expose_event", self._expose_event)
        self._area.connect("configure_event", self._configure_event)

    
    def __post_init__(self):
        self.connect('notify::needle-length', self._notify_must_redraw)
        self.connect('notify::metric-overlay', self._notify_must_redraw)
        self.connect('notify::imperial-overlay', self._notify_must_redraw)
        self.connect('notify::needle-color', self._notify_needle_cb)
        self.connect('notify::needle-width', self._notify_needle_cb)
        self.connect('notify::supported', self._notify_supported_cb)
        self.connect('notify::active', self._notify_active_cb)
        self.sensor.connect('data-changed', self._sensor_data_changed_cb)
               
                
    def _notify_must_redraw(self, o, pspec):
        self._draw()
        
   
    def _notify_needle_cb(self, o, pspec):
        if pspec.name == 'needle-color':
            if self._needle_gc:
                self._needle_gc.set_rgb_fg_color(gtk.gdk.color_parse(self.needle_color))
        if pspec.name == 'needle-width':    
            self._needle_gc.line_width = width
        self._draw()


    def  _notify_supported_cb(self, o, pspec):
        self.set_sensitive(self.supported)
        if not self.supported:
            self.active = False


    def _notify_active_cb(self, o, pspec):
        self._area.set_sensitive(self.active)
        self._draw()
        self.emit('active-changed', self.active)

    
    def _button_press_cb(self, widget, event):
        if event.type == gdk.BUTTON_PRESS:
            self.active = not self.active


    def _sensor_data_changed_cb(self, sensor, data):
        self._value = eval(self.sensor.metric_value)
        self._draw()
           
               
    def _set_default_values(self):
        raise NotImplementedError, 'Use one of the subclasses please'
        
        
    def _construct_needle (self) :
        angle_range = self.max_angle - self.min_angle
        value_range = self.max_value - self.min_value
        value = self._value
        if value < self.min_value:
            value = self.min_value
        if value > self.max_value:
            value = self.max_value
        angle = (value - self.min_value) / value_range * angle_range + self.min_angle
        
        point_x = int(self._needle_origin_x + self.needle_length * math.cos((angle + 180) * math.pi / 180))
        point_y = int(self._needle_origin_y + self.needle_length * math.sin((angle + 180) * math.pi / 180))
        
        side1_x = int(self._needle_origin_x + self.needle_width * math.cos((angle + 270) * math.pi / 180))
        side1_y = int(self._needle_origin_y + self.needle_width * math.sin((angle + 270) * math.pi / 180))
        
        side2_x = int(self._needle_origin_x + self.needle_width * math.cos((angle + 90) * math.pi / 180))
        side2_y = int(self._needle_origin_y + self.needle_width * math.sin((angle + 90) * math.pi / 180))
        
        return [(self._needle_origin_x, self._needle_origin_y),
                    (side1_x, side1_y),
                    (point_x, point_y),
                    (side2_x, side2_y)]
        
        
    def _draw (self) :
        if self._pixmap is None :
            return
        x, y, width, height = self.get_allocation()
        bg_gc = self.get_style().bg_gc[gtk.STATE_NORMAL]
        self._pixmap.draw_rectangle(bg_gc, True, 0, 0, width, height)
        
        if self.unit_standard == 'Imperial':
            overlay = self.imperial_overlay
        else:
            overlay = self.metric_overlay
        self._pixmap.draw_pixbuf(gtk.gdk.GC(self.window), overlay, 0, 0, 0, 0)
        
        needle = self._construct_needle()
        if self.active:
            self._pixmap.draw_polygon(self._needle_gc, True, needle)
        
        fg_gc = self.get_style().fg_gc[gtk.STATE_NORMAL]
        fg_gc.set_foreground(gtk.gdk.color_parse('#000000'))
        self._pixmap.draw_arc(fg_gc, True, 
                                     self._needle_origin_x - self._circle_radius, 
                                     self._needle_origin_y - self._circle_radius, 
                                     self._circle_radius * 2, self._circle_radius * 2, 0, 360 * 64)
                                         
        self._area.queue_draw()

        
    def _expose_event (self, widget, event):
        x , y, width, height = event.area
        widget.window.draw_drawable(widget.get_style().bg_gc[gtk.STATE_NORMAL], self._pixmap, x, y, x, y, width, height)
        return False


    def _configure_event (self, widget, event):
        x, y, width, height = widget.get_allocation()
        self._pixmap = gtk.gdk.Pixmap(widget.window, width, height)
        self._needle_gc = gtk.gdk.GC(widget.window)
        self._needle_gc.set_rgb_fg_color(gtk.gdk.color_parse(self.needle_color))
        self._needle_gc.line_width = self.needle_width
        self.idle()
        
        
    def idle (self) :
        """Set value to the defined idle value""" 
        self._value = self.idle_value
        self._draw()
Ejemplo n.º 8
0
class Gauge(gtk.EventBox, StateMixin, UnitMixin, PropertyObject):
    __gtype_name__ = "Gauge"

    def prop_get_value(self):
        return self._value

    # FIXME: min-angle and max-angle should be float,
    # but set_property does not accept negative values for gdouble or gint.
    # Warning: value "-50" of type `gint' is invalid or out of range for property `min-angle' of type `gint'
    gproperty('min-angle', object)
    gproperty('max-angle', object)
    gproperty('min-value', float)
    gproperty('max-value', float)
    gproperty('idle-value', float)
    gproperty('value', float, flags=gobject.PARAM_READABLE)
    gproperty('needle-color', str, '#FDC62D')
    gproperty('needle-length', int)
    gproperty('needle-width', int)
    gproperty('metric-overlay', object)
    gproperty('imperial-overlay', object)

    def __init__(self, pid, metric, imperial=None, index=0):
        if not imperial:
            imperial = metric
        gtk.EventBox.__init__(self)
        PropertyObject.__init__(self,
                                command=pid,
                                index=index,
                                metric_overlay=metric,
                                imperial_overlay=imperial)
        self.sensor = Sensor(pid, index)

        width = self.metric_overlay.get_width()
        height = self.metric_overlay.get_height()
        self.set_size_request(width, height)

        self._needle_gc = None

        self._set_default_values()

        self._value = self.idle_value
        self._pixmap = None
        self._area = gtk.DrawingArea()
        self.add(self._area)

        self.connect('button-press-event', self._button_press_cb)
        self._area.connect("expose_event", self._expose_event)
        self._area.connect("configure_event", self._configure_event)

    def __post_init__(self):
        self.connect('notify::needle-length', self._notify_must_redraw)
        self.connect('notify::metric-overlay', self._notify_must_redraw)
        self.connect('notify::imperial-overlay', self._notify_must_redraw)
        self.connect('notify::needle-color', self._notify_needle_cb)
        self.connect('notify::needle-width', self._notify_needle_cb)
        self.connect('notify::supported', self._notify_supported_cb)
        self.connect('notify::active', self._notify_active_cb)
        self.sensor.connect('data-changed', self._sensor_data_changed_cb)

    def _notify_must_redraw(self, o, pspec):
        self._draw()

    def _notify_needle_cb(self, o, pspec):
        if pspec.name == 'needle-color':
            if self._needle_gc:
                self._needle_gc.set_rgb_fg_color(
                    gtk.gdk.color_parse(self.needle_color))
        if pspec.name == 'needle-width':
            self._needle_gc.line_width = width
        self._draw()

    def _notify_supported_cb(self, o, pspec):
        self.set_sensitive(self.supported)
        if not self.supported:
            self.active = False

    def _notify_active_cb(self, o, pspec):
        self._area.set_sensitive(self.active)
        self._draw()
        self.emit('active-changed', self.active)

    def _button_press_cb(self, widget, event):
        if event.type == gdk.BUTTON_PRESS:
            self.active = not self.active

    def _sensor_data_changed_cb(self, sensor, data):
        self._value = eval(self.sensor.metric_value)
        self._draw()

    def _set_default_values(self):
        raise NotImplementedError, 'Use one of the subclasses please'

    def _construct_needle(self):
        angle_range = self.max_angle - self.min_angle
        value_range = self.max_value - self.min_value
        value = self._value
        if value < self.min_value:
            value = self.min_value
        if value > self.max_value:
            value = self.max_value
        angle = (value -
                 self.min_value) / value_range * angle_range + self.min_angle

        point_x = int(self._needle_origin_x + self.needle_length *
                      math.cos((angle + 180) * math.pi / 180))
        point_y = int(self._needle_origin_y + self.needle_length *
                      math.sin((angle + 180) * math.pi / 180))

        side1_x = int(self._needle_origin_x + self.needle_width *
                      math.cos((angle + 270) * math.pi / 180))
        side1_y = int(self._needle_origin_y + self.needle_width *
                      math.sin((angle + 270) * math.pi / 180))

        side2_x = int(self._needle_origin_x + self.needle_width *
                      math.cos((angle + 90) * math.pi / 180))
        side2_y = int(self._needle_origin_y + self.needle_width *
                      math.sin((angle + 90) * math.pi / 180))

        return [(self._needle_origin_x, self._needle_origin_y),
                (side1_x, side1_y), (point_x, point_y), (side2_x, side2_y)]

    def _draw(self):
        if self._pixmap is None:
            return
        x, y, width, height = self.get_allocation()
        bg_gc = self.get_style().bg_gc[gtk.STATE_NORMAL]
        self._pixmap.draw_rectangle(bg_gc, True, 0, 0, width, height)

        if self.unit_standard == 'Imperial':
            overlay = self.imperial_overlay
        else:
            overlay = self.metric_overlay
        self._pixmap.draw_pixbuf(gtk.gdk.GC(self.window), overlay, 0, 0, 0, 0)

        needle = self._construct_needle()
        if self.active:
            self._pixmap.draw_polygon(self._needle_gc, True, needle)

        fg_gc = self.get_style().fg_gc[gtk.STATE_NORMAL]
        fg_gc.set_foreground(gtk.gdk.color_parse('#000000'))
        self._pixmap.draw_arc(fg_gc, True,
                              self._needle_origin_x - self._circle_radius,
                              self._needle_origin_y - self._circle_radius,
                              self._circle_radius * 2, self._circle_radius * 2,
                              0, 360 * 64)

        self._area.queue_draw()

    def _expose_event(self, widget, event):
        x, y, width, height = event.area
        widget.window.draw_drawable(widget.get_style().bg_gc[gtk.STATE_NORMAL],
                                    self._pixmap, x, y, x, y, width, height)
        return False

    def _configure_event(self, widget, event):
        x, y, width, height = widget.get_allocation()
        self._pixmap = gtk.gdk.Pixmap(widget.window, width, height)
        self._needle_gc = gtk.gdk.GC(widget.window)
        self._needle_gc.set_rgb_fg_color(gtk.gdk.color_parse(
            self.needle_color))
        self._needle_gc.line_width = self.needle_width
        self.idle()

    def idle(self):
        """Set value to the defined idle value"""
        self._value = self.idle_value
        self._draw()