Changed generic observer pattern and project dependency in plots to be a two-way selection-listener model.

The project listens to selections done in plot and broadcasts it back to all plots (including source) which allows them to redraw properly.
This commit is contained in:
2006-04-27 14:38:48 +00:00
parent c6ef6cdb07
commit 5bb93af6f0
2 changed files with 26 additions and 37 deletions

View File

@@ -60,7 +60,7 @@ class Plot (gtk.Frame):
self.sel_obj = None
self.active = False
self.title = title
self.project = None
self.selection_listener = None
def get_title(self):
return self.title
@@ -76,11 +76,17 @@ class Plot (gtk.Frame):
self.set_shadow_type(gtk.SHADOW_OUT)
self.active = active
def update(self, key):
def selection_changed(self, selection):
pass
def set_project(self, project):
self.project = project
def set_selection_listener(self, listener):
"""Allow project to listen to selections.
The selection will propagate back to all plots through the
selection_changed() method. The listener will be called as
listener(dimension_name, ids).
"""
self.selection_listener = listener
def get_toolbar(self):
return None
@@ -365,8 +371,9 @@ class LinePlot(Plot):
silent_eval("k <- sapply(1:length(res), function(id) rev(res[[id]]$y))")
self._bg_matrix = bg_matrix = rpy.r("k")
rpy.r.rm(["k", "res", "m"])
self.update(None)
# Hack - we draw plot in selection_changed()
self.selection_changed(None)
self.add(self.canvas)
self.canvas.show()
@@ -379,15 +386,14 @@ class LinePlot(Plot):
self.canvas.draw()
return self._toolbar
def update(self, key):
def selection_changed(self, selection):
self.ax.clear()
rows, cols = self._bg_matrix.shape
self.ax.imshow(self._bg_matrix, cmap=cm.Greys, extent=(0.5, cols+0.5, self._ymin, self._ymax))
if self.project:
curr_sel = self.project.get_selection() # get selection object
ids = curr_sel['ids'] # current identifiers
if selection:
ids = selection['ids'] # current identifiers
index = [ind for id,ind in self._dataset['ids'].items() if id in ids] #conversion to index
for i in index:
line = self._dataset.get_matrix()[i]
@@ -457,11 +463,10 @@ class ScatterPlot(Plot):
for ind in index:
ids.append(reverse[ind])
self.project.set_selection(self.current_dim,ids)
self.selection_listener(self.current_dim,ids)
def update(self, key):
curr_sel = self.project.get_selection() # get selection object
ids = curr_sel[self.current_dim] # current identifiers
def selection_changed(self, selection):
ids = selection[self.current_dim] # current identifiers
index = [ind for id,ind in self.dataset[self.current_dim].items() if id in ids] #conversion to index
xdata_new = scipy.take(self.xaxis_data,index) #take data