Skip to content

Commit c337047

Browse files
chapulinajennuine
andauthored
Sort out MinimalScene's mouse events (#295)
Signed-off-by: Louise Poubel <louise@openrobotics.org> Signed-off-by: ahcorde <ahcorde@gmail.com> Co-authored-by: Jenn Nguyen <jenn@openrobotics.org>
1 parent ac58762 commit c337047

File tree

9 files changed

+382
-121
lines changed

9 files changed

+382
-121
lines changed

include/ignition/gui/Conversions.hh

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ namespace ignition
4444

4545
namespace gui
4646
{
47-
/// \brief Return the equivalent qt color
47+
/// \brief Return the equivalent Qt color
4848
/// \param[in] _color Ignition color to convert
4949
/// \return Qt color value
5050
IGNITION_GUI_VISIBLE
5151
QColor convert(const math::Color &_color);
5252

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

65-
/// \brief Return the equivalent ignition vector.
65+
/// \brief Return the equivalent Ignition vector.
6666
/// \param[in] _pt QPointF to convert
6767
/// \return Ignition Vector2d.
6868
IGNITION_GUI_VISIBLE
6969
math::Vector2d convert(const QPointF &_pt);
7070

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

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

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

92+
/// \brief Return the equivalent Ignition mouse event.
93+
///
94+
/// Note that there isn't a 1-1 mapping between these types.
95+
/// \param[in] _e Qt wheel event
96+
/// \return Ignition mouse event
97+
IGNITION_GUI_VISIBLE
98+
common::MouseEvent convert(const QWheelEvent &_e);
99+
92100
/// \brief Return the equivalent ignition key event.
93101
///
94102
/// \param[in] _e Qt key event

include/ignition/gui/GuiEvents.hh

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ namespace ignition
151151
};
152152

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

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

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

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

336335
/// \brief Unique type for this event.
@@ -390,6 +389,66 @@ namespace ignition
390389
/// \brief Private data pointer
391390
IGN_UTILS_IMPL_PTR(dataPtr)
392391
};
392+
393+
/// \brief Event which is called to broadcast information about mouse
394+
/// scrolls on the scene.
395+
class IGNITION_GUI_VISIBLE ScrollOnScene : public QEvent
396+
{
397+
/// \brief Constructor
398+
/// \param[in] _mouse The scroll mouse event on the scene
399+
public: explicit ScrollOnScene(const common::MouseEvent &_mouse);
400+
401+
/// \brief Unique type for this event.
402+
static const QEvent::Type kType = QEvent::Type(QEvent::MaxUser - 16);
403+
404+
/// \brief Return the scroll mouse event
405+
public: const common::MouseEvent &Mouse() const;
406+
407+
/// \internal
408+
/// \brief Private data pointer
409+
IGN_UTILS_IMPL_PTR(dataPtr)
410+
};
411+
412+
/// \brief Event which is called to broadcast information about mouse
413+
/// drags on the scene.
414+
class IGNITION_GUI_VISIBLE DragOnScene : public QEvent
415+
{
416+
/// \brief Constructor
417+
/// \param[in] _mouse The drag mouse event on the scene
418+
public: explicit DragOnScene(const common::MouseEvent &_mouse);
419+
420+
/// \brief Unique type for this event.
421+
static const QEvent::Type kType = QEvent::Type(QEvent::MaxUser - 17);
422+
423+
/// \brief Get the point within the scene over which the user is
424+
/// dragging.
425+
/// \return The 2D point
426+
public: common::MouseEvent Mouse() const;
427+
428+
/// \internal
429+
/// \brief Private data pointer
430+
IGN_UTILS_IMPL_PTR(dataPtr)
431+
};
432+
433+
/// \brief Event which is called to broadcast information about mouse
434+
/// presses on the scene, with right, left or middle buttons.
435+
class IGNITION_GUI_VISIBLE MousePressOnScene : public QEvent
436+
{
437+
/// \brief Constructor
438+
/// \param[in] _mouse The mouse event on the scene
439+
public: MousePressOnScene(
440+
const common::MouseEvent &_mouse);
441+
442+
/// \brief Unique type for this event.
443+
static const QEvent::Type kType = QEvent::Type(QEvent::MaxUser - 18);
444+
445+
/// \brief Return the button press mouse event
446+
public: const common::MouseEvent &Mouse() const;
447+
448+
/// \internal
449+
/// \brief Private data pointer
450+
IGN_UTILS_IMPL_PTR(dataPtr)
451+
};
393452
}
394453
}
395454
}

