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.

238 lines
8.6KB

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