Prevent Treeview selection loss on drag and drop
I have a gtk.Treeview
setup as a drag source:
self.drag_source_set(gtk.gdk.BUTTON1_MASK, targets, gtk.gdk.ACTION_COPY)
and it's TreeSelection
is set to SELECT_MULTIPLE
.
But everytime I try to drag a multi row selection, the cursor jumps to the current mouse position, resetting the selection to the current row. Even though the mouse is above one of the selected rows. It only works when I hold down the Shift or Ctrl button down.
What is going on?
Edit 1:
I have set up a bare-bones treeview to rule out any bugs in my code and it does the same thing.
Edit 2:
I found a开发者_Python百科 code snippet that does what I want. It's from the quod libet sources, called MultiDragTreeView
.
I found a code snippet that does what I want. It's from the quod libet sources, called 'MultiDragTreeView'. Quoting the docstring:
"""TreeView with multirow drag support:
* Selections don't change until button-release-event...
* Unless they're a Shift/Ctrl modification, then they happen immediately
* Drag icons include 3 rows/2 plus a "and more" count"""
I found a workaround to this, if item is selected, remove the item from selection and add control mask to the event.:
def on_iconview_button_press_event(widget, event):
if event.type != Gdk.EventType.BUTTON_PRESS or event.button != 1:
return
if (event.state & Gdk.ModifierType.CONTROL_MASK):#do no changes if ctrl is pressed
return
path = widget.get_path_at_pos(event.x, event.y)
if not path:
return
if widget.path_is_selected(path):
widget.unselect_path(path)
event.state |= Gdk.ModifierType.CONTROL_MASK
return
iconview.connect('button-press-event', on_iconview_button_press_event)
精彩评论