/
Command.h
1023 lines (921 loc) · 35.2 KB
/
Command.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef GUI_COMMAND_H
#define GUI_COMMAND_H
#include <list>
#include <map>
#include <string>
#include <vector>
#include <Base/Type.h>
/** @defgroup CommandMacros Helper macros for running commands through Python interpreter */
//@{
/** Runs a command for accessing document attribute or method
* @param _type: type of document, Gui or App
* @param _doc: pointer to a document
* @param _cmd: command string, streamable
*
* Example:
* @code{.cpp}
* _FCMD_DOC_CMD(Gui,doc,"getObject('" << objName << "')");
* @endcode
*
* Translates to command (assuming doc's name is 'DocName', and
* and objName contains value 'ObjName'):
* @code{.py}
* Gui.getDocument('DocName').getObject('ObjName')
* @endcode
*/
#define _FCMD_DOC_CMD(_type,_doc,_cmd) do{\
auto __doc = _doc;\
if(__doc && __doc->getName()) {\
std::ostringstream _str;\
_str << #_type ".getDocument('" << __doc->getName() << "')." << _cmd;\
Gui::Command::runCommand(Gui::Command::Doc,_str.str().c_str());\
}\
}while(0)
/** Runs a command for accessing App.Document attribute or method
*
* @param _doc: pointer to a document
* @param _cmd: command string, streamable
* @sa _FCMD_DOC_CMD()
*/
#define FCMD_DOC_CMD(_doc,_cmd) _FCMD_DOC_CMD(App,_doc,_cmd)
/** Runs a command for accessing an object's document attribute or method
* @param _type: type of the document, Gui or App
* @param _obj: pointer to a DocumentObject
* @param _cmd: command string, streamable
*/
#define _FCMD_OBJ_DOC_CMD(_type,_obj,_cmd) do{\
auto __obj = _obj;\
if(__obj)\
_FCMD_DOC_CMD(_type,__obj->getDocument(),_cmd);\
}while(0)
/** Runs a command for accessing an object's App::Document attribute or method
* @param _obj: pointer to a DocumentObject
* @param _cmd: command string, streamable
*/
#define FCMD_OBJ_DOC_CMD(_obj,_cmd) _FCMD_OBJ_DOC_CMD(App,_obj,_cmd)
/** Runs a command for accessing an object's Gui::Document attribute or method
* @param _obj: pointer to a DocumentObject
* @param _cmd: command string, streamable
*/
#define FCMD_VOBJ_DOC_CMD(_obj,_cmd) _FCMD_OBJ_DOC_CMD(Gui,_obj,_cmd)
/** Runs a command for accessing a document/view object's attribute or method
* @param _type: type of the object, Gui or App
* @param _obj: pointer to a DocumentObject
* @param _cmd: command string, streamable
*
* Example:
* @code{.cpp}
* _FCMD_OBJ_CMD(Gui,obj,"Visibility = " << (visible?"True":"False"));
* @endcode
*
* Translates to command (assuming obj's document name is 'DocName', obj's name
* is 'ObjName', and visible is true):
* @code{.py}
* Gui.getDocument('DocName').getObject('ObjName').Visibility = True
* @endcode
*/
#define _FCMD_OBJ_CMD(_type,_cmd_type,_obj,_cmd) do{\
auto __obj = _obj;\
if(__obj && __obj->getNameInDocument()) {\
std::ostringstream _str;\
_str << #_type ".getDocument('" << __obj->getDocument()->getName() \
<< "').getObject('" << __obj->getNameInDocument() << "')." << _cmd;\
Gui::Command::runCommand(Gui::Command::_cmd_type,_str.str().c_str());\
}\
}while(0)
/** Runs a command for accessing an document object's attribute or method
* @param _obj: pointer to a DocumentObject
* @param _cmd: command string, streamable
* @sa _FCMD_OBJ_CMD()
*/
#define FCMD_OBJ_CMD(_obj,_cmd) _FCMD_OBJ_CMD(App,Doc,_obj,_cmd)
/** Runs a command for accessing an view object's attribute or method
* @param _obj: pointer to a DocumentObject
* @param _cmd: command string, streamable
* @sa _FCMD_OBJ_CMD()
*/
#define FCMD_VOBJ_CMD(_obj,_cmd) _FCMD_OBJ_CMD(Gui,Gui,_obj,_cmd)
/** Runs a command for accessing a document object's attribute or method
* @param _cmd: command string, supporting printf like formatter
* @param _obj: pointer to a DocumentObject
*
* Example:
* @code{.cpp}
* FCMD_OBJ_CMD2("Visibility = %s", obj, visible?"True":"False");
* @endcode
*
* Translates to command (assuming obj's document name is 'DocName', obj's name
* is 'ObjName', and visible is true):
* @code{.py}
* App.getDocument('DocName').getObject('ObjName').Visibility = True
* @endcode
*/
#define FCMD_OBJ_CMD2(_cmd,_obj,...) do{\
auto __obj = _obj;\
if(__obj && __obj->getNameInDocument()) {\
Gui::Command::doCommand(Gui::Command::Doc,"App.getDocument('%s').getObject('%s')." _cmd,\
__obj->getDocument()->getName(),__obj->getNameInDocument(),## __VA_ARGS__);\
}\
}while(0)
/** Runs a command for accessing a view object's attribute or method
* @param _cmd: command string, supporting printf like formatter
* @param _obj: pointer to a DocumentObject
* @sa FCMD_OBJ_CMD2()
*/
#define FCMD_VOBJ_CMD2(_cmd,_obj,...) do{\
auto __obj = _obj;\
if(__obj && __obj->getNameInDocument()) {\
Gui::Command::doCommand(Gui::Command::Gui,"Gui.getDocument('%s').getObject('%s')." _cmd,\
__obj->getDocument()->getName(),__obj->getNameInDocument(),## __VA_ARGS__);\
}\
}while(0)
/** Runs a command to start editing a give object
* @param _obj: pointer to a DocumentObject
*
* Unlike other FCMD macros, this macro editing the object using the current
* active document, instead of the object's owner document. This allows
* in-place editing an object, which may be brought in through linking to an
* external group.
*/
#define FCMD_SET_EDIT(_obj) do{\
auto __obj = _obj;\
if(__obj && __obj->getNameInDocument()) {\
Gui::Command::doCommand(Gui::Command::Gui,\
"Gui.ActiveDocument.setEdit(App.getDocument('%s').getObject('%s'))",\
__obj->getDocument()->getName(), __obj->getNameInDocument());\
}\
}while(0)
/// Hides an object
#define FCMD_OBJ_HIDE(_obj) FCMD_OBJ_CMD(_obj,"Visibility = False")
/// Shows an object
#define FCMD_OBJ_SHOW(_obj) FCMD_OBJ_CMD(_obj,"Visibility = True")
//@}
class QWidget;
class QByteArray;
typedef struct _object PyObject;
namespace App
{
class Document;
class DocumentObject;
}
namespace Gui {
class Action;
class Application;
class CommandManager;
class Command;
class ActionGroup;
class Document;
class SelectionSingleton;
class MDIView;
void CreateStdCommands(void);
void CreateDocCommands(void);
void CreateFeatCommands(void);
void CreateMacroCommands(void);
void CreateViewStdCommands(void);
void CreateWindowStdCommands(void);
void CreateStructureCommands(void);
void CreateTestCommands(void);
void CreateLinkCommands(void);
/** The CommandBase class
* This lightweight class is the base class of all commands in FreeCAD. It represents the link between the FreeCAD
* command framework and the QAction world of Qt.
* @author Werner Mayer
*/
class GuiExport CommandBase
{
protected:
CommandBase(const char* sMenu, const char* sToolTip=0, const char* sWhat=0,
const char* sStatus=0, const char* sPixmap=0, const char* sAccel=0);
virtual ~CommandBase();
public:
/**
* Returns the Action object of this command, or 0 if it doesn't exist.
*/
Action* getAction() const;
/** @name Methods to override when creating a new command */
//@{
protected:
/// Creates the used Action when adding to a widget. The default implementation does nothing.
virtual Action * createAction(void);
public:
/// Reassigns QAction stuff after the language has changed.
virtual void languageChange() = 0;
/// Updates the QAction with respect to the passed mode.
virtual void updateAction(int mode) = 0;
/// The C++ class name is needed as context for the translation framework
virtual const char* className() const = 0;
//@}
/** @name Methods to get the properties of the command */
//@{
virtual const char* getMenuText () const { return sMenuText; }
virtual const char* getToolTipText() const { return sToolTipText; }
virtual const char* getStatusTip () const { return sStatusTip; }
virtual const char* getWhatsThis () const { return sWhatsThis; }
virtual const char* getPixmap () const { return sPixmap; }
virtual const char* getAccel () const { return sAccel; }
//@}
/** @name Methods to set the properties of the command */
//@{
void setWhatsThis (const char*);
void setMenuText (const char*);
void setToolTipText(const char*);
void setStatusTip (const char*);
void setPixmap (const char*);
void setAccel (const char*);
//@}
protected:
/** @name Attributes set by the inherited constructor.
*
* They set up the most important properties of the command.
* In the constructor are set default values.
* The real values should be set in the constructor of the inheriting class.
*/
//@{
const char* sMenuText;
const char* sToolTipText;
const char* sWhatsThis;
const char* sStatusTip;
const char* sPixmap;
const char* sAccel;
//@}
protected:
Action *_pcAction; /**< The Action item. */
std::string displayText;
};
/** The Command class.
*
* This class is mostly used for commands implemented directly in C++ (see PythonCommand).
* It contains also a lot of helper methods to make implementing commands for FreeCAD as easy as possible.
*
* @note This class is intended to handle the GUI interaction like:
* - starting a dialog
* - doing view and window stuff
* - anything else, especially altering the document must be done on application level. See doCommand() for details.
*
* @see CommandManager
* @author Jürgen Riegel
*/
class GuiExport Command : public CommandBase
{
protected:
Command(const char* name);
virtual ~Command();
protected:
/** @name Methods to override when creating a new command
*/
//@{
/// Methods which gets called when activated, needs to be reimplemented!
virtual void activated(int iMsg)=0;
/// Creates the used Action
virtual Action * createAction(void);
/// Applies the menu text, tool and status tip to the passed action object
void applyCommandData(const char* context, Action* );
const char* keySequenceToAccel(int) const;
//@}
public:
/** @name interface used by the CommandManager and the Action */
//@{
/// CommandManager is a friend
friend class CommandManager;
/// Override this method if your Cmd is not always active
virtual bool isActive(void){return true;}
/// Get somtile called to check the state of the command
void testActive(void);
/// Enables or disables the command
void setEnabled(bool);
/// Command trigger source
enum TriggerSource {
/// No external trigger, e.g. invoked through Python
TriggerNone,
/// Command triggered by an action
TriggerAction,
/// Command triggered by a child action inside an action group
TriggerChildAction,
};
/// Return the current command trigger source
TriggerSource triggerSource() const {return _trigger;}
/** Called to invoke the command
*
* @param index: in case of group command, this is the index of the child
* command. For checkable command, this indicates the
* checkable state.
* @param trigger: indicate the command triggering source, see TriggerSource.
*/
void invoke (int index, TriggerSource trigger=TriggerNone);
/// adds this command to arbitrary widgets
void addTo(QWidget *);
void addToGroup(ActionGroup *, bool checkable);
void addToGroup(ActionGroup *);
//@}
/** @name Helper methods to get important classes */
//@{
/// Get pointer to the Application Window
static Application* getGuiApplication(void);
/// Get a reference to the selection
static Gui::SelectionSingleton& getSelection(void);
/// Get pointer to the active gui document
Gui::Document* getActiveGuiDocument(void) const;
/** Get pointer to the named or active App document
* Returns a pointer to the named document or the active
* document when no name is given. NULL is returned
* when the name does not exist or no document is active!
*/
App::Document* getDocument(const char* Name=0) const;
/// checks if the active view is of a special type or derived
bool isViewOfType(Base::Type t) const;
/// returns the named feature or the active one from the active document or NULL
App::DocumentObject* getObject(const char* Name) const;
/// returns a python command string to retrieve an object from a document
static std::string getObjectCmd(const char *Name, const App::Document *doc=0,
const char *prefix=0, const char *postfix=0, bool gui=false);
/// returns a python command string to retrieve the given object
static std::string getObjectCmd(const App::DocumentObject *obj,
const char *prefix=0, const char *postfix=0, bool gui=false);
/** Get unique Feature name from the active document
*
* @param BaseName: the base name
* @param obj: if not zero, then request the unique name in the document of
* the given object.
*/
std::string getUniqueObjectName(const char *BaseName, const App::DocumentObject *obj=0) const;
//@}
/** @name Helper methods for the Undo/Redo and Update handling */
//@{
/// Open a new Undo transaction on the active document
static void openCommand(const char* sName=0);
/// Commit the Undo transaction on the active document
static void commitCommand(void);
/// Abort the Undo transaction on the active document
static void abortCommand(void);
/// Check if an Undo transaction is open on the active document
static bool hasPendingCommand(void);
/// Updates the (active) document (propagate changes)
static void updateActive(void);
/// Updates the (all or listed) documents (propagate changes)
static void updateAll(std::list<Gui::Document*> cList);
/// Checks if the active object of the active document is valid
static bool isActiveObjectValid(void);
/// Translate command
void languageChange();
/// Updates the QAction with respect to the passed mode.
void updateAction(int mode);
/// Setup checkable actions based on current TriggerSource
void setupCheckable(int iMsg);
//@}
/** @name Helper methods for issuing commands to the Python interpreter */
//@{
/// types of application level actions for DoCommand()
enum DoCmd_Type {
/// Action alters the document
Doc,
/// Action alters only the application
App,
/// Action alters only the Gui
Gui
};
/// Blocks all command objects
static void blockCommand(bool);
/// Print to Python console the current Python calling source file and line number
static void printPyCaller();
/// Print to Python console the current calling source file and line number
static void printCaller(const char *file, int line);
//FIXME: ISO C++11 requires at least one argument for the "..." in a variadic macro
/** Convenience macro to run a command with printf like formatter
*
* @sa Command::_doCommand()
*/
#define doCommand(_type,_cmd,...) _doCommand(__FILE__,__LINE__,_type,_cmd,##__VA_ARGS__)
/** Run a command with printf like formatter
*
* @param file: the calling file path (for debugging purpose)
* @param line: the calling line number (for debugging purpose)
* @param eType: command type, See DoCmd_Type
* @param sCmd: command string that supports printf like formatter
*
* You can use the convenience macro doCommand() to automate \c file and \c
* line arguments. You may also want to use various helper @ref CommandMacros.
*/
static void _doCommand(const char *file, int line, DoCmd_Type eType,const char* sCmd,...);
/** Convenience macro to run a command
*
* @sa Command::_runCommand()
*/
#define runCommand(_type,_cmd) _runCommand(__FILE__,__LINE__,_type,_cmd)
/** Run a command
*
* @param file: the calling file path (for debugging purpose)
* @param line: the calling line number (for debugging purpose)
* @param eType: command type, See DoCmd_Type
* @param sCmd: command string
*
* @sa _doCommand()
*/
static void _runCommand(const char *file, int line, DoCmd_Type eType,const char* sCmd);
/** Run a command
*
* @param file: the calling file path (for debugging purpose)
* @param line: the calling line number (for debugging purpose)
* @param eType: command type, See DoCmd_Type
* @param sCmd: command string
*
* @sa _doCommand()
*/
static void _runCommand(const char *file, int line, DoCmd_Type eType,const QByteArray& sCmd);
/// import an external (or own) module only once
static void addModule(DoCmd_Type eType,const char* sModuleName);
/** Convenience macro to assure the switch to a certain workbench
*
* @sa _assureWorkbench()
*/
#define assureWorkbench(_name) _assureWorkbench(__FILE__,__LINE__,_name)
/** Assures the switch to a certain workbench
*
* @param file: the calling file path (for debugging purpose)
* @param line: the calling line number (for debugging purpose)
* @param sName: workbench name
*
* @return Return the current active workbench name before switching.
*
* If already in the workbench, does nothing.
*/
static std::string _assureWorkbench(const char *file, int line, const char * sName);
//@}
/** @name Methods for copying visiual properties */
//@{
/// Convenience macro to copy visual properties
#define copyVisual(...) _copyVisual(__FILE__,__LINE__,## __VA_ARGS__)
static void _copyVisual(const char *file, int line, const char* to, const char* attr, const char* from);
static void _copyVisual(const char *file, int line, const char* to, const char* attr_to, const char* from, const char* attr_from);
static void _copyVisual(const char *file, int line, const App::DocumentObject *to, const char *attr, const App::DocumentObject *from);
static void _copyVisual(const char *file, int line, const App::DocumentObject *to, const char *attr_to, const App::DocumentObject *from, const char *attr_from);
//@}
/// Get Python tuple from object and sub-elements
static std::string getPythonTuple(const std::string& name, const std::vector<std::string>& subnames);
/// translate a string to a python string literal (needed e.g. in file names for windows...)
const std::string strToPython(const char* Str);
const std::string strToPython(const std::string &Str){
return strToPython(Str.c_str());
}
/** @name Helper methods to generate help pages */
//@{
/// returns the begin of a online help page
const char * beginCmdHelp(void);
/// returns the end of a online help page
const char * endCmdHelp(void);
/// Get the help URL
virtual const char* getHelpUrl(void) const { return sHelpUrl; }
//@}
/** @name Helper methods for the Active tests */
//@{
/// true when there is a document
bool hasActiveDocument(void) const;
/// true when there is a document and a Feature with Name
bool hasObject(const char* Name);
//@}
/** @name checking of internal state */
//@{
/// returns the name to which the command belongs
const char* getAppModuleName(void) const {return sAppModule;}
void setAppModuleName(const char*);
/// Get the command name
const char* getName() const { return sName; }
/// Get the name of the grouping of the command
const char* getGroupName() const { return sGroup; }
void setGroupName(const char*);
//@}
/** @name arbitrary helper methods */
//@{
void adjustCameraPosition();
//@}
/// Helper class to disable python console log
class LogDisabler {
public:
LogDisabler() {
++Command::_busy;
}
~LogDisabler() {
--Command::_busy;
}
};
friend class LogDisabler;
protected:
enum CmdType {
AlterDoc = 1, /**< Command change the Document */
Alter3DView = 2, /**< Command change the Gui */
AlterSelection = 4, /**< Command change the Selection */
ForEdit = 8, /**< Command is in a special edit mode active */
NoTransaction = 16, /**< Do not setup auto transaction */
};
/** @name Attributes
* Set by the inherited constructor to set up the most important properties
* of the command. In the Command constructor are set default values!
* The real values should be set in the constructor of the inhereting class.
*/
//@{
const char* sAppModule;
const char* sGroup;
const char* sName;
const char* sHelpUrl;
int eType;
/// Indicate if the command shall log to MacroManager
bool bCanLog;
//@}
private:
static int _busy;
bool bEnabled;
static bool _blockCmd;
/// For storing the current command trigger source
TriggerSource _trigger = TriggerNone;
};
/** Class to help implement a group command
*
* To use this class, simply add children command in the constructor of your
* derived class by calling addCommand();
*/
class GuiExport GroupCommand : public Command {
public:
/// Constructor
GroupCommand(const char *name);
/** Add child command
* @param cmd: child command. Pass null pointer to add a separator.
* @param reg: whether to register the command with CommandManager
* @return Return the command index.
*/
int addCommand(Command *cmd = 0, bool reg=true);
/** Add child command
* @param cmd: child command name.
* @return Return the found command, or NULL if not found.
*/
Command *addCommand(const char *cmdName);
protected:
virtual void activated(int iMsg);
virtual Gui::Action * createAction(void);
virtual void languageChange();
void setup(Action *);
protected:
std::vector<std::pair<Command*,size_t> > cmds;
};
/** The Python command class
* This is a special type of command class. It's used to bind a Python command class into the
* FreeCAD command framework.
* An object of this class gets a reference to the Python command object and manages all the
* passing between the C++ and the Python world. This includes everything like setting resources such as
* bitmaps, activation or bindings to the user interface.
* @see CommandManager
* @author Jürgen Riegel
*/
class PythonCommand: public Command
{
public:
PythonCommand(const char* name, PyObject * pcPyCommand, const char* pActivationString);
virtual ~PythonCommand();
protected:
/** @name Methods reimplemented for Command Framework */
//@{
/// Method which gets called when activated
virtual void activated(int iMsg);
/// if the command is not always active
virtual bool isActive(void);
/// Get the help URL
const char* getHelpUrl(void) const;
/// Creates the used Action
virtual Action * createAction(void);
//@}
public:
/** @name Methods to get the properties of the command */
//@{
/// Reassigns QAction stuff after the language has changed.
void languageChange();
const char* className() const
{ return "PythonCommand"; }
const char* getWhatsThis () const;
const char* getMenuText () const;
const char* getToolTipText() const;
const char* getStatusTip () const;
const char* getPixmap () const;
const char* getAccel () const;
bool isCheckable () const;
bool isChecked () const;
//@}
protected:
/// Returns the resource values
const char* getResource(const char* sName) const;
/// a pointer to the Python command object
PyObject * _pcPyCommand;
/// the command object resource dictionary
PyObject * _pcPyResourceDict;
/// the activation sequence
std::string Activation;
};
/** The Python group command class
* @see CommandManager
* @author Werner Mayer
*/
class PythonGroupCommand: public Command
{
public:
PythonGroupCommand(const char* name, PyObject * pcPyCommand);
virtual ~PythonGroupCommand();
protected:
/** @name Methods reimplemented for Command Framework */
//@{
/// Method which gets called when activated
virtual void activated(int iMsg);
/// if the command is not always active
virtual bool isActive(void);
/// Get the help URL
const char* getHelpUrl(void) const;
/// Creates the used Action
virtual Action * createAction(void);
//@}
public:
/** @name Methods to get the properties of the command */
//@{
/// Reassigns QAction stuff after the language has changed.
void languageChange();
const char* className() const
{ return "PythonGroupCommand"; }
const char* getWhatsThis () const;
const char* getMenuText () const;
const char* getToolTipText() const;
const char* getStatusTip () const;
const char* getPixmap () const;
const char* getAccel () const;
bool isExclusive () const;
bool hasDropDownMenu () const;
//@}
protected:
/// Returns the resource values
const char* getResource(const char* sName) const;
/// a pointer to the Python command object
PyObject * _pcPyCommand;
/// the command object resources
PyObject * _pcPyResource;
};
/** The script command class
* This is a special type of command class. Its used to bind a macro or Python script to the
* FreeCAD command framework.
* An object of this class gets a string to the place where the script is in the file system.
* Unlike the other commands the resources can be set by several methods.
* @see Command
* @see CommandManager
* @author Werner Mayer
*/
class MacroCommand: public Command
{
public:
MacroCommand(const char* name, bool system = false);
virtual ~MacroCommand();
protected:
/** @name methods reimplemented for Command Framework */
//@{
/// Method which get called when activated
void activated(int iMsg);
/// Creates the used Action
Action * createAction(void);
//@}
public:
/// Returns the script name
const char* getScriptName () const { return sScriptName; }
/// Ignore when language has changed.
void languageChange() {}
const char* className() const
{ return "Gui::MacroCommand"; }
/** @name Methods to set the properties of the Script Command */
//@{
/// Sets the script name
void setScriptName ( const char* );
//@}
/** @name Methods to load and save macro commands. */
//@{
/** Loads all macros command from the preferences. */
static void load();
/** Saves all macros command to the preferences. */
static void save();
//@}
protected:
const char* sScriptName;
bool systemMacro;
};
/** The CommandManager class
* This class manage all available commands in FreeCAD. All
* Commands will registered here, also commands from Application
* modules. Also activation / deactivation, Icons Tooltips and so
* on are handles here. Further the Building of Toolbars and (Context)
* menus (connecting to a QAction) is done here.
* @see Command
* @author Jürgen Riegel
*/
class GuiExport CommandManager
{
public:
/// Construction
CommandManager();
/// Destruction
~CommandManager();
/// Insert a new command into the manager
void addCommand(Command* pCom);
/// Remove a command from the manager
void removeCommand(Command* pCom);
/// Adds the given command to a given widget
bool addTo(const char* Name, QWidget* pcWidget);
/** Returns all commands of a special App Module
* delivers a vector of all commands in the given application module. When no
* name is given the standard commands (build in ) are returned.
* @see Command
*/
std::vector <Command*> getModuleCommands(const char *sModName) const;
/** Returns all commands registered in the manager
* delivers a vector of all commands. If you intereted in commands of
* of a special app module use GetModuleCommands()
* @see Command
*/
std::vector <Command*> getAllCommands(void) const;
/** Returns all commands of a group
* delivers a vector of all commands in the given group.
*/
std::vector <Command*> getGroupCommands(const char *sGrpName) const;
/** Returns the command registered in the manager with the name sName
* If nothing is found it returns a null pointer
* @see Command
*/
Command* getCommandByName(const char* sName) const;
/**
* Runs the command
*/
void runCommandByName (const char* sName) const;
/// method is OBSOLETE use GetModuleCommands() or GetAllCommands()
const std::map<std::string, Command*>& getCommands() const { return _sCommands; }
/// get frequently called by the AppWnd to check the commands are active.
void testActive(void);
void addCommandMode(const char* sContext, const char* sName);
void updateCommands(const char* sContext, int mode);
private:
/// Destroys all commands in the manager and empties the list.
void clearCommands();
std::map<std::string, Command*> _sCommands;
std::map<std::string, std::list<std::string> > _sCommandModes;
};
} // namespace Gui
/** The Command Macro Standard
* This macro makes it easier to define a new command.
* The parameters are the class name.
* @author Jürgen Riegel
*/
#define DEF_STD_CMD(X) class X : public Gui::Command \
{\
public:\
X();\
virtual const char* className() const\
{ return #X; }\
protected: \
virtual void activated(int iMsg);\
};
/** The Command Macro Standard + isActive()
* This macro makes it easier to define a new command.
* The parameters are the class name
* @author Jürgen Riegel
*/
#define DEF_STD_CMD_A(X) class X : public Gui::Command \
{\
public:\
X();\
virtual ~X(){}\
virtual const char* className() const\
{ return #X; }\
protected: \
virtual void activated(int iMsg);\
virtual bool isActive(void);\
};
/** The Command Macro Standard + createAction()
* This macro makes it easier to define a new command.
* The parameters are the class name
* @author Jürgen Riegel
*/
#define DEF_STD_CMD_C(X) class X : public Gui::Command \
{\
public:\
X();\
virtual ~X(){}\
virtual const char* className() const\
{ return #X; }\
protected: \
virtual void activated(int iMsg);\
virtual Gui::Action * createAction(void);\
};
/** The Command Macro Standard + isActive() + createAction()
* This macro makes it easier to define a new command.
* The parameters are the class name
* @author Werner Mayer
*/
#define DEF_STD_CMD_AC(X) class X : public Gui::Command \
{\
public:\
X();\
virtual ~X(){}\
virtual const char* className() const\
{ return #X; }\
protected: \
virtual void activated(int iMsg);\
virtual bool isActive(void);\
virtual Gui::Action * createAction(void);\
};
/** The Command Macro Standard + isActive() + updateAction()
* This macro makes it easier to define a new command.
* The parameters are the class name
* @author Werner Mayer
*/
#define DEF_STD_CMD_AU(X) class X : public Gui::Command \
{\
public:\
X();\
virtual ~X(){}\
virtual void updateAction(int mode); \
virtual const char* className() const\
{ return #X; }\
protected: \
virtual void activated(int iMsg);\
virtual bool isActive(void);\
};
/** The Command Macro Standard + isActive() + createAction()
* + languageChange()
* This macro makes it easier to define a new command.
* The parameters are the class name
* @author Werner Mayer
*/
#define DEF_STD_CMD_ACL(X) class X : public Gui::Command \
{\
public:\
X();\
virtual ~X(){}\
virtual void languageChange(); \
virtual const char* className() const\
{ return #X; }\
protected: \
virtual void activated(int iMsg);\
virtual bool isActive(void);\
virtual Gui::Action * createAction(void);\
};
/** The Command Macro Standard + isActive() + createAction()
* + languageChange() + updateAction()
* This macro makes it easier to define a new command.
* The parameters are the class name
* @author Werner Mayer
*/
#define DEF_STD_CMD_ACLU(X) class X : public Gui::Command \
{\
public:\
X();\
virtual ~X(){}\
virtual void languageChange(); \
virtual void updateAction(int mode); \
virtual const char* className() const\
{ return #X; }\
protected: \
virtual void activated(int iMsg);\
virtual bool isActive(void);\
virtual Gui::Action * createAction(void);\
};