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

Sort out MinimalScene's mouse events #295

Merged
merged 10 commits into from
Oct 1, 2021
Merged
20 changes: 14 additions & 6 deletions include/ignition/gui/Conversions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ namespace ignition

namespace gui
{
/// \brief Return the equivalent qt color
/// \brief Return the equivalent Qt color
/// \param[in] _color Ignition color to convert
/// \return Qt color value
IGNITION_GUI_VISIBLE
QColor convert(const math::Color &_color);

/// \brief Return the equivalent ignition color
/// \brief Return the equivalent Ignition color
/// \param[in] _color Qt color to convert
/// \return Ignition color value
IGNITION_GUI_VISIBLE
Expand All @@ -62,25 +62,25 @@ namespace ignition
IGNITION_GUI_VISIBLE
QPointF convert(const math::Vector2d &_pt);

/// \brief Return the equivalent ignition vector.
/// \brief Return the equivalent Ignition vector.
/// \param[in] _pt QPointF to convert
/// \return Ignition Vector2d.
IGNITION_GUI_VISIBLE
math::Vector2d convert(const QPointF &_pt);

/// \brief Return the equivalent qt vector 3d.
/// \brief Return the equivalent Qt vector 3d.
/// \param[in] _vec Ignition vector 3d to convert.
/// \return Qt vector 3d value.
IGNITION_GUI_VISIBLE
QVector3D convert(const math::Vector3d &_vec);

/// \brief Return the equivalent ignition vector 3d.
/// \brief Return the equivalent Ignition vector 3d.
/// \param[in] _vec Qt vector 3d to convert.
/// \return Ignition vector 3d value
IGNITION_GUI_VISIBLE
math::Vector3d convert(const QVector3D &_vec);

/// \brief Return the equivalent ignition mouse event.
/// \brief Return the equivalent Ignition mouse event.
///
/// Note that there isn't a 1-1 mapping between these types, so fields such
/// as common::MouseEvent::PressPos need to be set afterwards.
Expand All @@ -89,6 +89,14 @@ namespace ignition
IGNITION_GUI_VISIBLE
common::MouseEvent convert(const QMouseEvent &_e);

/// \brief Return the equivalent Ignition mouse event.
///
/// Note that there isn't a 1-1 mapping between these types.
/// \param[in] _e Qt wheel event
/// \return Ignition mouse event
IGNITION_GUI_VISIBLE
common::MouseEvent convert(const QWheelEvent &_e);

/// \brief Return the equivalent ignition key event.
///
/// \param[in] _e Qt key event
Expand Down
71 changes: 65 additions & 6 deletions include/ignition/gui/GuiEvents.hh
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ namespace ignition
};

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

/// \brief Event which is called to broadcast the 3D coordinates of a
/// user's right click within the scene.
/// user's releasing the right button within the scene.
/// \sa RightClickOnScene
class IGNITION_GUI_VISIBLE RightClickToScene : public QEvent
{
Expand Down Expand Up @@ -257,7 +257,7 @@ namespace ignition
};

/// \brief Event which is called to broadcast information about left
/// mouse clicks on the scene.
/// mouse releases on the scene.
/// For the 3D coordinates of that point on the scene, see
/// `LeftClickToScene`.
/// \sa LeftClickToScene
Expand All @@ -280,7 +280,7 @@ namespace ignition
};

/// \brief Event which is called to broadcast information about right
/// mouse clicks on the scene.
/// mouse releases on the scene.
/// For the 3D coordinates of that point on the scene, see
/// `RightClickToScene`.
/// \sa RightClickToScene
Expand Down Expand Up @@ -329,8 +329,7 @@ namespace ignition
class IGNITION_GUI_VISIBLE HoverOnScene : public QEvent
{
/// \brief Constructor
/// \param[in] _point The point at which the mouse is hovering within
/// the scene
/// \param[in] _mouse The hover mouse event on the scene
public: explicit HoverOnScene(const common::MouseEvent &_mouse);

/// \brief Unique type for this event.
Expand Down Expand Up @@ -390,6 +389,66 @@ namespace ignition
/// \brief Private data pointer
IGN_UTILS_IMPL_PTR(dataPtr)
};

