23
#include "PreCompiled.h"
32
#include "ActionFunction.h"
39
class ActionFunctionPrivate
42
QMap<QAction*, std::function<void()> > triggerMap;
43
QMap<QAction*, std::function<void(bool)> > toggleMap;
44
QMap<QAction*, std::function<void()> > hoverMap;
48
ActionFunction::ActionFunction(QObject* parent)
49
: QObject(parent), d_ptr(new ActionFunctionPrivate())
53
ActionFunction::~ActionFunction() = default;
55
void ActionFunction::trigger(QAction* action, std::function<void()> func)
59
d->triggerMap[action] = func;
60
connect(action, &QAction::triggered, this, &ActionFunction::triggered);
63
void ActionFunction::triggered()
67
auto a = qobject_cast<QAction*>(sender());
68
QMap<QAction*, std::function<void()> >::iterator it = d->triggerMap.find(a);
69
if (it != d->triggerMap.end()) {
75
void ActionFunction::toggle(QAction* action, std::function<void(bool)> func)
79
d->toggleMap[action] = func;
80
connect(action, &QAction::toggled, this, &ActionFunction::toggled);
83
void ActionFunction::toggled(bool on)
87
auto a = qobject_cast<QAction*>(sender());
88
QMap<QAction*, std::function<void(bool)> >::iterator it = d->toggleMap.find(a);
89
if (it != d->toggleMap.end()) {
95
void ActionFunction::hover(QAction* action, std::function<void()> func)
99
d->hoverMap[action] = func;
100
connect(action, &QAction::hovered, this, &ActionFunction::hovered);
103
void ActionFunction::hovered()
107
auto a = qobject_cast<QAction*>(sender());
108
QMap<QAction*, std::function<void()> >::iterator it = d->hoverMap.find(a);
109
if (it != d->hoverMap.end()) {
118
class TimerFunctionPrivate
121
std::function<void()> timeoutFunc;
122
std::function<void(QObject*)> timeoutFuncQObject;
123
std::function<void(QVariant)> timeoutFuncQVariant;
125
QPointer<QObject> argQObject;
126
QVariant argQVariant;
130
TimerFunction::TimerFunction(QObject* parent)
131
: QObject(parent), d_ptr(new TimerFunctionPrivate())
133
d_ptr->autoDelete = false;
136
TimerFunction::~TimerFunction() = default;
138
void TimerFunction::setFunction(std::function<void()> func)
141
d->timeoutFunc = func;
144
void TimerFunction::setFunction(std::function<void(QObject*)> func, QObject* args)
147
d->timeoutFuncQObject = func;
148
d->argQObject = args;
151
void TimerFunction::setFunction(std::function<void(QVariant)> func, QVariant args)
154
d->timeoutFuncQVariant = func;
155
d->argQVariant = args;
158
void TimerFunction::setAutoDelete(bool on)
164
void TimerFunction::timeout()
169
else if (d->timeoutFuncQObject)
170
d->timeoutFuncQObject(d->argQObject);
171
else if (d->timeoutFuncQVariant)
172
d->timeoutFuncQVariant(d->argQVariant);
177
void TimerFunction::singleShot(int ms)
179
QTimer::singleShot(ms, this, &Gui::TimerFunction::timeout);
182
#include "moc_ActionFunction.cpp"