Debian Codegraph @

src/Gui/Action.h

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
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_ACTION_H
25 #define GUI_ACTION_H
26
27 #include <memory>
28 #include <QAction>
29 #include <QComboBox>
30 #include <QKeySequence>
31 #include <QMap>
32 #include <FCGlobal.h>
33
34 namespace Gui
35 {
36 class Command;
37
38 /**
39 * The Action class is the link between Qt's QAction class and FreeCAD's
40 * command classes (@ref Command). So, it is possible to have all actions
41 * (from toolbars, menus, ...) implemented in classes instead of many slot
42 * methods in the class @ref MainWindow.
43 * @author Werner Mayer
44 */
45 class GuiExport Action: public QObject
46 {
47 Q_OBJECT
48
49 public:
50 explicit Action(Command* pcCmd, QObject* parent = nullptr);
51 /// Action takes ownership of the 'action' object.
52 Action(Command* pcCmd, QAction* action, QObject* parent);
53 ~Action() override;
54
55 virtual void addTo(QWidget* widget);
56 virtual void setEnabled(bool);
57 virtual void setVisible(bool);
58
59 void setCheckable(bool);
60 void setChecked(bool);
61 void setBlockedChecked(bool);
62 bool isChecked() const;
63 bool isEnabled() const;
64
65 void setShortcut(const QString&);
66 QKeySequence shortcut() const;
67 void setIcon(const QIcon&);
68 QIcon icon() const;
69 void setStatusTip(const QString&);
70 QString statusTip() const;
71 void setText(const QString&);
72 QString text() const;
73 void setToolTip(const QString&, const QString& title = QString());
74 QString toolTip() const;
75 void setWhatsThis(const QString&);
76 QString whatsThis() const;
77 void setMenuRole(QAction::MenuRole menuRole);
78 QAction* action() const
79 {
80 return _action;
81 }
82
83 static QString createToolTip(
84 QString helpText,
85 const QString& title,
86 const QFont& font,
87 const QString& shortCut,
88 const Command* command = nullptr
89 );
90
91 /** Obtain tool tip of a given command
92 * @param cmd: input command
93 * @param richFormat: whether to output rich text formatted tooltip
94 */
95 static QString commandToolTip(const Command* cmd, bool richFormat = true);
96
97 /** Obtain the menu text of a given command
98 * @param cmd: input command
99 * @return Return the command menu text that is stripped with its mnemonic
100 * symbol '&' and ending punctuation
101 */
102 static QString commandMenuText(const Command* cmd);
103
104 /// Clean the title by stripping the mnemonic symbol '&' and ending punctuation
105 static QString cleanTitle(const QString& title);
106
107 Command* command() const
108 {
109 return _pcCmd;
110 }
111
112 public Q_SLOTS:
113 virtual void onActivated();
114 virtual void onToggled(bool);
115
116 private:
117 QAction* _action;
118 Command* _pcCmd;
119 QString _tooltip;
120 QString _title;
121 QMetaObject::Connection _connection;
122
123 private:
124 Q_DISABLE_COPY(Action)
125 };
126
127 // --------------------------------------------------------------------
128
129 /**
130 * The ActionGroup class is the link between Qt's QActionGroup class and
131 * FreeCAD's command classes (@ref Command). Compared to Action with an
132 * ActionGroup it is possible to implement a single command with a group
133 * of toggable actions where e.g. one is set exclusive.
134 * @author Werner Mayer
135 */
136 class GuiExport ActionGroup: public Action
137 {
138 Q_OBJECT
139
140 public:
141 explicit ActionGroup(Command* pcCmd, QObject* parent = nullptr);
142 ~ActionGroup() override;
143
144 void addTo(QWidget* widget) override;
145 void setEnabled(bool) override;
146 void setDisabled(bool);
147 void setExclusive(bool);
148 bool isExclusive() const;
149 void setVisible(bool) override;
150 void setIsMode(bool check)
151 {
152 _isMode = check;
153 }
154
155 void setRememberLast(bool);
156 bool doesRememberLast() const;
157
158 void setDropDownMenu(bool check)
159 {
160 _dropDown = check;
161 }
162 QAction* addAction(QAction*);
163 QAction* addAction(const QString&);
164 QList<QAction*> actions() const;
165 int checkedAction() const;
166 void setCheckedAction(int);
167
168 QActionGroup* groupAction() const
169 {
170 return _group;
171 }
172
173 public Q_SLOTS:
174 void onActivated() override;
175 void onToggled(bool) override;
176 void onActivated(QAction*);
177 void onHovered(QAction*);
178
179 Q_SIGNALS:
180 /// When drop down menu is enabled, the signal is triggered just before hiding the menu
181 void aboutToHide(QMenu*);
182 /// When drop down menu is enabled, the signal is triggered just before showing the menu
183 void aboutToShow(QMenu*);
184
185 private:
186 QActionGroup* _group;
187 bool _dropDown;
188 bool _isMode;
189 bool _rememberLast;
190
191 private:
192 Q_DISABLE_COPY(ActionGroup)
193 };
194
195 /**
196 * The WorkbenchGroup class represents a list of workbenches. When it is added
197 * to a menu a submenu gets created, if added to a toolbar a combo box gets created.
198 * @author Werner Mayer
199 */
200 class GuiExport WorkbenchGroup: public ActionGroup
201 {
202 Q_OBJECT
203
204 QAction* getOrCreateAction(const QString& wbName);
205
206 public:
207 /**
208 * Creates an action for the command \a pcCmd to load the workbench \a name
209 * when it gets activated.
210 */
211 WorkbenchGroup(Command* pcCmd, QObject* parent);
212
213 void addTo(QWidget* widget) override;
214 void refreshWorkbenchList();
215
216 void slotActivateWorkbench(const char*);
217
218 QList<QAction*> getEnabledWbActions() const;
219 QList<QAction*> getDisabledWbActions() const;
220
221 Q_SIGNALS:
222 void workbenchListRefreshed(QList<QAction*>);
223
224 protected Q_SLOTS:
225 void onWorkbenchActivated(const QString&);
226
227 private:
228 QList<QAction*> enabledWbsActions;
229 QList<QAction*> disabledWbsActions;
230
231 QMap<QString, QAction*> actionByWorkbenchName;
232
233 Q_DISABLE_COPY(WorkbenchGroup)
234 };
235
236 // --------------------------------------------------------------------
237
238 /**
239 * The RecentFilesAction class holds a menu listed with the recent files.
240 * @author Werner Mayer
241 */
242 class GuiExport RecentFilesAction: public ActionGroup
243 {
244 Q_OBJECT
245
246 public:
247 explicit RecentFilesAction(Command* pcCmd, QObject* parent = nullptr);
248 ~RecentFilesAction() override;
249
250 void appendFile(const QString&);
251 void activateFile(int);
252 void resizeList(int);
253
254 Q_SIGNALS:
255 void recentFilesListModified();
256
257 private:
258 void setFiles(const QStringList&);
259 QStringList files() const;
260 void restore();
261 void save();
262
263 private:
264 int visibleItems; /**< Number of visible items */
265 int maximumItems; /**< Number of maximum items */
266
267 QAction sep, clearRecentFilesListAction;
268
269 class Private;
270 friend class Private;
271 std::unique_ptr<Private> _pimpl;
272
273 Q_DISABLE_COPY(RecentFilesAction)
274 };
275
276 // --------------------------------------------------------------------
277
278 /**
279 * The RecentMacrosAction class holds a menu listed with the recent macros
280 * that were executed, edited, or created
281 */
282 class GuiExport RecentMacrosAction: public ActionGroup
283 {
284 Q_OBJECT
285
286 public:
287 explicit RecentMacrosAction(Command* pcCmd, QObject* parent = nullptr);
288
289 void appendFile(const QString&);
290 void activateFile(int);
291 void resizeList(int);
292
293 private:
294 void setFiles(const QStringList&);
295 QStringList files() const;
296 void restore();
297 void save();
298
299 private:
300 int visibleItems; /**< Number of visible items */
301 int maximumItems; /**< Number of maximum items */
302 std::string shortcut_modifiers; /**< default = "Ctrl+Shift+" */
303 int shortcut_count; /**< Number of dynamic shortcuts to create -- default = 3*/
304
305 Q_DISABLE_COPY(RecentMacrosAction)
306 };
307
308
309 // --------------------------------------------------------------------
310
311 /**
312 * The UndoAction class reimplements a special behaviour to make a menu
313 * appearing when the button with the arrow is clicked.
314 * @author Werner Mayer
315 */
316 class GuiExport UndoAction: public Action
317 {
318 Q_OBJECT
319
320 public:
321 explicit UndoAction(Command* pcCmd, QObject* parent = nullptr);
322 ~UndoAction() override;
323 void addTo(QWidget* widget) override;
324 void setEnabled(bool) override;
325 void setVisible(bool) override;
326
327 private Q_SLOTS:
328 void actionChanged();
329
330 private:
331 QAction* _toolAction;
332
333 Q_DISABLE_COPY(UndoAction)
334 };
335
336 // --------------------------------------------------------------------
337
338 /**
339 * The RedoAction class reimplements a special behaviour to make a menu
340 * appearing when the button with the arrow is clicked.
341 * @author Werner Mayer
342 */
343 class GuiExport RedoAction: public Action
344 {
345 Q_OBJECT
346
347 public:
348 explicit RedoAction(Command* pcCmd, QObject* parent = nullptr);
349 ~RedoAction() override;
350 void addTo(QWidget* widget) override;
351 void setEnabled(bool) override;
352 void setVisible(bool) override;
353
354 private Q_SLOTS:
355 void actionChanged();
356
357 private:
358 QAction* _toolAction;
359
360 Q_DISABLE_COPY(RedoAction)
361 };
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Special action to show all dockable views -- except of toolbars -- in an own popup menu.
367 * @author Werner Mayer
368 */
369 class GuiExport DockWidgetAction: public Action
370 {
371 Q_OBJECT
372
373 public:
374 explicit DockWidgetAction(Command* pcCmd, QObject* parent = nullptr);
375 ~DockWidgetAction() override;
376 void addTo(QWidget* widget) override;
377
378 private:
379 QMenu* _menu;
380
381 Q_DISABLE_COPY(DockWidgetAction)
382 };
383
384 // --------------------------------------------------------------------
385
386 /**
387 * Special action to show all toolbars in an own popup menu.
388 * @author Werner Mayer
389 */
390 class GuiExport ToolBarAction: public Action
391 {
392 Q_OBJECT
393
394 public:
395 explicit ToolBarAction(Command* pcCmd, QObject* parent = nullptr);
396 ~ToolBarAction() override;
397 void addTo(QWidget* widget) override;
398
399 private:
400 QMenu* _menu;
401
402 Q_DISABLE_COPY(ToolBarAction)
403 };
404
405 // --------------------------------------------------------------------
406
407 /**
408 * @author Werner Mayer
409 */
410 class GuiExport WindowAction: public ActionGroup
411 {
412 Q_OBJECT
413
414 public:
415 explicit WindowAction(Command* pcCmd, QObject* parent = nullptr);
416 void addTo(QWidget* widget) override;
417
418 private:
419 QMenu* _menu;
420
421 Q_DISABLE_COPY(WindowAction)
422 };
423
424 } // namespace Gui
425
426 #endif // GUI_ACTION_H