From 7189d3de056206097cce3254c8f359b558d30aaf Mon Sep 17 00:00:00 2001 From: vocx-fc Date: Wed, 11 Mar 2020 17:48:19 -0600 Subject: [PATCH] Draft: move line GuiCommands to gui_lineops module The commands `FinishLine`, `CloseLine`, and `UndoLine` are moved from the huge `DraftTools.py` to `gui_lineops.py` to reduce the complexity of the former file. These GuiCommands aren't actually used presently in the Draft Workbench. They were used in the past particularly from the context menu when editing a Line object. --- src/Mod/Draft/CMakeLists.txt | 1 + src/Mod/Draft/DraftTools.py | 66 +-------- src/Mod/Draft/draftguitools/gui_lineops.py | 163 +++++++++++++++++++++ 3 files changed, 167 insertions(+), 63 deletions(-) create mode 100644 src/Mod/Draft/draftguitools/gui_lineops.py diff --git a/src/Mod/Draft/CMakeLists.txt b/src/Mod/Draft/CMakeLists.txt index 40687f5a31..5d3a234361 100644 --- a/src/Mod/Draft/CMakeLists.txt +++ b/src/Mod/Draft/CMakeLists.txt @@ -96,6 +96,7 @@ SET(Draft_GUI_tools draftguitools/gui_snapper.py draftguitools/gui_trackers.py draftguitools/gui_edit.py + draftguitools/gui_lineops.py draftguitools/README.md ) diff --git a/src/Mod/Draft/DraftTools.py b/src/Mod/Draft/DraftTools.py index 3f9b08caed..e76434bb95 100644 --- a/src/Mod/Draft/DraftTools.py +++ b/src/Mod/Draft/DraftTools.py @@ -77,6 +77,9 @@ if not hasattr(FreeCAD, "DraftWorkingPlane"): import draftguitools.gui_edit import draftguitools.gui_selectplane import draftguitools.gui_planeproxy +from draftguitools.gui_lineops import FinishLine +from draftguitools.gui_lineops import CloseLine +from draftguitools.gui_lineops import UndoLine # import DraftFillet import drafttaskpanels.task_shapestring as task_shapestring import drafttaskpanels.task_scale as task_scale @@ -980,66 +983,6 @@ class CubicBezCurve(Line): self.Activated() -class FinishLine: - """a FreeCAD command to finish any running Line drawing operation""" - - def Activated(self): - if (FreeCAD.activeDraftCommand != None): - if (FreeCAD.activeDraftCommand.featureName == "Line"): - FreeCAD.activeDraftCommand.finish(False) - - def GetResources(self): - return {'Pixmap' : 'Draft_Finish', - 'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_FinishLine", "Finish line"), - 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Draft_FinishLine", "Finishes a line without closing it")} - - def IsActive(self): - if FreeCADGui.ActiveDocument: - return True - else: - return False - - -class CloseLine: - """a FreeCAD command to close any running Line drawing operation""" - - def Activated(self): - if (FreeCAD.activeDraftCommand != None): - if (FreeCAD.activeDraftCommand.featureName == "Line"): - FreeCAD.activeDraftCommand.finish(True) - - def GetResources(self): - return {'Pixmap' : 'Draft_Lock', - 'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_CloseLine", "Close Line"), - 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Draft_CloseLine", "Closes the line being drawn")} - - def IsActive(self): - if FreeCADGui.ActiveDocument: - return True - else: - return False - - -class UndoLine: - """a FreeCAD command to undo last drawn segment of a line""" - - def Activated(self): - if (FreeCAD.activeDraftCommand != None): - if (FreeCAD.activeDraftCommand.featureName == "Line"): - FreeCAD.activeDraftCommand.undolast() - - def GetResources(self): - return {'Pixmap' : 'Draft_Rotate', - 'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_UndoLine", "Undo last segment"), - 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Draft_UndoLine", "Undoes the last drawn segment of the line being drawn")} - - def IsActive(self): - if FreeCADGui.ActiveDocument: - return True - else: - return False - - class Rectangle(Creator): """the Draft_Rectangle FreeCAD command definition""" @@ -5501,9 +5444,6 @@ FreeCADGui.addCommand('Draft_Slope',Draft_Slope()) FreeCADGui.addCommand('Draft_Stretch',Stretch()) # context commands -FreeCADGui.addCommand('Draft_FinishLine',FinishLine()) -FreeCADGui.addCommand('Draft_CloseLine',CloseLine()) -FreeCADGui.addCommand('Draft_UndoLine',UndoLine()) FreeCADGui.addCommand('Draft_ToggleConstructionMode',ToggleConstructionMode()) FreeCADGui.addCommand('Draft_ToggleContinueMode',ToggleContinueMode()) FreeCADGui.addCommand('Draft_ApplyStyle',ApplyStyle()) diff --git a/src/Mod/Draft/draftguitools/gui_lineops.py b/src/Mod/Draft/draftguitools/gui_lineops.py new file mode 100644 index 0000000000..75d0d589fc --- /dev/null +++ b/src/Mod/Draft/draftguitools/gui_lineops.py @@ -0,0 +1,163 @@ +# *************************************************************************** +# * (c) 2009, 2010 Yorik van Havre * +# * (c) 2009, 2010 Ken Cline * +# * (c) 2020 Eliud Cabrera Castillo * +# * * +# * This file is part of the FreeCAD CAx development system. * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * 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 Library General Public License for more details. * +# * * +# * You should have received a copy of the GNU Library General Public * +# * License along with FreeCAD; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# *************************************************************************** +"""Provides certain line operations of the Draft Workbench. + +These GuiCommands aren't really used anymore, as the same actions +are called from the task panel interface by other methods. +""" +## @package gui_lineops +# \ingroup DRAFT +# \brief Provides certain line operations in the Draft Workbench. +from PySide.QtCore import QT_TRANSLATE_NOOP + +import FreeCAD as App +import FreeCADGui as Gui +import Draft_rc +import draftguitools.gui_base as gui_base +from draftutils.messages import _msg +from draftutils.translate import _tr + +# The module is used to prevent complaints from code checkers (flake8) +True if Draft_rc.__name__ else False + + +class LineAction(gui_base.GuiCommandSimplest): + """Base class for Line context GuiCommands. + + This is inherited by the other GuiCommand classes to run + a set of similar actions when editing a line, wire, spline, + or bezier curve. + + It inherits `GuiCommandSimplest` to set up the document + and other behavior. See this class for more information. + """ + + def Activated(self, action="None"): + """Execute when the command is called. + + Parameters + ---------- + action: str + Indicates the type of action to perform with the line object. + It can be `'finish'`, `'close'`, or `'undo'`. + """ + if hasattr(App, "activeDraftCommand"): + _command = App.activeDraftCommand + else: + _msg(_tr("No active command.")) + return + + if (_command is not None + and _command.featureName in ("Line", "Polyline", + "BSpline", "BezCurve", + "CubicBezCurve")): + if action == "finish": + _command.finish(False) + elif action == "close": + _command.finish(True) + elif action == "undo": + _command.undolast() + + +class FinishLine(LineAction): + """GuiCommand to finish any running line drawing operation.""" + + def __init__(self): + super().__init__(name=_tr("Finish line")) + + def GetResources(self): + """Set icon, menu and tooltip.""" + _tip = "Finishes a line without closing it." + + d = {'Pixmap': 'Draft_Finish', + 'MenuText': QT_TRANSLATE_NOOP("Draft_FinishLine", "Finish line"), + 'ToolTip': QT_TRANSLATE_NOOP("Draft_FinishLine", _tip), + 'CmdType': 'ForEdit'} + return d + + def Activated(self): + """Execute when the command is called. + + It calls the `finish(False)` method of the active Draft command. + """ + super().Activated(action="finish") + + +Gui.addCommand('Draft_FinishLine', FinishLine()) + + +class CloseLine(LineAction): + """GuiCommand to close the line being drawn and finish the operation.""" + + def __init__(self): + super().__init__(name=_tr("Close line")) + + def GetResources(self): + """Set icon, menu and tooltip.""" + _tip = "Closes the line being drawn, and finishes the operation." + + d = {'Pixmap': 'Draft_Lock', + 'MenuText': QT_TRANSLATE_NOOP("Draft_CloseLine", "Close Line"), + 'ToolTip': QT_TRANSLATE_NOOP("Draft_CloseLine", _tip), + 'CmdType': 'ForEdit'} + return d + + def Activated(self): + """Execute when the command is called. + + It calls the `finish(True)` method of the active Draft command. + """ + super().Activated(action="close") + + +Gui.addCommand('Draft_CloseLine', CloseLine()) + + +class UndoLine(LineAction): + """GuiCommand to undo the last drawn segment of a line.""" + + def __init__(self): + super().__init__(name=_tr("Undo line")) + + def GetResources(self): + """Set icon, menu and tooltip.""" + _tip = "Undoes the last drawn segment of the line being drawn." + + d = {'Pixmap': 'Draft_Rotate', + 'MenuText': QT_TRANSLATE_NOOP("Draft_UndoLine", + "Undo last segment"), + 'ToolTip': QT_TRANSLATE_NOOP("Draft_UndoLine", _tip), + 'CmdType': 'ForEdit'} + return d + + def Activated(self): + """Execute when the command is called. + + It calls the `undolast` method of the active Draft command. + """ + super().Activated(action="undo") + + +Gui.addCommand('Draft_UndoLine', UndoLine())