Track.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * Track.h - declaration of classes concerning tracks -> necessary for all
  3. * track-like objects (beat/bassline, sample-track...)
  4. *
  5. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef TRACK_H
  26. #define TRACK_H
  27. #include <QtCore/QVector>
  28. #include <QtCore/QList>
  29. #include <QWidget>
  30. #include <QColor>
  31. #include <QMimeData>
  32. #include "lmms_basics.h"
  33. #include "MidiTime.h"
  34. #include "Rubberband.h"
  35. #include "JournallingObject.h"
  36. #include "AutomatableModel.h"
  37. #include "ModelView.h"
  38. #include "DataFile.h"
  39. class QMenu;
  40. class QPushButton;
  41. class PixmapButton;
  42. class TextFloat;
  43. class Track;
  44. class TrackContentObjectView;
  45. class TrackContainer;
  46. class TrackContainerView;
  47. class TrackContentWidget;
  48. class TrackView;
  49. const int DEFAULT_SETTINGS_WIDGET_WIDTH = 224;
  50. const int TRACK_OP_WIDTH = 78;
  51. // This shaves 150-ish pixels off track buttons,
  52. // ruled from config: ui.compacttrackbuttons
  53. const int DEFAULT_SETTINGS_WIDGET_WIDTH_COMPACT = 96;
  54. const int TRACK_OP_WIDTH_COMPACT = 62;
  55. /*! The minimum track height in pixels
  56. *
  57. * Tracks can be resized by shift-dragging anywhere inside the track
  58. * display. This sets the minimum size in pixels for a track.
  59. */
  60. const int MINIMAL_TRACK_HEIGHT = 32;
  61. const int DEFAULT_TRACK_HEIGHT = 32;
  62. const int TCO_BORDER_WIDTH = 2;
  63. class LMMS_EXPORT TrackContentObject : public Model, public JournallingObject
  64. {
  65. Q_OBJECT
  66. MM_OPERATORS
  67. mapPropertyFromModel(bool,isMuted,setMuted,m_mutedModel);
  68. mapPropertyFromModel(bool,isSolo,setSolo,m_soloModel);
  69. public:
  70. TrackContentObject( Track * track );
  71. virtual ~TrackContentObject();
  72. inline Track * getTrack() const
  73. {
  74. return m_track;
  75. }
  76. inline const QString & name() const
  77. {
  78. return m_name;
  79. }
  80. inline void setName( const QString & name )
  81. {
  82. m_name = name;
  83. emit dataChanged();
  84. }
  85. QString displayName() const override
  86. {
  87. return name();
  88. }
  89. inline const MidiTime & startPosition() const
  90. {
  91. return m_startPosition;
  92. }
  93. inline MidiTime endPosition() const
  94. {
  95. const int sp = m_startPosition;
  96. return sp + m_length;
  97. }
  98. inline const MidiTime & length() const
  99. {
  100. return m_length;
  101. }
  102. inline void setAutoResize( const bool r )
  103. {
  104. m_autoResize = r;
  105. }
  106. inline const bool getAutoResize() const
  107. {
  108. return m_autoResize;
  109. }
  110. virtual void movePosition( const MidiTime & pos );
  111. virtual void changeLength( const MidiTime & length );
  112. virtual TrackContentObjectView * createView( TrackView * tv ) = 0;
  113. inline void selectViewOnCreate( bool select )
  114. {
  115. m_selectViewOnCreate = select;
  116. }
  117. inline bool getSelectViewOnCreate()
  118. {
  119. return m_selectViewOnCreate;
  120. }
  121. /// Returns true if and only if a->startPosition() < b->startPosition()
  122. static bool comparePosition(const TrackContentObject* a, const TrackContentObject* b);
  123. MidiTime startTimeOffset() const;
  124. void setStartTimeOffset( const MidiTime &startTimeOffset );
  125. public slots:
  126. void copy();
  127. void paste();
  128. void toggleMute();
  129. signals:
  130. void lengthChanged();
  131. void positionChanged();
  132. void destroyedTCO();
  133. private:
  134. enum Actions
  135. {
  136. NoAction,
  137. Move,
  138. Resize
  139. } ;
  140. Track * m_track;
  141. QString m_name;
  142. MidiTime m_startPosition;
  143. MidiTime m_length;
  144. MidiTime m_startTimeOffset;
  145. BoolModel m_mutedModel;
  146. BoolModel m_soloModel;
  147. bool m_autoResize;
  148. bool m_selectViewOnCreate;
  149. friend class TrackContentObjectView;
  150. } ;
  151. class TrackContentObjectView : public selectableObject, public ModelView
  152. {
  153. Q_OBJECT
  154. // theming qproperties
  155. Q_PROPERTY( QColor mutedColor READ mutedColor WRITE setMutedColor )
  156. Q_PROPERTY( QColor mutedBackgroundColor READ mutedBackgroundColor WRITE setMutedBackgroundColor )
  157. Q_PROPERTY( QColor selectedColor READ selectedColor WRITE setSelectedColor )
  158. Q_PROPERTY( QColor textColor READ textColor WRITE setTextColor )
  159. Q_PROPERTY( QColor textBackgroundColor READ textBackgroundColor WRITE setTextBackgroundColor )
  160. Q_PROPERTY( QColor textShadowColor READ textShadowColor WRITE setTextShadowColor )
  161. Q_PROPERTY( QColor BBPatternBackground READ BBPatternBackground WRITE setBBPatternBackground )
  162. Q_PROPERTY( bool gradient READ gradient WRITE setGradient )
  163. public:
  164. TrackContentObjectView( TrackContentObject * tco, TrackView * tv );
  165. virtual ~TrackContentObjectView();
  166. bool fixedTCOs();
  167. inline TrackContentObject * getTrackContentObject()
  168. {
  169. return m_tco;
  170. }
  171. inline TrackView * getTrackView()
  172. {
  173. return m_trackView;
  174. }
  175. // qproperty access func
  176. QColor mutedColor() const;
  177. QColor mutedBackgroundColor() const;
  178. QColor selectedColor() const;
  179. QColor textColor() const;
  180. QColor textBackgroundColor() const;
  181. QColor textShadowColor() const;
  182. QColor BBPatternBackground() const;
  183. bool gradient() const;
  184. void setMutedColor( const QColor & c );
  185. void setMutedBackgroundColor( const QColor & c );
  186. void setSelectedColor( const QColor & c );
  187. void setTextColor( const QColor & c );
  188. void setTextBackgroundColor( const QColor & c );
  189. void setTextShadowColor( const QColor & c );
  190. void setBBPatternBackground( const QColor & c );
  191. void setGradient( const bool & b );
  192. // access needsUpdate member variable
  193. bool needsUpdate();
  194. void setNeedsUpdate( bool b );
  195. public slots:
  196. virtual bool close();
  197. void cut();
  198. void remove();
  199. void update() override;
  200. protected:
  201. virtual void constructContextMenu( QMenu * )
  202. {
  203. }
  204. void contextMenuEvent( QContextMenuEvent * cme ) override;
  205. void dragEnterEvent( QDragEnterEvent * dee ) override;
  206. void dropEvent( QDropEvent * de ) override;
  207. void leaveEvent( QEvent * e ) override;
  208. void mousePressEvent( QMouseEvent * me ) override;
  209. void mouseMoveEvent( QMouseEvent * me ) override;
  210. void mouseReleaseEvent( QMouseEvent * me ) override;
  211. void resizeEvent( QResizeEvent * re ) override
  212. {
  213. m_needsUpdate = true;
  214. selectableObject::resizeEvent( re );
  215. }
  216. float pixelsPerBar();
  217. DataFile createTCODataFiles(const QVector<TrackContentObjectView *> & tcos) const;
  218. virtual void paintTextLabel(QString const & text, QPainter & painter);
  219. protected slots:
  220. void updateLength();
  221. void updatePosition();
  222. private:
  223. enum Actions
  224. {
  225. NoAction,
  226. Move,
  227. MoveSelection,
  228. Resize,
  229. ResizeLeft,
  230. CopySelection,
  231. ToggleSelected
  232. } ;
  233. static TextFloat * s_textFloat;
  234. TrackContentObject * m_tco;
  235. TrackView * m_trackView;
  236. Actions m_action;
  237. QPoint m_initialMousePos;
  238. QPoint m_initialMouseGlobalPos;
  239. MidiTime m_initialTCOPos;
  240. MidiTime m_initialTCOEnd;
  241. QVector<MidiTime> m_initialOffsets;
  242. TextFloat * m_hint;
  243. // qproperty fields
  244. QColor m_mutedColor;
  245. QColor m_mutedBackgroundColor;
  246. QColor m_selectedColor;
  247. QColor m_textColor;
  248. QColor m_textBackgroundColor;
  249. QColor m_textShadowColor;
  250. QColor m_BBPatternBackground;
  251. bool m_gradient;
  252. bool m_needsUpdate;
  253. inline void setInitialPos( QPoint pos )
  254. {
  255. m_initialMousePos = pos;
  256. m_initialMouseGlobalPos = mapToGlobal( pos );
  257. m_initialTCOPos = m_tco->startPosition();
  258. m_initialTCOEnd = m_initialTCOPos + m_tco->length();
  259. }
  260. void setInitialOffsets();
  261. bool mouseMovedDistance( QMouseEvent * me, int distance );
  262. MidiTime draggedTCOPos( QMouseEvent * me );
  263. } ;
  264. class TrackContentWidget : public QWidget, public JournallingObject
  265. {
  266. Q_OBJECT
  267. // qproperties for track background gradients
  268. Q_PROPERTY( QBrush darkerColor READ darkerColor WRITE setDarkerColor )
  269. Q_PROPERTY( QBrush lighterColor READ lighterColor WRITE setLighterColor )
  270. Q_PROPERTY( QBrush gridColor READ gridColor WRITE setGridColor )
  271. Q_PROPERTY( QBrush embossColor READ embossColor WRITE setEmbossColor )
  272. public:
  273. TrackContentWidget( TrackView * parent );
  274. virtual ~TrackContentWidget();
  275. /*! \brief Updates the background tile pixmap. */
  276. void updateBackground();
  277. void addTCOView( TrackContentObjectView * tcov );
  278. void removeTCOView( TrackContentObjectView * tcov );
  279. void removeTCOView( int tcoNum )
  280. {
  281. if( tcoNum >= 0 && tcoNum < m_tcoViews.size() )
  282. {
  283. removeTCOView( m_tcoViews[tcoNum] );
  284. }
  285. }
  286. bool canPasteSelection( MidiTime tcoPos, const QDropEvent *de );
  287. bool pasteSelection( MidiTime tcoPos, QDropEvent * de );
  288. MidiTime endPosition( const MidiTime & posStart );
  289. // qproperty access methods
  290. QBrush darkerColor() const;
  291. QBrush lighterColor() const;
  292. QBrush gridColor() const;
  293. QBrush embossColor() const;
  294. void setDarkerColor( const QBrush & c );
  295. void setLighterColor( const QBrush & c );
  296. void setGridColor( const QBrush & c );
  297. void setEmbossColor( const QBrush & c);
  298. public slots:
  299. void update();
  300. void changePosition( const MidiTime & newPos = MidiTime( -1 ) );
  301. protected:
  302. void dragEnterEvent( QDragEnterEvent * dee ) override;
  303. void dropEvent( QDropEvent * de ) override;
  304. void mousePressEvent( QMouseEvent * me ) override;
  305. void paintEvent( QPaintEvent * pe ) override;
  306. void resizeEvent( QResizeEvent * re ) override;
  307. QString nodeName() const override
  308. {
  309. return "trackcontentwidget";
  310. }
  311. void saveSettings( QDomDocument& doc, QDomElement& element ) override
  312. {
  313. Q_UNUSED(doc)
  314. Q_UNUSED(element)
  315. }
  316. void loadSettings( const QDomElement& element ) override
  317. {
  318. Q_UNUSED(element)
  319. }
  320. private:
  321. Track * getTrack();
  322. MidiTime getPosition( int mouseX );
  323. TrackView * m_trackView;
  324. typedef QVector<TrackContentObjectView *> tcoViewVector;
  325. tcoViewVector m_tcoViews;
  326. QPixmap m_background;
  327. // qproperty fields
  328. QBrush m_darkerColor;
  329. QBrush m_lighterColor;
  330. QBrush m_gridColor;
  331. QBrush m_embossColor;
  332. } ;
  333. class TrackOperationsWidget : public QWidget
  334. {
  335. Q_OBJECT
  336. public:
  337. TrackOperationsWidget( TrackView * parent );
  338. ~TrackOperationsWidget();
  339. protected:
  340. void mousePressEvent( QMouseEvent * me ) override;
  341. void paintEvent( QPaintEvent * pe ) override;
  342. private slots:
  343. void cloneTrack();
  344. void removeTrack();
  345. void updateMenu();
  346. void toggleRecording(bool on);
  347. void recordingOn();
  348. void recordingOff();
  349. void clearTrack();
  350. private:
  351. static QPixmap * s_grip;
  352. TrackView * m_trackView;
  353. QPushButton * m_trackOps;
  354. PixmapButton * m_muteBtn;
  355. PixmapButton * m_soloBtn;
  356. friend class TrackView;
  357. signals:
  358. void trackRemovalScheduled( TrackView * t );
  359. } ;
  360. // base-class for all tracks
  361. class LMMS_EXPORT Track : public Model, public JournallingObject
  362. {
  363. Q_OBJECT
  364. MM_OPERATORS
  365. mapPropertyFromModel(bool,isMuted,setMuted,m_mutedModel);
  366. mapPropertyFromModel(bool,isSolo,setSolo,m_soloModel);
  367. public:
  368. typedef QVector<TrackContentObject *> tcoVector;
  369. enum TrackTypes
  370. {
  371. InstrumentTrack,
  372. BBTrack,
  373. SampleTrack,
  374. EventTrack,
  375. VideoTrack,
  376. AutomationTrack,
  377. HiddenAutomationTrack,
  378. NumTrackTypes
  379. } ;
  380. Track( TrackTypes type, TrackContainer * tc );
  381. virtual ~Track();
  382. static Track * create( TrackTypes tt, TrackContainer * tc );
  383. static Track * create( const QDomElement & element,
  384. TrackContainer * tc );
  385. Track * clone();
  386. // pure virtual functions
  387. TrackTypes type() const
  388. {
  389. return m_type;
  390. }
  391. virtual bool play( const MidiTime & start, const fpp_t frames,
  392. const f_cnt_t frameBase, int tcoNum = -1 ) = 0;
  393. virtual TrackView * createView( TrackContainerView * view ) = 0;
  394. virtual TrackContentObject * createTCO( const MidiTime & pos ) = 0;
  395. virtual void saveTrackSpecificSettings( QDomDocument & doc,
  396. QDomElement & parent ) = 0;
  397. virtual void loadTrackSpecificSettings( const QDomElement & element ) = 0;
  398. void saveSettings( QDomDocument & doc, QDomElement & element ) override;
  399. void loadSettings( const QDomElement & element ) override;
  400. void setSimpleSerializing()
  401. {
  402. m_simpleSerializingMode = true;
  403. }
  404. // -- for usage by TrackContentObject only ---------------
  405. TrackContentObject * addTCO( TrackContentObject * tco );
  406. void removeTCO( TrackContentObject * tco );
  407. // -------------------------------------------------------
  408. void deleteTCOs();
  409. int numOfTCOs();
  410. TrackContentObject * getTCO( int tcoNum );
  411. int getTCONum(const TrackContentObject* tco );
  412. const tcoVector & getTCOs() const
  413. {
  414. return m_trackContentObjects;
  415. }
  416. void getTCOsInRange( tcoVector & tcoV, const MidiTime & start,
  417. const MidiTime & end );
  418. void swapPositionOfTCOs( int tcoNum1, int tcoNum2 );
  419. void createTCOsForBB( int bb );
  420. void insertBar( const MidiTime & pos );
  421. void removeBar( const MidiTime & pos );
  422. bar_t length() const;
  423. inline TrackContainer* trackContainer() const
  424. {
  425. return m_trackContainer;
  426. }
  427. // name-stuff
  428. virtual const QString & name() const
  429. {
  430. return m_name;
  431. }
  432. QString displayName() const override
  433. {
  434. return name();
  435. }
  436. using Model::dataChanged;
  437. inline int getHeight()
  438. {
  439. return m_height >= MINIMAL_TRACK_HEIGHT
  440. ? m_height
  441. : DEFAULT_TRACK_HEIGHT;
  442. }
  443. inline void setHeight( int height )
  444. {
  445. m_height = height;
  446. }
  447. void lock()
  448. {
  449. m_processingLock.lock();
  450. }
  451. void unlock()
  452. {
  453. m_processingLock.unlock();
  454. }
  455. bool tryLock()
  456. {
  457. return m_processingLock.tryLock();
  458. }
  459. BoolModel* getMutedModel();
  460. public slots:
  461. virtual void setName( const QString & newName )
  462. {
  463. m_name = newName;
  464. emit nameChanged();
  465. }
  466. void toggleSolo();
  467. private:
  468. TrackContainer* m_trackContainer;
  469. TrackTypes m_type;
  470. QString m_name;
  471. int m_height;
  472. protected:
  473. BoolModel m_mutedModel;
  474. private:
  475. BoolModel m_soloModel;
  476. bool m_mutedBeforeSolo;
  477. bool m_simpleSerializingMode;
  478. tcoVector m_trackContentObjects;
  479. QMutex m_processingLock;
  480. friend class TrackView;
  481. signals:
  482. void destroyedTrack();
  483. void nameChanged();
  484. void trackContentObjectAdded( TrackContentObject * );
  485. } ;
  486. class TrackView : public QWidget, public ModelView, public JournallingObject
  487. {
  488. Q_OBJECT
  489. public:
  490. TrackView( Track * _track, TrackContainerView* tcv );
  491. virtual ~TrackView();
  492. inline const Track * getTrack() const
  493. {
  494. return m_track;
  495. }
  496. inline Track * getTrack()
  497. {
  498. return m_track;
  499. }
  500. inline TrackContainerView* trackContainerView()
  501. {
  502. return m_trackContainerView;
  503. }
  504. inline TrackOperationsWidget * getTrackOperationsWidget()
  505. {
  506. return &m_trackOperationsWidget;
  507. }
  508. inline QWidget * getTrackSettingsWidget()
  509. {
  510. return &m_trackSettingsWidget;
  511. }
  512. inline TrackContentWidget * getTrackContentWidget()
  513. {
  514. return &m_trackContentWidget;
  515. }
  516. bool isMovingTrack() const
  517. {
  518. return m_action == MoveTrack;
  519. }
  520. virtual void update();
  521. // Create a menu for assigning/creating channels for this track
  522. // Currently instrument track and sample track supports it
  523. virtual QMenu * createFxMenu(QString title, QString newFxLabel);
  524. public slots:
  525. virtual bool close();
  526. protected:
  527. void modelChanged() override;
  528. void saveSettings( QDomDocument& doc, QDomElement& element ) override
  529. {
  530. Q_UNUSED(doc)
  531. Q_UNUSED(element)
  532. }
  533. void loadSettings( const QDomElement& element ) override
  534. {
  535. Q_UNUSED(element)
  536. }
  537. QString nodeName() const override
  538. {
  539. return "trackview";
  540. }
  541. void dragEnterEvent( QDragEnterEvent * dee ) override;
  542. void dropEvent( QDropEvent * de ) override;
  543. void mousePressEvent( QMouseEvent * me ) override;
  544. void mouseMoveEvent( QMouseEvent * me ) override;
  545. void mouseReleaseEvent( QMouseEvent * me ) override;
  546. void paintEvent( QPaintEvent * pe ) override;
  547. void resizeEvent( QResizeEvent * re ) override;
  548. private:
  549. enum Actions
  550. {
  551. NoAction,
  552. MoveTrack,
  553. ResizeTrack
  554. } ;
  555. Track * m_track;
  556. TrackContainerView * m_trackContainerView;
  557. TrackOperationsWidget m_trackOperationsWidget;
  558. QWidget m_trackSettingsWidget;
  559. TrackContentWidget m_trackContentWidget;
  560. Actions m_action;
  561. friend class TrackLabelButton;
  562. private slots:
  563. void createTCOView( TrackContentObject * tco );
  564. } ;
  565. #endif