LinkedModelGroups.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * LinkedModelGroups.h - base classes for groups of linked 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 <cstddef>
  27. #include <memory>
  28. #include <vector>
  29. #include "Model.h"
  30. /**
  31. @file LinkedModelGroups.h
  32. See Lv2ControlBase.h and Lv2Proc.h for example usage
  33. */
  34. /**
  35. Base class for a group of linked models
  36. See the LinkedModelGroup class for explanations
  37. Features:
  38. * Models are stored by their QObject::objectName
  39. * Models are linked automatically
  40. */
  41. class LinkedModelGroup : public Model
  42. {
  43. Q_OBJECT
  44. public:
  45. /*
  46. Initialization
  47. */
  48. //! @param parent model of the LinkedModelGroups class
  49. LinkedModelGroup(Model* parent) : Model(parent) {}
  50. /*
  51. Linking (initially only)
  52. */
  53. void linkControls(LinkedModelGroup *other);
  54. /*
  55. Models
  56. */
  57. struct ModelInfo
  58. {
  59. QString m_name;
  60. class AutomatableModel* m_model;
  61. ModelInfo() { /* hopefully no one will use this */ } // TODO: remove?
  62. ModelInfo(const QString& name, AutomatableModel* model)
  63. : m_name(name), m_model(model) {}
  64. };
  65. // TODO: refactor those 2
  66. template<class Functor>
  67. void foreach_model(const Functor& ftor)
  68. {
  69. for (auto itr = m_models.begin(); itr != m_models.end(); ++itr)
  70. {
  71. ftor(itr->first, itr->second);
  72. }
  73. }
  74. template<class Functor>
  75. void foreach_model(const Functor& ftor) const
  76. {
  77. for (auto itr = m_models.cbegin(); itr != m_models.cend(); ++itr)
  78. {
  79. ftor(itr->first, itr->second);
  80. }
  81. }
  82. std::size_t modelNum() const { return m_models.size(); }
  83. bool containsModel(const QString& name) const;
  84. void removeControl(AutomatableModel *);
  85. /*
  86. Load/Save
  87. */
  88. void saveValues(class QDomDocument& doc, class QDomElement& that);
  89. void loadValues(const class QDomElement& that);
  90. signals:
  91. // NOTE: when separating core from UI, this will need to be removed
  92. // (who would kno if the client is Qt, i.e. it may not have slots at all)
  93. // In this case you'd e.g. send the UI something like
  94. // "/added <model meta info>"
  95. void modelAdded(AutomatableModel* added);
  96. void modelRemoved(AutomatableModel* removed);
  97. public:
  98. AutomatableModel* getModel(const std::string& s)
  99. {
  100. auto itr = m_models.find(s);
  101. return (itr == m_models.end()) ? nullptr : itr->second.m_model;
  102. }
  103. //! Register a further model
  104. void addModel(class AutomatableModel* model, const QString& name);
  105. //! Unregister a model, return true if a model was erased
  106. bool eraseModel(const QString& name);
  107. //! Remove all models
  108. void clearModels();
  109. private:
  110. //! models for the controls
  111. std::map<std::string, ModelInfo> m_models;
  112. };
  113. /**
  114. Container for a group of linked models
  115. Each group contains the same models and model types. The models are
  116. numbered, and equal numbered models are associated and always linked.
  117. A typical application are two mono plugins making a stereo plugin.
  118. @note Though this class can contain multiple model groups, a corresponding
  119. view ("LinkedModelGroupViews") will only display one group, as they all have
  120. the same values
  121. @note Though called "container", this class does not contain, but only
  122. know the single groups. The inheriting classes are responsible for storage.
  123. */
  124. class LinkedModelGroups
  125. {
  126. public:
  127. virtual ~LinkedModelGroups();
  128. void linkAllModels();
  129. /*
  130. Load/Save
  131. */
  132. void saveSettings(class QDomDocument& doc, class QDomElement& that);
  133. void loadSettings(const class QDomElement& that);
  134. /*
  135. General
  136. */
  137. //! Derived classes must return the group with index @p idx,
  138. //! or nullptr if @p is out of range
  139. virtual LinkedModelGroup* getGroup(std::size_t idx) = 0;
  140. //! @see getGroup
  141. virtual const LinkedModelGroup* getGroup(std::size_t idx) const = 0;
  142. };
  143. #endif // LINKEDMODELGROUPS_H