Reimplement prohibitions on moving clips by throwing exceptions...

... Also comment some strong and weak guarantees
This commit is contained in:
Paul Licameli 2017-03-24 15:01:19 -04:00
parent 8e2e4a53b8
commit 608dcc9b3f
3 changed files with 96 additions and 78 deletions

View File

@ -1539,6 +1539,7 @@ void WaveClip::WriteXML(XMLWriter &xmlFile) const
}
bool WaveClip::Paste(double t0, const WaveClip* other)
// STRONG-GUARANTEE
{
const bool clipNeedsResampling = other->mRate != mRate;
const bool clipNeedsNewFormat =
@ -1558,34 +1559,42 @@ bool WaveClip::Paste(double t0, const WaveClip* other)
// Force sample formats to match.
newClip->ConvertToSampleFormat(mSequence->GetSampleFormat());
pastedClip = newClip.get();
} else
}
else
{
// No resampling or format change needed, just use original clip without making a copy
pastedClip = other;
}
// Paste cut lines contained in pasted clip
WaveClipHolders newCutlines;
for (const auto &cutline: pastedClip->mCutLines)
{
newCutlines.push_back(
make_movable<WaveClip>
( *cutline, mSequence->GetDirManager(),
// Recursively copy cutlines of cutlines. They don't need
// their offsets adjusted.
true));
newCutlines.back()->Offset(t0 - mOffset);
}
sampleCount s0;
TimeToSamplesClip(t0, &s0);
bool result = false;
// Assume STRONG-GUARANTEE from Sequence::Paste
if (mSequence->Paste(s0, pastedClip->mSequence.get()))
{
// Assume NOFAIL-GUARANTEE in the remaining
MarkChanged();
mEnvelope->Paste(s0.as_double()/mRate + mOffset, pastedClip->mEnvelope.get());
mEnvelope->RemoveUnneededPoints();
OffsetCutLines(t0, pastedClip->GetEndTime() - pastedClip->GetStartTime());
// Paste cut lines contained in pasted clip
for (const auto &cutline: pastedClip->mCutLines)
{
mCutLines.push_back(
make_movable<WaveClip>
( *cutline, mSequence->GetDirManager(),
// Recursively copy cutlines of cutlines. They don't need
// their offsets adjusted.
true));
mCutLines.back()->Offset(t0 - mOffset);
}
for (auto &holder : newCutlines)
mCutLines.push_back(std::move(holder));
result = true;
}
@ -1745,28 +1754,33 @@ bool WaveClip::FindCutLine(double cutLinePosition,
}
bool WaveClip::ExpandCutLine(double cutLinePosition)
// STRONG-GUARANTEE
{
for (auto it = mCutLines.begin(); it != mCutLines.end(); ++it)
{
WaveClip *const cutline = it->get();
if (fabs(mOffset + cutline->GetOffset() - cutLinePosition) < 0.0001)
{
if (!Paste(mOffset+cutline->GetOffset(), cutline))
return false;
// Now erase the cutline,
// but be careful to find it again, because Paste above may
// have modified the array of cutlines (if our cutline contained
// another cutline!), invalidating the iterator we had.
auto begin = mCutLines.begin(), end = mCutLines.end();
it = std::find_if(begin, end,
[=](decltype(*begin) &p){ return p.get() == cutline; });
if (it != end)
mCutLines.erase(it); // deletes cutline!
else {
wxASSERT(false);
}
return true;
auto end = mCutLines.end();
auto it = std::find_if( mCutLines.begin(), end,
[&](const WaveClipHolder &cutline) {
return fabs(mOffset + cutline->GetOffset() - cutLinePosition) < 0.0001;
} );
if ( it != end ) {
auto cutline = it->get();
// assume STRONG-GUARANTEE from Paste
if (!Paste(mOffset+cutline->GetOffset(), cutline))
return false;
// Now erase the cutline,
// but be careful to find it again, because Paste above may
// have modified the array of cutlines (if our cutline contained
// another cutline!), invalidating the iterator we had.
end = mCutLines.end();
it = std::find_if(mCutLines.begin(), end,
[=](const WaveClipHolder &p) { return p.get() == cutline; });
if (it != end)
mCutLines.erase(it); // deletes cutline!
else {
// THROW_INCONSISTENCY_EXCEPTION;
wxASSERT(false);
}
return true;
}
return false;
@ -1788,6 +1802,7 @@ bool WaveClip::RemoveCutLine(double cutLinePosition)
}
void WaveClip::OffsetCutLines(double t0, double len)
// NOFAIL-GUARANTEE
{
for (const auto &cutLine : mCutLines)
{

View File

@ -141,7 +141,8 @@ public:
class WaveClip;
// Array of pointers that assume ownership
using WaveClipHolders = std::vector < movable_ptr< WaveClip > >;
using WaveClipHolder = movable_ptr< WaveClip >;
using WaveClipHolders = std::vector < WaveClipHolder >;
using WaveClipConstHolders = std::vector < movable_ptr< const WaveClip > >;
// Temporary arrays of mere pointers

View File

@ -1202,6 +1202,7 @@ bool WaveTrack::SyncLockAdjust(double oldT1, double newT1)
}
bool WaveTrack::Paste(double t0, const Track *src)
// WEAK-GUARANTEE
{
bool editClipCanMove = true;
gPrefs->Read(wxT("/GUI/EditClipCanMove"), &editClipCanMove);
@ -1287,7 +1288,8 @@ bool WaveTrack::Paste(double t0, const Track *src)
insideClip = clip.get();
break;
}
} else
}
else
{
// If clips are immovable we also allow prepending to clips
if (clip->WithinClip(t0) ||
@ -1312,12 +1314,11 @@ bool WaveTrack::Paste(double t0, const Track *src)
if (clip->GetStartTime() > insideClip->GetStartTime() &&
insideClip->GetEndTime() + insertDuration >
clip->GetStartTime())
{
wxMessageBox(
_("There is not enough room available to paste the selection"),
_("Error"), wxICON_STOP);
return false;
}
// STRONG-GUARANTEE in case of this path
// not that it matters.
throw SimpleMessageBoxException{
_("There is not enough room available to paste the selection")
};
}
}
@ -1331,12 +1332,11 @@ bool WaveTrack::Paste(double t0, const Track *src)
//printf("paste: multi clip mode!\n");
if (!editClipCanMove && !IsEmpty(t0, t0+insertDuration-1.0/mRate))
{
wxMessageBox(
_("There is not enough room available to paste the selection"),
_("Error"), wxICON_STOP);
return false;
}
// STRONG-GUARANTEE in case of this path
// not that it matters.
throw SimpleMessageBoxException{
_("There is not enough room available to paste the selection")
};
for (const auto &clip : other->mClips)
{
@ -2447,50 +2447,52 @@ void WaveTrack::UpdateLocationsCache() const
// Expand cut line (that is, re-insert audio, then DELETE audio saved in cut line)
bool WaveTrack::ExpandCutLine(double cutLinePosition, double* cutlineStart,
double* cutlineEnd)
// STRONG-GUARANTEE
{
bool editClipCanMove = true;
gPrefs->Read(wxT("/GUI/EditClipCanMove"), &editClipCanMove);
// Find clip which contains this cut line
for (const auto &clip : mClips)
double start = 0, end = 0;
auto pEnd = mClips.end();
auto pClip = std::find_if( mClips.begin(), pEnd,
[&](const WaveClipHolder &clip) {
return clip->FindCutLine(cutLinePosition, &start, &end); } );
if (pClip != pEnd)
{
double start = 0, end = 0;
if (clip->FindCutLine(cutLinePosition, &start, &end))
auto &clip = *pClip;
if (!editClipCanMove)
{
if (!editClipCanMove)
// We are not allowed to move the other clips, so see if there
// is enough room to expand the cut line
for (const auto &clip2: mClips)
{
// We are not allowed to move the other clips, so see if there
// is enough room to expand the cut line
for (const auto &clip2: mClips)
{
if (clip2->GetStartTime() > clip->GetStartTime() &&
clip->GetEndTime() + end - start > clip2->GetStartTime())
{
wxMessageBox(
_("There is not enough room available to expand the cut line"),
_("Error"), wxICON_STOP);
return false;
}
}
}
if (clip2->GetStartTime() > clip->GetStartTime() &&
clip->GetEndTime() + end - start > clip2->GetStartTime())
// STRONG-GUARANTEE in case of this path
throw SimpleMessageBoxException{
_("There is not enough room available to expand the cut line")
};
}
}
if (!clip->ExpandCutLine(cutLinePosition))
return false;
if (!clip->ExpandCutLine(cutLinePosition))
return false;
if (cutlineStart)
*cutlineStart = start;
if (cutlineEnd)
*cutlineEnd = end;
// STRONG-GUARANTEE provided that the following gives NOFAIL-GUARANTEE
// Move clips which are to the right of the cut line
if (editClipCanMove)
if (cutlineStart)
*cutlineStart = start;
if (cutlineEnd)
*cutlineEnd = end;
// Move clips which are to the right of the cut line
if (editClipCanMove)
{
for (const auto &clip2 : mClips)
{
for (const auto &clip2 : mClips)
{
if (clip2->GetStartTime() > clip->GetStartTime())
clip2->Offset(end - start);
}
if (clip2->GetStartTime() > clip->GetStartTime())
clip2->Offset(end - start);
}
return true;