Track.h 15 KB

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