Debian Codegraph @

src/Gui/Macro.h

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Jürgen Riegel <[email protected]> *
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
24 #ifndef GUI_MACRO_H
25 #define GUI_MACRO_H
26
27 #include <stack>
28 #include <tuple>
29 #include <QString>
30 #include <QStringList>
31 #include <Base/Observer.h>
32 #include <Base/Parameter.h>
33
34
35 namespace Gui
36 {
37 struct ApplicationP;
38 class PythonConsole;
39 class PythonDebugger;
40
41 class MacroFile
42 {
43 public:
44 MacroFile();
45 void open(const char* sName);
46 /// indicates if a macro recording is in progress
47 bool isOpen() const
48 {
49 return openMacro;
50 }
51 void append(const QString&);
52 void append(const QStringList&);
53 QString fileName() const
54 {
55 return macroName;
56 }
57 bool commit();
58 void cancel();
59
60 private:
61 QStringList macroInProgress; /**< Container for the macro */
62 QString macroName; /**< name of the macro */
63 bool openMacro {false};
64 };
65
66 class MacroOutputBuffer
67 {
68 public:
69 MacroOutputBuffer();
70 /// Return the added lines regardless of recording or not
71 long getLines() const
72 {
73 return totalLines;
74 }
75 /// insert a new pending line in the macro
76 void addPendingLine(int type, const char* line);
77 bool addPendingLineIfComment(int type, const char* line);
78 bool hasPendingLines() const
79 {
80 return !pendingLine.empty();
81 }
82 void incrementIfNoComment(int type);
83
84 long totalLines {0};
85 std::vector<std::pair<int, std::string>> pendingLine;
86 };
87
88 class MacroOutputOption
89 {
90 public:
91 MacroOutputOption();
92 std::tuple<bool, bool> values(int type) const;
93
94 static bool isComment(int type);
95 static bool isGuiCommand(int type);
96 static bool isAppCommand(int type);
97
98 bool recordGui {true};
99 bool guiAsComment {true};
100 bool scriptToPyConsole {true};
101 };
102
103 /** Macro recording and play back management
104 * The purpos of this class is to handle record function calls from a command and save it in
105 * a macro file (so far).
106 * \author Jürgen Riegel
107 */
108 class GuiExport MacroManager: public Base::Observer<const char*>
109 {
110 protected:
111 MacroManager();
112 ~MacroManager() override;
113
114 public:
115 /** Macro type enumeration */
116 enum MacroType
117 {
118 File, /**< The macro will be saved in a file */
119 User, /**< The macro belongs to the Application and will be saved in the UserParameter */
120 Doc /**< The macro belongs to the Document and will be saved and restored with the Document */
121 };
122
123 /** Line type enumeration */
124 enum LineType
125 {
126 App, /**< The line effects only the document and Application (FreeCAD) */
127 Gui, /**< The line effects the Gui (FreeCADGui) */
128 Cmt, /**< The line is handled as a comment */
129 };
130
131 /** Redirect the macro output temporarily.
132 *
133 * This is a RAII class to redirect the macro output to a function.
134 * Initializing an instance with a redirect function, ensures that
135 * MacroManager::addLine() calls are redirected by means of the redirect
136 * function. More than one instances can be used, overriding earlier
137 * instances and their redirect functions.
138 *
139 * @code
140 * void myRedirectFunc1(MacroManager::LineType type, const char* line)
141 * { // do something with the line }
142 *
143 * void myRedirectFunc2(MacroManager::LineType type, const char* line)
144 * { // do something else with the line }
145 *
146 * {
147 * MacroRedirector redirect(myRedirectFunc1);
148 * // all macro output will go to myRedirectFunc1
149 * {
150 * MacroRedirector redirect2(myRedirectFunc2);
151 * // all macro output will go to myRedirectFunc2
152 * }
153 * // all macro output will go to myRedirectFunc1
154 * }
155 * // normal macro output is restored
156 * @endcode
157 */
158 class MacroRedirector
159 {
160 public:
161 explicit MacroRedirector(const std::function<void(LineType, const char*)>& func)
162 {
163 MacroManager::redirectFuncs.push(func);
164 }
165
166 ~MacroRedirector()
167 {
168 MacroManager::redirectFuncs.pop();
169 }
170 };
171
172 /** Opens a new Macro recording session
173 * Starts a session with the type and the name of the macro.
174 * All user interactions will be recorded as long as the commit() or cancel() isn't called.
175 * There is only one recording session possible. Trying to open a second one causes an exception:
176 * @param eType Type of the macro
177 * @param sName Name or path of the macro
178 * @see commit()
179 * @see cancel()
180 */
181 void open(MacroType eType, const char* sName);
182 /// close (and save) the recording session
183 void commit();
184 /// cancels the recording session
185 void cancel();
186 /// indicates if a macro recording is in progress
187 bool isOpen() const
188 {
189 return macroFile.isOpen();
190 }
191
192 /** Insert a new line in the macro.
193 *
194 * The line is added to the macro unless the output is redirected.
195 * @see MacroRedirector.
196 *
197 * @param Type The type of the line
198 * @param sLine The line to add
199 */
200 void addLine(LineType Type, const char* sLine);
201 /// insert a new pending line in the macro
202 void addPendingLine(LineType type, const char* line);
203 /** Set the active module
204 * This is normally done by the workbench switch. It sets
205 * the actually active application module so when the macro
206 * gets started the right import can be issued.
207 */
208 void setModule(const char* sModule);
209 void run(MacroType eType, const char* sName);
210 /// Get the Python debugger
211 PythonDebugger* debugger() const;
212 PythonConsole* getPythonConsole() const;
213 /** Observes its parameter group. */
214 void OnChange(Base::Subject<const char*>& rCaller, const char* sReason) override;
215
216 /// Return the added lines regardless of recording or not
217 long getLines() const
218 {
219 return buffer.getLines();
220 }
221
222 private:
223 void processPendingLines();
224 void makeComment(QStringList& lines) const;
225 void addToOutput(LineType type, const char* line);
226
227 private:
228 MacroFile macroFile;
229 MacroOutputBuffer buffer;
230 MacroOutputOption option;
231 bool localEnv {true};
232 mutable PythonConsole* pyConsole {nullptr}; // link to the python console
233 PythonDebugger* pyDebugger;
234 Base::Reference<ParameterGrp> params; // link to the Macro parameter group
235 static std::stack<std::function<void(LineType, const char*)>> redirectFuncs;
236
237 friend struct ApplicationP;
238 };
239
240 } // namespace Gui
241
242
243 #endif // GUI_MACRO_H