Debian Codegraph @

BambooTracker/gui/pattern_editor/pattern_editor_panel.hpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2018 Rerrah
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef PATTERN_EDITOR_PANEL_HPP
7 #define PATTERN_EDITOR_PANEL_HPP
8
9 #include <QWidget>
10 #include <QPixmap>
11 #include <QFont>
12 #include <QPaintEvent>
13 #include <QResizeEvent>
14 #include <QMouseEvent>
15 #include <QHoverEvent>
16 #include <QWheelEvent>
17 #include <QEvent>
18 #include <QRect>
19 #include <QColor>
20 #include <QUndoStack>
21 #include <QString>
22 #include <QPoint>
23 #include <QShortcut>
24 #include <memory>
25 #include <vector>
26 #include <atomic>
27 #include "bamboo_tracker.hpp"
28 #include "configuration.hpp"
29 #include "song.hpp"
30 #include "vector_2d.hpp"
31 #include "gui/pattern_editor/pattern_position.hpp"
32 #include "gui/color_palette.hpp"
33
34 class PatternEditorPanel : public QWidget
35 {
36 Q_OBJECT
37 public:
38 explicit PatternEditorPanel(QWidget *parent = nullptr);
39 ~PatternEditorPanel() override;
40 void setCore(std::shared_ptr<BambooTracker> core);
41 bool isReadyCore() const;
42 void setCommandStack(std::weak_ptr<QUndoStack> stack);
43 void setConfiguration(std::shared_ptr<Configuration> config);
44 void setColorPallete(std::shared_ptr<ColorPalette> palette);
45
46 void changeEditable();
47 int getFullColmunSize() const;
48 void updatePositionByStepUpdate(bool isFirstUpdate, bool forceJump = false, bool trackChanged = false);
49 int getScrollableCountByTrack() const;
50
51 void changeMarker();
52
53 void copySelectedCells();
54 void cutSelectedCells();
55
56 void redrawByPatternChanged(bool patternSizeChanged = false);
57 void redrawByFocusChanged();
58 void redrawByHoverChanged();
59 void redrawByMaskChanged();
60 void redrawPatterns();
61 void redrawAll();
62
63 void resetEntryCount();
64
65 void waitPaintFinish();
66
67 QFont getHeaderFont() const;
68 QFont getRowsFont() const;
69 QFont getDefaultHeaderFont() const;
70 QFont getDefaultRowsFont() const;
71 void setFonts(const QFont& headerFont, const QFont& rowsFont);
72
73 void setVisibleTracks(std::vector<int> tracks);
74 std::vector<int> getVisibleTracks() const;
75
76 public slots:
77 void onHScrollBarChanged(int num);
78 void onVScrollBarChanged(int num);
79 void onOrderListCurrentTrackChanged(int idx);
80 void onOrderListCurrentOrderChanged(int num);
81
82 void onOrderListEdited();
83 void onDefaultPatternSizeChanged();
84 void onShortcutUpdated();
85
86 void setPatternHighlight1Count(int count);
87 void setPatternHighlight2Count(int count);
88 void setEditableStep(int n);
89
90 void onSongLoaded();
91
92 void onDeletePressed();
93 void onPastePressed();
94 void onPasteMixPressed();
95 void onPasteOverwritePressed();
96 void onPasteInsertPressed();
97 /// 0: None
98 /// 1: All
99 /// 2: Row
100 /// 3: Column
101 /// 4: Pattern
102 /// 5: Order
103 void onSelectPressed(int type);
104 void onNoteTransposePressed(int semitone);
105 void onToggleTrackPressed();
106 void onSoloTrackPressed();
107 void onUnmuteAllPressed();
108 void onExpandPressed();
109 void onShrinkPressed();
110 void onInterpolatePressed();
111 void onReversePressed();
112 void onReplaceInstrumentPressed();
113 void onExpandEffectColumnPressed(int trackVisIdx);
114 void onShrinkEffectColumnPressed(int trackVisIdx);
115 void onFollowModeChanged();
116 void onChangeValuesPressed(int value);
117 void onPlayStepPressed();
118
119 signals:
120 void hScrollBarChangeRequested(int num);
121 void vScrollBarChangeRequested(int num, int max);
122 void currentTrackChanged(int num);
123 void currentOrderChanged(int num, int max);
124 void effectColsCompanded(int num, int max);
125
126 void selected(bool isSelected);
127 void instrumentEntered(int num);
128 void volumeEntered(int volume);
129 void effectEntered(QString text);
130
131 protected:
132 bool event(QEvent *event) override;
133 bool keyPressed(QKeyEvent* event);
134 bool keyReleased(QKeyEvent* event);
135 void paintEvent(QPaintEvent* event) override;
136 void resizeEvent(QResizeEvent* event) override;
137 void mousePressEvent(QMouseEvent* event) override;
138 void mouseMoveEvent(QMouseEvent* event) override;
139 void mouseReleaseEvent(QMouseEvent* event) override;
140 void mouseDoubleClickEvent(QMouseEvent* event) override;
141 bool mouseHoverd(QHoverEvent* event);
142 void wheelEvent(QWheelEvent* event) override;
143 void leaveEvent(QEvent*) override;
144
145 // Midi
146 private:
147 static void midiThreadReceivedEvent(double delay, const uint8_t *msg, size_t len, void *userData);
148 private slots:
149 void midiKeyEvent(uchar status, uchar key, uchar velocity);
150
151 private:
152 QPixmap completePixmap_, backPixmap_, textPixmap_, forePixmap_, headerPixmap_;
153 std::shared_ptr<BambooTracker> bt_;
154 std::weak_ptr<QUndoStack> comStack_;
155 std::shared_ptr<Configuration> config_;
156 std::shared_ptr<ColorPalette> palette_;
157
158 QFont stepFont_, headerFont_;
159 QFont stepFontDef_, headerFontDef_;
160 int stepFontWidth_, stepFontHeight_, stepFontAscent_, stepFontLeading_;
161 int headerFontAscent_;
162
163 int widthSpace_, widthSpaceDbl_;
164 int stepNumWidthCnt_, stepNumWidth_, stepNumBase_;
165 int baseTrackWidth_;
166 int toneNameWidth_, instWidth_;
167 int volWidth_;
168 int effWidth_, effIDWidth_, effValWidth_;
169 int tracksWidthFromLeftToEnd_;
170 int hdMuteToggleWidth_, hdEffCompandButtonWidth_;
171 int headerHeight_;
172 int hdPlusY_, hdMinusY_;
173 int curRowBaselineY_;
174 int curRowY_;
175
176 std::vector<int> visTracks_, rightEffn_;
177
178 int leftTrackVisIdx_;
179 SongStyle songStyle_;
180
181 int curSongNum_;
182 PatternPosition curPos_, hovPos_;
183 PatternPosition mousePressPos_, mouseReleasePos_;
184 PatternPosition selLeftAbovePos_, selRightBelowPos_;
185 PatternPosition shiftPressedPos_;
186 PatternPosition doubleClickPos_;
187 PatternPosition markerPos_;
188
189 bool isIgnoreToSlider_, isIgnoreToOrder_;
190
191 bool isPressedPlus_, isPressedMinus_;
192
193 int entryCnt_;
194
195 int selectAllState_;
196 bool isMuteElse_;
197
198 int hl1Cnt_, hl2Cnt_;
199 int editableStepCnt_;
200
201 int viewedRowCnt_;
202 int viewedRegionHeight_;
203 int viewedRowsHeight_, viewedRowOffset_, viewedCenterY_, viewedCenterBaseY_;
204 PatternPosition viewedFirstPos_, viewedCenterPos_, viewedLastPos_;
205
206 bool backChanged_, textChanged_, foreChanged_, headerChanged_, focusChanged_, followModeChanged_;
207 bool hasFocussedBefore_;
208 int stepDownCount_;
209
210 std::atomic_bool repaintable_; // Recurrensive repaint guard
211 std::atomic_int repaintingCnt_;
212 std::atomic_bool isInitedFirstMod_;
213
214 // Shortcuts
215 QShortcut upSc_, upWSSc_, dnSc_, dnWSSc_, pgUpSc_, pgUpWSSc_, pgDnSc_, pgDnWSSc_;
216 QShortcut homeSc_, homeWSSc_, endSc_, endWSSc_, hlUpSc_, hlUpWSSc_, hlDnSc_, hlDnWSSc_;
217 QShortcut ltSc_, ltWSSc_, rtSc_, rtWSSc_;
218 QShortcut keyOffSc_, keyCutSc_, echoBufSc_, stepMvUpSc_, stepMvDnSc_, expandColSc_, shrinkColSc_;
219
220 // Meta methods
221 int midiKeyEventMethod_;
222
223 void funcResize();
224 void updateSizes();
225 void initDisplay();
226 void drawPattern(const QRect& rect);
227 void drawRows(int maxWidth);
228 void quickDrawRows(int maxWidth);
229 /// Return:
230 /// track width
231 int drawStep(QPainter& forePainter, QPainter& textPainter, QPainter& backPainter, int trackVisIdx, int orderNum, int stepNum, int x, int baseY, int rowY);
232 void drawHeaders(int maxWidth);
233 void drawBorders(int maxWidth);
234 void drawShadow();
235
236 // NOTE: Calculated by visible tracks
237 int calculateTracksWidthWithRowNum(int beginIdx, int endIdx) const;
238 int calculateColNumInRow(int trackVisIdx, int colNumInTrack, bool isExpanded = false) const;
239 int calculateColumnDistance(int beginTrackIdx, int beginColumn, int endTrackIdx, int endColumn, bool isExpanded = false) const;
240 int calculateStepDistance(int beginOrder, int beginStep, int endOrder, int endStep) const;
241 PatternPosition calculatePositionFrom(int order, int step, int by) const;
242 QPoint calculateCurrentCursorPosition() const;
243
244 inline void updateTracksWidthFromLeftToEnd()
245 {
246 tracksWidthFromLeftToEnd_ = calculateTracksWidthWithRowNum(
247 leftTrackVisIdx_, static_cast<int>(visTracks_.size()) - 1);
248 }
249
250 void moveCursorToRight(int n);
251 void moveViewToRight(int n);
252 void moveCursorToDown(int n);
253
254 inline void checkSelectionByCursorMove(bool isShift) {
255 if (isShift) setSelectedRectangle(shiftPressedPos_, curPos_);
256 else onSelectPressed(0);
257 }
258
259 bool enterToneData(QKeyEvent* event);
260 void setStepKeyOn(const Note& note);
261 bool enterInstrumentData(int key);
262 void setStepInstrument(int num);
263 bool enterVolumeData(int key);
264 void setStepVolume(int volume);
265 bool enterEffectID(int key);
266 void setStepEffectID(QString str);
267 bool enterEffectValue(int key);
268 void setStepEffectValue(int value);
269
270 inline int updateEntryCount()
271 {
272 entryCnt_ = (entryCnt_ + 1) % 2;
273 return entryCnt_;
274 }
275
276 void insertStep();
277 void deletePreviousStep();
278
279 void eraseSelectedCells();
280 void pasteCopiedCells(const PatternPosition& cursorPos);
281 void pasteMixCopiedCells(const PatternPosition& cursorPos);
282 void pasteOverwriteCopiedCells(const PatternPosition& cursorPos);
283 void pasteInsertCopiedCells(const PatternPosition& cursorPos);
284 PatternPosition getPasteLeftAbovePosition(
285 int pasteCol, const PatternPosition& cursorPos, size_t cellW) const;
286 Vector2d<std::string> makeCopiedCellsForPasteFull(const PatternPosition& laPos, const Vector2d<std::string>& cells);
287
288 void transposeNote(const PatternPosition& startPos, const PatternPosition& endPos, int semitone);
289 void changeValuesInPattern(const PatternPosition& startPos, const PatternPosition& endPos, int value);
290
291 void toggleTrack(int trackIdx);
292 void soloTrack(int trackIdx);
293
294 void setSelectedRectangle(const PatternPosition& start, const PatternPosition& end);
295 bool isSelectedCell(int trackVisIdx, int colNum, int orderNum, int stepNum);
296
297 void showPatternContextMenu(const PatternPosition& pos, const QPoint& point);
298 };
299
300 #endif // PATTERN_EDITOR_PANEL_HPP