Audacity  3.2.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
SnapManager Class Reference

#include <Snap.h>

Collaboration diagram for SnapManager:
[legend]

Public Member Functions

 SnapManager (const AudacityProject &project, SnapPointArray candidates, const ZoomInfo &zoomInfo, bool noTimeSnap=false, int pixelTolerance=kPixelTolerance)
 Construct only for specified candidate points. More...
 
 SnapManager (const AudacityProject &project, const TrackList &tracks, const ZoomInfo &zoomInfo, SnapPointArray candidates={}, bool noTimeSnap=false, int pixelTolerance=kPixelTolerance)
 
 ~SnapManager ()
 
SnapResults Snap (Track *currentTrack, double t, bool rightEdge)
 

Static Public Member Functions

static const TranslatableStringsGetSnapLabels ()
 
static void Draw (wxDC *dc, wxInt64 snap0, wxInt64 snap1)
 

Private Member Functions

void Reinit ()
 
void CondListAdd (double t, const Track *track)
 
double Get (size_t index)
 
wxInt64 PixelDiff (double t, size_t index)
 
size_t Find (double t, size_t i0, size_t i1)
 
size_t Find (double t)
 
bool SnapToPoints (Track *currentTrack, double t, bool rightEdge, double *outT)
 

Private Attributes

const AudacityProjectmProject
 
const ZoomInfomZoomInfo
 
int mPixelTolerance
 
bool mNoTimeSnap
 
double mEpsilon { 1 / 44100.0 }
 Two time points closer than this are considered the same. More...
 
SnapPointArray mCandidates
 
SnapPointArray mSnapPoints
 
NumericConverter mConverter
 
bool mSnapToTime { false }
 
int mSnapTo { 0 }
 
double mRate { 0.0 }
 
NumericFormatSymbol mFormat {}
 

Detailed Description

Definition at line 55 of file Snap.h.

Constructor & Destructor Documentation

◆ SnapManager() [1/2]

SnapManager::SnapManager ( const AudacityProject project,
SnapPointArray  candidates,
const ZoomInfo zoomInfo,
bool  noTimeSnap = false,
int  pixelTolerance = kPixelTolerance 
)

Construct only for specified candidate points.

Definition at line 28 of file Snap.cpp.

33 : mProject{ &project }
34 , mZoomInfo{ &zoomInfo }
35 , mPixelTolerance{ pixelTolerance }
36 , mNoTimeSnap{ noTimeSnap }
37 , mCandidates{ move( candidates ) }
38 , mSnapPoints{}
40 {
41  Reinit();
42 }

◆ SnapManager() [2/2]

SnapManager::SnapManager ( const AudacityProject project,
const TrackList tracks,
const ZoomInfo zoomInfo,
SnapPointArray  candidates = {},
bool  noTimeSnap = false,
int  pixelTolerance = kPixelTolerance 
)

Construct for (optionally) specified points, plus significant points on the tracks in the given list

Definition at line 60 of file Snap.cpp.

66  : SnapManager{ project,
67  // Add candidates to given ones by default rules,
68  // then delegate to other ctor
69  FindCandidates( move(candidates), tracks ),
70  zoomInfo, noTimeSnap, pixelTolerance }
71 {
72 }

References anonymous_namespace{Snap.cpp}::FindCandidates().

Here is the call graph for this function:

◆ ~SnapManager()

SnapManager::~SnapManager ( )

Definition at line 74 of file Snap.cpp.

75 {
76 }

Member Function Documentation

◆ CondListAdd()

void SnapManager::CondListAdd ( double  t,
const Track track 
)
private

Definition at line 121 of file Snap.cpp.

122 {
123  if (mSnapToTime)
124  {
125  mConverter.SetValue(t);
126  }
127 
128  if (!mSnapToTime || mConverter.GetValue() == t)
129  {
130  mSnapPoints.push_back(SnapPoint{ t, track });
131  }
132 }

References NumericConverter::GetValue(), mConverter, mSnapPoints, mSnapToTime, and NumericConverter::SetValue().

