Blue Brain BioExplorer
KeyboardHandler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  *
4  * The Blue Brain BioExplorer is a tool for scientists to extract and analyse
5  * scientific data from visualization
6  *
7  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
8  *
9  * This library is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License version 3.0 as published
11  * by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include "KeyboardHandler.h"
24 
28 
29 #include <sstream>
30 
31 namespace core
32 {
33 void KeyboardHandler::registerKeyboardShortcut(const unsigned char key, const std::string& description,
34  std::function<void()> functor)
35 {
36  if (_registeredShortcuts.find(key) != _registeredShortcuts.end())
37  {
38  std::stringstream message;
39  message << key << " is already registered";
40  CORE_ERROR(message.str());
41  }
42  else
43  {
44  ShortcutInformation shortcutInformation = {description, functor};
45  _registeredShortcuts[key] = shortcutInformation;
46  }
47  _buildHelp();
48 }
49 
50 void KeyboardHandler::unregisterKeyboardShortcut(const unsigned char key)
51 {
52  auto it = _registeredShortcuts.find(key);
53  if (it != _registeredShortcuts.end())
54  _registeredShortcuts.erase(it);
55  _buildHelp();
56 }
57 
58 void KeyboardHandler::handleKeyboardShortcut(const unsigned char key)
59 {
60  auto it = _registeredShortcuts.find(key);
61  if (it != _registeredShortcuts.end())
62  {
63  CORE_DEBUG("Processing " << (*it).second.description);
64  (*it).second.functor();
65  }
66 }
67 
68 void KeyboardHandler::registerSpecialKey(const SpecialKey key, const std::string& description,
69  std::function<void()> functor)
70 {
71  if (_registeredSpecialKeys.find(key) != _registeredSpecialKeys.end())
72  {
73  std::stringstream message;
74  message << int(key) << " is already registered";
75  CORE_ERROR(message.str());
76  }
77  else
78  {
79  ShortcutInformation shortcutInformation = {description, functor};
80  _registeredSpecialKeys[key] = shortcutInformation;
81  }
82  _buildHelp();
83 }
84 
86 {
87  auto it = _registeredSpecialKeys.find(key);
88  if (it != _registeredSpecialKeys.end())
89  _registeredSpecialKeys.erase(it);
90  _buildHelp();
91 }
92 
94 {
95  auto it = _registeredSpecialKeys.find(key);
96  if (it != _registeredSpecialKeys.end())
97  {
98  CORE_INFO("Processing " << (*it).second.description);
99  (*it).second.functor();
100  }
101 }
102 
103 void KeyboardHandler::_buildHelp()
104 {
105  _helpStrings.clear();
106 
107  const auto specialKeyToString = [](const SpecialKey key)
108  {
109  switch (key)
110  {
111  case SpecialKey::RIGHT:
112  return "Right";
113  case SpecialKey::UP:
114  return "Up";
115  case SpecialKey::DOWN:
116  return "Down";
117  case SpecialKey::LEFT:
118  return "Left";
119  };
120 
121  return "INVALID";
122  };
123 
124  for (const auto& registeredShortcut : _registeredShortcuts)
125  {
126  std::stringstream ss;
127  ss << "'" << registeredShortcut.first << "' " << registeredShortcut.second.description;
128  _helpStrings.push_back(ss.str());
129  }
130  for (const auto& registeredShortcut : _registeredSpecialKeys)
131  {
132  std::stringstream ss;
133  ss << "'" << specialKeyToString(registeredShortcut.first) << "' " << registeredShortcut.second.description;
134  _helpStrings.push_back(ss.str());
135  }
136 
137 } // namespace core
138 
139 const std::vector<std::string>& KeyboardHandler::help() const
140 {
141  return _helpStrings;
142 }
143 
144 const std::string KeyboardHandler::getKeyboardShortcutDescription(const unsigned char key)
145 {
146  auto it = _registeredShortcuts.find(key);
147  if (it != _registeredShortcuts.end())
148  return (*it).second.description;
149 
150  return "";
151 }
152 
153 } // namespace core
void handle(const SpecialKey key)
Handles a special key.
void registerSpecialKey(const SpecialKey key, const std::string &description, std::function< void()> functor)
Registers a special key.
void handleKeyboardShortcut(const unsigned char key)
Handles a keyboard shortcut.
const std::string getKeyboardShortcutDescription(const unsigned char key)
Returns the description of a specific keyboard shortcut.
void registerKeyboardShortcut(const unsigned char key, const std::string &description, std::function< void()> functor)
Registers a keyboard shortcut.
void unregisterKeyboardShortcut(const unsigned char key)
Unregisters a keyboard shortcut.
const std::vector< std::string > & help() const
Returns a vector of help string descriptions for all registered keyboard shortcuts and special keys.
void unregisterSpecialKey(const SpecialKey key)
Unregisters a special key.
#define CORE_DEBUG(__msg)
Definition: Logs.h:38
#define CORE_INFO(__msg)
Definition: Logs.h:33
#define CORE_ERROR(__msg)
Definition: Logs.h:31