QCAD
Open Source 2D CAD
RGuiAction.h
Go to the documentation of this file.
1 
20 #ifndef RGUIACTION_H
21 #define RGUIACTION_H
22 
23 #include "core_global.h"
24 
25 #include <QFileInfo>
26 #include <QMap>
27 #include <QMetaType>
28 #include <QString>
29 #include <QStringList>
30 #include <QList>
31 #include <QAction>
32 
33 #include "RTransactionListener.h"
34 #include "RFocusListener.h"
35 #include "RSelectionListener.h"
36 
37 class QToolBar;
38 
39 class RDocument;
40 class RDocumentInterface;
41 
61  public QAction,
62  public RFocusListener,
63  public RTransactionListener,
64  public RSelectionListener {
65 
66 Q_OBJECT
67 
68 public:
69  typedef void (*FactoryFunction)(RGuiAction*);
70 
71 public:
72  RGuiAction(const QString& oriText, QObject* parent = 0);
73  virtual ~RGuiAction();
74 
75  void setText(const QString& oriText);
76 
81  virtual void initTexts();
82 
83  virtual void init();
84 
85  static QString formatToolTip(const QString& oriText, const QString& shortcut);
86  QString getToolTip(const QString& oriText, const QString& shortcut);
87 
88  void setDocumentInterface(RDocumentInterface* di);
90 
96  void setShortcut(const QKeySequence& shortcut);
97  void setDefaultShortcut(const QKeySequence& shortcut);
98  void setShortcuts(const QList<QKeySequence>& shortcuts);
99  void setDefaultShortcuts(const QList<QKeySequence>& shortcuts);
100  QList<QKeySequence> getDefaultShortcuts();
101  void setShortcutText(const QString& oriText);
102  QString getShortcutText() const;
103 
104  static void setGroupSortOrderStatic(QAction* a, int sortOrder);
105  static void setGroupSortOrderOverrideStatic(QAction* a, const QString& widgetName, int sortOrder);
106  static int getGroupSortOrderStatic(const QAction* a, const QWidget* w = NULL);
107 
108  static void setSortOrderStatic(QAction* a, int sortOrder);
109  static void setSortOrderOverrideStatic(QAction* a, const QString& widgetName, int sortOrder);
110  static int getSortOrderStatic(const QAction* a, const QWidget* w = NULL);
111 
112  void setGroupSortOrder(int sortOrder);
113  void setGroupSortOrderOverride(const QString& widgetName, int sortOrder);
114  int getGroupSortOrder(const QWidget* w = NULL) const;
115 
116  void setSortOrder(int sortOrder);
117  void setSortOrderOverride(const QString& widgetName, int sortOrder);
118  int getSortOrder(const QWidget* w = NULL) const;
119 
120  void addToMenu(QMenu* menu);
121  void addToToolBar(QToolBar* tb);
122 
123  static void fixSeparators(const QWidget* w);
124  static void addSeparatorToWidget(QAction* a, QWidget* w);
125  static void addToWidget(QAction* a, QWidget* w);
126  static void removeFromWidget(QAction* a, QWidget* w);
127 
131  virtual void setIcon(const QString& iconFile);
132 
133  void disableIcon();
134  bool isIconDisabled() const;
135 
140  virtual void setToolTip(const QString& tip);
141 
146  virtual void setStatusTip(const QString& tip);
147 
151  virtual void setEnabled(bool on);
152 
156  virtual void setEnabledOverride(bool on, int override);
157 
161  virtual void setChecked(bool on);
162 
163  static void triggerGroupDefault(const QString& group);
164  static void triggerGroupDefaults();
165 
169  virtual bool isChecked() const;
170 
175  void setRequiresDocument(bool on) {
176  requiresDocument = on;
177  }
178 
183  bool getRequiresDocument() const {
184  return requiresDocument;
185  }
186 
191  void setRequiresSelection(bool on);
192 
197  bool getRequiresSelection() const {
198  return requiresSelection;
199  }
200 
202  requiresUndoableTransaction = on;
203  }
204 
206  return requiresUndoableTransaction;
207  }
208 
210  requiresRedoableTransaction = on;
211  }
212 
214  return requiresRedoableTransaction;
215  }
216 
217  void setOverride() {
218  override = true;
219  }
220 
221  bool isOverride() const {
222  return override;
223  }
224 
225  void setNoState(bool checkable = false) {
226  noState = true;
227  setCheckable(checkable);
228  }
229 
230  bool hasNoState() const {
231  return noState;
232  }
233 
237  void setToggleable(bool on = true) {
238  toggleable = on;
239  }
240 
241  bool isToggleable() const {
242  return toggleable;
243  }
244 
245  void setUniqueGroup(const QString& ug) {
246  uniqueGroup = ug;
247  }
248 
249  QString getUniqueGroup() const {
250  return uniqueGroup;
251  }
252 
258  QString getMainCommand() const {
259  return mainCommand;
260  }
261 
265  QStringList getCommands() const {
266  return commandList;
267  }
268 
269  QStringList getDefaultCommands() {
270  return defaultCommandList;
271  }
272 
273  void setDefaultCommands(const QStringList& cmds);
274  void setCommands(const QStringList& cmds);
275 
279  void setFactory(FactoryFunction f);
280 
285  void setScriptFile(const QString& sf, bool isSecondary = false);
286 
290  QString getScriptFile() const {
291  return scriptFile;
292  }
293 
294  QString getScriptClass() const {
295  return QFileInfo(scriptFile).baseName();
296  }
297 
298  virtual void setGroup(const QString& title);
299  QString getGroup();
300 
301  void setGroupDefault(bool on);
302  bool isGroupDefault();
303 
304 // void setSeparatorGroup(const QString& title);
305 // QString getSeparatorGroup();
306 
310  static QList<RGuiAction*> getActions() {
311  return actions;
312  }
313 
314  static bool triggerByCommand(const QString& cmd);
315  static RGuiAction* getByScriptFile(const QString& scriptFile);
316  static RGuiAction* getByClassName(const QString& className);
317  static RGuiAction* getByCommand(const QString& command);
318  static bool triggerByScriptFile(const QString& scriptFile);
319 
320  static QStringList getAvailableCommands(bool primaryOnly=false);
321  static QStringList getAvailableCommands(const QString& start, bool primaryOnly=false);
322 
323  static void clear();
324 
325  virtual void updateTransactionListener(RDocument* document, RTransaction* transaction = NULL);
326  virtual void updateSelectionListener(RDocumentInterface* documentInterface);
327  virtual void updateFocus(RDocumentInterface* documentInterface);
328 
329  void addArgument(const QString& arg);
330  QStringList getArguments();
331  void clearArguments();
332 
333  static void setWidgetNamesStatic(QAction* a, const QStringList& widgetNames);
334  static QStringList getWidgetNamesStatic(const QAction *a);
335 
336  void setWidgetNames(const QStringList& widgetNames);
337  QStringList getWidgetNames() const;
338 
339 public slots:
343  bool slotTrigger(const QString& command = QString::null);
344 
345 signals:
346  void postTriggered();
347 
348 protected:
349  static QMap<QString, RGuiAction*> actionsByCommand;
350  static QMap<QString, RGuiAction*> actionsByPrimaryCommand;
351  static QMap<QString, RGuiAction*> actionsByScriptFile;
352  static QMultiMap<QString, RGuiAction*> actionsByGroup;
353  static QList<RGuiAction*> actions;
354 
355  FactoryFunction factory;
356  QString oriText;
357  QString scriptFile;
358  QString mainCommand;
359  QStringList commandList;
360  QStringList defaultCommandList;
361  QString group;
363 
364  // TODO: refactor to use flags:
369  bool override;
370  bool noState;
372  QString uniqueGroup;
373  QStringList arguments;
374 
375  QList<QKeySequence> defaultShortcuts;
376  QString shortcutText;
377  QString toolTip;
381 };
382 
385 
386 #endif
bool isOverride() const
Definition: RGuiAction.h:221
Abstract base class for classes that want to be notified on every transaction (objects added...
Definition: RTransactionListener.h:38
void setUniqueGroup(const QString &ug)
Definition: RGuiAction.h:245
bool isToggleable() const
Definition: RGuiAction.h:241
QString oriText
Definition: RGuiAction.h:356
bool toggleable
Definition: RGuiAction.h:371
RDocumentInterface * documentInterface
Definition: RGuiAction.h:380
QString getMainCommand() const
Definition: RGuiAction.h:258
bool requiresDocument
Definition: RGuiAction.h:365
bool requiresSelection
Definition: RGuiAction.h:366
QString getScriptClass() const
Definition: RGuiAction.h:294
bool getRequiresDocument() const
Definition: RGuiAction.h:183
bool requiresUndoableTransaction
Definition: RGuiAction.h:367
#define QCADCORE_EXPORT
Definition: core_global.h:10
QString shortcutText
Definition: RGuiAction.h:376
bool noState
Definition: RGuiAction.h:370
Transaction implementation.
Definition: RTransaction.h:74
static QMap< QString, RGuiAction * > actionsByCommand
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RGuiAction.h:349
bool requiresRedoableTransaction
Definition: RGuiAction.h:368
QString toolTip
Definition: RGuiAction.h:377
static QMap< QString, RGuiAction * > actionsByScriptFile
Definition: RGuiAction.h:351
QStringList getCommands() const
Definition: RGuiAction.h:265
bool getRequiresUndoableTransaction() const
Definition: RGuiAction.h:205
static QList< RGuiAction * > getActions()
Definition: RGuiAction.h:310
Abstract base class for classes that are interested in the current selection and want to be notified ...
Definition: RSelectionListener.h:36
bool getRequiresRedoableTransaction() const
Definition: RGuiAction.h:213
A graphics document contains and owns entities, layers, user coordinate systems, variables, block definitions, etc.
Definition: RDocument.h:67
QStringList arguments
Definition: RGuiAction.h:373
Q_DECLARE_METATYPE(RMath *)
QList< QKeySequence > defaultShortcuts
Definition: RGuiAction.h:375
QString getUniqueGroup() const
Definition: RGuiAction.h:249
QString group
Definition: RGuiAction.h:361
void setRequiresRedoableTransaction(bool on)
Definition: RGuiAction.h:209
QString getScriptFile() const
Definition: RGuiAction.h:290
int enabledOverride
Definition: RGuiAction.h:379
QStringList defaultCommandList
Definition: RGuiAction.h:360
void setOverride()
Definition: RGuiAction.h:217
static QMultiMap< QString, RGuiAction * > actionsByGroup
Definition: RGuiAction.h:352
virtual void updateFocus(RDocumentInterface *di)=0
Called by the document whenever the focus changed from one MDI to another.
bool iconDisabled
Definition: RGuiAction.h:378
QString uniqueGroup
Definition: RGuiAction.h:372
FactoryFunction factory
Definition: RGuiAction.h:355
static QList< RGuiAction * > actions
Definition: RGuiAction.h:353
Abstract base class for classes that are interested in the current focus.
Definition: RFocusListener.h:36
void setRequiresDocument(bool on)
If on is true, this action requires a document to be open.
Definition: RGuiAction.h:175
void setRequiresUndoableTransaction(bool on)
Definition: RGuiAction.h:201
bool groupDefault
Definition: RGuiAction.h:362
Abstract base class for GUI action classes.
Definition: RGuiAction.h:60
void getDocumentInterface()
Returns the current RDocumentInterface or undefined.
Definition: simple.js:69
QString mainCommand
Definition: RGuiAction.h:358
Interface for interaction between a graphics document and a user.
Definition: RDocumentInterface.h:82
QStringList commandList
Definition: RGuiAction.h:359
void init(void basePath)
Definition: AddBlockInit.js:2
void setNoState(bool checkable=false)
Definition: RGuiAction.h:225
bool getRequiresSelection() const
Definition: RGuiAction.h:197
void getArguments(void args, void shortFlag, void longFlag)
Definition: arguments.js:23
bool hasNoState() const
Definition: RGuiAction.h:230
virtual void updateSelectionListener(RDocumentInterface *documentInterface)=0
Called by the document whenever the current selection changes.
static QMap< QString, RGuiAction * > actionsByPrimaryCommand
Definition: RGuiAction.h:350
QStringList getDefaultCommands()
Definition: RGuiAction.h:269
virtual void updateTransactionListener(RDocument *document, RTransaction *transaction=NULL)=0
Called by the document whenever the current transaction stack changes.
QString scriptFile
Definition: RGuiAction.h:357
void setToggleable(bool on=true)
If enabled, action terminates when activated and already running.
Definition: RGuiAction.h:237