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.
This commit is contained in:
vocx-fc
2020-04-03 13:19:10 -06:00
committed by Yorik van Havre
parent a2a3cd18c2
commit 73e1027445
3 changed files with 98 additions and 38 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,96 @@
# ***************************************************************************
# * (c) 2009 Yorik van Havre <yorik@uncreated.net> *
# * (c) 2010 Ken Cline <cline@frii.com> *
# * (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
# * *
# * 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())