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 "Project.h"
20#include "UndoManager.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("Enable &Looping");
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
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);
265}
266
268{
269#ifdef EXPERIMENTAL_SCROLLING_LIMITS
271#endif
272 gPrefs->Read(wxT("/GUI/AdjustSelectionEdges"), &bAdjustSelectionEdges,
273 true);
274
276}
277
278void ViewInfo::SetBeforeScreenWidth(wxInt64 beforeWidth, wxInt64 screenWidth, double lowerBoundTime)
279{
280 h =
281 std::max(lowerBoundTime,
282 std::min(total - screenWidth / zoom,
283 beforeWidth / zoom));
284}
285
287// may throw
288{
289 selectedRegion.WriteXMLAttributes(xmlFile, "sel0", "sel1");
290 xmlFile.WriteAttr(wxT("vpos"), vpos);
291 xmlFile.WriteAttr(wxT("h"), h, 10);
292 xmlFile.WriteAttr(wxT("zoom"), zoom, 10);
293}
294
297
300{
302},
304};
305
307// Just a pointer to function, but needing overload resolution as non-const:
309{
310 { "vpos", [](auto &viewInfo, auto value){
311 viewInfo.vpos = value.Get(viewInfo.vpos);
312 // Note that (other than in import of old .aup files) there is no other
313 // reassignment of vpos, except in handling the vertical scroll.
314 } },
315 { "h", [](auto &viewInfo, auto value){
316 viewInfo.h = value.Get(viewInfo.h);
317 } },
318 { "zoom", [](auto &viewInfo, auto value){
319 viewInfo.zoom = value.Get(viewInfo.zoom);
320 } },
321} };
322
324
326{
327 return 10000;
328}
329
331[](const AudacityProject &project, XMLWriter &xmlFile){
333}
334};
335
336BoolSetting ScrollingPreference{ L"/GUI/ScrollBeyondZero", false };
337
338// Undo/redo handling of selection changes
339namespace {
342 : mSelectedRegion{ ViewInfo::Get(project).selectedRegion }
343 {}
345 ViewInfo::Get(project).selectedRegion = mSelectedRegion;
346 }
348};
349
351 [](AudacityProject &project) -> std::shared_ptr<UndoStateExtension> {
352 return std::make_shared<SelectedRegionRestorer>(project);
353 }
354};
355}
wxT("CloseDown"))
Toolkit-neutral facade for basic user interface services.
int min(int a, int b)
XXO("&Cut/Copy/Paste Toolbar")
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
const auto project
BoolSetting ScrollingPreference
Definition: ViewInfo.cpp:336
const TranslatableString LoopToggleText
Definition: ViewInfo.cpp:227
static ProjectFileIORegistry::AttributeWriterEntry entry
Definition: ViewInfo.cpp:330
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:90
This specialization of Setting for bool adds a Toggle method to negate the saved value.
Definition: Prefs.h:344
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:36
SelectedRegion mRegion
Definition: ViewInfo.h:88
double f1() const
Definition: ViewInfo.h:38
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:37
double t0() const
Definition: ViewInfo.h:35
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:128
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:135
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:180
void SetAllTimes(double start, double end)
Definition: ViewInfo.cpp:191
static constexpr auto invalidValue
Definition: ViewInfo.h:176
void Clear()
Set to an invalid state.
Definition: ViewInfo.cpp:197
double GetLastActiveStart() const
Definition: ViewInfo.h:142
double mEnd
Definition: ViewInfo.h:179
void SetEnd(double end)
Definition: ViewInfo.cpp:171
double GetLastActiveEnd() const
Definition: ViewInfo.h:149
void SetActive(bool active)
Definition: ViewInfo.cpp:146
double mLastActiveEnd
Definition: ViewInfo.h:181
double mStart
Definition: ViewInfo.h:178
void SetTimes(double start, double end)
Definition: ViewInfo.cpp:181
bool mActive
Definition: ViewInfo.h:183
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:205
Holds a msgid for the translation catalog; may also bind format arguments.
Base class for extra information attached to undo/redo states.
Definition: UndoManager.h:83
bool bUpdateTrackIndicator
Definition: ViewInfo.h:242
void WriteXMLAttributes(XMLWriter &xmlFile) const
Definition: ViewInfo.cpp:286
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:219
void SetBeforeScreenWidth(wxInt64 beforeWidth, wxInt64 screenWidth, double lowerBoundTime=0.0)
Definition: ViewInfo.cpp:278
ViewInfo(double start, double screenDuration, double pixelsPerSecond)
Definition: ViewInfo.cpp:245
double total
Definition: ViewInfo.h:224
static int UpdateScrollPrefsID()
Definition: ViewInfo.cpp:325
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
bool bScrollBeyondZero
Definition: ViewInfo.h:244
void UpdatePrefs() override
Definition: ViewInfo.cpp:267
void UpdateSelectedPrefs(int id) override
Definition: ViewInfo.cpp:260
bool bAdjustSelectionEdges
Definition: ViewInfo.h:245
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:25
static double GetDefaultZoom()
Definition: ZoomInfo.h:113
double zoom
Definition: ZoomInfo.h:55
double h
Definition: ZoomInfo.h:52
int vpos
Definition: ZoomInfo.h:50
virtual bool Read(const wxString &key, bool *value) const =0
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:208
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:196
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:770
void Notify(AudacityProject &project, ProjectSettings::EventCode code, long previousValue)
UndoRedoExtensionRegistry::Entry sEntry
Definition: ViewInfo.cpp:350
Typically statically constructed.
Definition: UndoManager.h:102
Construct once at static initialization time to hook project file IO.
Definition: ViewInfo.cpp:296
ProjectFileIORegistry::AttributeReaderEntries entries2
Definition: ViewInfo.cpp:306
ProjectFileIORegistry::AttributeReaderEntries entries
Definition: ViewInfo.cpp:298
Typically statically constructed.
void RestoreUndoRedoState(AudacityProject &project) override
Modify the project when undoing or redoing to some state in history.
Definition: ViewInfo.cpp:344