Debian Codegraph @

src/Gui/CommandMacro.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QApplication>
24 #include <QDesktopServices>
25 #include <QUrl>
26
27 #include "Command.h"
28 #include "Action.h"
29 #include "Application.h"
30 #include "BitmapFactory.h"
31 #include "Dialogs/DlgMacroExecuteImp.h"
32 #include "Dialogs/DlgMacroRecordImp.h"
33 #include "Macro.h"
34 #include "MainWindow.h"
35 #include "PythonDebugger.h"
36
37
38 using namespace Gui;
39
40
41 //===========================================================================
42 // Std_DlgMacroRecord
43 //===========================================================================
44 DEF_STD_CMD_A(StdCmdDlgMacroRecord)
45
46 StdCmdDlgMacroRecord::StdCmdDlgMacroRecord()
47 : Command("Std_DlgMacroRecord")
48 {
49 sGroup = "Macro";
50 sMenuText = QT_TR_NOOP("Record &Macro");
51
52 sToolTipText = QT_TR_NOOP("Opens a dialog to record a macro");
53 sWhatsThis = "Std_DlgMacroRecord";
54 sStatusTip = sToolTipText;
55 sPixmap = "media-record";
56 eType = 0;
57 }
58
59 void StdCmdDlgMacroRecord::activated(int iMsg)
60 {
61 Q_UNUSED(iMsg);
62 if (!getGuiApplication()->macroManager()->isOpen()) {
63 Gui::Dialog::DlgMacroRecordImp cDlg(getMainWindow());
64 if (cDlg.exec() && getAction()) {
65 getAction()->setIcon(Gui::BitmapFactory().iconFromTheme("media-playback-stop"));
66 getAction()->setText(
67 QCoreApplication::translate("StdCmdDlgMacroRecord", "S&top macro recording")
68 );
69 getAction()->setToolTip(
70 QCoreApplication::translate("StdCmdDlgMacroRecord", "Stop the macro recording session")
71 );
72 }
73 }
74 else {
75 getGuiApplication()->macroManager()->commit();
76 if (getAction()) {
77 getAction()->setIcon(Gui::BitmapFactory().iconFromTheme("media-record"));
78 getAction()->setText(QString::fromLatin1(sMenuText));
79 getAction()->setToolTip(QString::fromLatin1(sToolTipText));
80 }
81 }
82 }
83
84 bool StdCmdDlgMacroRecord::isActive()
85 {
86 return true;
87 }
88
89 //===========================================================================
90 // Std_DlgMacroExecute
91 //===========================================================================
92 DEF_STD_CMD_A(StdCmdDlgMacroExecute)
93
94 StdCmdDlgMacroExecute::StdCmdDlgMacroExecute()
95 : Command("Std_DlgMacroExecute")
96 {
97 sGroup = "Macro";
98 sMenuText = QT_TR_NOOP("Ma&cros");
99
100 sToolTipText = QT_TR_NOOP("Opens a dialog to execute a recorded macro");
101 sWhatsThis = "Std_DlgMacroExecute";
102 sStatusTip = sToolTipText;
103 sPixmap = "accessories-text-editor";
104 eType = 0;
105 }
106
107 void StdCmdDlgMacroExecute::activated(int iMsg)
108 {
109 Q_UNUSED(iMsg);
110 Gui::Dialog::DlgMacroExecuteImp cDlg(getMainWindow());
111 cDlg.exec();
112 }
113
114 bool StdCmdDlgMacroExecute::isActive()
115 {
116 return !(getGuiApplication()->macroManager()->isOpen());
117 }
118
119 //===========================================================================
120 // Std_DlgMacroExecuteDirect
121 //===========================================================================
122 DEF_STD_CMD_A(StdCmdDlgMacroExecuteDirect)
123
124 StdCmdDlgMacroExecuteDirect::StdCmdDlgMacroExecuteDirect()
125 : Command("Std_DlgMacroExecuteDirect")
126 {
127 sGroup = "Macro";
128 sMenuText = QT_TR_NOOP("&Execute Macro");
129 sToolTipText = QT_TR_NOOP("Executes the macro in the editor");
130 sWhatsThis = "Std_DlgMacroExecuteDirect";
131 sStatusTip = sToolTipText;
132 sPixmap = "media-playback-start";
133 sAccel = "Ctrl+F6";
134 eType = 0;
135 }
136
137 void StdCmdDlgMacroExecuteDirect::activated(int iMsg)
138 {
139 Q_UNUSED(iMsg);
140 doCommand(Command::Gui, "Gui.SendMsgToActiveView(\"Run\")");
141 }
142
143 bool StdCmdDlgMacroExecuteDirect::isActive()
144 {
145 return getGuiApplication()->sendHasMsgToActiveView("Run");
146 }
147
148 DEF_STD_CMD_A(StdCmdMacroAttachDebugger)
149
150 StdCmdMacroAttachDebugger::StdCmdMacroAttachDebugger()
151 : Command("Std_MacroAttachDebugger")
152 {
153 sGroup = "Macro";
154 sMenuText = QT_TR_NOOP("&Attach to Remote Debugger");
155
156 sToolTipText = QT_TR_NOOP("Attaches to a remotely running debugger");
157 sWhatsThis = "Std_MacroAttachDebugger";
158 sStatusTip = sToolTipText;
159 eType = 0;
160 }
161
162 void StdCmdMacroAttachDebugger::activated(int iMsg)
163 {
164 Q_UNUSED(iMsg);
165 doCommand(
166 Gui,
167 "from freecad.gui import RemoteDebugger\n"
168 "RemoteDebugger.attachToRemoteDebugger()"
169 );
170 }
171
172 bool StdCmdMacroAttachDebugger::isActive()
173 {
174 return true;
175 }
176
177 DEF_STD_CMD_A(StdCmdMacroStartDebug)
178
179 StdCmdMacroStartDebug::StdCmdMacroStartDebug()
180 : Command("Std_MacroStartDebug")
181 {
182 sGroup = "Macro";
183 sMenuText = QT_TR_NOOP("&Debug Macro");
184 sToolTipText = QT_TR_NOOP("Starts the debugging of macros");
185 sWhatsThis = "Std_MacroStartDebug";
186 sStatusTip = sToolTipText;
187 sPixmap = "debug-start";
188 sAccel = "F6";
189 eType = 0;
190 }
191
192 void StdCmdMacroStartDebug::activated(int iMsg)
193 {
194 Q_UNUSED(iMsg);
195 PythonDebugger* dbg = Application::Instance->macroManager()->debugger();
196 if (!dbg->isRunning()) {
197 doCommand(Command::Gui, "Gui.SendMsgToActiveView(\"StartDebug\")");
198 }
199 else {
200 dbg->stepRun();
201 }
202 }
203
204 bool StdCmdMacroStartDebug::isActive()
205 {
206 return getGuiApplication()->sendHasMsgToActiveView("StartDebug");
207 }
208
209 DEF_STD_CMD_A(StdCmdMacroStopDebug)
210
211 StdCmdMacroStopDebug::StdCmdMacroStopDebug()
212 : Command("Std_MacroStopDebug")
213 {
214 sGroup = "Macro";
215 sMenuText = QT_TR_NOOP("&Stop Debugging");
216 sToolTipText = QT_TR_NOOP("Stops the debugging of macros");
217 sWhatsThis = "Std_MacroStopDebug";
218 sStatusTip = sToolTipText;
219 sPixmap = "debug-stop";
220 sAccel = "Shift+F6";
221 eType = 0;
222 }
223
224 void StdCmdMacroStopDebug::activated(int iMsg)
225 {
226 Q_UNUSED(iMsg);
227 Application::Instance->macroManager()->debugger()->tryStop();
228 }
229
230 bool StdCmdMacroStopDebug::isActive()
231 {
232 static PythonDebugger* dbg = Application::Instance->macroManager()->debugger();
233 return dbg->isRunning();
234 }
235
236 DEF_STD_CMD_A(StdCmdMacroStepOver)
237
238 StdCmdMacroStepOver::StdCmdMacroStepOver()
239 : Command("Std_MacroStepOver")
240 {
241 sGroup = "Macro";
242 sMenuText = QT_TR_NOOP("Step &Over");
243 sToolTipText = QT_TR_NOOP("Steps to the next line in this file");
244 sWhatsThis = "Std_MacroStepOver";
245 sStatusTip = sToolTipText;
246 sPixmap = nullptr;
247 sAccel = "F10";
248 eType = 0;
249 }
250
251 void StdCmdMacroStepOver::activated(int iMsg)
252 {
253 Q_UNUSED(iMsg);
254 Application::Instance->macroManager()->debugger()->stepOver();
255 }
256
257 bool StdCmdMacroStepOver::isActive()
258 {
259 static PythonDebugger* dbg = Application::Instance->macroManager()->debugger();
260 return dbg->isRunning();
261 }
262
263 DEF_STD_CMD_A(StdCmdMacroStepInto)
264
265 StdCmdMacroStepInto::StdCmdMacroStepInto()
266 : Command("Std_MacroStepInto")
267 {
268 sGroup = "Macro";
269 sMenuText = QT_TR_NOOP("Step &Into");
270 sToolTipText = QT_TR_NOOP("Steps to the next line executed");
271 sWhatsThis = "Std_MacroStepInto";
272 sStatusTip = sToolTipText;
273 sPixmap = nullptr;
274 sAccel = "F11";
275 eType = 0;
276 }
277
278 void StdCmdMacroStepInto::activated(int iMsg)
279 {
280 Q_UNUSED(iMsg);
281 Application::Instance->macroManager()->debugger()->stepInto();
282 }
283
284 bool StdCmdMacroStepInto::isActive()
285 {
286 static PythonDebugger* dbg = Application::Instance->macroManager()->debugger();
287 return dbg->isRunning();
288 }
289
290 DEF_STD_CMD_A(StdCmdToggleBreakpoint)
291
292 StdCmdToggleBreakpoint::StdCmdToggleBreakpoint()
293 : Command("Std_ToggleBreakpoint")
294 {
295 sGroup = "Macro";
296 sMenuText = QT_TR_NOOP("Toggle &Breakpoint");
297 sToolTipText = QT_TR_NOOP("Adds or removes a breakpoint at this position");
298 sWhatsThis = "Std_ToggleBreakpoint";
299 sStatusTip = sToolTipText;
300 sPixmap = nullptr;
301 sAccel = "F9";
302 eType = 0;
303 }
304
305 void StdCmdToggleBreakpoint::activated(int iMsg)
306 {
307 Q_UNUSED(iMsg);
308 doCommand(Command::Gui, "Gui.SendMsgToActiveView(\"ToggleBreakpoint\")");
309 }
310
311 bool StdCmdToggleBreakpoint::isActive()
312 {
313 return getGuiApplication()->sendHasMsgToActiveView("ToggleBreakpoint");
314 }
315
316 DEF_STD_CMD_A(StdCmdMacrosFolder)
317
318 StdCmdMacrosFolder::StdCmdMacrosFolder()
319 : Command("Std_OpenMacrosFolder")
320 {
321 sGroup = "Macro";
322 sMenuText = QT_TR_NOOP("Open Macro Folder");
323 sToolTipText = QT_TR_NOOP("Opens the macros folder in the system file manager");
324 sWhatsThis = "Std_OpenMacrosFolder";
325 sStatusTip = sToolTipText;
326 sPixmap = "MacroFolder";
327 sAccel = "";
328 eType = 0;
329 }
330
331 void StdCmdMacrosFolder::activated(int iMsg)
332 {
333 Q_UNUSED(iMsg);
334
335 QString path = QString::fromStdString(App::Application::getUserMacroDir());
336 QUrl url = QUrl::fromLocalFile(path);
337 QDesktopServices::openUrl(url);
338 }
339
340 bool StdCmdMacrosFolder::isActive()
341 {
342 return true;
343 }
344
345 namespace Gui
346 {
347
348 void CreateMacroCommands()
349 {
350 CommandManager& rcCmdMgr = Application::Instance->commandManager();
351 rcCmdMgr.addCommand(new StdCmdDlgMacroRecord());
352 rcCmdMgr.addCommand(new StdCmdDlgMacroExecute());
353 rcCmdMgr.addCommand(new StdCmdMacrosFolder());
354 rcCmdMgr.addCommand(new StdCmdDlgMacroExecuteDirect());
355 rcCmdMgr.addCommand(new StdCmdMacroAttachDebugger());
356 rcCmdMgr.addCommand(new StdCmdMacroStartDebug());
357 rcCmdMgr.addCommand(new StdCmdMacroStopDebug());
358 rcCmdMgr.addCommand(new StdCmdMacroStepOver());
359 rcCmdMgr.addCommand(new StdCmdMacroStepInto());
360 rcCmdMgr.addCommand(new StdCmdToggleBreakpoint());
361 }
362
363 } // namespace Gui