Commit dfe0413c authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Forward mouse/key handler

parent 19129599
......@@ -16,6 +16,10 @@
*/
#include "webengineview.h"
#include <QEvent>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
using namespace MessageViewer;
......@@ -23,19 +27,111 @@ class MessageViewer::WebEngineViewPrivate
{
public:
WebEngineViewPrivate()
: mCurrentWidget(Q_NULLPTR)
{
}
QWidget *mCurrentWidget;
};
WebEngineView::WebEngineView(QWidget *parent)
: QWebEngineView(parent),
d(new MessageViewer::WebEngineViewPrivate)
{
installEventFilter(this);
}
WebEngineView::~WebEngineView()
{
delete d;
}
void WebEngineView::forwardWheelEvent(QWheelEvent *event)
{
qDebug()<<" void WebEngineView::forwardWheelEvent(QWheelEvent *event)";
}
void WebEngineView::forwardKeyPressEvent(QKeyEvent *event)
{
qDebug()<<" void WebEngineView::forwardKeyPressEvent(QKeyEvent *event)";
}
void WebEngineView::forwardKeyReleaseEvent(QKeyEvent *event)
{
qDebug()<<" void WebEngineView::forwardKeyReleaseEvent(QKeyEvent *event)";
}
void WebEngineView::forwardMousePressEvent(QMouseEvent *event)
{
qDebug()<<" void WebEngineView::forwardMousePressEvent(QKeyEvent *event)";
}
void WebEngineView::forwardMouseMoveEvent(QMouseEvent *event)
{
qDebug()<<" void WebEngineView::forwardMouseMoveEvent(QKeyEvent *event)";
}
void WebEngineView::forwardMouseReleaseEvent(QMouseEvent *event)
{
qDebug()<<" void WebEngineView::forwardMouseReleaseEvent(QKeyEvent *event)";
}
bool WebEngineView::eventFilter(QObject *obj, QEvent *event)
{
// Hack to find widget that receives input events
if (obj == this && event->type() == QEvent::ChildAdded) {
QWidget *child = qobject_cast<QWidget*>(static_cast<QChildEvent*>(event)->child());
if (child && child->inherits("QtWebEngineCore::RenderWidgetHostViewQtDelegateWidget")) {
d->mCurrentWidget = child;
d->mCurrentWidget->installEventFilter(this);
}
}
// Forward events to WebEngineView
if (obj == d->mCurrentWidget) {
#define HANDLE_EVENT(f, t) \
{ \
bool wasAccepted = event->isAccepted(); \
event->setAccepted(false); \
f(static_cast<t*>(event)); \
bool ret = event->isAccepted(); \
event->setAccepted(wasAccepted); \
return ret; \
}
switch (event->type()) {
case QEvent::KeyPress:
HANDLE_EVENT(forwardKeyPressEvent, QKeyEvent);
case QEvent::KeyRelease:
HANDLE_EVENT(forwardKeyReleaseEvent, QKeyEvent);
case QEvent::MouseButtonPress:
HANDLE_EVENT(forwardMousePressEvent, QMouseEvent);
case QEvent::MouseButtonRelease:
HANDLE_EVENT(forwardMouseReleaseEvent, QMouseEvent);
case QEvent::MouseMove:
HANDLE_EVENT(forwardMouseMoveEvent, QMouseEvent);
case QEvent::Wheel:
HANDLE_EVENT(forwardWheelEvent, QWheelEvent);
default:
break;
}
#undef HANDLE_EVENT
}
// Block already handled events
if (obj == this) {
switch (event->type()) {
case QEvent::KeyPress:
case QEvent::KeyRelease:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseMove:
case QEvent::Wheel:
return true;
default:
break;
}
}
return QWebEngineView::eventFilter(obj, event);
}
......@@ -30,6 +30,16 @@ class MESSAGEVIEWER_EXPORT WebEngineView : public QWebEngineView
public:
explicit WebEngineView(QWidget *parent = Q_NULLPTR);
~WebEngineView();
protected:
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
void forwardWheelEvent(QWheelEvent *event);
void forwardKeyPressEvent(QKeyEvent *event);
void forwardKeyReleaseEvent(QKeyEvent *event);
void forwardMousePressEvent(QMouseEvent *event);
void forwardMouseMoveEvent(QMouseEvent *event);
void forwardMouseReleaseEvent(QMouseEvent *event);
private:
WebEngineViewPrivate *const d;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment