Package sparked :: Module gui
[hide private]
[frames] | no frames]

Source Code for Module sparked.gui

  1  # Copyright (c) 2010 Arjan Scherpenisse 
  2  # See LICENSE for details. 
  3   
  4  """ 
  5  A status window for the Spark application. 
  6  """ 
  7   
  8  from datetime import datetime 
  9  import gtk 
 10   
 11  from twisted.python import log 
 12  from twisted.internet import reactor 
 13  from sparked import events 
 14   
 15   
16 -class StatusWindow (gtk.Window):
17 """ 18 The status window for the sparked application. 19 Contains the log area 20 """ 21 22 maxLogLines = 2000 23
24 - def __init__(self, app):
25 gtk.Window.__init__(self) 26 self.app = app 27 self.set_title(app.title + " - Status window") 28 self.connect("destroy", self.closed) 29 30 self.build() 31 log.addObserver(self.log) 32 self.created()
33 34
35 - def created(self):
36 """ 37 Called when the status window has been created and is showing 38 on the screen. Override to add custom functionality. 39 """
40 41
42 - def build(self):
43 """ 44 Create the status window 45 """ 46 self.box = gtk.HBox() 47 self.log_area = gtk.TextView() 48 w = gtk.ScrolledWindow() 49 w.add(self.log_area) 50 51 self.box.pack_end(w, True, True, 0) 52 53 if self.app.monitors: 54 # Create the monitor widget and connect 55 w = MonitorWidget(self.app.monitors) 56 self.box.pack_start(w, False, False, 0) 57 58 self.add(self.box) 59 self.set_size_request(720, 300) 60 self.show_all()
61 62
63 - def closed(self, window):
64 log.removeObserver(self.log) 65 guiEvents.dispatch("statuswindow-closed", window) 66 reactor.stop()
67 68
69 - def log(self, dict):
70 """ 71 Log observer that writes into the log scroller area of the UI. 72 73 This method implements C{twisted.python.log.ILogObserver}. 74 """ 75 b = self.log_area.get_buffer() 76 77 end = b.get_end_iter() 78 b.place_cursor(end) 79 80 for m in dict['message']: 81 b.insert_at_cursor("%s | %s\n" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), m)) 82 83 if b.get_line_count() > self.maxLogLines: 84 frm = b.get_start_iter() 85 to = b.get_iter_at_line(b.get_line_count() - self._max_log_lines) 86 b.delete(frm, to) 87 self.log_area.scroll_to_iter(b.get_end_iter(), 0.0)
88 89 90
91 -class MonitorWidget(gtk.VBox):
92 """ 93 A widget which is tied to a L{monitors.MonitorContainer} object; 94 receiving updates as the monitors in the container change state. 95 It shows a list with the titles and the state of each monitor. 96 """ 97 98 stock_map = { True: "gtk-apply", 99 False: "gtk-stop" 100 } 101 102
103 - def __init__(self, container):
104 gtk.VBox.__init__(self) 105 self.set_property("width_request", 240) 106 container.events.addObserver("updated", self.refresh)
107 108
109 - def refresh(self, container):
110 """ 111 Refresh the state when an "updated" event from the linked monitor container comes in. 112 """ 113 for c in self.get_children(): 114 self.remove(c) 115 116 for c in container.monitors: 117 v = gtk.HBox() 118 l = gtk.Label(c.title) 119 l.set_property("xalign", 0) 120 v.pack_start(l, True, True, 10) 121 122 im = gtk.Image() 123 im.set_from_stock(self.stock_map[c.ok], gtk.ICON_SIZE_MENU) 124 v.pack_start(im, False, True, 10) 125 126 self.pack_start(v, False, False, 10) 127 self.pack_start(gtk.HSeparator(), False, True, 0) 128 129 self.show_all()
130 131 132 133 guiEvents = events.EventDispatcher() 134