diff --git a/src/inputcontrol/inputcontrol.pri b/src/inputcontrol/inputcontrol.pri index bcbbdaf..8c1f481 100644 --- a/src/inputcontrol/inputcontrol.pri +++ b/src/inputcontrol/inputcontrol.pri @@ -1,8 +1,10 @@ HEADERS += \ $$PWD/controlevent.h \ - $$PWD/controller.h + $$PWD/controller.h \ + $$PWD/inputconvert.h SOURCES += \ $$PWD/controlevent.cpp \ - $$PWD/controller.cpp + $$PWD/controller.cpp \ + $$PWD/inputconvert.cpp diff --git a/src/inputcontrol/inputconvert.cpp b/src/inputcontrol/inputconvert.cpp new file mode 100644 index 0000000..165ee05 --- /dev/null +++ b/src/inputcontrol/inputconvert.cpp @@ -0,0 +1,97 @@ +#include "inputconvert.h" + +InputConvert::InputConvert() +{ + +} + +ControlEvent* InputConvert::mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize) +{ + if (!from) { + return Q_NULLPTR; + } + + // action + AndroidMotioneventAction action; + switch (from->type()) { + case QEvent::MouseButtonPress: + action = AMOTION_EVENT_ACTION_DOWN; + break; + case QEvent::MouseButtonRelease: + action = AMOTION_EVENT_ACTION_UP; + break; + case QEvent::MouseMove: + action = AMOTION_EVENT_ACTION_MOVE; + break; + default: + return Q_NULLPTR; + } + + // pos + QPointF pos = from->localPos(); + // convert pos + pos.setX(pos.x() * frameSize.width() / showSize.width()); + pos.setY(pos.y() * frameSize.height() / showSize.height()); + + // set data + ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_MOUSE); + if (!controlEvent) { + return Q_NULLPTR; + } + controlEvent->setMouseEventData(action, convertMouseButtons(from->buttons()), QRect(pos.toPoint(), frameSize)); + return controlEvent; +} + +ControlEvent *InputConvert::wheelEvent(const QWheelEvent *from, const QSize& frameSize, const QSize& showSize) +{ + if (!from) { + return Q_NULLPTR; + } + + // delta + qint32 hScroll = 0; + qint32 vScroll = 0; + switch (from->orientation()) { + case Qt::Horizontal: + hScroll = from->delta(); + break; + case Qt::Vertical: + vScroll = from->delta(); + break; + } + + // pos + QPointF pos = from->posF(); + // convert pos + pos.setX(pos.x() * frameSize.width() / showSize.width()); + pos.setY(pos.y() * frameSize.height() / showSize.height()); + + // set data + ControlEvent* controlEvent = new ControlEvent(ControlEvent::CET_SCROLL); + if (!controlEvent) { + return Q_NULLPTR; + } + controlEvent->setScrollEventData(QRect(pos.toPoint(), frameSize), hScroll, vScroll); + return controlEvent; +} + +AndroidMotioneventButtons InputConvert::convertMouseButtons(Qt::MouseButtons buttonState) +{ + quint32 buttons = 0; + if (buttonState & Qt::LeftButton) { + buttons |= AMOTION_EVENT_BUTTON_PRIMARY; + } + if (buttonState & Qt::RightButton) { + buttons |= AMOTION_EVENT_BUTTON_SECONDARY; + } + if (buttonState & Qt::MidButton) { + buttons |= AMOTION_EVENT_BUTTON_TERTIARY; + } + if (buttonState & Qt::XButton1) { + buttons |= AMOTION_EVENT_BUTTON_BACK; + } + if (buttonState & Qt::XButton2) { + buttons |= AMOTION_EVENT_BUTTON_FORWARD; + } + return (AndroidMotioneventButtons)buttons; +} diff --git a/src/inputcontrol/inputconvert.h b/src/inputcontrol/inputconvert.h new file mode 100644 index 0000000..13a72a6 --- /dev/null +++ b/src/inputcontrol/inputconvert.h @@ -0,0 +1,23 @@ +#ifndef INPUTCONVERT_H +#define INPUTCONVERT_H + +#include +#include + +#include "controlevent.h" + +class InputConvert +{ +public: + InputConvert(); + + // the frame size may be different from the real device size, so we need the size + // to which the absolute position apply, to scale it accordingly + static ControlEvent* mouseEvent(const QMouseEvent* from, const QSize& frameSize, const QSize& showSize); + static ControlEvent* wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize); + +private: + static AndroidMotioneventButtons convertMouseButtons(Qt::MouseButtons buttonState); +}; + +#endif // INPUTCONVERT_H diff --git a/src/videoform.cpp b/src/videoform.cpp index e029004..4ca11ee 100644 --- a/src/videoform.cpp +++ b/src/videoform.cpp @@ -3,6 +3,7 @@ #include "videoform.h" #include "ui_videoform.h" +#include "inputconvert.h" VideoForm::VideoForm(QWidget *parent) : QWidget(parent), @@ -91,14 +92,32 @@ void VideoForm::updateShowSize(const QSize &newSize) void VideoForm::mousePressEvent(QMouseEvent *event) { - QSize frameSize = ui->videoWidget->frameSize(); - QSize widgetWize = size(); - - QPoint pos = event->pos(); - // convert pos - pos.setX(pos.x() * 1.0f * frameSize.width() / widgetWize.width()); - pos.setY(pos.y() * 1.0f * frameSize.height() / widgetWize.height()); - - QRect rc(pos, frameSize); - m_controller.test(rc); + ControlEvent* controlEvent = InputConvert::mouseEvent(event, ui->videoWidget->frameSize(), size()); + if (controlEvent) { + m_controller.postControlEvent(controlEvent); + } +} + +void VideoForm::mouseReleaseEvent(QMouseEvent *event) +{ + ControlEvent* controlEvent = InputConvert::mouseEvent(event, ui->videoWidget->frameSize(), size()); + if (controlEvent) { + m_controller.postControlEvent(controlEvent); + } +} + +void VideoForm::mouseMoveEvent(QMouseEvent *event) +{ + ControlEvent* controlEvent = InputConvert::mouseEvent(event, ui->videoWidget->frameSize(), size()); + if (controlEvent) { + m_controller.postControlEvent(controlEvent); + } +} + +void VideoForm::wheelEvent(QWheelEvent *event) +{ + ControlEvent* controlEvent = InputConvert::wheelEvent(event, ui->videoWidget->frameSize(), size()); + if (controlEvent) { + m_controller.postControlEvent(controlEvent); + } } diff --git a/src/videoform.h b/src/videoform.h index 828ab24..56e86d9 100644 --- a/src/videoform.h +++ b/src/videoform.h @@ -25,6 +25,9 @@ private: protected: void mousePressEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void wheelEvent(QWheelEvent *event); private: Ui::videoForm *ui;