Skip to content
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

Added Mouse Events #228

Merged
merged 8 commits into from
Jun 25, 2021
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
55 changes: 55 additions & 0 deletions include/ignition/gui/GuiEvents.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
#define IGNITION_GUI_GUIEVENTS_HH_

#include <QEvent>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include <ignition/common/MouseEvent.hh>
#include <ignition/math/Vector2.hh>
#include <ignition/math/Vector3.hh>
#include <ignition/utils/ImplPtr.hh>

#include "ignition/gui/Export.hh"

namespace ignition
{
Expand Down Expand Up @@ -178,6 +185,7 @@ namespace ignition

/// \brief Event which is called to broadcast the 3D coordinates of a
/// user's left click within the scene.
/// \sa LeftClickOnScene
class LeftClickToScene : public QEvent
{
/// \brief Constructor
Expand All @@ -204,6 +212,7 @@ namespace ignition

/// \brief Event which is called to broadcast the 3D coordinates of a
/// user's right click within the scene.
/// \sa RightClickOnScene
class RightClickToScene : public QEvent
{
/// \brief Constructor
Expand Down Expand Up @@ -255,6 +264,52 @@ namespace ignition
/// for this event.
private: bool menuEnabled;
};

/// \brief Event which is called to broadcast information about left
/// mouse clicks on the scene.
/// For the 3D coordinates of that point on the scene, see
/// `LeftClickToScene`.
/// \sa LeftClickToScene
class IGNITION_GUI_VISIBLE LeftClickOnScene : public QEvent
{
/// \brief Constructor
/// \param[in] _mouse The left mouse event on the scene
public: explicit LeftClickOnScene(
const common::MouseEvent &_mouse);

/// \brief Unique type for this event.
static const QEvent::Type kType = QEvent::Type(QEvent::MaxUser - 10);
chapulina marked this conversation as resolved.
Show resolved Hide resolved

/// \brief Return the left mouse event
public: const common::MouseEvent &Mouse() const;

/// \internal
/// \brief Private data pointer
IGN_UTILS_IMPL_PTR(dataPtr)
};

/// \brief Event which is called to broadcast information about right
/// mouse clicks on the scene.
/// For the 3D coordinates of that point on the scene, see
/// `RightClickToScene`.
/// \sa RightClickToScene
class IGNITION_GUI_VISIBLE RightClickOnScene : public QEvent
{
/// \brief Constructor
/// \param[in] _mouse The right mouse event on the scene
public: RightClickOnScene(
const common::MouseEvent &_mouse);

/// \brief Unique type for this event.
static const QEvent::Type kType = QEvent::Type(QEvent::MaxUser - 11);

/// \brief Return the right mouse event
public: const common::MouseEvent &Mouse() const;

/// \internal
/// \brief Private data pointer
IGN_UTILS_IMPL_PTR(dataPtr)
};
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set (sources
${CMAKE_CURRENT_SOURCE_DIR}/Conversions.cc
${CMAKE_CURRENT_SOURCE_DIR}/Dialog.cc
${CMAKE_CURRENT_SOURCE_DIR}/DragDropModel.cc
${CMAKE_CURRENT_SOURCE_DIR}/GuiEvents.cc
${CMAKE_CURRENT_SOURCE_DIR}/Helpers.cc
${CMAKE_CURRENT_SOURCE_DIR}/ign.cc
${CMAKE_CURRENT_SOURCE_DIR}/MainWindow.cc
Expand Down
61 changes: 61 additions & 0 deletions src/GuiEvents.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "ignition/gui/GuiEvents.hh"

class ignition::gui::events::LeftClickOnScene::Implementation
{
/// \brief Mouse event
public: common::MouseEvent mouse;
};

class ignition::gui::events::RightClickOnScene::Implementation
{
/// \brief Mouse event
public: common::MouseEvent mouse;
};

using namespace ignition;
using namespace gui;
using namespace events;

/////////////////////////////////////////////////
RightClickOnScene::RightClickOnScene(const common::MouseEvent &_mouse)
: QEvent(kType), dataPtr(utils::MakeImpl<Implementation>())
{
this->dataPtr->mouse = _mouse;
}

/////////////////////////////////////////////////
const common::MouseEvent &RightClickOnScene::Mouse() const
{
return this->dataPtr->mouse;
}

/////////////////////////////////////////////////
LeftClickOnScene::LeftClickOnScene(const common::MouseEvent &_mouse)
: QEvent(kType), dataPtr(utils::MakeImpl<Implementation>())
{
this->dataPtr->mouse = _mouse;
}

/////////////////////////////////////////////////
const common::MouseEvent &LeftClickOnScene::Mouse() const
{
return this->dataPtr->mouse;
}

29 changes: 28 additions & 1 deletion src/GuiEvents_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ TEST(GuiEventsTest, LeftClickToScene)
EXPECT_EQ(math::Vector3d(1, 2, 3), event.Point());
}

/////////////////////////////////////////////////
TEST(GuiEventsTest, LeftClickOnScene)
{
ignition::common::MouseEvent mouse;
mouse.SetAlt(true);
mouse.SetShift(true);
events::LeftClickOnScene event(mouse);

EXPECT_LT(QEvent::User, event.type());
EXPECT_FALSE(event.Mouse().Control());
EXPECT_TRUE(event.Mouse().Alt());
EXPECT_TRUE(event.Mouse().Shift());
}

/////////////////////////////////////////////////
TEST(GuiEventsTest, RightClickToScene)
{
Expand All @@ -87,6 +101,20 @@ TEST(GuiEventsTest, RightClickToScene)
EXPECT_EQ(math::Vector3d(1, 2, 3), event.Point());
}

/////////////////////////////////////////////////
TEST(GuiEventsTest, RightClickOnScene)
{
ignition::common::MouseEvent mouse;
mouse.SetControl(true);
mouse.SetAlt(true);
events::RightClickOnScene event(mouse);

EXPECT_LT(QEvent::User, event.type());
EXPECT_TRUE(event.Mouse().Control());
EXPECT_TRUE(event.Mouse().Alt());
EXPECT_FALSE(event.Mouse().Shift());
}

/////////////////////////////////////////////////
TEST(GuiEventsTest, DropdownMenuEnabled)
{
Expand All @@ -100,4 +128,3 @@ TEST(GuiEventsTest, DropdownMenuEnabled)
EXPECT_LT(QEvent::User, event2.type());
EXPECT_EQ(false, event2.MenuEnabled());
}

6 changes: 6 additions & 0 deletions src/plugins/scene3d/Scene3D.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,10 @@ void IgnRenderer::BroadcastLeftClick()
auto pos = this->ScreenToScene(this->dataPtr->mouseEvent.Pos());

events::LeftClickToScene leftClickToSceneEvent(pos);
events::LeftClickOnScene leftClickOnSceneEvent(this->dataPtr->mouseEvent);

App()->sendEvent(App()->findChild<MainWindow *>(), &leftClickToSceneEvent);
App()->sendEvent(App()->findChild<MainWindow *>(), &leftClickOnSceneEvent);
}

/////////////////////////////////////////////////
Expand All @@ -1075,7 +1078,10 @@ void IgnRenderer::BroadcastRightClick()
auto pos = this->ScreenToScene(this->dataPtr->mouseEvent.Pos());

events::RightClickToScene rightClickToSceneEvent(pos);
events::RightClickOnScene rightClickOnSceneEvent(this->dataPtr->mouseEvent);

App()->sendEvent(App()->findChild<MainWindow *>(), &rightClickToSceneEvent);
App()->sendEvent(App()->findChild<MainWindow *>(), &rightClickOnSceneEvent);
}

