Debian Codegraph @

src/Gui/CommandT.h

raw
1 /***************************************************************************
2 * Copyright (c) 2019 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
24 #ifndef GUI_COMMAND_T_H
25 #define GUI_COMMAND_T_H
26
27 #include <App/Document.h>
28 #include <App/DocumentObject.h>
29 #include <Base/Exception.h>
30 #include <Gui/Command.h>
31 #include <type_traits>
32 #include <typeinfo>
33 #include <boost/format.hpp>
34
35
36 namespace Gui
37 {
38
39 class FormatString
40 {
41 public:
42 static std::string str(const std::string& s)
43 {
44 return s;
45 }
46 static std::string str(const char* s)
47 {
48 return s;
49 }
50 static std::string str(const QString& s)
51 {
52 return s.toStdString();
53 }
54 static std::string str(const std::stringstream& s)
55 {
56 return s.str();
57 }
58 static std::string str(const std::ostringstream& s)
59 {
60 return s.str();
61 }
62 static std::string str(const std::ostream& s)
63 {
64 if (typeid(s) == typeid(std::ostringstream)) {
65 return dynamic_cast<const std::ostringstream&>(s).str();
66 }
67 else if (typeid(s) == typeid(std::stringstream)) {
68 return dynamic_cast<const std::stringstream&>(s).str();
69 }
70 throw Base::TypeError("Not a std::stringstream or std::ostringstream");
71 }
72 static std::string toStr(boost::format& f)
73 {
74 return f.str();
75 }
76
77 template<typename T, typename... Args>
78 static std::string toStr(boost::format& f, T&& t, Args&&... args)
79 {
80 return toStr(f % std::forward<T>(t), std::forward<Args>(args)...);
81 }
82 };
83
84 /** @defgroup CommandFuncs Helper functions for running commands through Python interpreter */
85 //@{
86
87 /** Runs a command for accessing document attribute or method
88 * This function is an alternative to _FCMD_DOC_CMD
89 * @param doc: pointer to a document
90 * @param mod: module name, "Gui" or "App"
91 * @param cmd: command string, streamable
92 *
93 * Example:
94 * @code{.cpp}
95 * _cmdDocument(Gui::Command::Gui, doc, "Gui", std::stringstream() << "getObject('" << objName
96 * << "')");
97 * @endcode
98 *
99 * Translates to command (assuming doc's name is 'DocName', and
100 * and objName contains value 'ObjName'):
101 * @code{.py}
102 * Gui.getDocument('DocName').getObject('ObjName')
103 * @endcode
104 */
105 template<typename T>
106 void _cmdDocument(
107 Gui::Command::DoCmd_Type cmdType,
108 const App::Document* doc,
109 const std::string& mod,
110 T&& cmd
111 )
112 {
113 if (doc && doc->getName()) {
114 std::stringstream str;
115 str << mod << ".getDocument('" << doc->getName() << "')." << FormatString::str(cmd);
116 Gui::Command::runCommand(cmdType, str.str().c_str());
117 }
118 }
119
120 /** Runs a command for accessing document attribute or method
121 * This function is an alternative to _FCMD_DOC_CMD
122 * @param doc: document name
123 * @param mod: module name, "Gui" or "App"
124 * @param cmd: command string, streamable
125 *
126 * Example:
127 * @code{.cpp}
128 * _cmdDocument(Gui::Command::Gui, doc, "Gui", std::stringstream() << "getObject('" << objName
129 * << "')");
130 * @endcode
131 *
132 * Translates to command (assuming doc's name is 'DocName', and
133 * and objName contains value 'ObjName'):
134 * @code{.py}
135 * Gui.getDocument('DocName').getObject('ObjName')
136 * @endcode
137 */
138 template<typename T>
139 void _cmdDocument(Gui::Command::DoCmd_Type cmdType, const std::string& doc, const std::string& mod, T&& cmd)
140 {
141 if (!doc.empty()) {
142 std::stringstream str;
143 str << mod << ".getDocument('" << doc << "')." << FormatString::str(cmd);
144 Gui::Command::runCommand(cmdType, str.str().c_str());
145 }
146 }
147
148 /** Runs a command for accessing App.Document attribute or method
149 * This function is an alternative to FCMD_DOC_CMD
150 *
151 * @param doc: pointer to a document
152 * @param cmd: command string, streamable
153 * @sa _cmdDocument()
154 *
155 * Example:
156 * @code{.cpp}
157 * cmdAppDocument(doc, std::stringstream() << "getObject('" << objName << "')");
158 * @endcode
159 *
160 * Translates to command (assuming doc's name is 'DocName', and
161 * and objName contains value 'ObjName'):
162 * @code{.py}
163 * App.getDocument('DocName').getObject('ObjName')
164 * @endcode
165 */
166 template<typename T>
167 inline void cmdAppDocument(const App::Document* doc, T&& cmd)
168 {
169 _cmdDocument(Gui::Command::Doc, doc, "App", std::forward<T>(cmd));
170 }
171
172 /** Runs a command for accessing App.Document attribute or method
173 * This function is an alternative to FCMD_DOC_CMD
174 *
175 * @param doc: document name
176 * @param cmd: command string, streamable
177 * @sa _cmdDocument()
178 *
179 * Example:
180 * @code{.cpp}
181 * cmdAppDocument(doc, std::stringstream() << "getObject('" << objName << "')");
182 * @endcode
183 *
184 * Translates to command (assuming doc's name is 'DocName', and
185 * and objName contains value 'ObjName'):
186 * @code{.py}
187 * App.getDocument('DocName').getObject('ObjName')
188 * @endcode
189 */
190 template<typename T>
191 inline void cmdAppDocument(const std::string& doc, T&& cmd)
192 {
193 _cmdDocument(Gui::Command::Doc, doc, "App", std::forward<T>(cmd));
194 }
195
196 /** Runs a command for accessing App.Document attribute or method
197 *
198 * @param doc: pointer to a document
199 * @param cmd: command string, streamable
200 * @sa _cmdDocument()
201 *
202 * Example:
203 * @code{.cpp}
204 * cmdGuiDocument(doc, std::stringstream() << "getObject('" << objName << "')");
205 * @endcode
206 *
207 * Translates to command (assuming doc's name is 'DocName', and
208 * and objName contains value 'ObjName'):
209 * @code{.py}
210 * Gui.getDocument('DocName').getObject('ObjName')
211 * @endcode
212 */
213 template<typename T>
214 inline void cmdGuiDocument(const App::Document* doc, T&& cmd)
215 {
216 _cmdDocument(Gui::Command::Gui, doc, "Gui", std::forward<T>(cmd));
217 }
218
219 /** Runs a command for accessing App.Document attribute or method
220 *
221 * @param doc: document name
222 * @param cmd: command string, streamable
223 * @sa _cmdDocument()
224 *
225 * Example:
226 * @code{.cpp}
227 * cmdGuiDocument(doc, std::stringstream() << "getObject('" << objName << "')");
228 * @endcode
229 *
230 * Translates to command (assuming doc's name is 'DocName', and
231 * and objName contains value 'ObjName'):
232 * @code{.py}
233 * Gui.getDocument('DocName').getObject('ObjName')
234 * @endcode
235 */
236 template<typename T>
237 inline void cmdGuiDocument(const std::string& doc, T&& cmd)
238 {
239 _cmdDocument(Gui::Command::Gui, doc, "Gui", std::forward<T>(cmd));
240 }
241
242 /** Runs a command for accessing an object's document attribute or method
243 * This function is an alternative to _FCMD_OBJ_DOC_CMD
244 * @param obj: pointer to a DocumentObject
245 * @param mod: module name, "Gui" or "App"
246 * @param cmd: command string, streamable
247 */
248 template<typename T>
249 inline void _cmdDocument(
250 Gui::Command::DoCmd_Type cmdType,
251 const App::DocumentObject* obj,
252 const std::string& mod,
253 T&& cmd
254 )
255 {
256 if (obj) {
257 _cmdDocument(cmdType, obj->getDocument(), mod, std::forward<T>(cmd));
258 }
259 }
260
261 /** Runs a command for accessing an object's App::Document attribute or method
262 * This function is an alternative to FCMD_OBJ_DOC_CMD
263 * @param obj: pointer to a DocumentObject
264 * @param cmd: command string, streamable
265 */
266 template<typename T>
267 inline void cmdAppDocument(const App::DocumentObject* obj, T&& cmd)
268 {
269 _cmdDocument(Gui::Command::Doc, obj, "App", std::forward<T>(cmd));
270 }
271
272 /** Runs a command for accessing a document's attribute or method
273 * @param doc: pointer to a Document
274 * @param cmd: command string, supporting printf like formatter
275 *
276 * Example:
277 * @code{.cpp}
278 * cmdAppDocumentArgs(obj, "addObject('%s')", "Part::Feature");
279 * @endcode
280 *
281 * Translates to command (assuming obj's document name is 'DocName':
282 * @code{.py}
283 * App.getDocument('DocName').addObject('Part::Feature')
284 * @endcode
285 */
286 template<typename... Args>
287 void cmdAppDocumentArgs(const App::Document* doc, const std::string& cmd, Args&&... args)
288 {
289 std::string _cmd;
290 try {
291 boost::format fmt(cmd);
292 _cmd = FormatString::toStr(fmt, std::forward<Args>(args)...);
293 Gui::Command::doCommand(
294 Gui::Command::Doc,
295 "App.getDocument('%s').%s",
296 doc->getName(),
297 _cmd.c_str()
298 );
299 }
300 catch (const std::exception& e) {
301 Base::Console().developerError(doc->Label.getStrValue(), "%s: %s\n", e.what(), cmd.c_str());
302 }
303 catch (const Base::Exception&) {
304 Base::Console().developerError(
305 doc->Label.getStrValue(),
306 "App.getDocument('%s').%s\n",
307 doc->getName(),
308 _cmd.c_str()
309 );
310 throw;
311 }
312 }
313
314 /** Runs a command for accessing an object's Gui::Document attribute or method
315 * This function is an alternative to FCMD_VOBJ_DOC_CMD
316 * @param obj: pointer to a DocumentObject
317 * @param cmd: command string, streamable
318 */
319 template<typename T>
320 inline void cmdGuiDocument(const App::DocumentObject* obj, T&& cmd)
321 {
322 _cmdDocument(Gui::Command::Gui, obj, "Gui", std::forward<T>(cmd));
323 }
324
325 /** Runs a command for accessing a document/view object's attribute or method
326 * This function is an alternative to _FCMD_OBJ_CMD
327 * @param cmdType: Command type
328 * @param obj: pointer to a DocumentObject
329 * @param mod: module name, "Gui" or "App"
330 * @param cmd: command string, streamable
331 *
332 * Example:
333 * @code{.cpp}
334 * _cmdObject(Command::Gui,obj,"Gui", "Visibility = " << (visible?"True":"False"));
335 * @endcode
336 *
337 * Translates to command (assuming obj's document name is 'DocName', obj's name
338 * is 'ObjName', and visible is true):
339 * @code{.py}
340 * Gui.getDocument('DocName').getObject('ObjName').Visibility = True
341 * @endcode
342 */
343 template<typename T>
344 void _cmdObject(
345 Gui::Command::DoCmd_Type cmdType,
346 const App::DocumentObject* obj,
347 const std::string& mod,
348 T&& cmd
349 )
350 {
351 if (obj && obj->isAttachedToDocument()) {
352 std::ostringstream str;
353 str << mod << ".getDocument('" << obj->getDocument()->getName()
354 << "')"
355 ".getObject('"
356 << obj->getNameInDocument() << "')." << FormatString::str(cmd);
357 Gui::Command::runCommand(cmdType, str.str().c_str());
358 }
359 }
360
361 /** Runs a command for accessing an document object's attribute or method
362 * This function is an alternative to FCMD_OBJ_CMD
363 * @param obj: pointer to a DocumentObject
364 * @param cmd: command string, streamable
365 * @sa _cmdObject()
366 */
367 template<typename T>
368 inline void cmdAppObject(const App::DocumentObject* obj, T&& cmd)
369 {
370 _cmdObject(Gui::Command::Doc, obj, "App", std::forward<T>(cmd));
371 }
372
373 /** Runs a command for accessing an view object's attribute or method
374 * This function is an alternative to FCMD_VOBJ_CMD
375 * @param obj: pointer to a DocumentObject
376 * @param cmd: command string, streamable
377 * @sa _cmdObject()
378 */
379 template<typename T>
380 inline void cmdGuiObject(const App::DocumentObject* obj, T&& cmd)
381 {
382 _cmdObject(Gui::Command::Gui, obj, "Gui", std::forward<T>(cmd));
383 }
384
385 /// Hides an object
386 inline void cmdAppObjectHide(const App::DocumentObject* obj)
387 {
388 cmdAppObject(obj, "Visibility = False");
389 }
390
391 /// Shows an object
392 inline void cmdAppObjectShow(const App::DocumentObject* obj)
393 {
394 cmdAppObject(obj, "Visibility = True");
395 }
396
397 /** Runs a command to start editing a give object
398 * This function is an alternative to FCMD_SET_EDIT
399 * @param obj: pointer to a DocumentObject
400 *
401 * Unlike other helper functions, this one editing the object using the current
402 * active document, instead of the object's owner document. This allows
403 * in-place editing an object, which may be brought in through linking to an
404 * external group.
405 */
406 inline void cmdSetEdit(const App::DocumentObject* obj, int mod = 0)
407 {
408 if (obj && obj->isAttachedToDocument()) {
409 Gui::Command::doCommand(
410 Gui::Command::Gui,
411 "Gui.ActiveDocument.setEdit(App.getDocument('%s').getObject('%s'), %d)",
412 obj->getDocument()->getName(),
413 obj->getNameInDocument(),
414 mod
415 );
416 }
417 }
418
419 /** Runs a command for accessing a document object's attribute or method
420 * This function is an alternative to FCMD_OBJ_CMD2
421 * @param cmd: command string, supporting printf like formatter
422 * @param obj: pointer to a DocumentObject
423 *
424 * Example:
425 * @code{.cpp}
426 * cmdAppObjectArgs(obj, "Visibility = %s", visible ? "True" : "False");
427 * @endcode
428 *
429 * Translates to command (assuming obj's document name is 'DocName', obj's name
430 * is 'ObjName', and visible is true):
431 * @code{.py}
432 * App.getDocument('DocName').getObject('ObjName').Visibility = True
433 * @endcode
434 */
435 template<typename... Args>
436 void cmdAppObjectArgs(const App::DocumentObject* obj, const std::string& cmd, Args&&... args)
437 {
438 std::string _cmd;
439 try {
440 boost::format fmt(cmd);
441 _cmd = FormatString::toStr(fmt, std::forward<Args>(args)...);
442 Gui::Command::doCommand(
443 Gui::Command::Doc,
444 "App.getDocument('%s').getObject('%s').%s",
445 obj->getDocument()->getName(),
446 obj->getNameInDocument(),
447 _cmd.c_str()
448 );
449 }
450 catch (const std::exception& e) {
451 Base::Console().developerError(obj->getFullLabel(), "%s: %s\n", e.what(), cmd.c_str());
452 }
453 catch (const Base::Exception&) {
454 Base::Console().developerError(
455 obj->getFullLabel(),
456 "App.getDocument('%s').getObject('%s').%s\n",
457 obj->getDocument()->getName(),
458 obj->getNameInDocument(),
459 _cmd.c_str()
460 );
461 throw;
462 }
463 }
464
465 /** Runs a command for accessing a view object's attribute or method
466 * This function is an alternative to FCMD_VOBJ_CMD2
467 * @param cmd: command string, supporting printf like formatter
468 * @param obj: pointer to a DocumentObject
469 * @sa cmdAppObjectArgs()
470 */
471 template<typename... Args>
472 void cmdGuiObjectArgs(const App::DocumentObject* obj, const std::string& cmd, Args&&... args)
473 {
474 std::string _cmd;
475 try {
476 boost::format fmt(cmd);
477 _cmd = FormatString::toStr(fmt, std::forward<Args>(args)...);
478 Gui::Command::doCommand(
479 Gui::Command::Gui,
480 "Gui.getDocument('%s').getObject('%s').%s",
481 obj->getDocument()->getName(),
482 obj->getNameInDocument(),
483 _cmd.c_str()
484 );
485 }
486 catch (const std::exception& e) {
487 Base::Console().developerError(obj->getFullLabel(), "%s: %s\n", e.what(), cmd.c_str());
488 }
489 catch (const Base::Exception&) {
490 Base::Console().developerError(
491 obj->getFullLabel(),
492 "Gui.getDocument('%s').getObject('%s').%s\n",
493 obj->getDocument()->getName(),
494 obj->getNameInDocument(),
495 _cmd.c_str()
496 );
497 throw;
498 }
499 }
500
501 /** Runs a command
502 * @param cmdType: command type
503 * @param cmd: command string, supporting printf like formatter
504 *
505 * Example:
506 * @code{.cpp}
507 * doCommandT(Gui::Command::Gui, "Gui.getDocument(%s).getObject(%s).Visibility = %s",
508 * "DocName", "ObjName", visible?"True":"False");
509 * @endcode
510 *
511 * Translates to command (assuming obj's document name is 'DocName', obj's name
512 * is 'ObjName', and visible is true):
513 * @code{.py}
514 * Gui.getDocument('DocName').getObject('ObjName').Visibility = True
515 * @endcode
516 */
517 template<typename... Args>
518 void doCommandT(Gui::Command::DoCmd_Type cmdType, const std::string& cmd, Args&&... args)
519 {
520 std::string _cmd;
521 try {
522 boost::format fmt(cmd);
523 _cmd = FormatString::toStr(fmt, std::forward<Args>(args)...);
524 Gui::Command::doCommand(cmdType, "%s", _cmd.c_str());
525 }
526 catch (const std::exception& e) {
527 Base::Console().developerError("doCommandT", "%s: %s\n", e.what(), cmd.c_str());
528 }
529 catch (const Base::Exception&) {
530 Base::Console().developerError("doCommandT", "%s\n", _cmd.c_str());
531 throw;
532 }
533 }
534
535 /** Copy visual attributes from a source to a target object
536 */
537 template<typename... Args>
538 void copyVisualT(Args&&... args)
539 {
540 // file and line number is useless here. Check C++'s source location function once available
541 Gui::Command::_copyVisual(__FILE__, __LINE__, std::forward<Args>(args)...);
542 }
543
544 //@}
545
546 }; // namespace Gui
547
548 #endif // GUI_COMMAND_T_H