Audacity  3.0.3
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 
17 #include "Prefs.h"
18 #include "Project.h"
19 #include "XMLWriter.h"
20 
21 wxDEFINE_EVENT( EVT_SELECTED_REGION_CHANGE, SelectedRegionEvent );
22 
24  wxEventType commandType, NotifyingSelectedRegion *pReg )
25 : wxEvent{ 0, commandType }
26 , pRegion{ pReg }
27 {}
28 
30 {
31  return safenew SelectedRegionEvent{ *this };
32 }
33 
36  const wxString &legacyT0Name, const wxString &legacyT1Name)
37 {
39  // Get serialization methods of contained SelectedRegion, and wrap each
40  for (auto &delegate: SelectedRegion::Mutators(legacyT0Name, legacyT1Name)) {
41  results.emplace_back(
42  delegate.first,
43  [fn = std::move(delegate.second)](auto &region, auto value) {
44  fn( region.mRegion, value );
45  region.Notify( true );
46  }
47  );
48  }
49  return results;
50 }
51 
52 NotifyingSelectedRegion& NotifyingSelectedRegion::operator =
53 ( const SelectedRegion &other )
54 {
55  if ( mRegion != other ) {
56  mRegion = other;
57  Notify();
58  }
59  return *this;
60 }
61 
62 bool NotifyingSelectedRegion::setTimes(double t0, double t1)
63 {
64  bool result = false;
65  if ( mRegion.t0() != t0 || mRegion.t1() != t1 ) {
66  result = mRegion.setTimes( t0, t1 );
67  Notify();
68  }
69  return result;
70 }
71 
72 bool NotifyingSelectedRegion::setT0(double t, bool maySwap)
73 {
74  bool result = false;
75  if ( mRegion.t0() != t ) {
76  result = mRegion.setT0( t, maySwap );
77  Notify();
78  }
79  return result;
80 }
81 
82 bool NotifyingSelectedRegion::setT1(double t, bool maySwap)
83 {
84  bool result = false;
85  if ( mRegion.t1() != t ) {
86  result = mRegion.setT1( t, maySwap );
87  Notify();
88  }
89  return result;
90 }
91 
93 {
94  if ( mRegion.t0() != mRegion.t1() ) {
96  Notify();
97  }
98 }
99 
101 {
102  if ( mRegion.t0() != mRegion.t1() ) {
104  Notify();
105  }
106 }
107 
109 {
110  if (delta != 0) {
111  mRegion.move( delta );
112  Notify();
113  }
114 }
115 
116 bool NotifyingSelectedRegion::setFrequencies(double f0, double f1)
117 {
118  bool result = false;
119  if ( mRegion.f0() != f0 || mRegion.f1() != f1 ) {
120  result = mRegion.setFrequencies( f0, f1 );
121  Notify();
122  }
123  return result;
124 }
125 
126 bool NotifyingSelectedRegion::setF0(double f, bool maySwap)
127 {
128  bool result = false;
129  if ( mRegion.f0() != f ) {
130  result = mRegion.setF0( f, maySwap );
131  Notify();
132  }
133  return result;
134 }
135 
136 bool NotifyingSelectedRegion::setF1(double f, bool maySwap)
137 {
138  bool result = false;
139  if ( mRegion.f1() != f ) {
140  result = mRegion.setF1( f, maySwap );
141  Notify();
142  }
143  return result;
144 }
145 
147 {
148  SelectedRegionEvent evt{ EVT_SELECTED_REGION_CHANGE, this };
149  if ( delayed )
150  QueueEvent( evt.Clone() );
151  else
152  ProcessEvent( evt );
153 }
154 
156  []( AudacityProject &project ) {
157  return std::make_unique<ViewInfo>(0.0, 1.0, ZoomInfo::GetDefaultZoom());
158  }
159 };
160 
162 {
163  return project.AttachedObjects::Get< ViewInfo >( key );
164 }
165 
166 const ViewInfo &ViewInfo::Get( const AudacityProject &project )
167 {
168  return Get( const_cast< AudacityProject & >( project ) );
169 }
170 
171 ViewInfo::ViewInfo(double start, double screenDuration, double pixelsPerSecond)
172  : ZoomInfo(start, pixelsPerSecond)
173  , selectedRegion()
174  , total(screenDuration)
175  , sbarH(0)
176  , sbarScreen(1)
177  , sbarTotal(1)
178  , sbarScale(1.0)
179  , scrollStep(16)
180  , bUpdateTrackIndicator(true)
181  , bScrollBeyondZero(false)
182 {
183  UpdatePrefs();
184 }
185 
187 {
188  if (id == UpdateScrollPrefsID())
189  gPrefs->Read(wxT("/GUI/AutoScroll"), &bUpdateTrackIndicator,
190  true);
192 }
193 
195 {
197 #ifdef EXPERIMENTAL_SCROLLING_LIMITS
199 #endif
200  gPrefs->Read(wxT("/GUI/AdjustSelectionEdges"), &bAdjustSelectionEdges,
201  true);
202 
204 }
205 
206 void ViewInfo::SetBeforeScreenWidth(wxInt64 beforeWidth, wxInt64 screenWidth, double lowerBoundTime)
207 {
208  h =
209  std::max(lowerBoundTime,
210  std::min(total - screenWidth / zoom,
211  beforeWidth / zoom));
212 }
213 
215 // may throw
216 {
217  selectedRegion.WriteXMLAttributes(xmlFile, wxT("sel0"), wxT("sel1"));
218  xmlFile.WriteAttr(wxT("vpos"), vpos);
219  xmlFile.WriteAttr(wxT("h"), h, 10);
220  xmlFile.WriteAttr(wxT("zoom"), zoom, 10);
221 }
222 
225 
228 {
229  return ViewInfo::Get(project).selectedRegion;
230 },
231 NotifyingSelectedRegion::Mutators(L"sel0", L"sel1")
232 };
233 
235 // Just a pointer to function, but needing overload resolution as non-const:
237 {
238  { L"vpos", [](auto &viewInfo, auto value){
239  long longVpos;
240  wxString(value).ToLong(&longVpos);
241  viewInfo.vpos = (int)(longVpos);
242  // Note that (other than in import of old .aup files) there is no other
243  // reassignment of vpos, except in handling the vertical scroll.
244  } },
245  { L"h", [](auto &viewInfo, auto value){
246  Internat::CompatibleToDouble(value, &viewInfo.h);
247  } },
248  { L"zoom", [](auto &viewInfo, auto value){
249  Internat::CompatibleToDouble(value, &viewInfo.zoom);
250  } },
251 } };
252 
254 
256 {
257  return 10000;
258 }
259 
261 [](const AudacityProject &project, XMLWriter &xmlFile){
262  ViewInfo::Get(project).WriteXMLAttributes(xmlFile);
263 }
264 };
265 
266 BoolSetting ScrollingPreference{ L"/GUI/ScrollBeyondZero", false };
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:23
wxDEFINE_EVENT
wxDEFINE_EVENT(EVT_SELECTED_REGION_CHANGE, SelectedRegionEvent)
ZoomInfo::vpos
int vpos
Definition: ZoomInfo.h:58
SelectedRegionEvent
Definition: ViewInfo.h:28
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:161
XMLMethodRegistryBase::Mutators
std::vector< std::pair< wxString, Mutator< Substructure > > > Mutators
A helper type alias for a list of mutators, associated with tag strings.
Definition: XMLMethodRegistry.h:36
XMLWriter.h
ViewInfo::SetBeforeScreenWidth
void SetBeforeScreenWidth(wxInt64 beforeWidth, wxInt64 screenWidth, double lowerBoundTime=0.0)
Definition: ViewInfo.cpp:206
ViewInfo::ProjectFileIORegistration::entries
ProjectFileIORegistry::AttributeReaderEntries entries
Definition: ViewInfo.cpp:226
fn
static const auto fn
Definition: WaveformView.cpp:1113
ViewInfo::bUpdateTrackIndicator
bool bUpdateTrackIndicator
Definition: ViewInfo.h:219
ZoomInfo::UpdatePrefs
void UpdatePrefs() override
Definition: ZoomInfo.cpp:31
gPrefs
FileConfig * gPrefs
Definition: Prefs.cpp:70
SelectedRegion::t1
double t1() const
Definition: SelectedRegion.h:92
ZoomInfo::GetDefaultZoom
static double GetDefaultZoom()
Definition: ZoomInfo.h:123
Project.h
NotifyingSelectedRegion::setF0
bool setF0(double f, bool maySwap=true)
Definition: ViewInfo.cpp:126
ZoomInfo
Definition: ZoomInfo.h:47
ViewInfo::ProjectFileIORegistration
Construct once at static intialization time to hook project file IO.
Definition: ViewInfo.cpp:224
ViewInfo
Definition: ViewInfo.h:169
BoolSetting
This specialization of Setting for bool adds a Toggle method to negate the saved value.
Definition: Prefs.h:204
NotifyingSelectedRegion::collapseToT0
void collapseToT0()
Definition: ViewInfo.cpp:92
NotifyingSelectedRegion::setF1
bool setF1(double f, bool maySwap=true)
Definition: ViewInfo.cpp:136
SelectedRegion::setTimes
bool setTimes(double t0, double t1)
Definition: SelectedRegion.h:136
ViewInfo::ProjectFileIORegistration::entries2
ProjectFileIORegistry::AttributeReaderEntries entries2
Definition: ViewInfo.cpp:234
ViewInfo::WriteXMLAttributes
void WriteXMLAttributes(XMLWriter &xmlFile) const
Definition: ViewInfo.cpp:214
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
SelectedRegionEvent::Clone
wxEvent * Clone() const override
Definition: ViewInfo.cpp:29
NotifyingSelectedRegion::t1
double t1() const
Definition: ViewInfo.h:48
entry
static ProjectFileIORegistry::WriterEntry entry
Definition: ViewInfo.cpp:260
XMLMethodRegistry::WriterEntry
Typically statically constructed.
Definition: XMLMethodRegistry.h:150
ScrollingPreference
BoolSetting ScrollingPreference
Definition: ViewInfo.cpp:266
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:128
ViewInfo::bScrollBeyondZero
bool bScrollBeyondZero
Definition: ViewInfo.h:221
NotifyingSelectedRegion::setTimes
bool setTimes(double t0, double t1)
Definition: ViewInfo.cpp:62
PrefsListener::UpdateSelectedPrefs
virtual void UpdateSelectedPrefs(int id)
Definition: Prefs.cpp:127
SelectedRegion::collapseToT0
void collapseToT0()
Definition: SelectedRegion.h:157
SelectedRegion::Mutators
static XMLMethodRegistryBase::Mutators< SelectedRegion > Mutators(const wxString &legacyT0Name, const wxString &legacyT1Name)
Definition: SelectedRegion.cpp:68
anonymous_namespace{ProjectSettings.cpp}::Notify
void Notify(AudacityProject &project, ProjectSettings::EventCode code)
Definition: ProjectSettings.cpp:25
NotifyingSelectedRegion::Notify
void Notify(bool delayed=false)
Definition: ViewInfo.cpp:146
SelectedRegion::setT0
bool setT0(double t, bool maySwap=true)
Definition: SelectedRegion.h:112
ViewInfo::selectedRegion
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:196
NotifyingSelectedRegion::collapseToT1
void collapseToT1()
Definition: ViewInfo.cpp:100
key
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: ViewInfo.cpp:155
NotifyingSelectedRegion::setT1
bool setT1(double t, bool maySwap=true)
Definition: ViewInfo.cpp:82
ViewInfo::UpdateSelectedPrefs
void UpdateSelectedPrefs(int id) override
Definition: ViewInfo.cpp:186
ViewInfo::UpdateScrollPrefsID
static int UpdateScrollPrefsID()
Definition: ViewInfo.cpp:255
projectFileIORegistration
static struct ViewInfo::ProjectFileIORegistration projectFileIORegistration
ViewInfo.h
NotifyingSelectedRegion::f0
double f0() const
Definition: ViewInfo.h:49
NotifyingSelectedRegion::mRegion
SelectedRegion mRegion
Definition: ViewInfo.h:100
min
int min(int a, int b)
Definition: CompareAudioCommand.cpp:106
NotifyingSelectedRegion::f1
double f1() const
Definition: ViewInfo.h:50
SelectedRegion::t0
double t0() const
Definition: SelectedRegion.h:91
NotifyingSelectedRegion::Mutators
static XMLMethodRegistryBase::Mutators< NotifyingSelectedRegion > Mutators(const wxString &legacyT0Name, const wxString &legacyT1Name)
Return some information used for deserialization purposes by ViewInfo.
Definition: ViewInfo.cpp:35
SelectedRegionEvent::SelectedRegionEvent
SelectedRegionEvent(wxEventType commandType, NotifyingSelectedRegion *pRegion)
Definition: ViewInfo.cpp:23
ViewInfo::ViewInfo
ViewInfo(double start, double screenDuration, double pixelsPerSecond)
Definition: ViewInfo.cpp:171
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:92
ViewInfo::UpdatePrefs
void UpdatePrefs() override
Definition: ViewInfo.cpp:194
NotifyingSelectedRegion::setT0
bool setT0(double t, bool maySwap=true)
Definition: ViewInfo.cpp:72
ViewInfo::bAdjustSelectionEdges
bool bAdjustSelectionEdges
Definition: ViewInfo.h:222
NotifyingSelectedRegion::t0
double t0() const
Definition: ViewInfo.h:47
Prefs.h
NotifyingSelectedRegion::move
void move(double delta)
Definition: ViewInfo.cpp:108
NotifyingSelectedRegion::setFrequencies
bool setFrequencies(double f0, double f1)
Definition: ViewInfo.cpp:116
SelectedRegion::collapseToT1
void collapseToT1()
Definition: SelectedRegion.h:159
ZoomInfo::zoom
double zoom
Definition: ZoomInfo.h:63
safenew
#define safenew
Definition: MemoryX.h:10
XMLMethodRegistry::AttributeReaderEntries
Definition: XMLMethodRegistry.h:116
SelectedRegion::move
void move(double delta)
Definition: SelectedRegion.h:152
SelectedRegion::setT1
bool setT1(double t, bool maySwap=true)
Definition: SelectedRegion.h:124
ZoomInfo::h
double h
Definition: ZoomInfo.h:60
Internat::CompatibleToDouble
static bool CompatibleToDouble(const wxString &stringToConvert, double *result)
Convert a string to a number.
Definition: Internat.cpp:134
ViewInfo::total
double total
Definition: ViewInfo.h:201
NotifyingSelectedRegion
Definition: ViewInfo.h:44
SelectedRegion
Defines a selected portion of a project.
Definition: SelectedRegion.h:35