OpenShot Audio Library | OpenShotAudio  0.3.3
juce_ValueWithDefault.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
40 {
41 public:
42  //==============================================================================
44  ValueWithDefault() = default;
45 
47  ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um)
48  : targetTree (tree),
49  targetProperty (propertyID),
50  undoManager (um),
51  defaultValue()
52  {
53  }
54 
56  ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um,
57  const var& defaultToUse)
58  : targetTree (tree),
59  targetProperty (propertyID),
60  undoManager (um),
61  defaultValue (defaultToUse)
62  {
63  }
64 
71  ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um,
72  const var& defaultToUse, StringRef arrayDelimiter)
73  : targetTree (tree),
74  targetProperty (propertyID),
75  undoManager (um),
76  defaultValue (defaultToUse),
77  delimiter (arrayDelimiter)
78  {
79  }
80 
83  : targetTree (other.targetTree),
84  targetProperty (other.targetProperty),
85  undoManager (other.undoManager),
86  defaultValue (other.defaultValue),
87  delimiter (other.delimiter)
88  {
89  }
90 
91  //==============================================================================
95  var get() const noexcept
96  {
97  if (isUsingDefault())
98  return defaultValue;
99 
100  if (delimiter.isNotEmpty())
101  return delimitedStringToVarArray (targetTree[targetProperty].toString());
102 
103  return targetTree[targetProperty];
104  }
105 
107  Value getPropertyAsValue() { return targetTree.getPropertyAsValue (targetProperty, undoManager); }
108 
110  var getDefault() const { return defaultValue; }
111 
113  void setDefault (const var& newDefault)
114  {
115  if (defaultValue != newDefault)
116  {
117  defaultValue = newDefault;
118 
119  if (onDefaultChange != nullptr)
120  onDefaultChange();
121  }
122  }
123 
125  bool isUsingDefault() const
126  {
127  return ! targetTree.hasProperty (targetProperty);
128  }
129 
131  void resetToDefault() noexcept
132  {
133  targetTree.removeProperty (targetProperty, nullptr);
134  }
135 
137  std::function<void()> onDefaultChange;
138 
139  //==============================================================================
141  ValueWithDefault& operator= (const var& newValue)
142  {
143  setValue (newValue, undoManager);
144  return *this;
145  }
146 
148  void setValue (const var& newValue, UndoManager* undoManagerToUse)
149  {
150  if (auto* array = newValue.getArray())
151  targetTree.setProperty (targetProperty, varArrayToDelimitedString (*array), undoManagerToUse);
152  else
153  targetTree.setProperty (targetProperty, newValue, undoManagerToUse);
154  }
155 
156  //==============================================================================
158  void referTo (ValueTree& tree, const Identifier& property, UndoManager* um)
159  {
160  referToWithDefault (tree, property, um, var(), {});
161  }
162 
166  void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const var& defaultVal)
167  {
168  referToWithDefault (tree, property, um, defaultVal, {});
169  }
170 
171  void referTo (ValueTree& tree, const Identifier& property, UndoManager* um,
172  const var& defaultVal, StringRef arrayDelimiter)
173  {
174  referToWithDefault (tree, property, um, defaultVal, arrayDelimiter);
175  }
176 
177  //==============================================================================
179  ValueTree& getValueTree() noexcept { return targetTree; }
180 
182  Identifier& getPropertyID() noexcept { return targetProperty; }
183 
185  UndoManager* getUndoManager() noexcept { return undoManager; }
186 
187  //==============================================================================
189  {
190  referToWithDefault (other.targetTree, other.targetProperty, other.undoManager,
191  other.defaultValue, other.delimiter);
192 
193  return *this;
194  }
195 
196 private:
197  //==============================================================================
198  ValueTree targetTree;
199  Identifier targetProperty;
200  UndoManager* undoManager = nullptr;
201  var defaultValue;
202 
203  String delimiter;
204 
205  //==============================================================================
206  void referToWithDefault (const ValueTree& v, const Identifier& i, UndoManager* um,
207  const var& defaultVal, StringRef del)
208  {
209  targetTree = v;
210  targetProperty = i;
211  undoManager = um;
212  defaultValue = defaultVal;
213  delimiter = del;
214  }
215 
216  //==============================================================================
217  String varArrayToDelimitedString (const Array<var>& input) const noexcept
218  {
219  // if you are trying to control a var that is an array then you need to
220  // set a delimiter string that will be used when writing to XML!
221  jassert (delimiter.isNotEmpty());
222 
223  StringArray elements;
224 
225  for (auto& v : input)
226  elements.add (v.toString());
227 
228  return elements.joinIntoString (delimiter);
229  }
230 
231  Array<var> delimitedStringToVarArray (StringRef input) const noexcept
232  {
233  Array<var> arr;
234 
235  for (auto t : StringArray::fromTokens (input, delimiter, {}))
236  arr.add (t);
237 
238  return arr;
239  }
240 
241  //==============================================================================
242  JUCE_DECLARE_WEAK_REFERENCEABLE (ValueWithDefault)
243 };
244 
245 } // namespace juce
static StringArray fromTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
bool isNotEmpty() const noexcept
Definition: juce_String.h:302
Value getPropertyAsValue(const Identifier &name, UndoManager *undoManager, bool shouldUpdateSynchronously=false)
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
void removeProperty(const Identifier &name, UndoManager *undoManager)
bool hasProperty(const Identifier &name) const noexcept
var get() const noexcept
ValueWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um, const var &defaultToUse)
Identifier & getPropertyID() noexcept
ValueWithDefault(const ValueWithDefault &other)
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um)
void setValue(const var &newValue, UndoManager *undoManagerToUse)
void setDefault(const var &newDefault)
std::function< void()> onDefaultChange
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um, const var &defaultVal)
ValueWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um, const var &defaultToUse, StringRef arrayDelimiter)
ValueWithDefault & operator=(const var &newValue)
UndoManager * getUndoManager() noexcept
ValueWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um)
ValueTree & getValueTree() noexcept
Array< var > * getArray() const noexcept