Audacity 3.2.0
TrackUtilities.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 TrackUtilities.cpp
6
7 Paul Licameli split from TrackMenus.cpp
8
9 **********************************************************************/
10
11#include "TrackUtilities.h"
12
13#include "PlayableTrack.h"
14#include "ProjectHistory.h"
15#include "TrackFocus.h"
16#include "TrackPanel.h"
17#include "Viewport.h"
18
21 auto &trackPanel = TrackPanel::Get(project);
22
23 auto range = tracks.Selected();
24 using Iter = decltype(range.begin());
25
26 // Find the track preceding the first removed track
27 std::optional<Iter> focus;
28 if (!range.empty()) {
29 auto iter = tracks.Find(*range.begin());
30 // TrackIter allows decrement even of begin iterators
31 focus.emplace(--iter);
32 }
33
34 while (!range.empty())
35 tracks.Remove(**range.first++);
36
37 if (!(focus.has_value() && **focus))
38 // try to use the last track
39 focus.emplace(tracks.end().advance(-1));
40 assert(focus);
41 Track *f = **focus;
42 // Try to use the first track after the removal
43 // TrackIter allows increment even of end iterators
44 if (const auto nextF = * ++ *focus)
45 f = nextF;
46
47 // If we actually have something left, then set focus and make sure it's seen
48 if (f) {
51 }
52
54 .PushState(XO("Removed audio track(s)"), XO("Remove Track"));
55
56 trackPanel.UpdateViewIfNoTracks();
57}
58
60 AudacityProject &project, Track &track, bool exclusive)
61{
63
64 // "exclusive" mute means mute the chosen track and unmute all others.
65 if (exclusive) {
66 for (auto playable : tracks.Any<PlayableTrack>()) {
67 bool chosen = (&track == playable);
68 playable->SetMute(chosen);
69 playable->SetSolo(false);
70 }
71 }
72 else {
73 // Normal click toggles this track.
74 auto pt = dynamic_cast<PlayableTrack *>(&track);
75 if (!pt)
76 return;
77
78 bool wasMute = pt->GetMute();
79 pt->SetMute(!wasMute);
80
81 if (auto value = TracksBehaviorsSolo.ReadEnum();
82 value == SoloBehaviorSimple)
83 {
84 // We also set a solo indicator if we have just one track / stereo pair playing.
85 // in a group of more than one playable tracks.
86 // otherwise clear solo on everything.
87
88 auto range = tracks.Any<PlayableTrack>();
89 auto nPlayableTracks = range.size();
90 auto nPlaying = (range - &PlayableTrack::GetMute).size();
91 for (auto track : range)
92 track->SetSolo((nPlaying == 1) &&
93 (nPlayableTracks > 1) && !track->GetMute());
94 }
95 }
97
98 TrackFocus::Get( project ).UpdateAccessibility();
99}
100
102 AudacityProject &project, Track &track, bool exclusive)
103{
104 auto &tracks = TrackList::Get( project );
105
106 const auto pt = dynamic_cast<PlayableTrack *>(&track);
107 if (!pt)
108 return;
109 bool bWasSolo = pt->GetSolo();
110
111 bool simple = (TracksBehaviorsSolo.ReadEnum() == SoloBehaviorSimple);
112 bool bSoloMultiple = !simple ^ exclusive;
113
114 // Standard and Simple solo have opposite defaults:
115 // Standard - Behaves as individual buttons, shift=radio buttons
116 // Simple - Behaves as radio buttons, shift=individual
117 // In addition, Simple solo will mute/unmute tracks
118 // when in standard radio button mode.
119 if (bSoloMultiple)
120 pt->SetSolo(!bWasSolo);
121 else {
122 // Normal click solo this track only, mute everything else.
123 // OR unmute and unsolo everything.
124 for (auto playable : tracks.Any<PlayableTrack>()) {
125 bool chosen = (&track == playable);
126 if (chosen) {
127 playable->SetSolo(!bWasSolo);
128 if (simple)
129 playable->SetMute(false);
130 }
131 else {
132 playable->SetSolo(false);
133 if (simple)
134 playable->SetMute(!bWasSolo);
135 }
136 }
137 }
139
140 TrackFocus::Get( project ).UpdateAccessibility();
141}
142
144{
146 auto &trackFocus = TrackFocus::Get(project);
147
148 const auto iter = tracks.Find(&toRemove);
149
150 // If it was focused, then NEW focus is the next or, if
151 // unavailable, the previous track. (The NEW focus is set
152 // after the track has been removed.)
153 bool toRemoveWasFocused = trackFocus.Get() == *iter;
154 std::optional<decltype(iter)> newFocus{};
155 if (toRemoveWasFocused) {
156 auto iterNext = iter,
157 iterPrev = iter;
158 newFocus.emplace(++iterNext);
159 if (!**newFocus)
160 newFocus.emplace(--iterPrev);
161 }
162
163 wxString name = toRemove.GetName();
164
165 tracks.Remove(**iter);
166
167 if (toRemoveWasFocused)
168 trackFocus.Set(**newFocus);
169
171 XO("Removed track '%s'.").Format(name),
172 XO("Track Remove"));
173}
174
176 AudacityProject &project, Track& target, MoveChoice choice)
177{
178 auto &tracks = TrackList::Get( project );
179
180 TranslatableString longDesc, shortDesc;
181
182 switch (choice)
183 {
184 case OnMoveTopID:
185 /* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
186 longDesc = XO("Moved '%s' to Top");
187 shortDesc = XO("Move Track to Top");
188
189 // TODO: write TrackList::Rotate to do this in one step and avoid emitting
190 // an event for each swap
191 while (tracks.CanMoveUp(target))
192 tracks.Move(target, true);
193
194 break;
195 case OnMoveBottomID:
196 /* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
197 longDesc = XO("Moved '%s' to Bottom");
198 shortDesc = XO("Move Track to Bottom");
199
200 // TODO: write TrackList::Rotate to do this in one step and avoid emitting
201 // an event for each swap
202 while (tracks.CanMoveDown(target))
203 tracks.Move(target, false);
204
205 break;
206 default:
207 bool bUp = (OnMoveUpID == choice);
208
209 tracks.Move(target, bUp);
210 longDesc =
211 /* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
212 bUp? XO("Moved '%s' Up")
213 : XO("Moved '%s' Down");
214 shortDesc =
215 /* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
216 bUp? XO("Move Track Up")
217 : XO("Move Track Down");
218
219 }
220
221 longDesc.Format(target.GetName());
222
223 ProjectHistory::Get( project ).PushState(longDesc, shortDesc);
224}
@ OnMoveBottomID
@ OnMoveTopID
@ OnMoveUpID
const TranslatableString name
Definition: Distortion.cpp:76
XO("Cut/Copy/Paste")
EnumSetting< SoloBehavior > TracksBehaviorsSolo
Extends Track with notions of mute and solo setting.
@ SoloBehaviorSimple
Definition: PlayableTrack.h:70
const auto tracks
const auto project
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
size_t size() const
How many attachment pointers are in the Site.
Definition: ClientData.h:260
Abstract base class used in importing a file.
AudioTrack subclass that can also be audibly replayed by the program.
Definition: PlayableTrack.h:40
bool GetSolo() const
Definition: PlayableTrack.h:48
bool GetMute() const
Definition: PlayableTrack.h:47
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
void ModifyState(bool bWantsAutoSave)
static ProjectHistory & Get(AudacityProject &project)
Track * Get()
Definition: TrackFocus.cpp:156
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:110
const wxString & GetName() const
Name is always the same for all channels of a group.
Definition: Track.cpp:64
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:314
static TrackPanel & Get(AudacityProject &project)
Definition: TrackPanel.cpp:234
Holds a msgid for the translation catalog; may also bind format arguments.
TranslatableString & Format(Args &&...args) &
Capture variadic format arguments (by copy) when there is no plural.
void ShowTrack(const Track &track)
Definition: Viewport.cpp:456
static Viewport & Get(AudacityProject &project)
Definition: Viewport.cpp:33
AUDACITY_DLL_API void DoMoveTrack(AudacityProject &project, Track &target, MoveChoice choice)
Move a track up, down, to top or to bottom.
AUDACITY_DLL_API void DoRemoveTrack(AudacityProject &project, Track &toRemove)
AUDACITY_DLL_API void DoTrackSolo(AudacityProject &project, Track &track, bool exclusive)
AUDACITY_DLL_API void DoTrackMute(AudacityProject &project, Track &track, bool exclusive)
"exclusive" mute means mute the chosen track and unmute all others.
AUDACITY_DLL_API void DoRemoveTracks(AudacityProject &)