More modern termplate based version:
template<typename T>
class GuardedTransition : public T
{
public:
static_assert(std::is_convertible<T*, QAbstractTransition*>::value,
"GuardedTransition must inherit QAbstractTransition");
template <class... Args>
GuardedTransition(function<bool()> g, Args&&... args)
: T(std::forward<Args>(args)...), guard(g) {}
bool eventTest
(QEvent *e
) override
{ return T::eventTest(e) && guard(); }
private:
function<bool()> guard;
};
template<typename T>
class GuardedTransition : public T
{
public:
static_assert(std::is_convertible<T*, QAbstractTransition*>::value,
"GuardedTransition must inherit QAbstractTransition");
template <class... Args>
GuardedTransition(function<bool()> g, Args&&... args)
: T(std::forward<Args>(args)...), guard(g) {}
bool eventTest(QEvent *e) override
{ return T::eventTest(e) && guard(); }
private:
function<bool()> guard;
};
To copy to clipboard, switch view to plain text mode
Did not find out how to use the std::index stuff to make the guard the last parameter yet. However using this state transitions are finally somewhat readable:
static void addEventTransition(QState &src, QState &tgt,
function<bool()> guard = nullptr)
{
QAbstractTransition *t;
if (guard)
t = new GuardedTransition<QEventTransition>(guard, object, type, &src);
else
t = new QEventTransition(object, type, &src);
t->setTargetState(&tgt);
}
template <typename Func>
static void addSignalTransition(QState &src, QState &tgt,
const typename QtPrivate::FunctionPointer<Func>::Object *sender,
Func signal,
function<bool()> guard = nullptr)
{
QAbstractTransition *t;
if (guard)
t = new GuardedTransition<QSignalTransition>(guard, sender, signal, &src);
else
t = new QSignalTransition(sender, signal, &src);
t->setTargetState(&tgt);
}
static void addEventTransition(QState &src, QState &tgt,
QObject *object, QEvent::Type type,
function<bool()> guard = nullptr)
{
QAbstractTransition *t;
if (guard)
t = new GuardedTransition<QEventTransition>(guard, object, type, &src);
else
t = new QEventTransition(object, type, &src);
t->setTargetState(&tgt);
}
template <typename Func>
static void addSignalTransition(QState &src, QState &tgt,
const typename QtPrivate::FunctionPointer<Func>::Object *sender,
Func signal,
function<bool()> guard = nullptr)
{
QAbstractTransition *t;
if (guard)
t = new GuardedTransition<QSignalTransition>(guard, sender, signal, &src);
else
t = new QSignalTransition(sender, signal, &src);
t->setTargetState(&tgt);
}
To copy to clipboard, switch view to plain text mode
addSignalTransition(
settings_button_highlight, settings_button_visibile,
window, &Window::queryChanged,
[=] { window->input_frame.underMouse(); });
addSignalTransition(
settings_button_highlight, settings_button_visibile,
window, &Window::queryChanged,
[=] { window->input_frame.underMouse(); });
To copy to clipboard, switch view to plain text mode
Bookmarks