noteEditor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. This file is part of QTau
  3. Copyright (C) 2013-2018 Tobias "Tomoko" Platen <tplaten@posteo.de>
  4. Copyright (C) 2013 digited <https://github.com/digited>
  5. Copyright (C) 2010-2013 HAL@ShurabaP <https://github.com/haruneko>
  6. QTau is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. SPDX-License-Identifier: GPL-3.0+
  17. */
  18. #include "ui/noteEditor.h"
  19. #include "NoteEvents.h"
  20. #include "ui/noteEditorHandlers.h"
  21. #define __devloglevel__ 4
  22. #include <qevent.h>
  23. #include <qpainter.h>
  24. #include <QMimeData>
  25. #include <QPainterPath>
  26. #include <QTime>
  27. #include <QTimer>
  28. #include <QDebug>
  29. #include <QLineEdit>
  30. #include "tempomap.h"
  31. #include "../Controller.h"
  32. #include "../PluginInterfaces.h"
  33. const int cdef_cache_labels_num = 1000;
  34. const int cdef_cache_line_height = 12;
  35. const int cdef_cache_line_width = 100;
  36. const int cdef_lbl_draw_minwidth =
  37. 20; // minimal note width on screen in pixels to draw phoneme in it
  38. const int cdef_scroll_margin =
  39. 20; // px of space between edge of widget and target rect
  40. const double F_ROUNDER =
  41. 0.1; // because float is floor'ed to int by default, so 3.9999999 becomes 3
  42. qtauNoteEditor::qtauNoteEditor(QWidget *parent)
  43. : QWidget(parent),
  44. _bgCache(0),
  45. _delayingUpdate(false),
  46. _updateCalled(false),
  47. _lastCtrl(0) {
  48. setAttribute(Qt::WA_OpaquePaintEvent);
  49. setAttribute(Qt::WA_NoSystemBackground);
  50. setAutoFillBackground(false);
  51. setMouseTracking(true);
  52. setFocusPolicy(Qt::StrongFocus);
  53. _labelCache = new QPixmap(cdef_cache_line_width,
  54. cdef_cache_line_height * cdef_cache_labels_num);
  55. _labelCache->fill(Qt::transparent);
  56. _ctrl = new qtauEdController(*this, _setup, _notes, _state);
  57. _playLine = -1;
  58. // t.start();
  59. }
  60. qtauNoteEditor::~qtauNoteEditor() {
  61. if (_labelCache) delete _labelCache;
  62. if (_bgCache) delete _bgCache;
  63. if (_lastCtrl) delete _lastCtrl;
  64. if (_ctrl) delete _ctrl;
  65. }
  66. void qtauNoteEditor::configure(const SNoteSetup &newSetup) {
  67. _ctrl->reset();
  68. //FIXME gridChanged = newSetup.note != _setup.note || 1;
  69. _setup = newSetup;
  70. //if (gridChanged) {
  71. recalcNoteRects();
  72. updateBGCache();
  73. lazyUpdate();
  74. // }
  75. }
  76. void qtauNoteEditor::deleteSelected() {
  77. if (!_notes.selected.isEmpty()) {
  78. _ctrl->reset();
  79. _ctrl->deleteSelected();
  80. }
  81. }
  82. void qtauNoteEditor::onEvent(qtauEvent *e) { _ctrl->onEvent(e); }
  83. void qtauNoteEditor::lazyUpdate() {
  84. updateBGCache();
  85. update();
  86. }
  87. //--------------------------------------------------
  88. void qtauNoteEditor::recalcNoteRects() {
  89. _notes.grid.clear();
  90. //_setup.barWidth = _setup.note.width() * 4;//FIXME
  91. _setup.octHeight = _setup.note.height() * 12;
  92. double pulsesToPixels = (double)_setup.note.width() / c_midi_ppq;
  93. int startBar = 0, endBar = 0;
  94. _playLine = pulsesToPixels * _posPulses;
  95. foreach (quint64 key, _notes.idMap.keys()) {
  96. qne::editorNote &n = _notes.idMap[key];
  97. int nn1 = n.keyNumber + 1;
  98. if (n.keyNumber < 0 || n.keyNumber > 127) {
  99. DEVLOG_ERROR("invalid key number");
  100. return;
  101. }
  102. n.r.setRect((double)n.pulseOffset * pulsesToPixels + F_ROUNDER,
  103. ((_setup.baseOctave + _setup.numOctaves - 1) * 12 - nn1) *
  104. _setup.note.height(),
  105. (double)n.pulseLength * pulsesToPixels + F_ROUNDER,
  106. _setup.note.height());
  107. // determine bar(s) that have that note fully or partially
  108. startBar = _setup.getBarForScreenOffset(n.r.left());
  109. endBar = _setup.getBarForScreenOffset(n.r.right());
  110. if (endBar >= _notes.grid.size()) _notes.grid.resize(endBar + 10);
  111. if (startBar > 0 && startBar < _notes.grid.count())
  112. _notes.grid[startBar].append(n.id);
  113. else
  114. DEVLOG_DEBUG("ust too long");
  115. if (endBar != startBar) {
  116. if (endBar > 0 && endBar < _notes.grid.size())
  117. _notes.grid[endBar].append(n.id);
  118. else
  119. DEVLOG_DEBUG("ust to long");
  120. }
  121. }
  122. }
  123. void qtauNoteEditor::updateBGCache() {
  124. _setup.octHeight = _setup.note.height() * 12;
  125. int requiredCacheWidth = geometry().width(); //_setup.barScreenOffsets[_setup.numBars-1]
  126. //+ _setup.note.width() * 8;
  127. int requiredCacheHeight =
  128. geometry().height(); //;(geometry().height() / _setup.octHeight + 2) *
  129. //_setup.octHeight;
  130. if (!_bgCache || (_bgCache->width() < requiredCacheWidth ||
  131. _bgCache->height() < requiredCacheHeight)) {
  132. if (_bgCache) delete _bgCache;
  133. _bgCache = new QPixmap(requiredCacheWidth, requiredCacheHeight);
  134. }
  135. // prepare bg/grid data =========================================
  136. QPainterPath blacks; // rects for black keys
  137. QPainterPath outerLines;
  138. QPainterPath innerLines;
  139. // calculating indexes of visible notes of grid ----------------------
  140. int hSt = 0;
  141. int vSt = 0;
  142. int hEnd = requiredCacheWidth + 1;
  143. int vEnd = requiredCacheHeight - 1;
  144. int pxHOff = -_state.viewport.x();
  145. int pxVOff = -_state.viewport.y();
  146. int bar = 0;
  147. int beat = 0;
  148. int octCounter = 0;
  149. // horizontal pass to calc note & bar vertical delimiter lines
  150. // int hoffset = _state.viewport.x;
  151. // int startBar = _setup.getBarForScreenOffset(hoffset);
  152. do {
  153. fraction time = _setup.tmap->getTimeSignatureForBar(bar); // 1
  154. if (beat == time.numerator) {
  155. outerLines.moveTo(QPoint(pxHOff, vSt));
  156. outerLines.lineTo(QPoint(pxHOff, vEnd));
  157. beat = 1;
  158. bar++;
  159. time = _setup.tmap->getTimeSignatureForBar(bar); // 2
  160. } else {
  161. innerLines.moveTo(QPoint(pxHOff, vSt));
  162. innerLines.lineTo(QPoint(pxHOff, vEnd));
  163. beat++;
  164. }
  165. pxHOff += _setup.note.width() * 4.0 / time.denominator;
  166. } while (pxHOff <= hEnd);
  167. QRect noteBG(hSt, 0, hEnd - hSt, _setup.note.height());
  168. // vertical pass to calc note backgrounds, note & octave delimiter lines
  169. do {
  170. if (octCounter == 12) octCounter = 0;
  171. if (octCounter == 0) {
  172. outerLines.moveTo(QPoint(hSt, pxVOff));
  173. outerLines.lineTo(QPoint(hEnd, pxVOff));
  174. } else {
  175. innerLines.moveTo(QPoint(hSt, pxVOff));
  176. innerLines.lineTo(QPoint(hEnd, pxVOff));
  177. }
  178. //--- note bg's --------------
  179. noteBG.moveTop(pxVOff);
  180. if (octCounter == 1 || octCounter == 3 || octCounter == 5 ||
  181. octCounter == 8 || octCounter == 10)
  182. blacks.addRect(noteBG);
  183. //----------------------------
  184. octCounter++;
  185. pxVOff += _setup.note.height();
  186. } while (pxVOff <= vEnd);
  187. // paint 'em! ======================
  188. _bgCache->fill(Qt::white);
  189. QPainter p(_bgCache);
  190. QBrush brush(p.brush());
  191. p.setPen(Qt::NoPen);
  192. // background -------------
  193. if (!blacks.isEmpty()) {
  194. brush.setStyle(Qt::Dense6Pattern);
  195. brush.setColor(cdef_color_black_noteline_bg);
  196. p.setBrush(brush);
  197. p.drawPath(blacks);
  198. }
  199. p.setPen(Qt::SolidLine);
  200. // lines ------------------
  201. if (!innerLines.isEmpty()) {
  202. p.setPen(QColor(cdef_color_inner_line));
  203. p.drawPath(innerLines);
  204. }
  205. if (!outerLines.isEmpty()) {
  206. p.setPen(QColor(cdef_color_outer_line));
  207. p.drawPath(outerLines);
  208. }
  209. }
  210. void qtauNoteEditor::setVOffset(int voff) {
  211. if (voff != _state.viewport.y()) {
  212. _ctrl->reset();
  213. _state.viewport.moveTop(voff);
  214. lazyUpdate();
  215. }
  216. }
  217. void qtauNoteEditor::setHOffset(int hoff) {
  218. if (hoff != _state.viewport.x()) {
  219. _ctrl->reset();
  220. _state.viewport.moveLeft(hoff);
  221. lazyUpdate();
  222. }
  223. }
  224. QPoint qtauNoteEditor::scrollTo(const QRect &r) {
  225. QPoint result = _state.viewport.topLeft();
  226. if (r.x() < _state.viewport.x())
  227. result.setX(r.x() - cdef_scroll_margin);
  228. else if (r.x() > _state.viewport.x() + geometry().width() -
  229. cdef_cache_line_width - cdef_scroll_margin)
  230. result.setX(r.x() - geometry().width() / 2);
  231. if (r.y() < _state.viewport.y())
  232. result.setY(r.y() - cdef_scroll_margin);
  233. else if (r.y() >
  234. _state.viewport.y() + geometry().height() - cdef_scroll_margin)
  235. result.setY(r.y() - geometry().height() / 2);
  236. if (result != _state.viewport.topLeft()) emit requestsOffset(result);
  237. return result;
  238. }
  239. //----------------------------------------------------
  240. void qtauNoteEditor::paintEvent(QPaintEvent *event) {
  241. // lastUpdate = t.elapsed();
  242. emit repaintDynDrawer();
  243. // draw bg
  244. QRect r(event->rect());
  245. int hSt = r.x() + _state.viewport.x();
  246. int firstBar = _setup.getBarForScreenOffset(hSt);
  247. if (firstBar == -1) return;
  248. int vSt = r.y() + _state.viewport.y();
  249. int firstOct = vSt / _setup.octHeight;
  250. // QRect cacheRect(r);
  251. // cacheRect.moveTo(cacheRect.x() + cacheHOffset, cacheRect.y() +
  252. // cacheVOffset);
  253. QPainter p(this);
  254. p.drawPixmap(r, *_bgCache, r);
  255. // singing notes with phoneme labels -------
  256. int barSt = firstBar;
  257. int barEnd = _setup.getBarForScreenOffset(hSt + r.width());
  258. if (barEnd == -1) return;
  259. QPainterPath noteRects;
  260. QPainterPath selNoteRects;
  261. QPainterPath selNoteRectsRed;
  262. QPainterPath selNoteRectsGreen;
  263. QPainterPath noteRectsRed;
  264. QPainterPath noteRectsGreen;
  265. QMap<quint64, bool> processedIDMap;
  266. QVector<QPainter::PixmapFragment> cachedLabels;
  267. QPainter cacheP(_labelCache);
  268. cacheP.setBrush(Qt::white); // to clear pixmap completely
  269. p.translate(-_state.viewport.topLeft());
  270. if (barSt < _notes.grid.size()) {
  271. if (barEnd >= _notes.grid.size()) barEnd = _notes.grid.size() - 1;
  272. bool hasVisibleNotes = false;
  273. r.moveTo(_state.viewport.topLeft());
  274. for (int i = barSt; i < barEnd + 1; ++i)
  275. for (int k = 0; k < _notes.grid[i].size(); ++k) {
  276. qne::editorNote &n = _notes.idMap[_notes.grid[i][k]];
  277. if (!processedIDMap.contains(n.id) && r.intersects(n.r)) {
  278. hasVisibleNotes = true;
  279. if (n.selected) {
  280. if (n.color == EColor::green)
  281. selNoteRectsGreen.addRect(n.r);
  282. else if (n.color == EColor::red)
  283. selNoteRectsRed.addRect(n.r);
  284. else
  285. selNoteRects.addRect(n.r);
  286. } else {
  287. if (n.color == EColor::green)
  288. noteRectsGreen.addRect(n.r);
  289. else if (n.color == EColor::red)
  290. noteRectsRed.addRect(n.r);
  291. else
  292. noteRects.addRect(n.r);
  293. }
  294. if (n.r.width() >
  295. cdef_lbl_draw_minwidth) // don't draw labels for too narrow rects
  296. {
  297. QRectF fR(0, n.id * cdef_cache_line_height, cdef_cache_line_width,
  298. cdef_cache_line_height);
  299. if (!n.cached) {
  300. // clearing cache line
  301. cacheP.setCompositionMode(QPainter::CompositionMode_Clear);
  302. cacheP.drawRect(fR);
  303. cacheP.setCompositionMode(QPainter::CompositionMode_SourceOver);
  304. cacheP.drawText(fR, Qt::AlignLeft | Qt::AlignVCenter, n.txt);
  305. n.cached = true;
  306. }
  307. cachedLabels.append(QPainter::PixmapFragment::create(
  308. QPointF(n.r.x() + 55, n.r.y() + 7), fR)); // wtf is with pos?..
  309. }
  310. }
  311. processedIDMap[n.id] =
  312. true; // to avoid processing notes that go through 2 or more bars
  313. }
  314. if (hasVisibleNotes) {
  315. p.setPen(QColor(cdef_color_note_border));
  316. p.setBrush(QColor(cdef_color_note_bg_green));
  317. p.drawPath(noteRectsGreen);
  318. p.setPen(QColor(cdef_color_note_border));
  319. p.setBrush(QColor(cdef_color_note_bg_red));
  320. p.drawPath(noteRectsRed);
  321. p.setPen(QColor(cdef_color_note_border));
  322. p.setBrush(QColor(cdef_color_note_bg));
  323. p.drawPath(noteRects);
  324. p.setBrush(QColor(cdef_color_note_sel_bg));
  325. p.setPen(QColor(cdef_color_note_sel));
  326. p.drawPath(selNoteRects);
  327. p.setBrush(QColor(cdef_color_note_sel_bg_green));
  328. p.setPen(QColor(cdef_color_note_sel));
  329. p.drawPath(selNoteRectsGreen);
  330. p.setBrush(QColor(cdef_color_note_sel_bg_red));
  331. p.setPen(QColor(cdef_color_note_sel));
  332. p.drawPath(selNoteRectsRed);
  333. p.drawPixmapFragments(cachedLabels.data(), cachedLabels.size(),
  334. *_labelCache);
  335. }
  336. }
  337. if (_state.selectionRect.x() > -1 && _state.selectionRect.y() > -1) {
  338. QPen pen = p.pen();
  339. pen.setWidth(1);
  340. pen.setColor(QColor(cdef_color_selection_grey, cdef_color_selection_grey,
  341. cdef_color_selection_grey, cdef_color_selection_alpha));
  342. p.setPen(pen);
  343. p.setBrush(QBrush(
  344. QColor(cdef_color_selection_bg_grey, cdef_color_selection_bg_grey,
  345. cdef_color_selection_bg_grey, cdef_color_selection_bg_alpha)));
  346. p.drawRect(_state.selectionRect);
  347. }
  348. if (_state.snapLine > -1) {
  349. QPen pen = p.pen();
  350. pen.setWidth(1);
  351. pen.setColor(QColor(cdef_color_snap_line));
  352. p.setPen(pen);
  353. p.drawLine(_state.snapLine, vSt, _state.snapLine,
  354. vSt + _state.viewport.height());
  355. }
  356. #if 0
  357. //TODO: refactor this
  358. if (_playLine > -1)
  359. {
  360. QPen pen = p.pen();
  361. pen.setWidth(1);
  362. pen.setColor(QColor(0x00FF0000));
  363. p.setPen(pen);
  364. p.drawLine(_playLine, vSt, _playLine, vSt + _state.viewport.height());
  365. }
  366. #endif
  367. _updateCalled = false;
  368. }
  369. void qtauNoteEditor::resizeEvent(QResizeEvent *event) {
  370. _state.viewport.setSize(event->size());
  371. updateBGCache();
  372. emit heightChanged(_state.viewport.height());
  373. emit widthChanged(_state.viewport.width());
  374. }
  375. void qtauNoteEditor::mouseDoubleClickEvent(QMouseEvent *event) {
  376. _ctrl->mouseDoubleClickEvent(event);
  377. }
  378. void qtauNoteEditor::mouseMoveEvent(QMouseEvent *event) {
  379. _ctrl->mouseMoveEvent(event);
  380. }
  381. void qtauNoteEditor::mousePressEvent(QMouseEvent *event) {
  382. _ctrl->mousePressEvent(event);
  383. }
  384. void qtauNoteEditor::mouseReleaseEvent(QMouseEvent *event) {
  385. _ctrl->mouseReleaseEvent(event);
  386. }
  387. void qtauNoteEditor::wheelEvent(QWheelEvent *event) {
  388. if (event->modifiers() & Qt::ShiftModifier)
  389. emit hscrolled(event->delta());
  390. else if (event->modifiers() & Qt::ControlModifier)
  391. emit zoomed(event->delta());
  392. else
  393. emit vscrolled(event->delta());
  394. }
  395. void qtauNoteEditor::changeController(qtauEdController *c) {
  396. if (c) {
  397. if (_lastCtrl) delete _lastCtrl;
  398. if (_ctrl)
  399. _ctrl->cleanup(); // since we're not deleting last one (it's dangerous),
  400. // need to stop it if it isn't
  401. _lastCtrl = _ctrl;
  402. _ctrl = c;
  403. _ctrl->init();
  404. }
  405. }
  406. void qtauNoteEditor::rmbScrollHappened(const QPoint &delta,
  407. const QPoint &offset) {
  408. emit rmbScrolled(delta, offset);
  409. }
  410. void qtauNoteEditor::eventHappened(qtauEvent *e) { emit editorEvent(e); }
  411. void qtauNoteEditor::setPlaybackPosition(int pos) {
  412. _posPulses = pos;
  413. double pulsesToPixels = (double)_setup.note.width() / c_midi_ppq;
  414. _playLine = pulsesToPixels * _posPulses;
  415. lazyUpdate();
  416. }
  417. inline bool editorNotesComparison(const qne::editorNote *n1,
  418. const qne::editorNote *n2) {
  419. return n1->pulseOffset < n2->pulseOffset;
  420. }
  421. struct selectionRange qtauNoteEditor::getSelectionRange() {
  422. struct selectionRange sel;
  423. sel.end = -1;
  424. sel.start = -1;
  425. QVector<qne::editorNote *> ednotes;
  426. foreach (quint64 key, _notes.selected) {
  427. qne::editorNote &n = _notes.idMap[key];
  428. if (!ednotes.contains(&n)) ednotes.insert(0, &n);
  429. }
  430. qStableSort(ednotes.begin(), ednotes.end(), editorNotesComparison);
  431. foreach (qne::editorNote *n, ednotes) {
  432. if (n->selected) {
  433. if (sel.start == -1) sel.start = n->pulseOffset;
  434. sel.end = n->pulseOffset + n->pulseLength;
  435. }
  436. }
  437. return sel;
  438. }
  439. QVector<quint64> qtauNoteEditor::getSelection() {
  440. QVector<quint64> sel;
  441. foreach (quint64 key, _notes.selected) {
  442. qne::editorNote &n = _notes.idMap[key];
  443. if (n.selected) sel.push_back(key);
  444. }
  445. return sel;
  446. }
  447. void qtauNoteEditor::doPhonemeTransformation() {
  448. ISynth *s = qtauController::instance()->selectedSynth();
  449. if (s == nullptr) return;
  450. QVector<qne::editorNote *> ednotes;
  451. foreach (quint64 key, _notes.selected) {
  452. qne::editorNote &n = _notes.idMap[key];
  453. if (!ednotes.contains(&n)) ednotes.insert(0, &n);
  454. }
  455. if (ednotes.length() == 0) foreach (quint64 key, _notes.idMap.keys()) {
  456. qne::editorNote &n = _notes.idMap[key];
  457. if (!ednotes.contains(&n)) ednotes.insert(0, &n);
  458. }
  459. qStableSort(ednotes.begin(), ednotes.end(), editorNotesComparison);
  460. QStringList lyrics;
  461. foreach (qne::editorNote *n, ednotes) {
  462. lyrics.push_back(n->txt);
  463. }
  464. s->doPhonemeTransformation(lyrics);
  465. qtauEvent_NoteText::noteTextVector v;
  466. qtauEvent_NoteText::noteTextData d;
  467. int i = 0;
  468. foreach (qne::editorNote *n, ednotes) {
  469. d.id = n->id;
  470. d.txt = lyrics[i];
  471. n->cached = false;
  472. d.prevTxt = n->txt;
  473. n->txt = d.txt;
  474. v.append(d);
  475. i++;
  476. }
  477. qtauEvent_NoteText *e = new qtauEvent_NoteText(v);
  478. this->eventHappened(e);
  479. }
  480. void qtauNoteEditor::reset() {
  481. _notes.grid.clear();
  482. _notes.idMap.clear();
  483. }
  484. void qtauNoteEditor::setNoteColor(quint64 id, EColor color) {
  485. if (_notes.idMap.keys().contains(id)) _notes.idMap[id].color = color;
  486. }
  487. QList<quint64> qtauNoteEditor::getNoteIDs() { return _notes.idMap.keys(); }