Audacity  2.2.2
Public Member Functions | Private Attributes | List of all members
UndoManager Class Reference

Works with HistoryWindow to provide the Undo functionality. More...

#include <UndoManager.h>

Public Member Functions

 UndoManager ()
 
 ~UndoManager ()
 
void PushState (const TrackList *l, const SelectedRegion &selectedRegion, const std::shared_ptr< Tags > &tags, const wxString &longDescription, const wxString &shortDescription, UndoPush flags=UndoPush::AUTOSAVE)
 
void ModifyState (const TrackList *l, const SelectedRegion &selectedRegion, const std::shared_ptr< Tags > &tags)
 
void ClearStates ()
 
void RemoveStates (int num)
 
void RemoveStateAt (int n)
 
unsigned int GetNumStates ()
 
unsigned int GetCurrentState ()
 
void StopConsolidating ()
 
void GetShortDescription (unsigned int n, wxString *desc)
 
wxLongLong_t GetLongDescription (unsigned int n, wxString *desc, wxString *size)
 
void SetLongDescription (unsigned int n, const wxString &desc)
 
const UndoStateSetStateTo (unsigned int n, SelectedRegion *selectedRegion)
 
const UndoStateUndo (SelectedRegion *selectedRegion)
 
const UndoStateRedo (SelectedRegion *selectedRegion)
 
bool UndoAvailable ()
 
bool RedoAvailable ()
 
bool UnsavedChanges ()
 
void StateSaved ()
 
wxLongLong_t GetClipboardSpaceUsage () const
 
void CalculateSpaceUsage ()
 
void SetODChangesFlag ()
 to mark as unsaved changes without changing the state/tracks. More...
 
bool HasODChangesFlag ()
 
void ResetODChangesFlag ()
 

Private Attributes

int current
 
int saved
 
UndoStack stack
 
wxString lastAction
 
bool mayConsolidate { false }
 
SpaceArray space
 
unsigned long long mClipboardSpaceUsage {}
 
bool mODChanges
 
ODLock mODChangesMutex
 

Detailed Description

Works with HistoryWindow to provide the Undo functionality.

Definition at line 93 of file UndoManager.h.

Constructor & Destructor Documentation

UndoManager::UndoManager ( )

Definition at line 62 of file UndoManager.cpp.

References current, ResetODChangesFlag(), and saved.

63 {
64  current = -1;
65  saved = -1;
67 }
void ResetODChangesFlag()
UndoManager::~UndoManager ( )

Definition at line 69 of file UndoManager.cpp.

References ClearStates().

70 {
71  ClearStates();
72 }
void ClearStates()

Member Function Documentation

void UndoManager::CalculateSpaceUsage ( )

Definition at line 115 of file UndoManager.cpp.

References AudacityProject::GetClipboardTracks(), mClipboardSpaceUsage, space, and stack.

Referenced by HistoryWindow::DoUpdate().

116 {
117  space.clear();
118  space.resize(stack.size(), 0);
119 
120  Set seen;
121 
122  // After copies and pastes, a block file may be used in more than
123  // one place in one undo history state, and it may be used in more than
124  // one undo history state. It might even be used in two states, but not
125  // in another state that is between them -- as when you have state A,
126  // then make a cut to get state B, but then paste it back into state C.
127 
128  // So be sure to count each block file once only, in the last undo item that
129  // contains it.
130 
131  // Why the last and not the first? Because the user of the History dialog
132  // may DELETE undo states, oldest first. To reclaim disk space you must
133  // DELETE all states containing the block file. So the block file's
134  // contribution to space usage should be counted only in that latest state.
135 
136  for (size_t nn = stack.size(); nn--;)
137  {
138  // Scan all tracks at current level
139  auto tracks = stack[nn]->state.tracks.get();
140  space[nn] = CalculateUsage(tracks, &seen);
141  }
142 
143  mClipboardSpaceUsage = CalculateUsage
145 
146  //TIMER_STOP( space_calc );
147 }
SpaceArray space
Definition: UndoManager.h:151
static TrackList * GetClipboardTracks()
Definition: Project.cpp:4901
unsigned long long mClipboardSpaceUsage
Definition: UndoManager.h:152
std::unordered_set< ConstBlockFilePtr > Set
Definition: UndoManager.cpp:42
UndoStack stack
Definition: UndoManager.h:146
void UndoManager::ClearStates ( )

Definition at line 198 of file UndoManager.cpp.

References current, RemoveStates(), saved, and stack.

Referenced by AudacityProject::InitialState(), AudacityProject::OnCloseWindow(), AudacityProject::ResetProjectToEmpty(), and ~UndoManager().

