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.

174 lines
6.2KB

  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() : undoManager (nullptr) {}
  34. /** Creates an ValueWithDefault object. The default value will be an empty var. */
  35. ValueWithDefault (ValueTree& tree, const Identifier& propertyID,
  36. UndoManager* um)
  37. : targetTree (tree),
  38. targetProperty (propertyID),
  39. undoManager (um),
  40. defaultValue()
  41. {
  42. }
  43. /** Creates an ValueWithDefault object. The default value will be defaultToUse. */
  44. ValueWithDefault (ValueTree& tree, const Identifier& propertyID,
  45. UndoManager* um, const var& defaultToUse)
  46. : targetTree (tree),
  47. targetProperty (propertyID),
  48. undoManager (um),
  49. defaultValue (defaultToUse)
  50. {
  51. }
  52. /** Creates a ValueWithDefault object from another ValueWithDefault object. */
  53. ValueWithDefault (const ValueWithDefault& other)
  54. : targetTree (other.targetTree),
  55. targetProperty (other.targetProperty),
  56. undoManager (other.undoManager),
  57. defaultValue (other.defaultValue)
  58. {
  59. }
  60. //==============================================================================
  61. /** Returns the current value of the property. If the property does not exist or is empty,
  62. returns the default value.
  63. */
  64. var get() const noexcept
  65. {
  66. if (isUsingDefault())
  67. return defaultValue;
  68. return targetTree[targetProperty];
  69. }
  70. /** Returns the current property as a Value object. */
  71. Value getPropertyAsValue() { return targetTree.getPropertyAsValue (targetProperty, undoManager); }
  72. /** Returns the current default value. */
  73. var getDefault() const { return defaultValue; }
  74. /** Sets the default value to a new var. */
  75. void setDefault (const var& newDefault)
  76. {
  77. if (defaultValue != newDefault)
  78. {
  79. defaultValue = newDefault;
  80. if (onDefaultChange != nullptr)
  81. onDefaultChange();
  82. }
  83. }
  84. /** Returns true if the property does not exist or is empty. */
  85. bool isUsingDefault() const
  86. {
  87. return ! targetTree.hasProperty (targetProperty);
  88. }
  89. /** Resets the property to an empty var. */
  90. void resetToDefault() noexcept
  91. {
  92. targetTree.removeProperty (targetProperty, nullptr);
  93. }
  94. /** You can assign a lambda to this callback object to have it called when the default value is changed. */
  95. std::function<void()> onDefaultChange;
  96. //==============================================================================
  97. /** Sets the property and returns the new ValueWithDefault. This will modify the property in the referenced ValueTree. */
  98. ValueWithDefault& operator= (const var& newValue)
  99. {
  100. setValue (newValue, undoManager);
  101. return *this;
  102. }
  103. /** Sets the property. This will actually modify the property in the referenced ValueTree. */
  104. void setValue (const var& newValue, UndoManager* undoManagerToUse)
  105. {
  106. targetTree.setProperty (targetProperty, newValue, undoManagerToUse);
  107. }
  108. //==============================================================================
  109. /** Makes the ValueWithDefault refer to the specified property inside the given ValueTree. */
  110. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um)
  111. {
  112. referToWithDefault (tree, property, um, var());
  113. }
  114. /** Makes the ValueWithDefault refer to the specified property inside the given ValueTree,
  115. and specifies a default value to use.
  116. */
  117. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const var& defaultVal)
  118. {
  119. referToWithDefault (tree, property, um, defaultVal);
  120. }
  121. //==============================================================================
  122. /** Returns a reference to the ValueTree containing the referenced property. */
  123. ValueTree& getValueTree() noexcept { return targetTree; }
  124. /** Returns the property ID of the referenced property. */
  125. Identifier& getPropertyID() noexcept { return targetProperty; }
  126. private:
  127. //==============================================================================
  128. ValueTree targetTree;
  129. Identifier targetProperty;
  130. UndoManager* undoManager;
  131. var defaultValue;
  132. //==============================================================================
  133. void referToWithDefault (ValueTree& v, const Identifier& i, UndoManager* um, const var& defaultVal)
  134. {
  135. targetTree = v;
  136. targetProperty = i;
  137. undoManager = um;
  138. defaultValue = defaultVal;
  139. }
  140. };
  141. } // namespace juce