Audacity 3.2.0
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
LabelTrackShifter Class Referencefinal
Inheritance diagram for LabelTrackShifter:
[legend]
Collaboration diagram for LabelTrackShifter:
[legend]

Classes

struct  MovingInterval
 

Public Member Functions

 LabelTrackShifter (LabelTrack &track, AudacityProject &project)
 
 ~LabelTrackShifter () override
 
TrackGetTrack () const override
 Label track is always leader; satisfying the post. More...
 
HitTestResult HitTest (double time, const ViewInfo &viewInfo, HitTestParams *pParams) override
 Decide how shift behaves, based on the track that is clicked in. More...
 
void SelectInterval (const ChannelGroupInterval &interval) override
 Notifies the shifter that a region is selected, so it may update its fixed and moving intervals. More...
 
bool SyncLocks () override
 Whether unfixing of an interval should propagate to all overlapping intervals in the sync lock group. More...
 
bool MayMigrateTo (Track &otherTrack) override
 
Intervals Detach () override
 Remove all moving intervals from the track, if possible. More...
 
bool AdjustFit (const Track &, const Intervals &, double &, double) override
 
bool Attach (Intervals intervals, double offset) override
 Put moving intervals into the track, which may have migrated from another. More...
 
void DoHorizontalOffset (double offset) override
 
- Public Member Functions inherited from TrackShifter
 TrackShifter ()
 
 TrackShifter (const TrackShifter &)=delete
 
TrackShifteroperator= (const TrackShifter &)=delete
 
virtual ~TrackShifter ()=0
 
virtual TrackGetTrack () const =0
 There is always an associated track. More...
 
virtual HitTestResult HitTest (double time, const ViewInfo &viewInfo, HitTestParams *pParams=nullptr)=0
 Decide how shift behaves, based on the track that is clicked in. More...
 
const IntervalsFixedIntervals () const
 Return special intervals of the track that will not move. More...
 
const IntervalsMovingIntervals () const
 Return special intervals of the track that may move. More...
 
void UnfixIntervals (std::function< bool(const ChannelGroupInterval &)> pred)
 Change intervals satisfying a predicate from fixed to moving. More...
 
void UnfixAll ()
 Change all intervals from fixed to moving. More...
 
virtual void SelectInterval (const ChannelGroupInterval &interval)
 Notifies the shifter that a region is selected, so it may update its fixed and moving intervals. More...
 
virtual bool SyncLocks ()=0
 Whether unfixing of an interval should propagate to all overlapping intervals in the sync lock group. More...
 
virtual double HintOffsetLarger (double desiredOffset)
 Given amount to shift by horizontally, maybe adjust it from zero to suggest minimum distance. More...
 
virtual double QuantizeOffset (double desiredOffset)
 Given amount to shift by horizontally, do any preferred rounding, before placement constraint checks. More...
 
virtual double AdjustOffsetSmaller (double desiredOffset)
 Given amount to shift by horizontally, maybe adjust it toward zero to meet placement constraints. More...
 
virtual bool MayMigrateTo (Track &otherTrack)
 
virtual Intervals Detach ()
 Remove all moving intervals from the track, if possible. More...
 
virtual bool AdjustFit (const Track &otherTrack, const Intervals &intervals, double &desiredOffset, double tolerance)
 
virtual bool Attach (Intervals intervals, double offset)
 Put moving intervals into the track, which may have migrated from another. More...
 
virtual bool FinishMigration ()
 When dragging is done, do (once) the final steps of migration (which may be expensive) More...
 
virtual void DoHorizontalOffset (double offset)
 
virtual double AdjustT0 (double t0) const
 

Static Public Member Functions

static size_t & GetIndex (ChannelGroupInterval &interval)
 
static size_t GetIndex (const ChannelGroupInterval &interval)
 

Private Member Functions

void OnLabelPermuted (const LabelTrackEvent &e)
 
void OnLabelAdded (const LabelTrackEvent &e)
 
void OnLabelDeleted (const LabelTrackEvent e)
 

Private Attributes