199 {
200  RemoveStates(stack.size());
201  current = -1;
202  saved = -1;
203 }
void RemoveStates(int num)
UndoStack stack
Definition: UndoManager.h:146
wxLongLong_t UndoManager::GetClipboardSpaceUsage ( ) const
inline

Definition at line 131 of file UndoManager.h.

Referenced by HistoryWindow::DoUpdate().

132  { return mClipboardSpaceUsage; }
unsigned long long mClipboardSpaceUsage
Definition: UndoManager.h:152
unsigned int UndoManager::GetCurrentState ( )

Definition at line 210 of file UndoManager.cpp.

References current.

Referenced by HistoryWindow::DoUpdate(), and AudacityProject::ModifyUndoMenuItems().

211 {
212  return current + 1; // the array is 0 based, the abstraction is 1 based
213 }
wxLongLong_t UndoManager::GetLongDescription ( unsigned int  n,
wxString *  desc,
wxString *  size 
)

Definition at line 149 of file UndoManager.cpp.

References Internat::FormatSize(), space, and stack.

Referenced by HistoryWindow::DoUpdate().

151 {
152  n -= 1; // 1 based to zero based
153 
154  wxASSERT(n < stack.size());
155  wxASSERT(space.size() == stack.size());
156 
157  *desc = stack[n]->description;
158 
159  *size = Internat::FormatSize(space[n]);
160 
161  return space[n];
162 }
static wxString FormatSize(wxLongLong size)
Convert a number to a string while formatting it in bytes, KB, MB, GB.
Definition: Internat.cpp:191
const wxChar * desc
Definition: ExportPCM.cpp:58
SpaceArray space
Definition: UndoManager.h:151
UndoStack stack
Definition: UndoManager.h:146
unsigned int UndoManager::GetNumStates ( )

Definition at line 205 of file UndoManager.cpp.

References stack.

Referenced by HistoryWindow::DoUpdate().

206 {
207  return stack.size();
208 }
UndoStack stack
Definition: UndoManager.h:146
void UndoManager::GetShortDescription ( unsigned int  n,
wxString *  desc 
)

Definition at line 164 of file UndoManager.cpp.

References stack.

Referenced by AudacityProject::ModifyUndoMenuItems().

165 {
166  n -= 1; // 1 based to zero based
167 
168  wxASSERT(n < stack.size());
169 
170  *desc = stack[n]->shortDescription;
171 }
const wxChar * desc
Definition: ExportPCM.cpp:58
UndoStack stack
Definition: UndoManager.h:146
bool UndoManager::HasODChangesFlag ( )

Definition at line 395 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

Referenced by UnsavedChanges().

396 {
397  bool ret;
398  mODChangesMutex.Lock();
399  ret=mODChanges;
400  mODChangesMutex.Unlock();
401  return ret;
402 }
bool mODChanges
Definition: UndoManager.h:154
ODLock mODChangesMutex
Definition: UndoManager.h:155
void UndoManager::ModifyState ( const TrackList l,
const SelectedRegion selectedRegion,
const std::shared_ptr< Tags > &  tags 
)

Definition at line 225 of file UndoManager.cpp.

References TrackList::Create(), current, SonifyBeginModifyState, SonifyEndModifyState, and stack.

Referenced by AudacityProject::ModifyState(), and PushState().

228 {
229  if (current == wxNOT_FOUND) {
230  return;
231  }
232 
234  // Delete current -- not necessary, but let's reclaim space early
235  stack[current]->state.tracks.reset();
236 
237  // Duplicate
238  auto tracksCopy = TrackList::Create();
239  for (auto t : *l) {
240  if ( t->GetId() == TrackId{} )
241  // Don't copy a pending added track
242  continue;
243  tracksCopy->Add(t->Duplicate());
244  }
245 
246  // Replace
247  stack[current]->state.tracks = std::move(tracksCopy);
248  stack[current]->state.tags = tags;
249 
250  stack[current]->state.selectedRegion = selectedRegion;
252 }
#define SonifyEndModifyState()
Definition: NoteTrack.h:295
static std::shared_ptr< TrackList > Create()
Definition: Track.cpp:792
#define SonifyBeginModifyState()
Definition: NoteTrack.h:294
UndoStack stack
Definition: UndoManager.h:146
Definition: Track.h:81
void UndoManager::PushState ( const TrackList l,
const SelectedRegion selectedRegion,
const std::shared_ptr< Tags > &  tags,
const wxString &  longDescription,
const wxString &  shortDescription,
UndoPush  flags = UndoPush::AUTOSAVE 
)

