Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_ValueTreePropertyWithDefault.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. This class acts as a wrapper around a property inside a ValueTree.
  23. If the property inside the ValueTree is missing it will return a default value,
  24. which can be specified in the constructor or by calling setDefault().
  25. @tags{DataStructures}
  26. */
  27. class JUCE_API ValueTreePropertyWithDefault : private Value::Listener
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an uninitialised ValueTreePropertyWithDefault object.
  32. Initialise it using one of the referTo() methods.
  33. */
  34. ValueTreePropertyWithDefault() = default;
  35. /** Creates a ValueTreePropertyWithDefault object for the specified property.
  36. The default value will be an empty var.
  37. */
  38. ValueTreePropertyWithDefault (ValueTree& tree,
  39. const Identifier& propertyID,
  40. UndoManager* um)
  41. {
  42. referTo (tree, propertyID, um);
  43. }
  44. /** Creates an ValueTreePropertyWithDefault object for the specified property.
  45. The default value will be defaultToUse.
  46. */
  47. ValueTreePropertyWithDefault (ValueTree& tree,
  48. const Identifier& propertyID,
  49. UndoManager* um,
  50. var defaultToUse)
  51. {
  52. referTo (tree, propertyID, um, defaultToUse);
  53. }
  54. /** Creates a ValueTreePropertyWithDefault object for the specified property.
  55. The default value will be defaultToUse.
  56. Use this constructor if the underlying var object being controlled is an array and
  57. it will handle the conversion to/from a delimited String that can be written to
  58. XML format.
  59. */
  60. ValueTreePropertyWithDefault (ValueTree& tree,
  61. const Identifier& propertyID,
  62. UndoManager* um,
  63. var defaultToUse,
  64. StringRef arrayDelimiter)
  65. {
  66. referTo (tree, propertyID, um, defaultToUse, arrayDelimiter);
  67. }
  68. /** Creates a ValueTreePropertyWithDefault object from another ValueTreePropertyWithDefault object. */
  69. ValueTreePropertyWithDefault (const ValueTreePropertyWithDefault& other)
  70. {
  71. referToWithDefault (other.targetTree,
  72. other.targetProperty,
  73. other.undoManager,
  74. other.defaultValue,
  75. other.delimiter);
  76. }
  77. /** Destructor. */
  78. ~ValueTreePropertyWithDefault() override
  79. {
  80. defaultValue.removeListener (this);
  81. }
  82. //==============================================================================
  83. /** Returns the current value of the property.
  84. If the property does not exist this returns the default value.
  85. */
  86. var get() const noexcept
  87. {
  88. if (isUsingDefault())
  89. return defaultValue;
  90. if (delimiter.isNotEmpty())
  91. return delimitedStringToVarArray (targetTree[targetProperty].toString(), delimiter);
  92. return targetTree[targetProperty];
  93. }
  94. /** Returns the current property as a Value object. */
  95. Value getPropertyAsValue() { return targetTree.getPropertyAsValue (targetProperty, undoManager); }
  96. /** Returns the current default value. */
  97. var getDefault() const { return defaultValue; }
  98. /** Sets the default value to a new var. */
  99. void setDefault (const var& newDefault) { defaultValue = newDefault; }
  100. /** Returns true if the property does not exist in the referenced ValueTree. */
  101. bool isUsingDefault() const { return ! targetTree.hasProperty (targetProperty); }
  102. /** Removes the property from the referenced ValueTree. */
  103. void resetToDefault() noexcept { targetTree.removeProperty (targetProperty, nullptr); }
  104. /** You can assign a lambda to this callback and it will called when the default
  105. value is changed.
  106. @see setDefault
  107. */
  108. std::function<void()> onDefaultChange;
  109. //==============================================================================
  110. /** Sets the property and returns the new ValueTreePropertyWithDefault.
  111. This will modify the property in the referenced ValueTree.
  112. */
  113. ValueTreePropertyWithDefault& operator= (const var& newValue)
  114. {
  115. setValue (newValue, undoManager);
  116. return *this;
  117. }
  118. /** Sets the property.
  119. This will modify the property in the referenced ValueTree.
  120. */
  121. void setValue (const var& newValue, UndoManager* undoManagerToUse)
  122. {
  123. if (auto* array = newValue.getArray())
  124. targetTree.setProperty (targetProperty, varArrayToDelimitedString (*array, delimiter), undoManagerToUse);
  125. else
  126. targetTree.setProperty (targetProperty, newValue, undoManagerToUse);
  127. }
  128. //==============================================================================
  129. /** Makes the ValueTreePropertyWithDefault refer to the specified property inside
  130. the given ValueTree.
  131. The default value will be an empty var.
  132. */
  133. void referTo (ValueTree tree,
  134. const Identifier& property,
  135. UndoManager* um)
  136. {
  137. referToWithDefault (tree,
  138. property,
  139. um,
  140. Value (new SynchronousValueSource (var())),
  141. {});
  142. }
  143. /** Makes the ValueTreePropertyWithDefault refer to the specified property inside
  144. the given ValueTree.
  145. The default value will be defaultVal.
  146. */
  147. void referTo (ValueTree tree,
  148. const Identifier& property,
  149. UndoManager* um,
  150. var defaultVal)
  151. {
  152. referToWithDefault (tree,
  153. property,
  154. um,
  155. Value (new SynchronousValueSource (defaultVal)),
  156. {});
  157. }
  158. /** Makes the ValueTreePropertyWithDefault refer to the specified property inside
  159. the given ValueTree.
  160. The default value will be defaultVal.
  161. */
  162. void referTo (ValueTree tree,
  163. const Identifier& property,
  164. UndoManager* um,
  165. var defaultVal,
  166. StringRef arrayDelimiter)
  167. {
  168. referToWithDefault (tree,
  169. property,
  170. um,
  171. Value (new SynchronousValueSource (defaultVal)),
  172. arrayDelimiter);
  173. }
  174. //==============================================================================
  175. /** Returns a reference to the ValueTree containing the referenced property. */
  176. ValueTree& getValueTree() noexcept { return targetTree; }
  177. /** Returns the property ID of the referenced property. */
  178. Identifier& getPropertyID() noexcept { return targetProperty; }
  179. /** Returns the UndoManager that is being used. */
  180. UndoManager* getUndoManager() noexcept { return undoManager; }
  181. //==============================================================================
  182. ValueTreePropertyWithDefault& operator= (const ValueTreePropertyWithDefault& other)
  183. {
  184. referToWithDefault (other.targetTree,
  185. other.targetProperty,
  186. other.undoManager,
  187. other.defaultValue,
  188. other.delimiter);
  189. return *this;
  190. }
  191. private:
  192. //==============================================================================
  193. class SynchronousValueSource : public Value::ValueSource
  194. {
  195. public:
  196. explicit SynchronousValueSource (const var& initialValue)
  197. : value (initialValue)
  198. {
  199. }
  200. var getValue() const override
  201. {
  202. return value;
  203. }
  204. void setValue (const var& newValue) override
  205. {
  206. if (! newValue.equalsWithSameType (value))
  207. {
  208. value = newValue;
  209. sendChangeMessage (true);
  210. }
  211. }
  212. private:
  213. var value;
  214. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SynchronousValueSource)
  215. };
  216. //==============================================================================
  217. static String varArrayToDelimitedString (const Array<var>& input, StringRef delim)
  218. {
  219. // if you are trying to control a var that is an array then you need to
  220. // set a delimiter string that will be used when writing to XML!
  221. jassert (delim.isNotEmpty());
  222. StringArray elements;
  223. for (auto& v : input)
  224. elements.add (v.toString());
  225. return elements.joinIntoString (delim);
  226. }
  227. static Array<var> delimitedStringToVarArray (StringRef input, StringRef delim)
  228. {
  229. Array<var> arr;
  230. for (auto t : StringArray::fromTokens (input, delim, {}))
  231. arr.add (t);
  232. return arr;
  233. }
  234. void valueChanged (Value&) override
  235. {
  236. if (onDefaultChange != nullptr)
  237. onDefaultChange();
  238. }
  239. void referToWithDefault (ValueTree v,
  240. const Identifier& i,
  241. UndoManager* um,
  242. const Value& defaultVal,
  243. StringRef del)
  244. {
  245. targetTree = v;
  246. targetProperty = i;
  247. undoManager = um;
  248. defaultValue.referTo (defaultVal);
  249. delimiter = del;
  250. defaultValue.addListener (this);
  251. }
  252. //==============================================================================
  253. ValueTree targetTree;
  254. Identifier targetProperty;
  255. UndoManager* undoManager = nullptr;
  256. Value defaultValue;
  257. String delimiter;
  258. //==============================================================================
  259. JUCE_LEAK_DETECTOR (ValueTreePropertyWithDefault)
  260. };
  261. //==============================================================================
  262. #ifndef DOXYGEN
  263. using ValueWithDefault [[deprecated ("This class has been renamed to better describe what is does. "
  264. "This declaration is here for backwards compatibility and new "
  265. "code should use the new class name.")]]
  266. = ValueTreePropertyWithDefault;
  267. #endif
  268. } // namespace juce