MuseScore  3.4
Music composition and notation
shortcut.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2011-2016 Werner Schweer and others
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENSE.GPL
11 //=============================================================================
12 
13 #ifndef __SHORTCUT_H__
14 #define __SHORTCUT_H__
15 
16 /*---------------------------------------------------------
17 NOTE ON ARCHITECTURE
18 
19 The Shortcut class describes the basic configurable shortcut element.
20 'Real' data are contained in 2 static member variables:
21 
22 1) sc[], an array of Shortcut: contains the default, built-in data for each shortcut
23  except the key sequences; it is initialized at startup (code at the beginning of
24  mscore/actions.cpp)
25 2) _shortcuts, a QMap using the shortcut xml tag name as hash value: is initialized from
26  data in sc via a call to Shortcut::init() in program main() (mscore/musescore.cpp).
27  This also load actual key sequences either from an external, hard-coded, file with
28  user customizations or from a resource (<= mscore/data/shortcuts.xml), if there are
29  no customizations.
30  Later during startup, QAction's are derived from each of its elements and pooled
31  in a single QActionGroup during MuseScore::MuseScore() constructor (mscore/musescore.cpp)
32 
33 ShortcutFlags:
34  To be documented
35 
36 State flags:
37 
38 Defined in mscore/global.h (ScoreState enum): each shortcut is ignored if its _flags mask
39 does not include the current score state. This is different from (and additional to)
40 QAction processing performed by the Qt framework and happens only after the action has
41 been forwarded to the application (the action must be enabled).
42 
43 The STATE_NEVER requires an explanation. It has been introduced to mark shortcuts
44 which need to be recorded (and possibly customized) but are never used directly.
45 Currently, this applies to a number of shortcuts which:
46 - have been split between a common and a TAB-specific variant AND
47 - are linked to tool bar buttons or menu items
48 If QAction's are created for both, Qt blocks either as duplicate; in addition, the button
49 or menu item may become disabled on state change. The currently implemented solution is
50 to create a QAction only for one of them (the common one) and swap the key sequences when
51 entering or leaving the relevant state.
52 Swapping is implemented in MuseScore::changeState() (mscore/musescore.cpp).
53 QAction creation for the 'other' shortcut is blocked in Shortcut::action() (mscore/shortcut.cpp).
54 
55 This means that Shortcut::action() may return 0. When scanning the whole
56 shortcuts[] array, this has to be taken into account; currently it happens in two
57 cases:
58 - in MuseScore::MuseScore() constructor (mscore/musescore.cpp)
59 - in MuseScore::changeState() method (mscore/musescore.cpp)
60 
61 Shortcuts marked with the STATE_NEVER state should NEVER used directly as shortcuts!
62 ---------------------------------------------------------*/
63 
64 #include "icons.h"
65 #include "globals.h"
66 namespace Ms {
67 
68 class XmlWriter;
69 class XmlReader;
70 
71 //---------------------------------------------------------
72 // ShortcutFlags
73 //---------------------------------------------------------
74 
75 enum class ShortcutFlags : char {
76  NONE = 0,
77  A_SCORE = 1,
78  A_CMD = 1 << 1,
79  A_CHECKABLE = 1 << 2,
80  A_CHECKED = 1 << 3
81  };
82 
84  return static_cast<ShortcutFlags>(static_cast<int>(t1) | static_cast<int>(t2));
85  }
86 
87 constexpr bool operator& (ShortcutFlags t1, ShortcutFlags t2) {
88  return static_cast<int>(t1) & static_cast<int>(t2);
89  }
90 
91 static const int KEYSEQ_SIZE = 4;
92 
93 //---------------------------------------------------------
94 // Shortcut
95 // hold the basic values for configurable shortcuts
96 //---------------------------------------------------------
97 
98 class Shortcut {
100  int _state { 0 };
101  QByteArray _key;
102  QByteArray _descr;
103  QByteArray _text;
104  QByteArray _help;
105 
108  Qt::ShortcutContext _context { Qt::WindowShortcut };
110 
111  QList<QKeySequence> _keys;
112 
113  QKeySequence::StandardKey _standardKey { QKeySequence::UnknownKey };
114  mutable QAction* _action { 0 };
115 
116  static QString source;
117 
118  static Shortcut _sc[];
119  static QHash<QByteArray, Shortcut*> _shortcuts;
120  void translateAction(QAction* action) const;
121 
122  public:
123 
124  static constexpr const char* defaultFileName = ":/data/shortcuts.xml";
125 
126  Shortcut() {}
127  Shortcut(
128  Ms::MsWidget assignedWidget,
129  int state,
130  const char* key,
131  const char* d = 0,
132  const char* txt = 0,
133  const char* h = 0,
135  Qt::ShortcutContext cont = Qt::WindowShortcut,
137  );
138 
139  QAction* action() const;
140  const QByteArray& key() const { return _key; }
141  void setKey(const QByteArray& key) { _key = key; }
142  QString descr() const;
143  QString text() const;
144  QString help() const;
145  MsWidget assignedWidget() const { return _assignedWidget; }
146  void clear();
147  void reset();
148  void addShortcut(const QKeySequence&);
149  int state() const { return _state; }
150  void setState(int v) { _state = v; }
151  bool needsScore() const { return _flags & ShortcutFlags::A_SCORE; }
152  bool isCmd() const { return _flags & ShortcutFlags::A_CMD; }
153  bool isCheckable() const { return _flags & ShortcutFlags::A_CHECKABLE; }
154  bool isChecked() const { return _flags & ShortcutFlags::A_CHECKED; }
155  Icons icon() const { return _icon; }
156  const QList<QKeySequence>& keys() const { return _keys; }
157  QKeySequence::StandardKey standardKey() const { return _standardKey; }
158  void setStandardKey(QKeySequence::StandardKey k);
159  void setKeys(const QList<QKeySequence>& ks);
160  void setKeys(const Shortcut&);
161 
162  bool compareKeys(const Shortcut&) const;
163  QString keysToString() const;
164  static QString getMenuShortcutString(const QMenu* menu);
165 
166  void write(Ms::XmlWriter&) const;
167  void read(Ms::XmlReader&);
168 
169  static void init();
170  static void retranslate();
171  static void refreshIcons();
172  static void load();
173  static void loadFromNewFile(QString fileLocation);
174  static void save();
175  static void saveToNewFile(QString fileLocation);
176  static void resetToDefault();
177  static bool dirty;
178  static bool customSource() { return source != defaultFileName; }
179  static Shortcut* getShortcutByKeySequence(const QKeySequence &keySequence, const ScoreState state);
180  static Shortcut* getShortcut(const char* key);
181  static const QHash<QByteArray, Shortcut*>& shortcuts() { return _shortcuts; }
182  static QActionGroup* getActionGroupForWidget(MsWidget w);
183  static QActionGroup* getActionGroupForWidget(MsWidget w, Qt::ShortcutContext newShortcutContext);
184 
185  static QString keySeqToString(const QKeySequence& keySeq, QKeySequence::SequenceFormat fmt, bool escapeKeyStr = false);
186  static QKeySequence keySeqFromString(const QString& str, QKeySequence::SequenceFormat fmt);
187  };
188 
189 } // namespace Ms
190 #endif
191 
static bool dirty
Definition: shortcut.h:177
static const QHash< QByteArray, Shortcut * > & shortcuts()
Definition: shortcut.h:181
Definition: xml.h:67
MsWidget
Definition: globals.h:43
QList< QKeySequence > _keys
Definition: shortcut.h:111
constexpr ArticulationShowIn operator|(ArticulationShowIn a1, ArticulationShowIn a2)
Definition: articulation.h:45
const QList< QKeySequence > & keys() const
Definition: shortcut.h:156
ScoreState
Definition: globals.h:53
constexpr bool operator &(ArticulationShowIn a1, ArticulationShowIn a2)
Definition: articulation.h:48
bool needsScore() const
Definition: shortcut.h:151
MsWidget _assignedWidget
Definition: shortcut.h:99
bool isCmd() const
Definition: shortcut.h:152
QByteArray _descr
xml tag name for configuration file
Definition: shortcut.h:102
bool isCheckable() const
Definition: shortcut.h:153
Shortcut()
Definition: shortcut.h:126
MsWidget assignedWidget() const
Definition: shortcut.h:145
void setState(int v)
Definition: shortcut.h:150
const QByteArray & key() const
Definition: shortcut.h:140
QByteArray _help
text as shown on buttons or menus
Definition: shortcut.h:104
Definition: aeolus.cpp:26
void setKey(const QByteArray &key)
Definition: shortcut.h:141
Definition: xml.h:218
Icons
Definition: icons.h:27
QByteArray _key
shortcut is valid in this Mscore state
Definition: shortcut.h:101
Definition: xmlwriter.h:26
QKeySequence::StandardKey standardKey() const
Definition: shortcut.h:157
ShortcutFlags
Definition: shortcut.h:75
bool isChecked() const
Definition: shortcut.h:154
QByteArray _text
descriptor, shown in editor
Definition: shortcut.h:103
Icons icon() const
Definition: shortcut.h:155
Definition: xmlreader.h:28
static QHash< QByteArray, Shortcut * > _shortcuts
Definition: shortcut.h:119
static bool customSource()
Definition: shortcut.h:178
static QString source
cached action
Definition: shortcut.h:116
int state() const
Definition: shortcut.h:149
Definition: shortcut.h:98