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:
parent
75447d8840
commit
d45ae4f480
|
@ -22,6 +22,27 @@ DATADIR = os.path.dirname(sys.modules['fluents'].__file__)
|
||||||
ICONDIR = os.path.join(DATADIR,"..","icons")
|
ICONDIR = os.path.join(DATADIR,"..","icons")
|
||||||
GLADEFILENAME = os.path.join(DATADIR, 'fluents.glade')
|
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):
|
class TableSizeSelection(gtk.Window):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
|
@ -67,32 +67,32 @@ class Project:
|
||||||
else:
|
else:
|
||||||
parent_iter = None
|
parent_iter = None
|
||||||
|
|
||||||
icon_fname = os.path.join(fluents.ICONDIR, "folder_grey.png")
|
# Add the function node to the tree
|
||||||
icon = gtk.gdk.pixbuf_new_from_file(icon_fname)
|
icon = fluents.icon_factory.get_icon("folder_grey")
|
||||||
it = self.data_tree_insert(parent_iter, fun, None, None, "black", icon)
|
it = self.data_tree_insert(parent_iter, fun, None, None, "black", icon)
|
||||||
|
|
||||||
|
# Add all returned datasets/plots/selections
|
||||||
for d in data:
|
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)
|
self.add_dataset(d)
|
||||||
icon_fname = os.path.join(fluents.ICONDIR, "graph_dataset.png")
|
self.data_tree_insert(it, d.get_name(), d, None, "black", icon)
|
||||||
icon = gtk.gdk.pixbuf_new_from_file(icon_fname)
|
|
||||||
self.data_tree_insert(it, d.get_name(), d, None, "black",icon)
|
# Any kind of plot
|
||||||
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):
|
elif isinstance(d, plots.Plot):
|
||||||
icon_fname = os.path.join(fluents.ICONDIR,"line_plot.png")
|
icon = fluents.icon_factory.get_icon("line_plot")
|
||||||
icon = gtk.gdk.pixbuf_new_from_file(icon_fname)
|
self.data_tree_insert(it, d.get_title(), d, None, "black", icon)
|
||||||
self.data_tree_insert(it, d.get_title(), d, None, "black",icon)
|
|
||||||
d.set_selection_listener(self.set_selection)
|
d.set_selection_listener(self.set_selection)
|
||||||
self._selection_observers.append(d)
|
self._selection_observers.append(d)
|
||||||
|
|
||||||
|
# Selections are not added to the data tree
|
||||||
elif isinstance(d, dataset.Selection):
|
elif isinstance(d, dataset.Selection):
|
||||||
self.add_selection(d)
|
self.add_selection(d)
|
||||||
|
|
||||||
|
|
Reference in New Issue