[Implemented] Ticket #3923 - change order in tree view

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: change order in tree view

Post by yorik »

wmayer wrote: Fri Mar 29, 2019 3:35 pm IMO the easiest way would be to only change the tree view directly and do not touch the order of objects inside the document. To make the order of items in the tree view persistent the GuiDocument.xml can be extended to also store this order.
Okay that seems a good solution, I could try to do that.. good C++ exercise :) But there are a few things I don't understand well... In Gui/Tree.h & Gui/Tree.cpp there is an ObjectMap std::map, which holds names and objects. Apparently that is what the tree uses. But I can't find how or where it is populated... Could you give a few hints to where to start and look for?
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: change order in tree view

Post by wmayer »

But I can't find how or where it is populated...
Look at DocumentItem::createNewItem. But actually you don't have to delete or create items.
Could you give a few hints to where to start and look for?
Look at the methods https://doc.qt.io/qt-5/qtreewidgetitem.html#insertChild or https://doc.qt.io/qt-5/qtreewidgetitem.html#takeChild

Before trying too hard in C++ it might be better to experiment first a bit in Python.

Code: Select all

from PySide import QtGui

mw=Gui.getMainWindow()
tree=mw.findChild(QtGui.QTreeWidget)

item=tree.topLevelItem(0)

# Create a body...
child=item.child(0)
child=child.child(0)
child=child.child(0)

# remove first item and append it at the end
item=child.takeChild(0)
child.addChild(item)
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: change order in tree view

Post by yorik »

Okay.. So you are suggesting not to touch anything, but simply to keep a list of items that need to be moved up and down? I was thinking to have a second list, identical to the objects array kept by the document, but stored somewhere else (in the gui document probably), where the order could be changed, and use that list to build the tree instead of the document's objects array

Hmm... another idea crosses my mind now, I think that's what you meant. This could be solved entirely at QTreeWidget level, right? Enable everything needed to allow users to drag/reorder items, then find a way to save that state somehow... Then we're not touching anything else

Okay I'll start playing with this, thanks!
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: change order in tree view

Post by wmayer »

Hmm... another idea crosses my mind now, I think that's what you meant. This could be solved entirely at QTreeWidget level, right?
Exactly!
User avatar
furti
Posts: 344
Joined: Mon Nov 27, 2017 5:27 pm

Re: change order in tree view

Post by furti »

yorik wrote: Thu Apr 04, 2019 7:15 pm then find a way to save that state somehow
Maybe a simple "sort order" property in the view properties of an object. Everything that has a sort order will be sorted. Everything without is simply at the end of the tree like now.

Then one can set the property manually or per script or whatever. And the drag and drop logic only needs to set this property.

This should be a pretty flexible solution. Also groups can use this property later on to sort their children.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Ticket #3923 - change order in tree view

Post by Kunda1 »

Added ticket number to this thread title
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Ticket #3923 - change order in tree view

Post by bernd »

tpavlicek
Posts: 60
Joined: Sun Jan 07, 2018 2:15 am

Re: Ticket #3923 - change order in tree view

Post by tpavlicek »

Hello,

I have created Pull Request #4626 to implement this feature.

In a nutshell, each document object view provider obtains a new tree "rank" attribute, which is saved in the output GUI XML document. This attribute is later used during the document load to sort the corresponding tree view items in desired order.

To allow reordering, two new View->TreeView actions menu commands were added - "Move Up in Group" and "Move Down in Group". As the caption suggests, the reordering is possible only within the same group, i.e. between siblings. It is not possible to move an item up to its parent or down to its children level. If first sibling is selected, the "Move Up" menu action is grayed and if the last sibling is selected, the "Move Down" menu action is not accessible.

To simplify the reordering (because moving last item all the way up can be pretty tedious), the actions can be also accessed via keyboard shortcuts - "Alt + Up" and "Alt + Down". I believe this assignment is reasonable, as treeview itself uses Up/Down for single selection movement, Shift+Up/Down for selection expand/shrink and Ctrl+Up/Down for current (unselected) item position.

Please let me know if you find this implementation useful or if there are any problems I failed to notice.


Kind regards,


Tomas
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Ticket #3923 - change order in tree view

Post by adrianinsaval »

tpavlicek wrote: Tue Mar 16, 2021 9:02 pm Hello,

I have created Pull Request #4626 to implement this feature.
Awesome! Shouldn't these commands be added to the context menu? Otherwise I think many people will never know about it. Making it work through drag and drop would be ideal but keyboard shortcuts are a great improvement already.

There are some issues though, this shouldn't allow reordering elements within a PartDesign container as the order of elements is relevant there, not sure if there are other workbenches were order in the tree is important. I'm also not sure if moving around the origin object inside a part container is a good idea but I guess that doesn't do any harm.

On a different and non critical note about UX, maybe when moving something within a container and reaching the limit the selection shouldn't continue to move past that while pressing alt+arrow. Consider the case were you want to move an item to the top of a container and you press alt+up-arrow many times, by accident you press the arrow key two times more than necessary, now the container has changed it's order and collapsed at the same time, this could be annoying.
chrisb
Veteran
Posts: 53933
Joined: Tue Mar 17, 2015 9:14 am

Re: Ticket #3923 - change order in tree view

Post by chrisb »

adrianinsaval wrote: Wed Mar 17, 2021 5:58 am this shouldn't allow reordering elements within a PartDesign container
PartDesign has already its reorder function in the context menu. That could be a blueprint for the new global function.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply