AutomatableModel.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * AutomatableModel.h - declaration of class AutomatableModel
  3. *
  4. * Copyright (c) 2007-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef AUTOMATABLE_MODEL_H
  25. #define AUTOMATABLE_MODEL_H
  26. #include <QtCore/QMap>
  27. #include <QtCore/QMutex>
  28. #include "JournallingObject.h"
  29. #include "Model.h"
  30. #include "TimePos.h"
  31. #include "ValueBuffer.h"
  32. #include "MemoryManager.h"
  33. #include "ModelVisitor.h"
  34. // simple way to map a property of a view to a model
  35. #define mapPropertyFromModelPtr(type,getfunc,setfunc,modelname) \
  36. public: \
  37. type getfunc() const \
  38. { \
  39. return (type) modelname->value(); \
  40. } \
  41. public slots: \
  42. void setfunc( const type val ) \
  43. { \
  44. modelname->setValue( val ); \
  45. }
  46. #define mapPropertyFromModel(type,getfunc,setfunc,modelname) \
  47. public: \
  48. type getfunc() const \
  49. { \
  50. return (type) modelname.value(); \
  51. } \
  52. public slots: \
  53. void setfunc( const type val ) \
  54. { \
  55. modelname.setValue( (float) val ); \
  56. }
  57. // use this to make subclasses visitable
  58. #define MODEL_IS_VISITABLE \
  59. void accept(ModelVisitor& v) override { v.visit(*this); } \
  60. void accept(ConstModelVisitor& v) const override { v.visit(*this); }
  61. class ControllerConnection;
  62. class LMMS_EXPORT AutomatableModel : public Model, public JournallingObject
  63. {
  64. Q_OBJECT
  65. MM_OPERATORS
  66. public:
  67. typedef QVector<AutomatableModel *> AutoModelVector;
  68. enum ScaleType
  69. {
  70. Linear,
  71. Logarithmic,
  72. Decibel
  73. };
  74. virtual ~AutomatableModel();
  75. // Implement those by using the MODEL_IS_VISITABLE macro
  76. virtual void accept(ModelVisitor& v) = 0;
  77. virtual void accept(ConstModelVisitor& v) const = 0;
  78. public:
  79. /**
  80. @brief Return this class casted to Target
  81. @test AutomatableModelTest.cpp
  82. @param doThrow throw an assertion if the cast fails, instead of
  83. returning a nullptr
  84. @return the casted class if Target is the exact or a base class of
  85. *this, nullptr otherwise
  86. */
  87. template<class Target>
  88. Target* dynamicCast(bool doThrow = false)
  89. {
  90. DCastVisitor<Target> vis; accept(vis);
  91. if (doThrow && !vis.result) { Q_ASSERT(false); }
  92. return vis.result;
  93. }
  94. //! const overload, see overloaded function
  95. template<class Target>
  96. const Target* dynamicCast(bool doThrow = false) const
  97. {
  98. ConstDCastVisitor<Target> vis; accept(vis);
  99. if (doThrow && !vis.result) { Q_ASSERT(false); }
  100. return vis.result;
  101. }
  102. bool isAutomated() const;
  103. bool isAutomatedOrControlled() const
  104. {
  105. return isAutomated() || m_controllerConnection != nullptr;
  106. }
  107. ControllerConnection* controllerConnection() const
  108. {
  109. return m_controllerConnection;
  110. }
  111. void setControllerConnection( ControllerConnection* c );
  112. template<class T>
  113. static T castValue( const float v )
  114. {
  115. return (T)( v );
  116. }
  117. template<bool>
  118. static bool castValue( const float v )
  119. {
  120. return ( qRound( v ) != 0 );
  121. }
  122. template<class T>
  123. inline T value( int frameOffset = 0 ) const
  124. {
  125. if (m_controllerConnection)
  126. {
  127. if (!m_useControllerValue)
  128. {
  129. return castValue<T>(m_value);
  130. }
  131. else
  132. {
  133. return castValue<T>(controllerValue(frameOffset));
  134. }
  135. }
  136. else if (hasLinkedModels())
  137. {
  138. return castValue<T>( controllerValue( frameOffset ) );
  139. }
  140. return castValue<T>( m_value );
  141. }
  142. float controllerValue( int frameOffset ) const;
  143. //! @brief Function that returns sample-exact data as a ValueBuffer
  144. //! @return pointer to model's valueBuffer when s.ex.data exists, NULL otherwise
  145. ValueBuffer * valueBuffer();
  146. template<class T>
  147. T initValue() const
  148. {
  149. return castValue<T>( m_initValue );
  150. }
  151. bool isAtInitValue() const
  152. {
  153. return m_value == m_initValue;
  154. }
  155. template<class T>
  156. T minValue() const
  157. {
  158. return castValue<T>( m_minValue );
  159. }
  160. template<class T>
  161. T maxValue() const
  162. {
  163. return castValue<T>( m_maxValue );
  164. }
  165. template<class T>
  166. T step() const
  167. {
  168. return castValue<T>( m_step );
  169. }
  170. //! @brief Returns value scaled with the scale type and min/max values of this model
  171. float scaledValue( float value ) const;
  172. //! @brief Returns value applied with the inverse of this model's scale type
  173. float inverseScaledValue( float value ) const;
  174. void setInitValue( const float value );
  175. void setAutomatedValue( const float value );
  176. void setValue( const float value );
  177. void incValue( int steps )
  178. {
  179. setValue( m_value + steps * m_step );
  180. }
  181. float range() const
  182. {
  183. return m_range;
  184. }
  185. void setRange( const float min, const float max, const float step = 1 );
  186. void setScaleType( ScaleType sc ) {
  187. m_scaleType = sc;
  188. }
  189. void setScaleLogarithmic( bool setToTrue = true )
  190. {
  191. setScaleType( setToTrue ? Logarithmic : Linear );
  192. }
  193. bool isScaleLogarithmic() const
  194. {
  195. return m_scaleType == Logarithmic;
  196. }
  197. void setStep( const float step );
  198. float centerValue() const
  199. {
  200. return m_centerValue;
  201. }
  202. void setCenterValue( const float centerVal )
  203. {
  204. m_centerValue = centerVal;
  205. }
  206. //! link @p m1 and @p m2, let @p m1 take the values of @p m2
  207. static void linkModels( AutomatableModel* m1, AutomatableModel* m2 );
  208. static void unlinkModels( AutomatableModel* m1, AutomatableModel* m2 );
  209. void unlinkAllModels();
  210. /**
  211. * @brief Saves settings (value, automation links and controller connections) of AutomatableModel into
  212. * specified DOM element using <name> as attribute/node name
  213. * @param doc TODO
  214. * @param element Where this option shall be saved.
  215. * Depending on the model, this can be done in an attribute or in a subnode.
  216. * @param name Name to store this model as.
  217. */
  218. virtual void saveSettings( QDomDocument& doc, QDomElement& element, const QString& name );
  219. /*! \brief Loads settings (value, automation links and controller connections) of AutomatableModel from
  220. specified DOM element using <name> as attribute/node name */
  221. virtual void loadSettings( const QDomElement& element, const QString& name );
  222. QString nodeName() const override
  223. {
  224. return "automatablemodel";
  225. }
  226. virtual QString displayValue( const float val ) const = 0;
  227. bool hasLinkedModels() const
  228. {
  229. return !m_linkedModels.empty();
  230. }
  231. // a way to track changed values in the model and avoid using signals/slots - useful for speed-critical code.
  232. // note that this method should only be called once per period since it resets the state of the variable - so if your model
  233. // has to be accessed by more than one object, then this function shouldn't be used.
  234. bool isValueChanged()
  235. {
  236. if( m_valueChanged || valueBuffer() )
  237. {
  238. m_valueChanged = false;
  239. return true;
  240. }
  241. return false;
  242. }
  243. float globalAutomationValueAt( const TimePos& time );
  244. void setStrictStepSize( const bool b )
  245. {
  246. m_hasStrictStepSize = b;
  247. }
  248. static void incrementPeriodCounter()
  249. {
  250. ++s_periodCounter;
  251. }
  252. static void resetPeriodCounter()
  253. {
  254. s_periodCounter = 0;
  255. }
  256. bool useControllerValue()
  257. {
  258. return m_useControllerValue;
  259. }
  260. public slots:
  261. virtual void reset();
  262. void unlinkControllerConnection();
  263. void setUseControllerValue(bool b = true);
  264. protected:
  265. AutomatableModel(
  266. const float val = 0,
  267. const float min = 0,
  268. const float max = 0,
  269. const float step = 0,
  270. Model* parent = nullptr,
  271. const QString& displayName = QString(),
  272. bool defaultConstructed = false );
  273. //! returns a value which is in range between min() and
  274. //! max() and aligned according to the step size (step size 0.05 -> value
  275. //! 0.12345 becomes 0.10 etc.). You should always call it at the end after
  276. //! doing your own calculations.
  277. float fittedValue( float value ) const;
  278. private:
  279. // dynamicCast implementation
  280. template<class Target>
  281. struct DCastVisitor : public ModelVisitor
  282. {
  283. Target* result = nullptr;
  284. void visit(Target& tar) { result = &tar; }
  285. };
  286. // dynamicCast implementation
  287. template<class Target>
  288. struct ConstDCastVisitor : public ConstModelVisitor
  289. {
  290. const Target* result = nullptr;
  291. void visit(const Target& tar) { result = &tar; }
  292. };
  293. static bool mustQuoteName(const QString &name);
  294. void saveSettings( QDomDocument& doc, QDomElement& element ) override
  295. {
  296. saveSettings( doc, element, "value" );
  297. }
  298. void loadSettings( const QDomElement& element ) override
  299. {
  300. loadSettings( element, "value" );
  301. }
  302. void linkModel( AutomatableModel* model );
  303. void unlinkModel( AutomatableModel* model );
  304. //! @brief Scales @value from linear to logarithmic.
  305. //! Value should be within [0,1]
  306. template<class T> T logToLinearScale( T value ) const;
  307. //! rounds @a value to @a where if it is close to it
  308. //! @param value will be modified to rounded value
  309. template<class T> void roundAt( T &value, const T &where ) const;
  310. ScaleType m_scaleType; //!< scale type, linear by default
  311. float m_value;
  312. float m_initValue;
  313. float m_minValue;
  314. float m_maxValue;
  315. float m_step;
  316. float m_range;
  317. float m_centerValue;
  318. bool m_valueChanged;
  319. // currently unused?
  320. float m_oldValue;
  321. int m_setValueDepth;
  322. // used to determine if step size should be applied strictly (ie. always)
  323. // or only when value set from gui (default)
  324. bool m_hasStrictStepSize;
  325. AutoModelVector m_linkedModels;
  326. //! NULL if not appended to controller, otherwise connection info
  327. ControllerConnection* m_controllerConnection;
  328. ValueBuffer m_valueBuffer;
  329. long m_lastUpdatedPeriod;
  330. static long s_periodCounter;
  331. bool m_hasSampleExactData;
  332. // prevent several threads from attempting to write the same vb at the same time
  333. QMutex m_valueBufferMutex;
  334. bool m_useControllerValue;
  335. signals:
  336. void initValueChanged( float val );
  337. void destroyed( jo_id_t id );
  338. } ;
  339. template <typename T> class LMMS_EXPORT TypedAutomatableModel : public AutomatableModel
  340. {
  341. public:
  342. using AutomatableModel::AutomatableModel;
  343. T value( int frameOffset = 0 ) const
  344. {
  345. return AutomatableModel::value<T>( frameOffset );
  346. }
  347. T initValue() const
  348. {
  349. return AutomatableModel::initValue<T>();
  350. }
  351. T minValue() const
  352. {
  353. return AutomatableModel::minValue<T>();
  354. }
  355. T maxValue() const
  356. {
  357. return AutomatableModel::maxValue<T>();
  358. }
  359. };
  360. // some typed AutomatableModel-definitions
  361. class LMMS_EXPORT FloatModel : public TypedAutomatableModel<float>
  362. {
  363. Q_OBJECT
  364. MODEL_IS_VISITABLE
  365. public:
  366. FloatModel( float val = 0, float min = 0, float max = 0, float step = 0,
  367. Model * parent = nullptr,
  368. const QString& displayName = QString(),
  369. bool defaultConstructed = false ) :
  370. TypedAutomatableModel( val, min, max, step, parent, displayName, defaultConstructed )
  371. {
  372. }
  373. float getRoundedValue() const;
  374. int getDigitCount() const;
  375. QString displayValue( const float val ) const override;
  376. } ;
  377. class LMMS_EXPORT IntModel : public TypedAutomatableModel<int>
  378. {
  379. Q_OBJECT
  380. MODEL_IS_VISITABLE
  381. public:
  382. IntModel( int val = 0, int min = 0, int max = 0,
  383. Model* parent = nullptr,
  384. const QString& displayName = QString(),
  385. bool defaultConstructed = false ) :
  386. TypedAutomatableModel( val, min, max, 1, parent, displayName, defaultConstructed )
  387. {
  388. }
  389. QString displayValue( const float val ) const override;
  390. } ;
  391. class LMMS_EXPORT BoolModel : public TypedAutomatableModel<bool>
  392. {
  393. Q_OBJECT
  394. MODEL_IS_VISITABLE
  395. public:
  396. BoolModel( const bool val = false,
  397. Model* parent = nullptr,
  398. const QString& displayName = QString(),
  399. bool defaultConstructed = false ) :
  400. TypedAutomatableModel( val, false, true, 1, parent, displayName, defaultConstructed )
  401. {
  402. }
  403. QString displayValue( const float val ) const override;
  404. } ;
  405. typedef QMap<AutomatableModel*, float> AutomatedValueMap;
  406. #endif