What are the required return values of setEdit and unsetEdit?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

What are the required return values of setEdit and unsetEdit?

Post by Roy_043 »

I am confused about the required return values of setEdit and unsetEdit.

See my comment here:
https://github.com/FreeCAD/FreeCAD/comm ... t-77851915

Return for setEdit:
True or None?

Return for unsetEdit:
Always False?
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: What are the required return values of setEdit and unsetEdit?

Post by adrianinsaval »

The comment here:
https://github.com/FreeCAD/FreeCAD/blob ... se.py#L436
makes me believe returning false makes the base class manage the process of unsetting edit mode and returning true should then mean that it has been managed and no further actions are needed. Would be best to have this confirmed by some dev.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: What are the required return values of setEdit and unsetEdit?

Post by Roy_043 »

Thanks. But AFAICT this is handled inconsistently in the Draft code, but also in the code of other Python workbenches. And if setEdit returns True for mode=0, wouldn't it then make sense that unsetEdit returns True in that case as well?
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: What are the required return values of setEdit and unsetEdit?

Post by adrianinsaval »

I think it depends, if you close the dialog and whatever else needed when calling unsetedit with mode == 0 then you pass true, but if the command is generic enough that it can be handled by a generic unsetedit isn't it best to avoid code duplication and just pass false and let the generic one do it? It's not clear to me what unsetedit would be called if you return false in shapestring so I don't know what return value is better. The hierarchies hurt my brain :oops:
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: What are the required return values of setEdit and unsetEdit?

Post by Roy_043 »

@openBrain Can you help with this question?
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: What are the required return values of setEdit and unsetEdit?

Post by openBrain »

With a mobile now so not much detail, but according code in 'ViewProviderFeaturePython.cpp', I'd say return :
* 'None' if there is no specific implementation for your object
* 'True' if you were called with a mode that you can deal with
* 'False' if you were called with a mode that you don't deal with
wmayer
Founder
Posts: 20242
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: What are the required return values of setEdit and unsetEdit?

Post by wmayer »

To answer this question it's best to have a look at the C++ interface: https://github.com/FreeCAD/FreeCAD/blob ... der.h#L431
As you can see for setEdit() the returned type is a boolean for unsetEdit() it's void (i.e. None in Python).

For Python view providers a further option is possible:
https://github.com/FreeCAD/FreeCAD/blob ... ture.h#L43
https://github.com/FreeCAD/FreeCAD/blob ... ure.h#L522

This means a Python view provider can throw a NotImplemented exception to indicate the system to call the implementation of the C++ base class.
Return for setEdit:
True or None?
So, it's True, False or throwing a NotImplemented exception. Returning None is considered as False.
Return for unsetEdit:
Always False?
It's None or throwing a NotImplemented exception. Other values are possible too but they are not considered by the system.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: What are the required return values of setEdit and unsetEdit?

Post by Roy_043 »

wmayer wrote: Fri Jul 29, 2022 12:47 pm So, it's True, False or throwing a NotImplemented exception. Returning None is considered as False.
This is not my experience (which matches the info in openBrain's post). Only returning None is interpreted as 'not handled'. Returning 0 is not. And returning None is not the same as returning False. But maybe we are talking about different objects and there are some inconsistencies in how they are handled?

Related PR:
https://github.com/FreeCAD/FreeCAD/pull/7174

OpenBrain is probably referring to:
https://github.com/FreeCAD/FreeCAD/blob ... #L350-L388
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: What are the required return values of setEdit and unsetEdit?

Post by openBrain »

Roy_043 wrote: Fri Jul 29, 2022 5:54 pm OpenBrain is probably referring to:
https://github.com/FreeCAD/FreeCAD/blob ... #L350-L388
Exactly. :) That was what I looked at for my above reply.
Post Reply