PianoRoll.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 MidiClip;
  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. Q_PROPERTY(QBrush whiteKeyDisabledBackground MEMBER m_whiteKeyDisabledBackground)
  83. /* black key properties */
  84. Q_PROPERTY(int blackKeyWidth MEMBER m_blackKeyWidth)
  85. Q_PROPERTY(QBrush blackKeyInactiveBackground MEMBER m_blackKeyInactiveBackground)
  86. Q_PROPERTY(QBrush blackKeyActiveBackground MEMBER m_blackKeyActiveBackground)
  87. Q_PROPERTY(QBrush blackKeyDisabledBackground MEMBER m_blackKeyDisabledBackground)
  88. public:
  89. enum EditModes
  90. {
  91. ModeDraw,
  92. ModeErase,
  93. ModeSelect,
  94. ModeEditDetuning,
  95. ModeEditKnife
  96. };
  97. /*! \brief Resets settings to default when e.g. creating a new project */
  98. void reset();
  99. // functions to display the hover-text labeling a note's volume/panning
  100. void showTextFloat(const QString &text, const QPoint &pos, int timeout=-1);
  101. void showVolTextFloat(volume_t vol, const QPoint &pos, int timeout=-1);
  102. void showPanTextFloat(panning_t pan, const QPoint &pos, int timeout=-1);
  103. void setCurrentMidiClip( MidiClip* newMidiClip );
  104. void setGhostMidiClip( MidiClip* newMidiClip );
  105. void loadGhostNotes( const QDomElement & de );
  106. void loadMarkedSemiTones(const QDomElement & de);
  107. inline void stopRecording()
  108. {
  109. m_recording = false;
  110. }
  111. inline bool isRecording() const
  112. {
  113. return m_recording;
  114. }
  115. inline bool isStepRecording() const
  116. {
  117. return m_stepRecorder.isRecording();
  118. }
  119. const MidiClip* currentMidiClip() const
  120. {
  121. return m_midiClip;
  122. }
  123. bool hasValidMidiClip() const
  124. {
  125. return m_midiClip != nullptr;
  126. }
  127. Song::PlayModes desiredPlayModeForAccompany() const;
  128. int quantization() const;
  129. protected:
  130. enum QuantizeActions
  131. {
  132. QuantizeBoth,
  133. QuantizePos,
  134. QuantizeLength
  135. };
  136. void keyPressEvent( QKeyEvent * ke ) override;
  137. void keyReleaseEvent( QKeyEvent * ke ) override;
  138. void leaveEvent( QEvent * e ) override;
  139. void mousePressEvent( QMouseEvent * me ) override;
  140. void mouseDoubleClickEvent( QMouseEvent * me ) override;
  141. void mouseReleaseEvent( QMouseEvent * me ) override;
  142. void mouseMoveEvent( QMouseEvent * me ) override;
  143. void paintEvent( QPaintEvent * pe ) override;
  144. void resizeEvent( QResizeEvent * re ) override;
  145. void wheelEvent( QWheelEvent * we ) override;
  146. void focusOutEvent( QFocusEvent * ) override;
  147. void focusInEvent( QFocusEvent * ) override;
  148. int getKey( int y ) const;
  149. void drawNoteRect( QPainter & p, int x, int y,
  150. int width, const Note * n, const QColor & noteCol, const QColor & noteTextColor,
  151. const QColor & selCol, const int noteOpc, const bool borderless, bool drawNoteName );
  152. void removeSelection();
  153. void selectAll();
  154. NoteVector getSelectedNotes() const;
  155. void selectNotesOnKey();
  156. // for entering values with dblclick in the vol/pan bars
  157. void enterValue( NoteVector* nv );
  158. void updateYScroll();
  159. protected slots:
  160. void play();
  161. void record();
  162. void recordAccompany();
  163. bool toggleStepRecording();
  164. void stop();
  165. void startRecordNote( const Note & n );
  166. void finishRecordNote( const Note & n );
  167. void horScrolled( int new_pos );
  168. void verScrolled( int new_pos );
  169. void setEditMode(int mode);
  170. void copySelectedNotes();
  171. void cutSelectedNotes();
  172. void pasteNotes();
  173. bool deleteSelectedNotes();
  174. void updatePosition(const TimePos & t );
  175. void updatePositionAccompany(const TimePos & t );
  176. void updatePositionStepRecording(const TimePos & t );
  177. void zoomingChanged();
  178. void zoomingYChanged();
  179. void quantizeChanged();
  180. void noteLengthChanged();
  181. void keyChanged();
  182. void quantizeNotes(QuantizeActions mode = QuantizeBoth);
  183. void updateSemiToneMarkerMenu();
  184. void changeNoteEditMode( int i );
  185. void markSemiTone(int i, bool fromMenu = true);
  186. void hideMidiClip( MidiClip* clip );
  187. void selectRegionFromPixels( int xStart, int xEnd );
  188. void clearGhostClip();
  189. void glueNotes();
  190. void fitNoteLengths(bool fill);
  191. void constrainNoteLengths(bool constrainMax);
  192. void changeSnapMode();
  193. signals:
  194. void currentMidiClipChanged();
  195. void ghostClipSet(bool);
  196. void semiToneMarkerMenuScaleSetEnabled(bool);
  197. void semiToneMarkerMenuChordSetEnabled(bool);
  198. private:
  199. enum Actions
  200. {
  201. ActionNone,
  202. ActionMoveNote,
  203. ActionResizeNote,
  204. ActionSelectNotes,
  205. ActionChangeNoteProperty,
  206. ActionResizeNoteEditArea,
  207. ActionKnife
  208. };
  209. enum NoteEditMode
  210. {
  211. NoteEditVolume,
  212. NoteEditPanning,
  213. NoteEditCount // make sure this one is always last
  214. };
  215. enum SemiToneMarkerAction
  216. {
  217. stmaUnmarkAll,
  218. stmaMarkCurrentSemiTone,
  219. stmaMarkAllOctaveSemiTones,
  220. stmaMarkCurrentScale,
  221. stmaMarkCurrentChord,
  222. stmaCopyAllNotesOnKey
  223. };
  224. enum PianoRollKeyTypes
  225. {
  226. PR_WHITE_KEY_SMALL,
  227. PR_WHITE_KEY_BIG,
  228. PR_BLACK_KEY
  229. };
  230. enum GridMode
  231. {
  232. gridNudge,
  233. gridSnap
  234. // gridFree
  235. };
  236. PositionLine * m_positionLine;
  237. QVector<QString> m_nemStr; // gui names of each edit mode
  238. QMenu * m_noteEditMenu; // when you right click below the key area
  239. QList<int> m_markedSemiTones;
  240. QMenu * m_semiToneMarkerMenu; // when you right click on the key area
  241. int m_pianoKeySelected;
  242. PianoRoll();
  243. PianoRoll( const PianoRoll & );
  244. virtual ~PianoRoll();
  245. void autoScroll(const TimePos & t );
  246. TimePos newNoteLen() const;
  247. void shiftPos(int amount);
  248. void shiftPos(NoteVector notes, int amount);
  249. void shiftSemiTone(int amount);
  250. void shiftSemiTone(NoteVector notes, int amount);
  251. bool isSelection() const;
  252. int selectionCount() const;
  253. void testPlayNote( Note * n );
  254. void testPlayKey( int _key, int _vol, int _pan );
  255. void pauseTestNotes(bool pause = true );
  256. void playChordNotes(int key, int velocity=-1);
  257. void pauseChordNotes(int key);
  258. void setKnifeAction();
  259. void cancelKnifeAction();
  260. void updateScrollbars();
  261. void updatePositionLineHeight();
  262. QList<int> getAllOctavesForKey( int keyToMirror ) const;
  263. int noteEditTop() const;
  264. int keyAreaBottom() const;
  265. int noteEditBottom() const;
  266. int keyAreaTop() const;
  267. int noteEditRight() const;
  268. int noteEditLeft() const;
  269. void dragNotes(int x, int y, bool alt, bool shift, bool ctrl);
  270. static const int cm_scrollAmtHoriz = 10;
  271. static const int cm_scrollAmtVert = 1;
  272. static QPixmap * s_toolDraw;
  273. static QPixmap * s_toolErase;
  274. static QPixmap * s_toolSelect;
  275. static QPixmap * s_toolMove;
  276. static QPixmap * s_toolOpen;
  277. static QPixmap* s_toolKnife;
  278. static PianoRollKeyTypes prKeyOrder[];
  279. static TextFloat * s_textFloat;
  280. ComboBoxModel m_zoomingModel;
  281. ComboBoxModel m_zoomingYModel;
  282. ComboBoxModel m_quantizeModel;
  283. ComboBoxModel m_noteLenModel;
  284. ComboBoxModel m_keyModel;
  285. ComboBoxModel m_scaleModel;
  286. ComboBoxModel m_chordModel;
  287. ComboBoxModel m_snapModel;
  288. static const QVector<float> m_zoomLevels;
  289. static const QVector<float> m_zoomYLevels;
  290. MidiClip* m_midiClip;
  291. NoteVector m_ghostNotes;
  292. inline const NoteVector & ghostNotes() const
  293. {
  294. return m_ghostNotes;
  295. }
  296. QScrollBar * m_leftRightScroll;
  297. QScrollBar * m_topBottomScroll;
  298. TimePos m_currentPosition;
  299. bool m_recording;
  300. QList<Note> m_recordingNotes;
  301. Note * m_currentNote;
  302. Actions m_action;
  303. NoteEditMode m_noteEditMode;
  304. GridMode m_gridMode;
  305. int m_selectStartTick;
  306. int m_selectedTick;
  307. int m_selectStartKey;
  308. int m_selectedKeys;
  309. // boundary box around all selected notes when dragging
  310. int m_moveBoundaryLeft;
  311. int m_moveBoundaryTop;
  312. int m_moveBoundaryRight;
  313. int m_moveBoundaryBottom;
  314. // remember where the scrolling started when dragging so that
  315. // we can handle dragging while scrolling with arrow keys
  316. int m_mouseDownKey;
  317. int m_mouseDownTick;
  318. // remember the last x and y of a mouse movement
  319. int m_lastMouseX;
  320. int m_lastMouseY;
  321. // x,y of when the user starts a drag
  322. int m_moveStartX;
  323. int m_moveStartY;
  324. int m_notesEditHeight;
  325. int m_userSetNotesEditHeight;
  326. int m_ppb; // pixels per bar
  327. int m_totalKeysToScroll;
  328. int m_pianoKeysVisible;
  329. int m_keyLineHeight;
  330. int m_whiteKeySmallHeight;
  331. int m_whiteKeyBigHeight;
  332. int m_blackKeyHeight;
  333. // remember these values to use them
  334. // for the next note that is set
  335. TimePos m_lenOfNewNotes;
  336. volume_t m_lastNoteVolume;
  337. panning_t m_lastNotePanning;
  338. //When resizing several notes, we want to calculate a common minimum length
  339. TimePos m_minResizeLen;
  340. int m_startKey; // first key when drawing
  341. int m_lastKey;
  342. EditModes m_editMode;
  343. EditModes m_ctrlMode; // mode they were in before they hit ctrl
  344. EditModes m_knifeMode; // mode they where in before entering knife mode
  345. bool m_mouseDownRight; //true if right click is being held down
  346. TimeLineWidget * m_timeLine;
  347. bool m_scrollBack;
  348. void copyToClipboard(const NoteVector & notes ) const;
  349. void drawDetuningInfo( QPainter & _p, const Note * _n, int _x, int _y ) const;
  350. bool mouseOverNote();
  351. Note * noteUnderMouse();
  352. // turn a selection rectangle into selected notes
  353. void computeSelectedNotes( bool shift );
  354. void clearSelectedNotes();
  355. // did we start a mouseclick with shift pressed
  356. bool m_startedWithShift;
  357. // Variable that holds the position in ticks for the knife action
  358. int m_knifeTickPos;
  359. void updateKnifePos(QMouseEvent* me);
  360. friend class PianoRollWindow;
  361. StepRecorderWidget m_stepRecorderWidget;
  362. StepRecorder m_stepRecorder;
  363. // qproperty fields
  364. QColor m_barLineColor;
  365. QColor m_beatLineColor;
  366. QColor m_lineColor;
  367. QColor m_noteModeColor;
  368. QColor m_noteColor;
  369. QColor m_noteTextColor;
  370. QColor m_ghostNoteColor;
  371. QColor m_ghostNoteTextColor;
  372. QColor m_barColor;
  373. QColor m_selectedNoteColor;
  374. QColor m_textColor;
  375. QColor m_textColorLight;
  376. QColor m_textShadow;
  377. QColor m_markedSemitoneColor;
  378. QColor m_knifeCutLineColor;
  379. int m_noteOpacity;
  380. int m_ghostNoteOpacity;
  381. bool m_noteBorders;
  382. bool m_ghostNoteBorders;
  383. QColor m_backgroundShade;
  384. /* white key properties */
  385. int m_whiteKeyWidth;
  386. QColor m_whiteKeyActiveTextColor;
  387. QColor m_whiteKeyActiveTextShadow;
  388. QBrush m_whiteKeyActiveBackground;
  389. QColor m_whiteKeyInactiveTextColor;
  390. QColor m_whiteKeyInactiveTextShadow;
  391. QBrush m_whiteKeyInactiveBackground;
  392. QBrush m_whiteKeyDisabledBackground;
  393. /* black key properties */
  394. int m_blackKeyWidth;
  395. QBrush m_blackKeyActiveBackground;
  396. QBrush m_blackKeyInactiveBackground;
  397. QBrush m_blackKeyDisabledBackground;
  398. signals:
  399. void positionChanged( const TimePos & );
  400. } ;
  401. class PianoRollWindow : public Editor, SerializingObject
  402. {
  403. Q_OBJECT
  404. public:
  405. PianoRollWindow();
  406. const MidiClip* currentMidiClip() const;
  407. void setCurrentMidiClip( MidiClip* clip );
  408. void setGhostMidiClip( MidiClip* clip );
  409. int quantization() const;
  410. void play() override;
  411. void stop() override;
  412. void record() override;
  413. void recordAccompany() override;
  414. void toggleStepRecording() override;
  415. void stopRecording();
  416. bool isRecording() const;
  417. /*! \brief Resets settings to default when e.g. creating a new project */
  418. void reset();
  419. using SerializingObject::saveState;
  420. using SerializingObject::restoreState;
  421. void saveSettings(QDomDocument & doc, QDomElement & de ) override;
  422. void loadSettings( const QDomElement & de ) override;
  423. inline QString nodeName() const override
  424. {
  425. return "pianoroll";
  426. }
  427. QSize sizeHint() const override;
  428. bool hasFocus() const;
  429. signals:
  430. void currentMidiClipChanged();
  431. private slots:
  432. void updateAfterMidiClipChange();
  433. void ghostClipSet( bool state );
  434. void exportMidiClip();
  435. void importMidiClip();
  436. private:
  437. void clipRenamed();
  438. void focusInEvent(QFocusEvent * event) override;
  439. void stopStepRecording();
  440. void updateStepRecordingIcon();
  441. PianoRoll* m_editor;
  442. QToolButton* m_fileToolsButton;
  443. ComboBox * m_zoomingComboBox;
  444. ComboBox * m_zoomingYComboBox;
  445. ComboBox * m_quantizeComboBox;
  446. ComboBox * m_noteLenComboBox;
  447. ComboBox * m_keyComboBox;
  448. ComboBox * m_scaleComboBox;
  449. ComboBox * m_chordComboBox;
  450. ComboBox* m_snapComboBox;
  451. QPushButton * m_clearGhostButton;
  452. };
  453. #endif