Audacity 3.2.0
ExportOptionsHandler.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 ExportOptionsHandler.cpp
6
7 Vitaly Sverchinsky
8
9**********************************************************************/
10
12
13#include "ExportUtils.h"
15
16#include "ShuttleGui.h"
17
18#include <wx/spinctrl.h>
19#include <wx/choice.h>
20#include <wx/checkbox.h>
21#include <wx/textctrl.h>
22#include <wx/stattext.h>
23#include <wx/wupdlock.h>
24
25#ifdef wxUSE_ACCESSIBILITY
26#include "WindowAccessible.h"
27#endif
28
29wxDEFINE_EVENT(AUDACITY_FILE_SUFFIX_EVENT, wxCommandEvent);
30
32
34{
35 mParent = S.GetParent();
36
37 mEditor = plugin.CreateOptionsEditor(format, this);
38 if(mEditor)
39 {
40 mEditor->Load(*gPrefs);
41 if(auto uiServices = dynamic_cast<ExportOptionsUIServices*>(mEditor.get()))
42 uiServices->PopulateUI(S);
43 else
45 }
46 else
48}
49
51{
52 if(mEditor)
53 {
54 if(auto uiServices = dynamic_cast<ExportOptionsUIServices*>(mEditor.get()))
55 {
56 if(!uiServices->TransferDataFromWindow())
57 return false;
58 }
59 mEditor->Store(*gPrefs);
60 }
61 return true;
62}
63
65{
66 if(mEditor)
68 return {};
69}
70
72{
73 if(mEditor)
74 {
75 for(const auto& p : parameters)
76 mEditor->SetValue(std::get<0>(p), std::get<1>(p));
77 }
78}
79
81{
82 if(mEditor)
83 return mEditor->GetSampleRateList();
84 return {};
85}
86
88{
89 S.StartHorizontalLay(wxCENTER);
90 {
91 S.StartHorizontalLay(wxCENTER, 0);
92 {
93 S.Prop(1).AddTitle(XO("No format specific options"));
94 }
95 S.EndHorizontalLay();
96 }
97 S.EndHorizontalLay();
98}
99
101{
102 {
103 S.StartMultiColumn(2, wxALIGN_LEFT);
104 {
105 for(int i = 0; i < mEditor->GetOptionsCount(); ++i)
106 {
107 ExportOption option;
108 if(!mEditor->GetOption(i, option))
109 continue;
110
111 ExportValue value;
112 if(!mEditor->GetValue(option.id, value))
113 continue;
114
115 wxControl* control { nullptr };
116
117 auto prompt = S.AddPrompt(option.title);
118 prompt->SetMinSize({140, -1});
119
121 {
122 int selected = -1;
124
125 int index { 0 };
126 std::unordered_map<int, ExportValue> indexValueMap;
127 indexValueMap.reserve(option.values.size());
128 for(auto& e : option.values)
129 {
130 list.push_back(option.names[index]);
131 if(value == e)
132 selected = index;
133 indexValueMap[index] = e;
134 ++index;
135 }
136 control = S.AddChoice({}, list, selected);
137 control->Bind(wxEVT_CHOICE, [this, id = option.id, indexValueMap](const wxCommandEvent& evt)
138 {
139 const auto it = indexValueMap.find(evt.GetInt());
140 if(it != indexValueMap.end())
141 mEditor->SetValue(id, it->second);
142 });
143 }
144 else if(auto selected = std::get_if<bool>(&value))
145 {
146 control = S.Name(option.title).
147 AddCheckBox({}, *selected);
148#if wxUSE_ACCESSIBILITY
149 safenew WindowAccessible(control);
150#endif
151 control->Bind(wxEVT_CHECKBOX, [this, id = option.id](const wxCommandEvent& evt)
152 {
153 const auto checked = evt.GetInt() != 0;
154 mEditor->SetValue(id, checked);
155 });
156 }
157 else if(auto num = std::get_if<int>(&value))
158 {
160 {
161 const int min = *std::get_if<int>(&option.values[0]);
162 const int max = *std::get_if<int>(&option.values[1]);
163 if(max - min < 20)
164 {
165 control = S.Name(option.title)
166 .AddSlider({}, *num, max, min);
167 control->Bind(wxEVT_SLIDER, [this, id = option.id](const wxCommandEvent& evt)
168 {
169 mEditor->SetValue(id, evt.GetInt());
170 });
171 control->SetMinSize({180, -1});
172 }
173 else
174 {
175 control = S.AddSpinCtrl({}, *num, max, min);
176 control->Bind(wxEVT_SPINCTRL, [this, id = option.id](const wxSpinEvent& evt)
177 {
178 mEditor->SetValue(id, evt.GetInt());
179 });
180 }
181 }
182 else
183 {
184 control = S.AddNumericTextBox({}, wxString::Format("%d", *num), 0);
185 control->Bind(wxEVT_TEXT, [this, id = option.id](const wxCommandEvent& evt)
186 {
187 long num;
188 if(evt.GetString().ToLong(&num))
189 mEditor->SetValue(id, static_cast<int>(num));
190 });
191 }
192 }
193 else if(auto str = std::get_if<std::string>(&value))
194 {
195 control = S.AddTextBox({}, wxString::FromUTF8(*str), 0);
196 control->Bind(wxEVT_TEXT, [this, id = option.id](const wxCommandEvent& evt)
197 {
198 mEditor->SetValue(id, evt.GetString().ToStdString());
199 });
200 }
201 mIDRowIndexMap[option.id] = static_cast<int>(mRows.size());
202 mRows.emplace_back(prompt, control);
203
204 if(option.flags & ExportOption::ReadOnly)
205 control->Disable();
206 if(option.flags & ExportOption::Hidden)
207 {
208 prompt->Hide();
209 control->Hide();
210 }
211 }
212 }
213 S.EndMultiColumn();
214 }
215}
216
218{
219 mUpdateLocker = std::make_unique<wxWindowUpdateLocker>(mParent);
220}
221
223{
224 mParent->Layout();
225 mUpdateLocker.reset();
226}
227
229{
230 const auto it = mIDRowIndexMap.find(option.id);
231 if(it == mIDRowIndexMap.end())
232 return;
233
234 const auto index = it->second;
235 const auto [prompt, control] = mRows[index];
236
237 const auto visible = (option.flags & ExportOption::Hidden) == 0;
238
239 prompt->Show(visible);
240 control->Show(visible);
241
242 const auto enabled = (option.flags & ExportOption::ReadOnly) == 0;
243 control->Enable(enabled);
244}
245
247{
249}
250
252{
254}
255
int min(int a, int b)
#define str(a)
wxDEFINE_EVENT(AUDACITY_FILE_SUFFIX_EVENT, wxCommandEvent)
std::variant< bool, int, double, std::string > ExportValue
A type of option values (parameters) used by exporting plugins.
Definition: ExportTypes.h:38
XO("Cut/Copy/Paste")
#define safenew
Definition: MemoryX.h:10
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
#define S(N)
Definition: ToChars.cpp:64
std::vector< TranslatableString > TranslatableStrings
std::vector< int > SampleRateList
ExportOptionsHandler(ShuttleGui &S, const ExportPlugin &plugin, int format)
void PopulateOptions(ShuttleGui &S)
void OnExportOptionChange(const ExportOption &option) override
Called when option change.
std::unordered_map< int, int > mIDRowIndexMap
void PopulateEmpty(ShuttleGui &S)
void OnExportOptionChangeBegin() override
Called before OnExportOptionChange
ExportOptionsEditor::SampleRateList GetSampleRateList() const
void OnSampleRateListChange() override
std::vector< std::tuple< wxStaticText *, wxControl * > > mRows
void OnExportOptionChangeEnd() override
Called after OnExportOptionChange
void OnFormatInfoChange() override
Called when format extension change (usually in response parameter change)
void SetParameters(const ExportProcessor::Parameters &parameters)
std::unique_ptr< wxWindowUpdateLocker > mUpdateLocker
std::unique_ptr< ExportOptionsEditor > mEditor
ExportProcessor::Parameters GetParameters() const
virtual std::unique_ptr< ExportOptionsEditor > CreateOptionsEditor(int formatIndex, ExportOptionsEditor::Listener *listener) const =0
Creates format-dependent options editor, that is used to create a valid set of parameters to be used ...
std::vector< std::tuple< ExportOptionID, ExportValue > > Parameters
Definition: ExportPlugin.h:93
static ExportProcessor::Parameters ParametersFromEditor(const ExportOptionsEditor &editor)
Definition: ExportUtils.cpp:39
CallbackReturn Publish(const ExportOptionsHandlerEvent &message)
Send a message to connected callbacks.
Definition: Observer.h:207
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:640
An alternative to using wxWindowAccessible, which in wxWidgets 3.1.1 contained GetParent() which was ...
A type that provides a description of an exporting option. Isn't allowed to change except non-type re...
Definition: ExportTypes.h:43
ExportOptionID id
Internal option id.
Definition: ExportTypes.h:56
@ TypeMask
Mask for bits that hold option type.
Definition: ExportTypes.h:46
@ TypeEnum
List/enum option. values holds items, and names text to be displayed.
Definition: ExportTypes.h:48
@ ReadOnly
Parameter is read-only, client should not attempt to change it's value.
Definition: ExportTypes.h:50
@ TypeRange
Range option. values holds [min, max].
Definition: ExportTypes.h:47
@ Hidden
Option is not used and may be hidden from the user.
Definition: ExportTypes.h:51
int flags
A set of flag that desc.
Definition: ExportTypes.h:59
std::vector< ExportValue > values
Interpretation depends on type.
Definition: ExportTypes.h:60
TranslatableString title
Name of an option in a human-readable form.
Definition: ExportTypes.h:57
TranslatableStrings names
Interpretation depends on type.
Definition: ExportTypes.h:61