Stop module exit

Hello! Is there a way to stop a module from exiting, from inside its own exit() function? I would like to offer the user the option to cancel his exit from the module to allow him finish some operations that he may want to do before exiting.

For example, he clicks another module and gets a prompt: “Are you sure you want to exit the module?”

A Python module should never call exit(). I would recommend to ask the developer to remove this call. If the developer does not respond to this then you can use the module in a separate process (for example, run it in a Python CLI module).

If you mean that you would like to write a module that closes the application gracefully (lets the user decide to actually quit or not) then you can use slicer.util.mainWindow().close().

Hi Andras, I mean the exit(self) function that is called when you exit a module.

  def enter(self):
    self.interfaceFrame.enabled = False
    self.setupDialog()

  def exit(self):
    self.removeInteractorObservers()

The code above is from the Landmark Registration module, as an example.

A separate but important issue is that when I close Slicer, the dialog asking to save the scene appears before the call to exit(self) from the current module. Shouldn’t it be after? (the developer may want to do some changes on the scene within the current module exit(self), before allowing saving the scene, and thus keep those changes)

Once the method’s exit method is called, exiting the module is already in progress. You can always apply hacks, such as starting a timer in the exit method and switch back to the current module when the timer elapses, but the proper solution is to only display actions to the user that he is not allowed to perform. For example, you can hide the module selector toolbar.

All persistent information must be stored in the scene. If there is really absolutely no other way than doing some last-minute node updates then you can find some hacks (e.g., override default scene save dialog where you first do what you need and then call the default dialog), but I would not recommend to do this. Storing information in the scene is not required only because of scene saving but the application and all modules assume that information in the scene is up-to-date and you don’t withhold (or privately cache) anything essential.