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

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

#include <UndoManager.h>

Inheritance diagram for UndoManager:

Public Types

using Consumer = std::function< void(const UndoState &) >
 

Public Member Functions

 UndoManager ()
 
 ~UndoManager ()
 
 UndoManager (const UndoManager &)=delete
 
UndoManageroperator= (const UndoManager &)=delete
 
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)
 
void SetStateTo (unsigned int n, const Consumer &consumer)
 
void Undo (const Consumer &consumer)
 
void Redo (const Consumer &consumer)
 
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 107 of file UndoManager.h.

Member Typedef Documentation

using UndoManager::Consumer = std::function< void( const UndoState & ) >

Definition at line 137 of file UndoManager.h.

Constructor & Destructor Documentation

UndoManager::UndoManager ( )

Definition at line 66 of file UndoManager.cpp.

References current, ResetODChangesFlag(), and saved.

67 {
68  current = -1;
69  saved = -1;
71 }
void ResetODChangesFlag()
UndoManager::~UndoManager ( )

Definition at line 73 of file UndoManager.cpp.

References ClearStates().

74 {
75  ClearStates();
76 }
void ClearStates()
UndoManager::UndoManager ( const UndoManager )
delete

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:171
static TrackList * GetClipboardTracks()
Definition: Project.cpp:4735
unsigned long long mClipboardSpaceUsage
Definition: UndoManager.h:172
std::unordered_set< ConstBlockFilePtr > Set
Definition: UndoManager.cpp:46
UndoStack stack
Definition: UndoManager.h:166
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:166
wxLongLong_t UndoManager::GetClipboardSpaceUsage ( ) const
inline

Definition at line 151 of file UndoManager.h.

Referenced by HistoryWindow::DoUpdate().

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

Definition at line 210 of file UndoManager.cpp.

References current.

Referenced by HistoryWindow::DoUpdate().

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:171
UndoStack stack
Definition: UndoManager.h:166
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:166
void UndoManager::GetShortDescription ( unsigned int  n,
wxString *  desc 
)

Definition at line 164 of file UndoManager.cpp.

References stack.

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:166
bool UndoManager::HasODChangesFlag ( )

Definition at line 403 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

Referenced by UnsavedChanges().

404 {
405  bool ret;
406  mODChangesMutex.Lock();
407  ret=mODChanges;
408  mODChangesMutex.Unlock();
409  return ret;
410 }
bool mODChanges
Definition: UndoManager.h:174
ODLock mODChangesMutex
Definition: UndoManager.h:175
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, safenew, 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 
253  // wxWidgets will own the event object
254  QueueEvent( safenew wxCommandEvent{ EVT_UNDO_MODIFIED } );
255 }
#define SonifyEndModifyState()
Definition: NoteTrack.h:296
#define safenew
Definition: Audacity.h:230
static std::shared_ptr< TrackList > Create()
Definition: Track.cpp:574
#define SonifyBeginModifyState()
Definition: NoteTrack.h:295
UndoStack stack
Definition: UndoManager.h:166
Definition: Track.h:170
UndoManager& UndoManager::operator= ( const UndoManager )
delete
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 257 of file UndoManager.cpp.

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

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

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

Definition at line 345 of file UndoManager.cpp.

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

346 {
347  wxASSERT(RedoAvailable());
348 
349  current++;
350 
351  /*
352  if (!RedoAvailable()) {
353  *sel0 = stack[current]->sel0;
354  *sel1 = stack[current]->sel1;
355  }
356  else {
357  current++;
358  *sel0 = stack[current]->sel0;
359  *sel1 = stack[current]->sel1;
360  current--;
361  }
362  */
363 
364  lastAction = wxT("");
365  mayConsolidate = false;
366 
367  consumer( stack[current]->state );
368 
369  // wxWidgets will own the event object
370  QueueEvent( safenew wxCommandEvent{ EVT_UNDO_RESET } );
371 }
#define safenew
Definition: Audacity.h:230
bool mayConsolidate
Definition: UndoManager.h:169
bool RedoAvailable()
wxString lastAction
Definition: UndoManager.h:168
UndoStack stack
Definition: UndoManager.h:166
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:166
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:166
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 412 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