Observer::Subscription mSubscription
 
const std::shared_ptr< LabelTrackmpTrack
 
AudacityProjectmProject
 

Additional Inherited Members

- Public Types inherited from TrackShifter
enum class  HitTestResult { Miss , Selection , Intervals , Track }
 Possibilities for HitTest on the clicked track. More...
 
using Intervals = std::vector< std::shared_ptr< ChannelGroupInterval > >
 
- Protected Member Functions inherited from TrackShifter
void CommonSelectInterval (const ChannelGroupInterval &interval)
 
bool CommonMayMigrateTo (Track &otherTrack)
 
void InitIntervals ()
 Derived class constructor can initialize all intervals reported by the track as fixed, none moving. More...
 
bool AllFixed () const
 
- Protected Attributes inherited from TrackShifter
Intervals mFixed
 
Intervals mMoving
 

Detailed Description

Definition at line 11 of file LabelTrackShifter.cpp.

Constructor & Destructor Documentation

◆ LabelTrackShifter()

LabelTrackShifter::LabelTrackShifter ( LabelTrack track,
AudacityProject project 
)
inline

Definition at line 13 of file LabelTrackShifter.cpp.

14 : mpTrack{ track.SharedPointer<LabelTrack>() }
15 , mProject{ project }
16 {
18 mSubscription = mpTrack->Subscribe([this](const LabelTrackEvent &e){
19 switch (e.type) {
20 case LabelTrackEvent::Permutation:
21 return OnLabelPermuted(e);
22 case LabelTrackEvent::Addition:
23 return OnLabelAdded(e);
24 case LabelTrackEvent::Deletion:
25 return OnLabelDeleted(e);
26 default:
27 return;
28 }
29 });
30 }
const auto project
A LabelTrack is a Track that holds labels (LabelStruct).
Definition: LabelTrack.h:87
AudacityProject & mProject
const std::shared_ptr< LabelTrack > mpTrack
Observer::Subscription mSubscription
std::shared_ptr< Subclass > SharedPointer()
Definition: Track.h:160
void InitIntervals()
Derived class constructor can initialize all intervals reported by the track as fixed,...
enum LabelTrackEvent::Type type

References TrackShifter::InitIntervals(), mpTrack, mSubscription, and LabelTrackEvent::type.

Here is the call graph for this function:

◆ ~LabelTrackShifter()

LabelTrackShifter::~LabelTrackShifter ( )
inlineoverride

Definition at line 31 of file LabelTrackShifter.cpp.

32 {
33 }

Member Function Documentation

◆ AdjustFit()

bool LabelTrackShifter::AdjustFit ( const Track otherTrack,
const Intervals intervals,
double &  desiredOffset,
double  tolerance 
)
inlineoverridevirtual

Test whether intervals can fit into another track, maybe adjusting the offset slightly

Default implementation does nothing and returns false

Parameters
intervalsAssume these came from Detach() and only after MayMigrateTo returned true for otherTrack
[in,out]desiredOffset
toleranceNonnegative ceiling for allowed changes in fabs(desiredOffset)

Reimplemented from TrackShifter.

Definition at line 128 of file LabelTrackShifter.cpp.

130 {
131 // Labels have no overlapping constraints, so just...
132 return true;
133 }

◆ Attach()

bool LabelTrackShifter::Attach ( Intervals  intervals,
double  offset 
)
inlineoverridevirtual

Put moving intervals into the track, which may have migrated from another.

The ChannelGroupInterval objects pointed to by intervals will not be used again. The shifter should repopupate mMoving with new ChannelGroupInterval objects to reflect the new state of the track. In case of failure, track states are unspecified

Returns
success Default implementation does nothing and returns true

Reimplemented from TrackShifter.

Definition at line 135 of file LabelTrackShifter.cpp.

