~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Gui/Command.h

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
Import upstream version 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) 2002 J�rgen Riegel <juergen.riegel@web.de>              *
 
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
#ifndef __Qt4All__
 
28
# include "Qt4All.h"
 
29
#endif
 
30
 
 
31
 
 
32
#include <list>
 
33
#include <map>
 
34
#include <string>
 
35
#include <vector>
 
36
 
 
37
#include <Base/PyExport.h>
 
38
#include <Base/Type.h>
 
39
 
 
40
class QWidget;
 
41
 
 
42
namespace App
 
43
{
 
44
  class Document;
 
45
  class AbstractFeature;
 
46
  class DocumentObject;
 
47
}
 
48
 
 
49
namespace Gui {
 
50
 
 
51
class Action;
 
52
class Application;
 
53
class CommandManager;
 
54
class Command;
 
55
class ActionGroup;
 
56
class Document;
 
57
class SelectionSingleton;
 
58
class MDIView;
 
59
 
 
60
 
 
61
void CreateStdCommands(void);
 
62
void CreateDocCommands(void);
 
63
void CreateFeatCommands(void);
 
64
void CreateViewStdCommands(void);
 
65
void CreateWindowStdCommands(void);
 
66
void CreateTestCommands(void);
 
67
 
 
68
 
 
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
 
73
 */
 
74
class GuiExport CommandBase 
 
75
{
 
76
public:
 
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();
 
80
 
 
81
  /**
 
82
   * Returns the Action object of this command, or 0 if it doesn't exist.
 
83
   */
 
84
  Action*  getAction() const;
 
85
 
 
86
  /** @name Methods to override when creating a new command */
 
87
  //@{
 
88
protected:
 
89
  /// Creates the used Action when adding to a widget. The default implementation does nothing.
 
90
  virtual Action * createAction(void);
 
91
public:
 
92
  /// Reassigns QAction stuff after the language has changed. 
 
93
  virtual void languageChange();
 
94
  //@}
 
95
 
 
96
  /** @name Methods to get the properties of the command */
 
97
  //@{
 
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;       }
 
104
  //@}
 
105
 
 
106
  /** @name Methods to set the properties of the command */
 
107
  //@{
 
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         );
 
114
  //@}
 
115
 
 
116
protected:
 
117
  /** @name Attributes 
 
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.
 
121
   */
 
122
  //@{
 
123
  const char* sMenuText;
 
124
  const char* sToolTipText;
 
125
  const char* sWhatsThis;
 
126
  const char* sStatusTip;
 
127
  const char* sPixmap;
 
128
  int         iAccel;
 
129
  //@}
 
130
protected:
 
131
  Action *_pcAction; /**< The Action item. */
 
132
};
 
133
 
 
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.
 
137
 *
 
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.
 
142
 *
 
143
 * @see CommandManager
 
144
 * @author J�rgen Riegel
 
145
 */
 
146
class GuiExport Command : public CommandBase
 
147
{
 
148
public:
 
149
  Command( const char* name );
 
150
  virtual ~Command();
 
151
 
 
152
protected:
 
153
  /** @name Methods to override when creating a new command
 
154
   */
 
155
  //@{
 
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);
 
162
  //@}
 
163
 
 
164
public:
 
165
  /** @name interface used by the CommandManager and the Action */
 
166
  //@{
 
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
 
172
  void invoke (int); 
 
173
  /// adds this command to arbitrary widgets
 
174
  void addTo(QWidget *);
 
175
  //@}
 
176
 
 
177
 
 
178
  /** @name Helper methods to get important classes */
 
179
  //@{
 
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!
 
190
   */
 
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);
 
199
  //@}
 
200
 
 
201
  /** @name Helper methods for the Undo/Redo and Update handling */
 
202
  //@{
 
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);
 
213
  //@}
 
214
 
 
215
  /** @name Helper methods for issuing commands to the Python interpreter */
 
216
  //@{
 
