MuseScore  3.4
Music composition and notation
fret.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2010-2011 Werner Schweer
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 LICENCE.GPL
11 //=============================================================================
12 
13 #ifndef __FRET_H__
14 #define __FRET_H__
15 
16 #include "element.h"
17 #include "harmony.h"
18 
19 namespace Ms {
20 
21 class StringData;
22 class Chord;
23 
24 // Keep this in order - not used directly for comparisons, but the dots will appear in
25 // this order in fret multidot mode. See fretproperties.cpp.
26 enum class FretDotType : signed char {
27  NORMAL = 0,
28  CROSS,
29  SQUARE,
30  TRIANGLE = 3
31  };
32 
33 
34 enum class FretMarkerType : signed char {
35  NONE,
36  CIRCLE,
37  CROSS
38  };
39 
40 
41 class FretItem {
42  public:
43  struct Barre {
45  int endString;
46 
47  Barre() { startString = endString = -1; }
48  Barre(int s, int e) : startString(s), endString(e) {}
49  bool exists() const { return startString > -1; }
50  };
51 
52  struct Dot {
53  int fret { 0 };
55  int fingering; // NOTE:JT - possible future feature?
56 
57  Dot() {}
58  Dot(int f, FretDotType t = FretDotType::NORMAL) : fret(f), dtype(t) {}
59  bool exists() const { return fret > 0; }
60  };
61 
62  struct Marker {
64 
66  Marker(FretMarkerType t) : mtype(t) {}
67  bool exists() const { return mtype != FretMarkerType::NONE; }
68  };
69 
72  const char* name;
73  };
74 
75  struct DotTypeNameItem {
77  const char* name;
78  };
79 
80  static const std::vector<FretItem::MarkerTypeNameItem> markerTypeNameMap;
81  static const std::vector<FretItem::DotTypeNameItem> dotTypeNameMap;
82 
83  static QChar markerToChar(FretMarkerType t);
84  static QString markerTypeToName(FretMarkerType t);
85  static FretMarkerType nameToMarkerType(QString n);
86  static QString dotTypeToName(FretDotType t);
87  static FretDotType nameToDotType(QString n);
88  };
89 
90 
91 // The three main storage containers used by fret diagrams
92 typedef std::map<int, FretItem::Barre> BarreMap;
93 typedef std::map<int, FretItem::Marker> MarkerMap;
94 typedef std::map<int, std::vector<FretItem::Dot>> DotMap;
95 
96 
97 class FretUndoData {
98  FretDiagram* _diagram { nullptr };
99  BarreMap _barres;
100  MarkerMap _markers;
101  DotMap _dots;
102 
103  public:
106 
107  void updateDiagram();
108  };
109 
110 //---------------------------------------------------------
111 // @@ FretDiagram
113 //
114 // @P userMag qreal
115 // @P strings int number of strings
116 // @P frets int number of frets
117 // @P fretOffset int
118 //
119 // Note that, while strings are zero-indexed, frets are one-indexed
120 //---------------------------------------------------------
121 
122 class FretDiagram final : public Element {
123  int _strings { 6 };
124  int _frets { 4 };
125  int _fretOffset { 0 };
126  int _maxFrets { 24 };
127  bool _showNut { true };
128 
129  // Barres are stored in the format: K: fret, V: barre struct
130  BarreMap _barres;
131 
132  // Dots stored as K: string, V: dot struct
133  DotMap _dots;
134 
135  // Markers stored as K: string, V: marker struct
136  MarkerMap _markers;
137 
138  Harmony* _harmony { 0 };
139 
140  qreal stringLw;
141  qreal nutLw;
142  qreal stringDist;
143  qreal fretDist;
144  QFont font;
145  qreal _userMag { 1.0 }; // allowed 0.1 - 10.0
146  int _numPos;
147 
148  void removeDot(int s, int f = 0);
149  void removeBarre(int f);
150  void removeBarres(int string, int fret = 0);
151  void removeMarker(int s);
152  void removeDotsMarkers(int ss, int es, int fret);
153 
154  public:
155  FretDiagram(Score* s);
156  FretDiagram(const FretDiagram&);
157  ~FretDiagram();
158  virtual void draw(QPainter*) const override;
159  virtual FretDiagram* clone() const override { return new FretDiagram(*this); }
160 
161  Segment* segment() { return toSegment(parent()); }
162 
163  static FretDiagram* fromString(Score* score, const QString &s);
164 
165  virtual ElementType type() const override { return ElementType::FRET_DIAGRAM; }
166  virtual void layout() override;
167  virtual void write(XmlWriter& xml) const override;
168  void writeNew(XmlWriter& xml) const;
169  void writeOld(XmlWriter& xml) const;
170  virtual void read(XmlReader&) override;
171  void readNew(XmlReader&);
172  virtual QLineF dragAnchor() const override;
173  virtual QPointF pagePos() const override;
174 
175  // read / write MusicXML
176  void readMusicXML(XmlReader& de);
177  void writeMusicXML(XmlWriter& xml) const;
178 
179  int strings() const { return _strings; }
180  int frets() const { return _frets; }
181  void setStrings(int n);
182  void setFrets(int n) { _frets = n; }
183 
184  void setDot(int string, int fret, bool add = false, FretDotType dtype = FretDotType::NORMAL);
185  void setBarre(int startString, int endString, int fret);
186  void setBarre(int string, int fret, bool add = false);
187  void setMarker(int string, FretMarkerType marker);
188  /*void setFingering(int string, int finger);*/
189  void clear();
190  void undoSetFretDot(int _string, int _fret, bool _add = false, FretDotType _dtype = FretDotType::NORMAL);
191  void undoSetFretMarker(int _string, FretMarkerType _mtype);
192  void undoSetFretBarre(int _string, int _fret, bool _add = false);
193  void undoFretClear();
194  int fretOffset() const { return _fretOffset; }
195  void setFretOffset(int val) { _fretOffset = val; }
196  int maxFrets() const { return _maxFrets; }
197  void setMaxFrets(int val) { _maxFrets = val; }
198  bool showNut() const { return _showNut; }
199  void setShowNut(bool val) { _showNut = val; }
200 
201  QString harmonyText() const { return _harmony ? _harmony->plainText() : QString(); }
202  qreal centerX() const;
203  void setHarmony(QString harmonyText);
204 
205  std::vector<FretItem::Dot> dot(int s, int f = 0) const;
206  FretItem::Marker marker(int s) const;
207  FretItem::Barre barre(int fret) const;
208 #if 0 // NOTE:JT possible future feature
209  int fingering(int s) const { return _fingering ? _fingering[s] : 0; }
210 #endif
211 
212  BarreMap barres() const { return _barres; }
213  DotMap dots() const { return _dots; }
214  MarkerMap markers() const { return _markers; }
215 
216  Harmony* harmony() const { return _harmony; }
217 
218  void init(Ms::StringData *, Chord*);
219 
220  virtual void add(Element*) override;
221  virtual void remove(Element*) override;
222 
223  virtual bool acceptDrop(EditData&) const override;
224  virtual Element* drop(EditData&) override;
225 
226  void endEditDrag(EditData& editData) override;
227  virtual void scanElements(void* data, void (*func)(void*, Element*), bool all=true) override;
228 
229  virtual QVariant getProperty(Pid propertyId) const override;
230  virtual bool setProperty(Pid propertyId, const QVariant&) override;
231  virtual QVariant propertyDefault(Pid) const override;
232 
233  qreal userMag() const { return _userMag; }
234  void setUserMag(qreal m) { _userMag = m; }
235 
236  friend class FretUndoData;
237  };
238 
239 
240 } // namespace Ms
241 #endif
MarkerMap markers() const
Definition: fret.h:214
bool showNut() const
Definition: fret.h:198
int fretOffset() const
Definition: fret.h:194
Pid
Definition: property.h:62
Fretboard diagram.
Definition: fret.h:122
Definition: xml.h:67
QFont font
Definition: fret.h:144
Barre(int s, int e)
Definition: fret.h:48
int _numPos
Definition: fret.h:146
bool exists() const
Definition: fret.h:59
Definition: fret.h:62
const char * name
Definition: fret.h:77
void setUserMag(qreal m)
Definition: fret.h:234
qreal fretDist
Definition: fret.h:143
int startString
Definition: fret.h:44
Base class of score layout elements.
Definition: element.h:158
Segment * segment()
Definition: fret.h:161
Definition: fret.h:41
const char * name
Definition: fret.h:72
FretDotType dtype
Definition: fret.h:76
FretUndoData()
Definition: fret.h:104
MarkerMap _markers
Definition: fret.h:136
Definition: score.h:391
void setFretOffset(int val)
Definition: fret.h:195
Definition: stringdata.h:35
Barre()
Definition: fret.h:47
FretMarkerType mtype
Definition: fret.h:63
std::map< int, std::vector< FretItem::Dot > > DotMap
Definition: fret.h:94
BarreMap _barres
Definition: fret.h:99
Definition: fret.h:97
qreal userMag() const
Definition: fret.h:233
Harmony * harmony() const
Definition: fret.h:216
BarreMap barres() const
Definition: fret.h:212
int fingering
Definition: fret.h:55
FretMarkerType
Definition: fret.h:34
DotMap dots() const
Definition: fret.h:213
qreal stringLw
Definition: fret.h:140
bool exists() const
Definition: fret.h:49
int strings() const
Definition: fret.h:179
int frets() const
Definition: fret.h:180
void setShowNut(bool val)
Definition: fret.h:199
Definition: segment.h:50
Definition: aeolus.cpp:26
int maxFrets() const
Definition: fret.h:196
Definition: fret.h:75
Definition: fret.h:43
Marker()
Definition: fret.h:65
Definition: xml.h:218
BarreMap _barres
Definition: fret.h:130
std::map< int, FretItem::Barre > BarreMap
Definition: fret.h:92
static const std::vector< FretItem::DotTypeNameItem > dotTypeNameMap
Definition: fret.h:81
void setMaxFrets(int val)
Definition: fret.h:197
Graphic representation of a chord.
Definition: chord.h:55
MarkerMap _markers
Definition: fret.h:100
DotMap _dots
Definition: fret.h:101
Definition: harmony.h:72
void setFrets(int n)
Definition: fret.h:182
Definition: fret.h:52
Definition: element.h:111
bool exists() const
Definition: fret.h:67
std::map< int, FretItem::Marker > MarkerMap
Definition: fret.h:93
virtual ElementType type() const override
Definition: fret.h:165
Dot()
Definition: fret.h:57
DotMap _dots
Definition: fret.h:133
FretMarkerType mtype
Definition: fret.h:71
FretDotType
Definition: fret.h:26
qreal stringDist
Definition: fret.h:142
ElementType
Definition: types.h:34
qreal nutLw
Definition: fret.h:141
QString harmonyText() const
Definition: fret.h:201
virtual FretDiagram * clone() const override
Definition: fret.h:159
Marker(FretMarkerType t)
Definition: fret.h:66
Dot(int f, FretDotType t=FretDotType::NORMAL)
Definition: fret.h:58
int endString
Definition: fret.h:45
static const std::vector< FretItem::MarkerTypeNameItem > markerTypeNameMap
Definition: fret.h:80
Pid propertyId(const QStringRef &s)
Definition: property.cpp:347