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.

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