LMMS
Loading...
Searching...
No Matches
juce_ValueTreePropertyWithDefault.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29//==============================================================================
39{
40public:
41 //==============================================================================
47
53 const Identifier& propertyID,
54 UndoManager* um)
55 {
56 referTo (tree, propertyID, um);
57 }
58
64 const Identifier& propertyID,
65 UndoManager* um,
66 var defaultToUse)
67 {
68 referTo (tree, propertyID, um, defaultToUse);
69 }
70
80 const Identifier& propertyID,
81 UndoManager* um,
82 var defaultToUse,
83 StringRef arrayDelimiter)
84 {
85 referTo (tree, propertyID, um, defaultToUse, arrayDelimiter);
86 }
87
97
100 {
101 defaultValue.removeListener (this);
102 }
103
104 //==============================================================================
110 {
111 if (isUsingDefault())
112 return defaultValue;
113
114 if (delimiter.isNotEmpty())
116
118 }
119
122
124 var getDefault() const { return defaultValue; }
125
127 void setDefault (const var& newDefault) { defaultValue = newDefault; }
128
130 bool isUsingDefault() const { return ! targetTree.hasProperty (targetProperty); }
131
133 void resetToDefault() noexcept { targetTree.removeProperty (targetProperty, nullptr); }
134
140 std::function<void()> onDefaultChange;
141
142 //==============================================================================
147 ValueTreePropertyWithDefault& operator= (const var& newValue)
148 {
149 setValue (newValue, undoManager);
150 return *this;
151 }
152
157 void setValue (const var& newValue, UndoManager* undoManagerToUse)
158 {
159 if (auto* array = newValue.getArray())
160 targetTree.setProperty (targetProperty, varArrayToDelimitedString (*array, delimiter), undoManagerToUse);
161 else
162 targetTree.setProperty (targetProperty, newValue, undoManagerToUse);
163 }
164
165 //==============================================================================
171 void referTo (ValueTree tree,
172 const Identifier& property,
173 UndoManager* um)
174 {
176 property,
177 um,
179 {});
180 }
181
187 void referTo (ValueTree tree,
188 const Identifier& property,
189 UndoManager* um,
190 var defaultVal)
191 {
193 property,
194 um,
195 Value (new SynchronousValueSource (defaultVal)),
196 {});
197 }
198
204 void referTo (ValueTree tree,
205 const Identifier& property,
206 UndoManager* um,
207 var defaultVal,
208 StringRef arrayDelimiter)
209 {
211 property,
212 um,
213 Value (new SynchronousValueSource (defaultVal)),
214 arrayDelimiter);
215 }
216
217 //==============================================================================
220
223
226
227 //==============================================================================
229 {
231 other.targetProperty,
232 other.undoManager,
233 other.defaultValue,
234 other.delimiter);
235
236 return *this;
237 }
238
239private:
240 //==============================================================================
242 {
243 public:
244 explicit SynchronousValueSource (const var& initialValue)
245 : value (initialValue)
246 {
247 }
248
249 var getValue() const override
250 {
251 return value;
252 }
253
254 void setValue (const var& newValue) override
255 {
256 if (! newValue.equalsWithSameType (value))
257 {
258 value = newValue;
259 sendChangeMessage (true);
260 }
261 }
262
263 private:
265
267 };
268
269 //==============================================================================
271 {
272 // if you are trying to control a var that is an array then you need to
273 // set a delimiter string that will be used when writing to XML!
274 jassert (delim.isNotEmpty());
275
276 StringArray elements;
277
278 for (auto& v : input)
279 elements.add (v.toString());
280
281 return elements.joinIntoString (delim);
282 }
283
285 {
286 Array<var> arr;
287
288 for (auto t : StringArray::fromTokens (input, delim, {}))
289 arr.add (t);
290
291 return arr;
292 }
293
294 void valueChanged (Value&) override
295 {
296 if (onDefaultChange != nullptr)
298 }
299
301 const Identifier& i,
302 UndoManager* um,
303 const Value& defaultVal,
304 StringRef del)
305 {
306 targetTree = v;
308 undoManager = um;
309 defaultValue.referTo (defaultVal);
310 delimiter = del;
311
312 defaultValue.addListener (this);
313 }
314
315 //==============================================================================
321
322 //==============================================================================
324};
325
326//==============================================================================
327#ifndef DOXYGEN
328using ValueWithDefault [[deprecated ("This class has been renamed to better describe what is does. "
329 "This declaration is here for backwards compatibility and new "
330 "code should use the new class name.")]]
332#endif
333
334} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_Array.h:56
void add(const ElementType &newElement)
Definition juce_Array.h:418
Definition juce_Identifier.h:39
Definition juce_StringArray.h:35
String joinIntoString(StringRef separatorString, int startIndex=0, int numberOfElements=-1) const
Definition juce_StringArray.cpp:289
static StringArray fromTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Definition juce_StringArray.cpp:387
void add(String stringToAdd)
Definition juce_StringArray.cpp:136
Definition juce_String.h:53
Definition juce_StringRef.h:62
bool isNotEmpty() const noexcept
Definition juce_StringRef.h:103
Definition juce_UndoManager.h:52
Definition juce_Value.h:139
Definition juce_Value.h:180
void sendChangeMessage(bool dispatchSynchronously)
Definition juce_Value.cpp:43
Definition juce_Value.h:51
Definition juce_ValueTree.h:72
Definition juce_ValueTreePropertyWithDefault.h:242
void setValue(const var &newValue) override
Definition juce_ValueTreePropertyWithDefault.h:254
SynchronousValueSource(const var &initialValue)
Definition juce_ValueTreePropertyWithDefault.h:244
var getValue() const override
Definition juce_ValueTreePropertyWithDefault.h:249
var value
Definition juce_ValueTreePropertyWithDefault.h:264
Definition juce_ValueTreePropertyWithDefault.h:39
ValueTree & getValueTree() noexcept
Definition juce_ValueTreePropertyWithDefault.h:219
static Array< var > delimitedStringToVarArray(StringRef input, StringRef delim)
Definition juce_ValueTreePropertyWithDefault.h:284
ValueTreePropertyWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um, var defaultToUse, StringRef arrayDelimiter)
Definition juce_ValueTreePropertyWithDefault.h:79
Value getPropertyAsValue()
Definition juce_ValueTreePropertyWithDefault.h:121
void valueChanged(Value &) override
Definition juce_ValueTreePropertyWithDefault.h:294
~ValueTreePropertyWithDefault() override
Definition juce_ValueTreePropertyWithDefault.h:99
ValueTree targetTree
Definition juce_ValueTreePropertyWithDefault.h:316
static String varArrayToDelimitedString(const Array< var > &input, StringRef delim)
Definition juce_ValueTreePropertyWithDefault.h:270
bool isUsingDefault() const
Definition juce_ValueTreePropertyWithDefault.h:130
void referTo(ValueTree tree, const Identifier &property, UndoManager *um, var defaultVal, StringRef arrayDelimiter)
Definition juce_ValueTreePropertyWithDefault.h:204
var getDefault() const
Definition juce_ValueTreePropertyWithDefault.h:124
Identifier targetProperty
Definition juce_ValueTreePropertyWithDefault.h:317
UndoManager * undoManager
Definition juce_ValueTreePropertyWithDefault.h:318
void referToWithDefault(ValueTree v, const Identifier &i, UndoManager *um, const Value &defaultVal, StringRef del)
Definition juce_ValueTreePropertyWithDefault.h:300
String delimiter
Definition juce_ValueTreePropertyWithDefault.h:320
void setValue(const var &newValue, UndoManager *undoManagerToUse)
Definition juce_ValueTreePropertyWithDefault.h:157
Value defaultValue
Definition juce_ValueTreePropertyWithDefault.h:319
std::function< void()> onDefaultChange
Definition juce_ValueTreePropertyWithDefault.h:140
void referTo(ValueTree tree, const Identifier &property, UndoManager *um, var defaultVal)
Definition juce_ValueTreePropertyWithDefault.h:187
UndoManager * getUndoManager() noexcept
Definition juce_ValueTreePropertyWithDefault.h:225
ValueTreePropertyWithDefault(const ValueTreePropertyWithDefault &other)
Definition juce_ValueTreePropertyWithDefault.h:89
ValueTreePropertyWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um, var defaultToUse)
Definition juce_ValueTreePropertyWithDefault.h:63
void setDefault(const var &newDefault)
Definition juce_ValueTreePropertyWithDefault.h:127
Identifier & getPropertyID() noexcept
Definition juce_ValueTreePropertyWithDefault.h:222
ValueTreePropertyWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um)
Definition juce_ValueTreePropertyWithDefault.h:52
void resetToDefault() noexcept
Definition juce_ValueTreePropertyWithDefault.h:133
void referTo(ValueTree tree, const Identifier &property, UndoManager *um)
Definition juce_ValueTreePropertyWithDefault.h:171
var get() const noexcept
Definition juce_ValueTreePropertyWithDefault.h:109
Definition juce_Variant.h:42
Array< var > * getArray() const noexcept
Definition juce_Variant.cpp:573
bool equalsWithSameType(const var &other) const noexcept
Definition juce_Variant.cpp:639
struct huft * t
Definition inflate.c:943
unsigned v[N_MAX]
Definition inflate.c:1584
register unsigned i
Definition inflate.c:1575
#define JUCE_LEAK_DETECTOR(OwnerClass)
Definition juce_LeakedObjectDetector.h:138
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
#define JUCE_API
Definition juce_StandardHeader.h:152
Definition carla_juce.cpp:31
juce::String toString(const Steinberg::char8 *string) noexcept
Definition juce_VST3Common.h:159
@ tree
Definition juce_AccessibilityRole.h:58
#define void
Definition unzip.h:396
#define const
Definition zconf.h:137