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.

246 lines
8.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. This class acts as a wrapper around a property inside a ValueTree.
  24. If the property inside the ValueTree is missing or empty the ValueWithDefault will automatically
  25. return a default value, which can be specified when initialising the ValueWithDefault.
  26. @tags{DataStructures}
  27. */
  28. class ValueWithDefault
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an unitialised ValueWithDefault. Initialise it using one of the referTo() methods. */
  33. ValueWithDefault() {}
  34. /** Creates an ValueWithDefault object. The default value will be an empty var. */
  35. ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um)
  36. : targetTree (tree),
  37. targetProperty (propertyID),
  38. undoManager (um),
  39. defaultValue()
  40. {
  41. }
  42. /** Creates an ValueWithDefault object. The default value will be defaultToUse. */
  43. ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um,
  44. const var& defaultToUse)
  45. : targetTree (tree),
  46. targetProperty (propertyID),
  47. undoManager (um),
  48. defaultValue (defaultToUse)
  49. {
  50. }
  51. /** Creates an ValueWithDefault object. The default value will be defaultToUse.
  52. Use this constructor if the underlying var object being controlled is an array and
  53. it will handle the conversion to/from a delimited String that can be written to
  54. XML format.
  55. */
  56. ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um,
  57. const var& defaultToUse, StringRef arrayDelimiter)
  58. : targetTree (tree),
  59. targetProperty (propertyID),
  60. undoManager (um),
  61. defaultValue (defaultToUse),
  62. delimiter (arrayDelimiter)
  63. {
  64. }
  65. /** Creates a ValueWithDefault object from another ValueWithDefault object. */
  66. ValueWithDefault (const ValueWithDefault& other)
  67. : targetTree (other.targetTree),
  68. targetProperty (other.targetProperty),
  69. undoManager (other.undoManager),
  70. defaultValue (other.defaultValue),
  71. delimiter (other.delimiter)
  72. {
  73. }
  74. //==============================================================================
  75. /** Returns the current value of the property. If the property does not exist or is empty,
  76. returns the default value.
  77. */
  78. var get() const noexcept
  79. {
  80. if (isUsingDefault())
  81. return defaultValue;
  82. if (delimiter.isNotEmpty())
  83. return delimitedStringToVarArray (targetTree[targetProperty].toString());
  84. return targetTree[targetProperty];
  85. }
  86. /** Returns the current property as a Value object. */
  87. Value getPropertyAsValue() { return targetTree.getPropertyAsValue (targetProperty, undoManager); }
  88. /** Returns the current default value. */
  89. var getDefault() const { return defaultValue; }
  90. /** Sets the default value to a new var. */
  91. void setDefault (const var& newDefault)
  92. {
  93. if (defaultValue != newDefault)
  94. {
  95. defaultValue = newDefault;
  96. if (onDefaultChange != nullptr)
  97. onDefaultChange();
  98. }
  99. }
  100. /** Returns true if the property does not exist or is empty. */
  101. bool isUsingDefault() const
  102. {
  103. return ! targetTree.hasProperty (targetProperty) || targetTree.getProperty (targetProperty) == var();
  104. }
  105. /** Removes the property from the referenced ValueTree. */
  106. void resetToDefault() noexcept
  107. {
  108. targetTree.removeProperty (targetProperty, nullptr);
  109. }
  110. /** You can assign a lambda to this callback object to have it called when the default value is changed. */
  111. std::function<void()> onDefaultChange;
  112. //==============================================================================
  113. /** Sets the property and returns the new ValueWithDefault. This will modify the property in the referenced ValueTree. */
  114. ValueWithDefault& operator= (const var& newValue)
  115. {
  116. setValue (newValue, undoManager);
  117. return *this;
  118. }
  119. /** Sets the property. This will actually modify the property in the referenced ValueTree. */
  120. void setValue (const var& newValue, UndoManager* undoManagerToUse)
  121. {
  122. if (auto* array = newValue.getArray())
  123. targetTree.setProperty (targetProperty, varArrayToDelimitedString (*array), undoManagerToUse);
  124. else
  125. targetTree.setProperty (targetProperty, newValue, undoManagerToUse);
  126. }
  127. //==============================================================================
  128. /** Makes the ValueWithDefault refer to the specified property inside the given ValueTree. */
  129. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um)
  130. {
  131. referToWithDefault (tree, property, um, var(), {});
  132. }
  133. /** Makes the ValueWithDefault refer to the specified property inside the given ValueTree,
  134. and specifies a default value to use.
  135. */
  136. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const var& defaultVal)
  137. {
  138. referToWithDefault (tree, property, um, defaultVal, {});
  139. }
  140. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um,
  141. const var& defaultVal, StringRef arrayDelimiter)
  142. {
  143. referToWithDefault (tree, property, um, defaultVal, arrayDelimiter);
  144. }
  145. //==============================================================================
  146. /** Returns a reference to the ValueTree containing the referenced property. */
  147. ValueTree& getValueTree() noexcept { return targetTree; }
  148. /** Returns the property ID of the referenced property. */
  149. Identifier& getPropertyID() noexcept { return targetProperty; }
  150. /** Returns the UndoManager that is being used. */
  151. UndoManager* getUndoManager() noexcept { return undoManager; }
  152. //==============================================================================
  153. ValueWithDefault& operator= (const ValueWithDefault& other)
  154. {
  155. referToWithDefault (other.targetTree, other.targetProperty, other.undoManager,
  156. other.defaultValue, other.delimiter);
  157. return *this;
  158. }
  159. private:
  160. //==============================================================================
  161. ValueTree targetTree;
  162. Identifier targetProperty;
  163. UndoManager* undoManager = nullptr;
  164. var defaultValue;
  165. String delimiter;
  166. //==============================================================================
  167. void referToWithDefault (const ValueTree& v, const Identifier& i, UndoManager* um,
  168. const var& defaultVal, StringRef del)
  169. {
  170. targetTree = v;
  171. targetProperty = i;
  172. undoManager = um;
  173. defaultValue = defaultVal;
  174. delimiter = del;
  175. }
  176. //==============================================================================
  177. String varArrayToDelimitedString (const Array<var>& input) const noexcept
  178. {
  179. // if you are trying to control a var that is an array then you need to
  180. // set a delimiter string that will be used when writing to XML!
  181. jassert (delimiter.isNotEmpty());
  182. StringArray elements;
  183. for (auto& v : input)
  184. elements.add (v.toString());
  185. return elements.joinIntoString (delimiter);
  186. }
  187. Array<var> delimitedStringToVarArray (StringRef input) const noexcept
  188. {
  189. Array<var> arr;
  190. for (auto t : StringArray::fromTokens (input, delimiter, {}))
  191. arr.add (t);
  192. return arr;
  193. }
  194. //==============================================================================
  195. JUCE_DECLARE_WEAK_REFERENCEABLE (ValueWithDefault)
  196. };
  197. } // namespace juce