217
  /// types of application level actions for DoCommand()
 
218
  enum DoCmd_Type {
 
219
    /// Action alters the document
 
220
    Doc,
 
221
    /// Action alters only the application
 
222
    App,
 
223
    /// Action alters only the Gui
 
224
    Gui
 
225
  };
 
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());};
 
233
  //@}
 
234
 
 
235
  /** @name Helper methods to generate help pages */
 
236
  //@{
 
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);
 
241
  /// Get the help URL
 
242
  virtual const char* getHelpUrl(void) const { return sHelpUrl; }
 
243
  //@}
 
244
 
 
245
  /** @name Helper methods for the Active tests */
 
246
  //@{
 
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);
 
251
  //@}
 
252
 
 
253
  /** @name checking of internal state */
 
254
  //@{
 
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; }
 
261
  //@}
 
262
 
 
263
protected:
 
264
  /** @name Attributes 
 
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.
 
268
   */
 
269
  //@{
 
270
  const char* sAppModule;
 
271
  const char* sGroup;
 
272
  const char* sName;
 
273
  const char* sHelpUrl;
 
274
  //@}
 
275
private:
 
276
  static bool _blockCmd;
 
277
};
 
278
 
 
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
 
287
 */
 
288
class PythonCommand: public Command
 
289
{
 
290
public:
 
291
  PythonCommand(const char* name,PyObject * pcPyCommand, const char* pActivationString);
 
292
  virtual ~PythonCommand() {}
 
293
 
 
294
protected:
 
295
  /** @name Methods reimplemented for Command Framework */
 
296
  //@{
 
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);
 
301
  /// Get the help URL
 
302
  const char* getHelpUrl(void);
 
303
  /// Creates the used Action
 
304
  virtual Action * createAction(void);
 
305
  //@}
 
306
 
 
307
public:
 
308
  /** @name Methods to get the properties of the command */
 
309
  //@{
 
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;
 
318
  //@}
 
319
 
 
320
protected:
 
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;
 
329
};
 
330
 
 
331
 
 
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.
 
337
 * @see Command
 
338
 * @see CommandManager
 
339
 * @author Werner Mayer
 
340
 */
 
341
class MacroCommand: public Command
 
342
{
 
343
public:
 
344
  MacroCommand(const char* name);
 
345
  virtual ~MacroCommand() {}
 
346
 
 
347
protected:
 
348
  /** @name methods reimplemented for Command Framework */
 
349
  //@{
 
350
  /// Method which get called when activated
 
351
  void activated(int iMsg);
 
352
  /// Creates the used Action
 
353
  Action * createAction(void);
 
354
  //@}
 
355
 
 
356
public:
 
357
  /// Returns the script name
 
358
  const char* getScriptName () const { return sScriptName; }
 
359
  /// Ignore when language has changed. 
 
360
  void languageChange() {}
 
361
 
 
362
  /** @name Methods to set the properties of the Script Command */
 
363
  //@{
 
364
  /// Sets the script name
 
365
  void setScriptName ( const char* );
 
366
  //@}
 
367
 
 
368
  /** @name Methods to load and save macro commands. */
 
369
  //@{
 
370
  /** Loads all macros command from the preferences. */
 
371
  static void load();
 
372
  /** Saves all macros command to the preferences. */
 
373
  static void save();
 
374
  //@}
 
375
 
 
376
protected:
 
377
  const char* sScriptName;
 
378
};
 
379
 
 
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.
 
386
 *  @see Command
 
387
 *  @author J�rgen Riegel
 
388
 */
 
389
class GuiExport CommandManager
 
390
{
 
391
public:
 
392
  /// Construction
 
393
  CommandManager();
 
394
  /// Destruction
 
395
  ~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);
 
400
 
 
401
  /// Adds the given command to a given widget
 
402
  bool addTo(const char* Name,QWidget *pcWidget);
 
403
 
 
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.
 
