Catching document close event

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
aleph0
Posts: 43
Joined: Fri Jan 29, 2016 10:04 am
Location: Cambridge, England
Contact:

Catching document close event

Post by aleph0 »

Is there a way to get some Python code executed when a FreeCAD document is closed?

I have a macro which creates a document and a model in that document and also adds a menu to the menu bar to do some actions on the model. If I close the document, the menu should get removed.

I tried creating an invisible widget and catching its close event, but it doesn't get one when the document is closed even though the widget does get destroyed.

Currently the macro prevents duplicate menus by checking to see if its menu is already there in case I have previously run the macro and closed its document, but this is not really the right solution: the menu should go away as soon as I close the document.
User avatar
Chris_G
Veteran
Posts: 2580
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Catching document close event

Post by Chris_G »

You can use a document observer :

Code: Select all

import FreeCAD
import FreeCADGui

class myObserver(object):
    def __init__(self):
        self.target_doc = None
    def slotDeletedDocument(self, doc):
        if doc == self.target_doc:
            FreeCAD.Console.PrintMessage("%s has been deleted\n"%doc.Label)

obs = myObserver()
App.addDocumentObserver(obs)

doc = FreeCAD.newDocument("i_am_observed")
obs.target_doc = doc

The other methods that can be used in a document observer are here :
https://github.com/FreeCAD/FreeCAD/blob ... erPython.h
User avatar
flachyjoe
Veteran
Posts: 1869
Joined: Sat Mar 31, 2012 12:00 pm
Location: Limoges, France

Re: Catching document close event

Post by flachyjoe »

Hi,
is it possible to abort the document close :?:

Context : my workbench lets user edit an object with an external app (and save them back in FC document). I'd like to disallow document close until the external is closed.
- Flachy Joe -
Image
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Catching document close event

Post by openBrain »

flachyjoe wrote: Thu Dec 02, 2021 7:45 am Hi,
is it possible to abort the document close :?:

Context : my workbench lets user edit an object with an external app (and save them back in FC document). I'd like to disallow document close until the external is closed.
Not really possible to abort document close with the observer. However as the observer is run in the main thread, if you take care of closing your external inside its callback it will lock further processing until you're done.
wmayer
Founder
Posts: 20245
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Catching document close event

Post by wmayer »

flachyjoe wrote: Thu Dec 02, 2021 7:45 am is it possible to abort the document close :?:

Context : my workbench lets user edit an object with an external app (and save them back in FC document). I'd like to disallow document close until the external is closed.
In C++ the document class has the methods setClosable(bool)/isClosable() that have been added for this purpose. However, they are not yet exposed to Python.
User avatar
flachyjoe
Veteran
Posts: 1869
Joined: Sat Mar 31, 2012 12:00 pm
Location: Limoges, France

Re: Catching document close event

Post by flachyjoe »

openBrain wrote: Thu Dec 02, 2021 8:50 am as the observer is run in the main thread, if you take care of closing your external inside its callback it will lock further processing until you're done.
The problem is the object has to get it's state from the external before the document is saved. When does the close event occur, before or after user is asked to save?
wmayer wrote: Thu Dec 02, 2021 9:35 am In C++ the document class has the methods setClosable(bool)/isClosable() that have been added for this purpose. However, they are not yet exposed to Python.
So even if they are exposed soon my workbench won't be retro-compatible :(
- Flachy Joe -
Image
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Catching document close event

Post by openBrain »

flachyjoe wrote: Thu Dec 02, 2021 10:10 am The problem is the object has to get it's state from the external before the document is saved. When does the close event occur, before or after user is asked to save?
Just give a try to @Chris_G code. ;) As it prints in the Console, you'll see if it happens before or after save. ;)
User avatar
flachyjoe
Veteran
Posts: 1869
Joined: Sat Mar 31, 2012 12:00 pm
Location: Limoges, France

Re: Catching document close event

Post by flachyjoe »

openBrain wrote: Thu Dec 02, 2021 10:16 am Just give a try to @Chris_G code. ;) As it prints in the Console, you'll see if it happens before or after save. ;)
:oops:
slotDeletedDocument occurs after ask-to-save :(
- Flachy Joe -
Image
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Catching document close event

Post by openBrain »

flachyjoe wrote: Thu Dec 02, 2021 10:33 am slotDeletedDocument occurs after ask-to-save :(

Code: Select all

    /** Called when a document is about to be saved*/
    void slotStartSaveDocument(const App::Document&, const std::string&);
wmayer
Founder
Posts: 20245
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Catching document close event

Post by wmayer »

Post Reply