Added "Save dataset" option to navigator context menu.
This commit is contained in:
parent
476c830a5e
commit
cacb0a27b7
|
@ -35,7 +35,7 @@ class NavigatorView (gtk.TreeView):
|
|||
# Activate context menu
|
||||
self.menu = NavigatorMenu(self)
|
||||
self.connect('popup_menu', self.popup_menu)
|
||||
self.connect('button_press_event', self.mouse_popup_menu)
|
||||
self.connect('button_press_event', self.on_mouse_event)
|
||||
|
||||
self.textrenderer = textrenderer = gtk.CellRendererText()
|
||||
pixbufrenderer = gtk.CellRendererPixbuf()
|
||||
|
@ -160,7 +160,20 @@ class NavigatorView (gtk.TreeView):
|
|||
def popup_menu(self, *rest):
|
||||
self.menu.popup(None, None, None, 0, 0)
|
||||
|
||||
def mouse_popup_menu(self, widget, event):
|
||||
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)
|
||||
|
||||
|
@ -169,45 +182,65 @@ 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', on_load_dataset, navigator)
|
||||
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', on_save_dataset, navigator)
|
||||
self.save_item.connect('activate', self.on_save_dataset, navigator)
|
||||
self.append(self.save_item)
|
||||
self.save_item.show()
|
||||
|
||||
|
||||
def on_load_dataset(item, navigator):
|
||||
dialog = gtk.FileChooserDialog('Load dataset')
|
||||
dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)
|
||||
retval = dialog.run()
|
||||
if retval in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT]:
|
||||
print "Cancelled"
|
||||
elif retval == gtk.RESPONSE_OK:
|
||||
print "OK"
|
||||
print dialog.get_filename()
|
||||
fd = open(dialog.get_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")
|
||||
def set_dataset(self, ds):
|
||||
self.dataset = ds
|
||||
if ds == None:
|
||||
self.save_item.set_property('sensitive', False)
|
||||
else:
|
||||
icon = fluents.icon_factory.get("dataset")
|
||||
self.save_item.set_property('sensitive', True)
|
||||
|
||||
|
||||
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_load_dataset(self, item, navigator):
|
||||
dialog = gtk.FileChooserDialog('Load dataset')
|
||||
dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)
|
||||
retval = dialog.run()
|
||||
if retval in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT]:
|
||||
pass
|
||||
elif retval == gtk.RESPONSE_OK:
|
||||
fd = open(dialog.get_filename())
|
||||
ds = dataset.read_ftsv(fd)
|
||||
fd.close()
|
||||
|
||||
def on_save_dataset(item):
|
||||
pass
|
||||
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.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK)
|
||||
retval = dialog.run()
|
||||
if retval in [gtk.RESPONSE_CANCEL, gtk.RESPONSE_DELETE_EVENT]:
|
||||
print "Cancelled"
|
||||
elif retval == gtk.RESPONSE_OK:
|
||||
print "OK"
|
||||
print dialog.get_filename()
|
||||
fd = open(dialog.get_filename(), 'w')
|
||||
dataset.write_ftsv(fd, self.dataset)
|
||||
fd.close()
|
||||
else:
|
||||
print "unknown; ", retval
|
||||
dialog.destroy()
|
||||
|
||||
|
|
Reference in New Issue