We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
freedman
Veteran
Posts: 3440
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Post by freedman »

I can't figure out a way to trap prior to FreeCAD deleting the object on using the delete key. Using slotDeletedObject is after the event of deleting. I tried capturing the key press but that is also after the event. SlotBeforeChangeObject fires early but I don't know how to recognize if the action is a delete. It looks like I need "slotBeforeDeletedObject" and there is no such slot.

What I want: If the user presses the delete key I want to be able to code if they should under the current situation. I wouldn't have a problem with using clearSelection to stop the processes but I just don't know how to set the trap.
Thanks
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: We have "slotDeletedObject", I need "slotBeforeDeletedObject"

Post by openBrain »

What kind of object?
Any object or specific ones of a document?
Is the slot the one of DocumentObserver (Gui or App?) or of DocumentObjectObserver?
freedman
Veteran
Posts: 3440
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Post by freedman »

What kind of object?
Any object or specific ones of a document?
Is the slot the one of DocumentObserver (Gui or App?) or of DocumentObjectObserver?
Edit: I would want to allow other objs and some Bodies to be deleted so I do need to filter the object i.e. look for custom properties.

The object being deleted is a Body in PartDesign. The macro selects a Body (hightlighted in the tree and 3D), I want to be able to block if the user clicks delete.
Here are the last few lines of the macro:

Code: Select all

visCB = cbToolBox() 
v = Gui.ActiveDocument.ActiveView
o = visCB.ViewObserver()
s = o.SelObserver()
FreeCADGui.Selection.addObserver(s)
do = visCB.DocObserver()
App.addDocumentObserver(do)
Thanks
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Post by onekk »

freedman wrote: Thu Jun 01, 2023 9:30 pm ...
There is not an onDelete() method somewhere?


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/
Workshop_Notes
Posts: 590
Joined: Wed Sep 29, 2021 8:35 am

Re: We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Post by Workshop_Notes »

freedman wrote: Thu Jun 01, 2023 4:43 pm I tried capturing the key press but that is also after the event.
Can you capture the keypress, and if the key pressed is 'delete', immediately issue an 'undo' action, and then proceed? In short, go past where you want to be and then back up one step.
freedman
Veteran
Posts: 3440
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Post by freedman »

immediately issue an 'undo' action
I tried that first and it works .
I would be depending on the undo to work perfectly, could be issues with many depenencies and the Delete tears up the model.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Post by onekk »

freedman wrote: Sat Jun 03, 2023 3:36 am
immediately issue an 'undo' action
...
If there is an onDelete property you could simply intercept his action and substitute his content with your desired action.

On Qt there is some examples of intercepting onClose() actionas example to avoid to close a dialog using the "small x" that many OS used to close a Window.

Same work if an object have the onDelete() you could not pass the action or emit a dialog to confirm, like "are you sure you want to delete me?".

More object oriented and robust than intercept keypress or Gui Commands.

But it depends on what the object is and if it have such s method. @wmayer?

Regards.

Carlo D.
Last edited by onekk on Tue Jun 06, 2023 6:47 am, edited 1 time 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/
freedman
Veteran
Posts: 3440
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Post by freedman »

I think I found it in a search.
viewtopic.php?t=11818#p94994
Thanks, I will report back.
freedman
Veteran
Posts: 3440
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Post by freedman »

Here is the code from the prior post, I can't get it to work. I get a no attribute "proxy". I tried this code without the proxy and the class fires once at startup but never again. I don't understand.

Code: Select all

obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython",partName)
class MyViewProviderProxy:
#    print("1")
    def onDelete(self, feature, subelements): # subelements is a tuple of strings
#    print("2")
        ...
        return True # If False is returned the object won't be deleted
obj.ViewObject.Proxy = MyViewProviderProxy()
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: We have "slotDeletedObject", I need "slotBeforeDeleteObject"

Post by onekk »

freedman wrote: Sun Jun 04, 2023 6:54 pm ...
As usual with partial code and too minimal code fragments is hard to tell and even experiment with code.

A little standalone script that load and executed will show the problem could be of some help.

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