Debian Codegraph @

src/Gui/Command.h

raw
1 /***************************************************************************
2 * Copyright (c) 2002 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_COMMAND_H
25 #define GUI_COMMAND_H
26
27 #include <list>
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <boost/signals2.hpp>
32
33 #include <Base/Type.h>
34 #include <Gui/Application.h>
35
36 /** @defgroup CommandMacros Helper macros for running commands through Python interpreter */
37 //@{
38
39 /** Runs a command for accessing document attribute or method
40 * @param _type: type of document, Gui or App
41 * @param _doc: pointer to a document
42 * @param _cmd: command string, streamable
43 *
44 * Example:
45 * @code{.cpp}
46 * _FCMD_DOC_CMD(Gui,doc,"getObject('" << objName << "')");
47 * @endcode
48 *
49 * Translates to command (assuming doc's name is 'DocName', and
50 * and objName contains value 'ObjName'):
51 * @code{.py}
52 * Gui.getDocument('DocName').getObject('ObjName')
53 * @endcode
54 */
55 #define _FCMD_DOC_CMD(_type, _doc, _cmd) \
56 do { \
57 auto __doc = _doc; \
58 if (__doc && __doc->getName()) { \
59 std::ostringstream _str; \
60 _str << #_type ".getDocument('" << __doc->getName() << "')." << _cmd; \
61 Gui::Command::runCommand(Gui::Command::Doc, _str.str().c_str()); \
62 } \
63 } while (0)
64
65 /** Runs a command for accessing App.Document attribute or method
66 *
67 * @param _doc: pointer to a document
68 * @param _cmd: command string, streamable
69 * @sa _FCMD_DOC_CMD()
70 */
71 #define FCMD_DOC_CMD(_doc, _cmd) _FCMD_DOC_CMD(App, _doc, _cmd)
72
73 /** Runs a command for accessing an object's document attribute or method
74 * @param _type: type of the document, Gui or App
75 * @param _obj: pointer to a DocumentObject
76 * @param _cmd: command string, streamable
77 */
78 #define _FCMD_OBJ_DOC_CMD(_type, _obj, _cmd) \
79 do { \
80 auto __obj = _obj; \
81 if (__obj) \
82 _FCMD_DOC_CMD(_type, __obj->getDocument(), _cmd); \
83 } while (0)
84
85 /** Runs a command for accessing an object's App::Document attribute or method
86 * @param _obj: pointer to a DocumentObject
87 * @param _cmd: command string, streamable
88 */
89 #define FCMD_OBJ_DOC_CMD(_obj, _cmd) _FCMD_OBJ_DOC_CMD(App, _obj, _cmd)
90
91 /** Runs a command for accessing an object's Gui::Document attribute or method
92 * @param _obj: pointer to a DocumentObject
93 * @param _cmd: command string, streamable
94 */
95 #define FCMD_VOBJ_DOC_CMD(_obj, _cmd) _FCMD_OBJ_DOC_CMD(Gui, _obj, _cmd)
96
97 /** Runs a command for accessing a document/view object's attribute or method
98 * @param _type: type of the object, Gui or App
99 * @param _obj: pointer to a DocumentObject
100 * @param _cmd: command string, streamable
101 *
102 * Example:
103 * @code{.cpp}
104 * _FCMD_OBJ_CMD(Gui,obj,"Visibility = " << (visible?"True":"False"));
105 * @endcode
106 *
107 * Translates to command (assuming obj's document name is 'DocName', obj's name
108 * is 'ObjName', and visible is true):
109 * @code{.py}
110 * Gui.getDocument('DocName').getObject('ObjName').Visibility = True
111 * @endcode
112 */
113 #define _FCMD_OBJ_CMD(_type, _cmd_type, _obj, _cmd) \
114 do { \
115 auto __obj = _obj; \
116 if (__obj && __obj->isAttachedToDocument()) { \
117 std::ostringstream _str; \
118 _str << #_type ".getDocument('" << __obj->getDocument()->getName() << "').getObject('" \
119 << __obj->getNameInDocument() << "')." << _cmd; \
120 Gui::Command::runCommand(Gui::Command::_cmd_type, _str.str().c_str()); \
121 } \
122 } while (0)
123
124 /** Runs a command for accessing an document object's attribute or method
125 * @param _obj: pointer to a DocumentObject
126 * @param _cmd: command string, streamable
127 * @sa _FCMD_OBJ_CMD()
128 */
129 #define FCMD_OBJ_CMD(_obj, _cmd) _FCMD_OBJ_CMD(App, Doc, _obj, _cmd)
130
131 /** Runs a command for accessing an view object's attribute or method
132 * @param _obj: pointer to a DocumentObject
133 * @param _cmd: command string, streamable
134 * @sa _FCMD_OBJ_CMD()
135 */
136 #define FCMD_VOBJ_CMD(_obj, _cmd) _FCMD_OBJ_CMD(Gui, Gui, _obj, _cmd)
137
138 /** Runs a command for accessing a document object's attribute or method
139 * @param _cmd: command string, supporting printf like formatter
140 * @param _obj: pointer to a DocumentObject
141 *
142 * Example:
143 * @code{.cpp}
144 * FCMD_OBJ_CMD2("Visibility = %s", obj, visible?"True":"False");
145 * @endcode
146 *
147 * Translates to command (assuming obj's document name is 'DocName', obj's name
148 * is 'ObjName', and visible is true):
149 * @code{.py}
150 * App.getDocument('DocName').getObject('ObjName').Visibility = True
151 * @endcode
152 */
153 #define FCMD_OBJ_CMD2(_cmd, _obj, ...) \
154 do { \
155 auto __obj = _obj; \
156 if (__obj && __obj->isAttachedToDocument()) { \
157 Gui::Command::doCommand( \
158 Gui::Command::Doc, \
159 "App.getDocument('%s').getObject('%s')." _cmd, \
160 __obj->getDocument()->getName(), \
161 __obj->getNameInDocument(), \
162 ##__VA_ARGS__ \
163 ); \
164 } \
165 } while (0)
166
167 /** Runs a command for accessing a view object's attribute or method
168 * @param _cmd: command string, supporting printf like formatter
169 * @param _obj: pointer to a DocumentObject
170 * @sa FCMD_OBJ_CMD2()
171 */
172 #define FCMD_VOBJ_CMD2(_cmd, _obj, ...) \
173 do { \
174 auto __obj = _obj; \
175 if (__obj && __obj->isAttachedToDocument()) { \
176 Gui::Command::doCommand( \
177 Gui::Command::Gui, \
178 "Gui.getDocument('%s').getObject('%s')." _cmd, \
179 __obj->getDocument()->getName(), \
180 __obj->getNameInDocument(), \
181 ##__VA_ARGS__ \
182 ); \
183 } \
184 } while (0)
185
186 /** Runs a command to start editing a give object
187 * @param _obj: pointer to a DocumentObject
188 *
189 * Unlike other FCMD macros, this macro editing the object using the current
190 * active document, instead of the object's owner document. This allows
191 * in-place editing an object, which may be brought in through linking to an
192 * external group.
193 */
194 #define FCMD_SET_EDIT(_obj) \
195 do { \
196 auto __obj = _obj; \
197 if (__obj && __obj->isAttachedToDocument()) { \
198 Gui::Command::doCommand( \
199 Gui::Command::Gui, \
200 "Gui.ActiveDocument.setEdit(App.getDocument('%s').getObject('%s'), %i)", \
201 __obj->getDocument()->getName(), \
202 __obj->getNameInDocument(), \
203 0 \
204 ); \
205 } \
206 } while (0)
207
208
209 /// Hides an object
210 #define FCMD_OBJ_HIDE(_obj) FCMD_OBJ_CMD(_obj, "Visibility = False")
211
212 /// Shows an object
213 #define FCMD_OBJ_SHOW(_obj) FCMD_OBJ_CMD(_obj, "Visibility = True")
214
215 //@}
216
217 class QWidget;
218 class QByteArray;
219
220 using PyObject = struct _object;
221
222 namespace App
223 {
224 class Document;
225 class DocumentObject;
226 } // namespace App
227
228 namespace Gui
229 {
230
231 class Action;
232 class Application;
233 class CommandManager;
234 class Command;
235 class ActionGroup;
236 class Document;
237 class SelectionSingleton;
238 class MDIView;
239
240
241 void CreateStdCommands();
242 void CreateDocCommands();
243 void CreateFeatCommands();
244 void CreateMacroCommands();
245 void CreateViewStdCommands();
246 void CreateWindowStdCommands();
247 void CreateStructureCommands();
248 void CreateTestCommands();
249 void CreateLinkCommands();
250
251
252 /** The CommandBase class
253 * This lightweight class is the base class of all commands in FreeCAD. It represents the link
254 * between the FreeCAD command framework and the QAction world of Qt.
255 * @author Werner Mayer
256 */
257 class GuiExport CommandBase
258 {
259 protected:
260 explicit CommandBase(
261 const char* sMenu,
262 const char* sToolTip = nullptr,
263 const char* sWhat = nullptr,
264 const char* sStatus = nullptr,
265 const char* sPixmap = nullptr,
266 const char* sAccel = nullptr
267 );
268 virtual ~CommandBase();
269
270 public:
271 /**
272 * Returns the Action object of this command, or 0 if it doesn't exist.
273 */
274 Action* getAction() const;
275
276 /** @name Methods to override when creating a new command */
277 //@{
278 protected:
279 /// Creates the used Action when adding to a widget. The default implementation does nothing.
280 virtual Action* createAction();
281
282 public:
283 /// Reassigns QAction stuff after the language has changed.
284 virtual void languageChange() = 0;
285 /// Updates the QAction with respect to the passed mode.
286 virtual void updateAction(int mode) = 0;
287 /// The C++ class name is needed as context for the translation framework
288 virtual const char* className() const = 0;
289 //@}
290
291 /** @name Methods to get the properties of the command */
292 //@{
293 virtual const char* getMenuText() const
294 {
295 return sMenuText;
296 }
297 virtual const char* getToolTipText() const
298 {
299 return sToolTipText;
300 }
301 virtual const char* getStatusTip() const
302 {
303 return sStatusTip;
304 }
305 virtual const char* getWhatsThis() const
306 {
307 return sWhatsThis;
308 }
309 virtual const char* getPixmap() const
310 {
311 return sPixmap;
312 }
313 virtual const char* getAccel() const
314 {
315 return sAccel;
316 }
317 //@}
318
319 /** @name Methods to set the properties of the command */
320 //@{
321 void setWhatsThis(const char*);
322 void setMenuText(const char*);
323 void setToolTipText(const char*);
324 void setStatusTip(const char*);
325 void setPixmap(const char*);
326 void setAccel(const char*);
327 //@}
328
329 protected:
330 /** @name Attributes set by the inherited constructor.
331 *
332 * They set up the most important properties of the command.
333 * In the constructor are set default values.
334 * The real values should be set in the constructor of the inheriting class.
335 */
336 //@{
337 const char* sMenuText;
338 const char* sToolTipText;
339 const char* sWhatsThis;
340 const char* sStatusTip;
341 const char* sPixmap;
342 const char* sAccel;
343 //@}
344 protected:
345 Action* _pcAction; /**< The Action item. */
346 std::string displayText;
347 };
348
349 /** The Command class.
350 *
351 * This class is mostly used for commands implemented directly in C++ (see PythonCommand).
352 * It contains also a lot of helper methods to make implementing commands for FreeCAD as easy as
353 * possible.
354 *
355 * @note This class is intended to handle the GUI interaction like:
356 * - starting a dialog
357 * - doing view and window stuff
358 * - anything else, especially altering the document must be done on application level. See
359 * doCommand() for details.
360 *
361 * @see CommandManager
362 * @author Jürgen Riegel
363 */
364 class GuiExport Command: public CommandBase
365 {
366 protected:
367 explicit Command(const char* name);
368 ~Command() override;
369
370 protected:
371 /** @name Methods to override when creating a new command
372 */
373 //@{
374 /// Methods which gets called when activated, needs to be reimplemented!
375 virtual void activated(int iMsg) = 0;
376 /// Creates the used Action
377 Action* createAction() override;
378 /// Applies the menu text, tool and status tip to the passed action object
379 void applyCommandData(const char* context, Action*);
380 const char* keySequenceToAccel(int) const;
381 void printConflictingAccelerators() const;
382 //@}
383
384 public:
385 /** @name interface used by the CommandManager and the Action */
386 //@{
387 /// CommandManager is a friend
388 friend class CommandManager;
389 /// Override this method if your Cmd is not always active
390 virtual bool isActive()
391 {
392 return true;
393 }
394 /// Get somtile called to check the state of the command
395 void testActive();
396 /// Enables or disables the command
397 void setEnabled(bool);
398 /// Command trigger source
399 enum TriggerSource
400 {
401 /// No external trigger, e.g. invoked through Python
402 TriggerNone,
403 /// Command triggered by an action
404 TriggerAction,
405 /// Command triggered by a child action inside an action group
406 TriggerChildAction,
407 };
408 /// Return the current command trigger source
409 TriggerSource triggerSource() const
410 {
411 return _trigger;
412 }
413 /** Called to invoke the command
414 *
415 * @param index: in case of group command, this is the index of the child
416 * command. For checkable command, this indicates the
417 * checkable state.
418 * @param trigger: indicate the command triggering source, see TriggerSource.
419 */
420 void invoke(int index, TriggerSource trigger = TriggerNone);
421 /// adds this command to arbitrary widgets
422 void addTo(QWidget*);
423 void addToGroup(ActionGroup*, bool checkable);
424 void addToGroup(ActionGroup*);
425 /// Create the action if not exist
426 void initAction();
427 //@}
428
429
430 /** @name Helper methods to get important classes */
431 //@{
432 /// Get pointer to the Application Window
433 static Application* getGuiApplication();
434 /// Get a reference to the selection
435 static Gui::SelectionSingleton& getSelection();
436 /// Get pointer to the active app document
437 App::Document* getActiveDocument() const;
438 /// Get pointer to the active gui document
439 Gui::Document* getActiveGuiDocument() const;
440 /** Get pointer to the named or active App document
441 * Returns a pointer to the named document or the active
442 * document when no name is given. NULL is returned
443 * when the name does not exist or no document is active!
444 */
445 App::Document* getDocument(const char* Name = nullptr) const;
446 /// checks if the active view is of a special type or derived
447 bool isViewOfType(Base::Type t) const;
448 /// returns the named feature or the active one from the active document or NULL
449 App::DocumentObject* getObject(const char* Name) const;
450 /// returns a python command string to retrieve an object from a document
451 static std::string getObjectCmd(
452 const char* Name,
453 const App::Document* doc = nullptr,
454 const char* prefix = nullptr,
455 const char* postfix = nullptr,
456 bool gui = false
457 );
458 /// returns a python command string to retrieve the given object
459 static std::string getObjectCmd(
460 const App::DocumentObject* obj,
461 const char* prefix = nullptr,
462 const char* postfix = nullptr,
463 bool gui = false
464 );
465 /** Get unique Feature name from the active document
466 *
467 * @param BaseName: the base name
468 * @param obj: if not zero, then request the unique name in the document of
469 * the given object.
470 */
471 std::string getUniqueObjectName(const char* BaseName, const App::DocumentObject* obj = nullptr) const;
472 //@}
473
474 /** @name Helper methods for the Undo/Redo and Update handling */
475 //@{
476 /// Open a new Undo transaction on the active document
477 static void openCommand(const char* sName = nullptr);
478 /// Commit the Undo transaction on the active document
479 static void commitCommand();
480 /// Abort the Undo transaction on the active document
481 static void abortCommand();
482 /// Check if an Undo transaction is open on the active document
483 static bool hasPendingCommand();
484 /// Updates the (active) document (propagate changes)
485 static void updateActive();
486 /// Checks if the active object of the active document is valid
487 static bool isActiveObjectValid();
488 /// Translate command
489 void languageChange() override;
490 /// Updates the QAction with respect to the passed mode.
491 void updateAction(int mode) override;
492 /// Setup checkable actions based on current TriggerSource
493 void setupCheckable(int iMsg);
494 //@}
495
496 /** @name Helper methods for issuing commands to the Python interpreter */
497 //@{
498 /// types of application level actions for DoCommand()
499 enum DoCmd_Type
500 {
501 /// Action alters the document
502 Doc,
503 /// Action alters only the application
504 App,
505 /// Action alters only the Gui
506 Gui
507 };
508 /// Blocks all command objects
509 static void blockCommand(bool);
510 /// Print to Python console the current Python calling source file and line number
511 static void printPyCaller();
512 /// Print to Python console the current calling source file and line number
513 static void printCaller(const char* file, int line);
514
515 // ISO C++11 requires at least one argument for the "..." in a variadic macro
516 // https://en.wikipedia.org/wiki/Variadic_macro#Example
517 /** Convenience macro to run a command with printf like formatter
518 *
519 * @sa Command::_doCommand()
520 */
521 #ifdef _MSC_VER
522 # define doCommand(_type, ...) _doCommand(__FILE__, __LINE__, _type, ##__VA_ARGS__)
523 #else
524 # define doCommand(...) _doCommand(__FILE__, __LINE__, __VA_ARGS__)
525 #endif
526
527 /** Run a command with printf like formatter
528 *
529 * @param file: the calling file path (for debugging purpose)
530 * @param line: the calling line number (for debugging purpose)
531 * @param eType: command type, See DoCmd_Type
532 * @param sCmd: command string that supports printf like formatter
533 *
534 * You can use the convenience macro doCommand() to automate \c file and \c
535 * line arguments. You may also want to use various helper @ref CommandMacros.
536 */
537 static void _doCommand(const char* file, int line, DoCmd_Type eType, const char* sCmd, ...);
538
539 /** Convenience macro to run a command
540 *
541 * @sa Command::_runCommand()
542 */
543 #define runCommand(_type, _cmd) _runCommand(__FILE__, __LINE__, _type, _cmd)
544
545 /** Run a command
546 *
547 * @param file: the calling file path (for debugging purpose)
548 * @param line: the calling line number (for debugging purpose)
549 * @param eType: command type, See DoCmd_Type
550 * @param sCmd: command string
551 *
552 * @sa _doCommand()
553 */
554 static void _runCommand(const char* file, int line, DoCmd_Type eType, const char* sCmd);
555
556 /** Run a command
557 *
558 * @param file: the calling file path (for debugging purpose)
559 * @param line: the calling line number (for debugging purpose)
560 * @param eType: command type, See DoCmd_Type
561 * @param sCmd: command string
562 *
563 * @sa _doCommand()
564 */
565 static void _runCommand(const char* file, int line, DoCmd_Type eType, const QByteArray& sCmd);
566
567 /// import an external (or own) module only once
568 static void addModule(DoCmd_Type eType, const char* sModuleName);
569
570 /** Convenience macro to assure the switch to a certain workbench
571 *
572 * @sa _assureWorkbench()
573 */
574 #define assureWorkbench(_name) _assureWorkbench(__FILE__, __LINE__, _name)
575
576 /** Assures the switch to a certain workbench
577 *
578 * @param file: the calling file path (for debugging purpose)
579 * @param line: the calling line number (for debugging purpose)
580 * @param sName: workbench name
581 *
582 * @return Return the current active workbench name before switching.
583 *
584 * If already in the workbench, does nothing.
585 */
586 static std::string _assureWorkbench(const char* file, int line, const char* sName);
587 //@}
588
589 /** @name Methods for copying visiual properties */
590 //@{
591 /// Convenience macro to copy visual properties
592 #define copyVisual(...) _copyVisual(__FILE__, __LINE__, __VA_ARGS__)
593 static void _copyVisual(const char* file, int line, const char* to, const char* attr, const char* from);
594 static void _copyVisual(
595 const char* file,
596 int line,
597 const char* to,
598 const char* attr_to,
599 const char* from,
600 const char* attr_from
601 );
602 static void _copyVisual(
603 const char* file,
604 int line,
605 const App::DocumentObject* to,
606 const char* attr,
607 const App::DocumentObject* from
608 );
609 static void _copyVisual(
610 const char* file,
611 int line,
612 const App::DocumentObject* to,
613 const char* attr_to,
614 const App::DocumentObject* from,
615 const char* attr_from
616 );
617 //@}
618
619 /// Get Python tuple from object and sub-elements
620 static std::string getPythonTuple(const std::string& name, const std::vector<std::string>& subnames);
621 /// translate a string to a python string literal (needed e.g. in file names for windows...)
622 const std::string strToPython(const char* Str);
623 const std::string strToPython(const std::string& Str)
624 {
625 return strToPython(Str.c_str());
626 }
627
628 /** @name Helper methods to generate help pages */
629 //@{
630 /// returns the begin of a online help page
631 const char* beginCmdHelp();
632 /// returns the end of a online help page
633 const char* endCmdHelp();
634 /// Get the help URL
635 virtual const char* getHelpUrl() const
636 {
637 return sHelpUrl;
638 }
639 //@}
640
641 /** @name Helper methods for the Active tests */
642 //@{
643 /// true when there is a document
644 bool hasActiveDocument() const;
645 /// true when there is a document and a Feature with Name
646 bool hasObject(const char* Name);
647 //@}
648
649 /** @name checking of internal state */
650 //@{
651 /// returns the name to which the command belongs
652 const char* getAppModuleName() const
653 {
654 return sAppModule;
655 }
656 void setAppModuleName(const char*);
657 /// Get the command name
658 const char* getName() const
659 {
660 return sName;
661 }
662 /// Get the name of the grouping of the command
663 const char* getGroupName() const
664 {
665 return sGroup;
666 }
667 void setGroupName(const char*);
668 QString translatedGroupName() const;
669 //@}
670
671
672 /// Override shortcut of this command
673 virtual void setShortcut(const QString&);
674 /// Obtain the current shortcut of this command
675 virtual QString getShortcut() const;
676
677 /// Helper class to disable python console log
678 class LogDisabler
679 {
680 public:
681 LogDisabler()
682 {
683 ++Command::_busy;
684 }
685 ~LogDisabler()
686 {
687 --Command::_busy;
688 }
689 };
690 friend class LogDisabler;
691
692 private:
693 void _invoke(int, bool disablelog);
694
695 protected:
696 enum CmdType
697 {
698 AlterDoc = 1, /**< Command change the Document */
699 Alter3DView = 2, /**< Command change the Gui */
700 AlterSelection = 4, /**< Command change the Selection */
701 ForEdit = 8, /**< Command is in a special edit mode active */
702 NoTransaction = 16, /**< Do not setup auto transaction */
703 };
704
705 /** @name Attributes
706 * Set by the inherited constructor to set up the most important properties
707 * of the command. In the Command constructor are set default values!
708 * The real values should be set in the constructor of the inheriting class.
709 */
710 //@{
711 const char* sAppModule;
712 const char* sGroup;
713 const char* sName;
714 const char* sHelpUrl;
715 int eType;
716 /// Indicate if the command shall log to MacroManager
717 bool bCanLog;
718 //@}
719 private:
720 static int _busy;
721 bool bEnabled;
722 static bool _blockCmd;
723 /// For storing the current command trigger source
724 TriggerSource _trigger = TriggerNone;
725 };
726
727 /** Class to help implement a group command
728 *
729 * To use this class, simply add children command in the constructor of your
730 * derived class by calling addCommand();
731 */
732 class GuiExport GroupCommand: public Command
733 {
734 public:
735 /// Constructor
736 explicit GroupCommand(const char* name);
737
738 /** Add child command
739 * @param cmd: child command. Pass null pointer to add a separator.
740 * @param reg: whether to register the command with CommandManager
741 * @return Return the command index.
742 */
743 int addCommand(Command* cmd = nullptr, bool reg = true);
744 /** Add child command
745 * @param cmd: child command name.
746 * @return Return the found command, or NULL if not found.
747 */
748 Command* addCommand(const char* cmdName);
749
750 Command* getCommand(int idx) const;
751
752 protected:
753 bool isCheckable() const;
754 void setCheckable(bool);
755 bool isExclusive() const;
756 void setExclusive(bool);
757 bool doesRememberLast() const;
758 void setRememberLast(bool);
759 bool hasDropDownMenu() const;
760 void setDropDownMenu(bool);
761 void activated(int iMsg) override;
762 Gui::Action* createAction() override;
763 void languageChange() override;
764
765 void setup(Action*);
766
767 protected:
768 bool checkable = true;
769 bool exclusive = false;
770 bool rememberLast = true;
771 bool dropDownMenu = true;
772 std::vector<std::pair<Command*, size_t>> cmds;
773 };
774
775 /** The Python command class
776 * This is a special type of command class. It's used to bind a Python command class into the
777 * FreeCAD command framework.
778 * An object of this class gets a reference to the Python command object and manages all the
779 * passing between the C++ and the Python world. This includes everything like setting resources
780 * such as bitmaps, activation or bindings to the user interface.
781 * @see CommandManager
782 * @author Jürgen Riegel
783 */
784 class PythonCommand: public Command
785 {
786 public:
787 PythonCommand(const char* name, PyObject* pcPyCommand, const char* pActivationString);
788 ~PythonCommand() override;
789
790 protected:
791 /** @name Methods reimplemented for Command Framework */
792 //@{
793 /// Method which gets called when activated
794 void activated(int iMsg) override;
795 /// if the command is not always active
796 bool isActive() override;
797 /// Get the help URL
798 const char* getHelpUrl() const override;
799 //@}
800
801 public:
802 /** @name Methods to get the properties of the command */
803 //@{
804 /// Reassigns QAction stuff after the language has changed.
805 void languageChange() override;
806 const char* className() const override
807 {
808 return "PythonCommand";
809 }
810 const char* getWhatsThis() const override;
811 const char* getMenuText() const override;
812 const char* getToolTipText() const override;
813 const char* getStatusTip() const override;
814 const char* getPixmap() const override;
815 const char* getAccel() const override;
816 bool isCheckable() const;
817 bool isChecked() const;
818 //@}
819
820 protected:
821 /// Returns the resource values
822 const char* getResource(const char* sName) const;
823 /// Creates the used Action
824 Action* createAction() override;
825 /// a pointer to the Python command object
826 PyObject* _pcPyCommand;
827 /// the command object resource dictionary
828 PyObject* _pcPyResourceDict;
829 /// the activation sequence
830 std::string Activation;
831 //// set the parameters on action creation
832 void onActionInit() const;
833
834 boost::signals2::connection connPyCmdInitialized;
835 };
836
837 /** The Python group command class
838 * @see CommandManager
839 * @author Werner Mayer
840 */
841 class PythonGroupCommand: public Command
842 {
843 public:
844 PythonGroupCommand(const char* name, PyObject* pcPyCommand);
845 ~PythonGroupCommand() override;
846
847 protected:
848 /** @name Methods reimplemented for Command Framework */
849 //@{
850 /// Method which gets called when activated
851 void activated(int iMsg) override;
852 /// if the command is not always active
853 bool isActive() override;
854 /// Get the help URL
855 const char* getHelpUrl() const override;
856 /// Creates the used Action
857 Action* createAction() override;
858 //@}
859
860 public:
861 /** @name Methods to get the properties of the command */
862 //@{
863 /// Reassigns QAction stuff after the language has changed.
864 void languageChange() override;
865 const char* className() const override
866 {
867 return "PythonGroupCommand";
868 }
869 const char* getWhatsThis() const override;
870 const char* getMenuText() const override;
871 const char* getToolTipText() const override;
872 const char* getStatusTip() const override;
873 const char* getPixmap() const override;
874 const char* getAccel() const override;
875 bool isExclusive() const;
876 bool hasDropDownMenu() const;
877 //@}
878
879 protected:
880 /// Returns the resource values
881 const char* getResource(const char* sName) const;
882 //// set the parameters on action creation
883 void onActionInit() const;
884 /// a pointer to the Python command object
885 PyObject* _pcPyCommand;
886 /// the command object resources
887 PyObject* _pcPyResource;
888
889 boost::signals2::connection connPyCmdInitialized;
890 };
891
892
893 /** The script command class
894 * This is a special type of command class. Its used to bind a macro or Python script to the
895 * FreeCAD command framework.
896 * An object of this class gets a string to the place where the script is in the file system.
897 * Unlike the other commands the resources can be set by several methods.
898 * @see Command
899 * @see CommandManager
900 * @author Werner Mayer
901 */
902 class MacroCommand: public Command
903 {
904 public:
905 explicit MacroCommand(const char* name, bool system = false);
906 ~MacroCommand() override;
907
908 protected:
909 /** @name methods reimplemented for Command Framework */
910 //@{
911 /// Method which get called when activated
912 void activated(int iMsg) override;
913 /// Creates the used Action
914 Action* createAction() override;
915 //@}
916
917 public:
918 /// Returns the script name
919 const char* getScriptName() const
920 {
921 return sScriptName;
922 }
923 /// Ignore when language has changed.
924 void languageChange() override
925 {}
926 const char* className() const override
927 {
928 return "Gui::MacroCommand";
929 }
930
931 /** @name Methods to set the properties of the Script Command */
932 //@{
933 /// Sets the script name
934 void setScriptName(const char*);
935 //@}
936
937 /** @name Methods to load and save macro commands. */
938 //@{
939 /** Loads all macros command from the preferences. */
940 static void load();
941 /** Saves all macros command to the preferences. */
942 static void save();
943 //@}
944
945 protected:
946 const char* sScriptName;
947 bool systemMacro;
948 };
949
950 /** The CommandManager class
951 * This class manage all available commands in FreeCAD. All
952 * Commands will registered here, also commands from Application
953 * modules. Also activation / deactivation, Icons Tooltips and so
954 * on are handles here. Further the Building of Toolbars and (Context)
955 * menus (connecting to a QAction) is done here.
956 * @see Command
957 * @author Jürgen Riegel
958 */
959 class GuiExport CommandManager
960 {
961 public:
962 /// Construction
963 CommandManager();
964 /// Destruction
965 ~CommandManager();
966 /// Insert a new command into the manager
967 void addCommand(Command* pCom);
968 /// Remove a command from the manager
969 void removeCommand(Command* pCom);
970
971 /// Adds the given command to a given widget
972 bool addTo(const char* Name, QWidget* pcWidget);
973
974 /** Returns all commands of a special App Module
975 * delivers a vector of all commands in the given application module. When no
976 * name is given the standard commands (build in ) are returned.
977 * @see Command
978 */
979 std::vector<Command*> getModuleCommands(const char* sModName) const;
980
981 /** Returns all commands registered in the manager
982 * delivers a vector of all commands. If you intereted in commands of
983 * of a special app module use GetModuleCommands()
984 * @see Command
985 */
986 std::vector<Command*> getAllCommands() const;
987
988 /** Returns all commands of a group
989 * delivers a vector of all commands in the given group.
990 */
991 std::vector<Command*> getGroupCommands(const char* sGrpName) const;
992
993 /** Returns the command registered in the manager with the name sName
994 * If nothing is found it returns a null pointer
995 * @see Command
996 */
997 Command* getCommandByName(const char* sName) const;
998
999 /**
1000 * Runs the command
1001 */
1002 void runCommandByName(const char* sName) const;
1003
1004 /// method is OBSOLETE use GetModuleCommands() or GetAllCommands()
1005 const std::map<std::string, Command*>& getCommands() const
1006 {
1007 return _sCommands;
1008 }
1009 /// get frequently called by the AppWnd to check the commands are active.
1010 void testActive();
1011
1012 void addCommandMode(const char* sContext, const char* sName);
1013 void updateCommands(const char* sContext, int mode);
1014
1015 /// Return a revision number to check for addition or removal of any command
1016 int getRevision() const
1017 {
1018 return _revision;
1019 }
1020
1021 /// Signal on any addition or removal of command
1022 boost::signals2::signal<void()> signalChanged;
1023
1024 /// Signal to Python command on first workbench activation
1025 boost::signals2::signal<void()> signalPyCmdInitialized;
1026
1027 /**
1028 * Returns a pointer to a conflicting command, or nullptr if there is no conflict.
1029 * In the case of multiple conflicts, only the first is returned.
1030 * \param accel The accelerator to check
1031 * \param ignore (optional) A command to ignore matches with
1032 */
1033 const Command* checkAcceleratorForConflicts(const char* accel, const Command* ignore = nullptr) const;
1034
1035 /**
1036 * Returns the first available command name for a new macro (e.g. starting from 1,
1037 * examines the existing user preferences for Std_Macro_%1 and returns the lowest
1038 * available numbered string).
1039 */
1040 std::string newMacroName() const;
1041
1042 private:
1043 /// Destroys all commands in the manager and empties the list.
1044 void clearCommands();
1045 std::map<std::string, Command*> _sCommands;
1046 std::map<std::string, std::list<std::string>> _sCommandModes;
1047
1048 int _revision = 0;
1049 };
1050
1051 } // namespace Gui
1052
1053
1054 /** The Command Macro Standard
1055 * This macro makes it easier to define a new command.
1056 * The parameters are the class name.
1057 * @author Jürgen Riegel
1058 */
1059 #define DEF_STD_CMD(X) \
1060 class X: public Gui::Command \
1061 { \
1062 public: \
1063 X(); \
1064 virtual ~X() \
1065 {} \
1066 virtual const char* className() const \
1067 { \
1068 return #X; \
1069 } \
1070 \
1071 protected: \
1072 virtual void activated(int iMsg); \
1073 \
1074 private: \
1075 X(const X&) = delete; \
1076 X(X&&) = delete; \
1077 X& operator=(const X&) = delete; \
1078 X& operator=(X&&) = delete; \
1079 };
1080
1081 /** The Command Macro Standard + isActive()
1082 * This macro makes it easier to define a new command.
1083 * The parameters are the class name
1084 * @author Jürgen Riegel
1085 */
1086 #define DEF_STD_CMD_A(X) \
1087 class X: public Gui::Command \
1088 { \
1089 public: \
1090 X(); \
1091 virtual ~X() \
1092 {} \
1093 virtual const char* className() const \
1094 { \
1095 return #X; \
1096 } \
1097 \
1098 protected: \
1099 virtual void activated(int iMsg); \
1100 virtual bool isActive(void); \
1101 \
1102 private: \
1103 X(const X&) = delete; \
1104 X(X&&) = delete; \
1105 X& operator=(const X&) = delete; \
1106 X& operator=(X&&) = delete; \
1107 };
1108
1109 /** The Command Macro Standard + createAction()
1110 * This macro makes it easier to define a new command.
1111 * The parameters are the class name
1112 * @author Jürgen Riegel
1113 */
1114 #define DEF_STD_CMD_C(X) \
1115 class X: public Gui::Command \
1116 { \
1117 public: \
1118 X(); \
1119 virtual ~X() \
1120 {} \
1121 virtual const char* className() const \
1122 { \
1123 return #X; \
1124 } \
1125 \
1126 protected: \
1127 virtual void activated(int iMsg); \
1128 virtual Gui::Action* createAction(void); \
1129 \
1130 private: \
1131 X(const X&) = delete; \
1132 X(X&&) = delete; \
1133 X& operator=(const X&) = delete; \
1134 X& operator=(X&&) = delete; \
1135 };
1136
1137 /** The Command Macro Standard + isActive() + createAction()
1138 * This macro makes it easier to define a new command.
1139 * The parameters are the class name
1140 * @author Werner Mayer
1141 */
1142 #define DEF_STD_CMD_AC(X) \
1143 class X: public Gui::Command \
1144 { \
1145 public: \
1146 X(); \
1147 virtual ~X() \
1148 {} \
1149 virtual const char* className() const \
1150 { \
1151 return #X; \
1152 } \
1153 \
1154 protected: \
1155 virtual void activated(int iMsg); \
1156 virtual bool isActive(void); \
1157 virtual Gui::Action* createAction(void); \
1158 \
1159 private: \
1160 X(const X&) = delete; \
1161 X(X&&) = delete; \
1162 X& operator=(const X&) = delete; \
1163 X& operator=(X&&) = delete; \
1164 };
1165
1166 /** The Command Macro Standard + isActive() + updateAction()
1167 * This macro makes it easier to define a new command.
1168 * The parameters are the class name
1169 * @author Werner Mayer
1170 */
1171 #define DEF_STD_CMD_AU(X) \
1172 class X: public Gui::Command \
1173 { \
1174 public: \
1175 X(); \
1176 virtual ~X() \
1177 {} \
1178 virtual void updateAction(int mode); \
1179 virtual const char* className() const \
1180 { \
1181 return #X; \
1182 } \
1183 \
1184 protected: \
1185 virtual void activated(int iMsg); \
1186 virtual bool isActive(void); \
1187 \
1188 private: \
1189 X(const X&) = delete; \
1190 X(X&&) = delete; \
1191 X& operator=(const X&) = delete; \
1192 X& operator=(X&&) = delete; \
1193 };
1194
1195 /** The Command Macro Standard + isActive() + createAction()
1196 * + languageChange()
1197 * This macro makes it easier to define a new command.
1198 * The parameters are the class name
1199 * @author Werner Mayer
1200 */
1201 #define DEF_STD_CMD_ACL(X) \
1202 class X: public Gui::Command \
1203 { \
1204 public: \
1205 X(); \
1206 virtual ~X() \
1207 {} \
1208 virtual void languageChange(); \
1209 virtual const char* className() const \
1210 { \
1211 return #X; \
1212 } \
1213 \
1214 protected: \
1215 virtual void activated(int iMsg); \
1216 virtual bool isActive(void); \
1217 virtual Gui::Action* createAction(void); \
1218 \
1219 private: \
1220 X(const X&) = delete; \
1221 X(X&&) = delete; \
1222 X& operator=(const X&) = delete; \
1223 X& operator=(X&&) = delete; \
1224 };
1225
1226 /** The Command Macro Standard + isActive() + createAction()
1227 * + languageChange() + updateAction()
1228 * This macro makes it easier to define a new command.
1229 * The parameters are the class name
1230 * @author Werner Mayer
1231 */
1232 #define DEF_STD_CMD_ACLU(X) \
1233 class X: public Gui::Command \
1234 { \
1235 public: \
1236 X(); \
1237 virtual ~X() \
1238 {} \
1239 virtual void languageChange(); \
1240 virtual void updateAction(int mode); \
1241 virtual const char* className() const \
1242 { \
1243 return #X; \
1244 } \
1245 \
1246 protected: \
1247 virtual void activated(int iMsg); \
1248 virtual bool isActive(void); \
1249 virtual Gui::Action* createAction(void); \
1250 \
1251 private: \
1252 X(const X&) = delete; \
1253 X(X&&) = delete; \
1254 X& operator=(const X&) = delete; \
1255 X& operator=(X&&) = delete; \
1256 };
1257
1258 /** The Command Macro view
1259 * This macro makes it easier to define a new command for the 3D View
1260 * It activate the command only when a 3DView is active.
1261 * The parameters are the class name
1262 * @author Jürgen Riegel
1263 */
1264 #define DEF_3DV_CMD(X) \
1265 class X: public Gui::Command \
1266 { \
1267 public: \
1268 X(); \
1269 virtual ~X() \
1270 {} \
1271 virtual const char* className() const \
1272 { \
1273 return #X; \
1274 } \
1275 \
1276 protected: \
1277 virtual void activated(int iMsg); \
1278 virtual bool isActive(void) \
1279 { \
1280 Gui::MDIView* view = Gui::getMainWindow()->activeWindow(); \
1281 return view && view->isDerivedFrom<Gui::View3DInventor>(); \
1282 } \
1283 \
1284 private: \
1285 X(const X&) = delete; \
1286 X(X&&) = delete; \
1287 X& operator=(const X&) = delete; \
1288 X& operator=(X&&) = delete; \
1289 };
1290
1291 #endif // GUI_COMMAND_H