Audacity 3.2.0
ProjectHistory.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5ProjectHistory.cpp
6
7Paul Licameli split from ProjectManager.cpp
8
9**********************************************************************/
10
11#include "ProjectHistory.h"
12
13#include "Project.h"
14#include "Track.h"
15#include "UndoManager.h"
16#include "ViewInfo.h"
17
19 []( AudacityProject &project ) {
20 return std::make_shared< ProjectHistory >( project );
21 }
22};
23
25{
26 return project.AttachedObjects::Get< ProjectHistory >( sProjectHistoryKey );
27}
28
30{
31 return Get( const_cast< AudacityProject & >( project ) );
32}
33
35
36//
37// Undo/History methods
38//
39
41{
42 auto &project = mProject;
43 auto &tracks = TrackList::Get( project );
44 auto &viewInfo = ViewInfo::Get( project );
45 auto &undoManager = UndoManager::Get( project );
46
47 undoManager.ClearStates();
48
49 undoManager.PushState(
50 tracks, viewInfo.selectedRegion,
51 XO("Created new project"), {});
52
53 undoManager.StateSaved();
54}
55
57{
58 auto &project = mProject;
59 auto &tracks = TrackList::Get( project );
60 auto &undoManager = UndoManager::Get( project );
61 return undoManager.UndoAvailable() &&
62 !tracks.HasPendingTracks();
63}
64
66{
67 auto &project = mProject;
68 auto &tracks = TrackList::Get( project );
69 auto &undoManager = UndoManager::Get( project );
70 return undoManager.RedoAvailable() &&
71 !tracks.HasPendingTracks();
72}
73
75 const TranslatableString &desc, const TranslatableString &shortDesc)
76{
77 PushState(desc, shortDesc, UndoPush::NONE);
78}
79
81 const TranslatableString &shortDesc,
82 UndoPush flags )
83{
84 auto &project = mProject;
86 AutoSave::Call(project);
87
88 // remaining no-fail operations "commit" the changes of undo manager state
89 auto &tracks = TrackList::Get( project );
90 auto &viewInfo = ViewInfo::Get( project );
91 auto &undoManager = UndoManager::Get( project );
92 undoManager.PushState(
93 tracks, viewInfo.selectedRegion,
94 desc, shortDesc, flags);
95
96 mDirty = true;
97}
98
100{
101 auto &project = mProject;
102 auto &undoManager = UndoManager::Get( project );
103 SetStateTo( undoManager.GetCurrentState(), false );
104}
105
106void ProjectHistory::ModifyState(bool bWantsAutoSave)
107{
108 auto &project = mProject;
109 if (bWantsAutoSave)
110 AutoSave::Call(project);
111
112 // remaining no-fail operations "commit" the changes of undo manager state
113 auto &tracks = TrackList::Get( project );
114 auto &viewInfo = ViewInfo::Get( project );
115 auto &undoManager = UndoManager::Get( project );
116 undoManager.ModifyState(tracks, viewInfo.selectedRegion);
117}
118
119// LL: Is there a memory leak here as "l" and "t" are not deleted???
120// Vaughan, 2010-08-29: No, as "l" is a TrackList* of an Undo stack state.
121// Need to keep it and its tracks "t" available for Undo/Redo/SetStateTo.
122void ProjectHistory::PopState(const UndoState &state, bool doAutosave)
123{
124 auto &project = mProject;
125 if (doAutosave)
126 AutoSave::Call(project);
127
128 // remaining no-fail operations "commit" the changes of undo manager state
129 TrackList *const tracks = state.tracks.get();
130 wxASSERT(tracks);
131 auto &dstTracks = TrackList::Get( project );
132 auto &viewInfo = ViewInfo::Get( project );
133
134 viewInfo.selectedRegion = state.selectedRegion;
135
136 // Restore extra state
137 for (auto &pExtension : state.extensions)
138 if (pExtension)
139 pExtension->RestoreUndoRedoState(project);
140
141 dstTracks.Clear();
142 for (auto t : tracks->Any())
143 dstTracks.Add(t->Duplicate());
144}
145
146void ProjectHistory::SetStateTo(unsigned int n, bool doAutosave)
147{
148 auto &project = mProject;
149 auto &undoManager = UndoManager::Get( project );
150
151 undoManager.SetStateTo(n,
152 [this, doAutosave]( const UndoStackElem &elem ){
153 PopState(elem.state, doAutosave); } );
154}
const TranslatableString desc
Definition: ExportPCM.cpp:58
#define XO(s)
Definition: Internat.h:31
static AudacityProject::AttachedObjects::RegisteredFactory sProjectHistoryKey
declares abstract base class Track, TrackList, and iterators over TrackList
UndoPush
Definition: UndoManager.h:150
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:266
static result_type Call(Arguments &&...arguments)
Null check of the installed function is done for you.
bool UndoAvailable() const
~ProjectHistory() override
void SetStateTo(unsigned int n, bool doAutosave=true)
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
void PopState(const UndoState &state, bool doAutosave=true)
bool RedoAvailable() const
void ModifyState(bool bWantsAutoSave)
static ProjectHistory & Get(AudacityProject &project)
AudacityProject & mProject
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:1338
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1437
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:486
Holds a msgid for the translation catalog; may also bind format arguments.
static UndoManager & Get(AudacityProject &project)
Definition: UndoManager.cpp:67
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
Holds one item with description and time range for the UndoManager.
Definition: UndoManager.h:127
UndoState state
Definition: UndoManager.h:140
std::shared_ptr< TrackList > tracks
Definition: UndoManager.h:123
Extensions extensions
Definition: UndoManager.h:122
SelectedRegion selectedRegion
Definition: UndoManager.h:124