Audacity  2.3.1
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 ()
 
 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)
 
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()
UndoManager::UndoManager ( const UndoManager )
delete

Member Function Documentation

void UndoManager::CalculateSpaceUsage ( )

Definition at line 111 of file UndoManager.cpp.

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

Referenced by HistoryWindow::DoUpdate().

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

Definition at line 194 of file UndoManager.cpp.

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

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

195 {
196  RemoveStates(stack.size());
197  current = -1;
198  saved = -1;
199 }
void RemoveStates(int num)
UndoStack stack
Definition: UndoManager.h:149
wxLongLong_t UndoManager::GetClipboardSpaceUsage ( ) const
inline

Definition at line 134 of file UndoManager.h.

Referenced by HistoryWindow::DoUpdate().

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

Definition at line 206 of file UndoManager.cpp.

References current.

Referenced by HistoryWindow::DoUpdate().

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

Definition at line 145 of file UndoManager.cpp.

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

Referenced by HistoryWindow::DoUpdate().

147 {
148  n -= 1; // 1 based to zero based
149 
150  wxASSERT(n < stack.size());
151  wxASSERT(space.size() == stack.size());
152 
153  *desc = stack[n]->description;
154 
155  *size = Internat::FormatSize(space[n]);
156 
157  return space[n];
158 }
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:154
UndoStack stack
Definition: UndoManager.h:149
unsigned int UndoManager::GetNumStates ( )

Definition at line 201 of file UndoManager.cpp.

References stack.

Referenced by HistoryWindow::DoUpdate().

202 {
203  return stack.size();
204 }
UndoStack stack
Definition: UndoManager.h:149
void UndoManager::GetShortDescription ( unsigned int  n,
wxString *  desc 
)

Definition at line 160 of file UndoManager.cpp.

References stack.

161 {
162  n -= 1; // 1 based to zero based
163 
164  wxASSERT(n < stack.size());
165 
166  *desc = stack[n]->shortDescription;
167 }
const wxChar * desc
Definition: ExportPCM.cpp:58
UndoStack stack
Definition: UndoManager.h:149
bool UndoManager::HasODChangesFlag ( )

Definition at line 391 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

Referenced by UnsavedChanges().

392 {
393  bool ret;
394  mODChangesMutex.Lock();
395  ret=mODChanges;
396  mODChangesMutex.Unlock();
397  return ret;
398 }
bool mODChanges
Definition: UndoManager.h:157
ODLock mODChangesMutex
Definition: UndoManager.h:158
void UndoManager::ModifyState ( const TrackList l,
const SelectedRegion selectedRegion,
const std::shared_ptr< Tags > &  tags 
)

Definition at line 221 of file UndoManager.cpp.

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

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

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

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

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

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

Definition at line 334 of file UndoManager.cpp.

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

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

Definition at line 216 of file UndoManager.cpp.

References current, and stack.

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

217 {
218  return (current < (int)stack.size() - 1);
219 }
UndoStack stack
Definition: UndoManager.h:149
void UndoManager::RemoveStateAt ( int  n)

Definition at line 178 of file UndoManager.cpp.

References stack.

Referenced by PushState(), and RemoveStates().

179 {
180  stack.erase(stack.begin() + n);
181 }
UndoStack stack
Definition: UndoManager.h:149
void UndoManager::RemoveStates ( int  num)

Definition at line 184 of file UndoManager.cpp.

References current, RemoveStateAt(), and saved.

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

185 {
186  for (int i = 0; i < num; i++) {
187  RemoveStateAt(0);
188 
189  current -= 1;
190  saved -= 1;
191  }
192 }
void RemoveStateAt(int n)
void UndoManager::ResetODChangesFlag ( )

Definition at line 400 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

Referenced by StateSaved(), and UndoManager().

401 {
402  mODChangesMutex.Lock();
403  mODChanges=false;
404  mODChangesMutex.Unlock();
405 }
bool mODChanges
Definition: UndoManager.h:157
ODLock mODChangesMutex
Definition: UndoManager.h:158
void UndoManager::SetLongDescription ( unsigned int  n,
const wxString &  desc 
)

Definition at line 169 of file UndoManager.cpp.

References desc, and stack.

170 {
171  n -= 1;
172 
173  wxASSERT(n < stack.size());
174 
175  stack[n]->description = desc;
176 }
const wxChar * desc
Definition: ExportPCM.cpp:58
UndoStack stack
Definition: UndoManager.h:149
void UndoManager::SetODChangesFlag ( )

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

Definition at line 384 of file UndoManager.cpp.

References mODChanges, and mODChangesMutex.

385 {
386  mODChangesMutex.Lock();
387  mODChanges=true;
388  mODChangesMutex.Unlock();
389 }
bool mODChanges
Definition: UndoManager.h:157
ODLock mODChangesMutex
Definition: UndoManager.h:158
const UndoState & UndoManager::SetStateTo ( unsigned int  n,
SelectedRegion selectedRegion 
)

Definition at line 304 of file UndoManager.cpp.

Referenced by AudacityProject::SetStateTo().

305 {
306  n -= 1;
307 
308  wxASSERT(n < stack.size());
309 
310  current = n;
311 
312  *selectedRegion = stack[current]->state.selectedRegion;
313 
314  lastAction = wxT("");
315  mayConsolidate = false;
316 
317  return stack[current]->state;
318 }
bool mayConsolidate
Definition: UndoManager.h:152
wxString lastAction
Definition: UndoManager.h:151
UndoStack stack
Definition: UndoManager.h:149
void UndoManager::StateSaved ( )

Definition at line 366 of file UndoManager.cpp.

References current, ResetODChangesFlag(), and saved.

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

367 {
368  saved = current;
370 }
void ResetODChangesFlag()
void UndoManager::StopConsolidating ( )
inline

Definition at line 114 of file UndoManager.h.

Referenced by MenuCommandHandler::DoClipLeftOrRight().

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

Definition at line 320 of file UndoManager.cpp.

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

321 {
322  wxASSERT(UndoAvailable());
323 
324  current--;
325 
326  *selectedRegion = stack[current]->state.selectedRegion;
327 
328  lastAction = wxT("");
329  mayConsolidate = false;
330 
331  return stack[current]->state;
332 }
bool mayConsolidate
Definition: UndoManager.h:152
wxString lastAction
Definition: UndoManager.h:151
UndoStack stack
Definition: UndoManager.h:149
bool UndoAvailable()
bool UndoManager::UndoAvailable ( )

Definition at line 211 of file UndoManager.cpp.

References current.

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

212 {
213  return (current > 0);
214 }
bool UndoManager::UnsavedChanges ( )

Definition at line 361 of file UndoManager.cpp.

References current, HasODChangesFlag(), and saved.

Referenced by AudacityProject::OnCloseWindow().

362 {
363  return (saved != current) || HasODChangesFlag();
364 }
bool HasODChangesFlag()

Member Data Documentation

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

Definition at line 151 of file UndoManager.h.

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

bool UndoManager::mayConsolidate { false }
private

Definition at line 152 of file UndoManager.h.

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

unsigned long long UndoManager::mClipboardSpaceUsage {}
private

Definition at line 155 of file UndoManager.h.

Referenced by CalculateSpaceUsage().

bool UndoManager::mODChanges
private

Definition at line 157 of file UndoManager.h.

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

ODLock UndoManager::mODChangesMutex
private

Definition at line 158 of file UndoManager.h.

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

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

Definition at line 154 of file UndoManager.h.

Referenced by CalculateSpaceUsage(), and GetLongDescription().

UndoStack UndoManager::stack
private

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