Referenced by Reinit().

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

◆ Draw()

void SnapManager::Draw ( wxDC *  dc,
wxInt64  snap0,
wxInt64  snap1 
)
static

Definition at line 330 of file Snap.cpp.

331 {
333  if ( snap0 >= 0 ) {
334  AColor::Line(*dc, (int)snap0, 0, (int)snap0, 30000);
335  }
336  if ( snap1 >= 0 ) {
337  AColor::Line(*dc, (int)snap1, 0, (int)snap1, 30000);
338  }
339 }

References AColor::Line(), and AColor::SnapGuidePen().

Referenced by WaveClipTrimHandle::AdjustBorder::Draw().

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

◆ Find() [1/2]

size_t SnapManager::Find ( double  t)
private

Definition at line 167 of file Snap.cpp.

168 {
169  size_t cnt = mSnapPoints.size();
170  size_t index = Find(t, 0, cnt);
171 
172  // At this point, either index is the closest, or the next one
173  // to the right is. Keep moving to the right until we get a
174  // different value
175  size_t next = index + 1;
176  while (next + 1 < cnt && Get(next) == Get(index))
177  {
178  next++;
179  }
180 
181  // Now return whichever one is closer to time t
182  if (next < cnt && PixelDiff(t, next) < PixelDiff(t, index))
183  {
184  return next;
185  }
186 
187  return index;
188 }

References Find(), Get(), mSnapPoints, and PixelDiff().

Here is the call graph for this function:

◆ Find() [2/2]

size_t SnapManager::Find ( double  t,
size_t  i0,
size_t  i1 
)
private

Definition at line 149 of file Snap.cpp.

150 {
151  if (i1 <= i0 + 1)
152  {
153  return i0;
154  }
155 
156  size_t half = (i0 + i1) / 2;
157 
158  if (t < Get(half))
159  {
160  return Find(t, i0, half);
161  }
162 
163  return Find(t, half, i1);
164 }

References Get().

Referenced by Find(), and SnapToPoints().

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

◆ Get()

double SnapManager::Get ( size_t  index)
private

Definition at line 135 of file Snap.cpp.

136 {
137  return mSnapPoints[index].t;
138 }

References mSnapPoints.

Referenced by Find(), PixelDiff(), and SnapToPoints().

Here is the caller graph for this function:

◆ GetSnapLabels()

const TranslatableStrings & SnapManager::GetSnapLabels ( )
static

Definition at line 318 of file Snap.cpp.

319 {
320  static const TranslatableStrings result{
321  XO("Off") ,
322  XO("Nearest") ,
323  XO("Prior") ,
324  };
325  return result;
326 }

References XO.

Referenced by SelectionBar::Populate().

Here is the caller graph for this function:

◆ PixelDiff()

wxInt64 SnapManager::PixelDiff ( double  t,
size_t  index 
)
private

Definition at line 141 of file Snap.cpp.

142 {
143  return std::abs(mZoomInfo->TimeToPosition(t, 0) -
144  mZoomInfo->TimeToPosition(Get(index), 0));
145 }

References Get(), mZoomInfo, and ZoomInfo::TimeToPosition().

Referenced by Find(), and SnapToPoints().

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

◆ Reinit()

void SnapManager::Reinit ( )
private

Definition at line 78 of file Snap.cpp.

79 {
80  const auto &settings = ProjectSettings::Get( *mProject );
81  int snapTo = settings.GetSnapTo();
82  auto rate = ProjectRate::Get(*mProject).GetRate();
83  auto format = settings.GetSelectionFormat();
84 
85  // No need to reinit if these are still the same
86  if (snapTo == mSnapTo && rate == mRate && format == mFormat)
87  {
88  return;
89  }
90 
91  // Save NEW settings
92  mSnapTo = snapTo;
93  mRate = rate;
94  mFormat = format;
95 
96  mSnapPoints.clear();
97 
98  // Grab time-snapping prefs (unless otherwise requested)
99  mSnapToTime = false;
100 
101  // Look up the format string
102  if (mSnapTo != SNAP_OFF && !mNoTimeSnap)
103  {
104  mSnapToTime = true;
107  }
108 
109  // Add a SnapPoint at t=0
110  mSnapPoints.push_back(SnapPoint{});
111 
112  // Adjust and filter the candidate points
113  for (const auto &candidate : mCandidates)
114  CondListAdd( candidate.t, candidate.track );
115 
116  // Sort all by time
117  std::sort(mSnapPoints.begin(), mSnapPoints.end());
118 }

