How to have drag-and-drop and sorted GtkTreeView in GTK3?

后端 未结 2 574
执念已碎
执念已碎 2021-02-13 10:13

I am porting liblarch, a library for handling directed acyclic graphs, from PyGTK (GTK2) to PyGObject introspection (GTK3). I ran into the problem with GtkTreeView.

The

2条回答
  •  被撕碎了的回忆
    2021-02-13 10:35

    GTG is a great piece of software! But it's far too slow, at least on my computer. So I've been writing a C++ library which displays a directed acyclic graph using a Gtk::TreeView, and I looked at the LibLarch source code a lot.

    As far as I know, Python and C++ bindings of GTK share the same limitation, coming from GTK itself (I once looked at GTK source code to find exactly why it works like that): If you turn of drag-and-drop and sorting, drag-and-drop won't work. I offer three things you can do about it:

    1. Make a patch to GTK which limits dnd when sorting is enabled, instead of completely blocking it

    2. Implement sorting by yourself. It easy: start by loading your data to a sorted treeview. Now, every time the user drags and drops, move the dragged row to the new position using your sorting function. But leave GTK sorting off.

    3. This can be done in addition to 2, it's a GUI design issue: In GtkTreeView you can insert an item between to sibling items, which doesn't make much sense in sorted trees. In terms of UI it's better to allow dropping only ON rows, not BETWEEN them. Example: Nautilus list-view works like this. The solution is either override the TreeView drag_data_received() default handler, or better than that in terms of maintainability: send your model a hint from the view telling the model whether the drop position is ON or BEFORE. If the position is BEFORE, make your tree's drop_possible() virtual override return false, and then you don't see the treeview's "you can drop here" didplay", thus you get a cleaner GUI.

    2 and 3 is what I do in C++, you should be able to do that in Python easily :)

    Also, a note regarding option 1: GtktreeView (or was is GtkTreeStore? I forgot) simply blocks any drop if sorting is enabled. If someone just fixes that (you... or me...), or at least writes a derived view class, we'll have a default clean GUI for sorted trees with dnd support.

提交回复
热议问题