/////////////////////////////////////////////////
Expand Down
51 changes: 49 additions & 2 deletions test/integration/scene3d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ TEST(Scene3DTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Load))

// Cleanup
plugins.clear();
win->QuickWindow()->close();
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -124,6 +125,17 @@ TEST(Scene3DTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Config))
ASSERT_NE(nullptr, camera);

EXPECT_EQ(math::Pose3d(1, 2, 3, 0, 0, 1.57), camera->WorldPose());

// Cleanup
auto plugins = win->findChildren<Plugin *>();
for (auto & p : plugins)
{
auto pluginName = p->CardItem()->objectName();
app.RemovePlugin(pluginName.toStdString());
}
win->QuickWindow()->close();
engine->DestroyScene(scene);
rendering::unloadEngine(engine->Name());
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -160,6 +172,12 @@ TEST(Scene3DTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Events))
bool receivedRenderEvent{false};
bool receivedRightEvent{false};
bool receivedLeftEvent{false};
bool receivedRightAltEvent{false};
bool receivedRightControlEvent{false};
bool receivedRightShiftEvent{false};
bool receivedLeftAltEvent{false};
bool receivedLeftControlEvent{false};
bool receivedLeftShiftEvent{false};
bool receivedHoverEvent{false};

// Position vectors reported by click events
Expand All @@ -179,12 +197,26 @@ TEST(Scene3DTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Events))
auto rightClickToScene = static_cast<events::RightClickToScene*>(_event);
rightClickPoint = rightClickToScene->Point();
}
else if (_event->type() == events::RightClickOnScene::kType)
{
auto rightClickOnScene = static_cast<events::RightClickOnScene*>(_event);
receivedRightAltEvent = rightClickOnScene->Mouse().Alt();
receivedRightControlEvent = rightClickOnScene->Mouse().Control();
receivedRightShiftEvent = rightClickOnScene->Mouse().Shift();
}
else if (_event->type() == events::LeftClickToScene::kType)
{
receivedLeftEvent = true;
auto leftClickToScene = static_cast<events::LeftClickToScene*>(_event);
leftClickPoint = leftClickToScene->Point();
}
else if (_event->type() == events::LeftClickOnScene::kType)
{
auto leftClickOnScene = static_cast<events::LeftClickOnScene*>(_event);
receivedLeftAltEvent = leftClickOnScene->Mouse().Alt();
receivedLeftControlEvent = leftClickOnScene->Mouse().Control();
receivedLeftShiftEvent = leftClickOnScene->Mouse().Shift();
}
else if (_event->type() == events::HoverToScene::kType)
{
receivedHoverEvent = true;
Expand All @@ -203,11 +235,11 @@ TEST(Scene3DTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Events))
std::this_thread::sleep_for(std::chrono::milliseconds(100));
QCoreApplication::processEvents();