Definition at line 254 of file UndoManager.cpp.

References CONSOLIDATE, TrackList::Create(), current, lastAction, mayConsolidate, MINIMAL, ModifyState(), RemoveStateAt(), saved, and stack.

Referenced by AudacityProject::InitialState(), and AudacityProject::PushState().

260 {
261  unsigned int i;
262 
263  if ( ((flags & UndoPush::CONSOLIDATE) != UndoPush::MINIMAL) &&
264  lastAction == longDescription &&
265  mayConsolidate ) {
266  ModifyState(l, selectedRegion, tags);
267  // MB: If the "saved" state was modified by ModifyState, reset
268  // it so that UnsavedChanges returns true.
269  if (current == saved) {
270  saved = -1;
271  }
272  return;
273  }
274 
275  auto tracksCopy = TrackList::Create();
276  for (auto t : *l) {
277  if ( t->GetId() == TrackId{} )
278  // Don't copy a pending added track
279  continue;
280  tracksCopy->Add(t->Duplicate());
281  }
282 
283  mayConsolidate = true;
284 
285  i = current + 1;
286  while (i < stack.size()) {
287  RemoveStateAt(i);
288  }
289 
290  // Assume tags was duplicted before any changes.
291  // Just save a NEW shared_ptr to it.
292  stack.push_back(
293  std::make_unique<UndoStackElem>
294  (std::move(tracksCopy),
295  longDescription, shortDescription, selectedRegion, tags)
296  );
297 
298  current++;
299 
300  if (saved >= current) {
301  saved = -1;
302  }
303 
304  lastAction = longDescription;
305 }
void RemoveStateAt(int n)
static std::shared_ptr< TrackList > Create()
Definition: Track.cpp:792
bool mayConsolidate
Definition: UndoManager.h:149
wxString lastAction
Definition: UndoManager.h:148
void ModifyState(const TrackList *l, const SelectedRegion &selectedRegion, const std::shared_ptr< Tags > &tags)
UndoStack stack
Definition: UndoManager.h:146
Definition: Track.h:81
const UndoState & UndoManager::Redo ( SelectedRegion selectedRegion)

Definition at line 338 of file UndoManager.cpp.

References current, lastAction, mayConsolidate, RedoAvailable(), and stack.

Referenced by AudacityProject::OnRedo().

339 {
340  wxASSERT(RedoAvailable());
341 
342  current++;
343 
344  *selectedRegion = stack[current]->state.selectedRegion;
345 
346  /*
347  if (!RedoAvailable()) {
348  *sel0 = stack[current]->sel0;
349  *sel1 = stack[current]->sel1;
350  }
351  else {
352  current++;
353  *sel0 = stack[current]->sel0;
354  *sel1 = stack[current]->sel1;
355  current--;
356  }
357  */
358 
359  lastAction = wxT("");
360  mayConsolidate = false;
361 
362  return stack[current]->state;
363 }
bool mayConsolidate
Definition: UndoManager.h:149
bool RedoAvailable()
wxString lastAction
Definition: UndoManager.h:148
UndoStack stack
Definition: UndoManager.h:146
bool UndoManager::RedoAvailable ( )

Definition at line 220 of file UndoManager.cpp.

References current, and stack.

Referenced by Redo(), and AudacityProject::RedoAvailable().

221 {
222  return (current < (int)stack.size() - 1);
223 }
UndoStack stack
Definition: UndoManager.h:146
void UndoManager::RemoveStateAt ( int  n)

Definition at line 182 of file UndoManager.cpp.

References stack.

Referenced by PushState(), and RemoveStates().

183 {
184  stack.erase(stack.begin() + n);
185 }
UndoStack stack
Definition: UndoManager.h:146
void UndoManager::RemoveStates ( int  num)

Definition at line 188 of file UndoManager.cpp.

References current, RemoveStateAt(), and saved.

Referenced by ClearStates(), and HistoryWindow::OnDiscard().

189 {
190  for (int i = 0; i < num; i++) {
191  RemoveStateAt(0);
192 
193  current -= 1;
194  saved -= 1;
195  }
196 }
void RemoveStateAt(int n)
void UndoManager::ResetODChangesFlag ( )

Definition at line 404 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

Referenced by StateSaved(), and UndoManager().

405 {
406  mODChangesMutex.Lock();
407  mODChanges=false;
408  mODChangesMutex.Unlock();
409 }
bool mODChanges
Definition: UndoManager.h:154
ODLock mODChangesMutex
Definition: UndoManager.h:155
void UndoManager::SetLongDescription ( unsigned int  n,
const wxString &  desc 
)

