mirror of
https://github.com/cookiengineer/audacity
synced 2025-06-17 16:40:07 +02:00
Define UndoManager::VisitStates
This commit is contained in:
parent
d49a888b98
commit
490271f259
@ -173,6 +173,6 @@ void ProjectHistory::SetStateTo(unsigned int n, bool doAutosave)
|
|||||||
auto &undoManager = UndoManager::Get( project );
|
auto &undoManager = UndoManager::Get( project );
|
||||||
|
|
||||||
undoManager.SetStateTo(n,
|
undoManager.SetStateTo(n,
|
||||||
[this, doAutosave]( const UndoState &state ){
|
[this, doAutosave]( const UndoStackElem &elem ){
|
||||||
PopState(state, doAutosave); } );
|
PopState(elem.state, doAutosave); } );
|
||||||
}
|
}
|
||||||
|
@ -48,24 +48,6 @@ wxDEFINE_EVENT(EVT_UNDO_RESET, wxCommandEvent);
|
|||||||
|
|
||||||
using SampleBlockID = long long;
|
using SampleBlockID = long long;
|
||||||
|
|
||||||
struct UndoStackElem {
|
|
||||||
|
|
||||||
UndoStackElem(std::shared_ptr<TrackList> &&tracks_,
|
|
||||||
const TranslatableString &description_,
|
|
||||||
const TranslatableString &shortDescription_,
|
|
||||||
const SelectedRegion &selectedRegion_,
|
|
||||||
const std::shared_ptr<Tags> &tags_)
|
|
||||||
: state(std::move(tracks_), tags_, selectedRegion_)
|
|
||||||
, description(description_)
|
|
||||||
, shortDescription(shortDescription_)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
UndoState state;
|
|
||||||
TranslatableString description;
|
|
||||||
TranslatableString shortDescription;
|
|
||||||
};
|
|
||||||
|
|
||||||
static const AudacityProject::AttachedObjects::RegisteredFactory key{
|
static const AudacityProject::AttachedObjects::RegisteredFactory key{
|
||||||
[](AudacityProject &project)
|
[](AudacityProject &project)
|
||||||
{ return std::make_unique<UndoManager>( project ); }
|
{ return std::make_unique<UndoManager>( project ); }
|
||||||
@ -370,7 +352,7 @@ void UndoManager::SetStateTo(unsigned int n, const Consumer &consumer)
|
|||||||
lastAction = {};
|
lastAction = {};
|
||||||
mayConsolidate = false;
|
mayConsolidate = false;
|
||||||
|
|
||||||
consumer( stack[current]->state );
|
consumer( *stack[current] );
|
||||||
|
|
||||||
// wxWidgets will own the event object
|
// wxWidgets will own the event object
|
||||||
mProject.QueueEvent( safenew wxCommandEvent{ EVT_UNDO_RESET } );
|
mProject.QueueEvent( safenew wxCommandEvent{ EVT_UNDO_RESET } );
|
||||||
@ -385,7 +367,7 @@ void UndoManager::Undo(const Consumer &consumer)
|
|||||||
lastAction = {};
|
lastAction = {};
|
||||||
mayConsolidate = false;
|
mayConsolidate = false;
|
||||||
|
|
||||||
consumer( stack[current]->state );
|
consumer( *stack[current] );
|
||||||
|
|
||||||
// wxWidgets will own the event object
|
// wxWidgets will own the event object
|
||||||
mProject.QueueEvent( safenew wxCommandEvent{ EVT_UNDO_OR_REDO } );
|
mProject.QueueEvent( safenew wxCommandEvent{ EVT_UNDO_OR_REDO } );
|
||||||
@ -413,12 +395,39 @@ void UndoManager::Redo(const Consumer &consumer)
|
|||||||
lastAction = {};
|
lastAction = {};
|
||||||
mayConsolidate = false;
|
mayConsolidate = false;
|
||||||
|
|
||||||
consumer( stack[current]->state );
|
consumer( *stack[current] );
|
||||||
|
|
||||||
// wxWidgets will own the event object
|
// wxWidgets will own the event object
|
||||||
mProject.QueueEvent( safenew wxCommandEvent{ EVT_UNDO_OR_REDO } );
|
mProject.QueueEvent( safenew wxCommandEvent{ EVT_UNDO_OR_REDO } );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UndoManager::VisitStates( const Consumer &consumer, bool newestFirst )
|
||||||
|
{
|
||||||
|
auto fn = [&]( decltype(stack[0]) &ptr ){ consumer( *ptr ); };
|
||||||
|
if (newestFirst)
|
||||||
|
std::for_each(stack.rbegin(), stack.rend(), fn);
|
||||||
|
else
|
||||||
|
std::for_each(stack.begin(), stack.end(), fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UndoManager::VisitStates(
|
||||||
|
const Consumer &consumer, size_t begin, size_t end )
|
||||||
|
{
|
||||||
|
auto size = stack.size();
|
||||||
|
if (begin < end) {
|
||||||
|
end = std::min(end, size);
|
||||||
|
for (auto ii = begin; ii < end; ++ii)
|
||||||
|
consumer(*stack[ii]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (size == 0)
|
||||||
|
return;
|
||||||
|
begin = std::min(begin, size - 1);
|
||||||
|
for (auto ii = begin; ii > end; --ii)
|
||||||
|
consumer(*stack[ii]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool UndoManager::UnsavedChanges() const
|
bool UndoManager::UnsavedChanges() const
|
||||||
{
|
{
|
||||||
return (saved != current);
|
return (saved != current);
|
||||||
|
@ -76,7 +76,6 @@ class Tags;
|
|||||||
class Track;
|
class Track;
|
||||||
class TrackList;
|
class TrackList;
|
||||||
|
|
||||||
struct UndoStackElem;
|
|
||||||
struct UndoState {
|
struct UndoState {
|
||||||
UndoState(std::shared_ptr<TrackList> &&tracks_,
|
UndoState(std::shared_ptr<TrackList> &&tracks_,
|
||||||
const std::shared_ptr<Tags> &tags_,
|
const std::shared_ptr<Tags> &tags_,
|
||||||
@ -89,6 +88,24 @@ struct UndoState {
|
|||||||
SelectedRegion selectedRegion; // by value
|
SelectedRegion selectedRegion; // by value
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct UndoStackElem {
|
||||||
|
|
||||||
|
UndoStackElem(std::shared_ptr<TrackList> &&tracks_,
|
||||||
|
const TranslatableString &description_,
|
||||||
|
const TranslatableString &shortDescription_,
|
||||||
|
const SelectedRegion &selectedRegion_,
|
||||||
|
const std::shared_ptr<Tags> &tags_)
|
||||||
|
: state(std::move(tracks_), tags_, selectedRegion_)
|
||||||
|
, description(description_)
|
||||||
|
, shortDescription(shortDescription_)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
UndoState state;
|
||||||
|
TranslatableString description;
|
||||||
|
TranslatableString shortDescription;
|
||||||
|
};
|
||||||
|
|
||||||
using UndoStack = std::vector <std::unique_ptr<UndoStackElem>>;
|
using UndoStack = std::vector <std::unique_ptr<UndoStackElem>>;
|
||||||
|
|
||||||
using SpaceArray = std::vector <unsigned long long> ;
|
using SpaceArray = std::vector <unsigned long long> ;
|
||||||
@ -150,11 +167,18 @@ class AUDACITY_DLL_API UndoManager final
|
|||||||
// These functions accept a callback that uses the state,
|
// These functions accept a callback that uses the state,
|
||||||
// and then they send to the project EVT_UNDO_RESET or EVT_UNDO_OR_REDO when
|
// and then they send to the project EVT_UNDO_RESET or EVT_UNDO_OR_REDO when
|
||||||
// that has finished.
|
// that has finished.
|
||||||
using Consumer = std::function< void( const UndoState & ) >;
|
using Consumer = std::function< void( const UndoStackElem & ) >;
|
||||||
void SetStateTo(unsigned int n, const Consumer &consumer);
|
void SetStateTo(unsigned int n, const Consumer &consumer);
|
||||||
void Undo(const Consumer &consumer);
|
void Undo(const Consumer &consumer);
|
||||||
void Redo(const Consumer &consumer);
|
void Redo(const Consumer &consumer);
|
||||||
|
|
||||||
|
//! Give read-only access to all states
|
||||||
|
void VisitStates( const Consumer &consumer, bool newestFirst );
|
||||||
|
//! Visit a specified range of states
|
||||||
|
/*! end is exclusive; visit newer states first if end < begin */
|
||||||
|
void VisitStates(
|
||||||
|
const Consumer &consumer, size_t begin, size_t end );
|
||||||
|
|
||||||
bool UndoAvailable();
|
bool UndoAvailable();
|
||||||
bool RedoAvailable();
|
bool RedoAvailable();
|
||||||
|
|
||||||
|
@ -183,8 +183,8 @@ void OnUndo(const CommandContext &context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
undoManager.Undo(
|
undoManager.Undo(
|
||||||
[&]( const UndoState &state ){
|
[&]( const UndoStackElem &elem ){
|
||||||
ProjectHistory::Get( project ).PopState( state ); } );
|
ProjectHistory::Get( project ).PopState( elem.state ); } );
|
||||||
|
|
||||||
auto t = *tracks.Selected().begin();
|
auto t = *tracks.Selected().begin();
|
||||||
if (!t)
|
if (!t)
|
||||||
@ -213,8 +213,8 @@ void OnRedo(const CommandContext &context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
undoManager.Redo(
|
undoManager.Redo(
|
||||||
[&]( const UndoState &state ){
|
[&]( const UndoStackElem &elem ){
|
||||||
ProjectHistory::Get( project ).PopState( state ); } );
|
ProjectHistory::Get( project ).PopState( elem.state ); } );
|
||||||
|
|
||||||
auto t = *tracks.Selected().begin();
|
auto t = *tracks.Selected().begin();
|
||||||
if (!t)
|
if (!t)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user