References CondListAdd(), format, ProjectRate::Get(), ProjectSettings::Get(), ProjectRate::GetRate(), mCandidates, mConverter, mFormat, mNoTimeSnap, mProject, mRate, mSnapPoints, mSnapTo, mSnapToTime, NumericConverter::SetFormatName(), NumericConverter::SetSampleRate(), settings(), and SNAP_OFF.

Referenced by Snap().

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

◆ Snap()

SnapResults SnapManager::Snap ( Track currentTrack,
double  t,
bool  rightEdge 
)

Definition at line 272 of file Snap.cpp.

274 {
275 
276  SnapResults results;
277  // Check to see if we need to reinitialize
278  Reinit();
279 
280  results.timeSnappedTime = results.outTime = t;
281  results.outCoord = mZoomInfo->TimeToPosition(t);
282 
283  // First snap to points in mSnapPoints
284  results.snappedPoint =
285  SnapToPoints(currentTrack, t, rightEdge, &results.outTime);
286 
287  if (mSnapToTime) {
288  // Find where it would snap time to the grid
290  t,
292  );
294  results.timeSnappedTime = mConverter.GetValue();
295  }
296 
297  results.snappedTime = false;
298  if (mSnapToTime)
299  {
300  if (results.snappedPoint)
301  {
302  // Since mSnapPoints only contains points on the grid, we're done
303  results.snappedTime = true;
304  }
305  else
306  {
307  results.outTime = results.timeSnappedTime;
308  results.snappedTime = true;
309  }
310  }
311 
312  if (results.Snapped())
313  results.outCoord = mZoomInfo->TimeToPosition(results.outTime);
314 
315  return results;
316 }

References NumericConverter::ControlsToValue(), ProjectSettings::Get(), ProjectSettings::GetSnapTo(), NumericConverter::GetValue(), mConverter, mSnapToTime, mZoomInfo, SnapResults::outCoord, SnapResults::outTime, Reinit(), SNAP_NEAREST, SnapResults::Snapped(), SnapResults::snappedPoint, SnapResults::snappedTime, SnapToPoints(), SnapResults::timeSnappedTime, ZoomInfo::TimeToPosition(), and NumericConverter::ValueToControls().

Referenced by anonymous_namespace{TimeShiftHandle.cpp}::FindDesiredSlideAmount().

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

◆ SnapToPoints()

bool SnapManager::SnapToPoints ( Track currentTrack,
double  t,
bool  rightEdge,
double *  outT 
)
private

Definition at line 191 of file Snap.cpp.

195 {
196  *outT = t;
197 
198  size_t cnt = mSnapPoints.size();
199  if (cnt == 0)
200  {
201  return false;
202  }
203 
204  // Find the nearest SnapPoint
205  size_t index = Find(t);
206 
207  // If it's too far away, just give up now
208  if (PixelDiff(t, index) >= mPixelTolerance)
209  {
210  return false;
211  }
212 
213  // Otherwise, search left and right for all of the points
214  // within the allowed range.
215  size_t left = index;
216  size_t right = index;
217  size_t i;
218 
219  while (left > 0 && PixelDiff(t, left - 1) < mPixelTolerance)
220  {
221  left--;
222  }
223 
224  while (right < cnt - 1 && PixelDiff(t, right + 1) < mPixelTolerance)
225  {
226  right++;
227  }
228 
229  if (left == index && right == index)
230  {
231  // Awesome, there's only one point that matches!
232  *outT = Get(index);
233  return true;
234  }
235 
236  size_t indexInThisTrack = 0;
237  size_t countInThisTrack = 0;
238  for (i = left; i <= right; ++i)
239  {
240  if (mSnapPoints[i].track == currentTrack)
241  {
242  indexInThisTrack = i;
243  countInThisTrack++;
244  }
245  }
246 
247  if (countInThisTrack == 1)
248  {
249  // Cool, only one of the points is in the same track, so
250  // we'll use that one.
251  *outT = Get(indexInThisTrack);
252  return true;
253  }
254 
255  if (Get(right) - Get(left) < mEpsilon)
256  {
257  // OK, they're basically the same point
258  if (rightEdge)
259  {
260  *outT = Get(right); // Return rightmost
261  }
262  else {
263  *outT = Get(left); // Return leftmost
264  }
265  return true;
266  }
267 
268  // None of the points matched, bummer.
269  return false;
270 }

References Find(), Get(), mEpsilon, mPixelTolerance, mSnapPoints, and PixelDiff().

Referenced by Snap().

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

Member Data Documentation

◆ mCandidates

SnapPointArray SnapManager::mCandidates
private

Definition at line 108 of file Snap.h.

Referenced by Reinit().

◆ mConverter

NumericConverter SnapManager::mConverter
private

Definition at line 112 of file Snap.h.

Referenced by CondListAdd(), Reinit(), and Snap().

◆ mEpsilon

double SnapManager::mEpsilon { 1 / 44100.0 }
private

Two time points closer than this are considered the same.

Definition at line 107 of file Snap.h.

Referenced by SnapToPoints().

◆ mFormat

NumericFormatSymbol SnapManager::mFormat {}
private

Definition at line 117 of file Snap.h.

Referenced by Reinit().

◆ mNoTimeSnap

bool SnapManager::mNoTimeSnap
private

Definition at line 104 of file Snap.h.

Referenced by Reinit().

◆ mPixelTolerance

int SnapManager::mPixelTolerance
private

Definition at line 103 of file Snap.h.

Referenced by SnapToPoints().

◆ mProject

const AudacityProject* SnapManager::mProject
private

Definition at line 101 of file Snap.h.

Referenced by Reinit().

◆ mRate

double SnapManager::mRate { 0.0 }
private

Definition at line 116 of file Snap.h.

Referenced by Reinit().

◆ mSnapPoints

SnapPointArray SnapManager::mSnapPoints
private

Definition at line 109 of file Snap.h.

Referenced by CondListAdd(), Find(), Get(), Reinit(), and SnapToPoints().

◆ mSnapTo

int SnapManager::mSnapTo { 0 }
private

Definition at line 115 of file Snap.h.

Referenced by Reinit().

◆ mSnapToTime

bool SnapManager::mSnapToTime { false }
private

Definition at line 113 of file Snap.h.

Referenced by CondListAdd(), Reinit(), and Snap().

◆ mZoomInfo

const ZoomInfo* SnapManager::mZoomInfo
private

Definition at line 102 of file Snap.h.

Referenced by PixelDiff(), and Snap().


