Audacity  3.0.3
ProjectHistory.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 ProjectHistory.cpp
6 
7 Paul Licameli split from ProjectManager.cpp
8 
9 **********************************************************************/
10 
11 #include "ProjectHistory.h"
12 
13 #include "Project.h"
14 #include "ProjectFileIO.h"
15 #include "Tags.h"
16 #include "Track.h"
17 #include "UndoManager.h"
18 #include "ViewInfo.h"
19 
21  []( AudacityProject &project ) {
22  return std::make_shared< ProjectHistory >( project );
23  }
24 };
25 
27 {
28  return project.AttachedObjects::Get< ProjectHistory >( sProjectHistoryKey );
29 }
30 
32 {
33  return Get( const_cast< AudacityProject & >( project ) );
34 }
35 
37 
38 //
39 // Undo/History methods
40 //
41 
43 {
44  auto &project = mProject;
45  auto &tracks = TrackList::Get( project );
46  auto &viewInfo = ViewInfo::Get( project );
47  auto &undoManager = UndoManager::Get( project );
48  auto &tags = Tags::Get( project );
49 
50  undoManager.ClearStates();
51 
52  undoManager.PushState(
53  &tracks, viewInfo.selectedRegion, tags.shared_from_this(),
54  XO("Created new project"), {});
55 
56  undoManager.StateSaved();
57 }
58 
60 {
61  auto &project = mProject;
62  auto &tracks = TrackList::Get( project );
63  auto &undoManager = UndoManager::Get( project );
64  return undoManager.UndoAvailable() &&
65  !tracks.HasPendingTracks();
66 }
67 
69 {
70  auto &project = mProject;
71  auto &tracks = TrackList::Get( project );
72  auto &undoManager = UndoManager::Get( project );
73  return undoManager.RedoAvailable() &&
74  !tracks.HasPendingTracks();
75 }
76 
77 namespace {
78  void AutoSaveOrThrow( ProjectFileIO &projectFileIO )
79  {
80  if ( !projectFileIO.AutoSave() )
83  XO("Automatic database backup failed."),
84  XO("Warning"),
85  "Error:_Disk_full_or_not_writable"
86  };
87  }
88 }
89 
91  const TranslatableString &desc, const TranslatableString &shortDesc)
92 {
93  PushState(desc, shortDesc, UndoPush::NONE);
94 }
95 
97  const TranslatableString &shortDesc,
98  UndoPush flags )
99 {
100  auto &project = mProject;
101  auto &projectFileIO = ProjectFileIO::Get( project );
102  if((flags & UndoPush::NOAUTOSAVE) == UndoPush::NONE)
103  AutoSaveOrThrow( projectFileIO );
104 
105  // remaining no-fail operations "commit" the changes of undo manager state
106  auto &tracks = TrackList::Get( project );
107  auto &viewInfo = ViewInfo::Get( project );
108  auto &undoManager = UndoManager::Get( project );
109  auto &tags = Tags::Get( project );
110  undoManager.PushState(
111  &tracks, viewInfo.selectedRegion, tags.shared_from_this(),
112  desc, shortDesc, flags);
113 
114  mDirty = true;
115 }
116 
118 {
119  auto &project = mProject;
120  auto &undoManager = UndoManager::Get( project );
121  SetStateTo( undoManager.GetCurrentState(), false );
122 }
123 
124 void ProjectHistory::ModifyState(bool bWantsAutoSave)
125 {
126  auto &project = mProject;
127  auto &projectFileIO = ProjectFileIO::Get( project );
128  if (bWantsAutoSave)
129  AutoSaveOrThrow( projectFileIO );
130 
131  // remaining no-fail operations "commit" the changes of undo manager state
132  auto &tracks = TrackList::Get( project );
133  auto &viewInfo = ViewInfo::Get( project );
134  auto &undoManager = UndoManager::Get( project );
135  auto &tags = Tags::Get( project );
136  undoManager.ModifyState(
137  &tracks, viewInfo.selectedRegion, tags.shared_from_this());
138 }
139 
140 // LL: Is there a memory leak here as "l" and "t" are not deleted???
141 // Vaughan, 2010-08-29: No, as "l" is a TrackList* of an Undo stack state.
142 // Need to keep it and its tracks "t" available for Undo/Redo/SetStateTo.
143 void ProjectHistory::PopState(const UndoState &state, bool doAutosave)
144 {
145  auto &project = mProject;
146  auto &projectFileIO = ProjectFileIO::Get( project );
147  if (doAutosave)
148  AutoSaveOrThrow( projectFileIO );
149 
150  // remaining no-fail operations "commit" the changes of undo manager state
151  auto &dstTracks = TrackList::Get( project );
152  auto &viewInfo = ViewInfo::Get( project );
153 
154  viewInfo.selectedRegion = state.selectedRegion;
155 
156  // Restore tags
157  Tags::Set( project, state.tags );
158 
159  TrackList *const tracks = state.tracks.get();
160 
161  dstTracks.Clear();
162 
163  for (auto t : tracks->Any())
164  {
165  dstTracks.Add(t->Duplicate());
166  }
167 
168 }
169 
170 void ProjectHistory::SetStateTo(unsigned int n, bool doAutosave)
171 {
172  auto &project = mProject;
173  auto &undoManager = UndoManager::Get( project );
174 
175  undoManager.SetStateTo(n,
176  [this, doAutosave]( const UndoStackElem &elem ){
177  PopState(elem.state, doAutosave); } );
178 }
ProjectHistory::ModifyState
void ModifyState(bool bWantsAutoSave)
Definition: ProjectHistory.cpp:124
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:161
ProjectFileIO.h
ProjectHistory::mProject
AudacityProject & mProject
Definition: ProjectHistory.h:55
UndoState::tracks
std::shared_ptr< TrackList > tracks
Definition: UndoManager.h:92
Project.h
ProjectHistory::mDirty
bool mDirty
Definition: ProjectHistory.h:57
TrackList
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:1280
UndoPush::NONE
@ NONE
XO
#define XO(s)
Definition: Internat.h:31
ProjectFileIO::Get
static ProjectFileIO & Get(AudacityProject &project)
Definition: ProjectFileIO.cpp:265
ProjectHistory
Definition: ProjectHistory.h:22
sProjectHistoryKey
static AudacityProject::AttachedObjects::RegisteredFactory sProjectHistoryKey
Definition: ProjectHistory.cpp:20
ProjectHistory::UndoAvailable
bool UndoAvailable() const
Definition: ProjectHistory.cpp:59
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
desc
const TranslatableString desc
Definition: ExportPCM.cpp:58
Tags.h
Tags::Get
static Tags & Get(AudacityProject &project)
Definition: Tags.cpp:237
UndoPush::NOAUTOSAVE
@ NOAUTOSAVE
ProjectHistory::RedoAvailable
bool RedoAvailable() const
Definition: ProjectHistory.cpp:68
ProjectHistory::PopState
void PopState(const UndoState &state, bool doAutosave=true)
Definition: ProjectHistory.cpp:143
UndoStackElem::state
UndoState state
Definition: UndoManager.h:110
UndoManager.h
UndoPush
UndoPush
Definition: UndoManager.h:122
ProjectHistory::InitialState
void InitialState()
Definition: ProjectHistory.cpp:42
ViewInfo.h
Tags::Set
static Tags & Set(AudacityProject &project, const std::shared_ptr< Tags > &tags)
Definition: Tags.cpp:247
anonymous_namespace{ProjectHistory.cpp}::AutoSaveOrThrow
void AutoSaveOrThrow(ProjectFileIO &projectFileIO)
Definition: ProjectHistory.cpp:78
ProjectHistory::~ProjectHistory
~ProjectHistory() override
ProjectHistory::PushState
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
Definition: ProjectHistory.cpp:90
TrackList::Get
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:506
ProjectHistory::RollbackState
void RollbackState()
Definition: ProjectHistory.cpp:117
UndoManager::Get
static UndoManager & Get(AudacityProject &project)
Definition: UndoManager.cpp:57
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:92
ProjectFileIO::AutoSave
bool AutoSave(bool recording=false)
Definition: ProjectFileIO.cpp:1688
ProjectHistory.h
ExceptionType::Internal
@ Internal
Indicates internal failure from Audacity.
UndoState::tags
std::shared_ptr< Tags > tags
Definition: UndoManager.h:93
Track.h
declares abstract base class Track, TrackList, and iterators over TrackList
UndoState
Definition: UndoManager.h:85
TrackList::Any
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1371
UndoState::selectedRegion
SelectedRegion selectedRegion
Definition: UndoManager.h:94
SimpleMessageBoxException
A MessageBoxException that shows a given, unvarying string.
Definition: AudacityException.h:95
ProjectHistory::Get
static ProjectHistory & Get(AudacityProject &project)
Definition: ProjectHistory.cpp:26
UndoStackElem
Holds one item with description and time range for the UndoManager.
Definition: UndoManager.h:97
ProjectHistory::SetStateTo
void SetStateTo(unsigned int n, bool doAutosave=true)
Definition: ProjectHistory.cpp:170
ProjectFileIO
Object associated with a project that manages reading and writing of Audacity project file formats,...
Definition: ProjectFileIO.h:62