I would like some advice about how one is supposed to edit a GTK TreeListModel.
Problem statement
I want to create a UI for a drag-and-drop list of layers, like one has in e.g. GIMP or Inkscape. Layers can be put into groups and then one can drag the whole group.
My approach
I am using TreeListModel to store the layers. I have a custom object whose private data is:
- for a layer, a unique identifier for that layer together with some additional properties, such as a name (to display in the UI)
- for a group, a unique identifier, together with its children (if any)
The recursive structure of this data (with a group containing all its direct children, and thus recursively all descendants) means I can create a GTK TreeListModel, with the child model for a group being a simple GIO ListModel.
Now, the difficulty comes after a drag and drop operation has happened and I need to move data around in the model. It isn’t clear at all to me how to proceed.
Suppose for example I have the following layer structure
- G1
- G2
- L3
- L4
- G2
- G5
- L6
If I want to move L3 out of its parent groups and say put it inside G5 below L6, it seems I have to edit the list model in many places:
- I have to remove the 3rd list item (L3), and put it back elsewhere (after L6). So far, so good.
- I also have to edit the parent of L3, G2, because the private data for object G2 refers to L3, when L3 will no longer be one of its children.
- I ALSO have to edit the parent of G2, G1, because as noted above its private data stored all descendants, while I’m removing L3 (and moving it elsewhere).
- Conversely, I also need to update G5 to set its private data to add L3 as its second child.
This seems like I am going round the houses editing this list model, which leads me to believe I am going about this wrong and overcomplicating things.
Another issue is if I want to move a whole group around, say G2. I don’t think I can easily move the whole thing in one operation, I have to do a ListStore splice operation where I remove all the items and put them back elsewhere. This again suggests I’m going about this wrong, as I would naively think it should be possible to move an entire group with its descendants in one go, but perhaps that requires an approach where ListModels are nested inside other ListModels?
Summary
Can someone please advise how I should set up the ListModel for tree-like data like the layers and groups I am dealing with above, in such a way which facilitates updating the list model when data gets moved around?