FreeCAD
172 строки · 6.5 Кб
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 create simple Text objects.
26
27The textual block can consist of multiple lines.
28"""
29## @package gui_texts
30# \ingroup draftguitools
31# \brief Provides GUI tools to create simple Text objects.
32
33## \addtogroup draftguitools
34# @{
35import sys
36from PySide.QtCore import QT_TRANSLATE_NOOP
37
38import FreeCAD as App
39import FreeCADGui as Gui
40import Draft_rc
41import DraftVecUtils
42import draftguitools.gui_base_original as gui_base_original
43import draftguitools.gui_tool_utils as gui_tool_utils
44
45from draftutils.translate import translate
46from draftutils.messages import _toolmsg
47
48# The module is used to prevent complaints from code checkers (flake8)
49True if Draft_rc.__name__ else False
50
51
52class Text(gui_base_original.Creator):
53"""Gui command for the Text tool."""
54
55def GetResources(self):
56"""Set icon, menu and tooltip."""
57
58return {'Pixmap': 'Draft_Text',
59'Accel': "T, E",
60'MenuText': QT_TRANSLATE_NOOP("Draft_Text", "Text"),
61'ToolTip': QT_TRANSLATE_NOOP("Draft_Text", "Creates a multi-line annotation. CTRL to snap.")}
62
63def Activated(self):
64"""Execute when the command is called."""
65super().Activated(name="Text")
66if self.ui:
67self.text = ''
68self.ui.sourceCmd = self
69self.ui.pointUi(title=translate("draft", self.featureName), icon="Draft_Text")
70self.ui.isRelative.hide()
71self.ui.continueCmd.show()
72self.call = self.view.addEventCallback("SoEvent", self.action)
73self.active = True
74self.ui.xValue.setFocus()
75self.ui.xValue.selectAll()
76_toolmsg(translate("draft", "Pick location point"))
77
78def finish(self, cont=False):
79"""Terminate the operation.
80
81Parameters
82----------
83cont: bool or None, optional
84Restart (continue) the command if `True`, or if `None` and
85`ui.continueMode` is `True`.
86"""
87self.end_callbacks(self.call)
88super().finish(self)
89if cont or (cont is None and self.ui and self.ui.continueMode):
90self.Activated()
91
92def createObject(self):
93"""Create the actual object in the current document."""
94rot, sup, pts, fil = self.getStrings()
95base = pts[1:-1]
96
97text_list = self.text
98
99if not text_list:
100self.finish()
101return None
102
103# If the last element is an empty string "" we remove it
104if not text_list[-1]:
105text_list.pop()
106
107t_list = ['"' + line + '"' for line in text_list]
108
109list_as_text = ", ".join(t_list)
110
111string = '[' + list_as_text + ']'
112
113Gui.addModule("Draft")
114_cmd = 'Draft.make_text'
115_cmd += '('
116_cmd += string + ', '
117_cmd += 'placement=pl, '
118_cmd += 'screen=None, height=None, line_spacing=None'
119_cmd += ')'
120_cmd_list = ['pl = FreeCAD.Placement()',
121'pl.Rotation.Q = ' + rot,
122'pl.Base = ' + base,
123'_text_ = ' + _cmd,
124'Draft.autogroup(_text_)',
125'FreeCAD.ActiveDocument.recompute()']
126self.commit(translate("draft", "Create Text"),
127_cmd_list)
128self.finish(cont=None)
129
130def action(self, arg):
131"""Handle the 3D scene events.
132
133This is installed as an EventCallback in the Inventor view.
134
135Parameters
136----------
137arg: dict
138Dictionary with strings that indicates the type of event received
139from the 3D view.
140"""
141if arg["Type"] == "SoKeyboardEvent":
142if arg["Key"] == "ESCAPE":
143self.finish()
144elif arg["Type"] == "SoLocation2Event": # mouse movement detection
145if self.active:
146(self.point,
147ctrlPoint, info) = gui_tool_utils.getPoint(self, arg)
148gui_tool_utils.redraw3DView()
149elif arg["Type"] == "SoMouseButtonEvent":
150if arg["State"] == "DOWN" and arg["Button"] == "BUTTON1":
151if self.point:
152self.active = False
153Gui.Snapper.off()
154self.node.append(self.point)
155self.ui.textUi()
156self.ui.textValue.setFocus()
157
158def numericInput(self, numx, numy, numz):
159"""Validate the entry fields in the user interface.
160
161This function is called by the toolbar or taskpanel interface
162when valid x, y, and z have been entered in the input fields.
163"""
164self.point = App.Vector(numx, numy, numz)
165self.node.append(self.point)
166self.ui.textUi()
167self.ui.textValue.setFocus()
168
169
170Gui.addCommand('Draft_Text', Text())
171
172## @}
173