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 
226  allowInterrupt = true;
227  }
228 
229  bool getAllowInterrupt() const {
230  return allowInterrupt;
231  }
232 
233  void setNoState(bool checkable = false) {
234  noState = true;
235  setCheckable(checkable);
236  }
237 
238  bool hasNoState() const {
239  return noState;
240  }
241 
245  void setToggleable(bool on = true) {
246  toggleable = on;
247  }
248 
249  bool isToggleable() const {
250  return toggleable;
251  }
252 
253  void setUniqueGroup(const QString& ug) {
254  uniqueGroup = ug;
255  }
256 
257  QString getUniqueGroup() const {
258  return uniqueGroup;
259  }
260 
266  QString getMainCommand() const {
267  return mainCommand;
268  }
269 
273  QStringList getCommands() const {
274  return commandList;
275  }
276 
277  QStringList getDefaultCommands() {
278  return defaultCommandList;
279  }
280 
281  void setDefaultCommands(const QStringList& cmds);
282  void setCommands(const QStringList& cmds);
283 
287  void setFactory(FactoryFunction f);
288 
293  void setScriptFile(const QString& sf, bool isSecondary = false);
294 
298  QString getScriptFile() const {
299  return scriptFile;
300  }
301 
302  QString getScriptClass() const {
303  return QFileInfo(scriptFile).baseName();
304  }
305 
306  virtual void setGroup(const QString& title);
307  QString getGroup();
308 
309  void setGroupDefault(bool on);
310  bool isGroupDefault();
311 
312 // void setSeparatorGroup(const QString& title);
313 // QString getSeparatorGroup();
314 
318  static QList<RGuiAction*> getActions() {
319  return actions;
320  }
321 
322  static bool triggerByCommand(const QString& cmd);
323  static RGuiAction* getByScriptFile(const QString& scriptFile);
324  static RGuiAction* getByClassName(const QString& className);
325  static RGuiAction* getByCommand(const QString& command);
326  static bool triggerByScriptFile(const QString& scriptFile);
327 
328  static QStringList getAvailableCommands(bool primaryOnly=false);
329  static QStringList getAvailableCommands(const QString& start, bool primaryOnly=false);
330 
331  static void clear();
332 
333  virtual void updateTransactionListener(RDocument* document, RTransaction* transaction = NULL);
334  virtual void updateSelectionListener(RDocumentInterface* documentInterface);
335  virtual void updateFocus(RDocumentInterface* documentInterface);
336 
337  void addArgument(const QString& arg);
338  QStringList getArguments();
339  void clearArguments();
340 
341  static void setWidgetNamesStatic(QAction* a, const QStringList& widgetNames);
342  static QStringList getWidgetNamesStatic(const QAction *a);
343 
344  void setWidgetNames(const QStringList& widgetNames);
345  QStringList getWidgetNames() const;
346 
347 public slots:
351  bool slotTrigger(const QString& command = QString::null);
352 
353 signals:
354  void postTriggered();
355 
356 protected:
357  static QMap<QString, RGuiAction*> actionsByCommand;
358  static QMap<QString, RGuiAction*> actionsByPrimaryCommand;
359  static QMap<QString, RGuiAction*> actionsByScriptFile;
360  static QMultiMap<QString, RGuiAction*> actionsByGroup;
361  static QList<RGuiAction*> actions;
362 
363  FactoryFunction factory;
364  QString oriText;
365  QString scriptFile;
366  QString mainCommand;
367  QStringList commandList;
368  QStringList defaultCommandList;
369  QString group;
371 
372  // TODO: refactor to use flags:
377  bool override;
379  bool noState;
381  QString uniqueGroup;
382  QStringList arguments;
383 
384  QList<QKeySequence> defaultShortcuts;
385  QString shortcutText;
386  QString toolTip;
390 };
391 
394 
395 #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:253
bool isToggleable() const
Definition: RGuiAction.h:249
QString oriText
Definition: RGuiAction.h:364
bool toggleable
Definition: RGuiAction.h:380
RDocumentInterface * documentInterface
Definition: RGuiAction.h:389
QString getMainCommand() const
Definition: RGuiAction.h:266
bool requiresDocument
Definition: RGuiAction.h:373
bool requiresSelection
Definition: RGuiAction.h:374
QString getScriptClass() const
Definition: RGuiAction.h:302
bool getRequiresDocument() const
Definition: RGuiAction.h:183
void setAllowInterrupt()
Definition: RGuiAction.h:225
bool requiresUndoableTransaction
Definition: RGuiAction.h:375
#define QCADCORE_EXPORT
Definition: core_global.h:10
QString shortcutText
Definition: RGuiAction.h:385
bool noState
Definition: RGuiAction.h:379
Transaction implementation.
Definition: RTransaction.h:74
static QMap< QString, RGuiAction * > actionsByCommand
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RGuiAction.h:357
bool requiresRedoableTransaction
Definition: RGuiAction.h:376
QString toolTip
Definition: RGuiAction.h:386
static QMap< QString, RGuiAction * > actionsByScriptFile
Definition: RGuiAction.h:359
QStringList getCommands() const
Definition: RGuiAction.h:273
bool getRequiresUndoableTransaction() const
Definition: RGuiAction.h:205
static QList< RGuiAction * > getActions()
Definition: RGuiAction.h:318
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:382
Q_DECLARE_METATYPE(RMath *)
QList< QKeySequence > defaultShortcuts
Definition: RGuiAction.h:384
QString getUniqueGroup() const
Definition: RGuiAction.h:257
QString group
Definition: RGuiAction.h:369
void setRequiresRedoableTransaction(bool on)
Definition: RGuiAction.h:209
QString getScriptFile() const
Definition: RGuiAction.h:298
int enabledOverride
Definition: RGuiAction.h:388
QStringList defaultCommandList
Definition: RGuiAction.h:368
void setOverride()
Definition: RGuiAction.h:217
static QMultiMap< QString, RGuiAction * > actionsByGroup
Definition: RGuiAction.h:360
virtual void updateFocus(RDocumentInterface *di)=0
Called by the document whenever the focus changed from one MDI to another.
bool iconDisabled
Definition: RGuiAction.h:387
QString uniqueGroup
Definition: RGuiAction.h:381
FactoryFunction factory
Definition: RGuiAction.h:363
static QList< RGuiAction * > actions
Definition: RGuiAction.h:361
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:370
Abstract base class for GUI action classes.
Definition: RGuiAction.h:60
void getDocumentInterface()
Returns the current RDocumentInterface or undefined.
Definition: simple.js:76
QString mainCommand
Definition: RGuiAction.h:366
Interface for interaction between a graphics document and a user.
Definition: RDocumentInterface.h:82
QStringList commandList
Definition: RGuiAction.h:367
void init(void basePath)
Definition: AddBlockInit.js:2
void setNoState(bool checkable=false)
Definition: RGuiAction.h:233
bool getRequiresSelection() const
Definition: RGuiAction.h:197
void getArguments(void args, void shortFlag, void longFlag)
Definition: arguments.js:24
bool getAllowInterrupt() const
Definition: RGuiAction.h:229
bool hasNoState() const
Definition: RGuiAction.h:238
bool allowInterrupt
Definition: RGuiAction.h:378
virtual void updateSelectionListener(RDocumentInterface *documentInterface)=0
Called by the document whenever the current selection changes.
static QMap< QString, RGuiAction * > actionsByPrimaryCommand
Definition: RGuiAction.h:358
QStringList getDefaultCommands()
Definition: RGuiAction.h:277
virtual void updateTransactionListener(RDocument *document, RTransaction *transaction=NULL)=0
Called by the document whenever the current transaction stack changes.
QString scriptFile
Definition: RGuiAction.h:365
void setToggleable(bool on=true)
If enabled, action terminates when activated and already running.
Definition: RGuiAction.h:245