/toonz/sources/include/toonzqt/menubarcommand.h

https://github.com/opentoonz/opentoonz · C Header · 280 lines · 183 code · 58 blank · 39 comment · 2 complexity · 27363c389ef3e2e2d0600cba0a843fc6 MD5 · raw file

  1. #pragma once
  2. #ifndef MENUBAR_COMMAND_H
  3. #define MENUBAR_COMMAND_H
  4. #include <vector>
  5. #include <map>
  6. #include <string>
  7. #include "tcommon.h"
  8. #include <QAction>
  9. #include <QMenu>
  10. #undef DVAPI
  11. #undef DVVAR
  12. #ifdef TOONZQT_EXPORTS
  13. #define DVAPI DV_EXPORT_API
  14. #define DVVAR DV_EXPORT_VAR
  15. #else
  16. #define DVAPI DV_IMPORT_API
  17. #define DVVAR DV_IMPORT_VAR
  18. #endif
  19. //
  20. // forward declaration
  21. //
  22. class QAction;
  23. //
  24. // base class
  25. //
  26. class DVAPI CommandHandlerInterface {
  27. public:
  28. virtual ~CommandHandlerInterface() {}
  29. virtual void execute() = 0;
  30. };
  31. //-----------------------------------------------------------------------------
  32. //
  33. // command identifier (e.g. "copy"). They are NOT command names (e.g. "&Copy")
  34. //
  35. typedef const char *CommandId;
  36. enum CommandType {
  37. UndefinedCommandType = 0,
  38. RightClickMenuCommandType,
  39. MenuFileCommandType,
  40. MenuEditCommandType,
  41. MenuScanCleanupCommandType,
  42. MenuLevelCommandType,
  43. MenuXsheetCommandType,
  44. MenuCellsCommandType,
  45. MenuPlayCommandType,
  46. MenuRenderCommandType,
  47. MenuViewCommandType,
  48. MenuWindowsCommandType,
  49. MenuHelpCommandType,
  50. PlaybackCommandType,
  51. RGBACommandType,
  52. FillCommandType,
  53. ToolCommandType,
  54. ToolModifierCommandType,
  55. ZoomCommandType,
  56. MiscCommandType,
  57. MenuCommandType,
  58. VisualizationButtonCommandType
  59. };
  60. //-----------------------------------------------------------------------------
  61. class AuxActionsCreator {
  62. public:
  63. AuxActionsCreator();
  64. virtual ~AuxActionsCreator(){};
  65. virtual void createActions(QObject *parent) = 0;
  66. };
  67. //-----------------------------------------------------------------------------
  68. class AuxActionsCreatorManager {
  69. bool m_auxActionsCreated;
  70. std::vector<AuxActionsCreator *> m_auxActionsCreators;
  71. AuxActionsCreatorManager();
  72. public:
  73. static AuxActionsCreatorManager *instance();
  74. void addAuxActionsCreator(AuxActionsCreator *auxActionsCreator);
  75. void createAuxActions(QObject *parent);
  76. };
  77. //-----------------------------------------------------------------------------
  78. //
  79. // command manager:
  80. // setExecutor(id, executor)
  81. // setAction(id, qaction)
  82. // execute(qaction)/
  83. //
  84. class DVAPI CommandManager { // singleton
  85. class Node {
  86. public:
  87. std::string m_id;
  88. CommandType m_type;
  89. QAction *m_qaction;
  90. CommandHandlerInterface *m_handler;
  91. bool m_enabled;
  92. QString m_onText,
  93. m_offText; // for toggle commands. e.g. show/hide something
  94. Node(CommandId id)
  95. : m_id(id)
  96. , m_type(UndefinedCommandType)
  97. , m_qaction(0)
  98. , m_handler(0)
  99. , m_enabled(true) {}
  100. ~Node() {
  101. if (m_handler) delete m_handler;
  102. }
  103. };
  104. std::map<std::string, Node *> m_idTable;
  105. std::map<QAction *, Node *> m_qactionTable;
  106. std::map<std::string, Node *> m_shortcutTable;
  107. CommandManager();
  108. Node *getNode(CommandId id, bool createIfNeeded = true);
  109. void setShortcut(CommandId id, QAction *action, std::string shortcutString);
  110. void createAuxActions();
  111. public:
  112. static CommandManager *instance();
  113. ~CommandManager();
  114. void setHandler(CommandId id, CommandHandlerInterface *handler);
  115. void define(CommandId id, CommandType type, std::string defaultShortcutString,
  116. QAction *action);
  117. QAction *createAction(const char *id, const char *name,
  118. const char *defaultShortcut);
  119. void getActions(CommandType type, std::vector<QAction *> &actions);
  120. QAction *getActionFromShortcut(std::string shortcutString);
  121. std::string getShortcutFromAction(QAction *action);
  122. std::string getShortcutFromId(CommandId id);
  123. int getKeyFromShortcut(const std::string &shortcut);
  124. int getKeyFromId(CommandId id);
  125. void setShortcut(QAction *action, std::string shortcutString,
  126. bool keepDefault = true);
  127. QAction *getAction(CommandId id, bool createIfNeeded = false);
  128. // createAction creates a new indepenent QAction with text and shortcut
  129. // if the action is a toggle action (e.g. show/hide something) the text is
  130. // controlled by state
  131. // you can use createAction() for context menu
  132. QAction *createAction(CommandId id, QObject *parent = 0, bool state = true);
  133. void execute(QAction *action);
  134. /*! If action is defined in m_qactionTable recall \b execute(action),
  135. * otherwise recall execute(menuAction).*/
  136. void execute(QAction *action, QAction *menuAction);
  137. void execute(CommandId id);
  138. void enable(CommandId id, bool enabled);
  139. // if id is a toggle (e.g. a checkable menu item) then set its status;
  140. // note: this will trigger any associated handler
  141. void setChecked(CommandId id, bool checked);
  142. // use setToggleTexts for toggle commands that have two names according to the
  143. // current status. e.g. show/hide something
  144. void setToggleTexts(CommandId id, const QString &onText,
  145. const QString &offText);
  146. std::string getIdFromAction(QAction *action);
  147. // load user defined shortcuts
  148. void loadShortcuts();
  149. };
  150. //-----------------------------------------------------------------------------
  151. //
  152. // CommandHandlerHelper = target + method
  153. //
  154. template <class T>
  155. class CommandHandlerHelper final : public CommandHandlerInterface {
  156. T *m_target;
  157. void (T::*m_method)();
  158. public:
  159. CommandHandlerHelper(T *target, void (T::*method)())
  160. : m_target(target), m_method(method) {}
  161. void execute() override { (m_target->*m_method)(); }
  162. };
  163. template <class T, typename R>
  164. class CommandHandlerHelper2 final : public CommandHandlerInterface {
  165. T *m_target;
  166. void (T::*m_method)(R value);
  167. R m_value;
  168. public:
  169. CommandHandlerHelper2(T *target, void (T::*method)(R), R value)
  170. : m_target(target), m_method(method), m_value(value) {}
  171. void execute() override { (m_target->*m_method)(m_value); }
  172. };
  173. //-----------------------------------------------------------------------------
  174. template <class T>
  175. inline void setCommandHandler(CommandId id, T *target, void (T::*method)()) {
  176. CommandManager::instance()->setHandler(
  177. id, new CommandHandlerHelper<T>(target, method));
  178. }
  179. //-----------------------------------------------------------------------------
  180. class DVAPI MenuItemHandler : public QObject {
  181. Q_OBJECT
  182. public:
  183. MenuItemHandler(CommandId cmdId);
  184. virtual ~MenuItemHandler(){};
  185. virtual void execute() = 0;
  186. };
  187. template <class T>
  188. class OpenPopupCommandHandler final : public MenuItemHandler {
  189. T *m_popup;
  190. CommandId m_id;
  191. public:
  192. OpenPopupCommandHandler(CommandId cmdId)
  193. : MenuItemHandler(cmdId), m_popup(0) {}
  194. void execute() override {
  195. if (!m_popup) m_popup = new T();
  196. m_popup->show();
  197. m_popup->raise();
  198. m_popup->activateWindow();
  199. }
  200. };
  201. //-----------------------------------------------------------------------------
  202. class DVAPI DVAction final : public QAction {
  203. Q_OBJECT
  204. public:
  205. DVAction(const QString &text, QObject *parent);
  206. DVAction(const QIcon &icon, const QString &text, QObject *parent);
  207. public slots:
  208. void onTriggered();
  209. };
  210. //-----------------------------------------------------------------------------
  211. class DVAPI DVMenuAction final : public QMenu {
  212. Q_OBJECT
  213. int m_triggeredActionIndex;
  214. public:
  215. DVMenuAction(const QString &text, QWidget *parent, QList<QString> actions);
  216. void setActions(QList<QString> actions);
  217. int getTriggeredActionIndex() { return m_triggeredActionIndex; }
  218. public slots:
  219. void onTriggered(QAction *action);
  220. };
  221. //-----------------------------------------------------------------------------
  222. #endif // MENUBAR_COMMAND_H