Definition at line 173 of file UndoManager.cpp.

References desc, and stack.

174 {
175  n -= 1;
176 
177  wxASSERT(n < stack.size());
178 
179  stack[n]->description = desc;
180 }
const wxChar * desc
Definition: ExportPCM.cpp:58
UndoStack stack
Definition: UndoManager.h:146
void UndoManager::SetODChangesFlag ( )

to mark as unsaved changes without changing the state/tracks.

Definition at line 388 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

389 {
390  mODChangesMutex.Lock();
391  mODChanges=true;
392  mODChangesMutex.Unlock();
393 }
bool mODChanges
Definition: UndoManager.h:154
ODLock mODChangesMutex
Definition: UndoManager.h:155
const UndoState & UndoManager::SetStateTo ( unsigned int  n,
SelectedRegion selectedRegion 
)

Definition at line 308 of file UndoManager.cpp.

Referenced by AudacityProject::SetStateTo().

309 {
310  n -= 1;
311 
312  wxASSERT(n < stack.size());
313 
314  current = n;
315 
316  *selectedRegion = stack[current]->state.selectedRegion;
317 
318  lastAction = wxT("");
319  mayConsolidate = false;
320 
321  return stack[current]->state;
322 }
bool mayConsolidate
Definition: UndoManager.h:149
wxString lastAction
Definition: UndoManager.h:148
UndoStack stack
Definition: UndoManager.h:146
void UndoManager::StateSaved ( )

Definition at line 370 of file UndoManager.cpp.

References current, ResetODChangesFlag(), and saved.

Referenced by AudacityProject::DoSave(), and AudacityProject::InitialState().

371 {
372  saved = current;
374 }
void ResetODChangesFlag()
void UndoManager::StopConsolidating ( )
inline

Definition at line 111 of file UndoManager.h.

Referenced by AudacityProject::DoClipLeftOrRight(), and AudacityProject::OnResample().

111 { mayConsolidate = false; }
bool mayConsolidate
Definition: UndoManager.h:149
const UndoState & UndoManager::Undo ( SelectedRegion selectedRegion)

Definition at line 324 of file UndoManager.cpp.

References current, lastAction, mayConsolidate, stack, and UndoAvailable().

Referenced by AudacityProject::OnUndo().

325 {
326  wxASSERT(UndoAvailable());
327 
328  current--;
329 
330  *selectedRegion = stack[current]->state.selectedRegion;
331 
332  lastAction = wxT("");
333  mayConsolidate = false;
334 
335  return stack[current]->state;
336 }
bool mayConsolidate
Definition: UndoManager.h:149
wxString lastAction
Definition: UndoManager.h:148
UndoStack stack
Definition: UndoManager.h:146
bool UndoAvailable()
bool UndoManager::UndoAvailable ( )

Definition at line 215 of file UndoManager.cpp.

References current.

Referenced by Undo(), and AudacityProject::UndoAvailable().

216 {
217  return (current > 0);
218 }
bool UndoManager::UnsavedChanges ( )

Definition at line 365 of file UndoManager.cpp.

References current, HasODChangesFlag(), and saved.

Referenced by AudacityProject::DoSave(), and AudacityProject::OnCloseWindow().

366 {
367  return (saved != current) || HasODChangesFlag();
368 }
bool HasODChangesFlag()

Member Data Documentation

int UndoManager::current
private
wxString UndoManager::lastAction
private

Definition at line 148 of file UndoManager.h.

Referenced by PushState(), Redo(), and Undo().

bool UndoManager::mayConsolidate { false }
private

Definition at line 149 of file UndoManager.h.

Referenced by PushState(), Redo(), and Undo().

unsigned long long UndoManager::mClipboardSpaceUsage {}
private

Definition at line 152 of file UndoManager.h.

Referenced by CalculateSpaceUsage().

bool UndoManager::mODChanges
private

Definition at line 154 of file UndoManager.h.

Referenced by HasODChangesFlag(), ResetODChangesFlag(), and SetODChangesFlag().

ODLock UndoManager::mODChangesMutex
private

Definition at line 155 of file UndoManager.h.

Referenced by HasODChangesFlag(), ResetODChangesFlag(), and SetODChangesFlag().

int UndoManager::saved
private
SpaceArray UndoManager::space
private

Definition at line 151 of file UndoManager.h.

Referenced by CalculateSpaceUsage(), and GetLongDescription().

UndoStack UndoManager::stack
private

The documentation for this class was generated from the following files: