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