1
/***************************************************************************
2
* Copyright (c) 2002 J�rgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
37
#include <Base/PyExport.h>
38
#include <Base/Type.h>
45
class AbstractFeature;
57
class SelectionSingleton;
61
void CreateStdCommands(void);
62
void CreateDocCommands(void);
63
void CreateFeatCommands(void);
64
void CreateViewStdCommands(void);
65
void CreateWindowStdCommands(void);
66
void CreateTestCommands(void);
69
/** The CommandBase class
70
* This lightweigt class is the base class of all commands in FreeCAD. It represents the link between the FreeCAD
71
* command framework and the QAction world of Qt.
72
* @author Werner Mayer
74
class GuiExport CommandBase
77
CommandBase( const char* sMenu, const char* sToolTip=0, const char* sWhat=0,
78
const char* sStatus=0, const char* sPixmap=0, int accel=0);
79
virtual ~CommandBase();
82
* Returns the Action object of this command, or 0 if it doesn't exist.
84
Action* getAction() const;
86
/** @name Methods to override when creating a new command */
89
/// Creates the used Action when adding to a widget. The default implementation does nothing.
90
virtual Action * createAction(void);
92
/// Reassigns QAction stuff after the language has changed.
93
virtual void languageChange();
96
/** @name Methods to get the properties of the command */
98
virtual const char* getMenuText () const { return sMenuText; }
99
virtual const char* getToolTipText() const { return sToolTipText; }
100
virtual const char* getStatusTip () const { return sStatusTip; }
101
virtual const char* getWhatsThis () const { return sWhatsThis; }
102
virtual const char* getPixmap () const { return sPixmap; }
103
virtual int getAccel () const { return iAccel; }
106
/** @name Methods to set the properties of the command */
108
void setWhatsThis ( const char* );
109
void setMenuText ( const char* );
110
void setToolTipText( const char* );
111
void setStatusTip ( const char* );
112
void setPixmap ( const char* );
113
void setAccel ( int );
118
* Set by the inherited constructor to set up the most important properties
119
* of the command. In the constructor are set default values.
120
* The real values should be set in the constructor of the inheriting class.
123
const char* sMenuText;
124
const char* sToolTipText;
125
const char* sWhatsThis;
126
const char* sStatusTip;
131
Action *_pcAction; /**< The Action item. */
134
/** The Command class
135
* This class is mostly used for commands implemented directly in C++ (@see PythonCommand).
136
* It contains also a lot of helper methods to make implementing commands for FreeCAD as easy as possible.
138
* @note This class is intended to handle the GUI interaction like:
139
* - starting a dialog
140
* - doing view and window stuff
141
* - anything else, especially altering the document must be done on application level @see doCommand() for details.
143
* @see CommandManager
144
* @author J�rgen Riegel
146
class GuiExport Command : public CommandBase
149
Command( const char* name );
153
/** @name Methods to override when creating a new command
156
/// Methods which gets called when activated, needs to be reimplemented!
157
virtual void activated(int iMsg)=0;
158
/// Overite this method if your Cmd is not always active
159
virtual bool isActive(void){return true;}
160
/// Creates the used Action
161
virtual Action * createAction(void);
165
/** @name interface used by the CommandManager and the Action */
167
/// CommandManager is a friend
168
friend class CommandManager;
169
/// Get somtile called to check the state of the command
170
void testActive(void);
171
/// get called by the QAction
173
/// adds this command to arbitrary widgets
174
void addTo(QWidget *);
178
/** @name Helper methods to get important classes */
180
/// Get pointer to the Application Window
181
static Application* getGuiApplication(void);
182
/// Get a referenc to the selection
183
Gui::SelectionSingleton& getSelection(void);
184
/// Get pointer to the active gui document
185
Gui::Document* getActiveGuiDocument(void);
186
/** Get pointer to the named or active App document
187
* Returns a pointer to the named docuement or the active
188
* document when no name is given. NULL is returnd
189
* when the name does not exist or no document is active!
191
App::Document* getDocument(const char* Name=0);
192
/// checks if the active view is of a special type or derived
193
bool isViewOfType(Base::Type t) const;
194
/// returns the named feature or the active one from the active document or NULL
195
App::AbstractFeature* getFeature(const char* Name);
196
App::DocumentObject* getObject(const char* Name);
197
/// Get unique Feature name from the active document
198
std::string getUniqueObjectName(const char *BaseName);
201
/** @name Helper methods for the Undo/Redo and Update handling */
203
/// Open a new Undo transaction on the active document
204
void openCommand(const char* sName=0);
205
/// Commit the Undo transaction on the active document
206
void commitCommand(void);
207
/// Abort the Undo transaction on the active document
208
void abortCommand(void);
209
/// Updates the (active) document (propagate changes)
210
void updateActive(void);
211
/// Updates the (all or listed) documents (propagate changes)
212
void updateAll(std::list<Gui::Document*> cList);
215
/** @name Helper methods for issuing commands to the Python interpreter */
217
/// types of application level actions for DoCommand()
219
/// Action alters the document
221
/// Action alters only the application
223
/// Action alters only the Gui
226
/// Blocks all command objects
227
static void blockCommand(bool);
228
/// Run a App level Action
229
static void doCommand(DoCmd_Type eType,const char* sCmd,...);
230
/// translate a string to a python string literal (needed e.g. in file names for windows...)
231
const std::string strToPython(const char* Str);
232
const std::string strToPython(const std::string &Str){return strToPython(Str.c_str());};
235
/** @name Helper methods to generate help pages */
237
/// returns the begin of a online help page
238
const char * beginCmdHelp(void);
239
/// returns the end of a online help page
240
const char * endCmdHelp(void);
242
virtual const char* getHelpUrl(void) const { return sHelpUrl; }
245
/** @name Helper methods for the Active tests */
247
/// true when there is a document
248
bool hasActiveDocument(void);
249
/// true when there is a document and a Feature with Name
250
bool hasObject(const char* Name);
253
/** @name checking of internal state */
255
/// returns the name to which the command belongs
256
const char* getAppModuleName(void) const {return sAppModule;}
257
/// Get the command name
258
const char* getName() const { return sName; }
259
/// Get the name of the grouping of the command
260
const char* getGroupName() const { return sGroup; }
265
* Set by the inherited constructor to set up the most important properties
266
* of the command. In the Command constructor are set default values!
267
* The real values should be set in the constructor of the inhereting class.
270
const char* sAppModule;
273
const char* sHelpUrl;
276
static bool _blockCmd;
279
/** The Python command class
280
* This is a special type of command class. It's used to bind a Python command class into the
281
* FreeCAD command framework.
282
* An object of this class gets a reference to the Python command object and manages all the
283
* passing between the C++ and the Python world. This includes everything like setting resources such as
284
* bitmaps, activation or bindings to the user interface.
285
* @see CommandManager
286
* @author J�rgen Riegel
288
class PythonCommand: public Command
291
PythonCommand(const char* name,PyObject * pcPyCommand, const char* pActivationString);
292
virtual ~PythonCommand() {}
295
/** @name Methods reimplemented for Command Framework */
297
/// Method which gets called when activated
298
virtual void activated(int iMsg);
299
/// if the command is not always active
300
virtual bool isActive(void);
302
const char* getHelpUrl(void);
303
/// Creates the used Action
304
virtual Action * createAction(void);
308
/** @name Methods to get the properties of the command */
310
/// Reassigns QAction stuff after the language has changed.
311
void languageChange();
312
const char* getWhatsThis () const;
313
const char* getMenuText () const;
314
const char* getToolTipText() const;
315
const char* getStatusTip () const;
316
const char* getPixmap () const;
317
int getAccel () const;
321
/// Returns the resource values
322
const char* getResource(const char* sName) const;
323
/// a pointer to the Python command object
324
PyObject * _pcPyCommand;
325
/// the command object resource dictionary
326
PyObject * _pcPyResourceDict;
327
/// the activation sequence
328
std::string Activation;
332
/** The script command class
333
* This is a special type of command class. Its used to bind a macro or Python script to the
334
* FreeCAD command framework.
335
* An object of this class gets a string to the place where the script is in the file system.
336
* Unlike the other commands the resources can be set by several methods.
338
* @see CommandManager
339
* @author Werner Mayer
341
class MacroCommand: public Command
344
MacroCommand(const char* name);
345
virtual ~MacroCommand() {}
348
/** @name methods reimplemented for Command Framework */
350
/// Method which get called when activated
351
void activated(int iMsg);
352
/// Creates the used Action
353
Action * createAction(void);
357
/// Returns the script name
358
const char* getScriptName () const { return sScriptName; }
359
/// Ignore when language has changed.
360
void languageChange() {}
362
/** @name Methods to set the properties of the Script Command */
364
/// Sets the script name
365
void setScriptName ( const char* );
368
/** @name Methods to load and save macro commands. */
370
/** Loads all macros command from the preferences. */
372
/** Saves all macros command to the preferences. */
377
const char* sScriptName;
380
/** The CommandManager class
381
* This class manage all available commands in FreeCAD. All
382
* Commands will registered here, also commands from Application
383
* modules. Also activation / deactivation, Icons Tooltips and so
384
* on are handles here. Further the Building of Toolbars and (Context)
385
* menus (connecting to a QAction) is done here.
387
* @author J�rgen Riegel
389
class GuiExport CommandManager
396
/// Insert a new command into the manager
397
void addCommand(Command* pCom);
398
/// Remove a command from the manager
399
void removeCommand(Command* pCom);
401
/// Adds the given command to a given widget
402
bool addTo(const char* Name,QWidget *pcWidget);
404
/** Returns all commands of a special App Module
405
* delivers a vector of all comands in the given application module. When no
406
* name is given the standard commands (build in ) are returned.
409
std::vector <Command*> getModuleCommands(const char *sModName) const;
411
/** Returns all commands registered in the manager
412
* delivers a vector of all comands. If you intereted in commands of
413
* of a special app module use GetModuleCommands()
416
std::vector <Command*> getAllCommands(void) const;
418
/** Returns all commands of a group
419
* delivers a vector of all comands in the given group.
421
std::vector <Command*> getGroupCommands(const char *sGrpName) const;
423
/** Returns the command registered in the manager with the name sName
424
* If nothing is found it returns a null pointer
427
Command* getCommandByName(const char* sName) const;
432
void runCommandByName (const char* sName) const;
434
/// method is OBSOLET use GetModuleCommands() or GetAllCommands()
435
const std::map<std::string, Command*>& getCommands() const { return _sCommands; }
436
/// get frequently called by the AppWnd to check the commands are active
437
void testActive(void);
439
/// Destroys all commands in the manager and empties the list.
440
void clearCommands();
441
std::map<std::string,Command*> _sCommands;
448
/** The Command Macro Standard
449
* This macro makes it easier to define a new command.
450
* The parameters are the class name.
451
* @author J�rgen Riegel
453
#define DEF_STD_CMD(X) class X : public Gui::Command \
458
virtual void activated(int iMsg);\
461
/** The Command Macro Standard + isActive()
462
* This macro makes it easier to define a new command.
463
* The parameters are the class name
464
* @author J�rgen Riegel
466
#define DEF_STD_CMD_A(X) class X : public Gui::Command \
472
virtual void activated(int iMsg);\
473
virtual bool isActive(void);\
476
/** The Command Macro Standard + createAction()
477
* This macro makes it easier to define a new command.
478
* The parameters are the class name
479
* @author J�rgen Riegel
481
#define DEF_STD_CMD_C(X) class X : public Gui::Command \
487
virtual void activated(int iMsg);\
488
virtual Gui::Action * createAction(void);\
491
/** The Command Macro Standard + isActive() + createAction()
492
* This macro makes it easier to define a new command.
493
* The parameters are the class name
494
* @author Werner Mayer
496
#define DEF_STD_CMD_AC(X) class X : public Gui::Command \
502
virtual void activated(int iMsg);\
503
virtual bool isActive(void);\
504
virtual Gui::Action * createAction(void);\
507
/** The Command Macro Standard + isActive() + createAction()
509
* This macro makes it easier to define a new command.
510
* The parameters are the class name
511
* @author Werner Mayer
513
#define DEF_STD_CMD_ACL(X) class X : public Gui::Command \
518
virtual void languageChange(); \
520
virtual void activated(int iMsg);\
521
virtual bool isActive(void);\
522
virtual Gui::Action * createAction(void);\
525
/** The Command Macro view
526
* This macro makes it easyer to define a new command for the 3D View
527
* It activate the command only when a 3DView is active.
528
* The parameters are the class name
529
* @author J�rgen Riegel
531
#define DEF_3DV_CMD(X) class X : public Gui::Command \
537
virtual void activated(int iMsg);\
538
virtual bool isActive(void)\
540
Gui::MDIView* view = getMainWindow()->activeWindow();\
541
return view && view->isDerivedFrom(Gui::View3DInventor::getClassTypeId());\
545
#endif // GUI_COMMAND_H