Define virtual Track::PasteInto to simplfy Paste...

... Also making EditMenus not dependent on TimeTrack
This commit is contained in:
Paul Licameli 2021-01-29 00:25:33 -05:00
parent 8543d2dd30
commit 15313a27f7
10 changed files with 55 additions and 36 deletions

View File

@ -85,6 +85,13 @@ LabelTrack::LabelTrack(const LabelTrack &orig) :
}
}
Track::Holder LabelTrack::PasteInto( AudacityProject & ) const
{
auto pNewTrack = std::make_shared<LabelTrack>();
pNewTrack->Paste(0.0, this);
return pNewTrack;
}
template<typename IntervalType>
static IntervalType DoMakeInterval(const LabelStruct &label, size_t index)
{

View File

@ -155,6 +155,8 @@ public:
int FindNextLabel(const SelectedRegion& currentSelection);
int FindPrevLabel(const SelectedRegion& currentSelection);
Track::Holder PasteInto( AudacityProject & ) const override;
struct IntervalData final : Track::IntervalData {
size_t index;
explicit IntervalData(size_t index) : index{index} {};

View File

@ -683,6 +683,13 @@ QuantizedTimeAndBeat NoteTrack::NearestBeatTime( double time ) const
return { seq_time + GetOffset(), beat };
}
Track::Holder NoteTrack::PasteInto( AudacityProject & ) const
{
auto pNewTrack = std::make_shared<NoteTrack>();
pNewTrack->Paste(0.0, this);
return pNewTrack;
}
auto NoteTrack::GetIntervals() const -> ConstIntervals
{
ConstIntervals results;

View File

@ -185,6 +185,8 @@ public:
mVisibleChannels = CHANNEL_BIT(c);
}
Track::Holder PasteInto( AudacityProject & ) const override;
ConstIntervals GetIntervals() const override;
Intervals GetIntervals() override;

View File

@ -136,6 +136,18 @@ bool TimeTrack::SupportsBasicEditing() const
return false;
}
Track::Holder TimeTrack::PasteInto( AudacityProject &project ) const
{
// Maintain uniqueness of the time track!
std::shared_ptr<TimeTrack> pNewTrack;
if( auto pTrack = *TrackList::Get( project ).Any<TimeTrack>().begin() )
pNewTrack = pTrack->SharedPointer<TimeTrack>();
else
pNewTrack = std::make_shared<TimeTrack>( &ViewInfo::Get( project ) );
pNewTrack->Paste(0.0, this);
return pNewTrack;
}
Track::Holder TimeTrack::Cut( double t0, double t1 )
{
auto result = Copy( t0, t1, false );

View File

@ -42,6 +42,8 @@ class TimeTrack final : public Track {
bool SupportsBasicEditing() const override;
Holder PasteInto( AudacityProject & ) const override;
Holder Cut( double t0, double t1 ) override;
Holder Copy( double t0, double t1, bool forClipboard ) const override;
void Clear(double t0, double t1) override;

View File

@ -321,6 +321,12 @@ class AUDACITY_DLL_API Track /* not final */
//! Whether this track type implements cut-copy-paste; by default, true
virtual bool SupportsBasicEditing() const;
using Holder = std::shared_ptr<Track>;
//! Find or create the destination track for a paste, maybe in a different project
/*! @return A smart pointer to the track; its `use_count()` can tell whether it is new */
virtual Holder PasteInto( AudacityProject & ) const = 0;
//! Report times on the track where important intervals begin and end, for UI to snap to
/*!
Some intervals may be empty, and no ordering of the intervals is assumed.
@ -393,7 +399,6 @@ private:
void Init(const Track &orig);
using Holder = std::shared_ptr<Track>;
// public nonvirtual duplication function that invokes Clone():
virtual Holder Duplicate() const;

View File

@ -334,6 +334,15 @@ static Container MakeIntervals(const std::vector<WaveClipHolder> &clips)
return result;
}
Track::Holder WaveTrack::PasteInto( AudacityProject &project ) const
{
auto &trackFactory = WaveTrackFactory::Get( project );
auto &pSampleBlockFactory = trackFactory.GetSampleBlockFactory();
auto pNewTrack = EmptyCopy( pSampleBlockFactory );
pNewTrack->Paste(0.0, this);
return pNewTrack;
}
auto WaveTrack::GetIntervals() const -> ConstIntervals
{
return MakeIntervals<ConstIntervals>( mClips );

View File

@ -537,6 +537,8 @@ private:
std::shared_ptr<WaveClip> pClip;
};
Track::Holder PasteInto( AudacityProject & ) const override;
ConstIntervals GetIntervals() const override;
Intervals GetIntervals() override;

View File

@ -11,7 +11,6 @@
#include "../ProjectSettings.h"
#include "../ProjectWindow.h"
#include "../SelectUtilities.h"
#include "../TimeTrack.h"
#include "../TrackPanel.h"
#include "../TrackPanelAx.h"
#include "../UndoManager.h"
@ -76,8 +75,6 @@ bool DoPasteText(AudacityProject &project)
bool DoPasteNothingSelected(AudacityProject &project)
{
auto &tracks = TrackList::Get( project );
auto &trackFactory = WaveTrackFactory::Get( project );
auto &pSampleBlockFactory = trackFactory.GetSampleBlockFactory();
auto &selectedRegion = ViewInfo::Get( project ).selectedRegion;
auto &viewInfo = ViewInfo::Get( project );
auto &window = ProjectWindow::Get( project );
@ -94,44 +91,18 @@ bool DoPasteNothingSelected(AudacityProject &project)
Track* pFirstNewTrack = NULL;
for (auto pClip : clipTrackRange) {
Track::Holder uNewTrack;
Track *pNewTrack;
pClip->TypeSwitch(
[&](const WaveTrack *wc) {
uNewTrack = wc->EmptyCopy( pSampleBlockFactory );
pNewTrack = uNewTrack.get();
},
#ifdef USE_MIDI
[&](const NoteTrack *) {
uNewTrack = std::make_shared<NoteTrack>(),
pNewTrack = uNewTrack.get();
},
#endif
[&](const LabelTrack *) {
uNewTrack = std::make_shared<LabelTrack>(),
pNewTrack = uNewTrack.get();
},
[&](const TimeTrack *) {
// Maintain uniqueness of the time track!
pNewTrack = *tracks.Any<TimeTrack>().begin();
if (!pNewTrack)
uNewTrack = std::make_shared<TimeTrack>( &viewInfo ),
pNewTrack = uNewTrack.get();
}
);
auto pNewTrack = pClip->PasteInto( project );
bool newTrack = (pNewTrack.use_count() == 1);
wxASSERT(pClip);
pNewTrack->Paste(0.0, pClip);
if (!pFirstNewTrack)
pFirstNewTrack = pNewTrack;
pFirstNewTrack = pNewTrack.get();
pNewTrack->SetSelected(true);
if (uNewTrack)
FinishCopy(pClip, uNewTrack, tracks);
if (newTrack)
FinishCopy(pClip, pNewTrack, tracks);
else
Track::FinishCopy(pClip, pNewTrack);
Track::FinishCopy(pClip, pNewTrack.get());
}
// Select some pasted samples, which is probably impossible to get right