Battery palette
This commit is contained in:
parent
e322027996
commit
05379e85c1
@ -20,11 +20,15 @@ import dbus
|
||||
from model.devices import device
|
||||
|
||||
_LEVEL_PROP = 'battery.charge_level.percentage'
|
||||
_CHARGING_PROP = 'battery.rechargeable.is_charging'
|
||||
_DISCHARGING_PROP = 'battery.rechargeable.is_discharging'
|
||||
|
||||
class Device(device.Device):
|
||||
__gproperties__ = {
|
||||
'level' : (int, None, None, 0, 100, 0,
|
||||
gobject.PARAM_READABLE)
|
||||
gobject.PARAM_READABLE),
|
||||
'charging' : (bool, None, None, False, gobject.PARAM_READABLE),
|
||||
'discharging' : (bool, None, None, False, gobject.PARAM_READABLE)
|
||||
}
|
||||
|
||||
def __init__(self, udi):
|
||||
@ -40,16 +44,28 @@ class Device(device.Device):
|
||||
udi)
|
||||
|
||||
self._level = self._battery.GetProperty(_LEVEL_PROP)
|
||||
self._charging = self._battery.GetProperty(_CHARGING_PROP)
|
||||
self._discharging = self._battery.GetProperty(_DISCHARGING_PROP)
|
||||
|
||||
def do_get_property(self, pspec):
|
||||
if pspec.name == 'level':
|
||||
return self._level
|
||||
if pspec.name == 'charging':
|
||||
return self._charging
|
||||
if pspec.name == 'discharging':
|
||||
return self._discharging
|
||||
|
||||
def get_type(self):
|
||||
return 'battery'
|
||||
|
||||
def _battery_changed(self, num_changes, changes_list):
|
||||
for change in changes_list:
|
||||
if change[0] == 'battery.charge_level.percentage':
|
||||
if change[0] == _LEVEL_PROP:
|
||||
self._level = self._battery.GetProperty(_LEVEL_PROP)
|
||||
self.notify('level')
|
||||
elif change[0] == _CHARGING_PROP:
|
||||
self._charging = self._battery.GetProperty(_CHARGING_PROP)
|
||||
self.notify('charging')
|
||||
elif change[0] == _DISCHARGING_PROP:
|
||||
self._discharging = self._battery.GetProperty(_DISCHARGING_PROP)
|
||||
self.notify('discharging')
|
||||
|
@ -14,8 +14,12 @@
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import gtk
|
||||
|
||||
from gettext import gettext as _
|
||||
from sugar.graphics import canvasicon
|
||||
from sugar.graphics import units
|
||||
from sugar.graphics.palette import Palette
|
||||
|
||||
_ICON_NAME = 'device-battery'
|
||||
|
||||
@ -23,15 +27,56 @@ class DeviceView(canvasicon.CanvasIcon):
|
||||
def __init__(self, model):
|
||||
canvasicon.CanvasIcon.__init__(self, scale=units.MEDIUM_ICON_SCALE)
|
||||
self._model = model
|
||||
self._palette = BatteryPalette(_('My Battery life'))
|
||||
self.set_palette(self._palette)
|
||||
|
||||
model.connect('notify::level', self._level_changed_cb)
|
||||
model.connect('notify::level', self._battery_status_changed_cb)
|
||||
model.connect('notify::charging', self._battery_status_changed_cb)
|
||||
model.connect('notify::discharging', self._battery_status_changed_cb)
|
||||
self._update_info()
|
||||
|
||||
self._update_level()
|
||||
|
||||
def _update_level(self):
|
||||
def _update_info(self):
|
||||
self.props.icon_name = canvasicon.get_icon_state(
|
||||
_ICON_NAME, self._model.props.level)
|
||||
|
||||
def _level_changed_cb(self, pspec, param):
|
||||
self._update_level()
|
||||
# Update palette
|
||||
self._palette.update_progress_bar(self._model.props.level)
|
||||
self._palette.update_charge_status(self._model.props.charging, \
|
||||
self._model.props.discharging)
|
||||
|
||||
def _battery_status_changed_cb(self, pspec, param):
|
||||
self._update_info()
|
||||
|
||||
class BatteryPalette(Palette):
|
||||
def __init__(self, primary_text):
|
||||
Palette.__init__(self, primary_text)
|
||||
|
||||
self._level = 0
|
||||
self._progress_bar = gtk.ProgressBar()
|
||||
self._progress_bar.show()
|
||||
self._status_label = gtk.Label()
|
||||
self._status_label.show()
|
||||
|
||||
vbox = gtk.VBox()
|
||||
vbox.pack_start(self._progress_bar)
|
||||
vbox.pack_start(self._status_label)
|
||||
vbox.show()
|
||||
|
||||
self.set_content(vbox)
|
||||
|
||||
def update_progress_bar(self, percent):
|
||||
self._level = percent
|
||||
fraction = float(percent/100.0)
|
||||
self._progress_bar.set_fraction(fraction)
|
||||
|
||||
def update_charge_status(self, charging, discharging):
|
||||
percent_string = ' (%s%%)' % self._level
|
||||
|
||||
if charging:
|
||||
charge_text = _('Battery charging') + percent_string
|
||||
elif discharging:
|
||||
charge_text = _('Battery discharging') + percent_string
|
||||
else:
|
||||
charge_text = _('Battery fully charged')
|
||||
|
||||
self._status_label.set_text(charge_text)
|
||||
|
Loading…
Reference in New Issue
Block a user