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:4889
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 RemoveStates(), and stack.

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

199 {
200  RemoveStates(stack.size());
201 }
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 208 of file UndoManager.cpp.

References current.

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

209 {
210  return current + 1; // the array is 0 based, the abstraction is 1 based
211 }
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 203 of file UndoManager.cpp.

References stack.

Referenced by HistoryWindow::DoUpdate().

204 {
205  return stack.size();
206 }
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 398 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

Referenced by UnsavedChanges().

399 {
400  bool ret;
401  mODChangesMutex.Lock();
402  ret=mODChanges;
403  mODChangesMutex.Unlock();
404  return ret;
405 }
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 223 of file UndoManager.cpp.

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

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

226 {
227  if (current == wxNOT_FOUND) {
228  return;
229  }
230 
232  // Delete current -- not necessary, but let's reclaim space early
233  stack[current]->state.tracks.reset();
234 
235  // Duplicate
236  auto tracksCopy = TrackList::Create();
237  for (auto t : *l) {
238  if ( t->GetId() == TrackId{} )
239  // Don't copy a pending added track
240  continue;
241  tracksCopy->Add(t->Duplicate());
242  }
243 
244  // Replace
245  stack[current]->state.tracks = std::move(tracksCopy);
246  stack[current]->state.tags = tags;
247 
248  stack[current]->state.selectedRegion = selectedRegion;
250 }
#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 252 of file UndoManager.cpp.

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

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

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

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

Referenced by AudacityProject::OnRedo().

342 {
343  wxASSERT(RedoAvailable());
344 
345  current++;
346 
347  *selectedRegion = stack[current]->state.selectedRegion;
348 
349  /*
350  if (!RedoAvailable()) {
351  *sel0 = stack[current]->sel0;
352  *sel1 = stack[current]->sel1;
353  }
354  else {
355  current++;
356  *sel0 = stack[current]->sel0;
357  *sel1 = stack[current]->sel1;
358  current--;
359  }
360  */
361 
362  lastAction = wxT("");
363  mayConsolidate = false;
364 
365  return stack[current]->state;
366 }
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 218 of file UndoManager.cpp.

References current, and stack.

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

219 {
220  return (current < (int)stack.size() - 1);
221 }
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 407 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

Referenced by StateSaved(), and UndoManager().

408 {
409  mODChangesMutex.Lock();
410  mODChanges=false;
411  mODChangesMutex.Unlock();
412 }
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 391 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

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

Definition at line 306 of file UndoManager.cpp.

Referenced by AudacityProject::SetStateTo().

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

Definition at line 373 of file UndoManager.cpp.

References current, ResetODChangesFlag(), and saved.

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

374 {
375  saved = current;
377 }
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 327 of file UndoManager.cpp.

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

Referenced by AudacityProject::OnUndo().

328 {
329  wxASSERT(UndoAvailable());
330 
331  current--;
332 
333  *selectedRegion = stack[current]->state.selectedRegion;
334 
335  lastAction = wxT("");
336  mayConsolidate = false;
337 
338  return stack[current]->state;
339 }
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 213 of file UndoManager.cpp.

References current.

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

214 {
215  return (current > 0);
216 }
bool UndoManager::UnsavedChanges ( )

Definition at line 368 of file UndoManager.cpp.

References current, HasODChangesFlag(), and saved.

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

369 {
370  return (saved != current) || HasODChangesFlag();
371 }
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

Definition at line 145 of file UndoManager.h.

Referenced by PushState(), RemoveStates(), StateSaved(), UndoManager(), and UnsavedChanges().

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: