Debian Codegraph @

BambooTracker/gui/instrument_editor/arpeggio_macro_editor.cpp

raw
1 /*
2 * Copyright (C) 2019-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 "arpeggio_macro_editor.hpp"
27 #include <QRegularExpression>
28 #include "note.hpp"
29 #include "gui/note_name_manager.hpp"
30
31 ArpeggioMacroEditor::ArpeggioMacroEditor(QWidget* parent)
32 : VisualizedInstrumentMacroEditor(parent)
33 {
34 constexpr int MAX_DISP_CNT = 15;
35 setMaximumDisplayedRowCount(MAX_DISP_CNT);
36 setDefaultRow(Note::DEFAULT_NOTE_NUM);
37 setLabelDiaplayMode(true);
38 for (int i = 0; i < Note::NOTE_NUMBER_RANGE; ++i) {
39 AddRow(QString::asprintf("%+d", i - Note::DEFAULT_NOTE_NUM), false);
40 }
41 autoFitLabelWidth();
42 setUpperRow(Note::DEFAULT_NOTE_NUM + MAX_DISP_CNT / 2);
43 setMMLDisplay0As(-Note::DEFAULT_NOTE_NUM);
44 setSequenceType(SequenceType::AbsoluteSequence);
45 }
46
47 QString ArpeggioMacroEditor::convertSequenceDataUnitToMML(Column col)
48 {
49 if (type_ == SequenceType::FixedSequence) return NoteNameManager::getManager().getNoteString(col.row);
50 else return VisualizedInstrumentMacroEditor::convertSequenceDataUnitToMML(col);
51 }
52
53 bool ArpeggioMacroEditor::interpretDataInMML(QString &text, int &cnt, std::vector<Column> &column)
54 {
55 if (type_ == SequenceType::FixedSequence) {
56 NoteNameManager& nnm = NoteNameManager::getManager();
57 for (int i = 0; i < 12; ++i) {
58 QString name = QString("%1").arg(nnm.getNoteName(i), -2, QChar('-'));
59 QRegularExpressionMatch m = QRegularExpression("^" + name + "([0-7])").match(text);
60 if (m.hasMatch()) {
61 int oct = m.captured(1).toInt();
62 int d = 12 * oct + i;
63 column.push_back({ d, -1, "" });
64 ++cnt;
65 text.remove(QRegularExpression("^" + name + "[0-7]"));
66 return true;
67 }
68 }
69 return false;
70 }
71 else {
72 return VisualizedInstrumentMacroEditor::interpretDataInMML(text, cnt, column);
73 }
74 }
75
76 void ArpeggioMacroEditor::onNoteNamesUpdated()
77 {
78 updateLabels();
79 printMML();
80 }
81
82 void ArpeggioMacroEditor::updateLabels()
83 {
84 if (type_ == SequenceType::FixedSequence) {
85 for (int i = 0; i < Note::NOTE_NUMBER_RANGE; ++i)
86 setLabel(i, NoteNameManager::getManager().getNoteString(i));
87 }
88 else {
89 for (int i = 0; i < Note::NOTE_NUMBER_RANGE; ++i)
90 setLabel(i, QString::asprintf("%+d", i - Note::DEFAULT_NOTE_NUM));
91 }
92 }