/// \brief Event which is called to broadcast information about mouse
/// scrolls on the scene.
class IGNITION_GUI_VISIBLE ScrollOnScene : public QEvent
{
/// \brief Constructor
/// \param[in] _mouse The scroll mouse event on the scene
public: explicit ScrollOnScene(const common::MouseEvent &_mouse);

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

/// \brief Return the scroll 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 mouse
/// drags on the scene.
class IGNITION_GUI_VISIBLE DragOnScene : public QEvent
{
/// \brief Constructor
/// \param[in] _mouse The drag mouse event on the scene
public: explicit DragOnScene(const common::MouseEvent &_mouse);

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

/// \brief Get the point within the scene over which the user is
/// dragging.
/// \return The 2D point
public: common::MouseEvent Mouse() const;

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

/// \brief Event which is called to broadcast information about mouse
/// presses on the scene, with right, left or middle buttons.
class IGNITION_GUI_VISIBLE MousePressOnScene : public QEvent
{
/// \brief Constructor
/// \param[in] _mouse The mouse event on the scene
public: MousePressOnScene(
const common::MouseEvent &_mouse);

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

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

/// \internal
/// \brief Private data pointer
IGN_UTILS_IMPL_PTR(dataPtr)
};
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/Conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,43 @@ ignition::common::MouseEvent ignition::gui::convert(const QMouseEvent &_e)
return event;
}

//////////////////////////////////////////////////
ignition::common::MouseEvent ignition::gui::convert(const QWheelEvent &_e)
{
common::MouseEvent event;

event.SetType(common::MouseEvent::SCROLL);
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
event.SetPos(_e.x(), _e.y());
#else
event.SetPos(_e.position().x(), _e.position().y());
#endif
double scroll = (_e.angleDelta().y() > 0) ? -1.0 : 1.0;
event.SetScroll(scroll, scroll);

// Buttons
if (_e.buttons() & Qt::LeftButton)
event.SetButtons(event.Buttons() | common::MouseEvent::LEFT);

if (_e.buttons() & Qt::RightButton)
event.SetButtons(event.Buttons() | common::MouseEvent::RIGHT);

if (_e.buttons() & Qt::MiddleButton)
event.SetButtons(event.Buttons() | common::MouseEvent::MIDDLE);

// Modifiers
if (_e.modifiers() & Qt::ShiftModifier)
event.SetShift(true);

if (_e.modifiers() & Qt::ControlModifier)
event.SetControl(true);

if (_e.modifiers() & Qt::AltModifier)
event.SetAlt(true);

return event;
}

//////////////////////////////////////////////////
ignition::common::KeyEvent ignition::gui::convert(const QKeyEvent &_e)
{
Expand Down
22 changes: 22 additions & 0 deletions src/Conversions_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ TEST(ConversionsTest, MouseEvent)
EXPECT_TRUE(ignEvent.Dragging());
EXPECT_TRUE(ignEvent.Alt());
}

// Scroll
{
#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
QWheelEvent qtEvent(QPointF(123, 456), QPointF(1000, 2000), QPoint(2, 3),
QPoint(1, 4), -1, Qt::Horizontal, Qt::MiddleButton, Qt::ShiftModifier,
Qt::ScrollUpdate, Qt::MouseEventNotSynthesized, false);
#else
QWheelEvent qtEvent(QPointF(123, 456), QPointF(1000, 2000), QPoint(2, 3),
QPoint(1, 4), Qt::MiddleButton, Qt::ShiftModifier, Qt::ScrollUpdate,
false);
#endif

auto ignEvent = convert(qtEvent);

EXPECT_EQ(ignEvent.Type(), common::MouseEvent::SCROLL);
EXPECT_EQ(ignEvent.Pos(), math::Vector2i(123, 456));
EXPECT_EQ(ignEvent.Scroll(), math::Vector2i(-1, -1));
EXPECT_EQ(ignEvent.Buttons(), common::MouseEvent::MIDDLE);
EXPECT_FALSE(ignEvent.Dragging());
EXPECT_TRUE(ignEvent.Shift());
}
}

/////////////////////////////////////////////////
Expand Down
57 changes: 57 additions & 0 deletions src/GuiEvents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ class ignition::gui::events::DropOnScene::Implementation
public: ignition::math::Vector2i mouse;
};

class ignition::gui::events::ScrollOnScene::Implementation
{
/// \brief Mouse event with scroll information.
public: common::MouseEvent mouse;
};

class ignition::gui::events::DragOnScene::Implementation
{
/// \brief Mouse event with drag information.
public: common::MouseEvent mouse;
};

class ignition::gui::events::MousePressOnScene::Implementation
{
/// \brief Mouse event with press information.
public: common::MouseEvent mouse;
};

using namespace ignition;
using namespace gui;
using namespace events;
Expand Down Expand Up @@ -344,3 +362,42 @@ const ignition::math::Vector2i &DropOnScene::Mouse() const
{
return this->dataPtr->mouse;
}

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

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

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

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

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

/////////////////////////////////////////////////
const common::MouseEvent &MousePressOnScene::Mouse() const
{
return this->dataPtr->mouse;
}
42 changes: 42 additions & 0 deletions src/GuiEvents_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,45 @@ TEST(GuiEventsTest, DropOnScene)
EXPECT_EQ(ignition::math::Vector2i(3, 100), dropOnScene.Mouse());
EXPECT_EQ("text", dropOnScene.DropText());
}

/////////////////////////////////////////////////
TEST(GuiEventsTest, ScrollOnScene)
{
ignition::common::MouseEvent mouse;
mouse.SetControl(true);
mouse.SetAlt(true);
events::ScrollOnScene 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, DragOnScene)
{
ignition::common::MouseEvent mouse;
mouse.SetControl(true);
mouse.SetAlt(true);
events::DragOnScene 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, MousePressOnScene)
{
ignition::common::MouseEvent mouse;
mouse.SetControl(true);
mouse.SetAlt(true);
events::MousePressOnScene event(mouse);

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