Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pixmapkeyboard.cpp 17KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Pixmap Keyboard, a custom Qt4 widget
  3. * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "pixmapkeyboard.hpp"
  18. #include <QtCore/QTimer>
  19. #include <QtGui/QKeyEvent>
  20. #include <QtGui/QMouseEvent>
  21. #include <QtGui/QPainter>
  22. #ifndef HAVE_CPP11_SUPPORT
  23. static std::map<int, QRectF> kMidiKey2RectMapHorizontal;
  24. #else
  25. static std::map<int, QRectF> kMidiKey2RectMapHorizontal = {
  26. {0, QRectF(0, 0, 18, 64)}, // C
  27. {1, QRectF(13, 0, 11, 42)}, // C#
  28. {2, QRectF(18, 0, 25, 64)}, // D
  29. {3, QRectF(37, 0, 11, 42)}, // D#
  30. {4, QRectF(42, 0, 18, 64)}, // E
  31. {5, QRectF(60, 0, 18, 64)}, // F
  32. {6, QRectF(73, 0, 11, 42)}, // F#
  33. {7, QRectF(78, 0, 25, 64)}, // G
  34. {8, QRectF(97, 0, 11, 42)}, // G#
  35. {9, QRectF(102, 0, 25, 64)}, // A
  36. {10, QRectF(121, 0, 11, 42)}, // A#
  37. {11, QRectF(126, 0, 18, 64)} // B
  38. };
  39. #endif
  40. #ifndef HAVE_CPP11_SUPPORT
  41. static std::map<int, QRectF> kMidiKey2RectMapVertical;
  42. #else
  43. static std::map<int, QRectF> kMidiKey2RectMapVertical = {
  44. {11, QRectF(0, 0, 64, 18)}, // B
  45. {10, QRectF(0, 14, 42, 7)}, // A#
  46. {9, QRectF(0, 18, 64, 24)}, // A
  47. {8, QRectF(0, 38, 42, 7)}, // G#
  48. {7, QRectF(0, 42, 64, 24)}, // G
  49. {6, QRectF(0, 62, 42, 7)}, // F#
  50. {5, QRectF(0, 66, 64, 18)}, // F
  51. {4, QRectF(0, 84, 64, 18)}, // E
  52. {3, QRectF(0, 98, 42, 7)}, // D#
  53. {2, QRectF(0, 102, 64, 24)}, // D
  54. {1, QRectF(0, 122, 42, 7)}, // C#
  55. {0, QRectF(0, 126, 64, 18)} // C
  56. };
  57. #endif
  58. #ifndef HAVE_CPP11_SUPPORT
  59. static std::map<int, int> kMidiKeyboard2KeyMap;
  60. #else
  61. static const std::map<int, int> kMidiKeyboard2KeyMap = {
  62. // 3th octave
  63. {Qt::Key_Z, 48},
  64. {Qt::Key_S, 49},
  65. {Qt::Key_X, 50},
  66. {Qt::Key_D, 51},
  67. {Qt::Key_C, 52},
  68. {Qt::Key_V, 53},
  69. {Qt::Key_G, 54},
  70. {Qt::Key_B, 55},
  71. {Qt::Key_H, 56},
  72. {Qt::Key_N, 57},
  73. {Qt::Key_J, 58},
  74. {Qt::Key_M, 59},
  75. // 4th octave
  76. {Qt::Key_Q, 60},
  77. {Qt::Key_2, 61},
  78. {Qt::Key_W, 62},
  79. {Qt::Key_3, 63},
  80. {Qt::Key_E, 64},
  81. {Qt::Key_R, 65},
  82. {Qt::Key_5, 66},
  83. {Qt::Key_T, 67},
  84. {Qt::Key_6, 68},
  85. {Qt::Key_Y, 69},
  86. {Qt::Key_7, 70},
  87. {Qt::Key_U, 71}
  88. };
  89. #endif
  90. #ifndef HAVE_CPP11_SUPPORT
  91. static QVector<int> kBlackNotes;
  92. #else
  93. static const QVector<int> kBlackNotes = {1, 3, 6, 8, 10};
  94. #endif
  95. #ifndef HAVE_CPP11_SUPPORT
  96. static const struct PixmapKeyboardInit {
  97. PixmapKeyboardInit() {
  98. // kMidiKey2RectMapHorizontal
  99. kMidiKey2RectMapHorizontal[0] = QRectF(0, 0, 18, 64); // C
  100. kMidiKey2RectMapHorizontal[1] = QRectF(13, 0, 11, 42); // C#
  101. kMidiKey2RectMapHorizontal[2] = QRectF(18, 0, 25, 64); // D
  102. kMidiKey2RectMapHorizontal[3] = QRectF(37, 0, 11, 42); // D#
  103. kMidiKey2RectMapHorizontal[4] = QRectF(42, 0, 18, 64); // E
  104. kMidiKey2RectMapHorizontal[5] = QRectF(60, 0, 18, 64); // F
  105. kMidiKey2RectMapHorizontal[6] = QRectF(73, 0, 11, 42); // F#
  106. kMidiKey2RectMapHorizontal[7] = QRectF(78, 0, 25, 64); // G
  107. kMidiKey2RectMapHorizontal[8] = QRectF(97, 0, 11, 42); // G#
  108. kMidiKey2RectMapHorizontal[9] = QRectF(102, 0, 25, 64); // A
  109. kMidiKey2RectMapHorizontal[10] = QRectF(121, 0, 11, 42); // A#
  110. kMidiKey2RectMapHorizontal[11] = QRectF(126, 0, 18, 64); // B
  111. // kMidiKey2RectMapVertical
  112. kMidiKey2RectMapVertical[11] = QRectF(0, 0, 64, 18); // B
  113. kMidiKey2RectMapVertical[10] = QRectF(0, 14, 42, 7); // A#
  114. kMidiKey2RectMapVertical[9] = QRectF(0, 18, 64, 24); // A
  115. kMidiKey2RectMapVertical[8] = QRectF(0, 38, 42, 7); // G#
  116. kMidiKey2RectMapVertical[7] = QRectF(0, 42, 64, 24); // G
  117. kMidiKey2RectMapVertical[6] = QRectF(0, 62, 42, 7); // F#
  118. kMidiKey2RectMapVertical[5] = QRectF(0, 66, 64, 18); // F
  119. kMidiKey2RectMapVertical[4] = QRectF(0, 84, 64, 18); // E
  120. kMidiKey2RectMapVertical[3] = QRectF(0, 98, 42, 7); // D#
  121. kMidiKey2RectMapVertical[2] = QRectF(0, 102, 64, 24); // D
  122. kMidiKey2RectMapVertical[1] = QRectF(0, 122, 42, 7); // C#
  123. kMidiKey2RectMapVertical[0] = QRectF(0, 126, 64, 18); // C
  124. // kMidiKeyboard2KeyMap, 3th octave
  125. kMidiKeyboard2KeyMap[Qt::Key_Z] = 48;
  126. kMidiKeyboard2KeyMap[Qt::Key_S] = 49;
  127. kMidiKeyboard2KeyMap[Qt::Key_X] = 50;
  128. kMidiKeyboard2KeyMap[Qt::Key_D] = 51;
  129. kMidiKeyboard2KeyMap[Qt::Key_C] = 52;
  130. kMidiKeyboard2KeyMap[Qt::Key_V] = 53;
  131. kMidiKeyboard2KeyMap[Qt::Key_G] = 54;
  132. kMidiKeyboard2KeyMap[Qt::Key_B] = 55;
  133. kMidiKeyboard2KeyMap[Qt::Key_H] = 56;
  134. kMidiKeyboard2KeyMap[Qt::Key_N] = 57;
  135. kMidiKeyboard2KeyMap[Qt::Key_J] = 58;
  136. kMidiKeyboard2KeyMap[Qt::Key_M] = 59;
  137. // kMidiKeyboard2KeyMap, 4th octave
  138. kMidiKeyboard2KeyMap[Qt::Key_Q] = 60;
  139. kMidiKeyboard2KeyMap[Qt::Key_2] = 61;
  140. kMidiKeyboard2KeyMap[Qt::Key_W] = 62;
  141. kMidiKeyboard2KeyMap[Qt::Key_3] = 63;
  142. kMidiKeyboard2KeyMap[Qt::Key_E] = 64;
  143. kMidiKeyboard2KeyMap[Qt::Key_R] = 65;
  144. kMidiKeyboard2KeyMap[Qt::Key_5] = 66;
  145. kMidiKeyboard2KeyMap[Qt::Key_T] = 67;
  146. kMidiKeyboard2KeyMap[Qt::Key_6] = 68;
  147. kMidiKeyboard2KeyMap[Qt::Key_Y] = 69;
  148. kMidiKeyboard2KeyMap[Qt::Key_7] = 70;
  149. kMidiKeyboard2KeyMap[Qt::Key_U] = 71;
  150. // kBlackNotes
  151. kBlackNotes << 1;
  152. kBlackNotes << 3;
  153. kBlackNotes << 6;
  154. kBlackNotes << 8;
  155. kBlackNotes << 10;
  156. }
  157. } _pixmapKeyboardInitInit;
  158. #endif
  159. PixmapKeyboard::PixmapKeyboard(QWidget* parent)
  160. : QWidget(parent),
  161. fPixmap(""),
  162. fPixmapMode(HORIZONTAL),
  163. fColorStr("orange"),
  164. fFont("Monospace", 7, QFont::Normal),
  165. fOctaves(6),
  166. fLastMouseNote(-1),
  167. fWidth(0),
  168. fHeight(0),
  169. fMidiMap(kMidiKey2RectMapHorizontal)
  170. {
  171. setCursor(Qt::PointingHandCursor);
  172. setMode(HORIZONTAL);
  173. }
  174. void PixmapKeyboard::allNotesOff()
  175. {
  176. fEnabledKeys.clear();
  177. emit notesOff();
  178. update();
  179. }
  180. void PixmapKeyboard::sendNoteOn(int note, bool sendSignal)
  181. {
  182. if (0 <= note && note <= 127 && ! fEnabledKeys.contains(note))
  183. {
  184. fEnabledKeys.append(note);
  185. if (sendSignal)
  186. emit noteOn(note);
  187. update();
  188. }
  189. if (fEnabledKeys.count() == 1)
  190. emit notesOn();
  191. }
  192. void PixmapKeyboard::sendNoteOff(int note, bool sendSignal)
  193. {
  194. if (note >= 0 && note <= 127 && fEnabledKeys.contains(note))
  195. {
  196. fEnabledKeys.removeOne(note);
  197. if (sendSignal)
  198. emit noteOff(note);
  199. update();
  200. }
  201. if (fEnabledKeys.count() == 0)
  202. emit notesOff();
  203. }
  204. void PixmapKeyboard::setMode(Orientation mode, Color color)
  205. {
  206. if (color == COLOR_CLASSIC)
  207. {
  208. fColorStr = "classic";
  209. }
  210. else if (color == COLOR_ORANGE)
  211. {
  212. fColorStr = "orange";
  213. }
  214. else
  215. {
  216. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid color", mode, color);
  217. return setMode(mode);
  218. }
  219. if (mode == HORIZONTAL)
  220. {
  221. fMidiMap = kMidiKey2RectMapHorizontal;
  222. fPixmap.load(QString(":/bitmaps/kbd_h_%1.png").arg(fColorStr));
  223. fPixmapMode = HORIZONTAL;
  224. fWidth = fPixmap.width();
  225. fHeight = fPixmap.height() / 2;
  226. }
  227. else if (mode == VERTICAL)
  228. {
  229. fMidiMap = kMidiKey2RectMapVertical;
  230. fPixmap.load(QString(":/bitmaps/kbd_v_%1.png").arg(fColorStr));
  231. fPixmapMode = VERTICAL;
  232. fWidth = fPixmap.width() / 2;
  233. fHeight = fPixmap.height();
  234. }
  235. else
  236. {
  237. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid mode", mode, color);
  238. return setMode(HORIZONTAL);
  239. }
  240. setOctaves(fOctaves);
  241. }
  242. void PixmapKeyboard::setOctaves(int octaves)
  243. {
  244. Q_ASSERT(octaves >= 1 && octaves <= 10);
  245. if (octaves < 1)
  246. octaves = 1;
  247. else if (octaves > 10)
  248. octaves = 10;
  249. fOctaves = octaves;
  250. if (fPixmapMode == HORIZONTAL)
  251. {
  252. setMinimumSize(fWidth * fOctaves, fHeight);
  253. setMaximumSize(fWidth * fOctaves, fHeight);
  254. }
  255. else if (fPixmapMode == VERTICAL)
  256. {
  257. setMinimumSize(fWidth, fHeight * fOctaves);
  258. setMaximumSize(fWidth, fHeight * fOctaves);
  259. }
  260. update();
  261. }
  262. void PixmapKeyboard::handleMousePos(const QPoint& pos)
  263. {
  264. int note, octave;
  265. QPointF keyPos;
  266. if (fPixmapMode == HORIZONTAL)
  267. {
  268. if (pos.x() < 0 or pos.x() > fOctaves * 144)
  269. return;
  270. int posX = pos.x() - 1;
  271. octave = posX / fWidth;
  272. keyPos = QPointF(posX % fWidth, pos.y());
  273. }
  274. else if (fPixmapMode == VERTICAL)
  275. {
  276. if (pos.y() < 0 or pos.y() > fOctaves * 144)
  277. return;
  278. int posY = pos.y() - 1;
  279. octave = fOctaves - posY / fHeight;
  280. keyPos = QPointF(pos.x(), posY % fHeight);
  281. }
  282. else
  283. return;
  284. if (fMidiMap[1].contains(keyPos)) // C#
  285. note = 1;
  286. else if (fMidiMap[3].contains(keyPos)) // D#
  287. note = 3;
  288. else if (fMidiMap[6].contains(keyPos)) // F#
  289. note = 6;
  290. else if (fMidiMap[8].contains(keyPos)) // G#
  291. note = 8;
  292. else if (fMidiMap[10].contains(keyPos))// A#
  293. note = 10;
  294. else if (fMidiMap[0].contains(keyPos)) // C
  295. note = 0;
  296. else if (fMidiMap[2].contains(keyPos)) // D
  297. note = 2;
  298. else if (fMidiMap[4].contains(keyPos)) // E
  299. note = 4;
  300. else if (fMidiMap[5].contains(keyPos)) // F
  301. note = 5;
  302. else if (fMidiMap[7].contains(keyPos)) // G
  303. note = 7;
  304. else if (fMidiMap[9].contains(keyPos)) // A
  305. note = 9;
  306. else if (fMidiMap[11].contains(keyPos))// B
  307. note = 11;
  308. else
  309. note = -1;
  310. if (note != -1)
  311. {
  312. note += octave * 12;
  313. if (fLastMouseNote != note)
  314. {
  315. sendNoteOff(fLastMouseNote);
  316. sendNoteOn(note);
  317. }
  318. }
  319. else if (fLastMouseNote != -1)
  320. sendNoteOff(fLastMouseNote);
  321. fLastMouseNote = note;
  322. }
  323. void PixmapKeyboard::keyPressEvent(QKeyEvent* event)
  324. {
  325. if (! event->isAutoRepeat())
  326. {
  327. int qKey = event->key();
  328. std::map<int, int>::const_iterator it = kMidiKeyboard2KeyMap.find(qKey);
  329. if (it != kMidiKeyboard2KeyMap.end())
  330. sendNoteOn(it->second);
  331. }
  332. QWidget::keyPressEvent(event);
  333. }
  334. void PixmapKeyboard::keyReleaseEvent(QKeyEvent* event)
  335. {
  336. if (! event->isAutoRepeat())
  337. {
  338. int qKey = event->key();
  339. std::map<int, int>::const_iterator it = kMidiKeyboard2KeyMap.find(qKey);
  340. if (it != kMidiKeyboard2KeyMap.end())
  341. sendNoteOff(it->second);
  342. }
  343. QWidget::keyReleaseEvent(event);
  344. }
  345. void PixmapKeyboard::mousePressEvent(QMouseEvent* event)
  346. {
  347. fLastMouseNote = -1;
  348. handleMousePos(event->pos());
  349. setFocus();
  350. QWidget::mousePressEvent(event);
  351. }
  352. void PixmapKeyboard::mouseMoveEvent(QMouseEvent* event)
  353. {
  354. handleMousePos(event->pos());
  355. QWidget::mouseMoveEvent(event);
  356. }
  357. void PixmapKeyboard::mouseReleaseEvent(QMouseEvent* event)
  358. {
  359. if (fLastMouseNote != -1)
  360. {
  361. sendNoteOff(fLastMouseNote);
  362. fLastMouseNote = -1;
  363. }
  364. QWidget::mouseReleaseEvent(event);
  365. }
  366. void PixmapKeyboard::paintEvent(QPaintEvent* event)
  367. {
  368. QPainter painter(this);
  369. event->accept();
  370. // -------------------------------------------------------------
  371. // Paint clean keys (as background)
  372. for (int octave=0; octave < fOctaves; ++octave)
  373. {
  374. QRectF target;
  375. if (fPixmapMode == HORIZONTAL)
  376. target = QRectF(fWidth * octave, 0, fWidth, fHeight);
  377. else if (fPixmapMode == VERTICAL)
  378. target = QRectF(0, fHeight * octave, fWidth, fHeight);
  379. else
  380. return;
  381. QRectF source = QRectF(0, 0, fWidth, fHeight);
  382. painter.drawPixmap(target, fPixmap, source);
  383. }
  384. // -------------------------------------------------------------
  385. // Paint (white) pressed keys
  386. bool paintedWhite = false;
  387. for (int i=0, count=fEnabledKeys.count(); i < count; ++i)
  388. {
  389. int octave, note = fEnabledKeys[i];
  390. const QRectF& pos(_getRectFromMidiNote(note));
  391. if (_isNoteBlack(note))
  392. continue;
  393. if (note < 12)
  394. octave = 0;
  395. else if (note < 24)
  396. octave = 1;
  397. else if (note < 36)
  398. octave = 2;
  399. else if (note < 48)
  400. octave = 3;
  401. else if (note < 60)
  402. octave = 4;
  403. else if (note < 72)
  404. octave = 5;
  405. else if (note < 84)
  406. octave = 6;
  407. else if (note < 96)
  408. octave = 7;
  409. else if (note < 108)
  410. octave = 8;
  411. else if (note < 120)
  412. octave = 9;
  413. else if (note < 132)
  414. octave = 10;
  415. else
  416. // cannot paint this note
  417. continue;
  418. if (fPixmapMode == VERTICAL)
  419. octave = fOctaves - octave - 1;
  420. QRectF target, source;
  421. if (fPixmapMode == HORIZONTAL)
  422. {
  423. target = QRectF(pos.x() + (fWidth * octave), 0, pos.width(), pos.height());
  424. source = QRectF(pos.x(), fHeight, pos.width(), pos.height());
  425. }
  426. else if (fPixmapMode == VERTICAL)
  427. {
  428. target = QRectF(pos.x(), pos.y() + (fHeight * octave), pos.width(), pos.height());
  429. source = QRectF(fWidth, pos.y(), pos.width(), pos.height());
  430. }
  431. else
  432. return;
  433. paintedWhite = true;
  434. painter.drawPixmap(target, fPixmap, source);
  435. }
  436. // -------------------------------------------------------------
  437. // Clear white keys border
  438. if (paintedWhite)
  439. {
  440. for (int octave=0; octave < fOctaves; ++octave)
  441. {
  442. foreach (int note, kBlackNotes)
  443. {
  444. QRectF target, source;
  445. const QRectF& pos(_getRectFromMidiNote(note));
  446. if (fPixmapMode == HORIZONTAL)
  447. {
  448. target = QRectF(pos.x() + (fWidth * octave), 0, pos.width(), pos.height());
  449. source = QRectF(pos.x(), 0, pos.width(), pos.height());
  450. }
  451. else if (fPixmapMode == VERTICAL)
  452. {
  453. target = QRectF(pos.x(), pos.y() + (fHeight * octave), pos.width(), pos.height());
  454. source = QRectF(0, pos.y(), pos.width(), pos.height());
  455. }
  456. else
  457. return;
  458. painter.drawPixmap(target, fPixmap, source);
  459. }
  460. }
  461. }
  462. // -------------------------------------------------------------
  463. // Paint (black) pressed keys
  464. for (int i=0, count=fEnabledKeys.count(); i < count; ++i)
  465. {
  466. int octave, note = fEnabledKeys[i];
  467. const QRectF& pos(_getRectFromMidiNote(note));
  468. if (! _isNoteBlack(note))
  469. continue;
  470. if (note < 12)
  471. octave = 0;
  472. else if (note < 24)
  473. octave = 1;
  474. else if (note < 36)
  475. octave = 2;
  476. else if (note < 48)
  477. octave = 3;
  478. else if (note < 60)
  479. octave = 4;
  480. else if (note < 72)
  481. octave = 5;
  482. else if (note < 84)
  483. octave = 6;
  484. else if (note < 96)
  485. octave = 7;
  486. else if (note < 108)
  487. octave = 8;
  488. else if (note < 120)
  489. octave = 9;
  490. else if (note < 132)
  491. octave = 10;
  492. else
  493. // cannot paint this note
  494. continue;
  495. if (fPixmapMode == VERTICAL)
  496. octave = fOctaves - octave - 1;
  497. QRectF target, source;
  498. if (fPixmapMode == HORIZONTAL)
  499. {
  500. target = QRectF(pos.x() + (fWidth * octave), 0, pos.width(), pos.height());
  501. source = QRectF(pos.x(), fHeight, pos.width(), pos.height());
  502. }
  503. else if (fPixmapMode == VERTICAL)
  504. {
  505. target = QRectF(pos.x(), pos.y() + (fHeight * octave), pos.width(), pos.height());
  506. source = QRectF(fWidth, pos.y(), pos.width(), pos.height());
  507. }
  508. else
  509. return;
  510. painter.drawPixmap(target, fPixmap, source);
  511. }
  512. // Paint C-number note info
  513. painter.setFont(fFont);
  514. painter.setPen(Qt::black);
  515. for (int i=0; i < fOctaves; ++i)
  516. {
  517. if (fPixmapMode == HORIZONTAL)
  518. painter.drawText(i * 144, 48, 18, 18, Qt::AlignCenter, QString("C%1").arg(i-1));
  519. else if (fPixmapMode == VERTICAL)
  520. painter.drawText(45, (fOctaves * 144) - (i * 144) - 16, 18, 18, Qt::AlignCenter, QString("C%1").arg(i-1));
  521. }
  522. }
  523. bool PixmapKeyboard::_isNoteBlack(int note) const
  524. {
  525. const int baseNote = note % 12;
  526. return kBlackNotes.contains(baseNote);
  527. }
  528. const QRectF& PixmapKeyboard::_getRectFromMidiNote(int note) const
  529. {
  530. const int baseNote = note % 12;
  531. return fMidiMap[baseNote];
  532. }