FLamparski

Timezoned Clock v1

Jan 20th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.21 KB | None | 0 0
  1. #!/usr/bin/env python3.3
  2. from gi.repository import Gtk, GLib
  3. import datetime, cairo, math
  4.  
  5. TZTOOLBAR_DEF = """
  6. <toolbar name="tztoolbar">
  7.  <toolitem action="add" />
  8.  <toolitem action="remove" />
  9.  <toolitem action="edit" />
  10. </toolbar>
  11. """
  12.  
  13. class CairoClock (Gtk.DrawingArea):
  14.     def __init__ (self, radius=0.45, linewidth=4):
  15.         super().__init__()
  16.         self.radius = radius
  17.         self.line_width = linewidth
  18.         self.connect("draw", self.draw)
  19.         self.set_size_request(-1, 200)
  20.         self.set_hexpand(True)
  21.         self.set_vexpand(True)
  22.        
  23.     def set_time (self, timeobj):
  24.         self.m_time = timeobj
  25.    
  26.     def draw (self, da, ctx):
  27.         width = self.get_allocated_width()
  28.         height = self.get_allocated_height()
  29.         ctx.translate(width/2, height/2)
  30.         radius = height * self.radius
  31.         self.line_width = height / 100 * 3.5
  32.         ctx.set_line_width(self.line_width)
  33.        
  34.         ctx.save()
  35.         ctx.set_source_rgba(0.43, 0.43, 0.51, 1.0)
  36.         ctx.paint()
  37.         ctx.restore()
  38.        
  39.         ctx.arc(0, 0, radius, 0, 2 * math.pi)
  40.         ctx.save()
  41.         ctx.set_source_rgba(1, 1, 1, 0.9)
  42.         ctx.fill_preserve()
  43.         ctx.restore()
  44.         ctx.stroke_preserve()
  45.         ctx.clip()
  46.        
  47.         for i in range(12):
  48.             inset = self.line_width
  49.            
  50.             ctx.save()
  51.             ctx.set_line_cap(cairo.LINE_CAP_ROUND)
  52.            
  53.             if i % 3 != 0:
  54.                 inset *= 0.8
  55.                 ctx.set_line_width(self.line_width / 2)
  56.            
  57.             ctx.move_to((radius - inset) * math.cos(i * math.pi / 6),
  58.                 (radius - inset) * math.sin(i * math.pi / 6))
  59.             ctx.line_to(radius * math.cos(i * math.pi / 6),
  60.                 radius * math.sin(i * math.pi / 6))
  61.             ctx.stroke()
  62.             ctx.restore()
  63.        
  64.         minutes = self.m_time.minute * math.pi / 30
  65.         hours = self.m_time.hour * math.pi / 6
  66.         seconds = self.m_time.second * math.pi / 30
  67.        
  68.         ctx.save()
  69.         ctx.set_line_cap(cairo.LINE_CAP_ROUND)
  70.        
  71.         ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
  72.         ctx.move_to(0, 0)
  73.         ctx.line_to(math.sin(minutes + seconds / 60) * (radius * 0.75),
  74.             -(math.cos(minutes + seconds / 60) * (radius * 0.75)))
  75.         ctx.stroke()
  76.        
  77.         ctx.move_to(0, 0)
  78.         ctx.line_to(math.sin(hours + minutes / 12) * (radius * 0.6),
  79.             -(math.cos(hours + minutes / 12) * (radius * 0.6)))
  80.         ctx.stroke()
  81.        
  82.         ctx.save()
  83.         ctx.set_line_width(self.line_width / 3)
  84.         ctx.set_source_rgba(0.8, 0.0, 0.0, 0.9)
  85.         ctx.move_to(0, 0)
  86.         ctx.line_to(math.sin(seconds) * (radius * 0.9),
  87.             -(math.cos(seconds) * (radius * 0.9)))
  88.         ctx.stroke()
  89.         ctx.restore()
  90.        
  91.         ctx.restore()
  92.        
  93.         ctx.arc(0, 0, self.line_width / 3, 0, 2 * math.pi)
  94.         ctx.fill()
  95.        
  96.         return True
  97.  
  98. class ClockWindow (Gtk.Window):
  99.     def __init__ (self):
  100.         super().__init__()
  101.         self.set_title("Clock")
  102.         self.time_offset = 0
  103.        
  104.         self.build_window()
  105.        
  106.         self.clock_up()
  107.        
  108.         self.show_all()
  109.        
  110.         self.connect("destroy", Gtk.main_quit)
  111.         GLib.timeout_add(1000, self.clock_up)
  112.    
  113.     def set_clocklabel(self, value):
  114.         markup_open = "<span font=\"Anonymous Pro 50\"><b>"
  115.         markup_close = "</b></span>"
  116.         self.clocklabel.set_markup(markup_open + value + markup_close)
  117.    
  118.     def clock_up (self):
  119.         now = datetime.datetime.now()
  120.         delta = datetime.timedelta(hours=self.time_offset)
  121.         now_with_tz = now + delta
  122.         self.cairo_clock.set_time(now_with_tz)
  123.         self.cairo_clock.queue_draw()
  124.         if self.check_24h.get_active():
  125.             self.set_clocklabel(now_with_tz.time().strftime("%H:%M:%S"))
  126.         else:
  127.             self.set_clocklabel(now_with_tz.time().strftime("%I:%M:%S %p"))
  128.         return True
  129.        
  130.     def build_window(self):
  131.         grid = Gtk.Grid()
  132.         self.clocklabel = Gtk.Label()
  133.        
  134.         self.tz_store = Gtk.ListStore(int, str, str)
  135.         self.tz_store.append([-5, "GMT -5", "New York"])
  136.         self.tz_store.append([0, "GMT +0", "London"])
  137.         self.tz_store.append([1, "GMT +1", "Paris"])
  138.         self.tz_store.append([3, "GMT +3", "Moscow"])
  139.         self.tz_list = Gtk.TreeView(self.tz_store)
  140.         ren_tz = Gtk.CellRendererText()
  141.         col_tz = Gtk.TreeViewColumn("Timezone", ren_tz, text=1)
  142.         col_tz.set_sort_column_id(0)
  143.         ren_nm = Gtk.CellRendererText()
  144.         col_nm = Gtk.TreeViewColumn("City", ren_nm, text=2)
  145.         col_nm.set_sort_column_id(0)
  146.         self.tz_list.append_column(col_tz)
  147.         self.tz_list.append_column(col_nm)
  148.         self.tz_list.set_size_request(150, -1)
  149.         self.tz_list.set_vexpand(True)
  150.         self.tz_list.set_hexpand(False)
  151.         self.tz_list.get_selection().set_mode(Gtk.SelectionMode.BROWSE)
  152.         self.tz_list.set_cursor(1)
  153.         self.tz_list_selection = self.tz_list.get_selection()
  154.         self.tz_list_selection.connect("changed", self.tz_changed)
  155.        
  156.         self.cairo_clock = CairoClock()
  157.        
  158.         self.check_24h = Gtk.CheckButton("Use 24h time")
  159.         self.check_24h.set_active(True)
  160.        
  161.         self.tzlist_actions = Gtk.ActionGroup("tz_actions")
  162.         self.tzlist_actions.add_actions([
  163.                 ("add", None, None, None, "Add a new timezone", self.tz_add),
  164.                 ("remove", None, None, None, "Remove this timezone", self.tz_remove),
  165.                 ("edit", None, None, None, "Edit this timezone", self.tz_edit)
  166.         ])
  167.         self.tzlist_actions.get_action("add").set_icon_name("list-add-symbolic")
  168.         self.tzlist_actions.get_action("remove").set_icon_name("list-remove-symbolic")
  169.         self.tzlist_actions.get_action("edit").set_icon_name("preferences-system-symbolic")
  170.         self.tzlist_uimanager = Gtk.UIManager()
  171.         self.tzlist_uimanager.add_ui_from_string(TZTOOLBAR_DEF)
  172.         self.tzlist_uimanager.insert_action_group(self.tzlist_actions)
  173.         tztoolbar = self.tzlist_uimanager.get_widget("/tztoolbar")
  174.         tztoolbar.set_icon_size(1)
  175.         tztoolbar_css = tztoolbar.get_style_context()
  176.         tztoolbar_css.add_class("inline-toolbar")
  177.        
  178.         grid.attach(self.cairo_clock, 3, 0, 2, 2)
  179.         grid.attach_next_to(self.clocklabel, self.cairo_clock, Gtk.PositionType.BOTTOM, 2, 1)
  180.         grid.attach(self.tz_list, 0, 0, 1, 3)
  181.         grid.attach(self.check_24h, 3, 3, 1, 1)
  182.         grid.attach_next_to(tztoolbar, self.tz_list, Gtk.PositionType.BOTTOM, 1, 1)
  183.         grid.set_column_spacing(5)
  184.         self.add(grid)
  185.    
  186.     def tz_changed (self, selection):
  187.         m, i = selection.get_selected()
  188.         if i != None:
  189.             self.time_offset = m[i][0]
  190.        
  191.     def tz_add (self):
  192.         print("This action is not implemented yet. Sorry.")
  193.     def tz_edit (self):
  194.         print("This action is not implemented yet. Sorry.")
  195.     def tz_remove (self):
  196.         print("This action is not implemented yet. Sorry.")
  197.  
  198. if __name__ == "__main__":
  199.     app = ClockWindow()
  200.     Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment