MeshLib C++ Docs
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{
13
14
16{
17 int key{ 0 };
18 int mod{ 0 };
19
20 bool operator<( const ShortcutKey& other ) const
21 {
22 if ( key < other.key )
23 return true;
24 if ( key == other.key )
25 return mod < other.mod;
26 return false;
27 }
28};
29
40
46class MRVIEWER_CLASS ShortcutManager : public MultiListener<KeyDownListener, KeyRepeatListener>
47{
48public:
49 virtual ~ShortcutManager() = default;
50
53
55 {
57 std::string name;
58 std::function<void()> action;
59 bool repeatable = true;
60 };
61
62 inline static const std::string categoryNames[6] = { "Info", "Edit", "View", "Scene", "Objects", "Selection " };
63
67 MRVIEWER_API virtual void setShortcut( const ShortcutKey& key, const ShortcutCommand& command );
68
69 using ShortcutList = std::vector<std::tuple<ShortcutKey, Category, std::string>>;
70
73 MRVIEWER_API const ShortcutList& getShortcutList() const;
74
75 enum class Reason
76 {
79 };
80
82 bool isEnabled() const { return enabled_; }
83 void enable( bool on ) { enabled_ = on; }
84
86 MRVIEWER_API virtual bool processShortcut( const ShortcutKey& key, Reason = Reason::KeyDown ) const;
87
88 MRVIEWER_API bool onKeyDown_( int key, int modifier ) override;
89 MRVIEWER_API bool onKeyRepeat_( int key, int modifier ) override;
90
92 MRVIEWER_API static const char* getModifierString( int mod );
94 MRVIEWER_API static std::string getKeyString( int key );
96 MRVIEWER_API static std::string getKeyFullString( const ShortcutKey& key, bool respectKey = true );
97
99 MRVIEWER_API std::optional<ShortcutKey> findShortcutByName( const std::string& name ) const;
100
102 MRVIEWER_API void clear();
103protected:
106 MRVIEWER_API static int mapKeyFromKeyAndMod( const ShortcutKey& key, bool respectKeyboard );
108 static ShortcutKey kayAndModFromMapKey( int mapKey ) { return { mapKey >> 6, mapKey % ( 1 << 6 ) }; }
109
112
113 bool enabled_{ true };
114
117
118 mutable std::optional<ShortcutList> listCache_;
119};
120
121}
Definition MRShortcutManager.h:47
static ShortcutKey kayAndModFromMapKey(int mapKey)
returns key with modifier (alt, ctrl, shift, etc.) from simple map key
Definition MRShortcutManager.h:108
HashMap< std::string, int > ShourtcutsBackMap
Definition MRShortcutManager.h:111
static const char * getModifierString(int mod)
make string from strictly one modifier
std::optional< ShortcutKey > findShortcutByName(const std::string &name) const
if action with given name is present in shortcut list - returns it
Reason
Definition MRShortcutManager.h:76
const ShortcutList & getShortcutList() const
void clear()
clear all saved shortcuts
static std::string getKeyString(int key)
make string from a key without modifiers, for arrow characters it uses icons font
static const std::string categoryNames[6]
Definition MRShortcutManager.h:62
virtual void setShortcut(const ShortcutKey &key, const ShortcutCommand &command)
int key
Definition MRShortcutManager.h:17
void enable(bool on)
Definition MRShortcutManager.h:83
virtual bool processShortcut(const ShortcutKey &key, Reason=Reason::KeyDown) const
if given key has action in shortcut map - process it and returns true, otherwise returns false;
std::function< void()> action
name of action
Definition MRShortcutManager.h:58
std::string name
Definition MRShortcutManager.h:57
static int mapKeyFromKeyAndMod(const ShortcutKey &key, bool respectKeyboard)
std::vector< std::tuple< ShortcutKey, Category, std::string > > ShortcutList
Definition MRShortcutManager.h:69
static std::string getKeyFullString(const ShortcutKey &key, bool respectKey=true)
make string from all modifiers and with/without key and returns it
bool onKeyDown_(int key, int modifier) override
MR::ShortcutKey ShortcutKey
Definition MRShortcutManager.h:51
HashMap< int, ShortcutCommand > ShourtcutsMap
Definition MRShortcutManager.h:110
bool onKeyRepeat_(int key, int modifier) override
ShourtcutsBackMap backMap_
Definition MRShortcutManager.h:116
ShourtcutsMap map_
Definition MRShortcutManager.h:115
bool isEnabled() const
processShortcut does nothing if not enabled
Definition MRShortcutManager.h:82
ShortcutCategory
Definition MRShortcutManager.h:31
bool operator<(const ShortcutKey &other) const
Definition MRShortcutManager.h:20
virtual ~ShortcutManager()=default
Category category
Definition MRShortcutManager.h:56
MR::ShortcutCategory Category
Definition MRShortcutManager.h:52
bool repeatable
Definition MRShortcutManager.h:59
std::optional< ShortcutList > listCache_
Definition MRShortcutManager.h:118
int mod
Definition MRShortcutManager.h:18
bool enabled_
Definition MRShortcutManager.h:113
phmap::flat_hash_map< K, V, Hash, Eq > HashMap
Definition MRMeshFwd.h:606
@ KeyRepeat
the user just pressed the keys
Definition MRShortcutManager.h:78
@ KeyDown
Definition MRShortcutManager.h:77
@ Count
Definition MRPdf.h:42
@ other
Angle, normally float. Measure in radians.
Definition MRFeatureObject.h:27
@ Info
Definition MRNotificationType.h:13
@ View
Definition MRShortcutManager.h:34
@ Selection
Definition MRShortcutManager.h:37
@ Edit
Definition MRShortcutManager.h:33
@ Scene
Definition MRShortcutManager.h:35
@ Objects
Definition MRShortcutManager.h:36
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRViewerEventsListener.h:32
Definition MRShortcutManager.h:16
Definition MRShortcutManager.h:55