Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef MENUBAR_COMMAND_H
Toshihiro Shimizu 890ddd
#define MENUBAR_COMMAND_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
#include <map></map>
Toshihiro Shimizu 890ddd
#include <string></string>
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qaction></qaction>
Toshihiro Shimizu 890ddd
#include <qmenu></qmenu>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TOONZQT_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// forward declaration
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
class QAction;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// base class
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
class DVAPI CommandHandlerInterface
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	virtual ~CommandHandlerInterface() {}
Toshihiro Shimizu 890ddd
	virtual void execute() = 0;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// command identifier (e.g. "copy"). They are NOT command names (e.g. "&Copy")
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
typedef const char *CommandId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
enum CommandType {
Toshihiro Shimizu 890ddd
	UndefinedCommandType = 0,
Toshihiro Shimizu 890ddd
	RightClickMenuCommandType,
Toshihiro Shimizu 890ddd
	MenuFileCommandType,
Toshihiro Shimizu 890ddd
	MenuEditCommandType,
Toshihiro Shimizu 890ddd
	MenuScanCleanupCommandType,
Toshihiro Shimizu 890ddd
	MenuLevelCommandType,
Toshihiro Shimizu 890ddd
	MenuXsheetCommandType,
Toshihiro Shimizu 890ddd
	MenuCellsCommandType,
Toshihiro Shimizu 890ddd
	MenuViewCommandType,
Toshihiro Shimizu 890ddd
	MenuWindowsCommandType,
Toshihiro Shimizu 890ddd
	PlaybackCommandType,
Toshihiro Shimizu 890ddd
	RGBACommandType,
Toshihiro Shimizu 890ddd
	FillCommandType,
Toshihiro Shimizu 890ddd
	ToolCommandType,
Toshihiro Shimizu 890ddd
	ToolModifierCommandType,
Toshihiro Shimizu 890ddd
	ZoomCommandType,
Toshihiro Shimizu 890ddd
	MiscCommandType,
Toshihiro Shimizu 890ddd
	MenuCommandType
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class AuxActionsCreator
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	AuxActionsCreator();
Toshihiro Shimizu 890ddd
	virtual ~AuxActionsCreator(){};
Toshihiro Shimizu 890ddd
	virtual void createActions(QObject *parent) = 0;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class AuxActionsCreatorManager
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool m_auxActionsCreated;
Toshihiro Shimizu 890ddd
	std::vector<auxactionscreator *=""> m_auxActionsCreators;</auxactionscreator>
Toshihiro Shimizu 890ddd
	AuxActionsCreatorManager();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	static AuxActionsCreatorManager *instance();
Toshihiro Shimizu 890ddd
	void addAuxActionsCreator(AuxActionsCreator *auxActionsCreator);
Toshihiro Shimizu 890ddd
	void createAuxActions(QObject *parent);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// command manager:
Toshihiro Shimizu 890ddd
//    setExecutor(id, executor)
Toshihiro Shimizu 890ddd
//    setAction(id, qaction)
Toshihiro Shimizu 890ddd
//    execute(qaction)/
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
class DVAPI CommandManager
Toshihiro Shimizu 890ddd
{ // singleton
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	class Node
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		std::string m_id;
Toshihiro Shimizu 890ddd
		CommandType m_type;
Toshihiro Shimizu 890ddd
		QAction *m_qaction;
Toshihiro Shimizu 890ddd
		CommandHandlerInterface *m_handler;
Toshihiro Shimizu 890ddd
		bool m_enabled;
Toshihiro Shimizu 890ddd
		QString m_onText, m_offText; // for toggle commands. e.g. show/hide something
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		Node(CommandId id)
Toshihiro Shimizu 890ddd
			: m_id(id), m_type(UndefinedCommandType), m_qaction(0), m_handler(0), m_enabled(true) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		~Node()
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			if (m_handler)
Toshihiro Shimizu 890ddd
				delete m_handler;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::map<std::string, *="" node=""> m_idTable;</std::string,>
Toshihiro Shimizu 890ddd
	std::map<qaction *="" *,="" node=""> m_qactionTable;</qaction>
Toshihiro Shimizu 890ddd
	std::map<std::string, *="" node=""> m_shortcutTable;</std::string,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	CommandManager();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Node *getNode(CommandId id, bool createIfNeeded = true);
Toshihiro Shimizu 890ddd
	void setShortcut(CommandId id, QAction *action, std::string shortcutString);
Toshihiro Shimizu 890ddd
	void createAuxActions();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	static CommandManager *instance();
Toshihiro Shimizu 890ddd
	~CommandManager();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setHandler(CommandId id, CommandHandlerInterface *handler);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void define(
Toshihiro Shimizu 890ddd
		CommandId id,
Toshihiro Shimizu 890ddd
		CommandType type,
Toshihiro Shimizu 890ddd
		std::string defaultShortcutString,
Toshihiro Shimizu 890ddd
		QAction *action);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QAction *createAction(const char *id, const char *name, const char *defaultShortcut);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void getActions(CommandType type, std::vector<qaction *=""> &actions);</qaction>
Toshihiro Shimizu 890ddd
	QAction *getActionFromShortcut(std::string shortcutString);
Toshihiro Shimizu 890ddd
	std::string getShortcutFromAction(QAction *action);
Toshihiro Shimizu 890ddd
	std::string getShortcutFromId(CommandId id);
Toshihiro Shimizu 890ddd
	int getKeyFromShortcut(const std::string &shortcut);
Toshihiro Shimizu 890ddd
	int getKeyFromId(CommandId id);
Toshihiro Shimizu 890ddd
	void setShortcut(QAction *action, std::string shortcutString);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QAction *getAction(CommandId id, bool createIfNeeded = false);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// createAction creates a new indepenent QAction with text and shortcut
Toshihiro Shimizu 890ddd
	// if the action is a toggle action (e.g. show/hide something) the text is controlled by state
Toshihiro Shimizu 890ddd
	// you can use createAction() for context menu
Toshihiro Shimizu 890ddd
	QAction *createAction(CommandId id, QObject *parent = 0, bool state = true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void execute(QAction *action);
Toshihiro Shimizu 890ddd
	/*! If action is defined in m_qactionTable recall \b execute(action), otherwise recall execute(menuAction).*/
Toshihiro Shimizu 890ddd
	void execute(QAction *action, QAction *menuAction);
Toshihiro Shimizu 890ddd
	void execute(CommandId id);
Toshihiro Shimizu 890ddd
	void enable(CommandId id, bool enabled);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// if id is a toggle (e.g. a checkable menu item) then set its status;
Toshihiro Shimizu 890ddd
	// note: this will trigger any associated handler
Toshihiro Shimizu 890ddd
	void setChecked(CommandId id, bool checked);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// use setToggleTexts for toggle commands that have two names according to the current status. e.g. show/hide something
Toshihiro Shimizu 890ddd
	void setToggleTexts(CommandId id, const QString &onText, const QString &offText);
shun_iwasawa dde155
shun_iwasawa dde155
	std::string getIdFromAction(QAction* action);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// CommandHandlerHelper = target + method
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
class CommandHandlerHelper : public CommandHandlerInterface
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	T *m_target;
Toshihiro Shimizu 890ddd
	void (T::*m_method)();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	CommandHandlerHelper(T *target, void (T::*method)())
Toshihiro Shimizu 890ddd
		: m_target(target), m_method(method)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void execute() { (m_target->*m_method)(); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class r="" t,="" typename=""></class>
Toshihiro Shimizu 890ddd
class CommandHandlerHelper2 : public CommandHandlerInterface
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	T *m_target;
Toshihiro Shimizu 890ddd
	void (T::*m_method)(R value);
Toshihiro Shimizu 890ddd
	R m_value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	CommandHandlerHelper2(T *target, void (T::*method)(R), R value)
Toshihiro Shimizu 890ddd
		: m_target(target), m_method(method), m_value(value)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void execute() { (m_target->*m_method)(m_value); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
inline void setCommandHandler(CommandId id, T *target, void (T::*method)())
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	CommandManager::instance()->setHandler(id, new CommandHandlerHelper<t>(target, method));</t>
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI MenuItemHandler : public QObject
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Q_OBJECT
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	MenuItemHandler(CommandId cmdId);
Toshihiro Shimizu 890ddd
	virtual ~MenuItemHandler(){};
Toshihiro Shimizu 890ddd
	virtual void execute() = 0;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
class OpenPopupCommandHandler : public MenuItemHandler
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	T *m_popup;
Toshihiro Shimizu 890ddd
	CommandId m_id;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	OpenPopupCommandHandler(CommandId cmdId)
Toshihiro Shimizu 890ddd
		: MenuItemHandler(cmdId), m_popup(0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void execute()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (!m_popup)
Toshihiro Shimizu 890ddd
			m_popup = new T();
Toshihiro Shimizu 890ddd
		m_popup->show();
Toshihiro Shimizu 890ddd
		m_popup->raise();
Toshihiro Shimizu 890ddd
		m_popup->activateWindow();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI DVAction : public QAction
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Q_OBJECT
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	DVAction(const QString &text, QObject *parent);
Toshihiro Shimizu 890ddd
	DVAction(const QIcon &icon, const QString &text, QObject *parent);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public slots:
Toshihiro Shimizu 890ddd
	void onTriggered();
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI DVMenuAction : public QMenu
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Q_OBJECT
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int m_triggeredActionIndex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	DVMenuAction(const QString &text, QWidget *parent, QList<qstring> actions);</qstring>
Toshihiro Shimizu 890ddd
	void setActions(QList<qstring> actions);</qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getTriggeredActionIndex() { return m_triggeredActionIndex; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public slots:
Toshihiro Shimizu 890ddd
	void onTriggered(QAction *action);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // MENUBAR_COMMAND_H