Referenced by StateSaved(), and UndoManager().

413 {
414  mODChangesMutex.Lock();
415  mODChanges=false;
416  mODChangesMutex.Unlock();
417 }
bool mODChanges
Definition: UndoManager.h:174
ODLock mODChangesMutex
Definition: UndoManager.h:175
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:166
void UndoManager::SetODChangesFlag ( )

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

Definition at line 396 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

397 {
398  mODChangesMutex.Lock();
399  mODChanges=true;
400  mODChangesMutex.Unlock();
401 }
bool mODChanges
Definition: UndoManager.h:174
ODLock mODChangesMutex
Definition: UndoManager.h:175
void UndoManager::SetStateTo ( unsigned int  n,
const Consumer consumer 
)

Definition at line 313 of file UndoManager.cpp.

References current, lastAction, mayConsolidate, safenew, and stack.

Referenced by AudacityProject::SetStateTo().

314 {
315  n -= 1;
316 
317  wxASSERT(n < stack.size());
318 
319  current = n;
320 
321  lastAction = wxT("");
322  mayConsolidate = false;
323 
324  consumer( stack[current]->state );
325 
326  // wxWidgets will own the event object
327  QueueEvent( safenew wxCommandEvent{ EVT_UNDO_RESET } );
328 }
#define safenew
Definition: Audacity.h:230
bool mayConsolidate
Definition: UndoManager.h:169
wxString lastAction
Definition: UndoManager.h:168
UndoStack stack
Definition: UndoManager.h:166
void UndoManager::StateSaved ( )

Definition at line 378 of file UndoManager.cpp.

References current, ResetODChangesFlag(), and saved.

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

379 {
380  saved = current;
382 }
void ResetODChangesFlag()
void UndoManager::StopConsolidating ( )
inline

Definition at line 128 of file UndoManager.h.

128 { mayConsolidate = false; }
bool mayConsolidate
Definition: UndoManager.h:169
void UndoManager::Undo ( const Consumer consumer)

Definition at line 330 of file UndoManager.cpp.

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

331 {
332  wxASSERT(UndoAvailable());
333 
334  current--;
335 
336  lastAction = wxT("");
337  mayConsolidate = false;
338 
339  consumer( stack[current]->state );
340 
341  // wxWidgets will own the event object
342  QueueEvent( safenew wxCommandEvent{ EVT_UNDO_RESET } );
343 }
#define safenew
Definition: Audacity.h:230
bool mayConsolidate
Definition: UndoManager.h:169
wxString lastAction
Definition: UndoManager.h:168
UndoStack stack
Definition: UndoManager.h:166
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 373 of file UndoManager.cpp.

References current, HasODChangesFlag(), and saved.

Referenced by AudacityProject::OnCloseWindow().

374 {
375  return (saved != current) || HasODChangesFlag();
376 }
bool HasODChangesFlag()

Member Data Documentation

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

Definition at line 168 of file UndoManager.h.

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

bool UndoManager::mayConsolidate { false }
private

Definition at line 169 of file UndoManager.h.

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

unsigned long long UndoManager::mClipboardSpaceUsage {}
private

Definition at line 172 of file UndoManager.h.

Referenced by CalculateSpaceUsage().

bool UndoManager::mODChanges
private

Definition at line 174 of file UndoManager.h.

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

ODLock UndoManager::mODChangesMutex
private

Definition at line 175 of file UndoManager.h.

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

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

Definition at line 171 of file UndoManager.h.

Referenced by CalculateSpaceUsage(), and GetLongDescription().

UndoStack UndoManager::stack
private

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