I have a simple Gtk.TreeView with a Gtk.ListStore with four columns as the model. I want to be able to drag and drop rows within the TreeView and track row insertions, changes and deletions as they happen so I can implement undo/redo to drag and drop operations. I'm using PyGObject 3 and Python 3.2.
I thought that using methods under the Gtk.TreeDragSource and Gtk.TreeDragDest interfaces would suit my needs perfectly, with Gtk.TreeDragSource.drag_data_get() in my drag_data_get handler and Gtk.TreeDragDest.drag_data_received() or Gtk.tree_get_row_drag_data() in my drag_data_received handler. Basically, what I've tried looks something like this:
def drag_data_get(self, tv, context, selection, target_id, time):
treeselection = tv.get_selection()
model, my_iter = treeselection.get_selected()
path = model.get_path(my_iter)
result = Gtk.TreeDragSource.drag_data_get(path, selection)
def drag_data_received(self, tv, context, x, y, selection, info, time):
result, model, row = Gtk.tree_get_row_drag_data(selection)
my_iter = model.get_iter(row)
data = [model.get_value(my_iter, i) for i in range(model.get_n_columns())]
drop_info = tv.get_dest_row_at_pos(x, y)
if drop_info:
path, position = drop_info
my_iter = model.get_iter(path)
if (position == Gtk.TreeViewDropPosition.BEFORE
or position == Gtk.TreeViewDropPosition.INTO_OR_BEFORE):
model.insert_before(my_iter, [data])
else:
model.insert_after(my_iter, [data])
else:
model.append([data])
if context.get_actions() == Gdk.DragAction.MOVE|Gdk.DragAction.DEFAULT:
context.finish(True, True, time)
return
This fails spectacularly - when Python hits the call to Gtk.TreeDragSource.drag_data_get(), Python crashes and my program window swiftly disappears. I don't even get to the drag_data_received handler. Can anyone point me to some example code showing how these methods using the TreeDragSource and TreeDragDest interfaces work? Any help much appreciated!
Would it not work to use Gtk.tree_set_row_drag_data(selection, model, path)
instead of result = Gtk.TreeDragSource.drag_data_get(path, selection)
since your using Gtk.tree_get_row_drag_data(selection)
anyway?