2006-04-16 20:25:54 +02:00
|
|
|
|
|
|
|
import gtk
|
|
|
|
import gobject
|
|
|
|
import plots
|
2006-05-09 14:22:50 +02:00
|
|
|
import time
|
2007-01-12 15:37:44 +01:00
|
|
|
import fluents
|
2006-10-17 16:42:27 +02:00
|
|
|
import dataset, logger, plots, project, workflow
|
2006-04-16 20:25:54 +02:00
|
|
|
|
|
|
|
class NavigatorView (gtk.TreeView):
|
2006-04-21 11:23:05 +02:00
|
|
|
def __init__(self, project, app):
|
|
|
|
self.project = project
|
2006-04-16 20:25:54 +02:00
|
|
|
self.app = app
|
2006-04-22 23:46:44 +02:00
|
|
|
if project:
|
|
|
|
self.data_tree = project.data_tree
|
|
|
|
else:
|
|
|
|
self.data_tree = None
|
2006-04-21 11:23:05 +02:00
|
|
|
|
2006-04-27 17:51:25 +02:00
|
|
|
gtk.TreeView.__init__(self)
|
2006-04-16 20:25:54 +02:00
|
|
|
|
2006-05-03 13:11:45 +02:00
|
|
|
# various properties
|
2006-04-16 20:25:54 +02:00
|
|
|
self.set_headers_visible(False)
|
2006-08-30 12:27:45 +02:00
|
|
|
self.get_hadjustment().set_value(0)
|
2007-01-03 14:05:37 +01:00
|
|
|
self.set_enable_tree_lines(True)
|
|
|
|
|
2006-05-03 13:11:45 +02:00
|
|
|
# Selection Mode
|
|
|
|
self.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
|
|
|
|
self.get_selection().set_select_function(self.is_selectable)
|
2006-05-03 13:52:54 +02:00
|
|
|
self.get_selection().connect('changed',self.selection_changed_handler)
|
2006-05-09 14:22:50 +02:00
|
|
|
self._previous_selection = []
|
2006-05-03 13:11:45 +02:00
|
|
|
|
|
|
|
# Setting up TextRenderers etc
|
2006-05-03 13:52:54 +02:00
|
|
|
# self.connect('cursor_changed', self.cursor_changed_handler)
|
2006-04-21 16:59:25 +02:00
|
|
|
self.connect('row_activated', self.row_activated_handler)
|
2006-04-16 20:25:54 +02:00
|
|
|
|
2007-01-12 15:37:44 +01:00
|
|
|
# Activate context menu
|
|
|
|
self.menu = NavigatorMenu(self)
|
|
|
|
self.connect('popup_menu', self.popup_menu)
|
2007-01-15 14:58:56 +01:00
|
|
|
self.connect('button_press_event', self.on_mouse_event)
|
2007-01-12 15:37:44 +01:00
|
|
|
|
2006-08-28 14:06:05 +02:00
|
|
|
self.textrenderer = textrenderer = gtk.CellRendererText()
|
2006-08-30 12:27:45 +02:00
|
|
|
pixbufrenderer = gtk.CellRendererPixbuf()
|
2006-04-27 17:51:25 +02:00
|
|
|
self.object_col = gtk.TreeViewColumn('Object')
|
2006-08-30 12:27:45 +02:00
|
|
|
self.object_col.pack_start(pixbufrenderer,expand=False)
|
2006-08-28 14:06:05 +02:00
|
|
|
self.object_col.pack_start(textrenderer,expand=False)
|
|
|
|
self.object_col.set_attributes(textrenderer, cell_background=3, foreground=4, text=0)
|
2006-08-30 12:27:45 +02:00
|
|
|
self.object_col.set_attributes(pixbufrenderer, pixbuf=5)
|
2006-04-16 20:25:54 +02:00
|
|
|
self.append_column(self.object_col)
|
|
|
|
|
2006-05-03 13:11:45 +02:00
|
|
|
# send events to plots / itself
|
2006-08-31 12:57:44 +02:00
|
|
|
self.enable_model_drag_source(gtk.gdk.BUTTON1_MASK,
|
|
|
|
[("GTK_TREE_MODEL_ROW", gtk.TARGET_SAME_APP, 7)],
|
|
|
|
gtk.gdk.ACTION_LINK | gtk.gdk.ACTION_MOVE)
|
2006-05-03 13:11:45 +02:00
|
|
|
|
2006-05-02 13:09:55 +02:00
|
|
|
self.connect("drag-data-get",self.slot_drag_data)
|
|
|
|
|
2006-04-21 12:56:56 +02:00
|
|
|
logger.log('debug', 'Initializing navigator window.')
|
2006-04-16 20:25:54 +02:00
|
|
|
|
2006-05-03 13:11:45 +02:00
|
|
|
|
2006-05-02 13:09:55 +02:00
|
|
|
# sets data for drag event.
|
|
|
|
def slot_drag_data(self,treeview,context,selection,target_id,etime):
|
2006-08-28 14:06:05 +02:00
|
|
|
treeselection = treeview.get_selection()
|
|
|
|
model, paths = treeselection.get_selected_rows()
|
|
|
|
if paths:
|
2006-05-03 13:11:45 +02:00
|
|
|
self.data_tree.drag_data_get(paths[0], selection)
|
2006-05-02 13:09:55 +02:00
|
|
|
|
2006-04-22 23:46:44 +02:00
|
|
|
def add_project(self, project):
|
|
|
|
self.project = project
|
|
|
|
self.data_tree = project.data_tree
|
|
|
|
self.set_model(project.data_tree)
|
2006-04-27 16:15:13 +02:00
|
|
|
self.data_tree.connect('row-changed',self.row_changed_handler)
|
2006-05-03 13:11:45 +02:00
|
|
|
|
|
|
|
def is_selectable(self,path):
|
|
|
|
if self.data_tree:
|
|
|
|
obj = self.data_tree.get_value(self.data_tree.get_iter(path),2)
|
|
|
|
if not obj:
|
|
|
|
return False
|
|
|
|
if not isinstance(obj, dataset.Dataset):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
# selection changed, setting current_data ojbects
|
2006-05-03 13:52:54 +02:00
|
|
|
def selection_changed_handler(self, selection):
|
2006-05-09 14:22:50 +02:00
|
|
|
# update prev selection right away in case of multiple events
|
2006-05-03 13:11:45 +02:00
|
|
|
model, paths = selection.get_selected_rows()
|
2006-08-28 14:06:05 +02:00
|
|
|
if not paths: # a plot is marked: do nothing
|
|
|
|
return
|
|
|
|
|
|
|
|
tmp = self._previous_selection
|
2006-05-09 14:22:50 +02:00
|
|
|
self._previous_selection = paths
|
|
|
|
# set timestamp on newly selected objects
|
2006-08-31 12:57:44 +02:00
|
|
|
[self.data_tree.set_value(self.data_tree.get_iter(path), 6, time.time())
|
|
|
|
for path in paths if path not in tmp]
|
2006-05-09 14:22:50 +02:00
|
|
|
|
|
|
|
objs = [self.data_tree.get_iter(path) for path in paths]
|
2006-08-31 12:57:44 +02:00
|
|
|
objs = [(self.data_tree.get_value(iter,6), self.data_tree.get_value(iter,2))
|
|
|
|
for iter in objs]
|
2006-05-09 14:22:50 +02:00
|
|
|
objs.sort()
|
|
|
|
objs = [obj for timestamp, obj in objs]
|
|
|
|
# order dataset
|
|
|
|
|
2006-05-03 13:11:45 +02:00
|
|
|
if objs and isinstance(objs[0], dataset.Dataset):
|
2006-04-21 16:59:25 +02:00
|
|
|
logger.log('debug', 'Selecting dataset')
|
2006-05-03 13:11:45 +02:00
|
|
|
self.project.set_current_data(objs)
|
2006-05-03 13:52:54 +02:00
|
|
|
else:
|
|
|
|
logger.log('debug', 'Deselecting dataset')
|
|
|
|
self.project.set_current_data([])
|
2006-05-03 13:11:45 +02:00
|
|
|
|
|
|
|
|
2006-04-27 17:51:25 +02:00
|
|
|
# TreeView changed. Set correct focus and colours
|
2006-04-27 16:15:13 +02:00
|
|
|
def row_changed_handler(self, treestore, pos, iter):
|
2006-04-27 17:51:25 +02:00
|
|
|
obj = treestore.get_value(iter,2)
|
|
|
|
type_= treestore.get_value(iter,1)
|
|
|
|
|
|
|
|
if not (treestore.get_value(iter,2) or treestore.get_value(iter,1)):
|
2006-04-27 16:15:13 +02:00
|
|
|
return
|
|
|
|
self.expand_to_path(pos)
|
|
|
|
|
2006-04-27 17:51:25 +02:00
|
|
|
if isinstance(obj,dataset.Dataset):
|
2006-04-27 16:15:13 +02:00
|
|
|
self.set_cursor(pos)
|
2006-05-03 13:11:45 +02:00
|
|
|
self.grab_focus()
|
|
|
|
|
2006-04-21 17:02:13 +02:00
|
|
|
def display_data_info(self, data):
|
2006-08-08 12:34:15 +02:00
|
|
|
dims = zip(data.get_dim_name(), data.shape)
|
2006-04-21 17:02:13 +02:00
|
|
|
|
|
|
|
dim_text = ", ".join(["%s (%d)" % dim for dim in dims])
|
|
|
|
|
|
|
|
text = """<span weight="bold">Data:</span> %s
|
|
|
|
|
|
|
|
<span weight="bold">Dimensions:</span> %s""" % (data.get_name(), dim_text)
|
|
|
|
|
|
|
|
d = gtk.MessageDialog(flags=(gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT),
|
|
|
|
buttons=gtk.BUTTONS_OK)
|
|
|
|
d.set_markup(text)
|
|
|
|
d.set_default_response(gtk.BUTTONS_OK)
|
|
|
|
d.run()
|
|
|
|
d.destroy()
|
2006-08-28 14:06:05 +02:00
|
|
|
|
2006-04-21 16:59:25 +02:00
|
|
|
def row_activated_handler(self, widget, path, column):
|
|
|
|
tree_iter = self.data_tree.get_iter(path)
|
|
|
|
obj = self.data_tree.get_value(tree_iter, 2)
|
|
|
|
|
2006-04-21 11:23:05 +02:00
|
|
|
if isinstance(obj, plots.Plot):
|
|
|
|
logger.log('debug', 'Activating plot')
|
|
|
|
self.app.change_plot(obj)
|
|
|
|
elif isinstance(obj, dataset.Dataset):
|
2006-04-21 17:02:13 +02:00
|
|
|
self.display_data_info(obj)
|
2006-08-31 12:57:44 +02:00
|
|
|
elif obj == None:
|
|
|
|
children = []
|
|
|
|
i = self.data_tree.iter_children(tree_iter)
|
|
|
|
while i:
|
|
|
|
child = self.data_tree.get(i, 2)[0]
|
|
|
|
if isinstance(child, plots.Plot):
|
|
|
|
children.append(child)
|
|
|
|
i = self.data_tree.iter_next(i)
|
|
|
|
self.app.change_plots(children)
|
2006-04-21 11:23:05 +02:00
|
|
|
else:
|
|
|
|
t = type(obj)
|
2006-04-21 16:59:25 +02:00
|
|
|
logger.log('debug', 'Activated datatype was %s. Don\'t know what to do.' % t)
|
2006-08-07 16:14:42 +02:00
|
|
|
|
2007-01-12 15:37:44 +01:00
|
|
|
def popup_menu(self, *rest):
|
|
|
|
self.menu.popup(None, None, None, 0, 0)
|
|
|
|
|
2007-01-15 14:58:56 +01:00
|
|
|
def on_mouse_event(self, widget, event):
|
|
|
|
path = widget.get_path_at_pos(int(event.x), int(event.y))
|
2007-01-17 15:06:07 +01:00
|
|
|
iter = None
|
2007-01-15 14:58:56 +01:00
|
|
|
|
|
|
|
if path:
|
|
|
|
iter = self.data_tree.get_iter(path[0])
|
|
|
|
obj = self.data_tree.get_value(iter, 2)
|
|
|
|
else:
|
|
|
|
obj = None
|
|
|
|
|
|
|
|
if isinstance(obj, dataset.Dataset):
|
2007-01-17 15:06:07 +01:00
|
|
|
self.menu.set_dataset(obj, iter)
|
2007-01-15 14:58:56 +01:00
|
|
|
else:
|
2007-01-17 15:06:07 +01:00
|
|
|
self.menu.set_dataset(None, iter)
|
2007-01-15 14:58:56 +01:00
|
|
|
|
2007-01-12 15:37:44 +01:00
|
|
|
if event.button == 3:
|
|
|
|
self.menu.popup(None, None, None, event.button, event.time)
|
|
|
|
|
|
|
|
|
|
|
|
class NavigatorMenu(gtk.Menu):
|
|
|
|
def __init__(self, navigator):
|
|
|
|
gtk.Menu.__init__(self)
|
|
|
|
self.navigaotor = navigator
|
2007-01-15 14:58:56 +01:00
|
|
|
self.dataset = None
|
2007-01-17 15:06:07 +01:00
|
|
|
self.tree_iter = None
|
|
|
|
|
2007-01-12 15:37:44 +01:00
|
|
|
# Populate menu
|
|
|
|
self.load_item = gtk.MenuItem('Load dataset')
|
2007-01-15 14:58:56 +01:00
|
|
|
self.load_item.connect('activate', self.on_load_dataset, navigator)
|
2007-01-12 15:37:44 +01:00
|
|
|
self.append(self.load_item)
|
|
|
|
self.load_item.show()
|
|
|
|
|
|
|
|
self.save_item = gtk.MenuItem('Save dataset')
|
2007-01-15 14:58:56 +01:00
|
|
|
self.save_item.connect('activate', self.on_save_dataset, navigator)
|
2007-01-12 15:37:44 +01:00
|
|
|
self.append(self.save_item)
|
2007-01-15 14:58:56 +01:00
|
|
|
self.save_item.show()
|
2007-01-12 15:37:44 +01:00
|
|
|
|
2007-01-17 15:06:07 +01:00
|
|
|
# Build plot menu
|
|
|
|
self.plot_menu = gtk.Menu()
|
|
|
|
|
|
|
|
self.plot_image_item = gtk.MenuItem('Image Plot')
|
|
|
|
self.plot_image_item.connect('activate', self.on_plot_image, navigator)
|
|
|
|
self.plot_menu.append(self.plot_image_item)
|
|
|
|
self.plot_image_item.show()
|
|
|
|
|
|
|
|
self.plot_item = gtk.MenuItem('Plot')
|
|
|
|
self.append(self.plot_item)
|
|
|
|
self.plot_item.set_submenu(self.plot_menu)
|
|
|
|
self.plot_item.show()
|
|
|
|
|
|
|
|
def set_dataset(self, ds, it):
|
2007-01-15 14:58:56 +01:00
|
|
|
self.dataset = ds
|
2007-01-17 15:06:07 +01:00
|
|
|
self.tree_iter = it
|
|
|
|
|
2007-01-15 14:58:56 +01:00
|
|
|
if ds == None:
|
|
|
|
self.save_item.set_property('sensitive', False)
|
2007-01-17 15:06:07 +01:00
|
|
|
self.plot_item.set_property('sensitive', False)
|
2007-01-12 15:37:44 +01:00
|
|
|
else:
|
2007-01-15 14:58:56 +01:00
|
|
|
self.save_item.set_property('sensitive', True)
|
2007-01-17 15:06:07 +01:00
|
|
|
self.plot_item.set_property('sensitive', True)
|
2007-01-12 15:37:44 +01:00
|
|
|
|
2007-01-15 14:58:56 +01:00
|
|
|
def on_load_dataset(self, item, navigator):
|
|
|
|
dialog = gtk.FileChooserDialog('Load dataset')
|
2007-01-16 13:28:56 +01:00
|
|
|
dialog.set_action(gtk.FILE_CHOOSER_ACTION_OPEN)
|
2007-01-15 14:58:56 +01:00
|
|
|
dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)
|
2007-01-16 13:28:56 +01:00
|
|
|
dialog.set_select_multiple(True)
|
2007-01-15 14:58:56 +01:00
|
|
|
retval = dialog.run()
|
|
|
|
if retval in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT]:
|
|
|
|
pass
|
|
|
|
elif retval == gtk.RESPONSE_OK:
|
2007-01-16 13:28:56 +01:00
|
|
|
for filename in dialog.get_filenames():
|
|
|
|
fd = open(filename)
|
|
|
|
ds = dataset.read_ftsv(fd)
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
if isinstance(ds, dataset.GraphDataset):
|
|
|
|
icon = fluents.icon_factory.get("graph_dataset")
|
|
|
|
elif isinstance(ds, dataset.CategoryDataset):
|
|
|
|
icon = fluents.icon_factory.get("category_dataset")
|
|
|
|
else:
|
|
|
|
icon = fluents.icon_factory.get("dataset")
|
|
|
|
|
|
|
|
project = navigator.project
|
|
|
|
project.add_dataset(ds)
|
|
|
|
project.data_tree_insert(None, ds.get_name(), ds, None, "black", icon)
|
2007-01-15 14:58:56 +01:00
|
|
|
else:
|
|
|
|
print "unknown; ", retval
|
|
|
|
dialog.destroy()
|
|
|
|
|
|
|
|
def on_save_dataset(self, item, navigator):
|
|
|
|
dialog = gtk.FileChooserDialog('Save dataset')
|
2007-01-16 13:28:56 +01:00
|
|
|
dialog.set_action(gtk.FILE_CHOOSER_ACTION_SAVE)
|
2007-01-15 14:58:56 +01:00
|
|
|
dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK)
|
2007-01-16 13:28:56 +01:00
|
|
|
dialog.set_current_name("%s.ftsv" % self.dataset.get_name())
|
2007-01-15 14:58:56 +01:00
|
|
|
retval = dialog.run()
|
|
|
|
if retval in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT]:
|
2007-01-16 13:28:56 +01:00
|
|
|
logger.log("debug", "Cancelled save dataset")
|
2007-01-15 14:58:56 +01:00
|
|
|
elif retval == gtk.RESPONSE_OK:
|
2007-01-16 13:28:56 +01:00
|
|
|
logger.log("debug", "Saving dataset as: %s" % dialog.get_filename())
|
2007-01-15 14:58:56 +01:00
|
|
|
fd = open(dialog.get_filename(), 'w')
|
|
|
|
dataset.write_ftsv(fd, self.dataset)
|
|
|
|
fd.close()
|
|
|
|
else:
|
|
|
|
print "unknown; ", retval
|
|
|
|
dialog.destroy()
|
2007-01-12 15:37:44 +01:00
|
|
|
|
2007-01-17 15:06:07 +01:00
|
|
|
def on_plot_image(self, item, navigator):
|
|
|
|
project = navigator.project
|
|
|
|
plot = plots.ImagePlot(self.dataset, name='Image Plot')
|
|
|
|
icon = fluents.icon_factory.get("line_plot")
|
|
|
|
project.data_tree_insert(self.tree_iter, 'Image Plot', plot, None, "black", icon)
|
|
|
|
|
|
|
|
|