Package flumotion :: Package admin :: Package gtk :: Module workerlist
[hide private]

Source Code for Module flumotion.admin.gtk.workerlist

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3   
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  import gettext 
 19   
 20  import gobject 
 21  import gtk 
 22  import pango 
 23  from zope.interface import implements 
 24   
 25  from flumotion.common.pygobject import gsignal 
 26  from flumotion.twisted import flavors 
 27   
 28  __version__ = "$Rev$" 
 29  _ = gettext.gettext 
 30   
 31   
32 -class WorkerListStore(gtk.ListStore):
33 implements(flavors.IStateListener) 34 gsignal('changed') 35
36 - def __init__(self, whs):
37 gtk.ListStore.__init__(self, str) 38 for x in whs.get('names'): 39 i = self.append() 40 self.set_value(i, 0, x) 41 whs.addListener(self, append=self.stateAppend, 42 remove=self.stateRemove)
43
44 - def stateAppend(self, state, key, val):
45 if key == 'names': 46 i = self.append() 47 self.set_value(i, 0, val) 48 self.emit('changed')
49
50 - def stateRemove(self, state, key, val):
51 if key == 'names': 52 for r in self: 53 if self.get_value(r.iter, 0) == val: 54 self.remove(r.iter) 55 self.emit('changed') 56 return
57 gobject.type_register(WorkerListStore) 58 59
60 -class WorkerList(gtk.HBox):
61 gsignal('worker-selected', str) 62 _combobox = None 63 _label = None 64
65 - def __init__(self):
66 gtk.HBox.__init__(self) 67 68 self._combobox = gtk.ComboBox() 69 self._label = gtk.Label(_('Worker:')) 70 71 self._label.show() 72 self.pack_start(self._label, False, False, 0) 73 vb = gtk.VBox() 74 self.pack_start(vb, False, False, 10) 75 vb.show() 76 a = gtk.Alignment(0.5, 0.5) 77 a.show() 78 vb.pack_start(a, True, False, 0) 79 cell = gtk.CellRendererText() 80 cell.set_property('ellipsize', pango.ELLIPSIZE_MIDDLE) 81 cell.set_property('width', 100) 82 self._combobox.pack_start(cell, True) 83 self._combobox.add_attribute(cell, 'text', 0) 84 85 def onChanged(cb): 86 self.emit('worker-selected', self.getWorker())
87 88 self._combobox.connect('changed', onChanged) 89 self._combobox.show() 90 # GTK 2.4 91 try: 92 self._combobox.set_property('focus-on-click', False) 93 self._combobox.set_property('has-frame', False) 94 except TypeError: 95 pass 96 a.add(self._combobox)
97
98 - def setWorkerHeavenState(self, whs):
99 self._combobox.set_model(WorkerListStore(whs)) 100 self.selectWorker(None) 101 102 def onModelChanged(model): 103 if not self.getWorker(): 104 # need to select a new worker 105 self.selectWorker(None) # will emit if worker selected 106 if not self.getWorker(): 107 # no workers present! 108 self.emit('worker-selected', None)
109 110 self._combobox.get_model().connect('changed', onModelChanged) 111
112 - def selectWorker(self, worker):
113 # worker == none means select first worker 114 for r in self._combobox.get_model(): 115 if not worker or r.model.get_value(r.iter, 0) == worker: 116 self._combobox.set_active_iter(r.iter) 117 return 118 119 if worker: 120 # FIXME: let's not print, have correct logging 121 print 'warning: worker %s not available' % worker
122
123 - def getWorker(self):
124 i = self._combobox.get_active_iter() 125 if i: 126 return self._combobox.get_model().get_value(i, 0) 127 128 return None
129
130 - def notifySelected(self):
131 self.emit('worker-selected', self.getWorker())
132 133 gobject.type_register(WorkerList) 134