407
   *  @see Command
 
408
   */
 
409
  std::vector <Command*> getModuleCommands(const char *sModName) const;
 
410
 
 
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()
 
414
   *  @see Command
 
415
   */
 
416
  std::vector <Command*> getAllCommands(void) const;
 
417
 
 
418
  /** Returns all commands of a group
 
419
   *  delivers a vector of all comands in the given group.
 
420
   */
 
421
  std::vector <Command*> getGroupCommands(const char *sGrpName) const;
 
422
 
 
423
  /** Returns the command registered in the manager with the name sName
 
424
   *  If nothing is found it returns a null pointer
 
425
   *  @see Command
 
426
   */
 
427
  Command* getCommandByName(const char* sName) const;
 
428
 
 
429
  /**
 
430
   * Runs the command
 
431
   */
 
432
  void runCommandByName (const char* sName) const;
 
433
 
 
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);
 
438
private:
 
439
  /// Destroys all commands in the manager and empties the list.
 
440
  void clearCommands();
 
441
  std::map<std::string,Command*> _sCommands;
 
442
};
 
443
 
 
444
} // namespace Gui
 
445
 
 
446
 
 
447
 
 
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
 
452
 */
 
453
#define DEF_STD_CMD(X) class X : public Gui::Command \
 
454
{\
 
455
public:\
 
456
  X();\
 
457
protected: \
 
458
  virtual void activated(int iMsg);\
 
459
};
 
460
 
 
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
 
465
 */
 
466
#define DEF_STD_CMD_A(X) class X : public Gui::Command \
 
467
{\
 
468
public:\
 
469
  X();\
 
470
  virtual ~X(){}\
 
471
protected: \
 
472
  virtual void activated(int iMsg);\
 
473
  virtual bool isActive(void);\
 
474
};
 
475
 
 
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
 
480
 */
 
481
#define DEF_STD_CMD_C(X) class X : public Gui::Command \
 
482
{\
 
483
public:\
 
484
  X();\
 
485
  virtual ~X(){}\
 
486
protected: \
 
487
  virtual void activated(int iMsg);\
 
488
  virtual Gui::Action * createAction(void);\
 
489
};
 
490
 
 
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
 
495
 */
 
496
#define DEF_STD_CMD_AC(X) class X : public Gui::Command \
 
497
{\
 
498
public:\
 
499
  X();\
 
500
  virtual ~X(){}\
 
501
protected: \
 
502
  virtual void activated(int iMsg);\
 
503
  virtual bool isActive(void);\
 
504
  virtual Gui::Action * createAction(void);\
 
505
};
 
506
 
 
507
/** The Command Macro Standard + isActive() + createAction()
 
508
 *  + languageChange()
 
509
 *  This macro makes it easier to define a new command.
 
510
 *  The parameters are the class name
 
511
 *  @author Werner Mayer
 
512
 */
 
513
#define DEF_STD_CMD_ACL(X) class X : public Gui::Command \
 
514
{\
 
515
public:\
 
516
  X();\
 
517
  virtual ~X(){}\
 
518
  virtual void languageChange(); \
 
519
protected: \
 
520
  virtual void activated(int iMsg);\
 
521
  virtual bool isActive(void);\
 
522
  virtual Gui::Action * createAction(void);\
 
523
};
 
524
 
 
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
 
530
 */
 
531
#define DEF_3DV_CMD(X) class X : public Gui::Command \
 
532
{\
 
533
public:\
 
534
  X();\
 
535
  virtual ~X(){}\
 
536
protected: \
 
537
  virtual void activated(int iMsg);\
 
538
  virtual bool isActive(void)\
 
539
  {\
 
540
    Gui::MDIView* view = getMainWindow()->activeWindow();\
 
541
    return view && view->isDerivedFrom(Gui::View3DInventor::getClassTypeId());\
 
542
  }\
 
543
};
 
544
 
 
545
#endif // GUI_COMMAND_H