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
 There is always an associated track. 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 (TimeInterval 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 (TimeInterval 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 (TimeInterval 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:98
AudacityProject & mProject
const std::shared_ptr< LabelTrack > mpTrack
Observer::Subscription mSubscription
std::shared_ptr< Subclass > SharedPointer()
Definition: Track.h:146
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 130 of file LabelTrackShifter.cpp.

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

◆ 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 137 of file LabelTrackShifter.cpp.

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

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 110 of file LabelTrackShifter.cpp.

111 {
112 auto pTrack = mpTrack.get();
113 auto moveLabel = [pTrack](const auto &pInterval) {
114 auto index = GetIndex(*pInterval);
115 auto result = std::make_shared<MovingInterval>(
116 pInterval->Start(), pInterval->End(), *pTrack->GetLabel(index));
117 pTrack->DeleteLabel(index);
118 return result;
119 };
120 Intervals result;
121 std::transform(
122 // Reverse traversal may lessen the shifting-left in the label array
123 // responding to label deletion messages published by DeleteLabel
124 mMoving.rbegin(), mMoving.rend(), std::back_inserter(result),
125 moveLabel);
126 mMoving = Intervals{};
127 return result;
128 }
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 152 of file LabelTrackShifter.cpp.

153 {
154 auto &labels = mpTrack->GetLabels();
155 for (auto &pInterval : MovingIntervals()) {
156 auto index = GetIndex(*pInterval);
157 auto labelStruct = labels[index];
158 labelStruct.selectedRegion.move(offset);
159 mpTrack->SetLabel(index, labelStruct);
160 }
161
162 mpTrack->SortLabels(); // causes callback to OnLabelPermuted
163 }
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 36 of file LabelTrackShifter.cpp.

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

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 41 of file LabelTrackShifter.cpp.

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

References GetIndex().

Here is the call graph for this function:

◆ GetTrack()

Track & LabelTrackShifter::GetTrack ( ) const
inlineoverridevirtual

There is always an associated track.

Implements TrackShifter.

Definition at line 34 of file LabelTrackShifter.cpp.

34{ 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 46 of file LabelTrackShifter.cpp.

48 {
50 auto t0 = viewInfo.selectedRegion.t0();
51 auto t1 = viewInfo.selectedRegion.t1();
52 if ( mpTrack->IsSelected() && time >= t0 && time < t1 )
54
55 // Prefer the box that the mouse hovers over, else the selected one
56 int iLabel = -1;
57 if ( pParams )
58 iLabel =
59 LabelTrackView::OverATextBox(*mpTrack, pParams->xx, pParams->yy);
60 if (iLabel == -1)
62 if (iLabel != -1) {
63 UnfixIntervals([&](const auto &myInterval){
64 return GetIndex( myInterval ) == iLabel;
65 });
66 return result;
67 }
68 else {
69 // If the pick is within the selection, which overlaps some intervals,
70 // then move those intervals only
71 // Else move all labels (preserving the older beahvior of time shift)
72 if ( result == HitTestResult::Selection )
73 SelectInterval({ t0, t1 });
74 if (mMoving.empty())
76 else
77 return result;
78 }
79 }
void SelectInterval(TimeInterval 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:216

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

Default implementation returns false

Reimplemented from TrackShifter.

Definition at line 88 of file LabelTrackShifter.cpp.

89 {
90 return CommonMayMigrateTo(otherTrack);
91 }
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 194 of file LabelTrackShifter.cpp.

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

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 217 of file LabelTrackShifter.cpp.

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

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 166 of file LabelTrackShifter.cpp.

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

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 ( TimeInterval  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 81 of file LabelTrackShifter.cpp.

82 {
83 CommonSelectInterval(interval);
84 }
void CommonSelectInterval(TimeInterval 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 86 of file LabelTrackShifter.cpp.

86{ return false; }

Member Data Documentation

◆ mProject

AudacityProject& LabelTrackShifter::mProject
private

Definition at line 245 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 243 of file LabelTrackShifter.cpp.

Referenced by LabelTrackShifter().


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