2006-08-08 10:48:55 +02:00
|
|
|
|
2006-10-17 16:42:27 +02:00
|
|
|
import logger, dataset
|
2007-02-15 23:57:40 +01:00
|
|
|
import annotations
|
2006-08-08 10:48:55 +02:00
|
|
|
|
|
|
|
import pygtk
|
|
|
|
import gtk
|
|
|
|
import gtk.gdk
|
|
|
|
import gtk.glade
|
|
|
|
import gnome
|
|
|
|
import gnome.ui
|
|
|
|
import gobject
|
|
|
|
|
2006-08-30 01:57:21 +02:00
|
|
|
class SimpleMenu(gtk.Menu):
|
|
|
|
def __init__(self):
|
|
|
|
gtk.Menu.__init__(self)
|
|
|
|
|
|
|
|
def add_simple_item(self, title, function):
|
|
|
|
item = gtk.MenuItem(title)
|
|
|
|
item.connect('activate', function)
|
|
|
|
self.append(item)
|
|
|
|
item.show()
|
|
|
|
|
2006-09-08 20:25:03 +02:00
|
|
|
|
|
|
|
class DimListController:
|
|
|
|
def __init__(self, dimlist, seltree, idlist):
|
|
|
|
|
|
|
|
self._current_dim = None
|
|
|
|
|
|
|
|
## dimstore is a list of all dimensions in the application
|
|
|
|
self.dimstore = gtk.ListStore(gobject.TYPE_STRING)
|
|
|
|
|
|
|
|
## Two types of lines, one for CategoryDatasets and one for
|
|
|
|
## Selections. The elements are title, link to dataset or selection
|
|
|
|
## and the name of the dimension.
|
|
|
|
self.selstore = gtk.TreeStore(gobject.TYPE_STRING,
|
|
|
|
gobject.TYPE_PYOBJECT,
|
|
|
|
gobject.TYPE_STRING)
|
|
|
|
self.selstore_filter = self.selstore.filter_new()
|
|
|
|
self.selstore_filter.set_visible_func(self.dimension_filter)
|
|
|
|
|
|
|
|
## idstore is a list of currently selected identifiers
|
|
|
|
## the list is cleared and rebuilt each time a new selection is
|
|
|
|
## focused.
|
|
|
|
self.idstore = gtk.ListStore(gobject.TYPE_STRING)
|
|
|
|
|
|
|
|
## The widgets we are controlling
|
|
|
|
self.dimlist = dimlist
|
|
|
|
self.seltree = seltree
|
|
|
|
self.idlist = idlist
|
|
|
|
|
|
|
|
## Set up dimensions list
|
|
|
|
dimlist.set_model(self.dimstore)
|
|
|
|
|
|
|
|
renderer = gtk.CellRendererText()
|
|
|
|
dim_column = gtk.TreeViewColumn('Dimension', renderer, text=0)
|
|
|
|
dimlist.insert_column(dim_column, 0)
|
|
|
|
|
|
|
|
dimlist.connect('row-activated', self._dim_row_activated)
|
|
|
|
dimlist.connect('cursor-changed', self._dim_cursor_changed)
|
|
|
|
|
|
|
|
## Set up selection tree
|
|
|
|
seltree.set_model(self.selstore_filter)
|
|
|
|
|
|
|
|
renderer = gtk.CellRendererText()
|
2006-09-15 16:17:17 +02:00
|
|
|
sel_column = gtk.TreeViewColumn('Selection', renderer, text=0)
|
2006-09-08 20:25:03 +02:00
|
|
|
seltree.insert_column(sel_column, 0)
|
|
|
|
|
|
|
|
seltree.connect('row-activated', self._sel_row_activated)
|
|
|
|
seltree.connect('cursor-changed', self._sel_cursor_changed)
|
|
|
|
|
|
|
|
seltree.drag_dest_set(gtk.DEST_DEFAULT_ALL,
|
|
|
|
[("GTK_TREE_MODEL_ROW", gtk.TARGET_SAME_APP, 7)],
|
|
|
|
gtk.gdk.ACTION_LINK)
|
2006-09-08 21:42:32 +02:00
|
|
|
|
|
|
|
seltree.connect('drag-data-received', self._drag_data_received)
|
2006-09-08 20:25:03 +02:00
|
|
|
|
|
|
|
## Set up identifier list
|
|
|
|
idlist.set_model(self.idstore)
|
|
|
|
|
|
|
|
renderer = gtk.CellRendererText()
|
|
|
|
dim_column = gtk.TreeViewColumn('Identifiers', renderer, text=0)
|
|
|
|
idlist.insert_column(dim_column, 0)
|
2007-02-15 23:57:40 +01:00
|
|
|
idlist.connect('button-press-event', self._idlist_button_pressed)
|
|
|
|
|
|
|
|
## Set up identifier list context menu
|
|
|
|
self._idlist_menu = SimpleMenu()
|
|
|
|
self._idlist_menu.add_simple_item('Import...', self._on_idlist_import)
|
|
|
|
self._idlist_menu.add_simple_item('Export...', self._on_idlist_export)
|
|
|
|
|
|
|
|
i = gtk.MenuItem('Show annotations')
|
|
|
|
self._idlist_menu.append(i)
|
|
|
|
i.show()
|
|
|
|
self._idlist_menu_ann = i
|
2006-09-08 20:25:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_dataset_iter(self, dataset):
|
|
|
|
"""Returns the iterator to the selection tree row containing a
|
|
|
|
given dataset."""
|
|
|
|
i = self.selstore.get_iter_first()
|
|
|
|
while i:
|
|
|
|
if self.selstore.get_value(i, 1) == dataset:
|
|
|
|
return i
|
|
|
|
i = self.selstore.iter_next(i)
|
|
|
|
return None
|
2006-09-08 21:42:32 +02:00
|
|
|
|
|
|
|
def get_current_selection_iter(self, selection, dimension):
|
|
|
|
i = self.selstore.get_iter_first()
|
|
|
|
while i:
|
|
|
|
if self.selstore.get_value(i, 1) == selection:
|
|
|
|
if self.selstore.get_value(i, 2) == dimension:
|
|
|
|
return i
|
|
|
|
i = self.selstore.iter_next(i)
|
|
|
|
return None
|
|
|
|
|
2006-09-08 20:25:03 +02:00
|
|
|
def set_project(self, project):
|
|
|
|
"""Dependency injection."""
|
2006-09-08 21:42:32 +02:00
|
|
|
self.project = project
|
2006-09-08 20:25:03 +02:00
|
|
|
self.dim_names = project.dim_names
|
|
|
|
self.update_dims()
|
|
|
|
project.add_selection_observer(self)
|
|
|
|
project.add_dataset_observer(self)
|
|
|
|
|
|
|
|
def get_dimension(self, dim):
|
|
|
|
"""Returns the iterator to the dimension with the given name, or
|
|
|
|
None if not found."""
|
|
|
|
|
|
|
|
i = self.dimstore.get_iter_first()
|
|
|
|
while i:
|
|
|
|
if self.dimstore.get_value(i, 0) == dim:
|
|
|
|
return i
|
|
|
|
i = self.dimstore.iter_next(i)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def dimension_filter(self, store, row):
|
|
|
|
"""Filters out everything but the selected dimension."""
|
|
|
|
row_dim = store.get_value(row, 2)
|
|
|
|
return row_dim == self._current_dim
|
|
|
|
|
|
|
|
def update_dims(self):
|
|
|
|
"""Update the list of dimensions shown"""
|
|
|
|
for dim in self.dim_names:
|
|
|
|
if not self.get_dimension(dim):
|
|
|
|
self.dimstore.insert_after(None, (dim,))
|
|
|
|
|
|
|
|
def add_dataset(self, dataset):
|
|
|
|
di = self.get_dataset_iter(dataset)
|
|
|
|
if not di:
|
|
|
|
values = (dataset.get_name(), dataset, dataset.get_dim_name(0))
|
|
|
|
i = self.selstore.insert_after(None, None, values)
|
|
|
|
for selection in dataset.as_selections():
|
|
|
|
values = (selection.title, selection, dataset.get_dim_name(0))
|
|
|
|
self.selstore.insert_after(i, None, values)
|
|
|
|
|
2006-10-12 19:38:41 +02:00
|
|
|
def selection_changed(self, dim_name, selection):
|
2006-09-08 20:25:03 +02:00
|
|
|
"""Callback function from Project."""
|
2006-09-08 21:42:32 +02:00
|
|
|
|
|
|
|
for dim in selection.dims():
|
|
|
|
if not self.get_current_selection_iter(selection, dim):
|
|
|
|
values = (selection.title, selection, dim)
|
|
|
|
self.selstore.insert_after(None, None, values)
|
|
|
|
|
2006-09-08 20:25:03 +02:00
|
|
|
self.update_dims()
|
|
|
|
|
|
|
|
def dataset_changed(self):
|
|
|
|
"""Callback function from Project."""
|
|
|
|
self.update_dims()
|
|
|
|
|
|
|
|
## GTK Callbacks.
|
|
|
|
def _dim_cursor_changed(self, widget):
|
|
|
|
cursor = self.dimlist.get_cursor()[0]
|
|
|
|
i = self.dimstore.get_iter(cursor)
|
|
|
|
row = self.dimstore.get_value(i, 0)
|
|
|
|
self.set_dimension(row)
|
|
|
|
|
|
|
|
def _dim_row_activated(self, widget, path, column):
|
|
|
|
self.seltree.grab_focus()
|
|
|
|
|
|
|
|
def _sel_cursor_changed(self, widget):
|
|
|
|
"Show the list of identifier strings."
|
|
|
|
p = self.seltree.get_cursor()[0]
|
2006-09-08 21:42:32 +02:00
|
|
|
p = self.selstore_filter.convert_path_to_child_path(p)
|
2006-09-08 20:25:03 +02:00
|
|
|
i = self.selstore.get_iter(p)
|
|
|
|
obj = self.selstore.get_value(i, 1)
|
|
|
|
|
|
|
|
id_list = []
|
|
|
|
if isinstance(obj, dataset.Selection):
|
2006-09-08 21:42:32 +02:00
|
|
|
id_list = list(obj[self._current_dim])
|
2006-09-08 20:25:03 +02:00
|
|
|
id_list.sort()
|
|
|
|
|
|
|
|
self.idstore.clear()
|
|
|
|
for e in id_list:
|
|
|
|
self.idstore.append((e,))
|
|
|
|
|
|
|
|
def _sel_row_activated(self, widget, path, column):
|
2006-09-08 21:42:32 +02:00
|
|
|
path = self.selstore_filter.convert_path_to_child_path(path)
|
|
|
|
i = self.selstore.get_iter(path)
|
|
|
|
obj = self.selstore.get_value(i, 1)
|
|
|
|
if isinstance(obj, dataset.Dataset):
|
|
|
|
seltree = self.seltree
|
|
|
|
if seltree.row_expanded(path):
|
|
|
|
seltree.collapse_row(path)
|
|
|
|
else:
|
|
|
|
seltree.expand_row(path, True)
|
|
|
|
elif isinstance(obj, dataset.Selection):
|
|
|
|
self.project.set_selection(self._current_dim,
|
|
|
|
obj[self._current_dim])
|
2006-09-08 20:25:03 +02:00
|
|
|
|
|
|
|
def set_dimension(self, dimname):
|
|
|
|
self._current_dim = dimname
|
|
|
|
self.selstore_filter.refilter()
|
|
|
|
|
|
|
|
dim = self.get_dimension(self._current_dim)
|
|
|
|
path = self.dimstore.get_path(dim)
|
|
|
|
|
|
|
|
if self.dimlist.get_cursor()[0] != path:
|
|
|
|
self.dimlist.set_cursor(self.dimstore.get_path(dim))
|
|
|
|
|
2006-09-08 21:42:32 +02:00
|
|
|
def _drag_data_received(self, widget, drag_context, x, y,
|
2006-09-08 20:25:03 +02:00
|
|
|
selection, info, timestamp):
|
|
|
|
|
|
|
|
treestore, path = selection.tree_get_row_drag_data()
|
|
|
|
i = treestore.get_iter(path)
|
|
|
|
obj = treestore.get_value(i, 2)
|
|
|
|
if isinstance(obj, dataset.CategoryDataset):
|
|
|
|
self.add_dataset(obj)
|
|
|
|
self.set_dimension(obj.get_dim_name(0))
|
2006-09-08 21:42:32 +02:00
|
|
|
widget.emit_stop_by_name('drag-data-received')
|
2007-02-15 23:57:40 +01:00
|
|
|
|
|
|
|
def _update_annotations_menu(self):
|
|
|
|
dim_h = annotations.get_dim_handler(self._current_dim)
|
|
|
|
if not dim_h:
|
|
|
|
print "No annotations etc"
|
|
|
|
self._idlist_menu_ann.set_sensitive(False)
|
|
|
|
else:
|
2007-02-16 12:23:29 +01:00
|
|
|
annotations_menu = gtk.Menu()
|
2007-02-15 23:57:40 +01:00
|
|
|
self._idlist_menu_ann.set_sensitive(True)
|
|
|
|
ann_names = annotations.get_dim_handler(self._current_dim).get_annotation_names()
|
|
|
|
for ann in ann_names:
|
2007-02-16 12:23:29 +01:00
|
|
|
item = gtk.MenuItem(ann)
|
|
|
|
item.connect('activate', self._on_annotation_activated, ann)
|
|
|
|
annotations_menu.append(item)
|
|
|
|
item.show()
|
|
|
|
|
2007-02-15 23:57:40 +01:00
|
|
|
self._idlist_menu_ann.set_submenu(annotations_menu)
|
|
|
|
|
|
|
|
def _idlist_button_pressed(self, widget, event):
|
|
|
|
if event.button == 3:
|
|
|
|
self._update_annotations_menu()
|
|
|
|
self._idlist_menu.popup(None, None, None, event.button, event.time)
|
|
|
|
|
|
|
|
def _popup_idlist_menu(self, *rest):
|
|
|
|
self._update_annotations_menu()
|
|
|
|
self._idlist_menu.popup(None, None, None, 0, 0)
|
|
|
|
|
2007-02-16 12:23:29 +01:00
|
|
|
def _on_annotation_activated(self, menuitem, annotation):
|
|
|
|
print "Congratulations. You chouse %s" % annotation
|
|
|
|
|
2007-02-15 23:57:40 +01:00
|
|
|
def _on_idlist_export(self, menuitem):
|
|
|
|
print "export stuff"
|
|
|
|
|
|
|
|
def _on_idlist_import(self, menuitem):
|
2007-02-16 12:23:29 +01:00
|
|
|
dialog = gtk.FileChooserDialog('Load annotations')
|
|
|
|
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():
|
|
|
|
annotations.read_annotations_file(filename)
|
|
|
|
else:
|
|
|
|
print "unknown; ", retval
|
|
|
|
dialog.destroy()
|
|
|
|
|
2007-02-15 23:57:40 +01:00
|
|
|
pass
|
|
|
|
|