136 {
137 auto pTrack = mpTrack.get();
138 std::for_each(intervals.rbegin(), intervals.rend(),
139 [this, pTrack, offset](const auto &pInterval){
140 auto data = static_cast<const MovingInterval&>(*pInterval);
141 if (offset != .0)
142 data.region.move(offset);
143 auto index = pTrack->AddLabel(data.region, data.title);
144 // Recreate the simpler TrackInterval as would be reported by LabelTrack
145 mMoving.emplace_back(pTrack->MakeInterval(index));
146 } );
147 return true;
148 }

References mpTrack.

◆ Detach()

Intervals LabelTrackShifter::Detach ( )
inlineoverridevirtual

Remove all moving intervals from the track, if possible.

Default implementation does nothing

Reimplemented from TrackShifter.

Definition at line 108 of file LabelTrackShifter.cpp.

109 {
110 auto pTrack = mpTrack.get();
111 auto moveLabel = [pTrack](const auto &pInterval) {
112 auto index = GetIndex(*pInterval);
113 auto result = std::make_shared<MovingInterval>(
114 pInterval->Start(), pInterval->End(), *pTrack->GetLabel(index));
115 pTrack->DeleteLabel(index);
116 return result;
117 };
118 Intervals result;
119 std::transform(
120 // Reverse traversal may lessen the shifting-left in the label array
121 // responding to label deletion messages published by DeleteLabel
122 mMoving.rbegin(), mMoving.rend(), std::back_inserter(result),
123 moveLabel);
124 mMoving = Intervals{};
125 return result;
126 }
static size_t & GetIndex(ChannelGroupInterval &interval)
Intervals mMoving
std::vector< std::shared_ptr< ChannelGroupInterval > > Intervals

References GetIndex(), TrackShifter::mMoving, and mpTrack.

Here is the call graph for this function:

◆ DoHorizontalOffset()

void LabelTrackShifter::DoHorizontalOffset ( double  offset)
inlineoverridevirtual

Shift all moving intervals horizontally Default moves the whole track, provided !AllFixed(); else does nothing

Reimplemented from TrackShifter.

Definition at line 150 of file LabelTrackShifter.cpp.

151 {
152 auto &labels = mpTrack->GetLabels();
153 for (auto &pInterval : MovingIntervals()) {
154 auto index = GetIndex(*pInterval);
155 auto labelStruct = labels[index];
156 labelStruct.selectedRegion.move(offset);
157 mpTrack->SetLabel(index, labelStruct);
158 }
159
160 mpTrack->SortLabels(); // causes callback to OnLabelPermuted
161 }
const Intervals & MovingIntervals() const
Return special intervals of the track that may move.

References GetIndex(), TrackShifter::MovingIntervals(), and mpTrack.

Here is the call graph for this function:

◆ GetIndex() [1/2]

static size_t & LabelTrackShifter::GetIndex ( ChannelGroupInterval interval)
inlinestatic

Definition at line 37 of file LabelTrackShifter.cpp.

38 {
39 return static_cast<LabelTrack::Interval&>(interval).index;
40 }

Referenced by Detach(), DoHorizontalOffset(), GetIndex(), HitTest(), OnLabelAdded(), OnLabelDeleted(), and OnLabelPermuted().

Here is the caller graph for this function:

◆ GetIndex() [2/2]

static size_t LabelTrackShifter::GetIndex ( const ChannelGroupInterval interval)
inlinestatic

Definition at line 42 of file LabelTrackShifter.cpp.

43 {
44 return GetIndex(const_cast<ChannelGroupInterval&>(interval));
45 }
A start and an end time, and whatever else subclasses associate with them.
Definition: Channel.h:30

References GetIndex().

Here is the call graph for this function:

◆ GetTrack()

Track & LabelTrackShifter::GetTrack ( ) const
inlineoverridevirtual

Label track is always leader; satisfying the post.

Implements TrackShifter.

Definition at line 35 of file LabelTrackShifter.cpp.

35{ return *mpTrack; }

References mpTrack.

◆ HitTest()

HitTestResult LabelTrackShifter::HitTest ( double  time,
const ViewInfo viewInfo,
HitTestParams pParams 
)
inlineoverridevirtual

Decide how shift behaves, based on the track that is clicked in.

If the return value is Intervals or Selection, then some intervals may be marked moving as a side effect

