Debian Codegraph @

BambooTracker/gui/instrument_editor/visualized_instrument_macro_editor.cpp

raw
1 /*
2 * Copyright (C) 2018-2021 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "visualized_instrument_macro_editor.hpp"
27 #include "ui_visualized_instrument_macro_editor.h"
28 #include <algorithm>
29 #include <numeric>
30 #include <utility>
31 #include <deque>
32 #include <unordered_map>
33 #include <QApplication>
34 #include <QFontMetrics>
35 #include <QPainter>
36 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
37 #include <QPointF>
38 #else
39 #include <QPoint>
40 #endif
41 #include <QRegularExpression>
42 #include <QRegularExpressionMatch>
43 #include "gui/event_guard.hpp"
44 #include "enum_hash.hpp"
45
46 VisualizedInstrumentMacroEditor::VisualizedInstrumentMacroEditor(QWidget *parent)
47 : QWidget(parent),
48 font_(QApplication::font()),
49 met_(font_),
50 maxDispRowCnt_(0),
51 upperRow_(-1),
52 defaultRow_(0),
53 hovRow_(-1),
54 hovCol_(-1),
55 type_(SequenceType::PlainSequence),
56 permittedReleaseType_(PermittedReleaseFlag::FIXED_RELEASE),
57 isLabelOmitted_(false),
58 release_(InstrumentSequenceRelease::NoRelease),
59 ui(new Ui::VisualizedInstrumentMacroEditor),
60 pressRow_(-1),
61 pressCol_(-1),
62 grabLoop_(-1),
63 isGrabLoopHead_(false),
64 isGrabRelease_(false),
65 isMultiReleaseState_(false),
66 mmlBase_(0),
67 isIgnoreEvent_(false)
68 {
69 ui->setupUi(this);
70
71 /* Font */
72 font_.setPointSize(10);
73 // Check font size
74 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
75 fontWidth_ = met_.horizontalAdvance('0');
76 #else
77 fontWidth_ = met_.width('0');
78 #endif
79 fontAscend_ = met_.ascent();
80 fontHeight_ = met_.height();
81 fontLeading_ = met_.leading();
82
83 /* Width & height */
84 autoFitLabelWidth();
85
86 ui->panel->setAttribute(Qt::WA_Hover);
87 ui->verticalScrollBar->setVisible(false);
88 ui->panel->installEventFilter(this);
89 }
90
91 VisualizedInstrumentMacroEditor::~VisualizedInstrumentMacroEditor()
92 {
93 delete ui;
94 }
95
96 void VisualizedInstrumentMacroEditor::setColorPalette(std::shared_ptr<ColorPalette> palette)
97 {
98 palette_ = palette;
99 }
100
101 void VisualizedInstrumentMacroEditor::AddRow(QString label, bool fitLabelWidth)
102 {
103 labels_.push_back(label);
104 if (static_cast<int>(labels_.size()) <= maxDispRowCnt_) {
105 upperRow_ = static_cast<int>(labels_.size()) - 1;
106 ui->verticalScrollBar->setVisible(false);
107 ui->verticalScrollBar->setMaximum(0);
108 }
109 else {
110 ui->verticalScrollBar->setVisible(true);
111 int max = static_cast<int>(labels_.size()) - maxDispRowCnt_;
112 ui->verticalScrollBar->setMaximum(max);
113 ui->verticalScrollBar->setValue(max);
114 }
115 if (fitLabelWidth) autoFitLabelWidth();
116 updateRowHeight();
117 }
118
119 void VisualizedInstrumentMacroEditor::setMaximumDisplayedRowCount(int count)
120 {
121 maxDispRowCnt_ = count;
122 if (static_cast<int>(labels_.size()) <= maxDispRowCnt_) {
123 upperRow_ = static_cast<int>(labels_.size()) - 1;
124 ui->verticalScrollBar->setVisible(false);
125 ui->verticalScrollBar->setMaximum(0);
126 }
127 else {
128 ui->verticalScrollBar->setVisible(true);
129 int max = static_cast<int>(labels_.size()) - maxDispRowCnt_;
130 ui->verticalScrollBar->setMaximum(max);
131 ui->verticalScrollBar->setValue(max);
132 }
133 updateRowHeight();
134 }
135
136 void VisualizedInstrumentMacroEditor::setDefaultRow(int row)
137 {
138 defaultRow_ = row;
139 }
140
141 int VisualizedInstrumentMacroEditor::getSequenceLength() const
142 {
143 return static_cast<int>(cols_.size());
144 }
145
146 void VisualizedInstrumentMacroEditor::setSequenceData(int row, int col, QString str, int subdata)
147 {
148 size_t idx = static_cast<size_t>(col);
149 cols_.at(idx).row = row;
150 cols_.at(idx).text = str;
151 cols_.at(idx).data = subdata;
152
153 ui->panel->update();
154
155 printMML();
156
157 emit sequenceDataChanged(row, col);
158 }
159
160 void VisualizedInstrumentMacroEditor::setText(int col, QString text)
161 {
162 cols_.at(static_cast<size_t>(col)).text = text;
163 }
164
165 void VisualizedInstrumentMacroEditor::setSubdata(int col, int subdata)
166 {
167 cols_.at(static_cast<size_t>(col)).data = subdata;
168 printMML();
169 }
170
171 int VisualizedInstrumentMacroEditor::getSequenceAt(int col) const
172 {
173 return cols_.at(static_cast<size_t>(col)).row;
174 }
175
176 int VisualizedInstrumentMacroEditor::getSequenceDataAt(int col) const
177 {
178 return cols_.at(static_cast<size_t>(col)).data;
179 }
180
181 void VisualizedInstrumentMacroEditor::setMultipleReleaseState(bool enabled)
182 {
183 isMultiReleaseState_ = enabled;
184 }
185
186 void VisualizedInstrumentMacroEditor::addSequenceData(int row, QString str, int subdata)
187 {
188 cols_.push_back({ row, subdata, str });
189
190 updateColumnWidth();
191 ui->panel->update();
192
193 ui->colSizeLabel->setText(tr("Size: %1").arg(cols_.size()));
194
195 printMML();
196
197 emit sequenceDataAdded(row, static_cast<int>(cols_.size()) - 1);
198 }
199
200 void VisualizedInstrumentMacroEditor::removeSequenceData()
201 {
202 if (cols_.size() == 1) return;
203
204 cols_.pop_back();
205
206 // Modify loop
207 for (size_t i = 0; i < loops_.size();) {
208 if (loops_[i].begin >= static_cast<int>(cols_.size())) {
209 loops_.erase(loops_.begin() + static_cast<int>(i));
210 }
211 else {
212 if (loops_[i].end >= static_cast<int>(cols_.size()))
213 loops_[i].end = static_cast<int>(cols_.size()) - 1;
214 ++i;
215 }
216 }
217
218 // Modify release
219 if (release_.getBeginPos() >= static_cast<int>(cols_.size()))
220 release_.disable();
221
222 updateColumnWidth();
223 ui->panel->update();
224
225 ui->colSizeLabel->setText(tr("Size: %1").arg(cols_.size()));
226
227 printMML();
228
229 emit sequenceDataRemoved();
230 }
231
232 void VisualizedInstrumentMacroEditor::addLoop(int begin, int end, int times)
233 {
234 int inx = 0;
235
236 for (size_t i = 0; i < loops_.size(); ++i) {
237 if (loops_[i].begin > begin) {
238 break;
239 }
240 ++inx;
241 }
242
243 loops_.insert(loops_.begin() + inx, { begin, end, times });
244
245 printMML();
246
247 emit loopAdded(InstrumentSequenceLoop(begin, end, times));
248 }
249
250 void VisualizedInstrumentMacroEditor::setSequenceType(SequenceType type)
251 {
252 type_ = type;
253
254 updateLabels();
255 printMML();
256 }
257
258 void VisualizedInstrumentMacroEditor::setPermittedReleaseTypes(int types)
259 {
260 permittedReleaseType_ = types;
261 }
262
263 void VisualizedInstrumentMacroEditor::setRelease(const InstrumentSequenceRelease& release)
264 {
265 release_ = release;
266 printMML();
267 }
268
269 void VisualizedInstrumentMacroEditor::clearData()
270 {
271 cols_.clear();
272 loops_.clear();
273 release_.disable();
274 updateColumnWidth();
275
276 printMML();
277 }
278
279 void VisualizedInstrumentMacroEditor::clearRow()
280 {
281 labels_.clear();
282 autoFitLabelWidth();
283 }
284
285 void VisualizedInstrumentMacroEditor::setUpperRow(int row)
286 {
287 upperRow_ = row;
288 int pos = upperRow_ + 1 - getDisplayedRowCount();
289 ui->panel->update();
290 ui->verticalScrollBar->setValue(ui->verticalScrollBar->maximum() - pos);
291 }
292
293 void VisualizedInstrumentMacroEditor::setLabel(int row, QString text)
294 {
295 labels_.at(static_cast<size_t>(row)) = text;
296 autoFitLabelWidth();
297 ui->panel->update();
298 }
299
300 void VisualizedInstrumentMacroEditor::clearAllLabelText()
301 {
302 std::fill(labels_.begin(), labels_.end(), "");
303 autoFitLabelWidth();
304 ui->panel->update();
305 }
306
307 void VisualizedInstrumentMacroEditor::setLabelDiaplayMode(bool isOmitted)
308 {
309 isLabelOmitted_ = isOmitted;
310 ui->panel->update();
311 }
312
313 void VisualizedInstrumentMacroEditor::autoFitLabelWidth()
314 {
315 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
316 int w = met_.horizontalAdvance(VisualizedInstrumentMacroEditor::tr("Release") + " ");
317 for (auto& lab : labels_) w = std::max(w, met_.horizontalAdvance(lab + " "));
318 #else
319 int w = met_.width(VisualizedInstrumentMacroEditor::tr("Release") + " ");
320 for (auto& lab : labels_) w = std::max(w, met_.width(lab + " "));
321 #endif
322 labWidth_ = w;
323 }
324
325 /******************************/
326 void VisualizedInstrumentMacroEditor::initDisplay()
327 {
328 pixmap_ = QPixmap(ui->panel->geometry().size());
329 }
330
331 void VisualizedInstrumentMacroEditor::drawField()
332 {
333 QPainter painter(&pixmap_);
334 painter.setFont(font_);
335
336 int textOffset = fontAscend_ - fontHeight_ + fontLeading_ / 2;
337
338 // Row label
339 painter.setPen(palette_->instSeqTagColor);
340 int dispCnt = getDisplayedRowCount();
341 if (isLabelOmitted_ && !labels_.empty()) {
342 painter.drawText(1,
343 rowHeights_.front() + textOffset,
344 labels_[static_cast<size_t>(upperRow_)]);
345 int c = dispCnt / 2;
346 painter.drawText(1,
347 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + c + 1, 0) + textOffset,
348 labels_[static_cast<size_t>(upperRow_ - c)]);
349 int l = dispCnt - 1;
350 painter.drawText(1,
351 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + l + 1, 0) + textOffset,
352 labels_[static_cast<size_t>(upperRow_ - l)]);
353 }
354 else {
355 for (int i = 0; i < dispCnt; ++i) {
356 painter.drawText(1,
357 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + i + 1, 0) + textOffset,
358 labels_[static_cast<size_t>(upperRow_ - i)]);
359 }
360 }
361
362 for (size_t i = 1; i < cols_.size(); i += 2) {
363 painter.fillRect(labWidth_
364 + std::accumulate(colWidths_.begin(), colWidths_.begin() + static_cast<int>(i), 0),
365 0,
366 colWidths_[i], fieldHeight_,
367 palette_->instSeqOddColColor);
368 }
369
370 // Sequence
371 painter.setPen(palette_->instSeqCellTextColor);
372 for (size_t i = 0; i < cols_.size(); ++i) {
373 if (upperRow_ >= cols_[i].row && cols_[i].row > upperRow_ - dispCnt) {
374 int x = labWidth_ + std::accumulate(colWidths_.begin(), colWidths_.begin() + static_cast<int>(i), 0);
375 int hi = upperRow_ - cols_[i].row;
376 int y = std::accumulate(rowHeights_.begin(), rowHeights_.begin() + hi, 0);
377 painter.fillRect(x, y, colWidths_[i],
378 rowHeights_[static_cast<size_t>(hi)], palette_->instSeqCellColor);
379 painter.drawText(x + 2,
380 y + rowHeights_[static_cast<size_t>(upperRow_ - cols_[i].row)] + textOffset,
381 cols_[i].text);
382 }
383 }
384
385 if (hovCol_ >= 0 && hovRow_ >= 0) {
386 painter.fillRect(labWidth_ + std::accumulate(colWidths_.begin(), colWidths_.begin() + hovCol_, 0),
387 std::accumulate(rowHeights_.begin(), rowHeights_.begin() + hovRow_, 0),
388 colWidths_[static_cast<size_t>(hovCol_)], rowHeights_[static_cast<size_t>(hovRow_)],
389 palette_->instSeqHovColor);
390 }
391 }
392
393 void VisualizedInstrumentMacroEditor::drawLoop()
394 {
395 QPainter painter(&pixmap_);
396 painter.setFont(font_);
397
398 painter.fillRect(0, loopY_, panelWidth(), fontHeight_, palette_->instSeqLoopBackColor);
399 painter.setPen(palette_->instSeqLoopTextColor);
400 painter.drawText(1, loopBaseY_, tr("Loop"));
401
402 int w = labWidth_;
403 int seqLen = static_cast<int>(cols_.size());
404 for (int i = 0; i < seqLen; ++i) {
405 for (size_t j = 0; j < loops_.size(); ++j) {
406 if (loops_[j].begin <= i && i <= loops_[j].end) {
407 int colWidth = colWidths_[static_cast<size_t>(i)];
408 painter.fillRect(w, loopY_, colWidth, fontHeight_, palette_->instSeqLoopColor);
409 if (loops_[j].begin == i) {
410 painter.fillRect(w, loopY_, 2, fontHeight_, palette_->instSeqLoopEdgeColor);
411 QString times = (loops_[j].times == 1) ? "" : QString::number(loops_[j].times);
412 painter.drawText(QRectF(w + 2, loopY_, colWidth - 2, fontHeight_),
413 Qt::AlignLeft | Qt::AlignVCenter, tr("Loop %1").arg(times));
414 }
415 if (loops_[j].end == i) {
416 painter.fillRect(w + colWidth - 2, loopY_, 2,
417 fontHeight_, palette_->instSeqLoopEdgeColor);
418 }
419 }
420 }
421
422 if (hovRow_ == -2 && hovCol_ == i)
423 painter.fillRect(w, loopY_, colWidths_[static_cast<size_t>(i)],
424 fontHeight_, palette_->instSeqHovColor);
425
426 w += colWidths_[static_cast<size_t>(i)];
427 }
428 }
429
430 void VisualizedInstrumentMacroEditor::drawRelease()
431 {
432 QPainter painter(&pixmap_);
433 painter.setFont(font_);
434
435 painter.fillRect(0, releaseY_, panelWidth(), fontHeight_, palette_->instSeqReleaseBackColor);
436 painter.setPen(palette_->instSeqReleaseTextColor);
437 painter.drawText(1, releaseBaseY_, tr("Release"));
438
439 static const std::unordered_map<InstrumentSequenceRelease::ReleaseType, QString> DISP_TEXT_MAP = {
440 { InstrumentSequenceRelease::NoRelease, "" },
441 { InstrumentSequenceRelease::FixedRelease, tr("Fixed") },
442 { InstrumentSequenceRelease::AbsoluteRelease, tr("Absolute") },
443 { InstrumentSequenceRelease::RelativeRelease, tr("Relative") }
444 };
445
446 int w = labWidth_;
447 int seqLen = static_cast<int>(cols_.size());
448 for (int i = 0; i < seqLen; ++i) {
449 if (release_.getBeginPos() == i) {
450 painter.fillRect(w, releaseY_, panelWidth() - w, fontHeight_, palette_->instSeqReleaseColor);
451 painter.fillRect(w, releaseY_, 2, fontHeight_, palette_->instSeqReleaseEdgeColor);
452 painter.setPen(palette_->instSeqReleaseTextColor);
453 painter.drawText(w + 2, releaseBaseY_, DISP_TEXT_MAP.at(release_.getType()));
454 }
455
456 if (hovRow_ == -3 && hovCol_ == i)
457 painter.fillRect(w, releaseY_, colWidths_[static_cast<size_t>(i)],
458 fontHeight_, palette_->instSeqHovColor);
459
460 w += colWidths_[static_cast<size_t>(i)];
461 }
462 }
463
464 void VisualizedInstrumentMacroEditor::drawBorder()
465 {
466 QPainter painter(&pixmap_);
467 painter.setPen(palette_->instSeqBorderColor);
468 painter.drawLine(labWidth_, 0, labWidth_, ui->panel->geometry().height());
469 for (int i = 1; i < maxDispRowCnt_; ++i) {
470 painter.drawLine(labWidth_, std::accumulate(rowHeights_.begin(), rowHeights_.begin() + i, 0),
471 panelWidth(), std::accumulate(rowHeights_.begin(), rowHeights_.begin() + i, 0));
472 }
473 }
474
475 void VisualizedInstrumentMacroEditor::drawShadow()
476 {
477 QPainter painter(&pixmap_);
478 painter.fillRect(0, 0, panelWidth(), ui->panel->geometry().height(), palette_->instSeqMaskColor);
479 }
480
481 void VisualizedInstrumentMacroEditor::updateLabels()
482 {
483 }
484
485 void VisualizedInstrumentMacroEditor::printMML()
486 {
487 if (cols_.empty()) return;
488
489 QString text = "";
490 std::vector<Loop> lstack;
491
492 static const std::unordered_map<InstrumentSequenceRelease::ReleaseType, QString> DISP_REL_MAP = {
493 { InstrumentSequenceRelease::NoRelease, "" },
494 { InstrumentSequenceRelease::FixedRelease, "| " },
495 { InstrumentSequenceRelease::AbsoluteRelease, "/ " },
496 { InstrumentSequenceRelease::RelativeRelease, ": " }
497 };
498
499 int seqLen = static_cast<int>(cols_.size());
500 for (int cnt = 0; cnt < seqLen; ++cnt) {
501 if (release_.getBeginPos() == cnt) {
502 text += DISP_REL_MAP.at(release_.getType());
503 }
504
505 for (size_t i = 0; i < loops_.size(); ++i) {
506 if (loops_[i].begin == cnt) {
507 lstack.push_back(loops_[i]);
508 text += "[ ";
509 }
510 else if (loops_[i].begin > cnt) {
511 break;
512 }
513 }
514
515 text += (convertSequenceDataUnitToMML(cols_[static_cast<size_t>(cnt)]) + " ");
516
517 while (!lstack.empty()) {
518 if (lstack.back().end == cnt) {
519 text += "]";
520 if (lstack.back().times > 1) {
521 text += QString::number(lstack.back().times);
522 }
523 text += " ";
524 lstack.pop_back();
525 }
526 else {
527 break;
528 }
529 }
530 }
531
532 ui->lineEdit->setText(text);
533 }
534
535 QString VisualizedInstrumentMacroEditor::convertSequenceDataUnitToMML(Column col)
536 {
537 return QString::number(col.row + mmlBase_);
538 }
539
540 void VisualizedInstrumentMacroEditor::interpretMML()
541 {
542 if (cols_.empty()) return;
543
544 QString text = ui->lineEdit->text();
545
546 std::vector<Column> column;
547 std::vector<Loop> loop;
548 InstrumentSequenceLoopRoot loopRoot(1);
549
550 std::deque<int> lbstack;
551 std::deque<InstrumentSequenceLoop> loopStack;
552 InstrumentSequenceRelease release(InstrumentSequenceRelease::NoRelease);
553 std::vector<size_t> lstack;
554
555 int cnt = 0;
556 while (!text.isEmpty()) {
557
558 QRegularExpressionMatch m = QRegularExpression("^\\[").match(text);
559 if (m.hasMatch()) {
560 loop.push_back({ cnt, cnt, 1 });
561 lstack.push_back(loop.size() - 1);
562 lbstack.push_back(cnt);
563 text.remove(QRegularExpression("^\\["));
564 continue;
565 }
566
567 m = QRegularExpression("^\\](\\d*)").match(text);
568 if (m.hasMatch()) {
569 if (lstack.empty() || cnt == 0) return;
570 loop[lstack.back()].end = cnt - 1;
571 if (!m.captured(1).isEmpty()) {
572 int t = m.captured(1).toInt();
573 if (t > 1) loop[lstack.back()].times = t;
574 else return;
575 }
576 lstack.pop_back();
577 if (lbstack.empty() || lbstack.back() == cnt) return;
578 if (!m.captured(1).isEmpty()) {
579 int t = m.captured(1).toInt();
580 if (t > 1) loopStack.push_back(InstrumentSequenceLoop(lbstack.back(), cnt - 1, t));
581 else return;
582 }
583 else {
584 loopStack.push_back(InstrumentSequenceLoop(lbstack.back(), cnt - 1));
585 }
586 lbstack.pop_back();
587 text.remove(QRegularExpression("^\\]\\d*"));
588 continue;
589 }
590
591 if (permittedReleaseType_ & PermittedReleaseFlag::FIXED_RELEASE) {
592 m = QRegularExpression("^\\|").match(text);
593 if (m.hasMatch()) {
594 if (release.isEnabled()) return;
595 release.setType(InstrumentSequenceRelease::FixedRelease);
596 release.setBeginPos(cnt);
597 text.remove(QRegularExpression("^\\|"));
598 continue;
599 }
600 }
601
602 if (permittedReleaseType_ & PermittedReleaseFlag::ABSOLUTE_RELEASE) {
603 m = QRegularExpression("^/").match(text);
604 if (m.hasMatch()) {
605 if (release.isEnabled()) return;
606 release.setType(InstrumentSequenceRelease::AbsoluteRelease);
607 release.setBeginPos(cnt);
608 text.remove(QRegularExpression("^/"));
609 continue;
610 }
611 }
612
613 if (permittedReleaseType_ & PermittedReleaseFlag::RELATIVE_RELEASE) {
614 m = QRegularExpression("^:").match(text);
615 if (m.hasMatch()) {
616 if (release.isEnabled()) return;
617 release.setType(InstrumentSequenceRelease::RelativeRelease);
618 release.setBeginPos(cnt);
619 text.remove(QRegularExpression("^:"));
620 continue;
621 }
622 }
623
624 if (interpretSlopeInMML(text, cnt, column)) { // Slope
625 loopRoot.resize(cnt);
626 continue;
627 }
628
629 if (interpretDataInMML(text, cnt, column)) { // Data
630 loopRoot.resize(cnt);
631 continue;
632 }
633
634 m = QRegularExpression("^ +").match(text);
635 if (m.hasMatch()) {
636 text.remove(QRegularExpression("^ +"));
637 continue;
638 }
639
640 return;
641 }
642
643 if (column.empty()) return;
644 if (!lstack.empty()) return;
645 if (!lbstack.empty()) return;
646 if (release.isEnabled() && release.getBeginPos() >= static_cast<int>(column.size())) return;
647
648 while (cols_.size() > 1) removeSequenceData();
649 setSequenceData(column.front().row, 0);
650 for (size_t i = 1; i < column.size(); ++i) {
651 addSequenceData(column[i].row);
652 }
653
654 loops_ = loop;
655 emit loopCleared();
656 // Reverse order because deepest is first in
657 for (auto itr = loopStack.crbegin(); itr != loopStack.crend(); ++itr) {
658 emit loopAdded(*itr);
659 }
660
661 release_ = release;
662 emit releaseChanged(release);
663
664 ui->panel->update();
665 }
666
667 bool VisualizedInstrumentMacroEditor::interpretSlopeInMML(QString& text, int& cnt, std::vector<Column>& column)
668 {
669 QRegularExpressionMatch m = QRegularExpression("^(?<start>-?\\d+) *_ *(?<end>-?\\d+)( *_ *(?<diff>-?\\d+))?").match(text);
670 if (m.hasMatch()) {
671 int start = m.captured("start").toInt();
672 int end = m.captured("end").toInt();
673 QString diffStr = m.captured("diff");
674 if (start < 0 || maxInMML() <= start || end < 0 || maxInMML() <= end || start == end)
675 return false;
676 int diff;
677 int d = start;
678 if (start < end) {
679 if (diffStr.isEmpty()) {
680 diff = 1;
681 }
682 else {
683 diff = diffStr.toInt();
684 if (diff <= 0) return false;
685 }
686 for (bool flag = true; flag ; ) {
687 if (end <= d) {
688 flag = false;
689 d = end;
690 }
691 column.push_back({ d, -1, "" });
692 d += diff;
693 ++cnt;
694 }
695 }
696 else {
697 if (diffStr.isEmpty()) {
698 diff = -1;
699 }
700 else {
701 diff = diffStr.toInt();
702 if (0 <= diff) return false;
703 }
704 for (bool flag = true; flag ; ) {
705 if (d <= end) {
706 flag = false;
707 d = end;
708 }
709 column.push_back({ d, -1, "" });
710 d += diff;
711 ++cnt;
712 }
713 }
714 text.remove(QRegularExpression("^-?\\d+ *_ *-?\\d+( *_ *-?\\d+)?"));
715 return true;
716 }
717 return false;
718 }
719
720 bool VisualizedInstrumentMacroEditor::interpretDataInMML(QString& text, int& cnt, std::vector<Column>& column)
721 {
722 QRegularExpressionMatch m = QRegularExpression("^(-?\\d+)").match(text);
723 if (m.hasMatch()) {
724 int d = m.captured(1).toInt() - mmlBase_;
725 if (d < 0 || maxInMML() <= d) return false;
726 column.push_back({ d, -1, "" });
727 ++cnt;
728 text.remove(QRegularExpression("^-?\\d+"));
729 return true;
730 }
731 return false;
732 }
733
734 int VisualizedInstrumentMacroEditor::checkLoopRegion(int col)
735 {
736 int ret = -1;
737
738 for (size_t i = 0; i < loops_.size(); ++i) {
739 if (loops_[i].begin <= col) {
740 if (loops_[i].end >= col) {
741 ret = static_cast<int>(i);
742 }
743 }
744 else {
745 break;
746 }
747 }
748
749 return ret;
750 }
751
752 void VisualizedInstrumentMacroEditor::moveLoop()
753 {
754 if (hovCol_ < 0) return;
755
756 size_t glabIdxU = static_cast<size_t>(grabLoop_);
757 auto& tgtLoopRef = loops_[glabIdxU];
758 int prevBegin = tgtLoopRef.begin;
759 int prevEnd = tgtLoopRef.end;
760
761 if (isGrabLoopHead_) {
762 if (hovCol_ < prevBegin) {
763 if (glabIdxU && hovCol_ <= loops_[glabIdxU - 1].end) {
764 tgtLoopRef.begin = loops_[glabIdxU - 1].end + 1;
765 }
766 else {
767 tgtLoopRef.begin = hovCol_;
768 }
769 }
770 else if (prevBegin < hovCol_) {
771 if (prevEnd < hovCol_) {
772 loops_.erase(loops_.begin() + grabLoop_);
773 emit loopRemoved(prevBegin, prevEnd);
774 printMML();
775 return;
776 }
777 else {
778 tgtLoopRef.begin = hovCol_;
779 }
780 }
781 else return;
782 }
783 else {
784 if (hovCol_ < prevEnd) {
785 if (hovCol_ < prevBegin) {
786 loops_.erase(loops_.begin() + grabLoop_);
787 emit loopRemoved(prevBegin, prevEnd);
788 printMML();
789 return;
790 }
791 else {
792 tgtLoopRef.end = hovCol_;
793 }
794 }
795 else if (prevEnd < hovCol_) {
796 if (glabIdxU < loops_.size() - 1 && loops_[glabIdxU + 1].begin <= hovCol_) {
797 tgtLoopRef.end = loops_[glabIdxU + 1].begin - 1;
798 }
799 else {
800 tgtLoopRef.end = hovCol_;
801 }
802 }
803 else return;
804 }
805
806 emit loopChanged(prevBegin, prevEnd,
807 InstrumentSequenceLoop(tgtLoopRef.begin, tgtLoopRef.end, tgtLoopRef.times));
808 printMML();
809 }
810
811 void VisualizedInstrumentMacroEditor::setMMLDisplay0As(int n)
812 {
813 mmlBase_ = n;
814 }
815
816 int VisualizedInstrumentMacroEditor::panelWidth() const
817 {
818 return ui->panel->geometry().width();
819 }
820
821 /********** Events **********/
822 bool VisualizedInstrumentMacroEditor::eventFilter(QObject*object, QEvent* event)
823 {
824 if (object == ui->panel) {
825 switch (event->type()) {
826 case QEvent::Paint:
827 paintEventInView(dynamic_cast<QPaintEvent*>(event));
828 return false;
829 case QEvent::Resize:
830 resizeEventInView(dynamic_cast<QResizeEvent*>(event));
831 return false;
832 case QEvent::MouseButtonPress:
833 if (isEnabled())
834 mousePressEventInView(dynamic_cast<QMouseEvent*>(event));
835 return false;
836 case QEvent::MouseButtonDblClick:
837 if (isEnabled())
838 mousePressEventInView(dynamic_cast<QMouseEvent*>(event));
839 return false;
840 case QEvent::MouseButtonRelease:
841 if (isEnabled())
842 mouseReleaseEventInView(dynamic_cast<QMouseEvent*>(event));
843 return false;
844 case QEvent::MouseMove:
845 if (isEnabled())
846 mouseMoveEventInView();
847 return true;
848 case QEvent::HoverMove:
849 mouseHoverdEventInView(dynamic_cast<QHoverEvent*>(event));
850 return false;
851 case QEvent::Leave:
852 leaveEventInView();
853 return false;
854 case QEvent::Wheel:
855 wheelEventInView(dynamic_cast<QWheelEvent*>(event));
856 return false;
857 default:
858 return false;
859 }
860 }
861
862 return QWidget::eventFilter(object, event);
863 }
864
865 void VisualizedInstrumentMacroEditor::paintEventInView(QPaintEvent* event)
866 {
867 if (!palette_) return;
868
869 pixmap_.fill(Qt::black);
870
871 drawField();
872
873 drawLoop();
874 drawRelease();
875 drawBorder();
876 if (!isEnabled()) drawShadow();
877
878 QPainter painter(ui->panel);
879 painter.drawPixmap(event->rect(), pixmap_, event->rect());
880 }
881
882 void VisualizedInstrumentMacroEditor::resizeEventInView(QResizeEvent* event)
883 {
884 Q_UNUSED(event)
885
886 updateRowHeight();
887 updateColumnWidth();
888
889 releaseY_ = ui->panel->geometry().height() - fontHeight_;
890 releaseBaseY_ = releaseY_ + fontAscend_ + fontLeading_ / 2;
891 loopY_ = releaseY_ - fontHeight_;
892 loopBaseY_ = releaseBaseY_ - fontHeight_;
893
894 fieldHeight_ = loopY_;
895
896 initDisplay();
897 }
898
899 int VisualizedInstrumentMacroEditor::detectRowNumberForMouseEvent(int col, int internalRow) const
900 {
901 Q_UNUSED(col)
902 return upperRow_ - internalRow;
903 }
904
905 void VisualizedInstrumentMacroEditor::mousePressEventInView(QMouseEvent* event)
906 {
907 if (!cols_.size()) return;
908
909 pressRow_ = hovRow_;
910 pressCol_ = hovCol_;
911
912 // Check grab
913 int x = event->pos().x();
914 if (hovRow_ == -2) {
915 if (event->button() == Qt::LeftButton) {
916 int seqLen = static_cast<int>(cols_.size());
917 for (int col = 0, w = labWidth_; col < seqLen; ++col) {
918 if (w - 4 < x && x < w + 4) {
919 for (size_t i = 0; i < loops_.size(); ++i) {
920 if (loops_[i].begin == col) {
921 grabLoop_ = static_cast<int>(i);
922 isGrabLoopHead_ = true;
923 }
924 else if (loops_[i].begin > col) {
925 break;
926 }
927 }
928 }
929 else if (w + colWidths_[static_cast<size_t>(col)] - 4 < x
930 && x < w + colWidths_[static_cast<size_t>(col)] + 4) {
931 for (size_t i = 0; i < loops_.size(); ++i) {
932 if (loops_[i].end == col) {
933 grabLoop_ = static_cast<int>(i);
934 isGrabLoopHead_ = false;
935 }
936 else if (loops_[i].end > col) {
937 break;
938 }
939 }
940 }
941 w += colWidths_[static_cast<size_t>(col)];
942 }
943 }
944 }
945 else if (hovRow_ == -3 && release_.isEnabled()) {
946 if (event->button() == Qt::LeftButton) {
947 int w = labWidth_ + std::accumulate(colWidths_.begin(), colWidths_.begin() + release_.getBeginPos(), 0);
948 if (w - 4 < x && x < w + 4) {
949 isGrabRelease_ = true;
950 }
951 }
952 }
953
954 // Press process
955 if (pressCol_ > -1) {
956 if (pressRow_ == -2) {
957 if (grabLoop_ == -1) {
958 int i = checkLoopRegion(pressCol_);
959 switch (event->button()) {
960 case Qt::LeftButton:
961 {
962 if (i == -1) { // New loop
963 addLoop(pressCol_, pressCol_, 1);
964 }
965 else { // Loop count up
966 ++loops_[static_cast<size_t>(i)].times;
967 printMML();
968 auto& l = loops_[static_cast<size_t>(i)];
969 emit loopChanged(l.begin, l.end, InstrumentSequenceLoop(l.begin, l.end, l.times));
970 }
971 break;
972 }
973 case Qt::RightButton:
974 {
975 if (i > -1) { // Loop count down
976 if (loops_[static_cast<size_t>(i)].times > 1) {
977 --loops_[static_cast<size_t>(i)].times;
978 auto& l = loops_[static_cast<size_t>(i)];
979 emit loopChanged(l.begin, l.end, InstrumentSequenceLoop(l.begin, l.end, l.times));
980 }
981 else { // Erase loop
982 auto& l = loops_[static_cast<size_t>(i)];
983 emit loopRemoved(l.begin, l.end);
984 loops_.erase(loops_.begin() + i);
985 }
986 printMML();
987 }
988 break;
989 }
990 default:
991 break;
992 }
993 }
994 }
995 else if (pressRow_ == -3) {
996 if (!isGrabRelease_) {
997 switch (event->button()) {
998 case Qt::LeftButton:
999 {
1000 if (!release_.isEnabled() || pressCol_ < release_.getBeginPos()) { // New release
1001 if (!release_.isEnabled()) release_.setType(InstrumentSequenceRelease::FixedRelease);
1002 release_.setBeginPos(pressCol_);
1003 printMML();
1004 emit releaseChanged(release_);
1005 }
1006 else if (isMultiReleaseState_) { // Change release type
1007 switch (release_.getType()) {
1008 case InstrumentSequenceRelease::FixedRelease:
1009 release_.setType(InstrumentSequenceRelease::AbsoluteRelease);
1010 break;
1011 case InstrumentSequenceRelease::AbsoluteRelease:
1012 release_.setType(InstrumentSequenceRelease::RelativeRelease);
1013 break;
1014 case InstrumentSequenceRelease::NoRelease:
1015 case InstrumentSequenceRelease::RelativeRelease:
1016 release_.setType(InstrumentSequenceRelease::FixedRelease);
1017 break;
1018 }
1019 printMML();
1020 emit releaseChanged(release_);
1021 }
1022 break;
1023 }
1024 case Qt::RightButton:
1025 {
1026 if (pressCol_ >= release_.getBeginPos()) { // Erase release
1027 release_.disable();
1028 printMML();
1029 emit releaseChanged(release_);
1030 }
1031 break;
1032 }
1033 default:
1034 break;
1035 }
1036 }
1037 }
1038 else {
1039 setSequenceData(detectRowNumberForMouseEvent(hovCol_, hovRow_), hovCol_);
1040 prevPressRow_ = hovRow_;
1041 prevPressCol_ = hovCol_;
1042 }
1043 }
1044
1045 ui->panel->update();
1046 }
1047
1048 void VisualizedInstrumentMacroEditor::mouseReleaseEventInView(QMouseEvent* event)
1049 {
1050 if (!cols_.size()) return;
1051
1052 if (grabLoop_ != -1) { // Move loop
1053 if (event->button() == Qt::LeftButton) {
1054 moveLoop();
1055 }
1056 }
1057 else if (isGrabRelease_) { // Move release
1058 if (event->button() == Qt::LeftButton) {
1059 if (hovCol_ > -1) {
1060 release_.setBeginPos(hovCol_);
1061 printMML();
1062 emit releaseChanged(release_);
1063 }
1064 }
1065 }
1066
1067 pressRow_ = -1;
1068 pressCol_ = -1;
1069 prevPressRow_ = -1;
1070 prevPressCol_ = -1;
1071 grabLoop_ = -1;
1072 isGrabLoopHead_ = false;
1073 isGrabRelease_ = false;
1074
1075 ui->panel->update();
1076 }
1077
1078 void VisualizedInstrumentMacroEditor::mouseMoveEventInView()
1079 {
1080 if (!cols_.size()) return;
1081
1082 if (pressRow_ >= 0 && pressCol_ >= 0 && hovRow_ >= 0 && hovCol_ >= 0) {
1083 if (prevPressRow_ != hovRow_ || prevPressCol_ != hovCol_) {
1084 prevPressRow_ = hovRow_;
1085 prevPressCol_ = hovCol_;
1086 setSequenceData(detectRowNumberForMouseEvent(hovCol_, hovRow_), hovCol_);
1087 }
1088 }
1089 }
1090
1091 void VisualizedInstrumentMacroEditor::mouseHoverdEventInView(QHoverEvent* event)
1092 {
1093 if (!cols_.size()) return;
1094
1095 int oldCol = hovCol_;
1096 int oldRow = hovRow_;
1097
1098 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1099 QPointF pos = event->position();
1100 #else
1101 QPoint pos = event->pos();
1102 #endif
1103
1104 // Detect column
1105 if (pos.x() < labWidth_) {
1106 hovCol_ = -2;
1107 }
1108 else {
1109 int seqLen = static_cast<int>(cols_.size());
1110 for (int i = 0, w = labWidth_; i < seqLen; ++i) {
1111 w += colWidths_[static_cast<size_t>(i)];
1112 if (pos.x() < w) {
1113 hovCol_ = i;
1114 break;
1115 }
1116 }
1117 if (hovCol_ >= seqLen) hovCol_ = -1; // Out of range
1118 }
1119
1120 // Detect row
1121 if (releaseY_ < pos.y()) {
1122 hovRow_ = -3;
1123 }
1124 else if (loopY_ < pos.y()) {
1125 hovRow_ = -2;
1126 }
1127 else {
1128 int cnt = getDisplayedRowCount();
1129 for (int i = 0, w = 0; i < cnt; ++i) {
1130 w += rowHeights_[static_cast<size_t>(i)];
1131 if (pos.y() < w) {
1132 hovRow_ = i;
1133 break;
1134 }
1135 }
1136 }
1137
1138 if (hovRow_ != oldRow || hovCol_ != oldCol) ui->panel->update();
1139 }
1140
1141 void VisualizedInstrumentMacroEditor::leaveEventInView()
1142 {
1143 hovRow_ = -1;
1144 hovCol_ = -1;
1145
1146 ui->panel->update();
1147 }
1148
1149 void VisualizedInstrumentMacroEditor::wheelEventInView(QWheelEvent* event)
1150 {
1151 if (!cols_.size()) return;
1152
1153 Ui::EventGuard eg(isIgnoreEvent_);
1154 int degree = - event->angleDelta().y() / 8;
1155 int pos = ui->verticalScrollBar->value() + degree / 15;
1156 int labCnt = static_cast<int>(labels_.size());
1157 if (0 > pos) pos = 0;
1158 else if (pos > labCnt - maxDispRowCnt_) pos = labCnt - maxDispRowCnt_;
1159 scrollUp(ui->verticalScrollBar->maximum() - pos);
1160 ui->panel->update();
1161 ui->verticalScrollBar->setValue(pos);
1162 }
1163
1164 void VisualizedInstrumentMacroEditor::on_colIncrToolButton_clicked()
1165 {
1166 addSequenceData(defaultRow_);
1167 }
1168
1169 void VisualizedInstrumentMacroEditor::on_colDecrToolButton_clicked()
1170 {
1171 removeSequenceData();
1172 }
1173
1174 void VisualizedInstrumentMacroEditor::on_verticalScrollBar_valueChanged(int value)
1175 {
1176 if (!isIgnoreEvent_) {
1177 scrollUp(ui->verticalScrollBar->maximum() - value);
1178 ui->panel->update();
1179 }
1180 }
1181
1182 int VisualizedInstrumentMacroEditor::maxInMML() const
1183 {
1184 return static_cast<int>(labels_.size());
1185 }
1186
1187 void VisualizedInstrumentMacroEditor::on_lineEdit_editingFinished()
1188 {
1189 interpretMML();
1190 printMML();
1191 }
1192
1193 void VisualizedInstrumentMacroEditor::updateColumnWidth()
1194 {
1195 colWidths_.clear();
1196
1197 if (!cols_.size()) return;
1198
1199 float ww = (panelWidth() - labWidth_) / static_cast<float>(cols_.size());
1200 int w = static_cast<int>(ww);
1201 float dif = ww - w;
1202 float sum = 0;
1203 for (size_t i = 0; i < cols_.size(); ++i) {
1204 int width = w;
1205 sum += dif;
1206 if (sum >= 1.0f) {
1207 ++width;
1208 sum -= 1.0f;
1209 }
1210 colWidths_.push_back(width);
1211 }
1212 }
1213
1214 void VisualizedInstrumentMacroEditor::updateRowHeight()
1215 {
1216 rowHeights_.clear();
1217
1218 if (!labels_.size()) return;
1219
1220 int div = getDisplayedRowCount();
1221 float hh = (ui->panel->geometry().height() - fontHeight_ * 2) / static_cast<float>(div);
1222 int h = static_cast<int>(hh);
1223 float dif = hh - h;
1224 float sum = 0;
1225
1226 for (int i = 0; i < div; ++i) {
1227 int height = h;
1228 sum += dif;
1229 if (sum >= 1.0f) {
1230 ++height;
1231 sum -= 1.0f;
1232 }
1233 rowHeights_.push_back(height);
1234 }
1235 }