From 1ddd85641d906607b256c0e5b5197adc471d3dc9 Mon Sep 17 00:00:00 2001 From: flavio Date: Fri, 04 Jan 2013 21:28:18 +0000 Subject: ported --- diff --git a/clock.py b/clock.py index bda3496..195efb2 100755 --- a/clock.py +++ b/clock.py @@ -109,6 +109,7 @@ _MODE_DIGITAL_CLOCK = 2 POWERD_INHIBIT_DIR = '/var/run/powerd-inhibit-suspend' GObject.threads_init() +Gst.init([]) class ClockActivity(activity.Activity): """The clock activity displays a simple clock widget. @@ -228,7 +229,7 @@ class ClockActivity(activity.Activity): self._add_clock_controls(toolbar_box.toolbar) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.props.draw = False separator.set_size_request(0, -1) separator.set_expand(True) @@ -244,18 +245,18 @@ class ClockActivity(activity.Activity): def _add_clock_controls(self, display_toolbar): # First group of radio button to select the type of clock to display - button1 = RadioToolButton(named_icon="simple-clock") + button1 = RadioToolButton(icon_name="simple-clock") button1.set_tooltip(_('Simple Clock')) button1.connect("toggled", self._display_mode_changed_cb, _MODE_SIMPLE_CLOCK) display_toolbar.insert(button1, -1) - button2 = RadioToolButton(named_icon="nice-clock", + button2 = RadioToolButton(icon_name="nice-clock", group=button1) button2.set_tooltip(_('Nice Clock')) button2.connect("toggled", self._display_mode_changed_cb, _MODE_NICE_CLOCK) display_toolbar.insert(button2, -1) - button3 = RadioToolButton(named_icon="digital-clock", + button3 = RadioToolButton(icon_name="digital-clock", group=button1) button3.set_tooltip(_('Digital Clock')) button3.connect("toggled", self._display_mode_changed_cb, @@ -263,7 +264,7 @@ class ClockActivity(activity.Activity): display_toolbar.insert(button3, -1) # A separator between the two groups of buttons - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) display_toolbar.insert(separator, -1) @@ -282,7 +283,7 @@ class ClockActivity(activity.Activity): display_toolbar.insert(button, -1) # A separator between the two groups of buttons - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.set_draw(True) display_toolbar.insert(separator, -1) @@ -303,7 +304,7 @@ class ClockActivity(activity.Activity): self._clock = ClockFace() # The label to print the time in full letters - self._time_letters = gtk.Label() + self._time_letters = Gtk.Label() self._time_letters.set_no_show_all(True) # Following line in ineffective! #self._time_letters.set_line_wrap(True) @@ -313,16 +314,16 @@ class ClockActivity(activity.Activity): self._TIME_LETTERS_FORMAT % self._time_in_letters) # The label to write the date - self._date = gtk.Label() + self._date = Gtk.Label() self._date.set_no_show_all(True) self._date.set_markup( self._clock.get_time().strftime(self._DATE_SHORT_FORMAT)) # Put all these widgets in a vertical box - vbox = gtk.VBox(False) - vbox.pack_start(self._clock, True) - vbox.pack_start(self._time_letters, False) - vbox.pack_start(self._date, False) + vbox = Gtk.VBox() + vbox.pack_start(self._clock, True, True, 0) + vbox.pack_start(self._time_letters, False, False, 0) + vbox.pack_start(self._date, False, False, 0) # Attach the display to the activity self.set_canvas(vbox) @@ -419,8 +420,8 @@ class ClockActivity(activity.Activity): """ def gstmessage_cb(bus, message, pipe): - if message.type in (gst.MESSAGE_EOS, gst.MESSAGE_ERROR): - pipe.set_state(gst.STATE_NULL) + if message.type in (Gst.MessageType.EOS, Gst.MessageType.ERROR): + pipe.set_state(Gst.State.NULL) if self._time_speaker is None: self._time_speaker = Speaker() @@ -433,11 +434,11 @@ class ClockActivity(activity.Activity): 'rate':self._time_speaker.SPEED, 'gap':self._time_speaker.WORD_GAP} try: - pipe = gst.parse_launch(pipeline) + pipe = Gst.parse_launch(pipeline) bus = pipe.get_bus() bus.add_signal_watch() bus.connect('message', gstmessage_cb, pipe) - pipe.set_state(gst.STATE_PLAYING) + pipe.set_state(Gst.State.PLAYING) except: self._time_speaker.speak(self._untag(self._time_in_letters)) @@ -453,7 +454,7 @@ class ClockActivity(activity.Activity): return result -class ClockFace(gtk.DrawingArea): +class ClockFace(Gtk.DrawingArea): """The Pango widget of the clock. This widget draws a simple analog clock, with 3 hands (hours, @@ -508,30 +509,31 @@ class ClockFace(gtk.DrawingArea): self._COLOR_BLACK = "#000000" # gtk.Widget signals - self.connect("expose-event", self._expose_cb) + self.connect("draw", self._expose_cb) self.connect("size-allocate", self._size_allocate_cb) # The masks to capture the events we are interested in - self.add_events(gdk.EXPOSURE_MASK | gdk.VISIBILITY_NOTIFY_MASK) + self.add_events(Gdk.EventMask.EXPOSURE_MASK | Gdk.EventMask.VISIBILITY_NOTIFY_MASK) # Define a new signal to notify the application when minutes # change. If the user wants to display the time in full # letters, the method of the activity will be called back to # refresh the display. - gobject.signal_new("time_minute", ClockFace, - gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []) + GObject.signal_new("time_minute", ClockFace, + GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, []) def set_display_mode(self, mode): """Set the type of clock to display (simple, nice, digital). 'mode' is one of MODE_XXX_CLOCK constants. """ self._mode = mode - + def _size_allocate_cb(self, widget, allocation): """We know the size of the widget on the screen, so we keep the parameters which are important for our rendering (center of the clock, radius). """ + # Store the measures of the clock face widget self._center_x = int(allocation.width / 2.0) self._center_y = int(allocation.height / 2.0) @@ -542,8 +544,8 @@ class ClockFace(gtk.DrawingArea): self._line_width = int(self._radius / 150) # Reload the svg handle - self._svg_handle = rsvg.Handle(file="clock.svg") - cr = self.window.cairo_create() + self._svg_handle = Rsvg.Handle.new_from_file("clock.svg") + cr = self.get_property('window').cairo_create() self._nice_background_cache = cr.get_target().create_similar( cairo.CONTENT_COLOR_ALPHA, self._radius * 2, self._radius * 2) @@ -565,7 +567,7 @@ class ClockFace(gtk.DrawingArea): before the expose event is called and it prevents the screen from flickering. """ - if not self.initialized and self.window: + if not self.initialized and self.get_property('window'): self.queue_resize() if self._active: @@ -591,15 +593,15 @@ class ClockFace(gtk.DrawingArea): display rich text fully localizable. """ pango_context = self.get_pango_context() - layout = pango.Layout(pango_context) + layout = Pango.Layout(pango_context) layout.set_markup(markup) - layout.set_alignment(pango.ALIGN_CENTER) + layout.set_alignment(Pango.Alignment.CENTER) x_bearing, y_bearing, width, height = layout.get_pixel_extents()[1][:4] x_delta = int(x - width / 2 - x_bearing) y_delta = int(y - height / 2 - y_bearing) - self.window.draw_layout(self._gc, x_delta, y_delta, layout) + self.get_property('window').draw_layout(self._gc, x_delta, y_delta, layout) def _draw_digital_clock(self): """Draw the digital clock. @@ -617,7 +619,7 @@ class ClockFace(gtk.DrawingArea): seconds_length = 2 * self._radius / 60 * self._time.second # Fill background - cr = self.window.cairo_create() + cr = self.get_property('window').cairo_create() cr.set_source_rgba(*style.Color(self._COLOR_WHITE).get_rgba()) cr.rectangle(round(self._center_x - 1.1 * self._radius), round(self._center_y - 0.85 * self._radius), @@ -666,16 +668,16 @@ class ClockFace(gtk.DrawingArea): markup_time = self._time.strftime(markup) #markup_time = time.strftime(markup) - cr = self.window.cairo_create() - cr = pangocairo.CairoContext(cr) + cr = self.get_property('window').cairo_create() cr.set_source_rgba(*style.Color(self._COLOR_BLACK).get_rgba()) - pango_layout = cr.create_layout() + pango_layout = PangoCairo.create_layout( + PangoCairo.create_context(cr)) d = int(self._center_y + 0.3 * self._radius) pango_layout.set_markup(markup_time) dx, dy = pango_layout.get_pixel_size() - pango_layout.set_alignment(pango.ALIGN_CENTER) + pango_layout.set_alignment(Pango.Alignment.CENTER) cr.translate(self._center_x - dx / 2.0, d - dy / 2.0) - cr.show_layout(pango_layout) + PangoCairo.show_layout(cr, pango_layout) def _draw_simple_clock(self): """Draw the simple clock variants. @@ -689,7 +691,7 @@ class ClockFace(gtk.DrawingArea): The simple clock background is a white disk, with hours and minutes ticks, and the hour numbers. """ - cr = self.window.cairo_create() + cr = self.get_property('window').cairo_create() cr.set_line_width(4 * self._line_width) cr.set_line_cap(cairo.LINE_CAP_ROUND) @@ -728,7 +730,7 @@ class ClockFace(gtk.DrawingArea): with cairo. """ # We transform the background SVG - cr = self.window.cairo_create() + cr = self.get_property('window').cairo_create() cr.translate(self._center_x - self._radius, self._center_y - self._radius) cr.set_source_surface(self._nice_background_cache) @@ -747,7 +749,7 @@ class ClockFace(gtk.DrawingArea): minutes = self._time.minute seconds = self._time.second - cr = self.window.cairo_create() + cr = self.get_property('window').cairo_create() cr.set_line_cap(cairo.LINE_CAP_ROUND) # Hour hand: @@ -793,10 +795,10 @@ class ClockFace(gtk.DrawingArea): def _draw_numbers(self): """Draw the numbers of the hours. """ - cr = self.window.cairo_create() - cr = pangocairo.CairoContext(cr) + cr = self.get_property('window').cairo_create() cr.set_source_rgba(*style.Color(self._COLOR_HOURS).get_rgba()) - pango_layout = cr.create_layout() + pango_layout = PangoCairo.create_layout( + PangoCairo.create_context(cr))#cr) for i in xrange(12): # TRANS: The format of the font used to print hour @@ -810,17 +812,17 @@ font_desc="Sans Bold 40">%d') % (i + 1) self._radius * math.cos((i - 2) * math.pi / 6.0), - dy / 2.0 + self._center_y + 0.75 * self._radius * math.sin((i - 2) * math.pi / 6.0)) - cr.update_layout(pango_layout) - cr.show_layout(pango_layout) + PangoCairo.update_layout(cr, pango_layout) + PangoCairo.show_layout(cr, pango_layout) cr.restore() def _redraw_canvas(self): """Force a redraw of the clock on the screen. """ # If we are attached to a window, redraw ourself. - if self.window: + if self.get_property('window'): self.queue_draw() - self.window.process_updates(True) + self.get_property('window').process_updates(True) def _update_cb(self): """Called every seconds to update the time value. @@ -828,7 +830,7 @@ font_desc="Sans Bold 40">%d') % (i + 1) # update the time and force a redraw of the clock self._time = datetime.now() - gobject.idle_add(self._redraw_canvas) + GObject.idle_add(self._redraw_canvas) # When the minutes change, we raise the 'time_minute' # signal. We can't test on 'self._time.second == 0' for @@ -866,6 +868,6 @@ font_desc="Sans Bold 40">%d') % (i + 1) self._update_cb() # And update again the clock every seconds. - gobject.timeout_add(1000, self._update_cb) + GObject.timeout_add(1000, self._update_cb) active = property(_get_active, _set_active) -- cgit v0.9.1