Audacity 3.2.0
ViewInfo.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5ViewInfo.cpp
6
7Paul Licameli
8
9**********************************************************************/
10
11#include "ViewInfo.h"
12
13
14
15#include <algorithm>
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
41NotifyingSelectedRegion& NotifyingSelectedRegion::operator =
42( const SelectedRegion &other )
43{
44 if ( mRegion != other ) {
45 mRegion = other;
46 Notify();
47 }
48 return *this;
49}
50
51bool 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
61bool 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
71bool 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
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
115bool 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
125bool 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
146void 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
161void PlayRegion::SetStart( double start )
162{
163 if (mStart != start) {
164 if (mActive)
165 mLastActiveStart = start;
166 mStart = start;
167 Notify();
168 }
169}
170
172{
173 if (mEnd != end) {
174 if (mActive)
176 mEnd = end;
177 Notify();
178 }
179}
180
181void PlayRegion::SetTimes( double start, double end )
182{
183 if (mStart != start || mEnd != end) {
184 if (mActive)
186 mStart = start, mEnd = end;
187 Notify();
188 }
189}
190
191void PlayRegion::SetAllTimes( double start, double end )
192{
193 SetTimes(start, 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) {
216 if (mActive)
218 Notify();
219 }
220}
221
223{
224 Publish({});
225}
226
227const 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
240const ViewInfo &ViewInfo::Get( const AudacityProject &project )
241{
242 return Get( const_cast< AudacityProject & >( project ) );
243}
244
245ViewInfo::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
280void 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},
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
338BoolSetting ScrollingPreference{ L"/GUI/ScrollBeyondZero", false };
wxT("CloseDown"))
Toolkit-neutral facade for basic user interface services.
int min(int a, int b)
#define XXO(s)
Definition: Internat.h:44
FileConfig * gPrefs
Definition: Prefs.cpp:71
BoolSetting ScrollingPreference
Definition: ViewInfo.cpp:338
const TranslatableString LoopToggleText
Definition: ViewInfo.cpp:227
static ProjectFileIORegistry::AttributeWriterEntry entry
Definition: ViewInfo.cpp:332
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: ViewInfo.cpp:229
static struct ViewInfo::ProjectFileIORegistration projectFileIORegistration
static const auto fn
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
This specialization of Setting for bool adds a Toggle method to negate the saved value.
Definition: Prefs.h:339
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:266
double t1() const
Definition: ViewInfo.h:35
SelectedRegion mRegion
Definition: ViewInfo.h:87
double f1() const
Definition: ViewInfo.h:37
bool setTimes(double t0, double t1)
Definition: ViewInfo.cpp:51
bool setT1(double t, bool maySwap=true)
Definition: ViewInfo.cpp:71
bool setF0(double f, bool maySwap=true)
Definition: ViewInfo.cpp:115
bool setT0(double t, bool maySwap=true)
Definition: ViewInfo.cpp:61
void Notify(bool delayed=false)
Definition: ViewInfo.cpp:135
bool setFrequencies(double f0, double f1)
Definition: ViewInfo.cpp:105
double f0() const
Definition: ViewInfo.h:36
double t0() const
Definition: ViewInfo.h:34
void move(double delta)
Definition: ViewInfo.cpp:97
bool setF1(double f, bool maySwap=true)
Definition: ViewInfo.cpp:125
static XMLMethodRegistryBase::Mutators< NotifyingSelectedRegion > Mutators(const char *legacyT0Name, const char *legacyT1Name)
Return some information used for deserialization purposes by ViewInfo.
Definition: ViewInfo.cpp:24
CallbackReturn Publish(const NotifyingSelectedRegionMessage &message)
Send a message to connected callbacks.
Definition: Observer.h:207
double GetStart() const
Definition: ViewInfo.h:127
void SetStart(double start)
Definition: ViewInfo.cpp:161
bool IsClear() const
Test whether in invalid state.
Definition: ViewInfo.cpp:202
double GetEnd() const
Definition: ViewInfo.h:134
bool IsLastActiveRegionClear() const
Test whether last active region is in invalid state.
Definition: ViewInfo.cpp:207
void Notify()
Definition: ViewInfo.cpp:222
void Order()
Definition: ViewInfo.cpp:212
double mLastActiveStart
Definition: ViewInfo.h:179
void SetAllTimes(double start, double end)
Definition: ViewInfo.cpp:191
static constexpr auto invalidValue
Definition: ViewInfo.h:175
void Clear()
Set to an invalid state.
Definition: ViewInfo.cpp:197
double GetLastActiveStart() const
Definition: ViewInfo.h:141
double mEnd
Definition: ViewInfo.h:178
void SetEnd(double end)
Definition: ViewInfo.cpp:171
double GetLastActiveEnd() const
Definition: ViewInfo.h:148
void SetActive(bool active)
Definition: ViewInfo.cpp:146
double mLastActiveEnd
Definition: ViewInfo.h:180
double mStart
Definition: ViewInfo.h:177
void SetTimes(double start, double end)
Definition: ViewInfo.cpp:181
bool mActive
Definition: ViewInfo.h:182
virtual void UpdateSelectedPrefs(int id)
Definition: Prefs.cpp:128
Defines a selected portion of a project.
double t1() const
static XMLMethodRegistryBase::Mutators< SelectedRegion > Mutators(const char *legacyT0Name, const char *legacyT1Name)
bool setTimes(double t0, double t1)
double t0() const
bool setT0(double t, bool maySwap=true)
bool setT1(double t, bool maySwap=true)
void move(double delta)
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined *‍/
Definition: Prefs.h:200
Holds a msgid for the translation catalog; may also bind format arguments.
bool bUpdateTrackIndicator
Definition: ViewInfo.h:239
void WriteXMLAttributes(XMLWriter &xmlFile) const
Definition: ViewInfo.cpp:288
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:216
void SetBeforeScreenWidth(wxInt64 beforeWidth, wxInt64 screenWidth, double lowerBoundTime=0.0)
Definition: ViewInfo.cpp:280
ViewInfo(double start, double screenDuration, double pixelsPerSecond)
Definition: ViewInfo.cpp:245
double total
Definition: ViewInfo.h:221
static int UpdateScrollPrefsID()
Definition: ViewInfo.cpp:327
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
bool bScrollBeyondZero
Definition: ViewInfo.h:241
void UpdatePrefs() override
Definition: ViewInfo.cpp:268
void UpdateSelectedPrefs(int id) override
Definition: ViewInfo.cpp:260
bool bAdjustSelectionEdges
Definition: ViewInfo.h:242
std::vector< std::pair< std::string, Mutator< Substructure > > > Mutators
A helper type alias for a list of mutators, associated with tag strings.
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26
static double GetDefaultZoom()
Definition: ZoomInfo.h:123
double zoom
Definition: ZoomInfo.h:63
void UpdatePrefs() override
Definition: ZoomInfo.cpp:33
double h
Definition: ZoomInfo.h:60
int vpos
Definition: ZoomInfo.h:58
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:206
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:753
void Notify(AudacityProject &project, ProjectSettings::EventCode code, long previousValue)
Construct once at static initialization time to hook project file IO.
Definition: ViewInfo.cpp:298
ProjectFileIORegistry::AttributeReaderEntries entries2
Definition: ViewInfo.cpp:308
ProjectFileIORegistry::AttributeReaderEntries entries
Definition: ViewInfo.cpp:300
Typically statically constructed.