FreeCAD
160 строк · 6.6 Кб
1# ***************************************************************************
2# * (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
3# * (c) 2009, 2010 Ken Cline <cline@frii.com> *
4# * (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
5# * *
6# * This file is part of the FreeCAD CAx development system. *
7# * *
8# * This program is free software; you can redistribute it and/or modify *
9# * it under the terms of the GNU Lesser General Public License (LGPL) *
10# * as published by the Free Software Foundation; either version 2 of *
11# * the License, or (at your option) any later version. *
12# * for detail see the LICENCE text file. *
13# * *
14# * FreeCAD is distributed in the hope that it will be useful, *
15# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17# * GNU Library General Public License for more details. *
18# * *
19# * You should have received a copy of the GNU Library General Public *
20# * License along with FreeCAD; if not, write to the Free Software *
21# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22# * USA *
23# * *
24# ***************************************************************************
25"""Provides GUI tools to set different modes of other tools.
26
27For example, a construction mode, a continue mode to repeat commands,
28and to toggle the appearance of certain shapes to wireframe.
29"""
30## @package gui_togglemodes
31# \ingroup draftguitools
32# \brief Provides GUI tools to set different modes of other tools.
33
34## \addtogroup draftguitools
35# @{
36from PySide.QtCore import QT_TRANSLATE_NOOP37
38import FreeCADGui as Gui39import Draft_rc40import draftguitools.gui_base as gui_base41
42from draftutils.messages import _msg43from draftutils.translate import translate44
45# The module is used to prevent complaints from code checkers (flake8)
46True if Draft_rc.__name__ else False47
48
49class BaseMode(gui_base.GuiCommandSimplest):50"""Base class for mode context GuiCommands.51
52This is inherited by the other GuiCommand classes to run
53a set of similar actions when changing modes.
54
55It inherits `GuiCommandSimplest` to set up the document
56and other behavior. See this class for more information.
57"""
58
59def Activated(self, mode="None"):60"""Execute when the command is called.61
62Parameters
63----------
64action: str
65Indicates the type of mode to switch to.
66It can be `'construction'` or `'continue'`.
67"""
68super(BaseMode, self).Activated()69
70if hasattr(Gui, "draftToolBar"):71_ui = Gui.draftToolBar72else:73_msg(translate("draft","No active Draft Toolbar."))74return75
76if _ui is not None:77if mode == "construction" and hasattr(_ui, "constrButton"):78_ui.constrButton.toggle()79elif mode == "continue":80_ui.toggleContinue()81
82
83class ToggleConstructionMode(BaseMode):84"""GuiCommand for the Draft_ToggleConstructionMode tool.85
86When construction mode is active, the following objects created
87will be included in the construction group, and will be drawn
88with the specified color and properties.
89"""
90
91def __init__(self):92super(ToggleConstructionMode,93self).__init__(name=translate("draft","Construction mode"))94
95def GetResources(self):96"""Set icon, menu and tooltip."""97
98d = {'Pixmap': 'Draft_Construction',99'MenuText': QT_TRANSLATE_NOOP("Draft_ToggleConstructionMode","Toggle construction mode"),100'Accel': "C, M",101'ToolTip': QT_TRANSLATE_NOOP("Draft_ToggleConstructionMode","Toggles the Construction mode.\nWhen this is active, the following objects created will be included in the construction group, and will be drawn with the specified color and properties.")}102return d103
104def Activated(self):105"""Execute when the command is called.106
107It calls the `toggle()` method of the construction button
108in the `DraftToolbar` class.
109"""
110super(ToggleConstructionMode, self).Activated(mode="construction")111
112
113Gui.addCommand('Draft_ToggleConstructionMode', ToggleConstructionMode())114
115
116class ToggleDisplayMode(gui_base.GuiCommandNeedsSelection):117"""GuiCommand for the Draft_ToggleDisplayMode tool.118
119Switches the display mode of selected objects from flatlines
120to wireframe and back.
121
122It inherits `GuiCommandNeedsSelection` to only be available
123when there is a document and a selection.
124See this class for more information.
125"""
126
127def __init__(self):128super(ToggleDisplayMode,129self).__init__(name=translate("draft","Toggle display mode"))130
131def GetResources(self):132"""Set icon, menu and tooltip."""133
134d = {'Pixmap': 'Draft_SwitchMode',135'Accel': "Shift+Space",136'MenuText': QT_TRANSLATE_NOOP("Draft_ToggleDisplayMode","Toggle normal/wireframe display"),137'ToolTip': QT_TRANSLATE_NOOP("Draft_ToggleDisplayMode","Switches the display mode of selected objects from flatlines to wireframe and back.\nThis is helpful to quickly visualize objects that are hidden by other objects.\nThis is intended to be used with closed shapes and solids, and doesn't affect open wires.")}138return d139
140def Activated(self):141"""Execute when the command is called.142
143It tests the view provider of the selected objects
144and changes their `DisplayMode` from `'Wireframe'`
145to `'Flat Lines'`, and the other way around, if possible.
146"""
147super(ToggleDisplayMode, self).Activated()148
149for obj in Gui.Selection.getSelection():150if obj.ViewObject.DisplayMode == "Flat Lines":151if "Wireframe" in obj.ViewObject.listDisplayModes():152obj.ViewObject.DisplayMode = "Wireframe"153elif obj.ViewObject.DisplayMode == "Wireframe":154if "Flat Lines" in obj.ViewObject.listDisplayModes():155obj.ViewObject.DisplayMode = "Flat Lines"156
157
158Gui.addCommand('Draft_ToggleDisplayMode', ToggleDisplayMode())159
160## @}
161