LinkedModelGroups.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * LinkedModelGroups.h - base classes for groups of linkable models
  3. *
  4. * Copyright (c) 2019-2019 Johannes Lorenz <j.git$$$lorenz-ho.me, $$$=@>
  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 LINKEDMODELGROUPS_H
  25. #define LINKEDMODELGROUPS_H
  26. #include <memory>
  27. #include <vector>
  28. #include "Model.h"
  29. /**
  30. @file LinkedModelGroups.h
  31. See Lv2ControlBase.h and Lv2Proc.h for example usage
  32. */
  33. /**
  34. Base class for a group of linkable models
  35. See the LinkedModelGroup class for explenations
  36. */
  37. class LinkedModelGroup : public Model
  38. {
  39. Q_OBJECT
  40. signals:
  41. //! Signal emitted after any of the per-control link-enabled models switch
  42. void linkStateChanged(int id, bool value);
  43. public:
  44. /*
  45. Initialization
  46. */
  47. //! @param parent model of the LinkedModelGroups class
  48. LinkedModelGroup(Model* parent) : Model(parent) {}
  49. //! After all models have been added, make this processor the one which
  50. //! will contain link models associated with its controls
  51. void makeLinkingProc();
  52. /*
  53. Linking
  54. */
  55. //! Set all per-control link-enabled models to @p state, which will
  56. //! also link or unlink them (via `Lv2ControlBase::linkPort()`)
  57. void linkAllModels(bool state);
  58. //! Link specified port with the associated port of @p other
  59. //! @param id id of the port, conforming to m_models
  60. void linkControls(LinkedModelGroup* other, int id);
  61. //! @see linkControls
  62. void unlinkControls(LinkedModelGroup *other, int id);
  63. //! Return whether this is the first of more than one processors
  64. bool isLinking() { return m_linkEnabled.size(); }
  65. /*
  66. Models
  67. */
  68. class BoolModel* linkEnabledModel(std::size_t id) {
  69. return m_linkEnabled[id]; }
  70. std::vector<class AutomatableModel*> models() { return m_models; }
  71. protected:
  72. //! Register a further model
  73. void addModel(class AutomatableModel* model);
  74. private slots:
  75. //! Callback called after any of the per-control link-enabled models switch
  76. void linkStateChangedSlot();
  77. private:
  78. //! models for the per-control link-enabled models
  79. std::vector<class BoolModel*> m_linkEnabled;
  80. //! models for the controls; the vector defines indices for the controls
  81. std::vector<class AutomatableModel*> m_models;
  82. };
  83. /**
  84. Container for multiple equal groups of linked models
  85. Each group contains the same models and model types. The models are
  86. numbered, and equal numbered models are associated and can be linked.
  87. A typical application are two mono plugins making a stereo plugin.
  88. Inheriting classes need to do the following connections, where the slots
  89. must be defined by those classes and call the equal named functions of this
  90. class:
  91. \code
  92. if(multiChannelLinkModel()) {
  93. connect(multiChannelLinkModel(), SIGNAL(dataChanged()),
  94. this, SLOT(updateLinkStatesFromGlobal()));
  95. connect(getGroup(0), SIGNAL(linkStateChanged(int, bool)),
  96. this, SLOT(linkPort(int, bool)));
  97. }
  98. \endcode
  99. */
  100. class LinkedModelGroups
  101. {
  102. public:
  103. virtual ~LinkedModelGroups();
  104. //! Return the model for multi channel linking
  105. BoolModel* multiChannelLinkModel() { return m_multiChannelLinkModel.get(); }
  106. //! Create the model for multi channel linking
  107. void createMultiChannelLinkModel();
  108. /*
  109. to be called by slots
  110. */
  111. //! Take a specified port from the first Lv2Proc and link or unlink it
  112. //! from the associated port of every other Lv2Proc
  113. //! @param port number conforming to Lv2Proc::m_modelVector
  114. //! @param state True iff it should be linked
  115. void linkPort(int port, bool state);
  116. //! Callback for the global linking LED
  117. void updateLinkStatesFromGlobal();
  118. //! Derived classes must return the group with index @p idx,
  119. //! or nullptr if @p is out of range
  120. virtual LinkedModelGroup* getGroup(std::size_t idx) = 0;
  121. private:
  122. //! Model for the "global" linking
  123. //! Only allocated if #processors > 1
  124. std::unique_ptr<class BoolModel> m_multiChannelLinkModel;
  125. //! Force updateLinkStatesFromGlobal() to not unlink any ports
  126. //! Implementation detail, see linkPort() implementation
  127. bool m_noLink = false;
  128. };
  129. #endif // LINKEDMODELGROUPS_H