Audacity 3.2.0
SelectionState.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 SelectionState.h
6
7 **********************************************************************/
8
9
10#include "SelectionState.h"
11
12#include "ViewInfo.h"
13#include "SyncLock.h"
14#include "Track.h"
15#include "Project.h"
16
18 [](AudacityProject &){ return std::make_shared< SelectionState >(); }
19};
20
22{
23 return project.AttachedObjects::Get< SelectionState >( key );
24}
25
27{
28 return Get( const_cast< AudacityProject & >( project ) );
29}
30
31// Set selection length to the length of a track -- but if sync-lock is turned
32// on, use the largest possible selection in the sync-lock group.
33// If it's a stereo track, do the same for the stereo channels.
35( ViewInfo &viewInfo, Track &track, bool syncLocked )
36{
37 auto trackRange = syncLocked
38 // If we have a sync-lock group and sync-lock linking is on,
39 // check the sync-lock group tracks.
40 ? SyncLock::Group(&track)
41
42 // Otherwise, check for a stereo pair
43 : TrackList::Channels(&track);
44
45 auto minOffset = trackRange.min( &Track::GetOffset );
46 auto maxEnd = trackRange.max( &Track::GetEndTime );
47
48 // PRL: double click or click on track control.
49 // should this select all frequencies too? I think not.
50 viewInfo.selectedRegion.setTimes(minOffset, maxEnd);
51}
52
54 Track &track, bool selected, bool updateLastPicked )
55{
56 //bool wasCorrect = (selected == track.GetSelected());
57
58 for (auto channel : TrackList::Channels(&track))
59 channel->SetSelected(selected);
60
61 if (updateLastPicked)
63
64//The older code below avoids an anchor on an unselected track.
65
66 /*
67 if (selected) {
68 // This handles the case of linked tracks, selecting all channels
69 mTracks->Select(pTrack, true);
70 if (updateLastPicked)
71 mLastPickedTrack = Track::Pointer( pTrack );
72 }
73 else {
74 mTracks->Select(pTrack, false);
75 if (updateLastPicked && pTrack == mLastPickedTrack.lock().get())
76 mLastPickedTrack.reset();
77 }
78*/
79}
80
82( TrackList &tracks, Track &rsTrack, Track &reTrack )
83{
84 Track *sTrack = &rsTrack, *eTrack = &reTrack;
85 // Swap the track pointers if needed
86 if (eTrack->GetIndex() < sTrack->GetIndex())
87 std::swap(sTrack, eTrack);
88
89 for (auto track :
90 tracks.Any().StartingWith(sTrack).EndingAfter(eTrack))
91 SelectTrack(*track, true, false);
92}
93
95{
96 for (auto t : tracks.Any())
97 SelectTrack( *t, false, false );
98}
99
101( TrackList &tracks, Track &track )
102{
103 // We will either extend from the first or from the last.
104 auto pExtendFrom = tracks.Lock(mLastPickedTrack);
105
106 if( !pExtendFrom ) {
107 auto trackRange = tracks.Selected();
108 auto pFirst = *trackRange.begin();
109
110 // If our track is at or after the first, extend from the first.
111 if( pFirst && track.GetIndex() >= pFirst->GetIndex() )
112 pExtendFrom = pFirst->SharedPointer();
113
114 // Our track was earlier than the first. Extend from the last.
115 if( !pExtendFrom )
116 pExtendFrom = Track::SharedPointer( *trackRange.rbegin() );
117 }
118
119 SelectNone( tracks );
120 if( pExtendFrom )
121 SelectRangeOfTracks( tracks, track, *pExtendFrom );
122 else
123 SelectTrack( track, true, true );
124 mLastPickedTrack = pExtendFrom;
125}
126
128( TrackList &tracks, ViewInfo &viewInfo,
129 Track &track, bool shift, bool ctrl, bool syncLocked )
130{
131 // AS: If the shift button is being held down, invert
132 // the selection on this track.
133 if (ctrl)
134 SelectTrack( track, !track.GetSelected(), true );
135 else {
136 if (shift && mLastPickedTrack.lock())
137 ChangeSelectionOnShiftClick( tracks, track );
138 else {
139 SelectNone( tracks );
140 SelectTrack( track, true, true );
141 SelectTrackLength( viewInfo, track, syncLocked );
142 }
143 }
144}
145
147( SelectionState &state, TrackList &tracks )
148 : mpState{ &state }
149 , mTracks{ tracks }
150 , mInitialLastPickedTrack{ state.mLastPickedTrack }
151{
152 // Save initial state of track selections
154 for (const auto track : tracks.Any()) {
155 const bool isSelected = track->GetSelected();
156 mInitialTrackSelection.push_back(isSelected);
157 }
158}
159
161{
162 if ( mpState ) {
163 // roll back changes
165 std::vector<bool>::const_iterator
166 it = mInitialTrackSelection.begin(),
168
169 for (auto track : mTracks.Any()) {
170 if (it == end)
171 break;
172 track->SetSelected( *it++ );
173 }
174 }
175}
176
178{
179 mpState = nullptr;
180}
static const AudacityProject::AttachedObjects::RegisteredFactory key
declares abstract base class Track, TrackList, and iterators over TrackList
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:266
bool setTimes(double t0, double t1)
Definition: ViewInfo.cpp:51
SelectionState * mpState
SelectionStateChanger(SelectionState &state, TrackList &tracks)
std::vector< bool > mInitialTrackSelection
std::weak_ptr< Track > mInitialLastPickedTrack
std::weak_ptr< Track > mLastPickedTrack
void HandleListSelection(TrackList &tracks, ViewInfo &viewInfo, Track &track, bool shift, bool ctrl, bool syncLocked)
void ChangeSelectionOnShiftClick(TrackList &tracks, Track &track)
static void SelectTrackLength(ViewInfo &viewInfo, Track &track, bool syncLocked)
void SelectRangeOfTracks(TrackList &tracks, Track &sTrack, Track &eTrack)
void SelectNone(TrackList &tracks)
static SelectionState & Get(AudacityProject &project)
void SelectTrack(Track &track, bool selected, bool updateLastPicked)
static TrackIterRange< Track > Group(Track *pTrack)
Definition: SyncLock.cpp:122
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:225
bool GetSelected() const
Definition: Track.h:461
std::shared_ptr< Subclass > SharedPointer()
Definition: Track.h:290
virtual double GetOffset() const =0
virtual double GetEndTime() const =0
int GetIndex() const
Definition: Track.cpp:143
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:1330
std::shared_ptr< Subclass > Lock(const std::weak_ptr< Subclass > &wTrack)
Definition: Track.h:1595
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1429
auto Selected() -> TrackIterRange< TrackType >
Definition: Track.h:1446
static auto Channels(TrackType *pTrack) -> TrackIterRange< TrackType >
Definition: Track.h:1533
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:216
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:753