Code cleanup.
This commit is contained in:
parent
055c0ea4ac
commit
d0a7b1cbc2
|
@ -8,6 +8,12 @@ import dataset, logger, plots, project, workflow, main
|
||||||
import scipy
|
import scipy
|
||||||
|
|
||||||
class NavigatorView (gtk.TreeView):
|
class NavigatorView (gtk.TreeView):
|
||||||
|
"""The NavigatorView is a tree view of the project.
|
||||||
|
|
||||||
|
There is always one NavigatorView, that shows the functions, plots and
|
||||||
|
datasets in the current project.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
if main.project:
|
if main.project:
|
||||||
self.data_tree = main.project.data_tree
|
self.data_tree = main.project.data_tree
|
||||||
|
@ -16,15 +22,14 @@ class NavigatorView (gtk.TreeView):
|
||||||
|
|
||||||
gtk.TreeView.__init__(self)
|
gtk.TreeView.__init__(self)
|
||||||
|
|
||||||
# various properties
|
# Various properties
|
||||||
|
|
||||||
self.set_enable_tree_lines(True)
|
self.set_enable_tree_lines(True)
|
||||||
self.set_headers_visible(False)
|
self.set_headers_visible(False)
|
||||||
self.get_hadjustment().set_value(0)
|
self.get_hadjustment().set_value(0)
|
||||||
|
|
||||||
# Selection Mode
|
# Selection Mode
|
||||||
self.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
|
self.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
|
||||||
self.get_selection().connect('changed',self.selection_changed_handler)
|
self.get_selection().connect('changed',self.on_selection_changed)
|
||||||
self._previous_selection = []
|
self._previous_selection = []
|
||||||
|
|
||||||
# Setting up TextRenderers etc
|
# Setting up TextRenderers etc
|
||||||
|
@ -41,7 +46,8 @@ class NavigatorView (gtk.TreeView):
|
||||||
self.object_col = gtk.TreeViewColumn('Object')
|
self.object_col = gtk.TreeViewColumn('Object')
|
||||||
self.object_col.pack_start(pixbufrenderer,expand=False)
|
self.object_col.pack_start(pixbufrenderer,expand=False)
|
||||||
self.object_col.pack_start(textrenderer,expand=False)
|
self.object_col.pack_start(textrenderer,expand=False)
|
||||||
self.object_col.set_attributes(textrenderer, cell_background=3, foreground=4, text=0)
|
self.object_col.set_attributes(textrenderer, cell_background=3,
|
||||||
|
foreground=4, text=0)
|
||||||
self.object_col.set_attributes(pixbufrenderer, pixbuf=5)
|
self.object_col.set_attributes(pixbufrenderer, pixbuf=5)
|
||||||
self.append_column(self.object_col)
|
self.append_column(self.object_col)
|
||||||
|
|
||||||
|
@ -54,20 +60,22 @@ class NavigatorView (gtk.TreeView):
|
||||||
|
|
||||||
logger.log('debug', 'Initializing navigator window.')
|
logger.log('debug', 'Initializing navigator window.')
|
||||||
|
|
||||||
# sets data for drag event.
|
|
||||||
def slot_drag_data(self, treeview, context, selection, target_id, etime):
|
def slot_drag_data(self, treeview, context, selection, target_id, etime):
|
||||||
|
"""Sets the data for a drag event."""
|
||||||
treeselection = treeview.get_selection()
|
treeselection = treeview.get_selection()
|
||||||
model, paths = treeselection.get_selected_rows()
|
model, paths = treeselection.get_selected_rows()
|
||||||
if paths:
|
if paths:
|
||||||
self.data_tree.drag_data_get(paths[0], selection)
|
self.data_tree.drag_data_get(paths[0], selection)
|
||||||
|
|
||||||
def add_project(self, project):
|
def add_project(self, project):
|
||||||
|
"""Dependency injection."""
|
||||||
self.data_tree = project.data_tree
|
self.data_tree = project.data_tree
|
||||||
self.set_model(project.data_tree)
|
self.set_model(project.data_tree)
|
||||||
self.data_tree.connect('row-changed',self.on_row_changed)
|
self.data_tree.connect('row-changed',self.on_row_changed)
|
||||||
|
|
||||||
# selection changed, setting current_data ojbects
|
def on_selection_changed(self, selection):
|
||||||
def selection_changed_handler(self, selection):
|
"""Update the list of currently selected datasets."""
|
||||||
|
|
||||||
# update prev selection right away in case of multiple events
|
# update prev selection right away in case of multiple events
|
||||||
model, paths = selection.get_selected_rows()
|
model, paths = selection.get_selected_rows()
|
||||||
if not paths: # a plot is marked: do nothing
|
if not paths: # a plot is marked: do nothing
|
||||||
|
@ -75,17 +83,17 @@ class NavigatorView (gtk.TreeView):
|
||||||
|
|
||||||
tmp = self._previous_selection
|
tmp = self._previous_selection
|
||||||
self._previous_selection = paths
|
self._previous_selection = paths
|
||||||
|
tree = self.data_tree
|
||||||
|
|
||||||
# set timestamp on newly selected objects
|
# set timestamp on newly selected objects
|
||||||
[self.data_tree.set_value(self.data_tree.get_iter(path), 6, time.time())
|
[tree.set_value(tree.get_iter(path), 6, time.time())
|
||||||
for path in paths if path not in tmp]
|
for path in paths if path not in tmp]
|
||||||
|
|
||||||
objs = [self.data_tree.get_iter(path) for path in paths]
|
objs = [tree.get_iter(path) for path in paths]
|
||||||
objs = [(self.data_tree.get_value(iter,6), self.data_tree.get_value(iter,2))
|
objs = [(tree[iter][6], tree[iter][2]) for iter in objs]
|
||||||
for iter in objs]
|
|
||||||
objs.sort()
|
objs.sort()
|
||||||
objs = [obj for timestamp, obj in objs]
|
objs = [obj for timestamp, obj in objs]
|
||||||
# order dataset
|
|
||||||
|
|
||||||
if objs and isinstance(objs[0], dataset.Dataset):
|
if objs and isinstance(objs[0], dataset.Dataset):
|
||||||
logger.log('debug', 'Selecting dataset')
|
logger.log('debug', 'Selecting dataset')
|
||||||
|
@ -94,13 +102,12 @@ class NavigatorView (gtk.TreeView):
|
||||||
logger.log('debug', 'Deselecting dataset')
|
logger.log('debug', 'Deselecting dataset')
|
||||||
main.project.set_current_data([])
|
main.project.set_current_data([])
|
||||||
|
|
||||||
|
|
||||||
# TreeView changed. Set correct focus and colours
|
|
||||||
def on_row_changed(self, treestore, pos, iter):
|
def on_row_changed(self, treestore, pos, iter):
|
||||||
obj = treestore.get_value(iter,2)
|
"""Set correct focus and colours when rows have changed."""
|
||||||
type_= treestore.get_value(iter,1)
|
obj = treestore[iter][2]
|
||||||
|
obj_type = treestore[iter][1]
|
||||||
|
|
||||||
if not (treestore.get_value(iter,2) or treestore.get_value(iter,1)):
|
if not (obj or obj_type):
|
||||||
return
|
return
|
||||||
self.expand_to_path(pos)
|
self.expand_to_path(pos)
|
||||||
|
|
||||||
|
@ -135,12 +142,12 @@ class NavigatorView (gtk.TreeView):
|
||||||
|
|
||||||
def on_mouse_event(self, widget, event):
|
def on_mouse_event(self, widget, event):
|
||||||
path = widget.get_path_at_pos(int(event.x), int(event.y))
|
path = widget.get_path_at_pos(int(event.x), int(event.y))
|
||||||
iter = None
|
|
||||||
|
|
||||||
if path:
|
if path:
|
||||||
iter = self.data_tree.get_iter(path[0])
|
iter = self.data_tree.get_iter(path[0])
|
||||||
obj = self.data_tree.get_value(iter, 2)
|
obj = self.data_tree.get_value(iter, 2)
|
||||||
else:
|
else:
|
||||||
|
iter = None
|
||||||
obj = None
|
obj = None
|
||||||
|
|
||||||
if isinstance(obj, dataset.Dataset):
|
if isinstance(obj, dataset.Dataset):
|
||||||
|
@ -152,9 +159,15 @@ class NavigatorView (gtk.TreeView):
|
||||||
self.menu.popup(None, None, None, event.button, event.time)
|
self.menu.popup(None, None, None, event.button, event.time)
|
||||||
|
|
||||||
def on_cursor_changed(self, widget):
|
def on_cursor_changed(self, widget):
|
||||||
|
"""Update statusbar to contain dataset information.
|
||||||
|
|
||||||
|
Lists the dimensions of a dataset in the statusbar of the program
|
||||||
|
if a dataset is focused in the navigator.
|
||||||
|
"""
|
||||||
path = widget.get_cursor()[0]
|
path = widget.get_cursor()[0]
|
||||||
tree_iter = self.data_tree.get_iter(path)
|
tree_iter = self.data_tree.get_iter(path)
|
||||||
obj = self.data_tree.get_value(tree_iter, 2)
|
obj = self.data_tree.get_value(tree_iter, 2)
|
||||||
|
|
||||||
if isinstance(obj, dataset.Dataset):
|
if isinstance(obj, dataset.Dataset):
|
||||||
dims = zip(obj.get_dim_name(), obj.shape)
|
dims = zip(obj.get_dim_name(), obj.shape)
|
||||||
dim_text = ", ".join(["%s (%d)" % dim for dim in dims])
|
dim_text = ", ".join(["%s (%d)" % dim for dim in dims])
|
||||||
|
@ -250,20 +263,9 @@ class NavigatorMenu(gtk.Menu):
|
||||||
self.plot_item.set_property('sensitive', True)
|
self.plot_item.set_property('sensitive', True)
|
||||||
self.trans_item.set_property('sensitive', True)
|
self.trans_item.set_property('sensitive', True)
|
||||||
|
|
||||||
def on_load_dataset(self, item, navigator):
|
def load_dataset(self, filename):
|
||||||
dialog = gtk.FileChooserDialog('Load dataset')
|
"""Load the dataset from the given file and add it to the project."""
|
||||||
dialog.set_action(gtk.FILE_CHOOSER_ACTION_OPEN)
|
ds = dataset.read_ftsv(filename)
|
||||||
dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)
|
|
||||||
dialog.set_select_multiple(True)
|
|
||||||
dialog.set_current_folder(main.options.datadir)
|
|
||||||
retval = dialog.run()
|
|
||||||
if retval in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT]:
|
|
||||||
pass
|
|
||||||
elif retval == gtk.RESPONSE_OK:
|
|
||||||
for filename in dialog.get_filenames():
|
|
||||||
fd = open(filename)
|
|
||||||
ds = dataset.read_ftsv(fd)
|
|
||||||
fd.close()
|
|
||||||
|
|
||||||
if isinstance(ds, dataset.GraphDataset):
|
if isinstance(ds, dataset.GraphDataset):
|
||||||
icon = fluents.icon_factory.get("graph_dataset")
|
icon = fluents.icon_factory.get("graph_dataset")
|
||||||
|
@ -274,6 +276,22 @@ class NavigatorMenu(gtk.Menu):
|
||||||
|
|
||||||
main.project.add_dataset(ds)
|
main.project.add_dataset(ds)
|
||||||
main.project.data_tree_insert(None, ds.get_name(), ds, None, "black", icon)
|
main.project.data_tree_insert(None, ds.get_name(), ds, None, "black", icon)
|
||||||
|
|
||||||
|
def on_load_dataset(self, item, navigator):
|
||||||
|
# Set up file chooser.
|
||||||
|
dialog = gtk.FileChooserDialog('Load dataset')
|
||||||
|
dialog.set_action(gtk.FILE_CHOOSER_ACTION_OPEN)
|
||||||
|
dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
|
||||||
|
gtk.STOCK_OPEN, gtk.RESPONSE_OK)
|
||||||
|
dialog.set_select_multiple(True)
|
||||||
|
dialog.set_current_folder(main.options.datadir)
|
||||||
|
|
||||||
|
retval = dialog.run()
|
||||||
|
if retval in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT]:
|
||||||
|
pass
|
||||||
|
elif retval == gtk.RESPONSE_OK:
|
||||||
|
for filename in dialog.get_filenames():
|
||||||
|
self.load_dataset(filename)
|
||||||
else:
|
else:
|
||||||
print "unknown; ", retval
|
print "unknown; ", retval
|
||||||
dialog.destroy()
|
dialog.destroy()
|
||||||
|
|
|
@ -7,14 +7,21 @@ import logger
|
||||||
import dataset, plots, main
|
import dataset, plots, main
|
||||||
|
|
||||||
class Project:
|
class Project:
|
||||||
|
"""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,name="Testing"):
|
def __init__(self,name="Testing"):
|
||||||
self.data_tree = gtk.TreeStore(gobject.TYPE_STRING,
|
self.data_tree = gtk.TreeStore(str,
|
||||||
gobject.TYPE_STRING,
|
str,
|
||||||
gobject.TYPE_PYOBJECT,
|
object,
|
||||||
gobject.TYPE_STRING,
|
str,
|
||||||
gobject.TYPE_STRING,
|
str,
|
||||||
gobject.TYPE_OBJECT,
|
gobject.TYPE_OBJECT,
|
||||||
gobject.TYPE_DOUBLE)
|
float)
|
||||||
|
|
||||||
self.name = name
|
self.name = name
|
||||||
self.dim_names = []
|
self.dim_names = []
|
||||||
|
@ -104,16 +111,17 @@ class Project:
|
||||||
elif isinstance(d, dataset.Selection):
|
elif isinstance(d, dataset.Selection):
|
||||||
self.add_selection(d)
|
self.add_selection(d)
|
||||||
|
|
||||||
def data_tree_insert(self, parent, text, data, bgcolour, fontcolour, icon, selected = 0):
|
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
|
tree = self.data_tree
|
||||||
it = tree.append(parent)
|
it = tree.append(parent)
|
||||||
tree.set_value(it, 0, text)
|
tree[it] = [text, type(data), data, bg, fg, icon, selected]
|
||||||
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)
|
|
||||||
self._dataset_iter_map[data] = it
|
self._dataset_iter_map[data] = it
|
||||||
return it
|
return it
|
||||||
|
|
||||||
|
@ -144,4 +152,3 @@ class Project:
|
||||||
def set_current_data(self, obj):
|
def set_current_data(self, obj):
|
||||||
self.current_data = obj
|
self.current_data = obj
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue