JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ChoicePropertyComponent.cpp
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//==============================================================================
31 private Value::Listener
32{
33public:
34 ChoiceRemapperValueSource (const Value& source, const Array<var>& map)
35 : sourceValue (source),
36 mappings (map)
37 {
38 sourceValue.addListener (this);
39 }
40
41 var getValue() const override
42 {
43 auto targetValue = sourceValue.getValue();
44
45 for (auto& map : mappings)
46 if (map.equalsWithSameType (targetValue))
47 return mappings.indexOf (map) + 1;
48
49 return mappings.indexOf (targetValue) + 1;
50 }
51
52 void setValue (const var& newValue) override
53 {
54 auto remappedVal = mappings [static_cast<int> (newValue) - 1];
55
56 if (! remappedVal.equalsWithSameType (sourceValue))
57 sourceValue = remappedVal;
58 }
59
60protected:
61 Value sourceValue;
62 Array<var> mappings;
63
64 void valueChanged (Value&) override { sendChangeMessage (true); }
65
66 //==============================================================================
68};
69
70//==============================================================================
72 private Value::Listener
73{
74public:
76 : value (v),
77 sourceValue (value.getPropertyAsValue()),
78 mappings (map)
79 {
80 sourceValue.addListener (this);
81 }
82
83 var getValue() const override
84 {
85 if (! value.isUsingDefault())
86 {
87 const auto target = sourceValue.getValue();
88 const auto equalsWithSameType = [&target] (const var& map) { return map.equalsWithSameType (target); };
89
90 auto iter = std::find_if (mappings.begin(), mappings.end(), equalsWithSameType);
91
92 if (iter == mappings.end())
93 iter = std::find (mappings.begin(), mappings.end(), target);
94
95 if (iter != mappings.end())
96 return 1 + (int) std::distance (mappings.begin(), iter);
97 }
98
99 return -1;
100 }
101
102 void setValue (const var& newValue) override
103 {
104 auto newValueInt = static_cast<int> (newValue);
105
106 if (newValueInt == -1)
107 {
108 value.resetToDefault();
109 }
110 else
111 {
112 auto remappedVal = mappings [newValueInt - 1];
113
114 if (! remappedVal.equalsWithSameType (sourceValue))
115 value = remappedVal;
116 }
117 }
118
119private:
120 void valueChanged (Value&) override { sendChangeMessage (true); }
121
122 ValueTreePropertyWithDefault value;
123 Value sourceValue;
124 Array<var> mappings;
125
126 //==============================================================================
127 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChoiceRemapperValueSourceWithDefault)
128};
129
130//==============================================================================
131ChoicePropertyComponent::ChoicePropertyComponent (const String& name)
132 : PropertyComponent (name),
133 isCustomClass (true)
134{
135}
136
137ChoicePropertyComponent::ChoicePropertyComponent (const String& name,
138 const StringArray& choiceList,
140 : PropertyComponent (name),
141 choices (choiceList)
142{
143 // The array of corresponding values must contain one value for each of the items in
144 // the choices array!
146}
147
148ChoicePropertyComponent::ChoicePropertyComponent (const Value& valueToControl,
149 const String& name,
150 const StringArray& choiceList,
153{
154 refreshChoices();
156}
157
158ChoicePropertyComponent::ChoicePropertyComponent (const ValueTreePropertyWithDefault& valueToControl,
159 const String& name,
160 const StringArray& choiceList,
163{
164 value = valueToControl;
165
166 auto getDefaultString = [this, correspondingValues] { return choices [correspondingValues.indexOf (value.getDefault())]; };
167
168 refreshChoices (getDefaultString());
169 initialiseComboBox (Value (new ChoiceRemapperValueSourceWithDefault (value, correspondingValues)));
170
171 value.onDefaultChange = [this, getDefaultString]
172 {
173 auto selectedId = comboBox.getSelectedId();
174 refreshChoices (getDefaultString());
175 comboBox.setSelectedId (selectedId);
176 };
177}
178
179ChoicePropertyComponent::ChoicePropertyComponent (const ValueTreePropertyWithDefault& valueToControl,
180 const String& name)
181 : PropertyComponent (name),
182 choices ({ "Enabled", "Disabled" })
183{
184 value = valueToControl;
185
186 auto getDefaultString = [this] { return value.getDefault() ? "Enabled" : "Disabled"; };
187
188 refreshChoices (getDefaultString());
189 initialiseComboBox (Value (new ChoiceRemapperValueSourceWithDefault (value, { true, false })));
190
191 value.onDefaultChange = [this, getDefaultString]
192 {
193 auto selectedId = comboBox.getSelectedId();
194 refreshChoices (getDefaultString());
195 comboBox.setSelectedId (selectedId);
196 };
197}
198
199//==============================================================================
200void ChoicePropertyComponent::initialiseComboBox (const Value& v)
201{
202 if (v != Value())
203 comboBox.setSelectedId (v.getValue(), dontSendNotification);
204
205 comboBox.getSelectedIdAsValue().referTo (v);
206 comboBox.setEditableText (false);
207 addAndMakeVisible (comboBox);
208}
209
210void ChoicePropertyComponent::refreshChoices()
211{
212 comboBox.clear();
213
214 for (int i = 0; i < choices.size(); ++i)
215 {
216 const auto& choice = choices[i];
217
218 if (choice.isNotEmpty())
219 comboBox.addItem (choice, i + 1);
220 else
221 comboBox.addSeparator();
222 }
223}
224
225void ChoicePropertyComponent::refreshChoices (const String& defaultString)
226{
227 refreshChoices();
228 comboBox.addItem ("Default" + (defaultString.isNotEmpty() ? " (" + defaultString + ")" : ""), -1);
229}
230
231//==============================================================================
232void ChoicePropertyComponent::setIndex (const int /*newIndex*/)
233{
234 jassertfalse; // you need to override this method in your subclass!
235}
236
238{
239 jassertfalse; // you need to override this method in your subclass!
240 return -1;
241}
242
244{
245 return choices;
246}
247
248//==============================================================================
250{
251 if (isCustomClass)
252 {
253 if (! comboBox.isVisible())
254 {
255 refreshChoices();
256 initialiseComboBox ({});
257 comboBox.onChange = [this] { changeIndex(); };
258 }
259
261 }
262}
263
264void ChoicePropertyComponent::changeIndex()
265{
266 if (isCustomClass)
267 {
268 auto newIndex = comboBox.getSelectedId() - 1;
269
270 if (newIndex != getIndex())
272 }
273}
274
275} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
A PropertyComponent that shows its value as a combo box.
StringArray choices
The list of options that will be shown in the combo box.
virtual int getIndex() const
Returns the index of the item that should currently be shown.
void refresh() override
Updates the property component if the item it refers to has changed.
virtual void setIndex(int newIndex)
Called when the user selects an item from the combo box.
const StringArray & getChoices() const
Returns the list of options.
var getValue() const override
Returns the current value of this object.
void setValue(const var &newValue) override
Changes the current value.
void setValue(const var &newValue) override
Changes the current value.
var getValue() const override
Returns the current value of this object.
void valueChanged(Value &) override
Called when a Value object is changed.
void clear(NotificationType notification=sendNotificationAsync)
Removes all the items from the drop-down list.
Value & getSelectedIdAsValue()
Returns a Value object that can be used to get or set the selected item's ID.
void setSelectedId(int newItemId, NotificationType notification=sendNotificationAsync)
Sets one of the items to be the current selection.
std::function< void()> onChange
You can assign a lambda to this callback object to have it called when the selected ID is changed.
void setEditableText(bool isEditable)
Sets whether the text in the combo-box is editable.
void addSeparator()
Adds a separator line to the drop-down list.
int getSelectedId() const noexcept
Returns the ID of the item that's currently shown in the box.
void addItem(const String &newItemText, int newItemId)
Adds an item to be shown in the drop-down list.
bool isVisible() const noexcept
Tests whether the component is visible or not.
void addAndMakeVisible(Component *child, int zOrder=-1)
Adds a child component to this one, and also makes the child visible if it isn't already.
A base class for a component that goes in a PropertyPanel and displays one of an item's properties.
A special array for holding a list of strings.
int size() const noexcept
Returns the number of strings in the array.
The JUCE String class!
Definition juce_String.h:53
This class acts as a wrapper around a property inside a ValueTree.
bool isUsingDefault() const
Returns true if the property does not exist in the referenced ValueTree.
var getDefault() const
Returns the current default value.
std::function< void()> onDefaultChange
You can assign a lambda to this callback and it will called when the default value is changed.
void resetToDefault() noexcept
Removes the property from the referenced ValueTree.
Receives callbacks when a Value object changes.
Definition juce_Value.h:139
Used internally by the Value class as the base class for its shared value objects.
Definition juce_Value.h:180
void sendChangeMessage(bool dispatchSynchronously)
Delivers a change message to all the listeners that are registered with this value.
Represents a shared variant value.
Definition juce_Value.h:51
void addListener(Listener *listener)
Adds a listener to receive callbacks when the value changes.
void referTo(const Value &valueToReferTo)
Makes this object refer to the same underlying ValueSource as another one.
var getValue() const
Returns the current value.
A variant class, that can be used to hold a range of primitive values.
T distance(T... args)
T find_if(T... args)
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
#define jassertfalse
This will always cause an assertion failure.
typedef int
JUCE Namespace.
@ valueChanged
Indicates that the UI element's value has changed.
@ dontSendNotification
No notification message should be sent.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88