]>
code.delx.au - virtualtones/blob - pianoinstrument.cpp
1 // pianoinstrument.cpp - A piano simulator
2 // Written by James Bunton <james@delx.cjb.net>
3 // Licensed under the GPL, see COPYING.txt for more details
6 #include "pianoinstrument.h"
11 PianoInstrument::PianoInstrument(QWidget
*parent
)
14 // Set us up to look pretty
15 setPaletteBackgroundPixmap(QPixmap("piano.png"));
16 setFixedSize(184, 220);
17 parentWidget()->setFixedSize(184, 220);
19 for(int i
= 0; i
< 26; i
++) {
29 PianoInstrument::~PianoInstrument()
34 QString
PianoInstrument::generateHelp()
41 "Playing the keyboard:"
43 "<li>You can change the octave using the "Octave" box above. Middle C is the third octave</li>"
44 "<li>The keys - qwertyui are the top row white keys</li>"
45 "<li>The keys - 23 567 9 are the top row black keys</li>"
46 "<li>The keys - zxcvbnm, are the bottom row white keys</li>"
47 "<li>The keys - sd ghj l are the bottom row black keys</li>"
56 void PianoInstrument::paintEvent(QPaintEvent
*)
59 paint
.setPen(Qt::red
);
61 const int topBlackY
= 38;
62 const int topWhiteY
= 70;
63 const int botBlackY
= 38 + 110;
64 const int botWhiteY
= 70 + 110;
68 if(notes
[0] == true) {
69 paint
.drawEllipse(6, topWhiteY
, w
, h
);
71 if(notes
[1] == true) {
72 paint
.drawEllipse(15, topBlackY
, w
, h
);
74 if(notes
[2] == true) {
75 paint
.drawEllipse(29, topWhiteY
, w
, h
);
77 if(notes
[3] == true) {
78 paint
.drawEllipse(45, topBlackY
, w
, h
);
80 if(notes
[4] == true) {
81 paint
.drawEllipse(52, topWhiteY
, w
, h
);
83 if(notes
[5] == true) {
84 paint
.drawEllipse(75, topWhiteY
, w
, h
);
86 if(notes
[6] == true) {
87 paint
.drawEllipse(85, topBlackY
, w
, h
);
89 if(notes
[7] == true) {
90 paint
.drawEllipse(97, topWhiteY
, w
, h
);
92 if(notes
[8] == true) {
93 paint
.drawEllipse(113, topBlackY
, w
, h
);
95 if(notes
[9] == true) {
96 paint
.drawEllipse(120, topWhiteY
, w
, h
);
98 if(notes
[10] == true) {
99 paint
.drawEllipse(136, topBlackY
, w
, h
);
101 if(notes
[11] == true) {
102 paint
.drawEllipse(143, topWhiteY
, w
, h
);
104 if(notes
[12] == true) {
105 paint
.drawEllipse(166, topWhiteY
, w
, h
);
106 paint
.drawEllipse(6, botWhiteY
, w
, h
);
108 if(notes
[13] == true) {
109 paint
.drawEllipse(176, topBlackY
, w
, h
);
110 paint
.drawEllipse(15, botBlackY
, w
, h
);
112 if(notes
[14] == true) {
113 paint
.drawEllipse(29, botWhiteY
, w
, h
);
115 if(notes
[15] == true) {
116 paint
.drawEllipse(45, botBlackY
, w
, h
);
118 if(notes
[16] == true) {
119 paint
.drawEllipse(52, botWhiteY
, w
, h
);
121 if(notes
[17] == true) {
122 paint
.drawEllipse(75, botWhiteY
, w
, h
);
124 if(notes
[18] == true) {
125 paint
.drawEllipse(85, botBlackY
, w
, h
);
127 if(notes
[19] == true) {
128 paint
.drawEllipse(97, botWhiteY
, w
, h
);
130 if(notes
[20] == true) {
131 paint
.drawEllipse(113, botBlackY
, w
, h
);
133 if(notes
[21] == true) {
134 paint
.drawEllipse(120, botWhiteY
, w
, h
);
136 if(notes
[22] == true) {
137 paint
.drawEllipse(136, botBlackY
, w
, h
);
139 if(notes
[23] == true) {
140 paint
.drawEllipse(143, botWhiteY
, w
, h
);
142 if(notes
[24] == true) {
143 paint
.drawEllipse(166, botWhiteY
, w
, h
);
145 if(notes
[25] == true) {
146 paint
.drawEllipse(176, botBlackY
, w
, h
);
152 void PianoInstrument::keyPressEvent(QKeyEvent
*e
)
154 if(e
->isAutoRepeat() == true) {
159 // Make a copy of the old notes so we know what's changed
160 copyArray(notes
, oldNotes
);
164 /* First row of keys */
208 /* Second row of keys */
259 void PianoInstrument::keyReleaseEvent(QKeyEvent
*e
)
261 if(e
->isAutoRepeat() == true) {
266 // Make a copy of the old notes so we know what's changed
267 copyArray(notes
, oldNotes
);
271 /* First row of keys */
315 /* Second row of keys */
366 void PianoInstrument::copyArray(bool source
[26], bool dest
[26])
368 for(int i
= 0; i
< 26; i
++) {
373 void PianoInstrument::emitSounds()
375 for(int i
= 0; i
< 26; i
++) {
376 if(notes
[i
] == oldNotes
[i
]) continue;
378 if(notes
[i
] == false)
379 emit
stopNote(i
+ noteStart
);
381 emit
playNote(i
+ noteStart
, 120, 0);