Question: how to adapt "Property editor" width to new property names (Combo View)

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Question: how to adapt "Property editor" width to new property names (Combo View)

Post by onekk »

Hello, I'm in need to resize "property editor" to adapt the "property" column to new "property names" created in a Feature Python Object (FPO).

We are speaking of this:
https://wiki.freecadweb.org/Property_editor

There is a way to adapt the size of "property editor" first column to adapt it to content added by a FPO?

I'm speaking about first column that contains the "Property Name".


I think that I could probably obtain the ComboView using this code:

Code: Select all

import PySide2
import FreeCADGui
mw = FreeCADGui.getMainWindow()
cb = mw.findChild(PySide2.QtCore.QObject, "Combo View")
What I have found

Combo View widgets is made in this way:

Code: Select all

<PySide2.QtWidgets.QDockWidget(0x5591a73daf00, name="Combo View") at 0x7fcad3269800>
16:06:43  <PySide2.QtWidgets.QWidget(0x5591a3dda1a0, name="Combo View") at 0x7fcad0343340>
16:06:43  [<PySide2.QtWidgets.QGridLayout(0x5591a3d6cd50) at 0x7fcad03307c0>, <PySide2.QtWidgets.QTabWidget(0x5591a3d6ef40, name="combiTab") at 0x7fcad0330800>]
So seems that there is :

- a QDockWidget named "Combo View" that I suppose is a sort of container
- among his children there are anothe widget named again "Combo View" specified as QtWidget, probably another container
- this container contains a QTabWidget named "combiTab"


So I suppose I have to operate on "combiTab" finding a way to resize the "first column".

I'm lost in the nidification.

I want to use it on a 0.19.3 version (It is only that I have this version installed), if possible.
If not possible on 0.19.3, even some hints for 0.20 will be appreciated.

Any hints will be appreciated.

TIA and Regards

Carlo D.
OS: Artix Linux (openbox)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24366 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0f9259c)
Hash: 0f9259cda103ae1824ac16c68ac9b4a0d54b05fc
Python version: 3.9.7
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.3
Locale: Italian/Italy (it_IT)
Edited to clarify the request and title changed to be more precise (I hope)
Edited II change title to make more clear that is a question, some minor English corrections, new code added to shorten thread.
Last edited by onekk on Sun Jun 26, 2022 8:32 am, edited 5 times in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Question: how to adapt "Property editor" widht to new property names (Combo View)

Post by onekk »

I have edited first post to joint the new infos found.

Sorry for the mess.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Question: how to adapt "Property editor" widht to new property names (Combo View)

Post by onekk »

No hints?

I have done a search in the source tree using a grep for combiTab but sadly no luck.

@wmayer Please, do you have some hints?
wmayer wrote:
Sorry but I think you are the one that could help or give an authoritative answer.


Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Question: how to adapt "Property editor" width to new property names (Combo View)

Post by wmayer »

Code: Select all

from PySide2 import QtWidgets

mw = Gui.getMainWindow()
tab = mw.findChild(QtWidgets.QTabWidget, "combiTab")
tab = tab.findChild(QtWidgets.QTabWidget, "propertyTab")

# Has two tree views
tree = tab.findChildren(QtWidgets.QTreeView)[1]
tree.resizeColumnToContents(0)
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Question: how to adapt "Property editor" width to new property names (Combo View)

Post by onekk »

wmayer wrote: Wed Jun 29, 2022 11:38 am

Many thanks for the reply, I was lost.

I have a FeaturePython Object and the code is made to resize the property Tab based on on a property change.

I have a property Type that will add or delete the properties of the object based on his value.

I want the "Property column" to be adapted when I modify these properties.

I have tried to add them to onChanged() and in execute() and resizing seems not to be taken in account.

If I put the code in a python file, load it in "FC internal editor" and run the code resizing is taken in account.

What I'm doing wrong?

I have placed the code in a function (method) as follow:

Code: Select all

    def resize_property_tab(self):
        """Adapt Property Column of Data Tab to content."""
        mw = FreeCADGui.getMainWindow()
        c_tab = mw.findChild(QtWidgets.QTabWidget, "combiTab")
        p_tab = c_tab.findChild(QtWidgets.QTabWidget, "propertyTab")
        tree = p_tab.findChildren(QtWidgets.QTreeView)[1]
        tree.resizeColumnToContents(0)
        # print("resized")


And the code seems to be executed as "resized" is printed on the "Report View".


TIA and Regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Question: how to adapt "Property editor" width to new property names (Combo View)

Post by wmayer »

I have tried to add them to onChanged() and in execute() and resizing seems not to be taken in account.
I don't think this is a suitable place to add the code there. A better way is to implement your own DocumentObserver and implement the function slotAppendDynamicProperty.

Code: Select all

from PySide2 import QtWidgets
from PySide2 import QtCore

mw = Gui.getMainWindow()
tab = mw.findChild(QtWidgets.QTabWidget, "combiTab")
tab = tab.findChild(QtWidgets.QTabWidget, "propertyTab")

