# CellTracking.py
# By MW, Jun 2013
# GPLv3+
#
# Class the display of the cells. Should be called by a player instance for example.
import gtk, imp, logging, itertools, gobject
MenuCells = imp.load_source("MenuCells", './bin/Movies/Cells/Cells_menu_gtk.py')
NewCells = imp.load_source("NewCells", './bin/Movies/Cells/Cells_newcell_gtk.py')
[docs]class CellsPanel :
"""A class displaying a cell panel with lot of options to edit them"""
def __init__(self, state) :
# Variables
self.state = state
self.hidden = False
self.initiated = False
self.cells_dict = {} # To initialize properly
self.cells_list_dict = {} # ibid.
self.panel_rb_none = gtk.RadioButton(None)
self.panel_rb = self.panel_rb_none # The radiobutton for the left panel
self.r_panel = None # The right panel
#self.r_panel = gtk.ScrolledWindow() ## EXPERIMENTAL
self.cells_list = None
# Init cell panel
self.init_panel()
self.selected_cell = None # Reference to the gtk selected cell
self.new_cell_panel = NewCells.Panel(state, self)
# Populating cells gtk
cells = self.state.movie.Cells.get_cells_dict()
for n in cells.keys() :
tmpc = self.new_cell_panel.new_cell('pol')
tmpc.set_cell(cells[n])
self.add_cell(n, tmpc)
[docs] def init_panel(self) :
"""Initialize the panel in the background. This is required to
load cells from a file"""
# Window
self.window = gtk.Window()
self.window.set_title("Cells")
self.window.connect("delete-event", self.hide_show_change)
panel = gtk.VBox()
self.window.add(panel)
self.window.set_default_size(530, 500) # Specify size
# Menu
self.menu = MenuCells.Menu(self)
self.menu_w = self.menu.get_menu()
panel.pack_start(self.menu_w, fill=False, expand=False)
# Left panel
l_scrollbar = gtk.ScrolledWindow()
l_scrollbar.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
self.cells_list = gtk.VBox()
self.cells_list.pack_start(gtk.Label("Cells"), fill=False, expand=False)
self.cells_list.pack_start(gtk.HSeparator(), fill=False, expand=False)
l_scrollbar.add_with_viewport(self.cells_list)
# Right panel
self.r_panel = gtk.ScrolledWindow()
self.r_panel.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
self.r_panel.show_all()
# split window
self.b_panel = gtk.HPaned() # Bottom panel
self.b_panel.add1(l_scrollbar)
self.b_panel.add2(self.r_panel)
self.b_panel.set_position(200) # Size of the left panel
panel.pack_start(self.b_panel, expand=True, fill=True)
self.window.show_all()
self.initiated = True
self.hidden = True
# Hide panel
self.hide_show_panel(hide=True)
[docs] def hide_show_panel(self, hide=None) :
"""hide=None means 'switch state'"""
if ((hide or (not self.hidden and hide==None)) and self.initiated) : # Hide a displayed panel
self.window.hide()
self.hidden = True
elif ((not hide or (self.hidden and hide==None)) and self.initiated) :
self.window.show()
self.hidden = False
elif ((self.hidden or not hide) and not self.initiated) : # Create a new display
pass
# Moved to init_panel
else :
logging.error("This case in hide_show_panel had never been considered. Fatal.")
return True
[docs] def draw(self, im) :
"""Function that return an image with the cells that have to be displayed"""
if (self.cells_dict != {}) or (self.new_cell_panel.is_polygon_open()):
im = self.new_cell_panel.draw(im)
sth_selected = False
for c in self.cells_dict.values() :
im = c.draw_polygon(im)
if c.get_selected() :
self.selected_cell = c
sth_selected = True
if not sth_selected :
self.selected_cell = None
return im
[docs] def hide_show_change(self, q, qq=None) :
self.hide_show_panel(hide=True)
return True
[docs] def hide_show_new_cell_panel(self, celltype=None) :
self.new_cell_panel.set_visible(celltype)
[docs] def click(self, tup) :
r = self.new_cell_panel.click(tup) # Returns True if click intercepted
sth_selected = False
if not r :
for c in self.cells_list_dict.values() :
res = c.click(tup)
if res :
sth_selected = True
self.selected_cell = c
if not sth_selected :
logging.debug("everything is unselected")
self.panel_rb_none.set_active(True)
self.selected_cell = None
[docs] def set_image_move(self, tup) :
"""Function called when the users keeps the left button pressed on
the viewer panel"""
# directly send to selected cell
if self.selected_cell != None :
self.selected_cell.move(tup)
[docs] def unclick(self) :
"""Function that forward the signal that the user has released the
click to all cells"""
for c in self.cells_list_dict.values() :
c.unclick()
[docs] def delete_cell(self, name) :
"""Function that delete a cell"""
cell = self.cells_list_dict[name] # A gtk cell
cell.deleted = True
self.r_panel.remove(self.r_panel.get_children()[0]) # Right panel
# left panel
self.panel_rb_none.set_active(True) # Unselecting cell
self.cells_list.remove(cell.l_panel)
#logging.error("Function remove cell to be fully implemented")
[docs] def add_cell(self, name, cell) :
if name not in self.cells_dict.keys() :
self.cells_dict[name] = cell
n = cell.get_name()
# Record it to the base so that we can esily remove it
self.cells_list_dict[n] = cell
# Add cell to the panel
cell.set_r_panel(self.r_panel)
self.panel_rb = gtk.RadioButton(self.panel_rb)
p = cell.get_panel(self.panel_rb, self.panel_rb_none)
p.show_all()
box = gtk.HBox()
self.panel_rb.connect('toggled', self.rb_toggled, cell)
#cell.set_selected(True)
self.panel_rb.set_active(True)
cell.rb_toggled(True)
box.pack_start(self.panel_rb, fill=False, expand=False)
box.pack_start(p, fill=False, expand=False)
box.show_all()
cell.l_panel = box
self.cells_list.pack_start(box, fill=False, expand=False)
# The newly created cell is automatically selected
self.selected_cell = cell
[docs] def rb_toggled(self, w, cell) :
"""Function called when the user clicks on a radiobutton
to select another cell"""
cell.rb_toggled(w.get_active())
[docs] def make_iter_progress(self, it, pb, label, incr, name) :
"""Returns an iterator that update everything;
it: the cell iterator, that actually do the job
pb: a progress abr instance
label: a gtk label instance
incr: the progress bar increment
name: the name to set as the new label
This function Never *yields* False because it will break the gobject.
When yield False is needed, you should call the end_progress function
at the end of the full iteration.
"""
r = it.next()
while r :
pb.set_fraction(pb.get_fraction()+incr)
label.set_text(name)
r = it.next()
yield True
[docs] def end_progress(self, window) :
"""Destroys the window with the progress bar.
Returns an iterator
"""
window.destroy()
yield False
[docs] def export_progress_bar(self, cells_list, export_kw) :
"""Function that manage everything to create a progress bar and
export the data specified by both the list of cells (cells_list),
a list of tuples of format (cell_object, channel_to_export) and the
export keyword corresponding to the function to use for the export
(for instance self.export_tiff) that bind to the export function in
the cell. The export keyword can be 'tiff' or 'csv' for the moment.
"""
# Create window
window = gtk.Window()
window.set_title("Exporting cells")
box = gtk.VBox()
progress_bar = gtk.ProgressBar()
label = gtk.Label('Initiating')
box.pack_start(label)
box.pack_start(progress_bar)
window.add(box)
window.show_all()
# Initiate the bar
nframes = 0
for cell in cells_list :
bounds = cell[0].get_bounds()
channel = cell[1]
nframes += bounds[1]-bounds[0]
incr = 1./nframes
# Export
it3 = []
for cell in cells_list :
export_function = {'tiff' : cell[0].export_as_tiff, 'csv' : cell[0].export_as_csv}[export_kw]
name = cell[0].get_name()
it = export_function(cell[2], cell[1]) # An iterator (folder, channel)
it2 = self.make_iter_progress(it, progress_bar, label, incr, name)
it3 = itertools.chain(it3, it2)
print cell[0].get_name()
it3 = itertools.chain(it3, self.end_progress(window))
gobject.idle_add(it3.next)