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, 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 pixelsPerSecond)
246 : ZoomInfo(start, pixelsPerSecond)
247 , selectedRegion()
248 , bUpdateTrackIndicator(true)
249{
250 UpdatePrefs();
251}
252
254{
255 if (id == UpdateScrollPrefsID())
256 gPrefs->Read(wxT("/GUI/AutoScroll"), &bUpdateTrackIndicator,
257 true);
258}
259
261{
263}
264
266// may throw
267{
268 selectedRegion.WriteXMLAttributes(xmlFile, "sel0", "sel1");
269 xmlFile.WriteAttr(wxT("vpos"), vpos);
270 xmlFile.WriteAttr(wxT("h"), hpos, 10);
271 xmlFile.WriteAttr(wxT("zoom"), zoom, 10);
272}
273
276
279{
281},
283};
284
286// Just a pointer to function, but needing overload resolution as non-const:
288{
289 { "vpos", [](auto &viewInfo, auto value){
290 viewInfo.vpos = value.Get(viewInfo.vpos);
291 // Note that (other than in import of old .aup files) there is no other
292 // reassignment of vpos, except in handling the vertical scroll.
293 } },
294 { "h", [](auto &viewInfo, auto value){
295 viewInfo.hpos = value.Get(viewInfo.hpos);
296 } },
297 { "zoom", [](auto &viewInfo, auto value){
298 viewInfo.zoom = value.Get(viewInfo.zoom);
299 } },
300} };
301
303
305{
306 return 10000;
307}
308
310[](const AudacityProject &project, XMLWriter &xmlFile){
312}
313};
314
315// Undo/redo handling of selection changes
316namespace {
319 : mSelectedRegion{ ViewInfo::Get(project).selectedRegion }
320 {}
322 ViewInfo::Get(project).selectedRegion = mSelectedRegion;
323 }
325};
326
328 [](AudacityProject &project) -> std::shared_ptr<UndoStateExtension> {
329 return std::make_shared<SelectedRegionRestorer>(project);
330 }
331};
332}
wxT("CloseDown"))
Toolkit-neutral facade for basic user interface services.
XXO("&Cut/Copy/Paste Toolbar")
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
const auto project
const TranslatableString LoopToggleText
Definition: ViewInfo.cpp:227
static ProjectFileIORegistry::AttributeWriterEntry entry
Definition: ViewInfo.cpp:309
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
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:275
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
double f0() const
static XMLMethodRegistryBase::Mutators< SelectedRegion > Mutators(const char *legacyT0Name, const char *legacyT1Name)
bool setTimes(double t0, double t1)
bool setF0(double f, bool maySwap=true)
bool setF1(double f, bool maySwap=true)
double t0() const
bool setFrequencies(double f0, double f1)
bool setT0(double t, bool maySwap=true)
double f1() const
bool setT1(double t, bool maySwap=true)
void move(double delta)
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
int vpos
Pixel distance from top of tracks to top of visible scrolled area.
Definition: ViewInfo.h:221
ViewInfo(double start, double pixelsPerSecond)
Definition: ViewInfo.cpp:245
bool bUpdateTrackIndicator
Definition: ViewInfo.h:226
void WriteXMLAttributes(XMLWriter &xmlFile) const
Definition: ViewInfo.cpp:265
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:215
static int UpdateScrollPrefsID()
Definition: ViewInfo.cpp:304
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
void UpdatePrefs() override
Definition: ViewInfo.cpp:260
void UpdateSelectedPrefs(int id) override
Definition: ViewInfo.cpp:253
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:116
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
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:628
UndoRedoExtensionRegistry::Entry sEntry
Definition: ViewInfo.cpp:327
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
Typically statically constructed.
Definition: UndoManager.h:102
Construct once at static initialization time to hook project file IO.
Definition: ViewInfo.cpp:275
ProjectFileIORegistry::AttributeReaderEntries entries2
Definition: ViewInfo.cpp:285
ProjectFileIORegistry::AttributeReaderEntries entries
Definition: ViewInfo.cpp:277
Typically statically constructed.
void RestoreUndoRedoState(AudacityProject &project) override
Modify the project when undoing or redoing to some state in history.
Definition: ViewInfo.cpp:321