/
shortcut.h
197 lines (166 loc) · 7.41 KB
/
shortcut.h
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
//=============================================================================
// MuseScore
// Music Composition & Notation
// $Id:$
//
// Copyright (C) 2011 Werner Schweer and others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENSE.GPL
//=============================================================================
#ifndef __SHORTCUT_H__
#define __SHORTCUT_H__
/*---------------------------------------------------------
NOTE ON ARCHITECTURE
The Shortcut class describes the basic configurable shortcut element.
'Real' data are contained in 2 static member variables:
1) sc[], an array of Shortcut: contains the default, built-in data for each shortcut
except the key sequences; it is initialized at startup (code at the begining of
mscore/actions.cpp)
2) _shortcuts, a QMap using the shortcut xml tag name as hash value: is initialized from
data in sc via a call to Shortcut::init() in program main() (mscore/musescore.cpp).
This also load actual key sequences either from an external, hard-coded, file with
user customizations or from a resource (<= mscore/data/shortcuts.xml), if there are
no customizations.
Later during startup, QAction's are derived from each of its elements and pooled
in a single QActionGroup during MuseScore::MuseScore() costructor (mscore/musescore.cpp)
ShortcutFlags:
To be documented
State flags:
Defined in mscore/global.h (ScoreState enum): each shortcut is ignored if its _flags mask
does not include the current score state. This is different from (and additional to)
QAction processing performed by the Qt framework and happens only after the action has
been forwarded to the application (the action must be enabled).
The STATE_NEVER requires an explanation. It has been introduced to mark shortcuts
which need to be recorded (and possibly customized) but are never used directly.
Currently, this applies to a number of shortcuts which:
- have been split between a common and a TAB-specific variant AND
- are linked to tool bar buttons or menu items
If QAction's are created for both, Qt blocks either as duplicate; in addition, the button
or menu item may become disabled on state change. The currently implemented solution is
to create a QAction only for one of them (the common one) and swap the key sequences when
entering or leaving the relevant state.
Swapping is implemented in MuseScore::changeState() (mscore/musescore.cpp).
QAction creation for the 'other' shortcut is blocked in Shortcut::action() (mscore/shortcut.cpp).
This means that Shortcut::action() may return 0. When scanning the whole
shortcuts[] array, this has to be taken into account; currently it happens in two
cases:
- in MuseScore::MuseScore() constructor (mscore/musescore.cpp)
- in MuseScore::changeState() method (mscore/musescore.cpp)
Shortcuts marked with the STATE_NEVER state should NEVER used directly as shortcuts!
---------------------------------------------------------*/
#include "icons.h"
#include "globals.h"
namespace Ms {
class Xml;
class XmlReader;
enum ShortcutFlags : char {
A_SCORE = 0x1, A_CMD = 0x2
};
static const int KEYSEQ_SIZE = 4;
//---------------------------------------------------------
// Shortcut
// hold the basic values for configurable shortcuts
//---------------------------------------------------------
class Shortcut {
const char* _key { 0 }; //! xml tag name for configuration file
const char* _descr { 0 }; //! descriptor, shown in editor
const char* _text { 0 }; //! text as shown on buttons or menus
const char* _help { 0 }; //! ballon help
int _state { 0 }; //! shortcut is valid in this Mscore state
//! (or'd list of states)
int _flags { 0 };
QList<QKeySequence> _keys; //! shortcut list
QKeySequence::StandardKey _standardKey { QKeySequence::UnknownKey };
Qt::ShortcutContext _context { Qt::WindowShortcut };
Icons _icon { Icons::Invalid_ICON };
mutable QAction* _action { 0 }; //! cached action
MsWidget _assignedWidget; //! the widget where the action will be assigned
static Shortcut sc[];
static QMap<QString, Shortcut*> _shortcuts;
public:
Shortcut() {}
Shortcut(
Ms::MsWidget assignedWidget,
int state, int flags,
const char* name,
Qt::ShortcutContext cont,
const char* d,
const char* txt = 0,
const char* h = 0,
Icons i = Icons::Invalid_ICON);
Shortcut(
Ms::MsWidget assignedWidget,
int state, int flags,
const char* name,
const char* d,
const char* txt = 0,
const char* h = 0,
Icons i = Icons::Invalid_ICON);
Shortcut(
Ms::MsWidget assignedWidget,
int state, int flags,
const char* name,
const char* d,
Icons i);
Shortcut(
Ms::MsWidget assignedWidget,
int state, int flags,
const char* name,
const char* d,
const char* txt,
Icons i);
Shortcut(
Ms::MsWidget assignedWidget,
int state, int flags,
const char* name,
Qt::ShortcutContext cont,
const char* d,
Icons i);
Shortcut(
Ms::MsWidget assignedWidget,
int state, int flags,
const char* name,
Qt::ShortcutContext cont,
const char* d,
const char* txt,
Icons i);
Shortcut(const Shortcut& c);
~Shortcut() {}
QAction* action() const;
const char* key() const { return _key; }
QString descr() const;
QString text() const;
QString help() const;
MsWidget assignedWidget() const { return _assignedWidget; }
void clear(); //! remove shortcuts
void reset(); //! reset to buildin
void addShortcut(const QKeySequence&);
int state() const { return _state; }
int flags() const { return _flags; }
Icons icon() const { return _icon; }
const QList<QKeySequence>& keys() const { return _keys; }
QKeySequence::StandardKey standardKey() const { return _standardKey; }
void setKeys(const QList<QKeySequence>& ks);
void setStandardKey(QKeySequence::StandardKey k) { _standardKey = k; }
bool compareKeys(const Shortcut&) const;
QString keysToString() const;
static QString getMenuShortcutString(const QMenu* menu);
void write(Ms::Xml&) const;
void read(Ms::XmlReader&);
static void init();
static void load();
static void save();
static void resetToDefault();
static bool dirty;
static Shortcut* getShortcut(const char* key);
static const QMap<QString, Shortcut*>& shortcuts() { return _shortcuts; }
static QActionGroup* getActionGroupForWidget(MsWidget w);
static QActionGroup* getActionGroupForWidget(MsWidget w, Qt::ShortcutContext newShortcutContext);
static QString keySeqToString(const QKeySequence& keySeq, QKeySequence::SequenceFormat fmt);
static QKeySequence keySeqFromString(const QString& str, QKeySequence::SequenceFormat fmt);
};
} // namespace Ms
#endif