2016-05-17 03:04:11 +12:00
|
|
|
#pragma once
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#ifndef MENUBAR_COMMAND_H
|
|
|
|
#define MENUBAR_COMMAND_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include "tcommon.h"
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QMenu>
|
|
|
|
|
|
|
|
#undef DVAPI
|
|
|
|
#undef DVVAR
|
|
|
|
#ifdef TOONZQT_EXPORTS
|
|
|
|
#define DVAPI DV_EXPORT_API
|
|
|
|
#define DVVAR DV_EXPORT_VAR
|
|
|
|
#else
|
|
|
|
#define DVAPI DV_IMPORT_API
|
|
|
|
#define DVVAR DV_IMPORT_VAR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// forward declaration
|
|
|
|
//
|
|
|
|
class QAction;
|
|
|
|
|
|
|
|
//
|
|
|
|
// base class
|
|
|
|
//
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI CommandHandlerInterface {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
virtual ~CommandHandlerInterface() {}
|
|
|
|
virtual void execute() = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//
|
|
|
|
// command identifier (e.g. "copy"). They are NOT command names (e.g. "&Copy")
|
|
|
|
//
|
|
|
|
typedef const char *CommandId;
|
|
|
|
|
|
|
|
enum CommandType {
|
2016-06-15 18:43:10 +12:00
|
|
|
UndefinedCommandType = 0,
|
|
|
|
RightClickMenuCommandType,
|
|
|
|
MenuFileCommandType,
|
|
|
|
MenuEditCommandType,
|
|
|
|
MenuScanCleanupCommandType,
|
|
|
|
MenuLevelCommandType,
|
|
|
|
MenuXsheetCommandType,
|
|
|
|
MenuCellsCommandType,
|
2019-10-31 08:10:33 +13:00
|
|
|
MenuPlayCommandType,
|
|
|
|
MenuRenderCommandType,
|
2016-06-15 18:43:10 +12:00
|
|
|
MenuViewCommandType,
|
|
|
|
MenuWindowsCommandType,
|
2019-10-31 08:10:33 +13:00
|
|
|
MenuHelpCommandType,
|
2016-06-15 18:43:10 +12:00
|
|
|
PlaybackCommandType,
|
|
|
|
RGBACommandType,
|
|
|
|
FillCommandType,
|
|
|
|
ToolCommandType,
|
|
|
|
ToolModifierCommandType,
|
|
|
|
ZoomCommandType,
|
|
|
|
MiscCommandType,
|
2019-08-05 20:37:40 +12:00
|
|
|
MenuCommandType,
|
2020-05-01 07:54:36 +12:00
|
|
|
VisualizationButtonCommandType,
|
|
|
|
StopMotionCommandType
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class AuxActionsCreator {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
AuxActionsCreator();
|
|
|
|
virtual ~AuxActionsCreator(){};
|
|
|
|
virtual void createActions(QObject *parent) = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class AuxActionsCreatorManager {
|
|
|
|
bool m_auxActionsCreated;
|
|
|
|
std::vector<AuxActionsCreator *> m_auxActionsCreators;
|
|
|
|
AuxActionsCreatorManager();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
static AuxActionsCreatorManager *instance();
|
|
|
|
void addAuxActionsCreator(AuxActionsCreator *auxActionsCreator);
|
|
|
|
void createAuxActions(QObject *parent);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//
|
|
|
|
// command manager:
|
|
|
|
// setExecutor(id, executor)
|
|
|
|
// setAction(id, qaction)
|
|
|
|
// execute(qaction)/
|
|
|
|
//
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI CommandManager { // singleton
|
|
|
|
|
|
|
|
class Node {
|
|
|
|
public:
|
|
|
|
std::string m_id;
|
|
|
|
CommandType m_type;
|
|
|
|
QAction *m_qaction;
|
|
|
|
CommandHandlerInterface *m_handler;
|
|
|
|
bool m_enabled;
|
|
|
|
QString m_onText,
|
|
|
|
m_offText; // for toggle commands. e.g. show/hide something
|
|
|
|
|
|
|
|
Node(CommandId id)
|
|
|
|
: m_id(id)
|
|
|
|
, m_type(UndefinedCommandType)
|
|
|
|
, m_qaction(0)
|
|
|
|
, m_handler(0)
|
|
|
|
, m_enabled(true) {}
|
|
|
|
|
|
|
|
~Node() {
|
|
|
|
if (m_handler) delete m_handler;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<std::string, Node *> m_idTable;
|
|
|
|
std::map<QAction *, Node *> m_qactionTable;
|
|
|
|
std::map<std::string, Node *> m_shortcutTable;
|
|
|
|
|
|
|
|
CommandManager();
|
|
|
|
|
|
|
|
Node *getNode(CommandId id, bool createIfNeeded = true);
|
|
|
|
void setShortcut(CommandId id, QAction *action, std::string shortcutString);
|
|
|
|
void createAuxActions();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
static CommandManager *instance();
|
|
|
|
~CommandManager();
|
|
|
|
|
|
|
|
void setHandler(CommandId id, CommandHandlerInterface *handler);
|
|
|
|
|
|
|
|
void define(CommandId id, CommandType type, std::string defaultShortcutString,
|
|
|
|
QAction *action);
|
|
|
|
|
|
|
|
QAction *createAction(const char *id, const char *name,
|
|
|
|
const char *defaultShortcut);
|
|
|
|
|
|
|
|
void getActions(CommandType type, std::vector<QAction *> &actions);
|
|
|
|
QAction *getActionFromShortcut(std::string shortcutString);
|
|
|
|
std::string getShortcutFromAction(QAction *action);
|
|
|
|
std::string getShortcutFromId(CommandId id);
|
|
|
|
int getKeyFromShortcut(const std::string &shortcut);
|
|
|
|
int getKeyFromId(CommandId id);
|
2016-10-08 00:45:49 +13:00
|
|
|
void setShortcut(QAction *action, std::string shortcutString,
|
|
|
|
bool keepDefault = true);
|
2020-10-19 12:39:39 +13:00
|
|
|
bool canUseShortcut(QString shortcut);
|
2016-06-15 18:43:10 +12:00
|
|
|
QAction *getAction(CommandId id, bool createIfNeeded = false);
|
|
|
|
|
2021-07-08 13:39:28 +12:00
|
|
|
// createAction creates a new independent QAction with text and shortcut
|
2016-06-15 18:43:10 +12:00
|
|
|
// if the action is a toggle action (e.g. show/hide something) the text is
|
|
|
|
// controlled by state
|
|
|
|
// you can use createAction() for context menu
|
|
|
|
QAction *createAction(CommandId id, QObject *parent = 0, bool state = true);
|
|
|
|
|
|
|
|
void execute(QAction *action);
|
|
|
|
/*! If action is defined in m_qactionTable recall \b execute(action),
|
|
|
|
* otherwise recall execute(menuAction).*/
|
|
|
|
void execute(QAction *action, QAction *menuAction);
|
|
|
|
void execute(CommandId id);
|
|
|
|
void enable(CommandId id, bool enabled);
|
|
|
|
|
|
|
|
// if id is a toggle (e.g. a checkable menu item) then set its status;
|
|
|
|
// note: this will trigger any associated handler
|
|
|
|
void setChecked(CommandId id, bool checked);
|
|
|
|
|
|
|
|
// use setToggleTexts for toggle commands that have two names according to the
|
|
|
|
// current status. e.g. show/hide something
|
|
|
|
void setToggleTexts(CommandId id, const QString &onText,
|
|
|
|
const QString &offText);
|
|
|
|
|
|
|
|
std::string getIdFromAction(QAction *action);
|
2017-10-23 20:40:26 +13:00
|
|
|
|
|
|
|
// load user defined shortcuts
|
|
|
|
void loadShortcuts();
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//
|
|
|
|
// CommandHandlerHelper = target + method
|
|
|
|
//
|
|
|
|
|
|
|
|
template <class T>
|
2016-06-29 18:17:12 +12:00
|
|
|
class CommandHandlerHelper final : public CommandHandlerInterface {
|
2016-06-15 18:43:10 +12:00
|
|
|
T *m_target;
|
|
|
|
void (T::*m_method)();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
CommandHandlerHelper(T *target, void (T::*method)())
|
|
|
|
: m_target(target), m_method(method) {}
|
2016-06-19 20:06:29 +12:00
|
|
|
void execute() override { (m_target->*m_method)(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T, typename R>
|
2016-06-29 18:17:12 +12:00
|
|
|
class CommandHandlerHelper2 final : public CommandHandlerInterface {
|
2016-06-15 18:43:10 +12:00
|
|
|
T *m_target;
|
|
|
|
void (T::*m_method)(R value);
|
|
|
|
R m_value;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
CommandHandlerHelper2(T *target, void (T::*method)(R), R value)
|
|
|
|
: m_target(target), m_method(method), m_value(value) {}
|
2016-06-19 20:06:29 +12:00
|
|
|
void execute() override { (m_target->*m_method)(m_value); }
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <class T>
|
2016-06-15 18:43:10 +12:00
|
|
|
inline void setCommandHandler(CommandId id, T *target, void (T::*method)()) {
|
|
|
|
CommandManager::instance()->setHandler(
|
|
|
|
id, new CommandHandlerHelper<T>(target, method));
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI MenuItemHandler : public QObject {
|
|
|
|
Q_OBJECT
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
MenuItemHandler(CommandId cmdId);
|
|
|
|
virtual ~MenuItemHandler(){};
|
|
|
|
virtual void execute() = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2016-06-29 18:17:12 +12:00
|
|
|
class OpenPopupCommandHandler final : public MenuItemHandler {
|
2016-06-15 18:43:10 +12:00
|
|
|
T *m_popup;
|
|
|
|
CommandId m_id;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
OpenPopupCommandHandler(CommandId cmdId)
|
|
|
|
: MenuItemHandler(cmdId), m_popup(0) {}
|
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void execute() override {
|
2016-06-15 18:43:10 +12:00
|
|
|
if (!m_popup) m_popup = new T();
|
|
|
|
m_popup->show();
|
|
|
|
m_popup->raise();
|
|
|
|
m_popup->activateWindow();
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class DVAPI DVAction final : public QAction {
|
2016-06-15 18:43:10 +12:00
|
|
|
Q_OBJECT
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
DVAction(const QString &text, QObject *parent);
|
|
|
|
DVAction(const QIcon &icon, const QString &text, QObject *parent);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public slots:
|
2016-06-15 18:43:10 +12:00
|
|
|
void onTriggered();
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class DVAPI DVMenuAction final : public QMenu {
|
2016-06-15 18:43:10 +12:00
|
|
|
Q_OBJECT
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int m_triggeredActionIndex;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
DVMenuAction(const QString &text, QWidget *parent, QList<QString> actions);
|
|
|
|
void setActions(QList<QString> actions);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int getTriggeredActionIndex() { return m_triggeredActionIndex; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public slots:
|
2016-06-15 18:43:10 +12:00
|
|
|
void onTriggered(QAction *action);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
#endif // MENUBAR_COMMAND_H
|