Projects/laydi
Projects
/
laydi
Archived
7
0
Fork 0
This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
laydi/fluents/navigator.py

251 lines
9.1 KiB
Python

import gtk
import gobject
import plots
import time
import fluents
import dataset, logger, plots, project, workflow
class NavigatorView (gtk.TreeView):
def __init__(self, project, app):
self.project = project
self.app = app
if project:
self.data_tree = project.data_tree
else:
self.data_tree = None
gtk.TreeView.__init__(self)
# various properties
self.set_headers_visible(False)
self.get_hadjustment().set_value(0)
self.set_enable_tree_lines(True)
# Selection Mode
self.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
self.get_selection().set_select_function(self.is_selectable)
self.get_selection().connect('changed',self.selection_changed_handler)
self._previous_selection = []
# Setting up TextRenderers etc
# self.connect('cursor_changed', self.cursor_changed_handler)
self.connect('row_activated', self.row_activated_handler)
# Activate context menu
self.menu = NavigatorMenu(self)
self.connect('popup_menu', self.popup_menu)
self.connect('button_press_event', self.on_mouse_event)
self.textrenderer = textrenderer = gtk.CellRendererText()
pixbufrenderer = gtk.CellRendererPixbuf()
self.object_col = gtk.TreeViewColumn('Object')
self.object_col.pack_start(pixbufrenderer,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(pixbufrenderer, pixbuf=5)
self.append_column(self.object_col)
# send events to plots / itself
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)
self.connect("drag-data-get",self.slot_drag_data)
logger.log('debug', 'Initializing navigator window.')
# sets data for drag event.
def slot_drag_data(self,treeview,context,selection,target_id,etime):
treeselection = treeview.get_selection()
model, paths = treeselection.get_selected_rows()
if paths:
self.data_tree.drag_data_get(paths[0], selection)
def add_project(self, project):
self.project = project
self.data_tree = project.data_tree
self.set_model(project.data_tree)
self.data_tree.connect('row-changed',self.row_changed_handler)
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
def selection_changed_handler(self, selection):
# update prev selection right away in case of multiple events
model, paths = selection.get_selected_rows()
if not paths: # a plot is marked: do nothing
return
tmp = self._previous_selection
self._previous_selection = paths
# set timestamp on newly selected objects
[self.data_tree.set_value(self.data_tree.get_iter(path), 6, time.time())
for path in paths if path not in tmp]
objs = [self.data_tree.get_iter(path) for path in paths]
objs = [(self.data_tree.get_value(iter,6), self.data_tree.get_value(iter,2))
for iter in objs]
objs.sort()
objs = [obj for timestamp, obj in objs]
# order dataset
if objs and isinstance(objs[0], dataset.Dataset):
logger.log('debug', 'Selecting dataset')
self.project.set_current_data(objs)
else:
logger.log('debug', 'Deselecting dataset')
self.project.set_current_data([])
# TreeView changed. Set correct focus and colours
def row_changed_handler(self, treestore, pos, iter):
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)):
return
self.expand_to_path(pos)
if isinstance(obj,dataset.Dataset):
self.set_cursor(pos)
self.grab_focus()
def display_data_info(self, data):
dims = zip(data.get_dim_name(), data.shape)
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()
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)
if isinstance(obj, plots.Plot):
logger.log('debug', 'Activating plot')
self.app.change_plot(obj)
elif isinstance(obj, dataset.Dataset):
self.display_data_info(obj)
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)
else:
t = type(obj)
logger.log('debug', 'Activated datatype was %s. Don\'t know what to do.' % t)
def popup_menu(self, *rest):
self.menu.popup(None, None, None, 0, 0)
def on_mouse_event(self, widget, event):
path = widget.get_path_at_pos(int(event.x), int(event.y))
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):
self.menu.set_dataset(obj)
else:
self.menu.set_dataset(None)
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
self.dataset = None
# Populate menu
self.load_item = gtk.MenuItem('Load dataset')
self.load_item.connect('activate', self.on_load_dataset, navigator)
self.append(self.load_item)
self.load_item.show()
self.save_item = gtk.MenuItem('Save dataset')
self.save_item.connect('activate', self.on_save_dataset, navigator)
self.append(self.save_item)
self.save_item.show()
def set_dataset(self, ds):
self.dataset = ds
if ds == None:
self.save_item.set_property('sensitive', False)
else:
self.save_item.set_property('sensitive', True)
def on_load_dataset(self, item, navigator):
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)
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):
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)
else:
print "unknown; ", retval
dialog.destroy()
def on_save_dataset(self, item, navigator):
dialog = gtk.FileChooserDialog('Save dataset')
dialog.set_action(gtk.FILE_CHOOSER_ACTION_SAVE)
dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK)
dialog.set_current_name("%s.ftsv" % self.dataset.get_name())
retval = dialog.run()
if retval in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT]:
logger.log("debug", "Cancelled save dataset")
elif retval == gtk.RESPONSE_OK:
logger.log("debug", "Saving dataset as: %s" % dialog.get_filename())
fd = open(dialog.get_filename(), 'w')
dataset.write_ftsv(fd, self.dataset)
fd.close()
else:
print "unknown; ", retval
dialog.destroy()