src/Conversions.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,43 @@ ignition::common::MouseEvent ignition::gui::convert(const QMouseEvent &_e)
114114
return event;
115115
}
116116

117+
//////////////////////////////////////////////////
118+
ignition::common::MouseEvent ignition::gui::convert(const QWheelEvent &_e)
119+
{
120+
common::MouseEvent event;
121+
122+
event.SetType(common::MouseEvent::SCROLL);
123+
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
124+
event.SetPos(_e.x(), _e.y());
125+
#else
126+
event.SetPos(_e.position().x(), _e.position().y());
127+
#endif
128+
double scroll = (_e.angleDelta().y() > 0) ? -1.0 : 1.0;
129+
event.SetScroll(scroll, scroll);
130+
131+
// Buttons
132+
if (_e.buttons() & Qt::LeftButton)
133+
event.SetButtons(event.Buttons() | common::MouseEvent::LEFT);
134+
135+
if (_e.buttons() & Qt::RightButton)
136+
event.SetButtons(event.Buttons() | common::MouseEvent::RIGHT);
137+
138+
if (_e.buttons() & Qt::MiddleButton)
139+
event.SetButtons(event.Buttons() | common::MouseEvent::MIDDLE);
140+
141+
// Modifiers
142+
if (_e.modifiers() & Qt::ShiftModifier)
143+
event.SetShift(true);
144+
145+
if (_e.modifiers() & Qt::ControlModifier)
146+
event.SetControl(true);
147+
148+
if (_e.modifiers() & Qt::AltModifier)
149+
event.SetAlt(true);
150+
151+
return event;
152+
}
153+
117154
//////////////////////////////////////////////////
118155
ignition::common::KeyEvent ignition::gui::convert(const QKeyEvent &_e)
119156
{

src/Conversions_TEST.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ TEST(ConversionsTest, MouseEvent)
146146
EXPECT_TRUE(ignEvent.Dragging());
147147
EXPECT_TRUE(ignEvent.Alt());
148148
}
149+
150+
// Scroll
151+
{
152+
#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
153+
QWheelEvent qtEvent(QPointF(123, 456), QPointF(1000, 2000), QPoint(2, 3),
154+
QPoint(1, 4), -1, Qt::Horizontal, Qt::MiddleButton, Qt::ShiftModifier,
155+
Qt::ScrollUpdate, Qt::MouseEventNotSynthesized, false);
156+
#else
157+
QWheelEvent qtEvent(QPointF(123, 456), QPointF(1000, 2000), QPoint(2, 3),
158+
QPoint(1, 4), Qt::MiddleButton, Qt::ShiftModifier, Qt::ScrollUpdate,
159+
false);
160+
#endif
161+
162+
auto ignEvent = convert(qtEvent);
163+
164+
EXPECT_EQ(ignEvent.Type(), common::MouseEvent::SCROLL);
165+
EXPECT_EQ(ignEvent.Pos(), math::Vector2i(123, 456));
166+
EXPECT_EQ(ignEvent.Scroll(), math::Vector2i(-1, -1));
167+
EXPECT_EQ(ignEvent.Buttons(), common::MouseEvent::MIDDLE);
168+
EXPECT_FALSE(ignEvent.Dragging());
169+
EXPECT_TRUE(ignEvent.Shift());
170+
}
149171
}
150172

151173
/////////////////////////////////////////////////

src/GuiEvents.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ class ignition::gui::events::DropOnScene::Implementation
118118
public: ignition::math::Vector2i mouse;
119119
};
120120