# Has two tree views
tree = tab.findChildren(QtWidgets.QTreeView)[1]

class MyDocObserver:
    def __init__(self, tree):
        self.tree = tree
    
    def resizeColumnToContents(self):
        self.tree.resizeColumnToContents(0)
    
    def slotAppendDynamicProperty(self, obj, prop):
        print ("Call slotAppendDynamicProperty")
        QtCore.QTimer.singleShot(200, self.resizeColumnToContents)


doc_observer = MyDocObserver(tree)
App.addDocumentObserver(doc_observer)

obj = App.ActiveDocument.addObject("App::FeaturePython", "Object")
# Select the object before running the next line
obj.addProperty("App::PropertyString", "Averyveryveryveryveryveryveryveryverylongpropertyname")
Hint: The property editor will only adjust its column width if the object is selected while adding the new property.
thyssentishman
Posts: 82
Joined: Mon May 16, 2022 10:35 am

Re: Question: how to adapt "Property editor" width to new property names (Combo View)

Post by thyssentishman »

onekk wrote: Wed Jun 29, 2022 2:50 pm I have a FeaturePython Object and the code is made to resize the property Tab based on on a property change.
Hi @onekk,

I managed to get something close to what you might want. I found two solutions for which I wrote two scripts.

Solution 1 (Hacky):

The header of the QTreeView from the PropertiesView has by default the interactive ResizeMode (QtWidgets.QHeaderView.Interactive). However there is also the QtWidgets.QHeaderView.ResizeToContents resize mode.

The clear disadvantage of using the latter is of course that you won't be able to resize the header columns anymore. A hacky (IMO) workaround would be to change to the ResizeToContents mode when your Type property is changed and after the columns have been resized to fit the content of the new properties, switch back to the Interactive mode. For the switch back to happen however, the document must be recomputed.

I did this by changing to the ResizeToContents mode inside the onChange() function and then switch back to the Interactive mode inside the execute() function.

Here is the example:
ResizeToFit1.gif
ResizeToFit1.gif (685.41 KiB) Viewed 789 times
And here is the script:
ResizeToFit1.FCMacro
(2.68 KiB) Downloaded 14 times

Solution 2:

As @wmayer shared, you can use QtWidgets.QTreeView.resizeColumnToContents(column) to resize specific columns. I used this method by calling your resize_property_tab function inside execute(). However when you change your Type property the changes will not be immediately visible. A document recompute has to occur first.

Here is the example:
ResizeToFit2.gif
ResizeToFit2.gif (669.54 KiB) Viewed 789 times
And here is the script:
ResizeToFit2.FCMacro
(2.5 KiB) Downloaded 11 times

I hope this helps. I'm sure there must be a better way but I thought that this could be enough for your purposes.

Kind regards,
Johannes
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Question: how to adapt "Property editor" width to new property names (Combo View)

Post by onekk »

wmayer wrote: Thu Jun 30, 2022 12:49 pm
I have tried to add them to onChanged() and in execute() and resizing seems not to be taken in account.
I don't think this is a suitable place to add the code there. A better way is to implement your own DocumentObserver and implement the function slotAppendDynamicProperty.
....
Hint: The property editor will only adjust its column width if the object is selected while adding the new property.

Many Thanks, I will experiment some more.

I have some question, if I could abuse of your kindness:

1) What is the scope of a "Document Observer"
2) In my "application" when I modify "Type" Property the Object is not selected "by default"
3) When I create a new object it is not selected by default

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Question: how to adapt "Property editor" width to new property names (Combo View)

Post by onekk »

thyssentishman wrote: Thu Jun 30, 2022 1:02 pm ...

I hope this helps. I'm sure there must be a better way but I thought that this could be enough for your purposes.

Kind regards,
Johannes

Yes it helps at least to have some code to experiment with, and to know better the working of the FC interface.

Let me analyze various options and decide what implement.


Many many thanks.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Question: how to adapt "Property editor" width to new property names (Combo View)

Post by onekk »

wmayer wrote: Thu Jun 30, 2022 12:49 pm ...

Thanks the code seems to work, but I note a strange behaviour:

When I activate Wb and open a file, the Property column is not adapting to the contents like in stock behaviour.

If I select an object it will correctly resize, it seems that "document Observer" is working adapting column 0 size as "debug" print statement:

Code: Select all

Call slotAppendDynamicProperty
FC_prop_editor.png
FC_prop_editor.png (28.23 KiB) Viewed 655 times
If I change Type property, first column is resized correctly.

Let me know if I my explaining is not giving you correct informations:

I'm testing on:

Code: Select all

OS: Artix Linux (openbox)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24366 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0f9259c)
Hash: 0f9259cda103ae1824ac16c68ac9b4a0d54b05fc
Python version: 3.9.7
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.3
Locale: Italian/Italy (it_IT)

EDIT:
The problem above is shown in the WB with "Customs FPO" in a saved FCStd file.

Strangely with this simple test code problem is not shown, I'm puzzled.
Fpobj.py
(6.01 KiB) Downloaded 10 times
End Edit


TIA and Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply