summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Client/Code/Core/Utility/HotKeys.h
blob: 27dc8fb90fe5a503c096d0a2d50436146f5ec60b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//==============================================================================
//	Prefix
//==============================================================================
#ifndef INCLUDED_HOT_KEYS_H
#define INCLUDED_HOT_KEYS_H 1

#pragma once

//==============================================================================
//	Includes
//==============================================================================
#include "Qt3DSString.h"
#include "Qt3DSObjectCounter.h"

#include <QtGlobal>

// New QAction based shortcuts can be added with this macro
#define ADD_GLOBAL_SHORTCUT(actionParent, keyList, handlerFunc) \
{ \
    QAction *action = new QAction(QStringLiteral(#handlerFunc), actionParent); \
    QObject::connect(action, &QAction::triggered, this, &handlerFunc); \
    action->setShortcuts(QList<QKeySequence>() << keyList); \
    actionParent->addAction(action); \
}

class KeyEventFilter;

//=============================================================================
/**
 * Pure-Virtual class for destination of key events.
 */
//=============================================================================
class CHotKeyConsumer
{
public:
    virtual ~CHotKeyConsumer() = default;
    virtual bool OnHotKeyEvent(int inChar, int inRepeatCount,
                               Qt::KeyboardModifiers modifiers) = 0;
};

//=============================================================================
/**
 * Dynamic implementation of CHotKeyConsumer to route events to functions.
 */
//=============================================================================
template <class Fixture>
class CDynHotKeyConsumer : public CHotKeyConsumer
{
    typedef void (Fixture::*TEventMethod)();
    typedef bool (Fixture::*TSmartEventMethod)(int inChar, int inRepeatCount,
                                               Qt::KeyboardModifiers modifiers);
    typedef bool (Fixture::*TBoolEventMethod)();
    typedef int (Fixture::*TIntEventMethod)();

public:
    //==========================================================================
    /**
     * Create a HotKeyConsumer mapping the event to a function with no arguments.
     *
     * @param inObject the object the method is to be called on.
     * @param inEventMethod the method that is to be called.
     */
    //==========================================================================
    CDynHotKeyConsumer(Fixture *inObject, TEventMethod inEventMethod)
    {
        m_Object = inObject;
        m_Method = inEventMethod;
        m_SmartMethod = NULL;
        m_BoolMethod = NULL;
        m_IntMethod = NULL;
    }

    //==========================================================================
    /**
     * Create a HotKeyConsumer mapping the event to a function with normal key args.
     *
     * @param inObject the object the method is to be called on.
     * @param inEventMethod the method that is to be called.
     */
    //==========================================================================
    CDynHotKeyConsumer(Fixture *inObject, TSmartEventMethod inEventMethod)
    {
        m_Object = inObject;
        m_Method = NULL;
        m_SmartMethod = inEventMethod;
        m_BoolMethod = NULL;
        m_IntMethod = NULL;
    }

    CDynHotKeyConsumer(Fixture *inObject, TBoolEventMethod inEventMethod)
    {
        m_Object = inObject;
        m_Method = NULL;
        m_SmartMethod = NULL;
        m_BoolMethod = inEventMethod;
        m_IntMethod = NULL;
    }

    //==========================================================================
    /**
     * Create a HotKeyConsumer mapping the event to a function with normal key args.
     *
     * @param inObject the object the method is to be called on.
     * @param inEventMethod the method that is to be called.
     */
    //==========================================================================
    CDynHotKeyConsumer(Fixture *inObject, TIntEventMethod inEventMethod)
    {
        m_Object = inObject;
        m_Method = NULL;
        m_SmartMethod = NULL;
        m_BoolMethod = NULL;
        m_IntMethod = inEventMethod;
    }

    //==========================================================================
    /**
     * Copy constructor.
     */
    //==========================================================================
    CDynHotKeyConsumer(const CDynHotKeyConsumer &inConsumer)
    {
        m_Object = inConsumer.m_Object;
        m_Method = inConsumer.m_Method;
        m_SmartMethod = inConsumer.m_SmartMethod;
    }

    //==========================================================================
    /**
     * This gets called on a registered key event, and in turn calls the function
     * that this was created with.
     *
     * @see WM_KEYDOWN for argument descriptions.
     */
    //==========================================================================
    bool OnHotKeyEvent(int inChar, int inRepeatCount,
                               Qt::KeyboardModifiers modifiers) override
    {
        bool theRetVal = true;

        // Determine which method to call...
        if (m_Method != NULL)
            (m_Object->*m_Method)();
        else if (m_SmartMethod != NULL)
            theRetVal = (m_Object->*m_SmartMethod)(inChar, inRepeatCount, modifiers);
        else if (m_BoolMethod != NULL)
            theRetVal = (m_Object->*m_BoolMethod)();
        else
            (m_Object->*m_IntMethod)();

        return theRetVal;
    }

protected:
    Fixture *m_Object;
    TEventMethod m_Method;
    TBoolEventMethod m_BoolMethod;
    TIntEventMethod m_IntMethod;
    TSmartEventMethod m_SmartMethod;
};

class CHotKeys
{
public:
    enum Modifier {

        MODIFIER_CONTROL = Qt::ControlModifier,
        MODIFIER_SHIFT = Qt::ShiftModifier,
        MODIFIER_ALT = Qt::AltModifier,

        MOUSE_LBUTTON = 0x100000,
        MOUSE_MBUTTON = 0x200000,
        MOUSE_RBUTTON = 0x400000
    };
    Q_DECLARE_FLAGS(Modifiers, Modifier)

public:
    CHotKeys();
    ~CHotKeys();

    DEFINE_OBJECT_COUNTER(CHotKeys)

    void RegisterKeyEvent(CHotKeyConsumer *inConsumer, Qt::KeyboardModifiers inModifiers,
                          int inCharacter);
    void RegisterKeyDownEvent(CHotKeyConsumer *inConsumer, Qt::KeyboardModifiers inModifiers,
                              int inVirtualKey);
    void RegisterKeyUpEvent(CHotKeyConsumer *inConsumer, Qt::KeyboardModifiers inModifiers,
                            int inVirtualKey);

    void RegisterKeyEvent(CHotKeyConsumer *inConsumer, const Q3DStudio::CString &inString);
    void RegisterKeyDownEvent(CHotKeyConsumer *inConsumer, const Q3DStudio::CString &inString);
    void RegisterKeyUpEvent(CHotKeyConsumer *inConsumer, const Q3DStudio::CString &inString);

    void UnregisterKeyEvent(Qt::KeyboardModifiers inModifiers, int inCharacter);
    void UnregisterKeyDownEvent(Qt::KeyboardModifiers inModifiers, int inVirtualKey);
    void UnregisterKeyUpEvent(Qt::KeyboardModifiers inModifiers, int inVirtualKey);

    void UnregisterKeyEvent(const Q3DStudio::CString &inString);
    void UnregisterKeyDownEvent(const Q3DStudio::CString &inString);
    void UnregisterKeyUpEvent(const Q3DStudio::CString &inString);

    bool OnChar(int inChar, int inRepeatCount, Qt::KeyboardModifiers modifiers);
    bool OnKeyDown(int inChar, int inRepeatCount, Qt::KeyboardModifiers modifiers);
    bool OnKeyUp(int inChar, int inRepeatCount, Qt::KeyboardModifiers modifiers);

    void ClearAllEvents();

    void CaptureHotKeys();
    void ReleaseHotKeys();

    static bool IsKeyDown(Qt::KeyboardModifier inKeyCode);
    static bool isCtrlDown();
    static bool isAltDown();
    static Qt::KeyboardModifiers GetCurrentKeyModifiers();
    static bool isFocusOnControlThatWantsKeys();

protected:
    struct SHotKeyInfo
    {
        CHotKeyConsumer *Consumer;
        int Character;
        Qt::KeyboardModifiers Modifiers;
    };

    typedef std::vector<SHotKeyInfo *> THotKeyList;
    typedef std::map<unsigned int, THotKeyList> THotKeyMap;

    THotKeyMap m_KeyCharMap;
    THotKeyMap m_KeyDownMap;
    THotKeyMap m_KeyUpMap;
    KeyEventFilter *m_EventFilter;
    bool m_CaptureKeys; ///< Boolean that ignores all non-modified keys

    void InsertConsumer(CHotKeyConsumer *inConsumer, Qt::KeyboardModifiers inModifiers,
                        int inCharacter, THotKeyMap *inMap);
    void RemoveConsumers(Qt::KeyboardModifiers inModifiers, int inCharacter, THotKeyMap *inMap);
    bool FireEvent(int inChar, int inRepeatCount, Qt::KeyboardModifiers modifiers,
                   THotKeyMap *inMap);
    Qt::KeyboardModifier GetModifierFromString(const Q3DStudio::CString &inString);
    int GetKeyFromString(Q3DStudio::CString inChar);
    void GetModAndKeyFromString(const Q3DStudio::CString &inString, Qt::KeyboardModifiers &inMod,
                                int &inKey);
};
#endif // INCLUDED_HOT_KEYS_H