MeshLib
 
Loading...
Searching...
No Matches
MRShortcutManager.h
Go to the documentation of this file.
1#pragma once
2#include "MRViewerFwd.h"
3#include "MRMesh/MRphmap.h"
5#include <string>
6#include <functional>
7#include <optional>
8
9namespace MR
10{
11
13{
14 int key{ 0 };
15 int mod{ 0 };
16
17 bool operator<( const ShortcutKey& other ) const
18 {
19 if ( key < other.key )
20 return true;
21 if ( key == other.key )
22 return mod < other.mod;
23 return false;
24 }
25};
26
27enum class ShortcutCategory : char
28{
29 Info,
30 Edit,
31 View,
32 Scene,
33 Objects,
35 Count
36};
37
38// this class stores two maps:
39// 1) shortcut to action
40// 2) action name to shortcut
41// it can be used to process, customize and print shortcuts
42// indifferent to literals register
43class MRVIEWER_CLASS ShortcutManager : public MultiListener<KeyDownListener, KeyRepeatListener>
44{
45public:
46 virtual ~ShortcutManager() = default;
47
50
52 {
54 std::string name; // name of action
55 std::function<void()> action;
56 bool repeatable = true; // shortcut shall be applied many times while the user holds the keys down
57 };
58
59 inline static const std::string categoryNames[6] = { "Info", "Edit", "View", "Scene", "Objects", "Selection " };
60
61 // set shortcut
62 // note: one action can have only one shortcut, one shortcut can have only one action
63 // if action already has other shortcut, other one will be removed
64 MRVIEWER_API virtual void setShortcut( const ShortcutKey& key, const ShortcutCommand& command );
65
66 using ShortcutList = std::vector<std::tuple<ShortcutKey, Category, std::string>>;
67
68 // returns cached list of sorted shortcuts (sorting by key)
69 // if this structure was changed since last call of this function - updates cache
70 MRVIEWER_API const ShortcutList& getShortcutList() const;
71
72 enum class Reason
73 {
74 KeyDown, // the user just pressed the keys
75 KeyRepeat // the user holds the keys for a long time
76 };
77
78 // processShortcut does nothing if not enabled
79 bool isEnabled() const { return enabled_; }
80 void enable( bool on ) { enabled_ = on; }
81
82 // if given key has action in shortcut map - process it and returns true, otherwise returns false;
83 MRVIEWER_API virtual bool processShortcut( const ShortcutKey& key, Reason = Reason::KeyDown ) const;
84
85 MRVIEWER_API bool onKeyDown_( int key, int modifier ) override;
86 MRVIEWER_API bool onKeyRepeat_( int key, int modifier ) override;
87
88 //make string from strictly one modifier
89 MRVIEWER_API static std::string getModifierString( int mod );
90 //make string from a key without modifiers, for arrow characters it uses icons font
91 MRVIEWER_API static std::string getKeyString( int key );
92 // make string from all modifiers and with/without key and returns it
93 MRVIEWER_API static std::string getKeyFullString( const ShortcutKey& key, bool respectKey = true );
94
95 // if action with given name is present in shortcut list - returns it
96 MRVIEWER_API std::optional<ShortcutKey> findShortcutByName( const std::string& name ) const;
97protected:
98 // returns simple map key from key with modifier (alt, ctrl, shift, etc.)
99 // if respectKeyboard is set, key will be mapped using local keyboard settings (only if it is mapped to latin symbol)
100 MRVIEWER_API static int mapKeyFromKeyAndMod( const ShortcutKey& key, bool respectKeyboard );
101 // returns key with modifier (alt, ctrl, shift, etc.) from simple map key
102 static ShortcutKey kayAndModFromMapKey( int mapKey ) { return { mapKey >> 6, mapKey % ( 1 << 6 ) }; }
103
106
107 bool enabled_{ true };
108
111
112 mutable std::optional<ShortcutList> listCache_;
113};
114
115}
Definition MRShortcutManager.h:44
static ShortcutKey kayAndModFromMapKey(int mapKey)
Definition MRShortcutManager.h:102
MRVIEWER_API const ShortcutList & getShortcutList() const
HashMap< std::string, int > ShourtcutsBackMap
Definition MRShortcutManager.h:105
Reason
Definition MRShortcutManager.h:73
static MRVIEWER_API std::string getKeyString(int key)
MRVIEWER_API bool onKeyDown_(int key, int modifier) override
virtual MRVIEWER_API bool processShortcut(const ShortcutKey &key, Reason=Reason::KeyDown) const
void enable(bool on)
Definition MRShortcutManager.h:80
virtual MRVIEWER_API void setShortcut(const ShortcutKey &key, const ShortcutCommand &command)
std::vector< std::tuple< ShortcutKey, Category, std::string > > ShortcutList
Definition MRShortcutManager.h:66
HashMap< int, ShortcutCommand > ShourtcutsMap
Definition MRShortcutManager.h:104
ShourtcutsBackMap backMap_
Definition MRShortcutManager.h:110
ShourtcutsMap map_
Definition MRShortcutManager.h:109
bool isEnabled() const
Definition MRShortcutManager.h:79
virtual ~ShortcutManager()=default
static MRVIEWER_API int mapKeyFromKeyAndMod(const ShortcutKey &key, bool respectKeyboard)
static MRVIEWER_API std::string getModifierString(int mod)
static MRVIEWER_API std::string getKeyFullString(const ShortcutKey &key, bool respectKey=true)
MRVIEWER_API std::optional< ShortcutKey > findShortcutByName(const std::string &name) const
std::optional< ShortcutList > listCache_
Definition MRShortcutManager.h:112
MRVIEWER_API bool onKeyRepeat_(int key, int modifier) override
Definition MRCameraOrientationPlugin.h:7
ShortcutCategory
Definition MRShortcutManager.h:28
phmap::flat_hash_map< K, V, Hash, Eq > HashMap
Definition MRMesh/MRMeshFwd.h:450
Definition MRViewerEventsListener.h:29
Definition MRShortcutManager.h:13
int key
Definition MRShortcutManager.h:14
bool operator<(const ShortcutKey &other) const
Definition MRShortcutManager.h:17
int mod
Definition MRShortcutManager.h:15
Definition MRShortcutManager.h:52
std::function< void()> action
Definition MRShortcutManager.h:55
std::string name
Definition MRShortcutManager.h:54
Category category
Definition MRShortcutManager.h:53