File: SyncLock.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (163 lines) | stat: -rw-r--r-- 4,165 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**********************************************************************

Audacity: A Digital Audio Editor

@file SyncLock.cpp
@brief implements sync-lock logic

Paul Licameli split from Track.cpp

**********************************************************************/

#include "SyncLock.h"

#include "PendingTracks.h"
#include "Prefs.h"
#include "Project.h"
#include "Track.h"

static const AudacityProject::AttachedObjects::RegisteredFactory
sSyncLockStateKey{
  []( AudacityProject &project ){
     auto result = std::make_shared< SyncLockState >( project );
     return result;
   }
};

SyncLockState &SyncLockState::Get( AudacityProject &project )
{
   return project.AttachedObjects::Get< SyncLockState >(
      sSyncLockStateKey );
}

const SyncLockState &SyncLockState::Get( const AudacityProject &project )
{
   return Get( const_cast< AudacityProject & >( project ) );
}

SyncLockState::SyncLockState(AudacityProject &project)
   : mProject{project}
   , mIsSyncLocked(SyncLockTracks.Read())
{
}

bool SyncLockState::IsSyncLocked() const
{
   return mIsSyncLocked;
}

void SyncLockState::SetSyncLock(bool flag)
{
   if (flag != mIsSyncLocked) {
      mIsSyncLocked = flag;
      Publish({ flag });
   }
}

namespace {
inline bool IsSyncLockableNonSeparatorTrack(const Track &track)
{
   return GetSyncLockPolicy::Call(track) == SyncLockPolicy::Grouped;
}

inline bool IsSeparatorTrack(const Track &track)
{
   return GetSyncLockPolicy::Call(track) == SyncLockPolicy::EndSeparator;
}

bool IsGoodNextSyncLockTrack(const Track &t, bool inSeparatorSection)
{
   const bool isSeparator = IsSeparatorTrack(t);
   if (inSeparatorSection)
      return isSeparator;
   else if (isSeparator)
      return true;
   else
      return IsSyncLockableNonSeparatorTrack(t);
}
}

bool SyncLock::IsSyncLockSelected(const Track &track)
{
   auto pList = track.GetOwner();
   if (!pList)
      return false;

   auto p = pList->GetOwner();
   if (!p || !SyncLockState::Get( *p ).IsSyncLocked())
      return false;

   auto &orig = PendingTracks::Get(*p).SubstituteOriginalTrack(track);
   auto trackRange = Group(orig);

   if (trackRange.size() <= 1) {
      // Not in a sync-locked group.
      // Return true iff selected and of a sync-lockable type.
      return (IsSyncLockableNonSeparatorTrack(orig) ||
         IsSeparatorTrack(orig)) && track.GetSelected();
   }

   // Return true iff any track in the group is selected.
   return *(trackRange + &Track::IsSelected).begin();
}

bool SyncLock::IsSelectedOrSyncLockSelected(const Track &track)
{
   return track.IsSelected() || IsSyncLockSelected(track);
}

namespace {
std::pair<Track *, Track *> FindSyncLockGroup(Track &member)
{
   // A non-trivial sync-locked group is a maximal sub-sequence of the tracks
   // consisting of any positive number of audio tracks followed by zero or
   // more label tracks.

   // Step back through any label tracks.
   auto pList = member.GetOwner();
   auto ppMember = pList->Find(&member);
   while (*ppMember && IsSeparatorTrack(**ppMember))
      --ppMember;

   // Step back through the wave and note tracks before the label tracks.
   Track *first = nullptr;
   while (*ppMember && IsSyncLockableNonSeparatorTrack(**ppMember)) {
      first = *ppMember;
      --ppMember;
   }

   if (!first)
      // Can't meet the criteria described above.  In that case,
      // consider the track to be the sole member of a group.
      return { &member, &member };

   auto last = pList->Find(first);
   auto next = last;
   bool inLabels = false;

   while (*++next) {
      if (!IsGoodNextSyncLockTrack(**next, inLabels))
         break;
      last = next;
      inLabels = IsSeparatorTrack(**last);
   }

   return { first, *last };
}

}

TrackIterRange<Track> SyncLock::Group(Track &track)
{
   auto pList = track.GetOwner();
   assert(pList); // precondition
   auto tracks = FindSyncLockGroup(const_cast<Track&>(track));
   return pList->Any()
      .StartingWith(tracks.first).EndingAfter(tracks.second);
}

DEFINE_ATTACHED_VIRTUAL(GetSyncLockPolicy) {
   return [](auto&){ return SyncLockPolicy::Isolated; };
}

BoolSetting SyncLockTracks{ "/GUI/SyncLockTracks", false };