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 "AudioIOBase.h"
18 #include "Prefs.h"
19 #include "Project.h"
20 #include "xml/XMLWriter.h"
22 #include "xml/XMLWriter.h"
23 
24 wxDEFINE_EVENT( EVT_SELECTED_REGION_CHANGE, SelectedRegionEvent );
25 
27  wxEventType commandType, NotifyingSelectedRegion *pReg )
28 : wxEvent{ 0, commandType }
29 , pRegion{ pReg }
30 {}
31 
33 {
34  return safenew SelectedRegionEvent{ *this };
35 }
36 
38  (const wxChar *attr, const wxChar *value,
39  const wxChar *legacyT0Name, const wxChar *legacyT1Name)
40 {
41  auto result = mRegion.HandleXMLAttribute(
42  attr, value, legacyT0Name, legacyT1Name );
43  if ( result )
44  Notify( true );
45  return result;
46 }
47 
48 NotifyingSelectedRegion& NotifyingSelectedRegion::operator =
49 ( const SelectedRegion &other )
50 {
51  if ( mRegion != other ) {
52  mRegion = other;
53  Notify();
54  }
55  return *this;
56 }
57 
58 bool NotifyingSelectedRegion::setTimes(double t0, double t1)
59 {
60  bool result = false;
61  if ( mRegion.t0() != t0 || mRegion.t1() != t1 ) {
62  result = mRegion.setTimes( t0, t1 );
63  Notify();
64  }
65  return result;
66 }
67 
68 bool NotifyingSelectedRegion::setT0(double t, bool maySwap)
69 {
70  bool result = false;
71  if ( mRegion.t0() != t ) {
72  result = mRegion.setT0( t, maySwap );
73  Notify();
74  }
75  return result;
76 }
77 
78 bool NotifyingSelectedRegion::setT1(double t, bool maySwap)
79 {
80  bool result = false;
81  if ( mRegion.t1() != t ) {
82  result = mRegion.setT1( t, maySwap );
83  Notify();
84  }
85  return result;
86 }
87 
89 {
90  if ( mRegion.t0() != mRegion.t1() ) {
92  Notify();
93  }
94 }
95 
97 {
98  if ( mRegion.t0() != mRegion.t1() ) {
100  Notify();
101  }
102 }
103 
105 {
106  if (delta != 0) {
107  mRegion.move( delta );
108  Notify();
109  }
110 }
111 
112 bool NotifyingSelectedRegion::setFrequencies(double f0, double f1)
113 {
114  bool result = false;
115  if ( mRegion.f0() != f0 || mRegion.f1() != f1 ) {
116  result = mRegion.setFrequencies( f0, f1 );
117  Notify();
118  }
119  return result;
120 }
121 
122 bool NotifyingSelectedRegion::setF0(double f, bool maySwap)
123 {
124  bool result = false;
125  if ( mRegion.f0() != f ) {
126  result = mRegion.setF0( f, maySwap );
127  Notify();
128  }
129  return result;
130 }
131 
132 bool NotifyingSelectedRegion::setF1(double f, bool maySwap)
133 {
134  bool result = false;
135  if ( mRegion.f1() != f ) {
136  result = mRegion.setF1( f, maySwap );
137  Notify();
138  }
139  return result;
140 }
141 
143 {
144  SelectedRegionEvent evt{ EVT_SELECTED_REGION_CHANGE, this };
145  if ( delayed )
146  QueueEvent( evt.Clone() );
147  else
148  ProcessEvent( evt );
149 }
150 
152  []( AudacityProject &project ) {
153  auto result =
154  std::make_unique<ViewInfo>(0.0, 1.0, ZoomInfo::GetDefaultZoom());
155  project.Bind(EVT_TRACK_PANEL_TIMER,
157  result.get());
158  return std::move( result );
159  }
160 };
161 
163 {
164  return project.AttachedObjects::Get< ViewInfo >( key );
165 }
166 
167 const ViewInfo &ViewInfo::Get( const AudacityProject &project )
168 {
169  return Get( const_cast< AudacityProject & >( project ) );
170 }
171 
172 ViewInfo::ViewInfo(double start, double screenDuration, double pixelsPerSecond)
173  : ZoomInfo(start, pixelsPerSecond)
174  , selectedRegion()
175  , total(screenDuration)
176  , sbarH(0)
177  , sbarScreen(1)
178  , sbarTotal(1)
179  , sbarScale(1.0)
180  , scrollStep(16)
181  , bUpdateTrackIndicator(true)
182  , bScrollBeyondZero(false)
183  , mRecentStreamTime(-1.0)
184 {
185  UpdatePrefs();
186 }
187 
189 {
190  if (id == UpdateScrollPrefsID())
191  gPrefs->Read(wxT("/GUI/AutoScroll"), &bUpdateTrackIndicator,
192  true);
194 }
195 
197 {
199 #ifdef EXPERIMENTAL_SCROLLING_LIMITS
202 #endif
203  gPrefs->Read(wxT("/GUI/AdjustSelectionEdges"), &bAdjustSelectionEdges,
204  true);
205 
207 }
208 
209 void ViewInfo::SetBeforeScreenWidth(wxInt64 beforeWidth, wxInt64 screenWidth, double lowerBoundTime)
210 {
211  h =
212  std::max(lowerBoundTime,
213  std::min(total - screenWidth / zoom,
214  beforeWidth / zoom));
215 }
216 
218 // may throw
219 {
220  selectedRegion.WriteXMLAttributes(xmlFile, wxT("sel0"), wxT("sel1"));
221  xmlFile.WriteAttr(wxT("vpos"), vpos);
222  xmlFile.WriteAttr(wxT("h"), h, 10);
223  xmlFile.WriteAttr(wxT("zoom"), zoom, 10);
224 }
225 
226 bool ViewInfo::ReadXMLAttribute(const wxChar *attr, const wxChar *value)
227 {
228  if (selectedRegion.HandleXMLAttribute(attr, value, wxT("sel0"), wxT("sel1")))
229  return true;
230 
231  if (!wxStrcmp(attr, wxT("vpos"))) {
232  long longVpos;
233  wxString(value).ToLong(&longVpos);
234  vpos = (int)(longVpos);
235  return true;
236  }
237 
238  if (!wxStrcmp(attr, wxT("h"))) {
240  return true;
241  }
242 
243  if (!wxStrcmp(attr, wxT("zoom"))) {
245  return true;
246  }
247 
248  return false;
249 }
250 
251 void ViewInfo::OnTimer(wxCommandEvent &event)
252 {
253  auto gAudioIO = AudioIOBase::Get();
254  mRecentStreamTime = gAudioIO->GetStreamTime();
255  event.Skip();
256  // Propagate the message to other listeners bound to this
257  this->ProcessEvent( event );
258 }
259 
261 {
262  static int value = wxNewId();
263  return value;
264 }
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:26
AudioIOBase.h
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:162
XMLWriter.h
ViewInfo::SetBeforeScreenWidth
void SetBeforeScreenWidth(wxInt64 beforeWidth, wxInt64 screenWidth, double lowerBoundTime=0.0)
Definition: ViewInfo.cpp:209
ViewInfo::bUpdateTrackIndicator
bool bUpdateTrackIndicator
Definition: ViewInfo.h:222
NotifyingSelectedRegion::HandleXMLAttribute
bool HandleXMLAttribute(const wxChar *attr, const wxChar *value, const wxChar *legacyT0Name, const wxChar *legacyT1Name)
Definition: ViewInfo.cpp:38
ZoomInfo::UpdatePrefs
void UpdatePrefs() override
Definition: ZoomInfo.cpp:35
gPrefs
FileConfig * gPrefs
Definition: Prefs.cpp:67
SelectedRegion::t1
double t1() const
Definition: SelectedRegion.h:95
ZoomInfo::GetDefaultZoom
static double GetDefaultZoom()
Definition: ZoomInfo.h:121
Project.h
NotifyingSelectedRegion::setF0
bool setF0(double f, bool maySwap=true)
Definition: ViewInfo.cpp:122
ZoomInfo
Definition: ZoomInfo.h:47
ViewInfo
Definition: ViewInfo.h:172
NotifyingSelectedRegion::collapseToT0
void collapseToT0()
Definition: ViewInfo.cpp:88
TracksBehaviorsPrefs::ScrollingPreferenceKey
static const wxChar * ScrollingPreferenceKey()
Definition: TracksBehaviorsPrefs.cpp:49
NotifyingSelectedRegion::setF1
bool setF1(double f, bool maySwap=true)
Definition: ViewInfo.cpp:132
SelectedRegion::setTimes
bool setTimes(double t0, double t1)
Definition: SelectedRegion.h:139
TracksBehaviorsPrefs.h
ViewInfo::WriteXMLAttributes
void WriteXMLAttributes(XMLWriter &xmlFile) const
Definition: ViewInfo.cpp:217
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:32
NotifyingSelectedRegion::t1
double t1() const
Definition: ViewInfo.h:46
ViewInfo::bScrollBeyondZero
bool bScrollBeyondZero
Definition: ViewInfo.h:224
NotifyingSelectedRegion::setTimes
bool setTimes(double t0, double t1)
Definition: ViewInfo.cpp:58
PrefsListener::UpdateSelectedPrefs
virtual void UpdateSelectedPrefs(int id)
Definition: Prefs.cpp:99
SelectedRegion::collapseToT0
void collapseToT0()
Definition: SelectedRegion.h:160
anonymous_namespace{ProjectSettings.cpp}::Notify
void Notify(AudacityProject &project, ProjectSettings::EventCode code)
Definition: ProjectSettings.cpp:23
NotifyingSelectedRegion::Notify
void Notify(bool delayed=false)
Definition: ViewInfo.cpp:142
SelectedRegion::setT0
bool setT0(double t, bool maySwap=true)
Definition: SelectedRegion.h:115
ViewInfo::selectedRegion
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:199
NotifyingSelectedRegion::collapseToT1
void collapseToT1()
Definition: ViewInfo.cpp:96
key
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: ViewInfo.cpp:151
NotifyingSelectedRegion::setT1
bool setT1(double t, bool maySwap=true)
Definition: ViewInfo.cpp:78
ViewInfo::UpdateSelectedPrefs
void UpdateSelectedPrefs(int id) override
Definition: ViewInfo.cpp:188
AudioIOBase::Get
static AudioIOBase * Get()
Definition: AudioIOBase.cpp:94
ViewInfo::UpdateScrollPrefsID
static int UpdateScrollPrefsID()
Definition: ViewInfo.cpp:260
ViewInfo.h
NotifyingSelectedRegion::f0
double f0() const
Definition: ViewInfo.h:47
TracksBehaviorsPrefs::ScrollingPreferenceDefault
static bool ScrollingPreferenceDefault()
Definition: TracksBehaviorsPrefs.h:36
NotifyingSelectedRegion::mRegion
SelectedRegion mRegion
Definition: ViewInfo.h:98
min
int min(int a, int b)
Definition: CompareAudioCommand.cpp:106
NotifyingSelectedRegion::f1
double f1() const
Definition: ViewInfo.h:48
SelectedRegion::t0
double t0() const
Definition: SelectedRegion.h:94
SelectedRegionEvent::SelectedRegionEvent
SelectedRegionEvent(wxEventType commandType, NotifyingSelectedRegion *pRegion)
Definition: ViewInfo.cpp:26
ViewInfo::ViewInfo
ViewInfo(double start, double screenDuration, double pixelsPerSecond)
Definition: ViewInfo.cpp:172
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:112
ViewInfo::UpdatePrefs
void UpdatePrefs() override
Definition: ViewInfo.cpp:196
ViewInfo::ReadXMLAttribute
bool ReadXMLAttribute(const wxChar *attr, const wxChar *value)
Definition: ViewInfo.cpp:226
NotifyingSelectedRegion::setT0
bool setT0(double t, bool maySwap=true)
Definition: ViewInfo.cpp:68
ViewInfo::OnTimer
void OnTimer(wxCommandEvent &event)
Definition: ViewInfo.cpp:251
ViewInfo::bAdjustSelectionEdges
bool bAdjustSelectionEdges
Definition: ViewInfo.h:225
NotifyingSelectedRegion::t0
double t0() const
Definition: ViewInfo.h:45
Prefs.h
SelectedRegion::HandleXMLAttribute
bool HandleXMLAttribute(const wxChar *attr, const wxChar *value, const wxChar *legacyT0Name=sDefaultT0Name, const wxChar *legacyT1Name=sDefaultT1Name)
Definition: SelectedRegion.cpp:41
NotifyingSelectedRegion::move
void move(double delta)
Definition: ViewInfo.cpp:104
NotifyingSelectedRegion::setFrequencies
bool setFrequencies(double f0, double f1)
Definition: ViewInfo.cpp:112
SelectedRegion::collapseToT1
void collapseToT1()
Definition: SelectedRegion.h:162
ZoomInfo::zoom
double zoom
Definition: ZoomInfo.h:63
safenew
#define safenew
Definition: MemoryX.h:8
SelectedRegion::move
void move(double delta)
Definition: SelectedRegion.h:155
SelectedRegion::setT1
bool setT1(double t, bool maySwap=true)
Definition: SelectedRegion.h:127
ViewInfo::mRecentStreamTime
double mRecentStreamTime
Definition: ViewInfo.h:229
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:139
ViewInfo::total
double total
Definition: ViewInfo.h:204
NotifyingSelectedRegion
Definition: ViewInfo.h:42
SelectedRegion
Defines a selected portion of a project.
Definition: SelectedRegion.h:38