diff --git a/laydi/projectview.py b/laydi/projectview.py new file mode 100644 index 0000000..024ce21 --- /dev/null +++ b/laydi/projectview.py @@ -0,0 +1,154 @@ +import os +import scipy +import gobject +import gtk +import laydi +import logger, dataset, plots, main, project + +class ProjectView: + """A Project contains datasets, selections etc. + The project, of which the application has only one at any given time, + is the container for all datasets, plots and selections in use. The data + in the project is organized in a gtk.TreeStrore that is displayed in the + navigator. + """ + + def __init__(self, project_dir): + self.data_tree = gtk.TreeStore(str, + str, + object, + str, + str, + gobject.TYPE_OBJECT, + float) + + self.project = project.Project(project_dir) + 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 + self._dataset_iter_map = {} + + def add_selection_observer(self, observer): + self._selection_observers.append(observer) + observer.selection_changed(None, 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""" + 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 delete_data(self, it): + """Delete elements from the project.""" + child = self.data_tree.iter_children(it) + while child != None: + c = self.data_tree.iter_next(child) + self.delete_data(child) + child = c + main.application.main_view.remove_view(self.data_tree.get(it, 2)[0]) + self.data_tree.remove(it) + + 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.""" + + if len(parents) > 0: + parent_iter = self._dataset_iter_map[parents[0]] + else: + parent_iter = None + + # Add the function node to the tree + icon = laydi.icon_factory.get("folder_grey") + it = self.data_tree_insert(parent_iter, fun, None, None, "black", icon) + + # Add all returned datasets/plots/selections + for d in data: + # Any kind of dataset + if isinstance(d, dataset.Dataset): + if isinstance(d, dataset.GraphDataset): + icon = laydi.icon_factory.get("graph_dataset") + elif isinstance(d, dataset.CategoryDataset): + icon = laydi.icon_factory.get("category_dataset") + else: + icon = laydi.icon_factory.get("dataset") + + self.add_dataset(d) + self.data_tree_insert(it, d.get_name(), d, None, "black", icon) + + # Any kind of plot + elif isinstance(d, plots.Plot): + icon = laydi.icon_factory.get("line_plot") + self.data_tree_insert(it, d.get_title(), d, None, "black", icon) + d.set_selection_listener(self.set_selection) + self._selection_observers.append(d) + + # Selections are not added to the data tree + elif isinstance(d, dataset.Selection): + self.add_selection(d) + + def data_tree_insert(self, parent, text, data, bg, fg, icon, selected = 0): + """Inserts data into the tree view. + @param text: The title of the object. + @param data: A dataset, plot or function object. + @param bg: Background color. + @param fg: Foreground (font) color. + @param icon: Pixmap icon. + """ + tree = self.data_tree + it = tree.append(parent) + tree[it] = [text, type(data), data, bg, fg, icon, selected] + self._dataset_iter_map[data] = it + 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_selection_listeners(dim_name) + 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.""" + it = self.get_iter(path) + obj = self[it][2] + if obj: + obj.show() + return obj + + #def set_current_data(self, obj): + # self.current_data = obj +