[PATCH] add clock frame device
Martin Dengler
martinatmartindengler.com
Tue Oct 14 18:35:30 EDT 2008
---
model/devices/clock.py | 26 ++++++++++++
model/devices/devicesmodel.py | 6 +++
view/devices/clock.py | 88 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 120 insertions(+), 0 deletions(-)
create mode 100644 model/devices/clock.py
create mode 100644 view/devices/clock.py
diff --git a/model/devices/clock.py b/model/devices/clock.py
new file mode 100644
index 0000000..ce32855
--- /dev/null
+++ b/model/devices/clock.py
@@ -0,0 +1,26 @@
+# Copyright (C) 2008 Martin Dengler
+#
+# 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.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import gobject
+
+from model.devices import device
+
+class Device(device.Device):
+ def __init__(self):
+ device.Device.__init__(self)
+
+ def get_type(self):
+ return 'clock'
diff --git a/model/devices/devicesmodel.py b/model/devices/devicesmodel.py
index 1dd3c45..8f9615d 100644
--- a/model/devices/devicesmodel.py
+++ b/model/devices/devicesmodel.py
@@ -23,6 +23,7 @@ from model.devices import device
from model.devices.network import wireless
from model.devices.network import mesh
from model.devices import battery
+from model.devices import clock
from model.devices import reloadbutton
from model.devices import speaker
from model.devices import wirelessstrength
@@ -63,6 +64,11 @@ class DevicesModel(gobject.GObject):
logging.error("could not initialize reloadbutton device: %s" %
reloadbutton_fail_msg)
+ try:
+ self.add_device(clock.Device())
+ except Exception, clock_fail_msg:
+ logging.error("could not initialize clock device: %s" %
+ clock_fail_msg)
try:
self.add_device(wirelessstrength.Device())
diff --git a/view/devices/clock.py b/view/devices/clock.py
new file mode 100644
index 0000000..6fd71d6
--- /dev/null
+++ b/view/devices/clock.py
@@ -0,0 +1,88 @@
+# Copyright (C) 2008 Martin Dengler
+#
+# 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.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+from gettext import gettext as _
+
+import cairo
+import gobject
+import gtk, gtk.gdk
+import math
+import pango
+import pangocairo
+import time
+
+from sugar import profile
+from sugar.graphics.toolbutton import ToolButton
+from sugar.graphics.style import Color
+
+class DigitalClock(gtk.Image):
+ def __init__(self, *args, **kwargs):
+ gtk.Image.__init__(self, *args, **kwargs)
+ self.connect_after("expose-event", self.do_expose_event)
+ self.timer = gobject.timeout_add(1000, self.__timeout_cb)
+ mycolor = profile.get_color()
+ self._fill_rgba = Color(mycolor.fill).get_rgba()
+ self._stroke_rgba = Color(mycolor.stroke).get_rgba()
+
+ def __timeout_cb(self):
+ if self.window is not None:
+ x, y, w, h = self.allocation
+ self.window.invalidate_rect((x, y, x + w, y + h), False)
+ return True
+
+ def do_expose_event(self, widget, event):
+ cr = self.window.cairo_create()
+ x, y, w, h = event.area
+ redraw_region = gtk.gdk.region_rectangle(self.allocation)
+ exposed_region = gtk.gdk.region_rectangle(event.area)
+ redraw_region.intersect(exposed_region)
+ cr.region(redraw_region)
+ cr.clip()
+ self.draw(cr)
+
+ def write(self, cr, text, x=0, y=0, font="Sans 24"):
+ pcr = pangocairo.CairoContext(cr)
+ layout = pcr.create_layout()
+ font_description = pango.FontDescription(font)
+ layout.set_font_description(font_description)
+ layout.set_markup(text)
+ cr.save()
+ if x != 0 or y != 0:
+ cr.move_to(x, y)
+ pcr.layout_path(layout)
+ cr.set_source_rgba(*self._stroke_rgba)
+ cr.set_line_width(0.5)
+ cr.stroke_preserve()
+ cr.set_source_rgba(*self._fill_rgba)
+ cr.fill()
+ cr.restore()
+ self.set_size_request(*layout.get_pixel_size())
+
+ def draw(self, cr):
+ x, y, w, h = self.allocation
+ cr.translate(x, y)
+ self.write(cr, time.strftime("%c", time.localtime()))
+
+
+class DeviceView(ToolButton):
+
+ FRAME_POSITION_RELATIVE = 1100
+
+ def __init__(self, model):
+ ToolButton.__init__(self)
+ self._icon = DigitalClock()
+ self.set_icon_widget(self._icon)
+ self._icon.show()
--
1.5.5.1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.laptop.org/pipermail/sugar/attachments/20081015/0b715bfb/attachment-0001.pgp
More information about the Sugar-devel
mailing list