Draft: Revise close task panels on doc close solution (#21546)

Use built-in feature to close the task panels. See #21253.
This commit is contained in:
Roy-043
2025-05-24 10:25:40 +02:00
committed by GitHub
parent 8ef8f64e9b
commit 98d4222253
8 changed files with 15 additions and 93 deletions

View File

@@ -70,7 +70,6 @@ SET(Draft_tests
SET(Draft_utilities
draftutils/__init__.py
draftutils/doc_observer.py
draftutils/grid_observer.py
draftutils/groups.py
draftutils/gui_utils.py

View File

@@ -157,8 +157,6 @@ class DraftWorkbench(FreeCADGui.Workbench):
WorkingPlane._view_observer_start() # Updates the draftToolBar when switching views.
from draftutils import grid_observer
grid_observer._view_observer_setup()
from draftutils import doc_observer
doc_observer._doc_observer_start()
FreeCAD.Console.PrintLog("Draft workbench activated.\n")
def Deactivated(self):
@@ -173,8 +171,6 @@ class DraftWorkbench(FreeCADGui.Workbench):
WorkingPlane._view_observer_stop()
from draftutils import grid_observer
grid_observer._view_observer_setup()
from draftutils import doc_observer
doc_observer._doc_observer_stop()
FreeCAD.Console.PrintLog("Draft workbench deactivated.\n")
def ContextMenu(self, recipient):

View File

@@ -34,9 +34,6 @@ import FreeCAD as App
import FreeCADGui as Gui
from draftguitools import gui_base
from draftutils import gui_utils
from draftutils import todo
from draftutils.messages import _log
from draftutils.translate import translate
from drafttaskpanels import task_circulararray
@@ -77,8 +74,9 @@ class CircularArray(gui_base.GuiCommandBase):
# The calling class (this one) is saved in the object
# of the interface, to be able to call a function from within it.
self.ui.source_command = self
# Gui.Control.showDialog(self.ui)
todo.ToDo.delay(Gui.Control.showDialog, self.ui)
task = Gui.Control.showDialog(self.ui)
task.setDocumentName(Gui.ActiveDocument.Document.Name)
task.setAutoCloseOnDeletedDocument(True)
def move(self, event_cb):
"""Execute as a callback when the pointer moves in the 3D view.

View File

@@ -46,7 +46,9 @@ class Draft_Hatch(gui_base.GuiCommandNeedsSelection):
import FreeCADGui
if FreeCADGui.Selection.getSelection():
FreeCADGui.Control.showDialog(Draft_Hatch_TaskPanel(FreeCADGui.Selection.getSelection()[0]))
task = FreeCADGui.Control.showDialog(Draft_Hatch_TaskPanel(FreeCADGui.Selection.getSelection()[0]))
task.setDocumentName(FreeCADGui.ActiveDocument.Document.Name)
task.setAutoCloseOnDeletedDocument(True)
else:
FreeCAD.Console.PrintError(translate("Draft", "You must choose a base object before using this command") + "\n")

View File

@@ -34,9 +34,6 @@ import FreeCAD as App
import FreeCADGui as Gui
from draftguitools import gui_base
from draftutils import gui_utils
from draftutils import todo
from draftutils.messages import _log
from draftutils.translate import translate
from drafttaskpanels import task_orthoarray
@@ -77,8 +74,9 @@ class OrthoArray(gui_base.GuiCommandBase):
# The calling class (this one) is saved in the object
# of the interface, to be able to call a function from within it.
self.ui.source_command = self
# Gui.Control.showDialog(self.ui)
todo.ToDo.delay(Gui.Control.showDialog, self.ui)
task = Gui.Control.showDialog(self.ui)
task.setDocumentName(Gui.ActiveDocument.Document.Name)
task.setAutoCloseOnDeletedDocument(True)
def click(self, event_cb=None):
"""Execute as a callback when the pointer clicks on the 3D view.

View File

@@ -34,9 +34,6 @@ import FreeCAD as App
import FreeCADGui as Gui
from draftguitools import gui_base
from draftutils import gui_utils
from draftutils import todo
from draftutils.messages import _log
from draftutils.translate import translate
from drafttaskpanels import task_polararray
@@ -77,8 +74,9 @@ class PolarArray(gui_base.GuiCommandBase):
# The calling class (this one) is saved in the object
# of the interface, to be able to call a function from within it.
self.ui.source_command = self
# Gui.Control.showDialog(self.ui)
todo.ToDo.delay(Gui.Control.showDialog, self.ui)
task = Gui.Control.showDialog(self.ui)
task.setDocumentName(Gui.ActiveDocument.Document.Name)
task.setAutoCloseOnDeletedDocument(True)
def move(self, event_cb):
"""Execute as a callback when the pointer moves in the 3D view.

View File

@@ -42,7 +42,6 @@ from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCADGui as Gui
from draftguitools import gui_base
from draftutils import gui_utils
from draftutils import todo
from draftutils.messages import _toolmsg
from draftutils.translate import translate
from drafttaskpanels import task_shapestring
@@ -67,7 +66,9 @@ class ShapeString(gui_base.GuiCommandBase):
self.ui = task_shapestring.ShapeStringTaskPanelCmd(self)
self.call = self.view.addEventCallback("SoEvent", self.ui.action)
_toolmsg(translate("draft", "Pick ShapeString location point"))
todo.ToDo.delay(Gui.Control.showDialog, self.ui)
task = Gui.Control.showDialog(self.ui)
task.setDocumentName(Gui.ActiveDocument.Document.Name)
task.setAutoCloseOnDeletedDocument(True)
def finish(self):
try:

View File

@@ -1,70 +0,0 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * *
# * Copyright (c) 2025 FreeCAD Project Association *
# * *
# * This file is part of FreeCAD. *
# * *
# * FreeCAD is free software: you can redistribute it and/or modify it *
# * under the terms of the GNU Lesser General Public License as *
# * published by the Free Software Foundation, either version 2.1 of the *
# * License, or (at your option) any later version. *
# * *
# * FreeCAD is distributed in the hope that it will be useful, but *
# * WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with FreeCAD. If not, see *
# * <https://www.gnu.org/licenses/>. *
# * *
# ***************************************************************************
import FreeCAD as App
if App.GuiUp:
import FreeCADGui as Gui
from draftutils.todo import ToDo
class _Draft_DocObserver():
# See: /src/Gui/DocumentObserverPython.h
def slotDeletedDocument(self, gui_doc):
_finish_command_on_doc_close(gui_doc)
_doc_observer = None
def _doc_observer_start():
global _doc_observer
if _doc_observer is None:
_doc_observer = _Draft_DocObserver()
Gui.addDocumentObserver(_doc_observer)
def _doc_observer_stop():
global _doc_observer
try:
if _doc_observer is not None:
Gui.removeDocumentObserver(_doc_observer)
except:
pass
_doc_observer = None
def _finish_command_on_doc_close(gui_doc):
"""Finish the active Draft or BIM command if the related document has been
closed. Only works for commands that have set `App.activeDraftCommand.doc`
and use a task panel.
"""
if getattr(App, "activeDraftCommand", None) \
and getattr(App.activeDraftCommand, "doc", None) == gui_doc.Document:
if hasattr(App.activeDraftCommand, "ui"):
if hasattr(App.activeDraftCommand.ui, "reject"):
ToDo.delay(App.activeDraftCommand.ui.reject, None)
elif hasattr(App.activeDraftCommand.ui, "escape"):
ToDo.delay(App.activeDraftCommand.ui.escape, None)
elif hasattr(Gui, "Snapper") \
and hasattr(Gui.Snapper, "ui") \
and hasattr(Gui.Snapper.ui, "escape"):
ToDo.delay(Gui.Snapper.ui.escape, None)