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 14KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. static std::map<int, QRectF> kMidiKey2RectMapHorizontal = {
  23. {0, QRectF(0, 0, 18, 64)}, // C
  24. {1, QRectF(13, 0, 11, 42)}, // C#
  25. {2, QRectF(18, 0, 25, 64)}, // D
  26. {3, QRectF(37, 0, 11, 42)}, // D#
  27. {4, QRectF(42, 0, 18, 64)}, // E
  28. {5, QRectF(60, 0, 18, 64)}, // F
  29. {6, QRectF(73, 0, 11, 42)}, // F#
  30. {7, QRectF(78, 0, 25, 64)}, // G
  31. {8, QRectF(97, 0, 11, 42)}, // G#
  32. {9, QRectF(102, 0, 25, 64)}, // A
  33. {10, QRectF(121, 0, 11, 42)}, // A#
  34. {11, QRectF(126, 0, 18, 64)} // B
  35. };
  36. static std::map<int, QRectF> kMidiKey2RectMapVertical = {
  37. {11, QRectF(0, 0, 64, 18)}, // B
  38. {10, QRectF(0, 14, 42, 7)}, // A#
  39. {9, QRectF(0, 18, 64, 24)}, // A
  40. {8, QRectF(0, 38, 42, 7)}, // G#
  41. {7, QRectF(0, 42, 64, 24)}, // G
  42. {6, QRectF(0, 62, 42, 7)}, // F#
  43. {5, QRectF(0, 66, 64, 18)}, // F
  44. {4, QRectF(0, 84, 64, 18)}, // E
  45. {3, QRectF(0, 98, 42, 7)}, // D#
  46. {2, QRectF(0, 102, 64, 24)}, // D
  47. {1, QRectF(0, 122, 42, 7)}, // C#
  48. {0, QRectF(0, 126, 64, 18)} // C
  49. };
  50. static const std::map<int, int> kMidiKeyboard2KeyMap = {
  51. // 3th octave
  52. {Qt::Key_Z, 48},
  53. {Qt::Key_S, 49},
  54. {Qt::Key_X, 50},
  55. {Qt::Key_D, 51},
  56. {Qt::Key_C, 52},
  57. {Qt::Key_V, 53},
  58. {Qt::Key_G, 54},
  59. {Qt::Key_B, 55},
  60. {Qt::Key_H, 56},
  61. {Qt::Key_N, 57},
  62. {Qt::Key_J, 58},
  63. {Qt::Key_M, 59},
  64. // 4th octave
  65. {Qt::Key_Q, 60},
  66. {Qt::Key_2, 61},
  67. {Qt::Key_W, 62},
  68. {Qt::Key_3, 63},
  69. {Qt::Key_E, 64},
  70. {Qt::Key_R, 65},
  71. {Qt::Key_5, 66},
  72. {Qt::Key_T, 67},
  73. {Qt::Key_6, 68},
  74. {Qt::Key_Y, 69},
  75. {Qt::Key_7, 70},
  76. {Qt::Key_U, 71}
  77. };
  78. static const QVector<int> kBlackNotes = {1, 3, 6, 8, 10};
  79. PixmapKeyboard::PixmapKeyboard(QWidget* parent)
  80. : QWidget(parent),
  81. fPixmap(""),
  82. fPixmapMode(HORIZONTAL),
  83. fColorStr("orange"),
  84. fFont("Monospace", 8, QFont::Normal),
  85. fOctaves(6),
  86. fLastMouseNote(-1),
  87. fWidth(0),
  88. fHeight(0),
  89. fNeedsUpdate(false),
  90. fMidiMap(kMidiKey2RectMapHorizontal)
  91. {
  92. setCursor(Qt::PointingHandCursor);
  93. setMode(HORIZONTAL);
  94. }
  95. void PixmapKeyboard::allNotesOff()
  96. {
  97. fEnabledKeys.clear();
  98. fNeedsUpdate = true;
  99. emit notesOff();
  100. QTimer::singleShot(0, this, SLOT(updateOnce()));
  101. }
  102. void PixmapKeyboard::sendNoteOn(int note, bool sendSignal)
  103. {
  104. if (0 <= note && note <= 127 && ! fEnabledKeys.contains(note))
  105. {
  106. fEnabledKeys.append(note);
  107. if (sendSignal)
  108. emit noteOn(note);
  109. fNeedsUpdate = true;
  110. QTimer::singleShot(0, this, SLOT(updateOnce()));
  111. }
  112. if (fEnabledKeys.count() == 1)
  113. emit notesOn();
  114. }
  115. void PixmapKeyboard::sendNoteOff(int note, bool sendSignal)
  116. {
  117. if (note >= 0 && note <= 127 && fEnabledKeys.contains(note))
  118. {
  119. fEnabledKeys.removeOne(note);
  120. if (sendSignal)
  121. emit noteOff(note);
  122. fNeedsUpdate = true;
  123. QTimer::singleShot(0, this, SLOT(updateOnce()));
  124. }
  125. if (fEnabledKeys.count() == 0)
  126. emit notesOff();
  127. }
  128. void PixmapKeyboard::setMode(Orientation mode, Color color)
  129. {
  130. if (color == COLOR_CLASSIC)
  131. {
  132. fColorStr = "classic";
  133. }
  134. else if (color == COLOR_ORANGE)
  135. {
  136. fColorStr = "orange";
  137. }
  138. else
  139. {
  140. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid color", mode, color);
  141. return setMode(mode);
  142. }
  143. if (mode == HORIZONTAL)
  144. {
  145. fMidiMap = kMidiKey2RectMapHorizontal;
  146. fPixmap.load(QString(":/bitmaps/kbd_h_%1.png").arg(fColorStr));
  147. fPixmapMode = HORIZONTAL;
  148. fWidth = fPixmap.width();
  149. fHeight = fPixmap.height() / 2;
  150. }
  151. else if (mode == VERTICAL)
  152. {
  153. fMidiMap = kMidiKey2RectMapVertical;
  154. fPixmap.load(QString(":/bitmaps/kbd_v_%1.png").arg(fColorStr));
  155. fPixmapMode = VERTICAL;
  156. fWidth = fPixmap.width() / 2;
  157. fHeight = fPixmap.height();
  158. }
  159. else
  160. {
  161. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid mode", mode, color);
  162. return setMode(HORIZONTAL);
  163. }
  164. setOctaves(fOctaves);
  165. }
  166. void PixmapKeyboard::setOctaves(int octaves)
  167. {
  168. Q_ASSERT(octaves >= 1 && octaves <= 8);
  169. if (octaves < 1)
  170. octaves = 1;
  171. else if (octaves > 8)
  172. octaves = 8;
  173. fOctaves = octaves;
  174. if (fPixmapMode == HORIZONTAL)
  175. {
  176. setMinimumSize(fWidth * fOctaves, fHeight);
  177. setMaximumSize(fWidth * fOctaves, fHeight);
  178. }
  179. else if (fPixmapMode == VERTICAL)
  180. {
  181. setMinimumSize(fWidth, fHeight * fOctaves);
  182. setMaximumSize(fWidth, fHeight * fOctaves);
  183. }
  184. update();
  185. }
  186. void PixmapKeyboard::handleMousePos(const QPoint& pos)
  187. {
  188. int note;
  189. int octave;
  190. QPointF keyPos;
  191. if (fPixmapMode == HORIZONTAL)
  192. {
  193. if (pos.x() < 0 or pos.x() > fOctaves * 144)
  194. return;
  195. int posX = pos.x() - 1;
  196. octave = posX / fWidth;
  197. keyPos = QPointF(posX % fWidth, pos.y());
  198. }
  199. else if (fPixmapMode == VERTICAL)
  200. {
  201. if (pos.y() < 0 or pos.y() > fOctaves * 144)
  202. return;
  203. int posY = pos.y() - 1;
  204. octave = fOctaves - posY / fHeight;
  205. keyPos = QPointF(pos.x(), posY % fHeight);
  206. }
  207. else
  208. return;
  209. octave += 3;
  210. if (fMidiMap[1].contains(keyPos)) // C#
  211. note = 1;
  212. else if (fMidiMap[3].contains(keyPos)) // D#
  213. note = 3;
  214. else if (fMidiMap[6].contains(keyPos)) // F#
  215. note = 6;
  216. else if (fMidiMap[8].contains(keyPos)) // G#
  217. note = 8;
  218. else if (fMidiMap[10].contains(keyPos))// A#
  219. note = 10;
  220. else if (fMidiMap[0].contains(keyPos)) // C
  221. note = 0;
  222. else if (fMidiMap[2].contains(keyPos)) // D
  223. note = 2;
  224. else if (fMidiMap[4].contains(keyPos)) // E
  225. note = 4;
  226. else if (fMidiMap[5].contains(keyPos)) // F
  227. note = 5;
  228. else if (fMidiMap[7].contains(keyPos)) // G
  229. note = 7;
  230. else if (fMidiMap[9].contains(keyPos)) // A
  231. note = 9;
  232. else if (fMidiMap[11].contains(keyPos))// B
  233. note = 11;
  234. else
  235. note = -1;
  236. if (note != -1)
  237. {
  238. note += octave * 12;
  239. if (fLastMouseNote != note)
  240. {
  241. sendNoteOff(fLastMouseNote);
  242. sendNoteOn(note);
  243. }
  244. }
  245. else if (fLastMouseNote != -1)
  246. sendNoteOff(fLastMouseNote);
  247. fLastMouseNote = note;
  248. }
  249. void PixmapKeyboard::keyPressEvent(QKeyEvent* event)
  250. {
  251. if (! event->isAutoRepeat())
  252. {
  253. int qKey = event->key();
  254. std::map<int, int>::const_iterator it = kMidiKeyboard2KeyMap.find(qKey);
  255. if (it != kMidiKeyboard2KeyMap.end())
  256. sendNoteOn(it->second);
  257. }
  258. QWidget::keyPressEvent(event);
  259. }
  260. void PixmapKeyboard::keyReleaseEvent(QKeyEvent* event)
  261. {
  262. if (! event->isAutoRepeat())
  263. {
  264. int qKey = event->key();
  265. std::map<int, int>::const_iterator it = kMidiKeyboard2KeyMap.find(qKey);
  266. if (it != kMidiKeyboard2KeyMap.end())
  267. sendNoteOff(it->second);
  268. }
  269. QWidget::keyReleaseEvent(event);
  270. }
  271. void PixmapKeyboard::mousePressEvent(QMouseEvent* event)
  272. {
  273. fLastMouseNote = -1;
  274. handleMousePos(event->pos());
  275. setFocus();
  276. QWidget::mousePressEvent(event);
  277. }
  278. void PixmapKeyboard::mouseMoveEvent(QMouseEvent* event)
  279. {
  280. handleMousePos(event->pos());
  281. QWidget::mousePressEvent(event);
  282. }
  283. void PixmapKeyboard::mouseReleaseEvent(QMouseEvent* event)
  284. {
  285. if (fLastMouseNote != -1)
  286. {
  287. sendNoteOff(fLastMouseNote);
  288. fLastMouseNote = -1;
  289. }
  290. QWidget::mouseReleaseEvent(event);
  291. }
  292. void PixmapKeyboard::paintEvent(QPaintEvent* event)
  293. {
  294. QPainter painter(this);
  295. event->accept();
  296. // -------------------------------------------------------------
  297. // Paint clean keys (as background)
  298. for (int octave=0; octave < fOctaves; octave++)
  299. {
  300. QRectF target;
  301. if (fPixmapMode == HORIZONTAL)
  302. target = QRectF(fWidth * octave, 0, fWidth, fHeight);
  303. else if (fPixmapMode == VERTICAL)
  304. target = QRectF(0, fHeight * octave, fWidth, fHeight);
  305. else
  306. return;
  307. QRectF source = QRectF(0, 0, fWidth, fHeight);
  308. painter.drawPixmap(target, fPixmap, source);
  309. }
  310. // -------------------------------------------------------------
  311. // Paint (white) pressed keys
  312. bool paintedWhite = false;
  313. for (int i=0, count=fEnabledKeys.count(); i < count; i++)
  314. {
  315. int octave, note = fEnabledKeys[i];
  316. const QRectF& pos(_getRectFromMidiNote(note));
  317. if (_isNoteBlack(note))
  318. continue;
  319. if (note < 36)
  320. // cannot paint this note
  321. continue;
  322. else if (note < 48)
  323. octave = 0;
  324. else if (note < 60)
  325. octave = 1;
  326. else if (note < 72)
  327. octave = 2;
  328. else if (note < 84)
  329. octave = 3;
  330. else if (note < 96)
  331. octave = 4;
  332. else if (note < 108)
  333. octave = 5;
  334. else if (note < 120)
  335. octave = 6;
  336. else if (note < 132)
  337. octave = 7;
  338. else
  339. // cannot paint this note either
  340. continue;
  341. if (fPixmapMode == VERTICAL)
  342. octave = fOctaves - octave - 1;
  343. QRectF target, source;
  344. if (fPixmapMode == HORIZONTAL)
  345. {
  346. target = QRectF(pos.x() + (fWidth * octave), 0, pos.width(), pos.height());
  347. source = QRectF(pos.x(), fHeight, pos.width(), pos.height());
  348. }
  349. else if (fPixmapMode == VERTICAL)
  350. {
  351. target = QRectF(pos.x(), pos.y() + (fHeight * octave), pos.width(), pos.height());
  352. source = QRectF(fWidth, pos.y(), pos.width(), pos.height());
  353. }
  354. else
  355. return;
  356. paintedWhite = true;
  357. painter.drawPixmap(target, fPixmap, source);
  358. }
  359. // -------------------------------------------------------------
  360. // Clear white keys border
  361. if (paintedWhite)
  362. {
  363. for (int octave=0; octave < fOctaves; octave++)
  364. {
  365. foreach (int note, kBlackNotes)
  366. {
  367. QRectF target, source;
  368. const QRectF& pos(_getRectFromMidiNote(note));
  369. if (fPixmapMode == HORIZONTAL)
  370. {
  371. target = QRectF(pos.x() + (fWidth * octave), 0, pos.width(), pos.height());
  372. source = QRectF(pos.x(), 0, pos.width(), pos.height());
  373. }
  374. else if (fPixmapMode == VERTICAL)
  375. {
  376. target = QRectF(pos.x(), pos.y() + (fHeight * octave), pos.width(), pos.height());
  377. source = QRectF(0, pos.y(), pos.width(), pos.height());
  378. }
  379. else
  380. return;
  381. painter.drawPixmap(target, fPixmap, source);
  382. }
  383. }
  384. }
  385. // -------------------------------------------------------------
  386. // Paint (black) pressed keys
  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 < 36)
  394. // cannot paint this note
  395. continue;
  396. else if (note < 48)
  397. octave = 0;
  398. else if (note < 60)
  399. octave = 1;
  400. else if (note < 72)
  401. octave = 2;
  402. else if (note < 84)
  403. octave = 3;
  404. else if (note < 96)
  405. octave = 4;
  406. else if (note < 108)
  407. octave = 5;
  408. else if (note < 120)
  409. octave = 6;
  410. else if (note < 132)
  411. octave = 7;
  412. else
  413. // cannot paint this note either
  414. continue;
  415. if (fPixmapMode == VERTICAL)
  416. octave = fOctaves - octave - 1;
  417. QRectF target, source;
  418. if (fPixmapMode == HORIZONTAL)
  419. {
  420. target = QRectF(pos.x() + (fWidth * octave), 0, pos.width(), pos.height());
  421. source = QRectF(pos.x(), fHeight, pos.width(), pos.height());
  422. }
  423. else if (fPixmapMode == VERTICAL)
  424. {
  425. target = QRectF(pos.x(), pos.y() + (fHeight * octave), pos.width(), pos.height());
  426. source = QRectF(fWidth, pos.y(), pos.width(), pos.height());
  427. }
  428. else
  429. return;
  430. painter.drawPixmap(target, fPixmap, source);
  431. }
  432. // Paint C-number note info
  433. painter.setFont(fFont);
  434. painter.setPen(Qt::black);
  435. for (int i=0; i < fOctaves; i++)
  436. {
  437. if (fPixmapMode == HORIZONTAL)
  438. painter.drawText(i * 144, 48, 18, 18, Qt::AlignCenter, QString("C%1").arg(i + 2));
  439. else if (fPixmapMode == VERTICAL)
  440. painter.drawText(45, (fOctaves * 144) - (i * 144) - 16, 18, 18, Qt::AlignCenter, QString("C%1").arg(i + 2));
  441. }
  442. }
  443. void PixmapKeyboard::updateOnce()
  444. {
  445. if (fNeedsUpdate)
  446. {
  447. update();
  448. fNeedsUpdate = false;
  449. }
  450. }
  451. bool PixmapKeyboard::_isNoteBlack(int note) const
  452. {
  453. int baseNote = note % 12;
  454. return kBlackNotes.contains(baseNote);
  455. }
  456. const QRectF& PixmapKeyboard::_getRectFromMidiNote(int note) const
  457. {
  458. return fMidiMap[note % 12];
  459. }