Track.h 16 KB

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