144 lines
5.5 KiB
Python
144 lines
5.5 KiB
Python
import os
|
|
import scipy
|
|
import gobject
|
|
import gtk
|
|
import fluents
|
|
import logger
|
|
from system import dataset, plots
|
|
|
|
class Project:
|
|
def __init__(self,name="Testing"):
|
|
self.data_tree = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING,
|
|
gobject.TYPE_PYOBJECT, gobject.TYPE_STRING,
|
|
gobject.TYPE_STRING, gobject.TYPE_OBJECT,
|
|
gobject.TYPE_DOUBLE)
|
|
|
|
self.name = name
|
|
self.dim_names = []
|
|
self._selection_observers = []
|
|
self._dataset_observers = []
|
|
self.current_data = []
|
|
self.datasets = []
|
|
self.sel_obj = dataset.Selection('Current Selection')
|
|
self.selections = []
|
|
self._last_selection = None
|
|
|
|
def add_selection_observer(self, observer):
|
|
self._selection_observers.append(observer)
|
|
#observer.selection_changed(self.get_selection())
|
|
|
|
def notify_selection_listeners(self, dim_name):
|
|
"""Notifies observers"""
|
|
for observer in self._selection_observers:
|
|
observer.selection_changed(dim_name, self.get_selection())
|
|
|
|
def add_dataset_observer(self, observer):
|
|
self._dataset_observers.append(observer)
|
|
observer.dataset_changed()
|
|
|
|
def notify_dataset_listeners(self):
|
|
"""Notifies observers when new datasets are added"""
|
|
for observer in self._dataset_observers:
|
|
observer.dataset_changed()
|
|
|
|
def set_selection(self, dim_name, selection):
|
|
"""Sets a current selection and notify observers"""
|
|
if self._last_selection != selection:
|
|
self.sel_obj[dim_name] = set(selection)
|
|
self.notify_selection_listeners(dim_name)
|
|
self._last_selection = selection
|
|
|
|
def get_selection(self):
|
|
"""Returns the current selection object"""
|
|
return self.sel_obj
|
|
|
|
def get_data_iter(self, obj):
|
|
"""Retuns an iterator to data."""
|
|
retval = [None]
|
|
if obj:
|
|
# add data below first function data input
|
|
obj=obj[0]
|
|
|
|
def is_obj(m, p, i, d):
|
|
if obj == m.get_value(i, 2):
|
|
d.append(i)
|
|
return True
|
|
|
|
self.data_tree.foreach(is_obj, retval)
|
|
return retval[-1]
|
|
|
|
def add_data(self, parents, data, fun='Function'):
|
|
"""Adds a set of data and plots to the navigator.
|
|
|
|
This method is usually called after a Function in a workflow
|
|
has finished and returns its output."""
|
|
|
|
parent_iter = self.get_data_iter(parents)
|
|
icon_fname = os.path.join(fluents.ICONDIR,"folder_grey.png")
|
|
icon = gtk.gdk.pixbuf_new_from_file(icon_fname)
|
|
it = self.data_tree_insert(parent_iter, fun, None, None,"black",icon)
|
|
|
|
for d in data:
|
|
if isinstance(d, dataset.GraphDataset):
|
|
self.add_dataset(d)
|
|
icon_fname = os.path.join(fluents.ICONDIR,"graph_dataset.png")
|
|
icon = gtk.gdk.pixbuf_new_from_file(icon_fname)
|
|
self.data_tree_insert(it, d.get_name(), d, None, "black",icon)
|
|
elif isinstance(d,dataset.CategoryDataset):
|
|
icon_fname = os.path.join(fluents.ICONDIR,"category_dataset.png")
|
|
icon = gtk.gdk.pixbuf_new_from_file(icon_fname)
|
|
self.add_dataset(d)
|
|
self.data_tree_insert(it, d.get_name(), d, None, "black",icon)
|
|
elif isinstance(d, dataset.Dataset):
|
|
icon_fname = os.path.join(fluents.ICONDIR,"dataset.png")
|
|
icon = gtk.gdk.pixbuf_new_from_file(icon_fname)
|
|
self.add_dataset(d)
|
|
self.data_tree_insert(it, d.get_name(), d, None, "black",icon)
|
|
elif isinstance(d, plots.Plot):
|
|
icon_fname = os.path.join(fluents.ICONDIR,"line_plot.png")
|
|
icon = gtk.gdk.pixbuf_new_from_file(icon_fname)
|
|
self.data_tree_insert(it, d.get_title(), d, None, "black",icon)
|
|
d.set_selection_listener(self.set_selection)
|
|
self._selection_observers.append(d)
|
|
elif isinstance(d, dataset.Selection):
|
|
self.add_selection(d)
|
|
|
|
def data_tree_insert(self, parent, text, data, bgcolour,fontcolour,icon,selected = 0):
|
|
tree = self.data_tree
|
|
it = tree.append(parent)
|
|
tree.set_value(it, 0, text)
|
|
tree.set_value(it, 1, type(data))
|
|
tree.set_value(it, 2, data)
|
|
tree.set_value(it, 3, bgcolour)
|
|
tree.set_value(it, 4, fontcolour)
|
|
tree.set_value(it, 5, icon)
|
|
tree.set_value(it, 6, selected)
|
|
return it
|
|
|
|
def add_dataset(self, dataset):
|
|
"""Appends a new Dataset to the project."""
|
|
logger.log('debug','Adding dataset: %s' %dataset.get_name())
|
|
self.datasets.append(dataset)
|
|
for dim_name in dataset.get_all_dims():
|
|
if dim_name not in self.dim_names:
|
|
self.dim_names.append(dim_name)
|
|
self.sel_obj[dim_name] = set()
|
|
self.notify_dataset_listeners()
|
|
|
|
def add_selection(self, selection):
|
|
"""Adds a new selection to the project."""
|
|
self.selections.append(selection)
|
|
self.notify_dataset_listeners()
|
|
|
|
def object_at(self, path):
|
|
"Returns the object at a given path in the tree."
|
|
iter = self.get_iter(path)
|
|
object = self.get_value(iter, 2)
|
|
if object:
|
|
object.show()
|
|
return object
|
|
|
|
def set_current_data(self, obj):
|
|
self.current_data = obj
|
|
|