3 #include <QXmlStreamWriter>
4 #include <QtGui/QKeyEvent>
5 #include <QtGui/QMouseEvent>
8 #include <Gui/RaGui.hpp>
10 #include <Core/Utils/Index.hpp>
11 #include <Core/Utils/Log.hpp>
12 #include <Core/Utils/Observable.hpp>
13 #include <Core/Utils/Singleton.hpp>
14 #include <Core/Utils/StdOptional.hpp>
41 Qt::KeyboardModifiers modifiers,
43 bool wheel =
false ) :
44 m_buttons { buttons }, m_modifiers { modifiers }, m_key { key }, m_wheel { wheel } {}
50 Qt::KeyboardModifiers modifiers = Qt::NoModifier ) :
51 m_buttons { buttons }, m_modifiers { modifiers } {}
56 explicit EventBinding(
int key, Qt::KeyboardModifiers modifiers = Qt::NoModifier ) :
57 m_modifiers { modifiers }, m_key { key } {}
62 explicit EventBinding(
bool wheel, Qt::KeyboardModifiers modifiers = Qt::NoModifier ) :
63 m_modifiers { modifiers }, m_wheel { wheel } {}
65 bool isMouseEvent() {
return m_buttons != Qt::NoButton; }
66 bool isWheelEvent() {
return m_wheel; }
67 bool isKeyEvent() {
return !isMouseEvent() && !isWheelEvent(); }
68 bool operator<(
const EventBinding& b )
const;
71 Qt::MouseButtons m_buttons { Qt::NoButton };
72 Qt::KeyboardModifiers m_modifiers { Qt::NoModifier };
75 bool m_wheel {
false };
83 void loadConfiguration(
const std::string& filename = {} );
88 bool saveConfiguration(
const std::string& filename = {} );
91 void reloadConfiguration();
93 std::string getLoadedFilename() {
return m_file->fileName().toStdString(); }
101 KeyMappingAction getAction(
const Context& context,
102 const Qt::MouseButtons& buttons,
103 const Qt::KeyboardModifiers& modifiers,
105 bool wheel =
false );
107 getAction(
const Context& context,
const QEvent* event,
int key,
bool wheel =
false );
108 KeyMappingAction getAction(
const Context& context,
const EventBinding& binding );
112 std::optional<EventBinding> getBinding(
const Context& context, KeyMappingAction action );
116 KeyMappingAction addAction(
const Context& context,
const std::string& actionName );
121 addAction(
const Context& context,
const EventBinding& binding,
const std::string& actionName );
126 void setActionBinding(
const Context& context,
127 const EventBinding& binding,
128 const KeyMappingAction& action );
136 Context addContext(
const std::string& contextName );
141 Context getContext(
const std::string& contextName );
148 KeyMappingAction getAction(
const Context& context,
const std::string& actionName );
151 std::string getActionName(
const Context& context,
const KeyMappingAction& action );
154 std::string getContextName(
const Context& context );
161 void removeListener(
int callbackId );
164 std::string getHelpText();
168 static std::string enumNamesFromMouseButtons(
const Qt::MouseButtons& buttons );
172 static std::string enumNamesFromKeyboardModifiers(
const Qt::KeyboardModifiers& modifiers );
177 static EventBinding createEventBindingFromStrings(
const std::string& buttonsString =
"",
178 const std::string& modifiersString =
"",
179 const std::string& keyString =
"",
180 const std::string& wheelString =
"" );
184 ~KeyMappingManager();
187 void saveNode( QXmlStreamWriter& stream,
const QDomNode& domNode );
188 void saveKeymap( QXmlStreamWriter& stream );
190 void loadConfigurationInternal(
const QDomDocument& domDocument );
191 void loadConfigurationTagsInternal( QDomElement& node );
192 KeyMappingAction loadConfigurationMappingInternal(
const std::string& context,
193 const std::string& keyString,
194 const std::string& modifiersString,
195 const std::string& buttonsString,
196 const std::string& wheelString,
197 const std::string& actionName );
202 static Qt::KeyboardModifiers getQtModifiersValue(
const std::string& modifierString );
207 static Qt::MouseButtons getQtMouseButtonsValue(
const std::string& buttonsString );
208 static int getKeyCode(
const std::string& keyString );
210 std::string m_defaultConfigFile;
213 using EventBindingMap = std::map<EventBinding, KeyMappingAction>;
214 using ContextNameMap = std::map<std::string, Context>;
215 using ActionNameMap = std::map<std::string, Ra::Core::Utils::Index>;
217 ContextNameMap m_contextNameToIndex;
218 std::vector<ActionNameMap> m_actionNameToIndex;
219 std::vector<EventBindingMap> m_bindingToAction;
238 template <
typename T>
250 T&
self() {
return static_cast<T&
>( *this ); }
252 m_keyMappingContext = c;
260 template <
typename T>
274 using Callback = std::function<void( QEvent* )>;
284 inline void addEventCallback( KeyMappingAction action, Callback callback );
291 addActionAndCallback(
const std::string& actionName,
298 inline bool triggerEventCallback( QEvent* event,
int key,
bool wheel =
false );
303 inline bool triggerEventCallback( KeyMappingAction action, QEvent* event );
306 std::map<KeyMappingAction, Callback> m_keymappingCallbacks;
314 inline bool KeyMappingManager::EventBinding::operator<(
const EventBinding& b )
const {
315 return ( m_buttons < b.m_buttons ) ||
316 ( ( m_buttons == b.m_buttons ) && ( m_modifiers < b.m_modifiers ) ) ||
317 ( ( m_buttons == b.m_buttons ) && ( m_modifiers == b.m_modifiers ) &&
318 ( m_key < b.m_key ) ) ||
319 ( ( m_buttons == b.m_buttons ) && ( m_modifiers == b.m_modifiers ) &&
320 ( m_key == b.m_key ) && ( m_wheel < b.m_wheel ) );
324 m_context { context } {}
327 Callback callback ) {
328 m_keymappingCallbacks[action] = callback;
334 Callback callback ) {
336 auto mgr = KeyMappingManager::getInstance();
337 auto action = mgr->addAction( m_context, binding, actionName );
344 auto itr = m_keymappingCallbacks.find( actionIndex );
345 if ( itr == m_keymappingCallbacks.end() )
return false;
346 itr->second( event );
351 auto mgr = KeyMappingManager::getInstance();
352 auto actionIndex = mgr->getAction( m_context, event, key, wheel );
std::function< void(Args...)> Observer
Observer functor type.
This class manage a collection of binding/callback associated with a context.
bool triggerEventCallback(QEvent *event, int key, bool wheel=false)
void addEventCallback(KeyMappingAction action, Callback callback)
KeyMappingManager::KeyMappingAction addActionAndCallback(const std::string &actionName, const KeyMappingManager::EventBinding &binding, Callback callback)
KeyMappingCallbackManager(Context context)
KeyMappingManageable decorator to use as CRTP.
static void configureKeyMapping()
Inner class to store event binding.
EventBinding(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, int key, bool wheel=false)
EventBinding(bool wheel, Qt::KeyboardModifiers modifiers=Qt::NoModifier)
EventBinding(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers=Qt::NoModifier)
EventBinding(int key, Qt::KeyboardModifiers modifiers=Qt::NoModifier)
An utility class used to map a (combination of) key / modifiers / mouse buttons to a specific action....
Ra::Core::Utils::Index KeyMappingAction
handle to an action
Ra::Core::Utils::Index Context
handle to a Context