Track.h 15 KB

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