From 93c0b5a92da0ac4b06740b40735cc93e23a574a1 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Mon, 7 Nov 2016 11:25:27 -0500 Subject: [PATCH] Add button to History dialog to clear the clipboard & space calc --- src/HistoryWindow.cpp | 18 +++++++++- src/HistoryWindow.h | 2 ++ src/Project.h | 2 +- src/UndoManager.cpp | 78 ++++++++++++++++++++++++++----------------- src/UndoManager.h | 8 +++++ 5 files changed, 76 insertions(+), 32 deletions(-) diff --git a/src/HistoryWindow.cpp b/src/HistoryWindow.cpp index 1ac07a42c..291e53fbd 100644 --- a/src/HistoryWindow.cpp +++ b/src/HistoryWindow.cpp @@ -42,7 +42,8 @@ enum { ID_AVAIL = 1000, ID_TOTAL, ID_LEVELS, - ID_DISCARD + ID_DISCARD, + ID_DISCARD_CLIPBOARD }; BEGIN_EVENT_TABLE(HistoryWindow, wxDialogWrapper) @@ -50,6 +51,7 @@ BEGIN_EVENT_TABLE(HistoryWindow, wxDialogWrapper) EVT_CLOSE(HistoryWindow::OnCloseWindow) EVT_LIST_ITEM_SELECTED(wxID_ANY, HistoryWindow::OnItemSelected) EVT_BUTTON(ID_DISCARD, HistoryWindow::OnDiscard) + EVT_BUTTON(ID_DISCARD_CLIPBOARD, HistoryWindow::OnDiscardClipboard) END_EVENT_TABLE() HistoryWindow::HistoryWindow(AudacityProject *parent, UndoManager *manager): @@ -112,6 +114,10 @@ HistoryWindow::HistoryWindow(AudacityProject *parent, UndoManager *manager): S.AddWindow(mLevels); /* i18n-hint: (verb)*/ mDiscard = S.Id(ID_DISCARD).AddButton(_("&Discard")); + + mClipboard = S.AddTextBox(_("Clipboard space used"), wxT("0"), 10); + mClipboard->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(HistoryWindow::OnChar)); + S.Id(ID_DISCARD_CLIPBOARD).AddButton(_("Discard")); } S.EndMultiColumn(); } @@ -198,6 +204,10 @@ void HistoryWindow::DoUpdate() mTotal->SetValue(Internat::FormatSize(total)); + auto clipboardUsage = mManager->GetClipboardSpaceUsage(); + mClipboard->SetValue(Internat::FormatSize(clipboardUsage)); + FindWindowById(ID_DISCARD_CLIPBOARD)->Enable(clipboardUsage > 0); + mList->EnsureVisible(mSelected); mList->SetItemState(mSelected, @@ -248,6 +258,12 @@ void HistoryWindow::OnDiscard(wxCommandEvent & WXUNUSED(event)) DoUpdate(); } +void HistoryWindow::OnDiscardClipboard(wxCommandEvent & WXUNUSED(event)) +{ + AudacityProject::ClearClipboard(); + DoUpdate(); +} + void HistoryWindow::OnItemSelected(wxListEvent &event) { if (mAudioIOBusy) { diff --git a/src/HistoryWindow.h b/src/HistoryWindow.h index 18396b2db..7d4df5eb0 100644 --- a/src/HistoryWindow.h +++ b/src/HistoryWindow.h @@ -42,11 +42,13 @@ class HistoryWindow final : public wxDialogWrapper { void OnChar(wxKeyEvent & event); void OnItemSelected(wxListEvent & event); void OnDiscard(wxCommandEvent & event); + void OnDiscardClipboard(wxCommandEvent & event); AudacityProject *mProject; UndoManager *mManager; wxListCtrl *mList; wxTextCtrl *mTotal; + wxTextCtrl *mClipboard; wxTextCtrl *mAvail; wxSpinCtrl *mLevels; wxButton *mDiscard; diff --git a/src/Project.h b/src/Project.h index 44c7937a9..70fb55d93 100644 --- a/src/Project.h +++ b/src/Project.h @@ -341,6 +341,7 @@ class AUDACITY_DLL_API AudacityProject final : public wxFrame, // Other commands static TrackList *GetClipboardTracks(); + static void ClearClipboard(); static void DeleteClipboard(); int GetProjectNumber(){ return mProjectNo;}; @@ -528,7 +529,6 @@ public: private: void OnCapture(wxCommandEvent & evt); - void ClearClipboard(); void InitialState(); void ModifyState(bool bWantsAutoSave); // if true, writes auto-save file. Should set only if you really want the state change restored after // a crash, as it can take many seconds for large (eg. 10 track-hours) projects diff --git a/src/UndoManager.cpp b/src/UndoManager.cpp index aaa7b5661..bfeb2f3f9 100644 --- a/src/UndoManager.cpp +++ b/src/UndoManager.cpp @@ -27,6 +27,7 @@ UndoManager #include "BlockFile.h" #include "Diags.h" #include "Internat.h" +#include "Project.h" #include "Sequence.h" #include "WaveTrack.h" // temp #include "NoteTrack.h" // for Sonify* function declarations @@ -69,11 +70,50 @@ UndoManager::~UndoManager() ClearStates(); } +namespace { + SpaceArray::value_type + CalculateUsage(TrackList *tracks, Set *prev, Set *cur) + { + SpaceArray::value_type result = 0; + + //TIMER_START( "CalculateSpaceUsage", space_calc ); + TrackListOfKindIterator iter(Track::Wave); + WaveTrack *wt = (WaveTrack *) iter.First(tracks); + while (wt) + { + // Scan all clips within current track + for(const auto &clip : wt->GetAllClips()) + { + // Scan all blockfiles within current clip + BlockArray *blocks = clip->GetSequenceBlockArray(); + for (const auto &block : *blocks) + { + const auto &file = block.f; + + // Accumulate space used by the file if the file didn't exist + // in the previous level + if (!prev || !cur || + (prev->count( &*file ) == 0 && cur->count( &*file ) == 0)) + { + unsigned long long usage{ file->GetSpaceUsage() }; + result += usage; + } + + // Add file to current set + if (cur) + cur->insert( &*file ); + } + } + + wt = (WaveTrack *) iter.Next(); + } + + return result; + } +} + void UndoManager::CalculateSpaceUsage() { - //TIMER_START( "CalculateSpaceUsage", space_calc ); - TrackListOfKindIterator iter(Track::Wave); - space.clear(); space.resize(stack.size(), 0); @@ -90,35 +130,13 @@ void UndoManager::CalculateSpaceUsage() cur->clear(); // Scan all tracks at current level - WaveTrack *wt = (WaveTrack *) iter.First(stack[i]->state.tracks.get()); - while (wt) - { - // Scan all clips within current track - for(const auto &clip : wt->GetAllClips()) - { - // Scan all blockfiles within current clip - BlockArray *blocks = clip->GetSequenceBlockArray(); - for (const auto &block : *blocks) - { - const auto &file = block.f; - - // Accumulate space used by the file if the file didn't exist - // in the previous level - if (prev->count( &*file ) == 0 && cur->count( &*file ) == 0) - { - unsigned long long usage{ file->GetSpaceUsage() }; - space[i] += usage; - } - - // Add file to current set - cur->insert( &*file ); - } - } - - wt = (WaveTrack *) iter.Next(); - } + auto tracks = stack[i]->state.tracks.get(); + space[i] = CalculateUsage(tracks, prev, cur); } + mClipboardSpaceUsage = CalculateUsage + (AudacityProject::GetClipboardTracks(), nullptr, nullptr); + //TIMER_STOP( space_calc ); } diff --git a/src/UndoManager.h b/src/UndoManager.h index 4febd7ff9..f4108ea3f 100644 --- a/src/UndoManager.h +++ b/src/UndoManager.h @@ -108,6 +108,7 @@ class AUDACITY_DLL_API UndoManager { unsigned int GetCurrentState(); void GetShortDescription(unsigned int n, wxString *desc); + // Return value must first be calculated by CalculateSpaceUsage(): wxLongLong_t GetLongDescription(unsigned int n, wxString *desc, wxString *size); void SetLongDescription(unsigned int n, const wxString &desc); @@ -121,6 +122,12 @@ class AUDACITY_DLL_API UndoManager { bool UnsavedChanges(); void StateSaved(); + // Return value must first be calculated by CalculateSpaceUsage(): + // The clipboard is global, not specific to this project, but it is + // convenient to combine the space usage calculations in one class: + wxLongLong_t GetClipboardSpaceUsage() const + { return mClipboardSpaceUsage; } + void CalculateSpaceUsage(); // void Debug(); // currently unused @@ -139,6 +146,7 @@ class AUDACITY_DLL_API UndoManager { int consolidationCount; SpaceArray space; + unsigned long long mClipboardSpaceUsage {}; bool mODChanges; ODLock mODChangesMutex;//mODChanges is accessed from many threads.