diff --git a/fluents/fluents.py b/fluents/fluents.py index 003995b..fe9297e 100644 --- a/fluents/fluents.py +++ b/fluents/fluents.py @@ -22,6 +22,27 @@ DATADIR = os.path.dirname(sys.modules['fluents'].__file__) ICONDIR = os.path.join(DATADIR,"..","icons") GLADEFILENAME = os.path.join(DATADIR, 'fluents.glade') +class IconFactory: + """Factory for icons that ensures that each icon is only loaded once.""" + + def __init__(self, path): + self._path = path + self._icons = {} + + def get_icon(self, iconname): + """Returns the gdk loaded PixBuf for the given icon. + Reads the icon from file if necessary.""" + + if self._icons.has_key(iconname): + return self._icons[iconname] + + icon_fname = os.path.join(self._path, '%s.png' % iconname) + icon = gtk.gdk.pixbuf_new_from_file(icon_fname) + self._icons[iconname] = icon + return icon + +icon_factory = IconFactory(ICONDIR) + class TableSizeSelection(gtk.Window): def __init__(self): diff --git a/fluents/project.py b/fluents/project.py index fa5cecb..9eadfc3 100644 --- a/fluents/project.py +++ b/fluents/project.py @@ -67,32 +67,32 @@ class Project: else: parent_iter = None - icon_fname = os.path.join(fluents.ICONDIR, "folder_grey.png") - icon = gtk.gdk.pixbuf_new_from_file(icon_fname) + # Add the function node to the tree + icon = fluents.icon_factory.get_icon("folder_grey") it = self.data_tree_insert(parent_iter, fun, None, None, "black", icon) + # Add all returned datasets/plots/selections for d in data: - if isinstance(d, dataset.GraphDataset): + # Any kind of dataset + if isinstance(d, dataset.Dataset): + if isinstance(d, dataset.GraphDataset): + icon = fluents.icon_factory.get_icon("graph_dataset") + elif isinstance(d, dataset.CategoryDataset): + icon = fluents.icon_factory.get_icon("category_dataset") + else: + icon = fluents.icon_factory.get_icon("dataset") + 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) + self.data_tree_insert(it, d.get_name(), d, None, "black", icon) + + # Any kind of plot 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) + icon = fluents.icon_factory.get_icon("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)