From bad30329d783c4a9b057b95fd30f2d01b2f436fc Mon Sep 17 00:00:00 2001 From: vocx-fc Date: Fri, 3 Apr 2020 13:19:10 -0600 Subject: [PATCH] Draft: move ApplyStyle GuiCommand to gui_styles module Also fix an error in determining the objects of type group. Also call `finish` to properly terminate the command, otherwise it stays active and behaves incorrectly. --- src/Mod/Draft/CMakeLists.txt | 1 + src/Mod/Draft/DraftTools.py | 39 +-------- src/Mod/Draft/draftguitools/gui_styles.py | 96 +++++++++++++++++++++++ 3 files changed, 98 insertions(+), 38 deletions(-) create mode 100644 src/Mod/Draft/draftguitools/gui_styles.py diff --git a/src/Mod/Draft/CMakeLists.txt b/src/Mod/Draft/CMakeLists.txt index b25d750b8b..30d68c4c92 100644 --- a/src/Mod/Draft/CMakeLists.txt +++ b/src/Mod/Draft/CMakeLists.txt @@ -103,6 +103,7 @@ SET(Creator_tools SET(Modifier_tools draftguitools/gui_subelements.py draftguitools/gui_move.py + draftguitools/gui_styles.py ) SET(Draft_GUI_tools diff --git a/src/Mod/Draft/DraftTools.py b/src/Mod/Draft/DraftTools.py index 8801cd36e0..aee9de22b8 100644 --- a/src/Mod/Draft/DraftTools.py +++ b/src/Mod/Draft/DraftTools.py @@ -175,45 +175,9 @@ from draftguitools.gui_base_original import Modifier from draftguitools.gui_subelements import SubelementHighlight from draftguitools.gui_move import Move +from draftguitools.gui_styles import ApplyStyle -class ApplyStyle(Modifier): - """The Draft_ApplyStyle FreeCA command definition""" - - def GetResources(self): - return {'Pixmap' : 'Draft_Apply', - 'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_ApplyStyle", "Apply Current Style"), - 'ToolTip' : QtCore.QT_TRANSLATE_NOOP("Draft_ApplyStyle", "Applies current line width and color to selected objects")} - - def IsActive(self): - if FreeCADGui.Selection.getSelection(): - return True - else: - return False - - def Activated(self): - Modifier.Activated(self) - if self.ui: - self.sel = FreeCADGui.Selection.getSelection() - if (len(self.sel)>0): - FreeCADGui.addModule("Draft") - c = [] - for ob in self.sel: - if (ob.Type == "App::DocumentObjectGroup"): - c.extend(self.formatGroup(ob)) - else: - c.append('Draft.formatObject(FreeCAD.ActiveDocument.'+ob.Name+')') - self.commit(translate("draft","Change Style"),c) - - def formatGroup(self,grpob): - FreeCADGui.addModule("Draft") - c=[] - for ob in grpob.Group: - if (ob.Type == "App::DocumentObjectGroup"): - c.extend(self.formatGroup(ob)) - else: - c.append('Draft.formatObject(FreeCAD.ActiveDocument.'+ob.Name+')') - class Rotate(Modifier): """The Draft_Rotate FreeCAD command definition""" @@ -2373,7 +2337,6 @@ FreeCADGui.addCommand('Draft_Mirror',Mirror()) FreeCADGui.addCommand('Draft_Stretch',Stretch()) # context commands -FreeCADGui.addCommand('Draft_ApplyStyle',ApplyStyle()) FreeCADGui.addCommand('Draft_Shape2DView',Shape2DView()) # a global place to look for active draft Command diff --git a/src/Mod/Draft/draftguitools/gui_styles.py b/src/Mod/Draft/draftguitools/gui_styles.py new file mode 100644 index 0000000000..0f5e744fc8 --- /dev/null +++ b/src/Mod/Draft/draftguitools/gui_styles.py @@ -0,0 +1,96 @@ +# *************************************************************************** +# * (c) 2009 Yorik van Havre * +# * (c) 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 tools for applying styles to objects in the Draft Workbench.""" +## @package gui_styles +# \ingroup DRAFT +# \brief Provides tools for applying styles to objects in the Draft Workbench. + +from PySide.QtCore import QT_TRANSLATE_NOOP + +import FreeCADGui as Gui +import draftguitools.gui_base_original as gui_base_original +from draftutils.translate import translate, _tr + + +class ApplyStyle(gui_base_original.Modifier): + """Gui Command for the ApplyStyle tool.""" + + def GetResources(self): + """Set icon, menu and tooltip.""" + _menu = "Apply current style" + _tip = ("Applies the current style defined in the toolbar " + "(line width and colors) " + "to the selected objects and groups.") + + return {'Pixmap': 'Draft_Apply', + 'MenuText': QT_TRANSLATE_NOOP("Draft_ApplyStyle", _menu), + 'ToolTip': QT_TRANSLATE_NOOP("Draft_ApplyStyle", _tip)} + + def Activated(self): + """Execute when the command is called. + + Activate the specific BSpline tracker. + """ + super().Activated(name=_tr("Apply style")) + if self.ui: + self.sel = Gui.Selection.getSelection() + if len(self.sel) > 0: + Gui.addModule("Draft") + _cmd_list = [] + for obj in self.sel: + # TODO: instead of `TypeId`, use `utils.get_type` + # to get the type of the object and apply different + # formatting information depending on the type of object. + # The groups may also be things like `App::Parts` + # or `Arch_BuildingParts`. + if obj.TypeId == "App::DocumentObjectGroup": + _cmd_list.extend(self.formatGroup(obj)) + else: + _cmd = 'Draft.formatObject' + _cmd += '(' + _cmd += 'FreeCAD.ActiveDocument.' + obj.Name + _cmd += ')' + _cmd_list.append(_cmd) + self.commit(translate("draft", "Change Style"), + _cmd_list) + super().finish() + + def formatGroup(self, group): + """Format a group instead of simple object.""" + Gui.addModule("Draft") + _cmd_list = [] + for obj in group.Group: + if obj.TypeId == "App::DocumentObjectGroup": + _cmd_list.extend(self.formatGroup(obj)) + else: + _cmd = 'Draft.formatObject' + _cmd += '(' + _cmd += 'FreeCAD.ActiveDocument.' + obj.Name + _cmd += ')' + _cmd_list.append(_cmd) + return _cmd_list + + +Gui.addCommand('Draft_ApplyStyle', ApplyStyle())