Audacity  3.2.0
ViewInfo.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 ViewInfo.cpp
6 
7 Paul Licameli
8 
9 **********************************************************************/
10 
11 #include "ViewInfo.h"
12 
13 
14 
15 #include <algorithm>
16 #include "XMLAttributeValueView.h"
17 
18 #include "BasicUI.h"
19 #include "Prefs.h"
20 #include "Project.h"
21 #include "XMLWriter.h"
22 
25  const char *legacyT0Name, const char *legacyT1Name)
26 {
28  // Get serialization methods of contained SelectedRegion, and wrap each
29  for (auto &delegate: SelectedRegion::Mutators(legacyT0Name, legacyT1Name)) {
30  results.emplace_back(
31  delegate.first,
32  [fn = std::move(delegate.second)](auto &region, auto value) {
33  fn( region.mRegion, value );
34  region.Notify( true );
35  }
36  );
37  }
38  return results;
39 }
40 
41 NotifyingSelectedRegion& NotifyingSelectedRegion::operator =
42 ( const SelectedRegion &other )
43 {
44  if ( mRegion != other ) {
45  mRegion = other;
46  Notify();
47  }
48  return *this;
49 }
50 
51 bool NotifyingSelectedRegion::setTimes(double t0, double t1)
52 {
53  bool result = false;
54  if ( mRegion.t0() != t0 || mRegion.t1() != t1 ) {
55  result = mRegion.setTimes( t0, t1 );
56  Notify();
57  }
58  return result;
59 }
60 
61 bool NotifyingSelectedRegion::setT0(double t, bool maySwap)
62 {
63  bool result = false;
64  if ( mRegion.t0() != t ) {
65  result = mRegion.setT0( t, maySwap );
66  Notify();
67  }
68  return result;
69 }
70 
71 bool NotifyingSelectedRegion::setT1(double t, bool maySwap)
72 {
73  bool result = false;
74  if ( mRegion.t1() != t ) {
75  result = mRegion.setT1( t, maySwap );
76  Notify();
77  }
78  return result;
79 }
80 
82 {
83  if ( mRegion.t0() != mRegion.t1() ) {
85  Notify();
86  }
87 }
88 
90 {
91  if ( mRegion.t0() != mRegion.t1() ) {
93  Notify();
94  }
95 }
96 
98 {
99  if (delta != 0) {
100  mRegion.move( delta );
101  Notify();
102  }
103 }
104 
105 bool NotifyingSelectedRegion::setFrequencies(double f0, double f1)
106 {
107  bool result = false;
108  if ( mRegion.f0() != f0 || mRegion.f1() != f1 ) {
109  result = mRegion.setFrequencies( f0, f1 );
110  Notify();
111  }
112  return result;
113 }
114 
115 bool NotifyingSelectedRegion::setF0(double f, bool maySwap)
116 {
117  bool result = false;
118  if ( mRegion.f0() != f ) {
119  result = mRegion.setF0( f, maySwap );
120  Notify();
121  }
122  return result;
123 }
124 
125 bool NotifyingSelectedRegion::setF1(double f, bool maySwap)
126 {
127  bool result = false;
128  if ( mRegion.f1() != f ) {
129  result = mRegion.setF1( f, maySwap );
130  Notify();
131  }
132  return result;
133 }
134 
136 {
137  if (delayed)
138  BasicUI::CallAfter([This = wxWeakRef(this)]{
139  if (This)
140  This->Publish({});
141  });
142  else
143  Publish({});
144 }
145 
146 void PlayRegion::SetActive( bool active )
147 {
148  if (mActive != active) {
149  mActive = active;
150  if (mActive) {
151  // Restore values
155  }
156  }
157  Notify();
158  }
159 }
160 
161 void PlayRegion::SetStart( double start )
162 {
163  if (mStart != start) {
164  if (mActive)
165  mLastActiveStart = start;
166  mStart = start;
167  Notify();
168  }
169 }
170 
171 void PlayRegion::SetEnd( double end )
172 {
173  if (mEnd != end) {
174  if (mActive)
175  mLastActiveEnd = end;
176  mEnd = end;
177  Notify();
178  }
179 }
180 
181 void PlayRegion::SetTimes( double start, double end )
182 {
183  if (mStart != start || mEnd != end) {
184  if (mActive)
185  mLastActiveStart = start, mLastActiveEnd = end;
186  mStart = start, mEnd = end;
187  Notify();
188  }
189 }
190 
191 void PlayRegion::SetAllTimes( double start, double end )
192 {
193  SetTimes(start, end);
194  mLastActiveStart = start, mLastActiveEnd = end;
195 }
196 
198 {
200 }
201 
203 {
204  return GetStart() == invalidValue && GetEnd() == invalidValue;
205 }
206 
208 {
210 }
211 
213 {
214  if ( mStart >= 0 && mEnd >= 0 && mStart > mEnd) {
215  std::swap( mStart, mEnd );
216  if (mActive)
218  Notify();
219  }
220 }
221 
223 {
224  Publish({});
225 }
226 
227 const TranslatableString LoopToggleText = XXO("&Loop On/Off");
228 
230  []( AudacityProject &project ) {
231  return std::make_unique<ViewInfo>(0.0, 1.0, ZoomInfo::GetDefaultZoom());
232  }
233 };
234 
236 {
237  return project.AttachedObjects::Get< ViewInfo >( key );
238 }
239 
240 const ViewInfo &ViewInfo::Get( const AudacityProject &project )
241 {
242  return Get( const_cast< AudacityProject & >( project ) );
243 }
244 
245 ViewInfo::ViewInfo(double start, double screenDuration, double pixelsPerSecond)
246  : ZoomInfo(start, pixelsPerSecond)
247  , selectedRegion()
248  , total(screenDuration)
249  , sbarH(0)
250  , sbarScreen(1)
251  , sbarTotal(1)
252  , sbarScale(1.0)
253  , scrollStep(16)
254  , bUpdateTrackIndicator(true)
255  , bScrollBeyondZero(false)
256 {
257  UpdatePrefs();
258 }
259 
261 {
262  if (id == UpdateScrollPrefsID())
263  gPrefs->Read(wxT("/GUI/AutoScroll"), &bUpdateTrackIndicator,
264  true);
266 }
267 
269 {
271 #ifdef EXPERIMENTAL_SCROLLING_LIMITS
273 #endif
274  gPrefs->Read(wxT("/GUI/AdjustSelectionEdges"), &bAdjustSelectionEdges,
275  true);
276 
278 }
279 
280 void ViewInfo::SetBeforeScreenWidth(wxInt64 beforeWidth, wxInt64 screenWidth, double lowerBoundTime)
281 {
282  h =
283  std::max(lowerBoundTime,
284  std::min(total - screenWidth / zoom,
285  beforeWidth / zoom));
286 }
287 
289 // may throw
290 {
291  selectedRegion.WriteXMLAttributes(xmlFile, "sel0", "sel1");
292  xmlFile.WriteAttr(wxT("vpos"), vpos);
293  xmlFile.WriteAttr(wxT("h"), h, 10);
294  xmlFile.WriteAttr(wxT("zoom"), zoom, 10);
295 }
296 
299 
302 {
303  return ViewInfo::Get(project).selectedRegion;
304 },
305 NotifyingSelectedRegion::Mutators("sel0", "sel1")
306 };
307 
309 // Just a pointer to function, but needing overload resolution as non-const:
311 {
312  { "vpos", [](auto &viewInfo, auto value){
313  viewInfo.vpos = value.Get(viewInfo.vpos);
314  // Note that (other than in import of old .aup files) there is no other
315  // reassignment of vpos, except in handling the vertical scroll.
316  } },
317  { "h", [](auto &viewInfo, auto value){
318  viewInfo.h = value.Get(viewInfo.h);
319  } },
320  { "zoom", [](auto &viewInfo, auto value){
321  viewInfo.zoom = value.Get(viewInfo.zoom);
322  } },
323 } };
324 
326 
328 {
329  return 10000;
330 }
331 
333 [](const AudacityProject &project, XMLWriter &xmlFile){
334  ViewInfo::Get(project).WriteXMLAttributes(xmlFile);
335 }
336 };
337 
338 BoolSetting ScrollingPreference{ L"/GUI/ScrollBeyondZero", false };
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26
PlayRegion::Order
void Order()
Definition: ViewInfo.cpp:212
ZoomInfo::vpos
int vpos
Definition: ZoomInfo.h:58
SelectedRegion::Mutators
static XMLMethodRegistryBase::Mutators< SelectedRegion > Mutators(const char *legacyT0Name, const char *legacyT1Name)
Definition: SelectedRegion.cpp:70
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
LoopToggleText
const TranslatableString LoopToggleText
Definition: ViewInfo.cpp:227
PlayRegion::GetLastActiveEnd
double GetLastActiveEnd() const
Definition: ViewInfo.h:147
PlayRegion::GetEnd
double GetEnd() const
Definition: ViewInfo.h:133
XMLWriter.h
ViewInfo::SetBeforeScreenWidth
void SetBeforeScreenWidth(wxInt64 beforeWidth, wxInt64 screenWidth, double lowerBoundTime=0.0)
Definition: ViewInfo.cpp:280
ViewInfo::ProjectFileIORegistration::entries
ProjectFileIORegistry::AttributeReaderEntries entries
Definition: ViewInfo.cpp:300
fn
static const auto fn
Definition: WaveformView.cpp:1122
ViewInfo::bUpdateTrackIndicator
bool bUpdateTrackIndicator
Definition: ViewInfo.h:238
ZoomInfo::UpdatePrefs
void UpdatePrefs() override
Definition: ZoomInfo.cpp:33
gPrefs
FileConfig * gPrefs
Definition: Prefs.cpp:71
SelectedRegion::t1
double t1() const
Definition: SelectedRegion.h:95
ZoomInfo::GetDefaultZoom
static double GetDefaultZoom()
Definition: ZoomInfo.h:123
Project.h
NotifyingSelectedRegion::setF0
bool setF0(double f, bool maySwap=true)
Definition: ViewInfo.cpp:115
ZoomInfo
Definition: ZoomInfo.h:47
ViewInfo::ProjectFileIORegistration
Construct once at static intialization time to hook project file IO.
Definition: ViewInfo.cpp:298
ViewInfo
Definition: ViewInfo.h:188
PlayRegion::GetStart
double GetStart() const
Definition: ViewInfo.h:126
BoolSetting
This specialization of Setting for bool adds a Toggle method to negate the saved value.
Definition: Prefs.h:203
NotifyingSelectedRegion::collapseToT0
void collapseToT0()
Definition: ViewInfo.cpp:81
NotifyingSelectedRegion::setF1
bool setF1(double f, bool maySwap=true)
Definition: ViewInfo.cpp:125
SelectedRegion::setTimes
bool setTimes(double t0, double t1)
Definition: SelectedRegion.h:139
ViewInfo::ProjectFileIORegistration::entries2
ProjectFileIORegistry::AttributeReaderEntries entries2
Definition: ViewInfo.cpp:308
XMLMethodRegistryBase::Mutators
std::vector< std::pair< std::string, Mutator< Substructure > > > Mutators
A helper type alias for a list of mutators, associated with tag strings.
Definition: XMLMethodRegistry.h:39
BasicUI::CallAfter
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:38
PlayRegion::mEnd
double mEnd
Definition: ViewInfo.h:177
ViewInfo::WriteXMLAttributes
void WriteXMLAttributes(XMLWriter &xmlFile) const
Definition: ViewInfo.cpp:288
ClientData::Site::RegisteredFactory
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:266
PlayRegion::IsLastActiveRegionClear
bool IsLastActiveRegionClear() const
Test whether last active region is in invalid state.
Definition: ViewInfo.cpp:207
NotifyingSelectedRegion::t1
double t1() const
Definition: ViewInfo.h:35
PlayRegion::mLastActiveStart
double mLastActiveStart
Definition: ViewInfo.h:178
ScrollingPreference
BoolSetting ScrollingPreference
Definition: ViewInfo.cpp:338
Setting::Read
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined *‍/
Definition: Prefs.h:127
ViewInfo::bScrollBeyondZero
bool bScrollBeyondZero
Definition: ViewInfo.h:240
PlayRegion::mStart
double mStart
Definition: ViewInfo.h:176
NotifyingSelectedRegion::setTimes
bool setTimes(double t0, double t1)
Definition: ViewInfo.cpp:51
PrefsListener::UpdateSelectedPrefs
virtual void UpdateSelectedPrefs(int id)
Definition: Prefs.cpp:124
SelectedRegion::collapseToT0
void collapseToT0()
Definition: SelectedRegion.h:160
NotifyingSelectedRegion::Notify
void Notify(bool delayed=false)
Definition: ViewInfo.cpp:135
SelectedRegion::setT0
bool setT0(double t, bool maySwap=true)
Definition: SelectedRegion.h:115
XMLAttributeValueView.h
ViewInfo::selectedRegion
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:215
XXO
#define XXO(s)
Definition: Internat.h:44
NotifyingSelectedRegion::collapseToT1
void collapseToT1()
Definition: ViewInfo.cpp:89
PlayRegion::invalidValue
static constexpr auto invalidValue
Definition: ViewInfo.h:174
Observer::Publisher< NotifyingSelectedRegionMessage >::Publish
CallbackReturn Publish(const NotifyingSelectedRegionMessage &message)
Send a message to connected callbacks.
Definition: Observer.h:207
key
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: ViewInfo.cpp:229
NotifyingSelectedRegion::setT1
bool setT1(double t, bool maySwap=true)
Definition: ViewInfo.cpp:71
PlayRegion::mLastActiveEnd
double mLastActiveEnd
Definition: ViewInfo.h:179
PlayRegion::SetActive
void SetActive(bool active)
Definition: ViewInfo.cpp:146
PlayRegion::SetTimes
void SetTimes(double start, double end)
Definition: ViewInfo.cpp:181
entry
static ProjectFileIORegistry::AttributeWriterEntry entry
Definition: ViewInfo.cpp:332
PlayRegion::SetEnd
void SetEnd(double end)
Definition: ViewInfo.cpp:171
ViewInfo::UpdateSelectedPrefs
void UpdateSelectedPrefs(int id) override
Definition: ViewInfo.cpp:260
XMLMethodRegistry::AttributeWriterEntry
Typically statically constructed.
Definition: XMLMethodRegistry.h:158
ViewInfo::UpdateScrollPrefsID
static int UpdateScrollPrefsID()
Definition: ViewInfo.cpp:327
anonymous_namespace{NoteTrack.cpp}::swap
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:753
projectFileIORegistration
static struct ViewInfo::ProjectFileIORegistration projectFileIORegistration
ViewInfo.h
NotifyingSelectedRegion::f0
double f0() const
Definition: ViewInfo.h:36
anonymous_namespace{ProjectSettings.cpp}::Notify
void Notify(AudacityProject &project, ProjectSettings::EventCode code, long previousValue)
Definition: ProjectSettings.cpp:26
BasicUI.h
Toolkit-neutral facade for basic user interface services.
NotifyingSelectedRegion::mRegion
SelectedRegion mRegion
Definition: ViewInfo.h:87
min
int min(int a, int b)
Definition: CompareAudioCommand.cpp:106
NotifyingSelectedRegion::f1
double f1() const
Definition: ViewInfo.h:37
PlayRegion::GetLastActiveStart
double GetLastActiveStart() const
Definition: ViewInfo.h:140
SelectedRegion::t0
double t0() const
Definition: SelectedRegion.h:94
ViewInfo::ViewInfo
ViewInfo(double start, double screenDuration, double pixelsPerSecond)
Definition: ViewInfo.cpp:245
NotifyingSelectedRegion::Mutators
static XMLMethodRegistryBase::Mutators< NotifyingSelectedRegion > Mutators(const char *legacyT0Name, const char *legacyT1Name)
Return some information used for deserialization purposes by ViewInfo.
Definition: ViewInfo.cpp:24
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
PlayRegion::IsClear
bool IsClear() const
Test whether in invalid state.
Definition: ViewInfo.cpp:202
PlayRegion::SetAllTimes
void SetAllTimes(double start, double end)
Definition: ViewInfo.cpp:191
ViewInfo::UpdatePrefs
void UpdatePrefs() override
Definition: ViewInfo.cpp:268
PlayRegion::SetStart
void SetStart(double start)
Definition: ViewInfo.cpp:161
PlayRegion::mActive
bool mActive
Definition: ViewInfo.h:181
NotifyingSelectedRegion::setT0
bool setT0(double t, bool maySwap=true)
Definition: ViewInfo.cpp:61
ViewInfo::bAdjustSelectionEdges
bool bAdjustSelectionEdges
Definition: ViewInfo.h:241
NotifyingSelectedRegion::t0
double t0() const
Definition: ViewInfo.h:34
Prefs.h
NotifyingSelectedRegion::move
void move(double delta)
Definition: ViewInfo.cpp:97
NotifyingSelectedRegion::setFrequencies
bool setFrequencies(double f0, double f1)
Definition: ViewInfo.cpp:105
SelectedRegion::collapseToT1
void collapseToT1()
Definition: SelectedRegion.h:162
PlayRegion::Notify
void Notify()
Definition: ViewInfo.cpp:222
ZoomInfo::zoom
double zoom
Definition: ZoomInfo.h:63
XMLMethodRegistry::AttributeReaderEntries
Definition: XMLMethodRegistry.h:124
SelectedRegion::move
void move(double delta)
Definition: SelectedRegion.h:155
SelectedRegion::setT1
bool setT1(double t, bool maySwap=true)
Definition: SelectedRegion.h:127
PlayRegion::Clear
void Clear()
Set to an invalid state.
Definition: ViewInfo.cpp:197
ZoomInfo::h
double h
Definition: ZoomInfo.h:60
ViewInfo::total
double total
Definition: ViewInfo.h:220
NotifyingSelectedRegion
Definition: ViewInfo.h:31
SelectedRegion
Defines a selected portion of a project.
Definition: SelectedRegion.h:38