PianoRoll.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. class QPainter;
  41. class QPixmap;
  42. class QScrollBar;
  43. class QString;
  44. class QMenu;
  45. class ComboBox;
  46. class NotePlayHandle;
  47. class Pattern;
  48. class TimeLineWidget;
  49. class PianoRoll : public QWidget
  50. {
  51. Q_OBJECT
  52. Q_PROPERTY( QColor barLineColor READ barLineColor WRITE setBarLineColor )
  53. Q_PROPERTY( QColor beatLineColor READ beatLineColor WRITE setBeatLineColor )
  54. Q_PROPERTY( QColor lineColor READ lineColor WRITE setLineColor )
  55. Q_PROPERTY( QColor noteModeColor READ noteModeColor WRITE setNoteModeColor )
  56. Q_PROPERTY( QColor noteColor READ noteColor WRITE setNoteColor )
  57. Q_PROPERTY( QColor ghostNoteColor READ ghostNoteColor WRITE setGhostNoteColor )
  58. Q_PROPERTY( QColor noteTextColor READ noteTextColor WRITE setNoteTextColor )
  59. Q_PROPERTY( QColor ghostNoteTextColor READ ghostNoteTextColor WRITE setGhostNoteTextColor )
  60. Q_PROPERTY( QColor barColor READ barColor WRITE setBarColor )
  61. Q_PROPERTY( QColor selectedNoteColor READ selectedNoteColor WRITE setSelectedNoteColor )
  62. Q_PROPERTY( QColor textColor READ textColor WRITE setTextColor )
  63. Q_PROPERTY( QColor textColorLight READ textColorLight WRITE setTextColorLight )
  64. Q_PROPERTY( QColor textShadow READ textShadow WRITE setTextShadow )
  65. Q_PROPERTY( QColor markedSemitoneColor READ markedSemitoneColor WRITE setMarkedSemitoneColor )
  66. Q_PROPERTY( int noteOpacity READ noteOpacity WRITE setNoteOpacity )
  67. Q_PROPERTY( bool noteBorders READ noteBorders WRITE setNoteBorders )
  68. Q_PROPERTY( int ghostNoteOpacity READ ghostNoteOpacity WRITE setGhostNoteOpacity )
  69. Q_PROPERTY( bool ghostNoteBorders READ ghostNoteBorders WRITE setGhostNoteBorders )
  70. Q_PROPERTY( QColor backgroundShade READ backgroundShade WRITE setBackgroundShade )
  71. public:
  72. enum EditModes
  73. {
  74. ModeDraw,
  75. ModeErase,
  76. ModeSelect,
  77. ModeEditDetuning,
  78. };
  79. /*! \brief Resets settings to default when e.g. creating a new project */
  80. void reset();
  81. // functions to display the hover-text labeling a note's volume/panning
  82. void showTextFloat(const QString &text, const QPoint &pos, int timeout=-1);
  83. void showVolTextFloat(volume_t vol, const QPoint &pos, int timeout=-1);
  84. void showPanTextFloat(panning_t pan, const QPoint &pos, int timeout=-1);
  85. void setCurrentPattern( Pattern* newPattern );
  86. void setGhostPattern( Pattern* newPattern );
  87. void loadGhostNotes( const QDomElement & de );
  88. void loadMarkedSemiTones(const QDomElement & de);
  89. inline void stopRecording()
  90. {
  91. m_recording = false;
  92. }
  93. inline bool isRecording() const
  94. {
  95. return m_recording;
  96. }
  97. inline bool isStepRecording() const
  98. {
  99. return m_stepRecorder.isRecording();
  100. }
  101. const Pattern* currentPattern() const
  102. {
  103. return m_pattern;
  104. }
  105. bool hasValidPattern() const
  106. {
  107. return m_pattern != NULL;
  108. }
  109. Song::PlayModes desiredPlayModeForAccompany() const;
  110. int quantization() const;
  111. // qproperty access functions
  112. QColor barLineColor() const;
  113. void setBarLineColor( const QColor & c );
  114. QColor beatLineColor() const;
  115. void setBeatLineColor( const QColor & c );
  116. QColor lineColor() const;
  117. void setLineColor( const QColor & c );
  118. QColor noteModeColor() const;
  119. void setNoteModeColor( const QColor & c );
  120. QColor noteColor() const;
  121. void setNoteColor( const QColor & c );
  122. QColor noteTextColor() const;
  123. void setNoteTextColor( const QColor & c );
  124. QColor barColor() const;
  125. void setBarColor( const QColor & c );
  126. QColor selectedNoteColor() const;
  127. void setSelectedNoteColor( const QColor & c );
  128. QColor textColor() const;
  129. void setTextColor( const QColor & c );
  130. QColor textColorLight() const;
  131. void setTextColorLight( const QColor & c );
  132. QColor textShadow() const;
  133. void setTextShadow( const QColor & c );
  134. QColor markedSemitoneColor() const;
  135. void setMarkedSemitoneColor( const QColor & c );
  136. int noteOpacity() const;
  137. void setNoteOpacity( const int i );
  138. bool noteBorders() const;
  139. void setNoteBorders( const bool b );
  140. QColor ghostNoteColor() const;
  141. void setGhostNoteColor( const QColor & c );
  142. QColor ghostNoteTextColor() const;
  143. void setGhostNoteTextColor( const QColor & c );
  144. int ghostNoteOpacity() const;
  145. void setGhostNoteOpacity( const int i );
  146. bool ghostNoteBorders() const;
  147. void setGhostNoteBorders( const bool b );
  148. QColor backgroundShade() const;
  149. void setBackgroundShade( const QColor & c );
  150. protected:
  151. void keyPressEvent( QKeyEvent * ke ) override;
  152. void keyReleaseEvent( QKeyEvent * ke ) override;
  153. void leaveEvent( QEvent * e ) override;
  154. void mousePressEvent( QMouseEvent * me ) override;
  155. void mouseDoubleClickEvent( QMouseEvent * me ) override;
  156. void mouseReleaseEvent( QMouseEvent * me ) override;
  157. void mouseMoveEvent( QMouseEvent * me ) override;
  158. void paintEvent( QPaintEvent * pe ) override;
  159. void resizeEvent( QResizeEvent * re ) override;
  160. void wheelEvent( QWheelEvent * we ) override;
  161. void focusOutEvent( QFocusEvent * ) override;
  162. int getKey( int y ) const;
  163. static void drawNoteRect( QPainter & p, int x, int y,
  164. int width, const Note * n, const QColor & noteCol, const QColor & noteTextColor,
  165. const QColor & selCol, const int noteOpc, const bool borderless, bool drawNoteName );
  166. void removeSelection();
  167. void selectAll();
  168. NoteVector getSelectedNotes();
  169. void selectNotesOnKey();
  170. int xCoordOfTick( int tick );
  171. // for entering values with dblclick in the vol/pan bars
  172. void enterValue( NoteVector* nv );
  173. protected slots:
  174. void play();
  175. void record();
  176. void recordAccompany();
  177. bool toggleStepRecording();
  178. void stop();
  179. void startRecordNote( const Note & n );
  180. void finishRecordNote( const Note & n );
  181. void horScrolled( int new_pos );
  182. void verScrolled( int new_pos );
  183. void setEditMode(int mode);
  184. void copySelectedNotes();
  185. void cutSelectedNotes();
  186. void pasteNotes();
  187. void deleteSelectedNotes();
  188. void updatePosition(const MidiTime & t );
  189. void updatePositionAccompany(const MidiTime & t );
  190. void updatePositionStepRecording(const MidiTime & t );
  191. void zoomingChanged();
  192. void quantizeChanged();
  193. void noteLengthChanged();
  194. void quantizeNotes();
  195. void updateSemiToneMarkerMenu();
  196. void changeNoteEditMode( int i );
  197. void markSemiTone( int i );
  198. void hidePattern( Pattern* pattern );
  199. void selectRegionFromPixels( int xStart, int xEnd );
  200. void clearGhostPattern();
  201. signals:
  202. void currentPatternChanged();
  203. void ghostPatternSet(bool);
  204. void semiToneMarkerMenuScaleSetEnabled(bool);
  205. void semiToneMarkerMenuChordSetEnabled(bool);
  206. private:
  207. enum Actions
  208. {
  209. ActionNone,
  210. ActionMoveNote,
  211. ActionResizeNote,
  212. ActionSelectNotes,
  213. ActionChangeNoteProperty,
  214. ActionResizeNoteEditArea
  215. };
  216. enum NoteEditMode
  217. {
  218. NoteEditVolume,
  219. NoteEditPanning,
  220. NoteEditCount // make sure this one is always last
  221. };
  222. enum SemiToneMarkerAction
  223. {
  224. stmaUnmarkAll,
  225. stmaMarkCurrentSemiTone,
  226. stmaMarkAllOctaveSemiTones,
  227. stmaMarkCurrentScale,
  228. stmaMarkCurrentChord,
  229. stmaCopyAllNotesOnKey
  230. };
  231. enum PianoRollKeyTypes
  232. {
  233. PR_WHITE_KEY_SMALL,
  234. PR_WHITE_KEY_BIG,
  235. PR_BLACK_KEY
  236. };
  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. PianoRoll();
  242. PianoRoll( const PianoRoll & );
  243. virtual ~PianoRoll();
  244. void autoScroll(const MidiTime & t );
  245. MidiTime newNoteLen() const;
  246. void shiftPos(int amount);
  247. void shiftSemiTone(int amount);
  248. bool isSelection() const;
  249. int selectionCount() const;
  250. void testPlayNote( Note * n );
  251. void testPlayKey( int _key, int _vol, int _pan );
  252. void pauseTestNotes(bool pause = true );
  253. void playChordNotes(int key, int velocity=-1);
  254. void pauseChordNotes(int key);
  255. QList<int> getAllOctavesForKey( int keyToMirror ) const;
  256. int noteEditTop() const;
  257. int keyAreaBottom() const;
  258. int noteEditBottom() const;
  259. int keyAreaTop() const;
  260. int noteEditRight() const;
  261. int noteEditLeft() const;
  262. void dragNotes( int x, int y, bool alt, bool shift, bool ctrl );
  263. static const int cm_scrollAmtHoriz = 10;
  264. static const int cm_scrollAmtVert = 1;
  265. static QPixmap * s_whiteKeyBigPm;
  266. static QPixmap * s_whiteKeyBigPressedPm;
  267. static QPixmap * s_whiteKeySmallPm;
  268. static QPixmap * s_whiteKeySmallPressedPm;
  269. static QPixmap * s_blackKeyPm;
  270. static QPixmap * s_blackKeyPressedPm;
  271. static QPixmap * s_toolDraw;
  272. static QPixmap * s_toolErase;
  273. static QPixmap * s_toolSelect;
  274. static QPixmap * s_toolMove;
  275. static QPixmap * s_toolOpen;
  276. static PianoRollKeyTypes prKeyOrder[];
  277. static TextFloat * s_textFloat;
  278. ComboBoxModel m_zoomingModel;
  279. ComboBoxModel m_quantizeModel;
  280. ComboBoxModel m_noteLenModel;
  281. ComboBoxModel m_scaleModel;
  282. ComboBoxModel m_chordModel;
  283. static const QVector<double> m_zoomLevels;
  284. Pattern* m_pattern;
  285. NoteVector m_ghostNotes;
  286. inline const NoteVector & ghostNotes() const
  287. {
  288. return m_ghostNotes;
  289. }
  290. QScrollBar * m_leftRightScroll;
  291. QScrollBar * m_topBottomScroll;
  292. MidiTime m_currentPosition;
  293. bool m_recording;
  294. QList<Note> m_recordingNotes;
  295. Note * m_currentNote;
  296. Actions m_action;
  297. NoteEditMode m_noteEditMode;
  298. int m_selectStartTick;
  299. int m_selectedTick;
  300. int m_selectStartKey;
  301. int m_selectedKeys;
  302. // boundary box around all selected notes when dragging
  303. int m_moveBoundaryLeft;
  304. int m_moveBoundaryTop;
  305. int m_moveBoundaryRight;
  306. int m_moveBoundaryBottom;
  307. // remember where the scrolling started when dragging so that
  308. // we can handle dragging while scrolling with arrow keys
  309. int m_mouseDownKey;
  310. int m_mouseDownTick;
  311. // remember the last x and y of a mouse movement
  312. int m_lastMouseX;
  313. int m_lastMouseY;
  314. // x,y of when the user starts a drag
  315. int m_moveStartX;
  316. int m_moveStartY;
  317. int m_oldNotesEditHeight;
  318. int m_notesEditHeight;
  319. int m_ppb; // pixels per bar
  320. int m_totalKeysToScroll;
  321. // remember these values to use them
  322. // for the next note that is set
  323. MidiTime m_lenOfNewNotes;
  324. volume_t m_lastNoteVolume;
  325. panning_t m_lastNotePanning;
  326. int m_startKey; // first key when drawing
  327. int m_lastKey;
  328. EditModes m_editMode;
  329. EditModes m_ctrlMode; // mode they were in before they hit ctrl
  330. bool m_mouseDownRight; //true if right click is being held down
  331. TimeLineWidget * m_timeLine;
  332. bool m_scrollBack;
  333. void copyToClipboard(const NoteVector & notes ) const;
  334. void drawDetuningInfo( QPainter & _p, const Note * _n, int _x, int _y ) const;
  335. bool mouseOverNote();
  336. Note * noteUnderMouse();
  337. // turn a selection rectangle into selected notes
  338. void computeSelectedNotes( bool shift );
  339. void clearSelectedNotes();
  340. // did we start a mouseclick with shift pressed
  341. bool m_startedWithShift;
  342. friend class PianoRollWindow;
  343. StepRecorderWidget m_stepRecorderWidget;
  344. StepRecorder m_stepRecorder;
  345. // qproperty fields
  346. QColor m_barLineColor;
  347. QColor m_beatLineColor;
  348. QColor m_lineColor;
  349. QColor m_noteModeColor;
  350. QColor m_noteColor;
  351. QColor m_noteTextColor;
  352. QColor m_ghostNoteColor;
  353. QColor m_ghostNoteTextColor;
  354. QColor m_barColor;
  355. QColor m_selectedNoteColor;
  356. QColor m_textColor;
  357. QColor m_textColorLight;
  358. QColor m_textShadow;
  359. QColor m_markedSemitoneColor;
  360. int m_noteOpacity;
  361. int m_ghostNoteOpacity;
  362. bool m_noteBorders;
  363. bool m_ghostNoteBorders;
  364. QColor m_backgroundShade;
  365. signals:
  366. void positionChanged( const MidiTime & );
  367. } ;
  368. class PianoRollWindow : public Editor, SerializingObject
  369. {
  370. Q_OBJECT
  371. public:
  372. PianoRollWindow();
  373. const Pattern* currentPattern() const;
  374. void setCurrentPattern( Pattern* pattern );
  375. void setGhostPattern( Pattern* pattern );
  376. int quantization() const;
  377. void play() override;
  378. void stop() override;
  379. void record() override;
  380. void recordAccompany() override;
  381. void toggleStepRecording() override;
  382. void stopRecording();
  383. bool isRecording() const;
  384. /*! \brief Resets settings to default when e.g. creating a new project */
  385. void reset();
  386. using SerializingObject::saveState;
  387. using SerializingObject::restoreState;
  388. void saveSettings(QDomDocument & doc, QDomElement & de ) override;
  389. void loadSettings( const QDomElement & de ) override;
  390. inline QString nodeName() const override
  391. {
  392. return "pianoroll";
  393. }
  394. QSize sizeHint() const override;
  395. signals:
  396. void currentPatternChanged();
  397. private slots:
  398. void updateAfterPatternChange();
  399. void ghostPatternSet( bool state );
  400. private:
  401. void patternRenamed();
  402. void focusInEvent(QFocusEvent * event) override;
  403. void stopStepRecording();
  404. void updateStepRecordingIcon();
  405. PianoRoll* m_editor;
  406. ComboBox * m_zoomingComboBox;
  407. ComboBox * m_quantizeComboBox;
  408. ComboBox * m_noteLenComboBox;
  409. ComboBox * m_scaleComboBox;
  410. ComboBox * m_chordComboBox;
  411. QPushButton * m_clearGhostButton;
  412. };
  413. #endif