Debian Codegraph @

BambooTracker/gui/instrument_editor/visualized_instrument_macro_editor.hpp

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 #ifndef VISUALIZED_INSTRUMENT_MACRO_EDITOR_HPP
27 #define VISUALIZED_INSTRUMENT_MACRO_EDITOR_HPP
28
29 #include <QWidget>
30 #include <QObject>
31 #include <QPixmap>
32 #include <QFont>
33 #include <QPaintEvent>
34 #include <QResizeEvent>
35 #include <QMouseEvent>
36 #include <QHoverEvent>
37 #include <QWheelEvent>
38 #include <QEvent>
39 #include <QColor>
40 #include <QString>
41 #include <vector>
42 #include <memory>
43 #include "instrument/sequence_property.hpp"
44 #include "gui/color_palette.hpp"
45
46 namespace Ui {
47 class VisualizedInstrumentMacroEditor;
48 }
49
50 class VisualizedInstrumentMacroEditor : public QWidget
51 {
52 Q_OBJECT
53
54 public:
55 explicit VisualizedInstrumentMacroEditor(QWidget *parent = nullptr);
56 ~VisualizedInstrumentMacroEditor() override;
57
58 void setColorPalette(std::shared_ptr<ColorPalette> palette);
59
60 void AddRow(QString label = "", bool fitLabelWidth = true);
61 void setMaximumDisplayedRowCount(int count);
62 void setDefaultRow(int row);
63 int getSequenceLength() const;
64
65 void setSequenceData(int row, int col, QString str = "", int subdata = -1);
66 void setText(int col, QString text);
67 void setSubdata(int col, int subdata);
68
69 int getSequenceAt(int col) const;
70 int getSequenceDataAt(int col) const;
71
72 void setMultipleReleaseState(bool enabled);
73
74 void addSequenceData(int row, QString str = "", int subdata = -1);
75 void removeSequenceData();
76 void clearSequenceData();
77
78 // NOTE: It is desirable to use loop class
79 void addLoop(int begin, int end, int times);
80
81 void setSequenceType(SequenceType type);
82
83 enum PermittedReleaseFlag : int
84 {
85 NO_RELEASE = 0,
86 FIXED_RELEASE = 1,
87 ABSOLUTE_RELEASE = 2,
88 RELATIVE_RELEASE = 4
89 };
90
91 void setPermittedReleaseTypes(int types);
92 void setRelease(const InstrumentSequenceRelease& release);
93
94 void clearData();
95 void clearRow();
96
97 void setUpperRow(int row);
98
99 void setLabel(int row, QString text);
100 void clearAllLabelText();
101 void setLabelDiaplayMode(bool isOmitted);
102 void autoFitLabelWidth();
103
104 void setMMLDisplay0As(int n);
105
106 signals:
107 void sequenceDataChanged(int row, int col);
108 void sequenceDataAdded(int row, int col);
109 void sequenceDataRemoved();
110 void loopCleared();
111 void loopAdded(InstrumentSequenceLoop loop);
112 void loopRemoved(int begin, int end);
113 void loopChanged(int prevBegin, int prevEnd, InstrumentSequenceLoop loop);
114 void releaseChanged(InstrumentSequenceRelease release);
115
116 protected:
117 bool eventFilter(QObject*object, QEvent* event) override;
118 void paintEventInView(QPaintEvent* event);
119 void resizeEventInView(QResizeEvent* event);
120 void mousePressEventInView(QMouseEvent* event);
121 void mouseReleaseEventInView(QMouseEvent* event);
122 void mouseMoveEventInView();
123 void mouseHoverdEventInView(QHoverEvent* event);
124 void leaveEventInView();
125 void wheelEventInView(QWheelEvent* event);
126
127 QPixmap pixmap_;
128 std::shared_ptr<ColorPalette> palette_;
129
130 QFont font_;
131 QFontMetrics met_;
132 int fontWidth_, fontHeight_, fontAscend_, fontLeading_;
133 int labWidth_;
134 std::vector<int> rowHeights_, colWidths_;
135 int rowHeight_;
136 int fieldHeight_;
137
138 int maxDispRowCnt_;
139 int upperRow_, defaultRow_;
140
141 int hovRow_, hovCol_;
142
143 virtual void drawField();
144 virtual void updateLabels();
145
146 SequenceType type_;
147
148 struct Column
149 {
150 int row, data;
151 QString text;
152 };
153
154 std::vector<QString> labels_;
155 std::vector<Column> cols_;
156
157 int permittedReleaseType_;
158
159 bool isLabelOmitted_;
160
161 virtual int detectRowNumberForMouseEvent(int col, int internalRow) const;
162
163 // NOTE: It is desirable to use loop class
164 struct Loop
165 {
166 int begin, end, times;
167 };
168
169 std::vector<Loop> loops_;
170 InstrumentSequenceRelease release_;
171
172
173 void printMML();
174 virtual QString convertSequenceDataUnitToMML(Column col);
175 virtual int maxInMML() const;
176 void interpretMML();
177 bool interpretSlopeInMML(QString& text, int& cnt, std::vector<Column>& column);
178 virtual bool interpretDataInMML(QString& text, int& cnt, std::vector<Column>& column);
179
180 int panelWidth() const;
181
182 inline int getDisplayedRowCount() const
183 {
184 int labCnt = static_cast<int>(labels_.size());
185 return (maxDispRowCnt_ > labCnt) ? labCnt : maxDispRowCnt_;
186 }
187
188 private slots:
189 void on_colIncrToolButton_clicked();
190 void on_colDecrToolButton_clicked();
191 void on_verticalScrollBar_valueChanged(int value);
192 void on_lineEdit_editingFinished();
193
194 private:
195 Ui::VisualizedInstrumentMacroEditor *ui;
196
197 int pressRow_, pressCol_;
198 int prevPressRow_, prevPressCol_;
199
200 int loopY_, releaseY_;
201 int loopBaseY_, releaseBaseY_;
202
203 int grabLoop_;
204 bool isGrabLoopHead_;
205 bool isGrabRelease_;
206
207 bool isMultiReleaseState_;
208
209 int mmlBase_;
210
211 bool isIgnoreEvent_;
212
213 void initDisplay();
214
215 void drawLoop();
216 void drawRelease();
217 void drawBorder();
218 void drawShadow();
219
220 int checkLoopRegion(int col);
221 void moveLoop();
222
223 void updateColumnWidth();
224 void updateRowHeight();
225
226 inline void scrollUp(int pos)
227 {
228 upperRow_ = pos - 1 + getDisplayedRowCount();
229 }
230 };
231
232 #endif // VISUALIZED_INSTRUMENT_MACRO_EDITOR_HPP