Added icon factory to ensure that all icons are loaded exactly once. This also

cleans up code and saves X pixmap memory.
This commit is contained in:
Einar Ryeng 2007-01-03 13:34:40 +00:00
parent 75447d8840
commit d45ae4f480
2 changed files with 40 additions and 19 deletions

View File

@ -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):

View File

@ -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)