Precondition
!pParams || (time == pParams->viewInfo.PositionToTime(pParams->xx, pParams->rect.x))
Parameters
timeA time value to test
pParamsOptional extra information

Implements TrackShifter.

Definition at line 47 of file LabelTrackShifter.cpp.

49 {
51 auto t0 = viewInfo.selectedRegion.t0();
52 auto t1 = viewInfo.selectedRegion.t1();
53 if ( mpTrack->IsSelected() && time >= t0 && time < t1 )
55
56 // Prefer the box that the mouse hovers over, else the selected one
57 int iLabel = -1;
58 if ( pParams )
59 iLabel =
60 LabelTrackView::OverATextBox(*mpTrack, pParams->xx, pParams->yy);
61 if (iLabel == -1)
63 if (iLabel != -1) {
64 UnfixIntervals([&](const auto &myInterval){
65 return GetIndex( myInterval ) == iLabel;
66 });
67 return result;
68 }
69 else {
70 // If the pick is within the selection, which overlaps some intervals,
71 // then move those intervals only
72 // Else move all labels (preserving the older beahvior of time shift)
73 if ( result == HitTestResult::Selection )
74 SelectInterval({ t0, t1 });
75 if (mMoving.empty())
77 else
78 return result;
79 }
80 }
void SelectInterval(const ChannelGroupInterval &interval) override
Notifies the shifter that a region is selected, so it may update its fixed and moving intervals.
static LabelTrackView & Get(LabelTrack &)
static int OverATextBox(const LabelTrack &track, int xx, int yy)
int GetNavigationIndex(AudacityProject &project) const
double t1() const
Definition: ViewInfo.h:36
double t0() const
Definition: ViewInfo.h:35
void UnfixIntervals(std::function< bool(const ChannelGroupInterval &)> pred)
Change intervals satisfying a predicate from fixed to moving.
HitTestResult
Possibilities for HitTest on the clicked track.
@ Selection
Shift chosen intervals of this track; may shift other tracks' intervals.
@ Intervals
Shift intervals only of selected track and sister channels.
@ Track
Shift selected track and sister channels only, as a whole.
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:215

References LabelTrackView::Get(), GetIndex(), LabelTrackView::GetNavigationIndex(), TrackShifter::Intervals, TrackShifter::mMoving, mProject, mpTrack, LabelTrackView::OverATextBox(), ViewInfo::selectedRegion, SelectInterval(), TrackShifter::Selection, NotifyingSelectedRegion::t0(), NotifyingSelectedRegion::t1(), TrackShifter::Track, TrackShifter::UnfixIntervals(), TrackShifter::HitTestParams::xx, and TrackShifter::HitTestParams::yy.

Here is the call graph for this function:

◆ MayMigrateTo()

bool LabelTrackShifter::MayMigrateTo ( Track otherTrack)
inlineoverridevirtual

Whether intervals may migrate to the other track, not yet checking all placement constraints

Precondition
otherTrack.IsLeader() Default implementation returns false

Reimplemented from TrackShifter.

Definition at line 89 of file LabelTrackShifter.cpp.

90 {
91 return CommonMayMigrateTo(otherTrack);
92 }
bool CommonMayMigrateTo(Track &otherTrack)

References TrackShifter::CommonMayMigrateTo().

Here is the call graph for this function:

◆ OnLabelAdded()

void LabelTrackShifter::OnLabelAdded ( const LabelTrackEvent e)
inlineprivate

Definition at line 192 of file LabelTrackShifter.cpp.

193 {
194 if (e.mpTrack.lock() != mpTrack)
195 return;
196
197 auto present = e.mPresentPosition;
198
199 // Avoid signed-unsigned comparison below!
200 if (present < 0) {
201 assert(false);
202 return;
203 }
204
205 auto update = [=](const auto &pInterval){
206 auto &index = GetIndex(*pInterval);
207 if (index >= present)
208 ++index;
209 };
210
211 std::for_each(mFixed.begin(), mFixed.end(), update);
212 std::for_each(mMoving.begin(), mMoving.end(), update);
213 }
Intervals mFixed
const std::weak_ptr< Track > mpTrack
Definition: LabelTrack.h:222

References GetIndex(), TrackShifter::mFixed, TrackShifter::mMoving, LabelTrackEvent::mPresentPosition, LabelTrackEvent::mpTrack, and mpTrack.

Here is the call graph for this function:

◆ OnLabelDeleted()

void LabelTrackShifter::OnLabelDeleted ( const LabelTrackEvent  e)
inlineprivate

Definition at line 215 of file LabelTrackShifter.cpp.

216 {
217 if (e.mpTrack.lock() != mpTrack)
218 return;
219
220 auto former = e.mFormerPosition;
221
222 // Avoid signed-unsigned comparison below!
223 if (former < 0) {
224 assert(false);
225 return;
226 }
227
228 auto update = [=](const auto &pInterval){
229 auto &index = GetIndex(*pInterval);
230 if (index > former)
231 --index;
232 else if (index == former)
233 // It should have been deleted first!
234 assert(false);
235 };
236
237 std::for_each(mFixed.begin(), mFixed.end(), update);
238 std::for_each(mMoving.begin(), mMoving.end(), update);
239 }

References GetIndex(), TrackShifter::mFixed, LabelTrackEvent::mFormerPosition, TrackShifter::mMoving, LabelTrackEvent::mpTrack, and mpTrack.

Here is the call graph for this function:

◆ OnLabelPermuted()

void LabelTrackShifter::OnLabelPermuted ( const LabelTrackEvent e)
inlineprivate

Definition at line 164 of file LabelTrackShifter.cpp.

165 {
166 if (e.mpTrack.lock() != mpTrack)
167 return;
168
169 auto former = e.mFormerPosition;
170 auto present = e.mPresentPosition;
171
172 // Avoid signed-unsigned comparison below!
173 if (former < 0 || present < 0) {
174 assert(false);
175 return;
176 }
177
178 auto update = [=](const auto &pInterval){
179 auto &index = GetIndex(*pInterval);
180 if (index == former)
181 index = present;
182 else if (former < index && index <= present)
183 -- index;
184 else if (former > index && index >= present)
185 ++ index;
186 };
187
188 std::for_each(mFixed.begin(), mFixed.end(), update);
189 std::for_each(mMoving.begin(), mMoving.end(), update);
190 }

References GetIndex(), TrackShifter::mFixed, LabelTrackEvent::mFormerPosition, TrackShifter::mMoving, LabelTrackEvent::mPresentPosition, LabelTrackEvent::mpTrack, and mpTrack.

Here is the call graph for this function:

◆ SelectInterval()

void LabelTrackShifter::SelectInterval ( const ChannelGroupInterval interval)
inlineoverridevirtual

Notifies the shifter that a region is selected, so it may update its fixed and moving intervals.

Default behavior: if any part of the track is selected, unfix all parts of it.

Reimplemented from TrackShifter.

Definition at line 82 of file LabelTrackShifter.cpp.

83 {
84 CommonSelectInterval(interval);
85 }
void CommonSelectInterval(const ChannelGroupInterval &interval)

References TrackShifter::CommonSelectInterval().

Referenced by HitTest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SyncLocks()

bool LabelTrackShifter::SyncLocks ( )
inlineoverridevirtual

Whether unfixing of an interval should propagate to all overlapping intervals in the sync lock group.

Implements TrackShifter.

Definition at line 87 of file LabelTrackShifter.cpp.

87{ return false; }

Member Data Documentation

◆ mProject

AudacityProject& LabelTrackShifter::mProject
private

Definition at line 243 of file LabelTrackShifter.cpp.

Referenced by HitTest().

◆ mpTrack

const std::shared_ptr<LabelTrack> LabelTrackShifter::mpTrack
private

◆ mSubscription

Observer::Subscription LabelTrackShifter::mSubscription
private

Definition at line 241 of file LabelTrackShifter.cpp.

Referenced by LabelTrackShifter().


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