121+
class ignition::gui::events::ScrollOnScene::Implementation
122+
{
123+
/// \brief Mouse event with scroll information.
124+
public: common::MouseEvent mouse;
125+
};
126+
127+
class ignition::gui::events::DragOnScene::Implementation
128+
{
129+
/// \brief Mouse event with drag information.
130+
public: common::MouseEvent mouse;
131+
};
132+
133+
class ignition::gui::events::MousePressOnScene::Implementation
134+
{
135+
/// \brief Mouse event with press information.
136+
public: common::MouseEvent mouse;
137+
};
138+
121139
using namespace ignition;
122140
using namespace gui;
123141
using namespace events;
@@ -344,3 +362,42 @@ const ignition::math::Vector2i &DropOnScene::Mouse() const
344362
{
345363
return this->dataPtr->mouse;
346364
}
365+
366+
/////////////////////////////////////////////////
367+
ScrollOnScene::ScrollOnScene(const common::MouseEvent &_mouse)
368+
: QEvent(kType), dataPtr(utils::MakeImpl<Implementation>())
369+
{
370+
this->dataPtr->mouse = _mouse;
371+
}
372+
373+
/////////////////////////////////////////////////
374+
const common::MouseEvent &ScrollOnScene::Mouse() const
375+
{
376+
return this->dataPtr->mouse;
377+
}
378+
379+
/////////////////////////////////////////////////
380+
DragOnScene::DragOnScene(const common::MouseEvent &_mouse)
381+
: QEvent(kType), dataPtr(utils::MakeImpl<Implementation>())
382+
{
383+
this->dataPtr->mouse = _mouse;
384+
}
385+
386+
/////////////////////////////////////////////////
387+
common::MouseEvent DragOnScene::Mouse() const
388+
{
389+
return this->dataPtr->mouse;
390+
}
391+
392+
/////////////////////////////////////////////////
393+
MousePressOnScene::MousePressOnScene(const common::MouseEvent &_mouse)
394+
: QEvent(kType), dataPtr(utils::MakeImpl<Implementation>())
395+
{
396+
this->dataPtr->mouse = _mouse;
397+
}
398+
399+
/////////////////////////////////////////////////
400+
const common::MouseEvent &MousePressOnScene::Mouse() const
401+
{
402+
return this->dataPtr->mouse;
403+
}

src/GuiEvents_TEST.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,45 @@ TEST(GuiEventsTest, DropOnScene)
212212
EXPECT_EQ(ignition::math::Vector2i(3, 100), dropOnScene.Mouse());
213213
EXPECT_EQ("text", dropOnScene.DropText());
214214
}
215+
216+
/////////////////////////////////////////////////
217+
TEST(GuiEventsTest, ScrollOnScene)
218+
{
219+
ignition::common::MouseEvent mouse;
220+
mouse.SetControl(true);
221+
mouse.SetAlt(true);
222+
events::ScrollOnScene event(mouse);
223+
224+
EXPECT_LT(QEvent::User, event.type());
225+
EXPECT_TRUE(event.Mouse().Control());
226+
EXPECT_TRUE(event.Mouse().Alt());
227+
EXPECT_FALSE(event.Mouse().Shift());
228+
}
229+
230+
/////////////////////////////////////////////////
231+
TEST(GuiEventsTest, DragOnScene)
232+
{
233+
ignition::common::MouseEvent mouse;
234+
mouse.SetControl(true);
235+
mouse.SetAlt(true);
236+
events::DragOnScene event(mouse);
237+
238+
EXPECT_LT(QEvent::User, event.type());
239+
EXPECT_TRUE(event.Mouse().Control());
240+
EXPECT_TRUE(event.Mouse().Alt());
241+
EXPECT_FALSE(event.Mouse().Shift());
242+
}
243+
244+
/////////////////////////////////////////////////
245+
TEST(GuiEventsTest, MousePressOnScene)
246+
{
247+
ignition::common::MouseEvent mouse;
248+
mouse.SetControl(true);
249+
mouse.SetAlt(true);
250+
events::MousePressOnScene event(mouse);
251+
252+
EXPECT_LT(QEvent::User, event.type());
253+
EXPECT_TRUE(event.Mouse().Control());
254+
EXPECT_TRUE(event.Mouse().Alt());
255+
EXPECT_FALSE(event.Mouse().Shift());
256+
}

0 commit comments

Comments
 (0)