LMMS
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:
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
125
126 //==============================================================================
128};
129
130//==============================================================================
136
138 const StringArray& choiceList,
139 const Array<var>& correspondingValues)
141 choices (choiceList)
142{
143 // The array of corresponding values must contain one value for each of the items in
144 // the choices array!
145 jassertquiet (correspondingValues.size() == choices.size());
146}
147
149 const String& name,
150 const StringArray& choiceList,
151 const Array<var>& correspondingValues)
152 : ChoicePropertyComponent (name, choiceList, correspondingValues)
153{
155 initialiseComboBox (Value (new ChoiceRemapperValueSource (valueToControl, correspondingValues)));
156}
157
159 const String& name,
160 const StringArray& choiceList,
161 const Array<var>& correspondingValues)
162 : ChoicePropertyComponent (name, choiceList, correspondingValues)
163{
164 value = valueToControl;
165
166 auto getDefaultString = [this, correspondingValues] { return choices [correspondingValues.indexOf (value.getDefault())]; };
167
168 refreshChoices (getDefaultString());
170
171 value.onDefaultChange = [this, getDefaultString]
172 {
173 auto selectedId = comboBox.getSelectedId();
174 refreshChoices (getDefaultString());
175 comboBox.setSelectedId (selectedId);
176 };
177}
178
180 const String& 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//==============================================================================
201{
202 if (v != Value())
203 comboBox.setSelectedId (v.getValue(), dontSendNotification);
204
205 comboBox.getSelectedIdAsValue().referTo (v);
206 comboBox.setEditableText (false);
208}
209
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
226{
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 {
257 comboBox.onChange = [this] { changeIndex(); };
258 }
259
260 comboBox.setSelectedId (getIndex() + 1, dontSendNotification);
261 }
262}
263
265{
266 if (isCustomClass)
267 {
268 auto newIndex = comboBox.getSelectedId() - 1;
269
270 if (newIndex != getIndex())
271 setIndex (newIndex);
272 }
273}
274
275} // namespace juce
Definition juce_Array.h:56
int size() const noexcept
Definition juce_Array.h:215
int indexOf(ParameterType elementToLookFor) const
Definition juce_Array.h:382
StringArray choices
Definition juce_ChoicePropertyComponent.h:134
virtual int getIndex() const
Definition juce_ChoicePropertyComponent.cpp:237
void refreshChoices()
Definition juce_ChoicePropertyComponent.cpp:210
void refresh() override
Definition juce_ChoicePropertyComponent.cpp:249
bool isCustomClass
Definition juce_ChoicePropertyComponent.h:147
virtual void setIndex(int newIndex)
Definition juce_ChoicePropertyComponent.cpp:232
ValueTreePropertyWithDefault value
Definition juce_ChoicePropertyComponent.h:145
const StringArray & getChoices() const
Definition juce_ChoicePropertyComponent.cpp:243
void initialiseComboBox(const Value &)
Definition juce_ChoicePropertyComponent.cpp:200
void changeIndex()
Definition juce_ChoicePropertyComponent.cpp:264
ComboBox comboBox
Definition juce_ChoicePropertyComponent.h:146
ChoicePropertyComponent(const String &, const StringArray &, const Array< var > &)
Definition juce_ChoicePropertyComponent.cpp:137
Definition juce_ChoicePropertyComponent.cpp:32
Value sourceValue
Definition juce_ChoicePropertyComponent.cpp:61
void setValue(const var &newValue) override
Definition juce_ChoicePropertyComponent.cpp:52
Array< var > mappings
Definition juce_ChoicePropertyComponent.cpp:62
var getValue() const override
Definition juce_ChoicePropertyComponent.cpp:41
ChoiceRemapperValueSource(const Value &source, const Array< var > &map)
Definition juce_ChoicePropertyComponent.cpp:34
void valueChanged(Value &) override
Definition juce_ChoicePropertyComponent.cpp:64
Definition juce_ChoicePropertyComponent.cpp:73
void valueChanged(Value &) override
Definition juce_ChoicePropertyComponent.cpp:120
Array< var > mappings
Definition juce_ChoicePropertyComponent.cpp:124
ValueTreePropertyWithDefault value
Definition juce_ChoicePropertyComponent.cpp:122
ChoiceRemapperValueSourceWithDefault(const ValueTreePropertyWithDefault &v, const Array< var > &map)
Definition juce_ChoicePropertyComponent.cpp:75
var getValue() const override
Definition juce_ChoicePropertyComponent.cpp:83
Value sourceValue
Definition juce_ChoicePropertyComponent.cpp:123
void setValue(const var &newValue) override
Definition juce_ChoicePropertyComponent.cpp:102
void addAndMakeVisible(Component *child, int zOrder=-1)
Definition juce_Component.cpp:1554
PropertyComponent(const String &propertyName, int preferredHeight=25)
Definition juce_PropertyComponent.cpp:29
Definition juce_StringArray.h:35
Definition juce_String.h:53
bool isNotEmpty() const noexcept
Definition juce_String.h:316
Definition juce_Value.h:139
Definition juce_Value.h:180
void sendChangeMessage(bool dispatchSynchronously)
Definition juce_Value.cpp:43
friend class Value
Definition juce_Value.h:203
Definition juce_Value.h:51
Definition juce_ValueTreePropertyWithDefault.h:39
Definition juce_Variant.h:42
unsigned v[N_MAX]
Definition inflate.c:1584
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
static const char * name
Definition pugl.h:1582
#define jassertquiet(expression)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
#define jassertfalse
Definition carla_juce.cpp:31
@ dontSendNotification
Definition juce_NotificationType.h:33
@ comboBox
Definition juce_AccessibilityRole.h:41
#define true
Definition ordinals.h:82
typedef int(UZ_EXP MsgFn)()