]>
code.delx.au - virtualtones/blob - pianoinstrument.cpp
127d9e14ded50daf9eca339d68316ee9c96809c1
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 "Base 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>"
48 "<li>When you push a key, the note it corresponds to is highlighted</li>"
57 void PianoInstrument::paintEvent(QPaintEvent
*)
60 paint
.setPen(Qt::red
);
62 const int topBlackY
= 38;
63 const int topWhiteY
= 70;
64 const int botBlackY
= 38 + 110;
65 const int botWhiteY
= 70 + 110;
69 if(notes
[0] == true) {
70 paint
.drawEllipse(6, topWhiteY
, w
, h
);
72 if(notes
[1] == true) {
73 paint
.drawEllipse(15, topBlackY
, w
, h
);
75 if(notes
[2] == true) {
76 paint
.drawEllipse(29, topWhiteY
, w
, h
);
78 if(notes
[3] == true) {
79 paint
.drawEllipse(45, topBlackY
, w
, h
);
81 if(notes
[4] == true) {
82 paint
.drawEllipse(52, topWhiteY
, w
, h
);
84 if(notes
[5] == true) {
85 paint
.drawEllipse(75, topWhiteY
, w
, h
);
87 if(notes
[6] == true) {
88 paint
.drawEllipse(85, topBlackY
, w
, h
);
90 if(notes
[7] == true) {
91 paint
.drawEllipse(97, topWhiteY
, w
, h
);
93 if(notes
[8] == true) {
94 paint
.drawEllipse(113, topBlackY
, w
, h
);
96 if(notes
[9] == true) {
97 paint
.drawEllipse(120, topWhiteY
, w
, h
);
99 if(notes
[10] == true) {
100 paint
.drawEllipse(136, topBlackY
, w
, h
);
102 if(notes
[11] == true) {
103 paint
.drawEllipse(143, topWhiteY
, w
, h
);
105 if(notes
[12] == true) {
106 paint
.drawEllipse(166, topWhiteY
, w
, h
);
107 paint
.drawEllipse(6, botWhiteY
, w
, h
);
109 if(notes
[13] == true) {
110 paint
.drawEllipse(176, topBlackY
, w
, h
);
111 paint
.drawEllipse(15, botBlackY
, w
, h
);
113 if(notes
[14] == true) {
114 paint
.drawEllipse(29, botWhiteY
, w
, h
);
116 if(notes
[15] == true) {
117 paint
.drawEllipse(45, botBlackY
, w
, h
);
119 if(notes
[16] == true) {
120 paint
.drawEllipse(52, botWhiteY
, w
, h
);
122 if(notes
[17] == true) {
123 paint
.drawEllipse(75, botWhiteY
, w
, h
);
125 if(notes
[18] == true) {
126 paint
.drawEllipse(85, botBlackY
, w
, h
);
128 if(notes
[19] == true) {
129 paint
.drawEllipse(97, botWhiteY
, w
, h
);
131 if(notes
[20] == true) {
132 paint
.drawEllipse(113, botBlackY
, w
, h
);
134 if(notes
[21] == true) {
135 paint
.drawEllipse(120, botWhiteY
, w
, h
);
137 if(notes
[22] == true) {
138 paint
.drawEllipse(136, botBlackY
, w
, h
);
140 if(notes
[23] == true) {
141 paint
.drawEllipse(143, botWhiteY
, w
, h
);
143 if(notes
[24] == true) {
144 paint
.drawEllipse(166, botWhiteY
, w
, h
);
146 if(notes
[25] == true) {
147 paint
.drawEllipse(176, botBlackY
, w
, h
);
153 void PianoInstrument::keyPressEvent(QKeyEvent
*e
)
155 if(e
->isAutoRepeat() == true) {
160 // Make a copy of the old notes so we know what's changed
161 copyArray(notes
, oldNotes
);
165 /* First row of keys */
209 /* Second row of keys */
260 void PianoInstrument::keyReleaseEvent(QKeyEvent
*e
)
262 if(e
->isAutoRepeat() == true) {
267 // Make a copy of the old notes so we know what's changed
268 copyArray(notes
, oldNotes
);
272 /* First row of keys */
316 /* Second row of keys */
367 void PianoInstrument::copyArray(bool source
[26], bool dest
[26])
369 for(int i
= 0; i
< 26; i
++) {
374 void PianoInstrument::emitSounds()
376 for(int i
= 0; i
< 26; i
++) {
377 if(notes
[i
] == oldNotes
[i
]) continue;
379 if(notes
[i
] == false)
380 emit
stopNote(i
+ noteStart
);
382 emit
playNote(i
+ noteStart
, 120, 0);