QTest::mouseClick(win->QuickWindow(), Qt::RightButton, Qt::NoModifier);
QTest::mouseClick(win->QuickWindow(), Qt::RightButton, Qt::ShiftModifier);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
QCoreApplication::processEvents();

QTest::mouseClick(win->QuickWindow(), Qt::LeftButton, Qt::NoModifier);
QTest::mouseClick(win->QuickWindow(), Qt::LeftButton, Qt::AltModifier);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
QCoreApplication::processEvents();

Expand All @@ -218,9 +250,24 @@ TEST(Scene3DTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Events))
EXPECT_TRUE(receivedLeftEvent);
EXPECT_TRUE(receivedRightEvent);
EXPECT_TRUE(receivedHoverEvent);
EXPECT_TRUE(receivedLeftAltEvent);
EXPECT_FALSE(receivedLeftControlEvent);
EXPECT_FALSE(receivedLeftShiftEvent);
EXPECT_FALSE(receivedRightAltEvent);
EXPECT_FALSE(receivedRightControlEvent);
EXPECT_TRUE(receivedRightShiftEvent);

EXPECT_EQ(leftClickPoint, rightClickPoint);
EXPECT_NEAR(1.0, leftClickPoint.X(), 1e-4);
EXPECT_NEAR(11.942695, leftClickPoint.Y(), 1e-4);
EXPECT_NEAR(4.159424, leftClickPoint.Z(), 1e-4);

// Cleanups
auto plugins = win->findChildren<Plugin *>();
for (auto & p : plugins)
{
auto pluginName = p->CardItem()->objectName();
app.RemovePlugin(pluginName.toStdString());
}
win->QuickWindow()->close();
}