1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 WaveformPrefs.cpp
6 
7 Paul Licameli
8 
9 *******************************************************************//**
10 
11 \class WaveformPrefs
12 \brief A PrefsPanel for spectrum settings.
13 
14 *//*******************************************************************/
15 
16 
17 #include "WaveformPrefs.h"
18 
19 #include "GUIPrefs.h"
20 #include "Decibels.h"
21 
22 #include <wx/checkbox.h>
23 #include <wx/choice.h>
24 
25 #include "Project.h"
26 
27 #include "../TrackPanel.h"
28 #include "../ShuttleGui.h"
29 #include "../WaveTrack.h"
30 #include "../tracks/playabletrack/wavetrack/ui/WaveTrackView.h"
31 #include "../tracks/playabletrack/wavetrack/ui/WaveTrackViewConstants.h"
32 
WaveformPrefs(wxWindow * parent,wxWindowID winid,AudacityProject * pProject,WaveTrack * wt)33 WaveformPrefs::WaveformPrefs(wxWindow * parent, wxWindowID winid,
34    AudacityProject *pProject, WaveTrack *wt)
35 /* i18n-hint: A waveform is a visual representation of vibration */
36 : PrefsPanel(parent, winid, XO("Waveforms"))
37 , mProject{ pProject }
38 , mWt(wt)
39 , mPopulating(false)
40 {
41    if (mWt) {
42       WaveformSettings &settings = wt->GetWaveformSettings();
43       mDefaulted = (&WaveformSettings::defaults() == &settings);
44       mTempSettings = settings;
45    }
46    else  {
47       mTempSettings = WaveformSettings::defaults();
48       mDefaulted = false;
49    }
50 
51    mTempSettings.ConvertToEnumeratedDBRange();
52    Populate();
53 }
54 
~WaveformPrefs()55 WaveformPrefs::~WaveformPrefs()
56 {
57 }
58 
GetSymbol()59 ComponentInterfaceSymbol WaveformPrefs::GetSymbol()
60 {
61    return WAVEFORM_PREFS_PLUGIN_SYMBOL;
62 }
63 
GetDescription()64 TranslatableString WaveformPrefs::GetDescription()
65 {
66    return XO("Preferences for Waveforms");
67 }
68 
HelpPageName()69 ManualPageID WaveformPrefs::HelpPageName()
70 {
71    return "Waveform_Preferences";
72 }
73 
74 enum {
75    ID_DEFAULTS = 10001,
76 
77    ID_SCALE,
78    ID_RANGE,
79 };
80 
Populate()81 void WaveformPrefs::Populate()
82 {
83    // Reuse the same choices and codes as for Interface prefs
84    GUIPrefs::GetRangeChoices(&mRangeChoices, &mRangeCodes);
85 
86    //------------------------- Main section --------------------
87    // Now construct the GUI itself.
88    ShuttleGui S(this, eIsCreatingFromPrefs);
89    PopulateOrExchange(S);
90    // ----------------------- End of main section --------------
91 }
92 
PopulateOrExchange(ShuttleGui & S)93 void WaveformPrefs::PopulateOrExchange(ShuttleGui & S)
94 {
95    mPopulating = true;
96 
97    S.SetBorder(2);
98    S.StartScroller();
99 
100    // S.StartStatic(XO("Track Settings"));
101    {
102       mDefaultsCheckbox = 0;
103       if (mWt) {
104          /* i18n-hint: use is a verb */
105          mDefaultsCheckbox = S.Id(ID_DEFAULTS).TieCheckBox(XXO("&Use Preferences"), mDefaulted);
106       }
107 
108       S.StartStatic(XO("Display"));
109       {
110          S.StartTwoColumn();
111          {
112             mScaleChoice =
113                S.Id(ID_SCALE).TieChoice(XXO("S&cale:"),
114                   mTempSettings.scaleType,
115                   Msgids( WaveformSettings::GetScaleNames() ) );
116 
117             mRangeChoice =
118                S.Id(ID_RANGE).TieChoice(XXO("Waveform dB &range:"),
119                mTempSettings.dBRange,
120                mRangeChoices);
121          }
122          S.EndTwoColumn();
123       }
124       S.EndStatic();
125    }
126    // S.EndStatic();
127 
128    /*
129    S.StartStatic(XO("Global settings"));
130    {
131    }
132    S.EndStatic();
133    */
134 
135    S.EndScroller();
136 
137    EnableDisableRange();
138 
139    mPopulating = false;
140 }
141 
Validate()142 bool WaveformPrefs::Validate()
143 {
144    // Do checking for whole numbers
145 
146    // ToDo: use wxIntegerValidator<unsigned> when available
147 
148    ShuttleGui S(this, eIsGettingFromDialog);
149    PopulateOrExchange(S);
150 
151    // Delegate range checking to WaveformSettings class
152    mTempSettings.ConvertToActualDBRange();
153    const bool result = mTempSettings.Validate(false);
154    mTempSettings.ConvertToEnumeratedDBRange();
155    return result;
156 }
157 
Commit()158 bool WaveformPrefs::Commit()
159 {
160    const bool isOpenPage = this->IsShown();
161 
162    ShuttleGui S(this, eIsGettingFromDialog);
163    PopulateOrExchange(S);
164 
165    mTempSettings.ConvertToActualDBRange();
166    WaveformSettings::Globals::Get().SavePrefs();
167 
168    if (mWt) {
169       for (auto channel : TrackList::Channels(mWt)) {
170          if (mDefaulted)
171             channel->SetWaveformSettings({});
172          else {
173             WaveformSettings &settings =
174                channel->GetWaveformSettings();
175             settings = mTempSettings;
176          }
177       }
178    }
179 
180    WaveformSettings *const pSettings = &WaveformSettings::defaults();
181    if (!mWt || mDefaulted) {
182       *pSettings = mTempSettings;
183       pSettings->SavePrefs();
184    }
185    pSettings->LoadPrefs(); // always; in case Globals changed
186 
187    mTempSettings.ConvertToEnumeratedDBRange();
188 
189    if (mWt && isOpenPage) {
190       for (auto channel : TrackList::Channels(mWt))
191          WaveTrackView::Get( *channel )
192             .SetDisplay( WaveTrackViewConstants::Waveform );
193    }
194 
195    if (isOpenPage) {
196       if ( mProject ) {
197          auto &tp = TrackPanel::Get( *mProject );
198          tp.UpdateVRulers();
199          tp.Refresh(false);
200       }
201    }
202 
203    return true;
204 }
205 
ShowsPreviewButton()206 bool WaveformPrefs::ShowsPreviewButton()
207 {
208    return true;
209 }
210 
OnControl(wxCommandEvent &)211 void WaveformPrefs::OnControl(wxCommandEvent&)
212 {
213    // Common routine for most controls
214    // If any per-track setting is changed, break the association with defaults
215    // Skip this, and View Settings... will be able to change defaults instead
216    // when the checkbox is on, as in the original design.
217 
218    if (mDefaultsCheckbox && !mPopulating) {
219       mDefaulted = false;
220       mDefaultsCheckbox->SetValue(false);
221    }
222 }
223 
OnScale(wxCommandEvent & e)224 void WaveformPrefs::OnScale(wxCommandEvent &e)
225 {
226    EnableDisableRange();
227 
228    // do the common part
229    OnControl(e);
230 }
231 
OnDefaults(wxCommandEvent &)232 void WaveformPrefs::OnDefaults(wxCommandEvent &)
233 {
234    if (mDefaultsCheckbox->IsChecked()) {
235       mTempSettings = WaveformSettings::defaults();
236       mTempSettings.ConvertToEnumeratedDBRange();
237       mDefaulted = true;
238       ShuttleGui S(this, eIsSettingToDialog);
239       PopulateOrExchange(S);
240    }
241 }
242 
EnableDisableRange()243 void WaveformPrefs::EnableDisableRange()
244 {
245    mRangeChoice->Enable
246       (mScaleChoice->GetSelection() == WaveformSettings::stLogarithmic);
247 }
248 
BEGIN_EVENT_TABLE(WaveformPrefs,PrefsPanel)249 BEGIN_EVENT_TABLE(WaveformPrefs, PrefsPanel)
250 
251 EVT_CHOICE(ID_SCALE, WaveformPrefs::OnScale)
252 EVT_CHOICE(ID_RANGE, WaveformPrefs::OnControl)
253 
254 EVT_CHECKBOX(ID_DEFAULTS, WaveformPrefs::OnDefaults)
255 END_EVENT_TABLE()
256 
257 PrefsPanel::Factory
258 WaveformPrefsFactory(WaveTrack *wt)
259 {
260    return [=](wxWindow *parent, wxWindowID winid, AudacityProject *pProject)
261    {
262       wxASSERT(parent); // to justify safenew
263       return safenew WaveformPrefs(parent, winid, pProject, wt);
264    };
265 }
266 #if 0
267 namespace{
268 PrefsPanel::Registration sAttachment{ "Waveform",
269    WaveformPrefsFactory( nullptr ),
270    false,
271    // Register with an explicit ordering hint because this one is
272    // only conditionally compiled; and place it at a lower tree level
273    { "Tracks", { Registry::OrderingHint::Before, "Spectrum" } }
274 };
275 }
276 #endif
277