Draft: improved IsActive behavior (#14103)

Related issue: #14061.
This commit is contained in:
Roy-043
2024-05-18 11:38:38 +02:00
committed by GitHub
parent e50ef22e7b
commit df49763d3f
12 changed files with 104 additions and 140 deletions

View File

@@ -35,21 +35,18 @@ from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD as App
import FreeCADGui as Gui
import Draft
import Draft_rc
import DraftVecUtils
from FreeCAD import Units as U
from draftguitools import gui_base
from draftguitools import gui_base_original
from draftguitools import gui_tool_utils
from draftguitools import gui_trackers as trackers
from draftutils import gui_utils
from draftutils import params
from draftutils import utils
from draftutils.messages import _err, _toolmsg
from draftutils.translate import translate
# The module is used to prevent complaints from code checkers (flake8)
True if Draft_rc.__name__ else False
class Arc(gui_base_original.Creator):
"""Gui command for the Circular Arc tool."""
@@ -61,10 +58,10 @@ class Arc(gui_base_original.Creator):
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Draft_Arc',
'Accel': "A, R",
'MenuText': QT_TRANSLATE_NOOP("Draft_Arc", "Arc"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Arc", "Creates a circular arc by a center point and a radius.\nCTRL to snap, SHIFT to constrain.")}
return {"Pixmap": "Draft_Arc",
"Accel": "A, R",
"MenuText": QT_TRANSLATE_NOOP("Draft_Arc", "Arc"),
"ToolTip": QT_TRANSLATE_NOOP("Draft_Arc", "Creates a circular arc by a center point and a radius.\nCTRL to snap, SHIFT to constrain.")}
def Activated(self):
"""Execute when the command is called."""
@@ -475,10 +472,10 @@ class Arc_3Points(gui_base.GuiCommandBase):
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': "Draft_Arc_3Points",
'Accel': "A,T",
'MenuText': QT_TRANSLATE_NOOP("Draft_Arc_3Points", "Arc by 3 points"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Arc_3Points", "Creates a circular arc by picking 3 points.\nCTRL to snap, SHIFT to constrain.")}
return {"Pixmap": "Draft_Arc_3Points",
"Accel": "A,T",
"MenuText": QT_TRANSLATE_NOOP("Draft_Arc_3Points", "Arc by 3 points"),
"ToolTip": QT_TRANSLATE_NOOP("Draft_Arc_3Points", "Creates a circular arc by picking 3 points.\nCTRL to snap, SHIFT to constrain.")}
def Activated(self):
"""Execute when the command is called."""
@@ -611,22 +608,16 @@ class ArcGroup:
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'MenuText': QT_TRANSLATE_NOOP("Draft_ArcTools", "Arc tools"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_ArcTools", "Create various types of circular arcs.")}
return {"MenuText": QT_TRANSLATE_NOOP("Draft_ArcTools", "Arc tools"),
"ToolTip": QT_TRANSLATE_NOOP("Draft_ArcTools", "Create various types of circular arcs.")}
def GetCommands(self):
"""Return a tuple of commands in the group."""
return ('Draft_Arc', 'Draft_Arc_3Points')
def IsActive(self):
"""Return True when this command should be available.
It is `True` when there is a document.
"""
if Gui.ActiveDocument:
return True
else:
return False
"""Return True when this command should be available."""
return bool(gui_utils.get_3d_view())
Gui.addCommand('Draft_ArcTools', ArcGroup())

View File

@@ -30,7 +30,8 @@
from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCADGui as Gui
import Draft_rc
from draftutils import gui_utils
import draftguitools.gui_circulararray
import draftguitools.gui_orthoarray
import draftguitools.gui_patharray
@@ -39,12 +40,12 @@ import draftguitools.gui_polararray
import draftguitools.gui_pathtwistedarray
# The module is used to prevent complaints from code checkers (flake8)
bool(Draft_rc.__name__)
bool(draftguitools.gui_circulararray.__name__)
bool(draftguitools.gui_orthoarray.__name__)
bool(draftguitools.gui_patharray.__name__)
bool(draftguitools.gui_pointarray.__name__)
bool(draftguitools.gui_polararray.__name__)
bool(draftguitools.gui_pathtwistedarray.__name__)
class ArrayGroup:
@@ -61,16 +62,13 @@ class ArrayGroup:
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Draft_Array',
'MenuText': QT_TRANSLATE_NOOP("Draft_ArrayTools", "Array tools"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_ArrayTools", "Create various types of arrays, including rectangular, polar, circular, path, and point")}
return {"Pixmap": "Draft_Array",
"MenuText": QT_TRANSLATE_NOOP("Draft_ArrayTools", "Array tools"),
"ToolTip": QT_TRANSLATE_NOOP("Draft_ArrayTools", "Create various types of arrays, including rectangular, polar, circular, path, and point")}
def IsActive(self):
"""Return True when this command should be available."""
if Gui.activeDocument():
return True
else:
return False
return bool(gui_utils.get_3d_view())
Gui.addCommand('Draft_ArrayTools', ArrayGroup())

View File

@@ -31,8 +31,8 @@
# @{
import FreeCAD as App
import FreeCADGui as Gui
import draftutils.todo as todo
from draftutils import gui_utils
from draftutils import todo
from draftutils.messages import _toolmsg, _log
@@ -86,14 +86,8 @@ class GuiCommandSimplest:
self.doc = doc
def IsActive(self):
"""Return True when this command should be available.
It is `True` when there is a document.
"""
if App.activeDocument():
return True
else:
return False
"""Return True when this command should be available."""
return bool(App.activeDocument())
def Activated(self):
"""Execute when the command is called.
@@ -119,14 +113,8 @@ class GuiCommandNeedsSelection(GuiCommandSimplest):
"""
def IsActive(self):
"""Return True when this command should be available.
It is `True` when there is a selection.
"""
if App.activeDocument() and Gui.Selection.getSelection():
return True
else:
return False
"""Return True when this command should be available."""
return bool(Gui.Selection.getSelection())
class GuiCommandBase:
@@ -175,10 +163,7 @@ class GuiCommandBase:
def IsActive(self):
"""Return True when this command should be available."""
if App.ActiveDocument:
return True
else:
return False
return bool(gui_utils.get_3d_view())
def finish(self):
"""Terminate the active command by committing the list of commands.

View File

@@ -73,14 +73,8 @@ class DraftTool:
self.commitList = []
def IsActive(self):
"""Return True when this command should be available.
It is `True` when there is a document.
"""
if Gui.ActiveDocument:
return True
else:
return False
"""Return True when this command should be available."""
return bool(gui_utils.get_3d_view())
def Activated(self, name="None", is_subtool=False):
"""Execute when the command is called.

View File

@@ -43,6 +43,7 @@ from draftguitools import gui_base_original
from draftguitools import gui_lines
from draftguitools import gui_tool_utils
from draftguitools import gui_trackers as trackers
from draftutils import gui_utils
from draftutils import params
from draftutils import todo
from draftutils import utils
@@ -54,7 +55,7 @@ class BezCurve(gui_lines.Line):
"""Gui command for the Bézier Curve tool."""
def __init__(self):
super(BezCurve, self).__init__(wiremode=True)
super().__init__(wiremode=True)
self.degree = None
def GetResources(self):
@@ -70,9 +71,9 @@ class BezCurve(gui_lines.Line):
Activate the specific Bézier curve tracker.
"""
super(BezCurve, self).Activated(name="BezCurve",
icon="Draft_BezCurve",
task_title=translate("draft","Bézier curve"))
super().Activated(name="BezCurve",
icon="Draft_BezCurve",
task_title=translate("draft", "Bézier curve"))
if self.doc:
self.bezcurvetrack = trackers.bezcurveTracker()
@@ -240,7 +241,7 @@ class CubicBezCurve(gui_lines.Line):
"""
def __init__(self):
super(CubicBezCurve, self).__init__(wiremode=True)
super().__init__(wiremode=True)
self.degree = 3
self.old_EnableSelection = True
@@ -260,9 +261,9 @@ class CubicBezCurve(gui_lines.Line):
self.old_EnableSelection = params.get_param_view("EnableSelection")
params.set_param_view("EnableSelection", False)
super(CubicBezCurve, self).Activated(name="CubicBezCurve",
icon="Draft_CubicBezCurve",
task_title=translate("draft","Cubic Bézier curve"))
super().Activated(name="CubicBezCurve",
icon="Draft_CubicBezCurve",
task_title=translate("draft", "Cubic Bézier curve"))
if self.doc:
self.bezcurvetrack = trackers.bezcurveTracker()
@@ -489,14 +490,8 @@ class BezierGroup:
return ('Draft_CubicBezCurve', 'Draft_BezCurve')
def IsActive(self):
"""Return True when this command should be available.
It is `True` when there is a document.
"""
if Gui.ActiveDocument:
return True
else:
return False
"""Return True when this command should be available."""
return bool(gui_utils.get_3d_view())
Gui.addCommand('Draft_BezierTools', BezierGroup())

View File

@@ -36,6 +36,7 @@ import FreeCADGui as Gui
import WorkingPlane
from draftguitools import gui_base
from draftutils import gui_utils
from draftutils.translate import translate
@@ -54,7 +55,6 @@ class ToggleGrid(gui_base.GuiCommandSimplest):
def GetResources(self):
"""Set icon, menu and tooltip."""
return {"Pixmap": "Draft_Grid",
"Accel": "G, R",
"MenuText": QT_TRANSLATE_NOOP("Draft_ToggleGrid", "Toggle grid"),
@@ -62,6 +62,10 @@ class ToggleGrid(gui_base.GuiCommandSimplest):
"Toggles the Draft grid on and off."),
"CmdType": "ForEdit"}
def IsActive(self):
"""Return True when this command should be available."""
return bool(gui_utils.get_3d_view())
def Activated(self):
"""Execute when the command is called."""
super().Activated()

View File

@@ -30,15 +30,15 @@ from draftutils import params
from draftutils.translate import QT_TRANSLATE_NOOP, translate
class Draft_Hatch(gui_base.GuiCommandSimplest):
class Draft_Hatch(gui_base.GuiCommandNeedsSelection):
def GetResources(self):
return {'Pixmap' : "Draft_Hatch",
'MenuText': QT_TRANSLATE_NOOP("Draft_Hatch", "Hatch"),
'Accel': "H, A",
'ToolTip' : QT_TRANSLATE_NOOP("Draft_Hatch", "Creates hatches on the faces of a selected object")}
return {"Pixmap": "Draft_Hatch",
"MenuText": QT_TRANSLATE_NOOP("Draft_Hatch", "Hatch"),
"Accel": "H, A",
"ToolTip": QT_TRANSLATE_NOOP("Draft_Hatch", "Creates hatches on the faces of a selected object")}
def Activated(self):
@@ -47,7 +47,7 @@ class Draft_Hatch(gui_base.GuiCommandSimplest):
if FreeCADGui.Selection.getSelection():
FreeCADGui.Control.showDialog(Draft_Hatch_TaskPanel(FreeCADGui.Selection.getSelection()[0]))
else:
FreeCAD.Console.PrintError(translate("Draft","You must choose a base object before using this command")+"\n")
FreeCAD.Console.PrintError(translate("Draft", "You must choose a base object before using this command") + "\n")
class Draft_Hatch_TaskPanel:

View File

@@ -31,7 +31,7 @@
from PySide.QtCore import QT_TRANSLATE_NOOP
import os
import FreeCAD
import FreeCAD as App
import FreeCADGui as Gui
import Draft
import Draft_rc
@@ -60,20 +60,20 @@ class Layer(gui_base.GuiCommandSimplest):
"""GuiCommand to create a Layer object in the document."""
def __init__(self):
super(Layer, self).__init__(name=translate("draft","Layer"))
super().__init__(name=translate("draft", "Layer"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Draft_Layer',
'MenuText': QT_TRANSLATE_NOOP("Draft_Layer", "Layer"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Layer", "Adds a layer to the document.\nObjects added to this layer can share the same visual properties such as line color, line width, and shape color.")}
return {"Pixmap": "Draft_Layer",
"MenuText": QT_TRANSLATE_NOOP("Draft_Layer", "Layer"),
"ToolTip": QT_TRANSLATE_NOOP("Draft_Layer", "Adds a layer to the document.\nObjects added to this layer can share the same visual properties.")}
def Activated(self):
"""Execute when the command is called.
It calls the `finish(False)` method of the active Draft command.
"""
super(Layer, self).Activated()
super().Activated()
self.doc.openTransaction("Create Layer")
Gui.addModule("Draft")
@@ -88,9 +88,13 @@ class LayerManager:
def GetResources(self):
return {'Pixmap' : 'Draft_LayerManager',
'MenuText': QT_TRANSLATE_NOOP("Draft_LayerManager", "Manage layers..."),
'ToolTip' : QT_TRANSLATE_NOOP("Draft_LayerManager", "Set/modify the different layers of this document")}
return {"Pixmap" : "Draft_LayerManager",
"MenuText": QT_TRANSLATE_NOOP("Draft_LayerManager", "Manage layers..."),
"ToolTip" : QT_TRANSLATE_NOOP("Draft_LayerManager", "Set/modify the different layers of this document")}
def IsActive(self):
"""Return True when this command should be available."""
return bool(App.activeDocument())
def Activated(self):
@@ -150,14 +154,15 @@ class LayerManager:
"when OK button is pressed"
doc = App.ActiveDocument
changed = False
# delete layers
for name in self.deleteList:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
FreeCAD.ActiveDocument.removeObject(name)
doc.removeObject(name)
# apply changes
for row in range(self.model.rowCount()):
@@ -166,10 +171,10 @@ class LayerManager:
name = self.model.item(row,1).toolTip()
obj = None
if name:
obj = FreeCAD.ActiveDocument.getObject(name)
obj = doc.getObject(name)
if not obj:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
obj = Draft.make_layer(name=None, line_color=None, shape_color=None,
line_width=None, draw_style=None, transparency=None)
@@ -179,7 +184,7 @@ class LayerManager:
checked = True if self.model.item(row,0).checkState() == QtCore.Qt.Checked else False
if checked != vobj.Visibility:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
vobj.Visibility = checked
@@ -188,7 +193,7 @@ class LayerManager:
if label:
if obj.Label != label:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
obj.Label = label
@@ -197,7 +202,7 @@ class LayerManager:
if width:
if vobj.LineWidth != width:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
vobj.LineWidth = width
@@ -206,7 +211,7 @@ class LayerManager:
if style:
if vobj.DrawStyle != style:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
vobj.DrawStyle = style
@@ -215,7 +220,7 @@ class LayerManager:
if color:
if vobj.LineColor[:3] != color:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
vobj.LineColor = color
@@ -224,7 +229,7 @@ class LayerManager:
if color:
if vobj.ShapeColor[:3] != color:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
vobj.ShapeColor = color
@@ -233,7 +238,7 @@ class LayerManager:
if transparency:
if vobj.Transparency != transparency:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
vobj.Transparency = transparency
@@ -246,14 +251,14 @@ class LayerManager:
if "LinePrintColor" in vobj.PropertiesList:
if vobj.LinePrintColor[:3] != color:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
doc.openTransaction("Layers change")
changed = True
vobj.LinePrintColor = color
# recompute
if changed:
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
doc.commitTransaction()
doc.recompute()
# exit
self.dialog.reject()
@@ -288,7 +293,7 @@ class LayerManager:
self.dialog.tree.setColumnWidth(1,128) # name column
# populate
objs = [obj for obj in FreeCAD.ActiveDocument.Objects if Draft.getType(obj) == "Layer"]
objs = [obj for obj in App.ActiveDocument.Objects if Draft.getType(obj) == "Layer"]
objs.sort(key=lambda o:o.Label)
for obj in objs:
self.addItem(obj)
@@ -409,7 +414,7 @@ class LayerManager:
self.model.item(row,0).setCheckState(QtCore.Qt.Unchecked)
if FreeCAD.GuiUp:
if App.GuiUp:
from PySide import QtCore, QtGui, QtWidgets
@@ -507,8 +512,6 @@ if FreeCAD.GuiUp:
model.itemFromIndex(index).setIcon(getColorIcon(eval(editor.text())))
Gui.addCommand('Draft_Layer', Layer())
Gui.addCommand('Draft_LayerManager', LayerManager())

View File

@@ -29,10 +29,7 @@ from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD as App
import FreeCADGui as Gui
import Draft_rc
# The module is used to prevent complaints from code checkers (flake8)
True if Draft_rc.__name__ else False
from draftutils import gui_utils
__title__ = "FreeCAD Draft Workbench GUI Tools - Working plane-related tools"
__author__ = ("Yorik van Havre, Werner Mayer, Martin Burbaum, Ken Cline, "
@@ -45,18 +42,13 @@ class Draft_WorkingPlaneProxy:
def GetResources(self):
"""Set icon, menu and tooltip."""
d = {'Pixmap': 'Draft_PlaneProxy',
'MenuText': QT_TRANSLATE_NOOP("Draft_WorkingPlaneProxy","Create working plane proxy"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_WorkingPlaneProxy","Creates a proxy object from the current working plane.\nOnce the object is created double click it in the tree view to restore the camera position and objects' visibilities.\nThen you can use it to save a different camera position and objects' states any time you need.")}
return d
return {"Pixmap": "Draft_PlaneProxy",
"MenuText": QT_TRANSLATE_NOOP("Draft_WorkingPlaneProxy", "Create working plane proxy"),
"ToolTip": QT_TRANSLATE_NOOP("Draft_WorkingPlaneProxy", "Creates a proxy object from the current working plane.\nOnce the object is created double click it in the tree view to restore the camera position and objects' visibilities.\nThen you can use it to save a different camera position and objects' states any time you need.")}
def IsActive(self):
"""Return True when this command should be available."""
if Gui.ActiveDocument:
return True
else:
return False
return bool(gui_utils.get_3d_view())
def Activated(self):
"""Execute when the command is called."""

View File

@@ -36,6 +36,7 @@ import WorkingPlane
from FreeCAD import Units
from drafttaskpanels import task_selectplane
from draftutils import gui_utils
from draftutils import params
from draftutils import utils
from draftutils.messages import _msg
@@ -60,10 +61,7 @@ class Draft_SelectPlane:
def IsActive(self):
"""Return True when this command should be available."""
if Gui.ActiveDocument:
return True
else:
return False
return bool(gui_utils.get_3d_view())
def Activated(self):
"""Execute when the command is called."""

View File

@@ -49,7 +49,7 @@ class ApplyStyle(gui_base_original.Modifier):
}
def IsActive(self):
return bool(Gui.ActiveDocument and Gui.Selection.getSelection())
return bool(Gui.Selection.getSelection())
def Activated(self):
"""Execute when the command is called."""

View File

@@ -59,19 +59,23 @@ def get_3d_view():
Returns
-------
Gui::View3DInventor
Return the current `ActiveView` in the active document or `None`.
The Active 3D View or `None`.
"""
if App.GuiUp:
# FIXME The following two imports were added as part of PR4926
# Also see discussion https://forum.freecad.org/viewtopic.php?f=3&t=60251
import FreeCADGui as Gui
from pivy import coin
if Gui.ActiveDocument:
v = Gui.ActiveDocument.ActiveView
if "View3DInventor" in str(type(v)):
return v
if not App.GuiUp:
return None
return None
# FIXME The following two imports were added as part of PR4926
# Also see discussion https://forum.freecadweb.org/viewtopic.php?f=3&t=60251
import FreeCADGui as Gui
from pivy import coin
mw = Gui.getMainWindow()
view = mw.getActiveWindow()
if view is None:
return None
if not hasattr(view, "getSceneGraph"):
return None
return view
get3DView = get_3d_view