Loading...
Searching...
No Matches
juce_ValueTreePropertyWithDefault.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class JUCE_API ValueTreePropertyWithDefault : private Value::Listener
{
public:
//==============================================================================
ValueTreePropertyWithDefault() = default;
ValueTreePropertyWithDefault (ValueTree& tree,
const Identifier& propertyID,
UndoManager* um)
{
referTo (tree, propertyID, um);
}
ValueTreePropertyWithDefault (ValueTree& tree,
const Identifier& propertyID,
UndoManager* um,
var defaultToUse)
{
referTo (tree, propertyID, um, defaultToUse);
}
ValueTreePropertyWithDefault (ValueTree& tree,
const Identifier& propertyID,
UndoManager* um,
var defaultToUse,
StringRef arrayDelimiter)
{
referTo (tree, propertyID, um, defaultToUse, arrayDelimiter);
}
ValueTreePropertyWithDefault (const ValueTreePropertyWithDefault& other)
{
referToWithDefault (other.targetTree,
other.targetProperty,
other.undoManager,
other.defaultValue,
other.delimiter);
}
~ValueTreePropertyWithDefault() override
{
defaultValue.removeListener (this);
}
//==============================================================================
var get() const noexcept
{
if (isUsingDefault())
return defaultValue;
if (delimiter.isNotEmpty())
return delimitedStringToVarArray (targetTree[targetProperty].toString(), delimiter);
return targetTree[targetProperty];
}
Value getPropertyAsValue() { return targetTree.getPropertyAsValue (targetProperty, undoManager); }
var getDefault() const { return defaultValue; }
void setDefault (const var& newDefault) { defaultValue = newDefault; }
bool isUsingDefault() const { return ! targetTree.hasProperty (targetProperty); }
void resetToDefault() noexcept { targetTree.removeProperty (targetProperty, nullptr); }
std::function<void()> onDefaultChange;
//==============================================================================
ValueTreePropertyWithDefault& operator= (const var& newValue)
{
setValue (newValue, undoManager);
return *this;
}
void setValue (const var& newValue, UndoManager* undoManagerToUse)
{
if (auto* array = newValue.getArray())
targetTree.setProperty (targetProperty, varArrayToDelimitedString (*array, delimiter), undoManagerToUse);
else
targetTree.setProperty (targetProperty, newValue, undoManagerToUse);
}
//==============================================================================
void referTo (ValueTree tree,
const Identifier& property,
UndoManager* um)
{
referToWithDefault (tree,
property,
um,
Value (new SynchronousValueSource (var())),
{});
}
void referTo (ValueTree tree,
const Identifier& property,
UndoManager* um,
var defaultVal)
{
referToWithDefault (tree,
property,
um,
Value (new SynchronousValueSource (defaultVal)),
{});
}
void referTo (ValueTree tree,
const Identifier& property,
UndoManager* um,
var defaultVal,
StringRef arrayDelimiter)
{
referToWithDefault (tree,
property,
um,
Value (new SynchronousValueSource (defaultVal)),
arrayDelimiter);
}
//==============================================================================
ValueTree& getValueTree() noexcept { return targetTree; }
Identifier& getPropertyID() noexcept { return targetProperty; }
UndoManager* getUndoManager() noexcept { return undoManager; }
//==============================================================================
ValueTreePropertyWithDefault& operator= (const ValueTreePropertyWithDefault& other)
{
referToWithDefault (other.targetTree,
other.targetProperty,
other.undoManager,
other.defaultValue,
other.delimiter);
return *this;
}
private:
//==============================================================================
class SynchronousValueSource : public Value::ValueSource
{
public:
explicit SynchronousValueSource (const var& initialValue)
: value (initialValue)
{
}
var getValue() const override
{
return value;
}
void setValue (const var& newValue) override
{
if (! newValue.equalsWithSameType (value))
{
value = newValue;
sendChangeMessage (true);
}
}
private:
var value;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SynchronousValueSource)
};
//==============================================================================
static String varArrayToDelimitedString (const Array<var>& input, StringRef delim)
{
// if you are trying to control a var that is an array then you need to
// set a delimiter string that will be used when writing to XML!
jassert (delim.isNotEmpty());
StringArray elements;
for (auto& v : input)
elements.add (v.toString());
return elements.joinIntoString (delim);
}
static Array<var> delimitedStringToVarArray (StringRef input, StringRef delim)
{
Array<var> arr;
for (auto t : StringArray::fromTokens (input, delim, {}))
arr.add (t);
return arr;
}
void valueChanged (Value&) override
{
NullCheckedInvocation::invoke (onDefaultChange);
}
void referToWithDefault (ValueTree v,
const Identifier& i,
UndoManager* um,
const Value& defaultVal,
StringRef del)
{
targetTree = v;
targetProperty = i;
undoManager = um;
defaultValue.referTo (defaultVal);
delimiter = del;
defaultValue.addListener (this);
}
//==============================================================================
ValueTree targetTree;
Identifier targetProperty;
UndoManager* undoManager = nullptr;
Value defaultValue;
String delimiter;
//==============================================================================
JUCE_LEAK_DETECTOR (ValueTreePropertyWithDefault)
};
//==============================================================================
#ifndef DOXYGEN
using ValueWithDefault [[deprecated ("This class has been renamed to better describe what is does. "
"This declaration is here for backwards compatibility and new "
"code should use the new class name.")]]
= ValueTreePropertyWithDefault;
#endif
} // namespace juce
juce::Identifier
Definition juce_Identifier.h:39
juce::StringRef
Definition juce_StringRef.h:62
juce::UndoManager
Definition juce_UndoManager.h:52
juce::ValueTreePropertyWithDefault
Definition juce_ValueTreePropertyWithDefault.h:39
juce::ValueTreePropertyWithDefault::getValueTree
ValueTree & getValueTree() noexcept
Definition juce_ValueTreePropertyWithDefault.h:219
juce::ValueTreePropertyWithDefault::ValueTreePropertyWithDefault
ValueTreePropertyWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um, var defaultToUse, StringRef arrayDelimiter)
Definition juce_ValueTreePropertyWithDefault.h:79
juce::ValueTreePropertyWithDefault::getPropertyAsValue
Value getPropertyAsValue()
Definition juce_ValueTreePropertyWithDefault.h:121
juce::ValueTreePropertyWithDefault::~ValueTreePropertyWithDefault
~ValueTreePropertyWithDefault() override
Definition juce_ValueTreePropertyWithDefault.h:99
juce::ValueTreePropertyWithDefault::isUsingDefault
bool isUsingDefault() const
Definition juce_ValueTreePropertyWithDefault.h:130
juce::ValueTreePropertyWithDefault::referTo
void referTo(ValueTree tree, const Identifier &property, UndoManager *um, var defaultVal, StringRef arrayDelimiter)
Definition juce_ValueTreePropertyWithDefault.h:204
juce::ValueTreePropertyWithDefault::getDefault
var getDefault() const
Definition juce_ValueTreePropertyWithDefault.h:124
juce::ValueTreePropertyWithDefault::setValue
void setValue(const var &newValue, UndoManager *undoManagerToUse)
Definition juce_ValueTreePropertyWithDefault.h:157
juce::ValueTreePropertyWithDefault::onDefaultChange
std::function< void()> onDefaultChange
Definition juce_ValueTreePropertyWithDefault.h:140
juce::ValueTreePropertyWithDefault::referTo
void referTo(ValueTree tree, const Identifier &property, UndoManager *um, var defaultVal)
Definition juce_ValueTreePropertyWithDefault.h:187
juce::ValueTreePropertyWithDefault::getUndoManager
UndoManager * getUndoManager() noexcept
Definition juce_ValueTreePropertyWithDefault.h:225
juce::ValueTreePropertyWithDefault::ValueTreePropertyWithDefault
ValueTreePropertyWithDefault(const ValueTreePropertyWithDefault &other)
Definition juce_ValueTreePropertyWithDefault.h:89
juce::ValueTreePropertyWithDefault::ValueTreePropertyWithDefault
ValueTreePropertyWithDefault()=default
juce::ValueTreePropertyWithDefault::ValueTreePropertyWithDefault
ValueTreePropertyWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um, var defaultToUse)
Definition juce_ValueTreePropertyWithDefault.h:63
juce::ValueTreePropertyWithDefault::setDefault
void setDefault(const var &newDefault)
Definition juce_ValueTreePropertyWithDefault.h:127
juce::ValueTreePropertyWithDefault::getPropertyID
Identifier & getPropertyID() noexcept
Definition juce_ValueTreePropertyWithDefault.h:222
juce::ValueTreePropertyWithDefault::ValueTreePropertyWithDefault
ValueTreePropertyWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um)
Definition juce_ValueTreePropertyWithDefault.h:52
juce::ValueTreePropertyWithDefault::resetToDefault
void resetToDefault() noexcept
Definition juce_ValueTreePropertyWithDefault.h:133
juce::ValueTreePropertyWithDefault::referTo
void referTo(ValueTree tree, const Identifier &property, UndoManager *um)
Definition juce_ValueTreePropertyWithDefault.h:171
juce::ValueTreePropertyWithDefault::get
var get() const noexcept
Definition juce_ValueTreePropertyWithDefault.h:109
juce::ValueTree
Definition juce_ValueTree.h:72
juce::Value::Listener
Definition juce_Value.h:139
juce::Value
Definition juce_Value.h:51
juce::var
Definition juce_Variant.h:42
juce::var::getArray
Array< var > * getArray() const noexcept
Definition juce_Variant.cpp:561