Skip to content

Commit

Permalink
[gui][tests] refactor EventBinding constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasPaulin committed Apr 13, 2022
1 parent 5c2d654 commit caf7c4a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/Gui/Utils/KeyMappingManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,37 @@ class RA_GUI_API KeyMappingManager : public Ra::Core::Utils::ObservableVoid
class EventBinding
{
public:
explicit EventBinding( Qt::MouseButtons buttons = Qt::NoButton,
Qt::KeyboardModifiers modifiers = Qt::NoModifier,
int key = -1,
bool wheel = false ) :

/// Default constructor : empty event
EventBinding() = default;
/// Construct a mouse event : explicitely construct an event from all properties
/// @note When constructing a mouse event, buttons must be explicitely typed to prevent
/// constructor ambiguity
explicit EventBinding( Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers = Qt::NoModifier ) :
m_buttons { buttons }, m_modifiers { modifiers } {}
/// Construct a key event : explicitely construct an event from all properties
explicit EventBinding( int key, Qt::KeyboardModifiers modifiers = Qt::NoModifier ) :
m_modifiers { modifiers }, m_key { key } {}
/// Construct a key event : explicitely construct an event from all properties
explicit EventBinding( bool wheel, Qt::KeyboardModifiers modifiers = Qt::NoModifier ) :
m_modifiers { modifiers }, m_wheel { wheel } {}
/// Construct a combined event : explicitely construct combined event event from all
/// properties
EventBinding( Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers,
int key,
bool wheel = false ) :
m_buttons { buttons }, m_modifiers { modifiers }, m_key { key }, m_wheel { wheel } {}
bool operator<( const EventBinding& b ) const;
bool isMouseEvent() { return m_buttons != Qt::NoButton; }
bool isWheelEvent() { return m_wheel; }
bool isKeyEvent() { return !isMouseEvent() && !isWheelEvent(); }

Qt::MouseButtons m_buttons;
Qt::KeyboardModifiers m_modifiers;
Qt::MouseButtons m_buttons { Qt::NoButton };
Qt::KeyboardModifiers m_modifiers { Qt::NoModifier };
// only one key
int m_key;
bool m_wheel;
int m_key { -1 };
bool m_wheel { false };
};

using KeyMappingAction = Ra::Core::Utils::Index;
Expand Down
14 changes: 14 additions & 0 deletions tests/unittest/Gui/keymapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,5 +242,19 @@ TEST_CASE( "Gui/Utils/KeyMappingManager", "[Gui][Gui/Utils][KeyMappingManager]"

auto invalidBinding { mgr->getBinding( cameraContext, {} ) };
REQUIRE( !invalidBinding );

KeyMappingManager::EventBinding mouseEvent { Qt::MouseButtons { Qt::LeftButton },
Qt::ShiftModifier };
REQUIRE( mouseEvent.isMouseEvent() );

KeyMappingManager::EventBinding wheelEvent { true, Qt::ControlModifier };
REQUIRE( wheelEvent.isWheelEvent() );

KeyMappingManager::EventBinding keyEvent { Qt::Key_Bar, Qt::MetaModifier };
REQUIRE( keyEvent.isKeyEvent() );

KeyMappingManager::EventBinding combinedEvent {
Qt::LeftButton, Qt::MetaModifier, Qt::Key_Bar };
REQUIRE( ( !combinedEvent.isKeyEvent() && combinedEvent.isMouseEvent() ) );
}
}

0 comments on commit caf7c4a

Please sign in to comment.