forked from RaptDept/ptparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeysignature.cpp
More file actions
279 lines (247 loc) · 8.29 KB
/
keysignature.cpp
File metadata and controls
279 lines (247 loc) · 8.29 KB
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
274
275
276
277
278
279
/////////////////////////////////////////////////////////////////////////////
// Name: keysignature.cpp
// Purpose: Stores and renders a key signature
// Author: Brad Larsen
// Modified by:
// Created: Dec 10, 2004
// RCS-ID:
// Copyright: (c) Brad Larsen
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
#include "stdwx.h"
#include "keysignature.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Default constants
const wxByte KeySignature::DEFAULT_DATA = 0;
/// Default Constructor
KeySignature::KeySignature() :
m_data(DEFAULT_DATA)
{
//------Last Checked------//
// - Dec 10, 2004
}
/// Primary Constructor
/// @param keyType major or minor (see keyTypes enum in .h for values)
/// @param keyAccidentals type and number of accidentals (4 sharps, 2 flats,
/// etc.; see keyAccidentals enum in .h for values)
KeySignature::KeySignature(wxByte keyType, wxByte keyAccidentals) :
m_data(DEFAULT_DATA)
{
//------Last Checked------//
// - Dec 10, 2004
SetKey(keyType, keyAccidentals);
}
/// Copy Constructor
KeySignature::KeySignature(const KeySignature& keySignature) :
m_data(DEFAULT_DATA)
{
//------Last Checked------//
// - Dec 10, 2004
*this = keySignature;
}
/// Destructor
KeySignature::~KeySignature()
{
//------Last Checked------//
// - Dec 10, 2004
}
/// Assignment Operator
const KeySignature& KeySignature::operator=(const KeySignature& keySignature)
{
//------Last Checked------//
// - Dec 10, 2004
// Check for assignment to self
if (this != &keySignature)
m_data = keySignature.m_data;
return (*this);
}
/// Equality operator
bool KeySignature::operator==(const KeySignature& keySignature) const
{
//------Last Checked------//
// - Dec 10, 2004
return (m_data == keySignature.m_data);
}
/// Inequality operator
bool KeySignature::operator!=(const KeySignature& keySignature) const
{
//------Last Checked------//
// - Dec 10, 2004
return (!operator==(keySignature));
}
// Serialize Functions
/// Performs serialization for the class
/// @param stream Power Tab output stream to serialize to
/// @return True if the object was serialized, false if not
bool KeySignature::DoSerialize(PowerTabOutputStream& stream)
{
//------Last Checked------//
// - Dec 10, 2004
stream << m_data;
wxCHECK(stream.CheckState(), false);
return (stream.CheckState());
}
/// Performs deserialization for the class
/// @param stream Power Tab input stream to load from
/// @param version File version
/// @return True if the object was deserialized, false if not
bool KeySignature::DoDeserialize(PowerTabInputStream& stream,
wxWord WXUNUSED(version))
{
//------Last Checked------//
// - Apr 22, 2007
stream >> m_data;
wxCHECK(stream.CheckState(), false);
return (stream.CheckState());
}
// Key Functions
/// Sets the key type and accidentals
/// @param keyType major or minor (see keyTypes enum in .h for values)
/// @param keyAccidentals type and number of accidentals (4 sharps, 2 flats,
/// etc.; see keyAccidentals enum in .h for values)
bool KeySignature::SetKey(wxByte keyType, wxByte keyAccidentals)
{
//------Last Checked------//
// - Dec 11, 2004
if (!SetKeyType(keyType))
return (false);
if (!SetKeyAccidentals(keyAccidentals))
return (false);
return (true);
}
/// Gets the key type and accidentals
/// @param keyType major or minor (see keyTypes enum in .h for values)
/// @param keyAccidentals type and number of accidentals (4 sharps, 2 flats,
/// etc.; see keyAccidentals enum in .h for values)
void KeySignature::GetKey(wxByte& keyType, wxByte& keyAccidentals) const
{
//------Last Checked------//
// - Dec 11, 2004
keyType = GetKeyType();
keyAccidentals = GetKeyAccidentals();
// Cancellations store the key of the cancelled type, but we know that the
// cancellations will always
// occur on C Major/A Minor so set the key to 0
if (IsCancellation())
keyAccidentals = 0;
}
/// Gets the key type and accidentals required to draw the key signature
/// @param keyType major or minor (see keyTypes enum in .h for values)
/// @param keyAccidentals Type and number of accidentals (4 sharps, 2 flats,
/// etc.; see keyAccidentals enum in .h for values)
/// @return True if the key is a cancellation, false if not
bool KeySignature::GetDrawKey(wxByte& keyType, wxByte& keyAccidentals) const
{
//------Last Checked------//
// - Dec 11, 2004
keyType = GetKeyType();
keyAccidentals = (wxByte)(m_data & keyAccidentalsMask);
return (IsCancellation());
}
// Key Type Functions
/// Sets the key type
/// @param keyType Key type to set (see keyType enum in .h for values)
/// @return True if the key type was set, false if not
bool KeySignature::SetKeyType(wxByte keyType)
{
//------Last Checked------//
// - Dec 11, 2004
wxCHECK(IsValidKeyType(keyType), false);
m_data &= ~keyTypeMask;
m_data |= (keyType << 6);
return (true);
}
// Key Accidentals Functions
/// Sets the type and number of accidentals in the key signature (4 sharps,
/// 2 flats, etc.)
/// @param keyAccidentals Type and number of accidentals to set
/// @return True if the key accidentals was set, false if not
bool KeySignature::SetKeyAccidentals(wxByte keyAccidentals)
{
//------Last Checked------//
// - Dec 11, 2004
wxCHECK(IsValidKeyAccidentals(keyAccidentals), false);
m_data &= ~keyAccidentalsMask;
m_data |= keyAccidentals;
return (true);
}
/// Gets the type and number of accidentals in the key signature (4 sharps,
/// 2 flats, etc.)
/// @return Type and number of accidentals in the key signature (see
/// keyAccidentals enum in .h for values)
wxByte KeySignature::GetKeyAccidentals() const
{
//------Last Checked------//
// - Dec 10, 2004
wxByte returnValue = (wxByte)(m_data & keyAccidentalsMask);
// Cancellations store the key of the cancelled type, but we know that the
// cancellations will always
// occur on C Major/A Minor so set the key to 0
if (IsCancellation())
returnValue = 0;
return (returnValue);
}
// Operations
/// Gets a text representation of the key signature
/// @return A text representation of the key signature
wxString KeySignature::GetText() const
{
//------Last Checked------//
// - Apr 25, 2006
wxString returnValue;
if (HasOneSharp())
returnValue = "#";
else if (HasTwoSharps())
returnValue = "##";
else if (HasThreeSharps())
returnValue = "###";
else if (HasFourSharps())
returnValue = "####";
else if (HasFiveSharps())
returnValue = "#####";
else if (HasSixSharps())
returnValue = "######";
else if (HasSevenSharps())
returnValue = "#######";
else if (HasOneFlat())
returnValue = "b";
else if (HasTwoFlats())
returnValue = "bb";
else if (HasThreeFlats())
returnValue = "bbb";
else if (HasFourFlats())
returnValue = "bbbb";
else if (HasFiveFlats())
returnValue = "bbbbb";
else if (HasSixFlats())
returnValue = "bbbbbb";
else if (HasSevenFlats())
returnValue = "bbbbbbb";
if (IsMinorKey())
returnValue += "m";
return (returnValue);
}
/// Gets the width of the key signature, in drawing units (100ths of an inch)
/// @return The width of the key signature
int KeySignature::GetWidth() const
{
//------Last Checked------//
// - Aug 30, 2007
int returnValue = 0;
// Key signature must be shown to have width
if (IsShown())
{
// Get the draw key
wxByte keyType = 0;
wxByte keyAccidentals = 0;
GetDrawKey(keyType, keyAccidentals);
// Determine the number of accidentals, be it sharps or flats
int accidentals = ((keyAccidentals <= sevenSharps) ? keyAccidentals :
(keyAccidentals - 7));
// There are 6 drawing units per accidental
returnValue = accidentals * 6;
}
return (returnValue);
}