LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
lambdaeventfilter.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <type_traits>
12#include <QEvent>
13#include <QObject>
14#include "typegetter.h"
15
16namespace LC::Util
17{
18 namespace detail
19 {
20 template<QEvent::Type Type, typename F>
21 class LambdaEventFilter : public QObject
22 {
23 F F_;
24
25 using EventType_t = std::remove_pointer_t<std::decay_t<ArgType_t<F, 0>>>;
26 public:
27 LambdaEventFilter (F&& f, QObject& parent)
28 : QObject { &parent }
29 , F_ { std::move (f) }
30 {
31 }
32
33 bool eventFilter (QObject*, QEvent *srcEv) override
34 {
35 if (Type != QEvent::None && Type != srcEv->type ())
36 return false;
37
38 const auto ev = dynamic_cast<EventType_t*> (srcEv);
39 if (!ev)
40 return false;
41
42 if constexpr (requires { F_ (ev, static_cast<QObject&> (*this)); })
43 return F_ (ev, static_cast<QObject&> (*this));
44 else
45 return F_ (ev);
46 }
47 };
48 }
49
50 template<QEvent::Type Type = QEvent::None, typename F>
51 auto MakeLambdaEventFilter (F&& f, QObject& parent)
52 {
53 return new detail::LambdaEventFilter<Type, std::decay_t<F>> { std::forward<F> (f), parent };
54 }
55}
LambdaEventFilter(F &&f, QObject &parent)
bool eventFilter(QObject *, QEvent *srcEv) override
auto MakeLambdaEventFilter(F &&f, QObject &parent)