Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by openBrain »

wmayer wrote: Ping
@wmayer I'd like to analyse why the 'onChange' function isn't correctly called when I 'Attach' the instance to a parameter group (while the attachment itself works, and all works fine in a macro). Could you please indicate in which function the attached instances are called when a parameter is changed ?
Thanks
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by carlopav »

openBrain wrote: Mon Jan 24, 2022 4:26 pm * Regarding DocumentObserver, but grid management in Draft (through the 'Snapper' tracker) is too complex (chunky ? :P) for me to achieve something working smoothly without having to deep dive into it
If the purpose is to set the grid visible when a new document is opened while the Draft workbench is already active, this dirty solution works for me:

Code: Select all

    def slotActivateDocument(self, doc):
        """Called when a document is activated"""
        if FreeCADGui.Snapper:
            from PySide import QtCore
            QtCore.QTimer.singleShot(2000, FreeCADGui.Snapper.setGrid)
I guess because the 3DView is not already available when a new document is activated? :?: The solution is really dirty, it is only meant for debugging purposes. Perhaps we should observe the creation of a new view and not of a new document? In fact the grid is not displayed also when you open a new view of an already opened document.... Edit: is there a way to observe when "Gui.ActiveDocument.ActiveView" changes?

I think FreeCADGui.Snapper should always be available, also before the workbench is activated, can you reproduce a case when it does not exhist or it is None?
follow my experiments on BIM modelling for architecture design
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by openBrain »

carlopav wrote: Tue Jan 25, 2022 9:55 pmEdit: is there a way to observe when "Gui.ActiveDocument.ActiveView" changes?
Unfortunately not. I also had a look at this and there is no slot in DocumentObserver for this. I had a quick look to implement it, but it's over my spare time ATM.
Maybe we could ping @wmayer about this.
I think FreeCADGui.Snapper should always be available, also before the workbench is activated, can you reproduce a case when it does not exhist or it is None?
AFAIR no. 'Snapper' is always there.
But 'Snapper.grid' changes a lot : non existing, None, instantiated,...
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by yorik »

Yes, this is all old, legacy code from a time I wasn't very good at coding :oops: The whole grid and snapping system could definitely be reworked.

Indeed many startup procedures happen before the 3D view is on. Firing a Qt timer is how we usually solved this in the past, for ex. in the Tux module.

I can look at the onChange function too.. Which macro has it successfully working?
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by openBrain »

yorik wrote: Wed Jan 26, 2022 11:58 am Yes, this is all old, legacy code from a time I wasn't very good at coding :oops: The whole grid and snapping system could definitely be reworked.
It also looks like it was tweaked for feature improvement several times. Some refactor could be good, but I guess the main issue for me was my lack of knowledge on this part of code. :)
Indeed many startup procedures happen before the 3D view is on. Firing a Qt timer is how we usually solved this in the past, for ex. in the Tux module.
Yes, saw that in the past when fixing something in Tux. I'll refactor it when I get some spare time. ;)
However here it indeed probably needs to have an event when view is ready. I think having a slot in Gui::DocumentObserver when a view is created (fired when the view is ready) would be beneficial. Eventually one when a view is closed too, but IMO less useful. I'll ping @wmayer soon to probe how he feels about implementing this. :P
I can look at the onChange function too.. Which macro has it successfully working?
Ouch, I'm with my mobile now. Will post it here in the coming day.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by openBrain »

yorik wrote: Wed Jan 26, 2022 11:58 am I can look at the onChange function too.. Which macro has it successfully working?
Here the macro I used for testing "as is" :

Code: Select all

class obs:

    def onChange(self, arg1, arg2):
        print(f"{arg1=} // {arg2=}")

ob = obs()
grp = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
grp.Attach(ob)
Then to test you modify any Draft parameter (either in preferences or in parameter editor).
I had another issue that may eventually solved at the same time : each parameter is called twice when changing in preferences. ;)
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by wmayer »

openBrain wrote: Mon Jan 24, 2022 9:26 am Here's the basic fix as a PR : https://github.com/FreeCAD/FreeCAD/pull/5427
The PR lacks of the unit property (replace suffix with unit):

Code: Select all

          <property name="unit">
           <string>mm</string>
          </property>
Several UI pages of the Draft wb are added to the resource file Draft_rc.py. So, after the change you still have to update Draft_rc.py:

Code: Select all

pyside2-rcc <Path>/Mod/Draft/Resources/Draft.qrc -o <Path>/Mod/Draft/Draft_rc.py
Because preferences-draftsnap.ui is loaded by PreferenceUiForm an additional fix was needed: git commit 8da66398ad

Now there is still an odd behaviour of PrefQuantitySpinBox that of prefPath and prefEntry that latter is ignored. PrefQuantitySpinBox saves a history of several values (by default up to 5) and saves them as Hist0, Hist1, ... The context-menu of the widgets lists all values of the history.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by wmayer »

openBrain wrote: Tue Jan 25, 2022 5:11 pm @wmayer I'd like to analyse why the 'onChange' function isn't correctly called when I 'Attach' the instance to a parameter group (while the attachment itself works, and all works fine in a macro). Could you please indicate in which function the attached instances are called when a parameter is changed ?
Thanks
https://github.com/FreeCAD/FreeCAD/blob ... ver.h#L173
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by carlopav »

wmayer wrote: Sat Jan 29, 2022 7:10 am
@wmayer, how do you feel about having the possibility for the Gui Document Observer to be notified when a view is Activated?
follow my experiments on BIM modelling for architecture design
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Bug #1951: Draft workbench preferences snap settings does not use "units" settings

Post by wmayer »

Making the document observer aware of view changes doesn't fit very well in this concept. You better connect a slot function to the Qt signal subWindowActivated of the QMdiArea class: https://doc.qt.io/qt-5/qmdiarea.html#subWindowActivated

Code: Select all

from PySide2 import QtWidgets

def myFunction(args):
  print (args)

mw = Gui.getMainWindow()
mdi = mw.findChild(QtWidgets.QMdiArea)
mdi.subWindowActivated.connect(myFunction)
Post Reply