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