Skip to content

Add mouse wheel support #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ProjectPlayer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ ProjectScene {
onMouseMoved: (x, y)=> root.handleMouseMove(x, y)
onMousePressed: root.handleMousePress()
onMouseReleased: root.handleMouseRelease()
onMouseWheelUp: root.handleMouseWheelUp()
onMouseWheelDown: root.handleMouseWheelDown()
}

Component {
Expand Down
12 changes: 12 additions & 0 deletions src/mouseeventhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ bool MouseEventHandler::eventFilter(QObject *obj, QEvent *event)
return true;
}

case QEvent::Wheel: {
QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
const int delta = wheelEvent->angleDelta().y();

if (delta > 0)
emit mouseWheelUp();
else if (delta < 0)
emit mouseWheelDown();

break;
}

default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/mouseeventhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class MouseEventHandler : public QObject
void mouseMoved(qreal x, qreal y);
void mousePressed();
void mouseReleased();
void mouseWheelUp();
void mouseWheelDown();

private:
void getSprites();
Expand Down
13 changes: 13 additions & 0 deletions src/projectscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ void ProjectScene::handleMouseRelease()
m_engine->setMousePressed(false);
}

void ProjectScene::handleMouseWheelUp()
{
if (m_engine)
m_engine->mouseWheelUp();
}

void ProjectScene::handleMouseWheelDown()
{

if (m_engine)
m_engine->mouseWheelDown();
}

void ProjectScene::handleKeyPress(Qt::Key key, const QString &text)
{
m_pressedKeys.insert(key);
Expand Down
2 changes: 2 additions & 0 deletions src/projectscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ProjectScene : public QQuickItem
Q_INVOKABLE void handleMouseMove(qreal x, qreal y);
Q_INVOKABLE void handleMousePress();
Q_INVOKABLE void handleMouseRelease();
Q_INVOKABLE void handleMouseWheelUp();
Q_INVOKABLE void handleMouseWheelDown();

void handleKeyPress(Qt::Key key, const QString &text);
void handleKeyRelease(Qt::Key key, const QString &text);
Expand Down
2 changes: 2 additions & 0 deletions src/scenemousearea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ SceneMouseArea::SceneMouseArea(QQuickItem *parent) :
connect(m_mouseHandler, &MouseEventHandler::mouseMoved, this, &SceneMouseArea::mouseMoved);
connect(m_mouseHandler, &MouseEventHandler::mousePressed, this, &SceneMouseArea::mousePressed);
connect(m_mouseHandler, &MouseEventHandler::mouseReleased, this, &SceneMouseArea::mouseReleased);
connect(m_mouseHandler, &MouseEventHandler::mouseWheelUp, this, &SceneMouseArea::mouseWheelUp);
connect(m_mouseHandler, &MouseEventHandler::mouseWheelDown, this, &SceneMouseArea::mouseWheelDown);

setAcceptHoverEvents(true);
setAcceptTouchEvents(true);
Expand Down
2 changes: 2 additions & 0 deletions src/scenemousearea.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class SceneMouseArea : public QQuickItem
void mouseMoved(qreal x, qreal y);
void mousePressed();
void mouseReleased();
void mouseWheelUp();
void mouseWheelDown();
void stageChanged();
void projectLoaderChanged();

Expand Down
22 changes: 22 additions & 0 deletions test/mouseeventhandler/mouseeventhandler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,25 @@ TEST(MouseEventHandlerTest, MousePressReleaseEvent)
pressedSpy.clear();
releasedSpy.clear();
}

TEST(MouseEventHandlerTest, WheelEvent)
{
MouseEventHandler handler;
QSignalSpy upSpy(&handler, &MouseEventHandler::mouseWheelUp);
QSignalSpy downSpy(&handler, &MouseEventHandler::mouseWheelDown);
QWheelEvent event1(QPointF(), QPointF(), QPoint(2, 3), QPoint(10, 15), Qt::LeftButton, Qt::NoModifier, Qt::NoScrollPhase, false); // up
QWheelEvent event2(QPointF(), QPointF(), QPoint(1, -5), QPoint(10, -50), Qt::LeftButton, Qt::NoModifier, Qt::NoScrollPhase, false); // down
QWheelEvent event3(QPointF(), QPointF(), QPoint(-10, 0), QPoint(-100, 0), Qt::LeftButton, Qt::NoModifier, Qt::NoScrollPhase, false); // none

handler.eventFilter(nullptr, &event1);
ASSERT_EQ(upSpy.count(), 1);
ASSERT_EQ(downSpy.count(), 0);

handler.eventFilter(nullptr, &event2);
ASSERT_EQ(upSpy.count(), 1);
ASSERT_EQ(downSpy.count(), 1);

handler.eventFilter(nullptr, &event3);
ASSERT_EQ(upSpy.count(), 1);
ASSERT_EQ(downSpy.count(), 1);
}
13 changes: 13 additions & 0 deletions test/projectscene/projectscene_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ TEST(ProjectSceneTest, HandleMouseRelease)
scene.handleMouseRelease();
}

TEST(ProjectSceneTest, HandleMouseWheel)
{
ProjectScene scene;
EngineMock engine;
scene.setEngine(&engine);

EXPECT_CALL(engine, mouseWheelUp());
scene.handleMouseWheelUp();

EXPECT_CALL(engine, mouseWheelDown());
scene.handleMouseWheelDown();
}

TEST(ProjectSceneTest, HandleKeyPressAndRelease)
{
static const std::unordered_map<Qt::Key, KeyEvent::Type> SPECIAL_KEY_MAP = {
Expand Down