Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3.3
- from gi.repository import Gtk, GLib
- import datetime, cairo, math
- TZTOOLBAR_DEF = """
- <toolbar name="tztoolbar">
- <toolitem action="add" />
- <toolitem action="remove" />
- <toolitem action="edit" />
- </toolbar>
- """
- class CairoClock (Gtk.DrawingArea):
- def __init__ (self, radius=0.45, linewidth=4):
- super().__init__()
- self.radius = radius
- self.line_width = linewidth
- self.connect("draw", self.draw)
- self.set_size_request(-1, 200)
- self.set_hexpand(True)
- self.set_vexpand(True)
- def set_time (self, timeobj):
- self.m_time = timeobj
- def draw (self, da, ctx):
- width = self.get_allocated_width()
- height = self.get_allocated_height()
- ctx.translate(width/2, height/2)
- radius = height * self.radius
- self.line_width = height / 100 * 3.5
- ctx.set_line_width(self.line_width)
- ctx.save()
- ctx.set_source_rgba(0.43, 0.43, 0.51, 1.0)
- ctx.paint()
- ctx.restore()
- ctx.arc(0, 0, radius, 0, 2 * math.pi)
- ctx.save()
- ctx.set_source_rgba(1, 1, 1, 0.9)
- ctx.fill_preserve()
- ctx.restore()
- ctx.stroke_preserve()
- ctx.clip()
- for i in range(12):
- inset = self.line_width
- ctx.save()
- ctx.set_line_cap(cairo.LINE_CAP_ROUND)
- if i % 3 != 0:
- inset *= 0.8
- ctx.set_line_width(self.line_width / 2)
- ctx.move_to((radius - inset) * math.cos(i * math.pi / 6),
- (radius - inset) * math.sin(i * math.pi / 6))
- ctx.line_to(radius * math.cos(i * math.pi / 6),
- radius * math.sin(i * math.pi / 6))
- ctx.stroke()
- ctx.restore()
- minutes = self.m_time.minute * math.pi / 30
- hours = self.m_time.hour * math.pi / 6
- seconds = self.m_time.second * math.pi / 30
- ctx.save()
- ctx.set_line_cap(cairo.LINE_CAP_ROUND)
- ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
- ctx.move_to(0, 0)
- ctx.line_to(math.sin(minutes + seconds / 60) * (radius * 0.75),
- -(math.cos(minutes + seconds / 60) * (radius * 0.75)))
- ctx.stroke()
- ctx.move_to(0, 0)
- ctx.line_to(math.sin(hours + minutes / 12) * (radius * 0.6),
- -(math.cos(hours + minutes / 12) * (radius * 0.6)))
- ctx.stroke()
- ctx.save()
- ctx.set_line_width(self.line_width / 3)
- ctx.set_source_rgba(0.8, 0.0, 0.0, 0.9)
- ctx.move_to(0, 0)
- ctx.line_to(math.sin(seconds) * (radius * 0.9),
- -(math.cos(seconds) * (radius * 0.9)))
- ctx.stroke()
- ctx.restore()
- ctx.restore()
- ctx.arc(0, 0, self.line_width / 3, 0, 2 * math.pi)
- ctx.fill()
- return True
- class ClockWindow (Gtk.Window):
- def __init__ (self):
- super().__init__()
- self.set_title("Clock")
- self.time_offset = 0
- self.build_window()
- self.clock_up()
- self.show_all()
- self.connect("destroy", Gtk.main_quit)
- GLib.timeout_add(1000, self.clock_up)
- def set_clocklabel(self, value):
- markup_open = "<span font=\"Anonymous Pro 50\"><b>"
- markup_close = "</b></span>"
- self.clocklabel.set_markup(markup_open + value + markup_close)
- def clock_up (self):
- now = datetime.datetime.now()
- delta = datetime.timedelta(hours=self.time_offset)
- now_with_tz = now + delta
- self.cairo_clock.set_time(now_with_tz)
- self.cairo_clock.queue_draw()
- if self.check_24h.get_active():
- self.set_clocklabel(now_with_tz.time().strftime("%H:%M:%S"))
- else:
- self.set_clocklabel(now_with_tz.time().strftime("%I:%M:%S %p"))
- return True
- def build_window(self):
- grid = Gtk.Grid()
- self.clocklabel = Gtk.Label()
- self.tz_store = Gtk.ListStore(int, str, str)
- self.tz_store.append([-5, "GMT -5", "New York"])
- self.tz_store.append([0, "GMT +0", "London"])
- self.tz_store.append([1, "GMT +1", "Paris"])
- self.tz_store.append([3, "GMT +3", "Moscow"])
- self.tz_list = Gtk.TreeView(self.tz_store)
- ren_tz = Gtk.CellRendererText()
- col_tz = Gtk.TreeViewColumn("Timezone", ren_tz, text=1)
- col_tz.set_sort_column_id(0)
- ren_nm = Gtk.CellRendererText()
- col_nm = Gtk.TreeViewColumn("City", ren_nm, text=2)
- col_nm.set_sort_column_id(0)
- self.tz_list.append_column(col_tz)
- self.tz_list.append_column(col_nm)
- self.tz_list.set_size_request(150, -1)
- self.tz_list.set_vexpand(True)
- self.tz_list.set_hexpand(False)
- self.tz_list.get_selection().set_mode(Gtk.SelectionMode.BROWSE)
- self.tz_list.set_cursor(1)
- self.tz_list_selection = self.tz_list.get_selection()
- self.tz_list_selection.connect("changed", self.tz_changed)
- self.cairo_clock = CairoClock()
- self.check_24h = Gtk.CheckButton("Use 24h time")
- self.check_24h.set_active(True)
- self.tzlist_actions = Gtk.ActionGroup("tz_actions")
- self.tzlist_actions.add_actions([
- ("add", None, None, None, "Add a new timezone", self.tz_add),
- ("remove", None, None, None, "Remove this timezone", self.tz_remove),
- ("edit", None, None, None, "Edit this timezone", self.tz_edit)
- ])
- self.tzlist_actions.get_action("add").set_icon_name("list-add-symbolic")
- self.tzlist_actions.get_action("remove").set_icon_name("list-remove-symbolic")
- self.tzlist_actions.get_action("edit").set_icon_name("preferences-system-symbolic")
- self.tzlist_uimanager = Gtk.UIManager()
- self.tzlist_uimanager.add_ui_from_string(TZTOOLBAR_DEF)
- self.tzlist_uimanager.insert_action_group(self.tzlist_actions)
- tztoolbar = self.tzlist_uimanager.get_widget("/tztoolbar")
- tztoolbar.set_icon_size(1)
- tztoolbar_css = tztoolbar.get_style_context()
- tztoolbar_css.add_class("inline-toolbar")
- grid.attach(self.cairo_clock, 3, 0, 2, 2)
- grid.attach_next_to(self.clocklabel, self.cairo_clock, Gtk.PositionType.BOTTOM, 2, 1)
- grid.attach(self.tz_list, 0, 0, 1, 3)
- grid.attach(self.check_24h, 3, 3, 1, 1)
- grid.attach_next_to(tztoolbar, self.tz_list, Gtk.PositionType.BOTTOM, 1, 1)
- grid.set_column_spacing(5)
- self.add(grid)
- def tz_changed (self, selection):
- m, i = selection.get_selected()
- if i != None:
- self.time_offset = m[i][0]
- def tz_add (self):
- print("This action is not implemented yet. Sorry.")
- def tz_edit (self):
- print("This action is not implemented yet. Sorry.")
- def tz_remove (self):
- print("This action is not implemented yet. Sorry.")
- if __name__ == "__main__":
- app = ClockWindow()
- Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment