The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

294 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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());
  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), 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, property, um, {}, {});
  138. }
  139. /** Makes the ValueTreePropertyWithDefault refer to the specified property inside
  140. the given ValueTree.
  141. The default value will be defaultVal.
  142. */
  143. void referTo (ValueTree tree,
  144. const Identifier& property,
  145. UndoManager* um,
  146. var defaultVal)
  147. {
  148. referToWithDefault (tree, property, um, Value (defaultVal), {});
  149. }
  150. /** Makes the ValueTreePropertyWithDefault refer to the specified property inside
  151. the given ValueTree.
  152. The default value will be defaultVal.
  153. */
  154. void referTo (ValueTree tree,
  155. const Identifier& property,
  156. UndoManager* um,
  157. var defaultVal,
  158. StringRef arrayDelimiter)
  159. {
  160. referToWithDefault (tree, property, um, Value (defaultVal), arrayDelimiter);
  161. }
  162. //==============================================================================
  163. /** Returns a reference to the ValueTree containing the referenced property. */
  164. ValueTree& getValueTree() noexcept { return targetTree; }
  165. /** Returns the property ID of the referenced property. */
  166. Identifier& getPropertyID() noexcept { return targetProperty; }
  167. /** Returns the UndoManager that is being used. */
  168. UndoManager* getUndoManager() noexcept { return undoManager; }
  169. //==============================================================================
  170. ValueTreePropertyWithDefault& operator= (const ValueTreePropertyWithDefault& other)
  171. {
  172. referToWithDefault (other.targetTree,
  173. other.targetProperty,
  174. other.undoManager,
  175. other.defaultValue,
  176. other.delimiter);
  177. return *this;
  178. }
  179. private:
  180. //==============================================================================
  181. ValueTree targetTree;
  182. Identifier targetProperty;
  183. UndoManager* undoManager = nullptr;
  184. Value defaultValue;
  185. String delimiter;
  186. //==============================================================================
  187. void valueChanged (Value&) override
  188. {
  189. if (onDefaultChange != nullptr)
  190. onDefaultChange();
  191. }
  192. //==============================================================================
  193. void referToWithDefault (ValueTree v,
  194. const Identifier& i,
  195. UndoManager* um,
  196. const Value& defaultVal,
  197. StringRef del)
  198. {
  199. targetTree = v;
  200. targetProperty = i;
  201. undoManager = um;
  202. defaultValue.referTo (defaultVal);
  203. delimiter = del;
  204. defaultValue.addListener (this);
  205. }
  206. //==============================================================================
  207. String varArrayToDelimitedString (const Array<var>& input) const noexcept
  208. {
  209. // if you are trying to control a var that is an array then you need to
  210. // set a delimiter string that will be used when writing to XML!
  211. jassert (delimiter.isNotEmpty());
  212. StringArray elements;
  213. for (auto& v : input)
  214. elements.add (v.toString());
  215. return elements.joinIntoString (delimiter);
  216. }
  217. Array<var> delimitedStringToVarArray (StringRef input) const noexcept
  218. {
  219. Array<var> arr;
  220. for (auto t : StringArray::fromTokens (input, delimiter, {}))
  221. arr.add (t);
  222. return arr;
  223. }
  224. };
  225. //==============================================================================
  226. #ifndef DOXYGEN
  227. using ValueWithDefault [[deprecated ("This class has been renamed to better describe what is does. "
  228. "This declaration is here for backwards compatibility and new "
  229. "code should use the new class name.")]]
  230. = ValueTreePropertyWithDefault;
  231. #endif
  232. } // namespace juce