2019-01-01 11:15:16 +01:00
|
|
|
|
#ifndef MENUACTION_H
|
2019-01-01 08:26:20 +01:00
|
|
|
|
#define MENUACTION_H
|
|
|
|
|
|
|
2019-01-01 11:15:16 +01:00
|
|
|
|
#include "MenuLocation.h"
|
|
|
|
|
|
#include "ToolbarLocation.h"
|
2019-08-14 09:06:48 +02:00
|
|
|
|
#include "plugin_support/PluginContentWidget.h"
|
2019-01-01 08:26:20 +01:00
|
|
|
|
|
2019-08-14 09:06:48 +02:00
|
|
|
|
#include <QAction>
|
2019-01-01 11:15:16 +01:00
|
|
|
|
#include <QIcon>
|
|
|
|
|
|
#include <QKeySequence>
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
|
#include <functional>
|
2019-08-14 09:06:48 +02:00
|
|
|
|
#include <typeindex>
|
2019-01-01 11:15:16 +01:00
|
|
|
|
|
|
|
|
|
|
class IPluginContentWidgetContext;
|
|
|
|
|
|
|
2019-08-14 09:06:48 +02:00
|
|
|
|
class BaseAction {
|
2019-01-01 08:26:20 +01:00
|
|
|
|
public:
|
2019-08-14 09:06:48 +02:00
|
|
|
|
explicit BaseAction(const QString &text);
|
2019-01-01 11:15:16 +01:00
|
|
|
|
|
|
|
|
|
|
const QIcon& icon() const;
|
|
|
|
|
|
const MenuLocation& menuLocation() const;
|
|
|
|
|
|
void setIcon(QIcon icon);
|
|
|
|
|
|
void setMenuLocation(MenuLocation menu_location);
|
2019-01-31 19:25:54 +01:00
|
|
|
|
void setToolbarLocation(ToolbarLocation toolbar_location);
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void setShortcut(QKeySequence shortcut);
|
2019-01-01 11:15:16 +01:00
|
|
|
|
void setText(QString text);
|
|
|
|
|
|
void setToolTip(QString tooltip);
|
2019-01-05 19:58:23 +01:00
|
|
|
|
const QKeySequence& shortcut() const;
|
2019-01-01 11:15:16 +01:00
|
|
|
|
const QString& text() const;
|
|
|
|
|
|
const QString& toolTip() const;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
QString m_text;
|
|
|
|
|
|
QString m_toolTip;
|
|
|
|
|
|
QIcon m_icon;
|
2019-01-05 19:58:23 +01:00
|
|
|
|
QKeySequence m_shortcut;
|
2019-01-01 11:15:16 +01:00
|
|
|
|
MenuLocation m_menuLocation;
|
2019-01-31 19:25:54 +01:00
|
|
|
|
ToolbarLocation m_toolbarLocation;
|
2019-01-01 11:15:16 +01:00
|
|
|
|
|
2019-08-14 09:06:48 +02:00
|
|
|
|
};
|
|
|
|
|
|
/** An action for in a menu or toolbar that does not pertain to a specific
|
|
|
|
|
|
* widget. It often will create a widget for instance a New or Open action.
|
|
|
|
|
|
* It does need a context.
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
class StaticAction: public BaseAction {
|
|
|
|
|
|
public:
|
|
|
|
|
|
using Func = std::function<void(IPluginContentWidgetContext *context)>;
|
|
|
|
|
|
|
|
|
|
|
|
StaticAction(QString text, Func func);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void perform(IPluginContentWidgetContext *context) const;
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
2019-01-01 11:15:16 +01:00
|
|
|
|
Func m_func;
|
2019-01-01 08:26:20 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2019-08-14 09:06:48 +02:00
|
|
|
|
|
|
|
|
|
|
class ContextBaseAction: public BaseAction {
|
|
|
|
|
|
public:
|
|
|
|
|
|
using BaseAction::BaseAction;
|
|
|
|
|
|
virtual ~ContextBaseAction();
|
|
|
|
|
|
|
|
|
|
|
|
virtual std::type_index contextTypeIndex() const = 0;
|
|
|
|
|
|
|
|
|
|
|
|
virtual QAction* createAction(PluginContentWidget *widget)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto action = new QAction(widget);
|
|
|
|
|
|
action->setText(text());
|
|
|
|
|
|
setupConnectionForAction(action, widget);
|
|
|
|
|
|
return action;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void setupConnectionForAction(QAction *action, PluginContentWidget *context) = 0;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class QAction;
|
|
|
|
|
|
|
|
|
|
|
|
/** Defines an action that can be performed within a certain context.
|
|
|
|
|
|
* For instance the save action for a query can only be called when a query is loaded.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Note Func should be something that QAction::triggered can connect to. If not you
|
|
|
|
|
|
* could get quite a vague error.
|
|
|
|
|
|
*/
|
|
|
|
|
|
template <typename Context, typename Func>
|
|
|
|
|
|
class ContextAction: public ContextBaseAction {
|
|
|
|
|
|
public:
|
|
|
|
|
|
//using Func = void (Context::*)(bool);
|
|
|
|
|
|
|
|
|
|
|
|
ContextAction(QString text, Func func)
|
|
|
|
|
|
: ContextBaseAction(text)
|
|
|
|
|
|
, m_func(func)
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
std::type_index contextTypeIndex() const override
|
|
|
|
|
|
{
|
|
|
|
|
|
return std::type_index(typeid(Context));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Mostly a helper for the code that creates the QAction
|
|
|
|
|
|
// without the helper that would need template code to.
|
|
|
|
|
|
virtual void setupConnectionForAction(QAction *action, PluginContentWidget *context) override
|
|
|
|
|
|
{
|
|
|
|
|
|
QObject::connect(action, &QAction::triggered, dynamic_cast<Context*>(context), m_func);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
Func m_func;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Context, typename Func>
|
|
|
|
|
|
auto makeContextAction(QString text, Func func)
|
|
|
|
|
|
{
|
|
|
|
|
|
return std::make_shared<ContextAction<Context, Func>>(text, func);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-01-01 11:15:16 +01:00
|
|
|
|
#endif // MENUACTION_H
|