The documentation for this class was generated from the following files:
ProjectSettings::GetSnapTo
int GetSnapTo() const
Definition: ProjectSettings.cpp:168
SNAP_OFF
@ SNAP_OFF
Definition: ProjectSettings.h:31
SnapManager::mRate
double mRate
Definition: Snap.h:116
NumericConverter::SetFormatName
bool SetFormatName(const NumericFormatSymbol &formatName)
Definition: NumericTextCtrl.cpp:1111
TranslatableStrings
std::vector< TranslatableString > TranslatableStrings
Definition: TranslatableString.h:295
SnapManager::mConverter
NumericConverter mConverter
Definition: Snap.h:112
SnapManager::Get
double Get(size_t index)
Definition: Snap.cpp:135
ProjectRate::Get
static ProjectRate & Get(AudacityProject &project)
Definition: ProjectRate.cpp:28
SnapResults::snappedTime
bool snappedTime
Definition: Snap.h:50
AColor::Line
static void Line(wxDC &dc, wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
Definition: AColor.cpp:109
XO
#define XO(s)
Definition: Internat.h:31
ProjectSettings::Get
static ProjectSettings & Get(AudacityProject &project)
Definition: ProjectSettings.cpp:45
SnapManager::mFormat
NumericFormatSymbol mFormat
Definition: Snap.h:117
SnapManager::mZoomInfo
const ZoomInfo * mZoomInfo
Definition: Snap.h:102
NumericConverter::ValueToControls
virtual void ValueToControls()
Definition: NumericTextCtrl.cpp:949
SnapResults
Definition: Snap.h:45
ZoomInfo::TimeToPosition
wxInt64 TimeToPosition(double time, wxInt64 origin=0, bool ignoreFisheye=false) const
STM: Converts a project time to screen x position.
Definition: ZoomInfo.cpp:51
SnapResults::outTime
double outTime
Definition: Snap.h:47
SNAP_NEAREST
@ SNAP_NEAREST
Definition: ProjectSettings.h:32
anonymous_namespace{Snap.cpp}::FindCandidates
SnapPointArray FindCandidates(SnapPointArray candidates, const TrackList &tracks)
Definition: Snap.cpp:45
SnapResults::timeSnappedTime
double timeSnappedTime
Definition: Snap.h:46
NumericConverter::ControlsToValue
virtual void ControlsToValue()
Definition: NumericTextCtrl.cpp:1060
SnapResults::Snapped
bool Snapped() const
Definition: Snap.h:52
AColor::SnapGuidePen
static void SnapGuidePen(wxDC *dc)
Definition: AColor.cpp:396
NumericConverter::TIME
@ TIME
Definition: NumericTextCtrl.h:52
SnapResults::outCoord
wxInt64 outCoord
Definition: Snap.h:48
format
int format
Definition: ExportPCM.cpp:56
ProjectRate::GetRate
double GetRate() const
Definition: ProjectRate.cpp:53
SnapManager::Find
size_t Find(double t, size_t i0, size_t i1)
Definition: Snap.cpp:149
SnapManager::mNoTimeSnap
bool mNoTimeSnap
Definition: Snap.h:104
SnapManager::SnapToPoints
bool SnapToPoints(Track *currentTrack, double t, bool rightEdge, double *outT)
Definition: Snap.cpp:191
NumericConverter::SetValue
void SetValue(double newValue)
Definition: NumericTextCtrl.cpp:1138
SnapManager::CondListAdd
void CondListAdd(double t, const Track *track)
Definition: Snap.cpp:121
SnapManager::mSnapToTime
bool mSnapToTime
Definition: Snap.h:113
SnapManager
Definition: Snap.h:56
SnapManager::mSnapTo
int mSnapTo
Definition: Snap.h:115
SnapManager::mSnapPoints
SnapPointArray mSnapPoints
Definition: Snap.h:109
SnapResults::snappedPoint
bool snappedPoint
Definition: Snap.h:49
SnapPoint
Definition: Snap.h:31
NumericConverter::SetSampleRate
void SetSampleRate(double sampleRate)
Definition: NumericTextCtrl.cpp:1130
SnapManager::Reinit
void Reinit()
Definition: Snap.cpp:78
SnapManager::mPixelTolerance
int mPixelTolerance
Definition: Snap.h:103
SnapManager::PixelDiff
wxInt64 PixelDiff(double t, size_t index)
Definition: Snap.cpp:141
settings
static Settings & settings()
Definition: TrackInfo.cpp:87
SnapManager::mCandidates
SnapPointArray mCandidates
Definition: Snap.h:108
NumericConverter::GetValue
double GetValue()
Definition: NumericTextCtrl.cpp:1174
SnapManager::mProject
const AudacityProject * mProject
Definition: Snap.h:101
SnapManager::mEpsilon
double mEpsilon
Two time points closer than this are considered the same.
Definition: Snap.h:107