PianoRoll.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * PianoRoll.h - declaration of class PianoRoll which is a window where you
  3. * can set and edit notes in an easy way
  4. *
  5. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. * Copyright (c) 2008 Andrew Kelley <superjoe30/at/gmail/dot/com>
  7. *
  8. * This file is part of LMMS - https://lmms.io
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program (see COPYING); if not, write to the
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301 USA.
  24. *
  25. */
  26. #ifndef PIANO_ROLL_H
  27. #define PIANO_ROLL_H
  28. #include <QVector>
  29. #include <QWidget>
  30. #include <QInputDialog>
  31. #include "Editor.h"
  32. #include "ComboBoxModel.h"
  33. #include "SerializingObject.h"
  34. #include "Note.h"
  35. #include "lmms_basics.h"
  36. #include "Song.h"
  37. #include "ToolTip.h"
  38. #include "StepRecorder.h"
  39. #include "StepRecorderWidget.h"
  40. #include "PositionLine.h"
  41. class QPainter;
  42. class QPixmap;
  43. class QScrollBar;
  44. class QString;
  45. class QMenu;
  46. class QToolButton;
  47. class ComboBox;
  48. class NotePlayHandle;
  49. class Pattern;
  50. class TimeLineWidget;
  51. class PianoRoll : public QWidget
  52. {
  53. Q_OBJECT
  54. Q_PROPERTY(QColor barLineColor MEMBER m_barLineColor)
  55. Q_PROPERTY(QColor beatLineColor MEMBER m_beatLineColor)
  56. Q_PROPERTY(QColor lineColor MEMBER m_lineColor)
  57. Q_PROPERTY(QColor noteModeColor MEMBER m_noteModeColor)
  58. Q_PROPERTY(QColor noteColor MEMBER m_noteColor)
  59. Q_PROPERTY(QColor ghostNoteColor MEMBER m_ghostNoteColor)
  60. Q_PROPERTY(QColor noteTextColor MEMBER m_noteTextColor)
  61. Q_PROPERTY(QColor ghostNoteTextColor MEMBER m_ghostNoteTextColor)
  62. Q_PROPERTY(QColor barColor MEMBER m_barColor)
  63. Q_PROPERTY(QColor selectedNoteColor MEMBER m_selectedNoteColor)
  64. Q_PROPERTY(QColor textColor MEMBER m_textColor)
  65. Q_PROPERTY(QColor textColorLight MEMBER m_textColorLight)
  66. Q_PROPERTY(QColor textShadow MEMBER m_textShadow)
  67. Q_PROPERTY(QColor markedSemitoneColor MEMBER m_markedSemitoneColor)
  68. Q_PROPERTY(QColor knifeCutLine MEMBER m_knifeCutLineColor)
  69. Q_PROPERTY(int noteOpacity MEMBER m_noteOpacity)
  70. Q_PROPERTY(bool noteBorders MEMBER m_noteBorders)
  71. Q_PROPERTY(int ghostNoteOpacity MEMBER m_ghostNoteOpacity)
  72. Q_PROPERTY(bool ghostNoteBorders MEMBER m_ghostNoteBorders)
  73. Q_PROPERTY(QColor backgroundShade MEMBER m_backgroundShade)
  74. /* white key properties */
  75. Q_PROPERTY(int whiteKeyWidth MEMBER m_whiteKeyWidth)
  76. Q_PROPERTY(QColor whiteKeyInactiveTextColor MEMBER m_whiteKeyInactiveTextColor)
  77. Q_PROPERTY(QColor whiteKeyInactiveTextShadow MEMBER m_whiteKeyInactiveTextShadow)
  78. Q_PROPERTY(QBrush whiteKeyInactiveBackground MEMBER m_whiteKeyInactiveBackground)
  79. Q_PROPERTY(QColor whiteKeyActiveTextColor MEMBER m_whiteKeyActiveTextColor)
  80. Q_PROPERTY(QColor whiteKeyActiveTextShadow MEMBER m_whiteKeyActiveTextShadow)
  81. Q_PROPERTY(QBrush whiteKeyActiveBackground MEMBER m_whiteKeyActiveBackground)
  82. /* black key properties */
  83. Q_PROPERTY(int blackKeyWidth MEMBER m_blackKeyWidth)
  84. Q_PROPERTY(QBrush blackKeyInactiveBackground MEMBER m_blackKeyInactiveBackground)
  85. Q_PROPERTY(QBrush blackKeyActiveBackground MEMBER m_blackKeyActiveBackground)
  86. public:
  87. enum EditModes
  88. {
  89. ModeDraw,
  90. ModeErase,
  91. ModeSelect,
  92. ModeEditDetuning,
  93. ModeEditKnife
  94. };
  95. /*! \brief Resets settings to default when e.g. creating a new project */
  96. void reset();
  97. // functions to display the hover-text labeling a note's volume/panning
  98. void showTextFloat(const QString &text, const QPoint &pos, int timeout=-1);
  99. void showVolTextFloat(volume_t vol, const QPoint &pos, int timeout=-1);
  100. void showPanTextFloat(panning_t pan, const QPoint &pos, int timeout=-1);
  101. void setCurrentPattern( Pattern* newPattern );
  102. void setGhostPattern( Pattern* newPattern );
  103. void loadGhostNotes( const QDomElement & de );
  104. void loadMarkedSemiTones(const QDomElement & de);
  105. inline void stopRecording()
  106. {
  107. m_recording = false;
  108. }
  109. inline bool isRecording() const
  110. {
  111. return m_recording;
  112. }
  113. inline bool isStepRecording() const
  114. {
  115. return m_stepRecorder.isRecording();
  116. }
  117. const Pattern* currentPattern() const
  118. {
  119. return m_pattern;
  120. }
  121. bool hasValidPattern() const
  122. {
  123. return m_pattern != NULL;
  124. }
  125. Song::PlayModes desiredPlayModeForAccompany() const;
  126. int quantization() const;
  127. protected:
  128. enum QuantizeActions
  129. {
  130. QuantizeBoth,
  131. QuantizePos,
  132. QuantizeLength
  133. };
  134. void keyPressEvent( QKeyEvent * ke ) override;
  135. void keyReleaseEvent( QKeyEvent * ke ) override;
  136. void leaveEvent( QEvent * e ) override;
  137. void mousePressEvent( QMouseEvent * me ) override;
  138. void mouseDoubleClickEvent( QMouseEvent * me ) override;
  139. void mouseReleaseEvent( QMouseEvent * me ) override;
  140. void mouseMoveEvent( QMouseEvent * me ) override;
  141. void paintEvent( QPaintEvent * pe ) override;
  142. void resizeEvent( QResizeEvent * re ) override;
  143. void wheelEvent( QWheelEvent * we ) override;
  144. void focusOutEvent( QFocusEvent * ) override;
  145. void focusInEvent( QFocusEvent * ) override;
  146. int getKey( int y ) const;
  147. void drawNoteRect( QPainter & p, int x, int y,
  148. int width, const Note * n, const QColor & noteCol, const QColor & noteTextColor,
  149. const QColor & selCol, const int noteOpc, const bool borderless, bool drawNoteName );
  150. void removeSelection();
  151. void selectAll();
  152. NoteVector getSelectedNotes() const;
  153. void selectNotesOnKey();
  154. // for entering values with dblclick in the vol/pan bars
  155. void enterValue( NoteVector* nv );
  156. void updateYScroll();
  157. protected slots:
  158. void play();
  159. void record();
  160. void recordAccompany();
  161. bool toggleStepRecording();
  162. void stop();
  163. void startRecordNote( const Note & n );
  164. void finishRecordNote( const Note & n );
  165. void horScrolled( int new_pos );
  166. void verScrolled( int new_pos );
  167. void setEditMode(int mode);
  168. void copySelectedNotes();
  169. void cutSelectedNotes();
  170. void pasteNotes();
  171. bool deleteSelectedNotes();
  172. void updatePosition(const TimePos & t );
  173. void updatePositionAccompany(const TimePos & t );
  174. void updatePositionStepRecording(const TimePos & t );
  175. void zoomingChanged();
  176. void zoomingYChanged();
  177. void quantizeChanged();
  178. void noteLengthChanged();
  179. void keyChanged();
  180. void quantizeNotes(QuantizeActions mode = QuantizeBoth);
  181. void updateSemiToneMarkerMenu();
  182. void changeNoteEditMode( int i );
  183. void markSemiTone(int i, bool fromMenu = true);
  184. void hidePattern( Pattern* pattern );
  185. void selectRegionFromPixels( int xStart, int xEnd );
  186. void clearGhostPattern();
  187. void glueNotes();
  188. void fitNoteLengths(bool fill);
  189. void constrainNoteLengths(bool constrainMax);
  190. void changeSnapMode();
  191. signals:
  192. void currentPatternChanged();
  193. void ghostPatternSet(bool);
  194. void semiToneMarkerMenuScaleSetEnabled(bool);
  195. void semiToneMarkerMenuChordSetEnabled(bool);
  196. private:
  197. enum Actions
  198. {
  199. ActionNone,
  200. ActionMoveNote,
  201. ActionResizeNote,
  202. ActionSelectNotes,
  203. ActionChangeNoteProperty,
  204. ActionResizeNoteEditArea,
  205. ActionKnife
  206. };
  207. enum NoteEditMode
  208. {
  209. NoteEditVolume,
  210. NoteEditPanning,
  211. NoteEditCount // make sure this one is always last
  212. };
  213. enum SemiToneMarkerAction
  214. {
  215. stmaUnmarkAll,
  216. stmaMarkCurrentSemiTone,
  217. stmaMarkAllOctaveSemiTones,
  218. stmaMarkCurrentScale,
  219. stmaMarkCurrentChord,
  220. stmaCopyAllNotesOnKey
  221. };
  222. enum PianoRollKeyTypes
  223. {
  224. PR_WHITE_KEY_SMALL,
  225. PR_WHITE_KEY_BIG,
  226. PR_BLACK_KEY
  227. };
  228. enum GridMode
  229. {
  230. gridNudge,
  231. gridSnap
  232. // gridFree
  233. };
  234. PositionLine * m_positionLine;
  235. QVector<QString> m_nemStr; // gui names of each edit mode
  236. QMenu * m_noteEditMenu; // when you right click below the key area
  237. QList<int> m_markedSemiTones;
  238. QMenu * m_semiToneMarkerMenu; // when you right click on the key area
  239. int m_pianoKeySelected;
  240. PianoRoll();
  241. PianoRoll( const PianoRoll & );
  242. virtual ~PianoRoll();
  243. void autoScroll(const TimePos & t );
  244. TimePos newNoteLen() const;
  245. void shiftPos(int amount);
  246. void shiftPos(NoteVector notes, int amount);
  247. void shiftSemiTone(int amount);
  248. void shiftSemiTone(NoteVector notes, int amount);
  249. bool isSelection() const;
  250. int selectionCount() const;
  251. void testPlayNote( Note * n );
  252. void testPlayKey( int _key, int _vol, int _pan );
  253. void pauseTestNotes(bool pause = true );
  254. void playChordNotes(int key, int velocity=-1);
  255. void pauseChordNotes(int key);
  256. void setKnifeAction();
  257. void cancelKnifeAction();
  258. void updateScrollbars();
  259. void updatePositionLineHeight();
  260. QList<int> getAllOctavesForKey( int keyToMirror ) const;
  261. int noteEditTop() const;
  262. int keyAreaBottom() const;
  263. int noteEditBottom() const;
  264. int keyAreaTop() const;
  265. int noteEditRight() const;
  266. int noteEditLeft() const;
  267. void dragNotes(int x, int y, bool alt, bool shift, bool ctrl);
  268. static const int cm_scrollAmtHoriz = 10;
  269. static const int cm_scrollAmtVert = 1;
  270. static QPixmap * s_toolDraw;
  271. static QPixmap * s_toolErase;
  272. static QPixmap * s_toolSelect;
  273. static QPixmap * s_toolMove;
  274. static QPixmap * s_toolOpen;
  275. static QPixmap* s_toolKnife;
  276. static PianoRollKeyTypes prKeyOrder[];
  277. static TextFloat * s_textFloat;
  278. ComboBoxModel m_zoomingModel;
  279. ComboBoxModel m_zoomingYModel;
  280. ComboBoxModel m_quantizeModel;
  281. ComboBoxModel m_noteLenModel;
  282. ComboBoxModel m_keyModel;
  283. ComboBoxModel m_scaleModel;
  284. ComboBoxModel m_chordModel;
  285. ComboBoxModel m_snapModel;
  286. static const QVector<double> m_zoomLevels;
  287. static const QVector<double> m_zoomYLevels;
  288. Pattern* m_pattern;
  289. NoteVector m_ghostNotes;
  290. inline const NoteVector & ghostNotes() const
  291. {
  292. return m_ghostNotes;
  293. }
  294. QScrollBar * m_leftRightScroll;
  295. QScrollBar * m_topBottomScroll;
  296. TimePos m_currentPosition;
  297. bool m_recording;
  298. QList<Note> m_recordingNotes;
  299. Note * m_currentNote;
  300. Actions m_action;
  301. NoteEditMode m_noteEditMode;
  302. GridMode m_gridMode;
  303. int m_selectStartTick;
  304. int m_selectedTick;
  305. int m_selectStartKey;
  306. int m_selectedKeys;
  307. // boundary box around all selected notes when dragging
  308. int m_moveBoundaryLeft;
  309. int m_moveBoundaryTop;
  310. int m_moveBoundaryRight;
  311. int m_moveBoundaryBottom;
  312. // remember where the scrolling started when dragging so that
  313. // we can handle dragging while scrolling with arrow keys
  314. int m_mouseDownKey;
  315. int m_mouseDownTick;
  316. // remember the last x and y of a mouse movement
  317. int m_lastMouseX;
  318. int m_lastMouseY;
  319. // x,y of when the user starts a drag
  320. int m_moveStartX;
  321. int m_moveStartY;
  322. int m_notesEditHeight;
  323. int m_userSetNotesEditHeight;
  324. int m_ppb; // pixels per bar
  325. int m_totalKeysToScroll;
  326. int m_pianoKeysVisible;
  327. int m_keyLineHeight;
  328. int m_octaveHeight;
  329. int m_whiteKeySmallHeight;
  330. int m_whiteKeyBigHeight;
  331. int m_blackKeyHeight;
  332. // remember these values to use them
  333. // for the next note that is set
  334. TimePos m_lenOfNewNotes;
  335. volume_t m_lastNoteVolume;
  336. panning_t m_lastNotePanning;
  337. //When resizing several notes, we want to calculate a common minimum length
  338. TimePos m_minResizeLen;
  339. int m_startKey; // first key when drawing
  340. int m_lastKey;
  341. EditModes m_editMode;
  342. EditModes m_ctrlMode; // mode they were in before they hit ctrl
  343. EditModes m_knifeMode; // mode they where in before entering knife mode
  344. bool m_mouseDownRight; //true if right click is being held down
  345. TimeLineWidget * m_timeLine;
  346. bool m_scrollBack;
  347. void copyToClipboard(const NoteVector & notes ) const;
  348. void drawDetuningInfo( QPainter & _p, const Note * _n, int _x, int _y ) const;
  349. bool mouseOverNote();
  350. Note * noteUnderMouse();
  351. // turn a selection rectangle into selected notes
  352. void computeSelectedNotes( bool shift );
  353. void clearSelectedNotes();
  354. // did we start a mouseclick with shift pressed
  355. bool m_startedWithShift;
  356. // Variable that holds the position in ticks for the knife action
  357. int m_knifeTickPos;
  358. void updateKnifePos(QMouseEvent* me);
  359. friend class PianoRollWindow;
  360. StepRecorderWidget m_stepRecorderWidget;
  361. StepRecorder m_stepRecorder;
  362. // qproperty fields
  363. QColor m_barLineColor;
  364. QColor m_beatLineColor;
  365. QColor m_lineColor;
  366. QColor m_noteModeColor;
  367. QColor m_noteColor;
  368. QColor m_noteTextColor;
  369. QColor m_ghostNoteColor;
  370. QColor m_ghostNoteTextColor;
  371. QColor m_barColor;
  372. QColor m_selectedNoteColor;
  373. QColor m_textColor;
  374. QColor m_textColorLight;
  375. QColor m_textShadow;
  376. QColor m_markedSemitoneColor;
  377. QColor m_knifeCutLineColor;
  378. int m_noteOpacity;
  379. int m_ghostNoteOpacity;
  380. bool m_noteBorders;
  381. bool m_ghostNoteBorders;
  382. QColor m_backgroundShade;
  383. /* white key properties */
  384. int m_whiteKeyWidth;
  385. QColor m_whiteKeyActiveTextColor;
  386. QColor m_whiteKeyActiveTextShadow;
  387. QBrush m_whiteKeyActiveBackground;
  388. QColor m_whiteKeyInactiveTextColor;
  389. QColor m_whiteKeyInactiveTextShadow;
  390. QBrush m_whiteKeyInactiveBackground;
  391. /* black key properties */
  392. int m_blackKeyWidth;
  393. QBrush m_blackKeyActiveBackground;
  394. QBrush m_blackKeyInactiveBackground;
  395. signals:
  396. void positionChanged( const TimePos & );
  397. } ;
  398. class PianoRollWindow : public Editor, SerializingObject
  399. {
  400. Q_OBJECT
  401. public:
  402. PianoRollWindow();
  403. const Pattern* currentPattern() const;
  404. void setCurrentPattern( Pattern* pattern );
  405. void setGhostPattern( Pattern* pattern );
  406. int quantization() const;
  407. void play() override;
  408. void stop() override;
  409. void record() override;
  410. void recordAccompany() override;
  411. void toggleStepRecording() override;
  412. void stopRecording();
  413. bool isRecording() const;
  414. /*! \brief Resets settings to default when e.g. creating a new project */
  415. void reset();
  416. using SerializingObject::saveState;
  417. using SerializingObject::restoreState;
  418. void saveSettings(QDomDocument & doc, QDomElement & de ) override;
  419. void loadSettings( const QDomElement & de ) override;
  420. inline QString nodeName() const override
  421. {
  422. return "pianoroll";
  423. }
  424. QSize sizeHint() const override;
  425. bool hasFocus() const;
  426. signals:
  427. void currentPatternChanged();
  428. private slots:
  429. void updateAfterPatternChange();
  430. void ghostPatternSet( bool state );
  431. void exportPattern();
  432. void importPattern();
  433. private:
  434. void patternRenamed();
  435. void focusInEvent(QFocusEvent * event) override;
  436. void stopStepRecording();
  437. void updateStepRecordingIcon();
  438. PianoRoll* m_editor;
  439. QToolButton* m_fileToolsButton;
  440. ComboBox * m_zoomingComboBox;
  441. ComboBox * m_zoomingYComboBox;
  442. ComboBox * m_quantizeComboBox;
  443. ComboBox * m_noteLenComboBox;
  444. ComboBox * m_keyComboBox;
  445. ComboBox * m_scaleComboBox;
  446. ComboBox * m_chordComboBox;
  447. ComboBox* m_snapComboBox;
  448. QPushButton * m_clearGhostButton;
  449. };
  450. #endif