Audacity 3.2.0
TrackView.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5TrackView.cpp
6
7Paul Licameli split from TrackPanel.cpp
8
9**********************************************************************/
10
11#include "TrackView.h"
12#include "Track.h"
13
14#include "ClientData.h"
15#include "Project.h"
16#include "XMLTagHandler.h"
17#include "XMLWriter.h"
18
19TrackView::TrackView( const std::shared_ptr<Track> &pTrack )
20 : CommonTrackCell{ pTrack }
21{
23}
24
26{
27}
28
29int TrackView::GetTrackHeight( const Track *pTrack )
30{
31 return pTrack ? Get( *pTrack ).GetHeight() : 0;
32}
35{
36 return pTrack ? TrackList::Channels( pTrack ).sum( GetTrackHeight ) : 0;
37}
38
40{
41 if ( !pTrack )
42 return 0;
43 auto &view = Get( *pTrack );
44 return view.GetCumulativeHeightBefore() + view.GetHeight();
45}
46
48{
49 return GetCumulativeHeight( *list.Any().rbegin() );
50}
51
52void TrackView::CopyTo( Track &track ) const
53{
54 auto &other = Get( track );
55
56 other.mMinimized = mMinimized;
57
58 // Let mY remain 0 -- TrackPositioner corrects it later
59 other.mY = 0;
60 other.mHeight = mHeight;
61}
62
64 []( Track &track ){
65 return DoGetView::Call( track );
66 }
67};
68
70{
71 return track.AttachedObjects::Get< TrackView >( key );
72}
73
74const TrackView &TrackView::Get( const Track &track )
75{
76 return Get( const_cast< Track & >( track ) );
77}
78
80{
81 if (!pTrack)
82 return nullptr;
83 auto &track = *pTrack;
84 // do not create on demand if it is null
85 return track.AttachedObjects::Find< TrackView >( key );
86}
87
88const TrackView *TrackView::Find( const Track *pTrack )
89{
90 return Find( const_cast< Track* >( pTrack ) );
91}
92
93void TrackView::SetMinimized(bool isMinimized)
94{
95 // Do special changes appropriate to subclass
96 DoSetMinimized(isMinimized);
97
98 // Update positions and heights starting from the first track in the group
99 auto leader = *TrackList::Channels( FindTrack().get() ).begin();
100 if ( leader )
101 leader->AdjustPositions();
102}
103
105{
106 xmlFile.WriteAttr(wxT("height"), GetExpandedHeight());
107 xmlFile.WriteAttr(wxT("minimized"), GetMinimized());
108}
109
111 const std::string_view& attr, const XMLAttributeValueView& valueView)
112{
113 long nValue;
114
115 if (attr == "height" && valueView.TryGet(nValue)) {
116 // Bug 2803: Extreme values for track height (caused by integer overflow)
117 // will stall Audacity as it tries to create an enormous vertical ruler.
118 // So clamp to reasonable values.
119 nValue = std::max( 40l, std::min( nValue, 1000l ));
120 SetExpandedHeight(nValue);
121 return true;
122 }
123 else if ( attr == "minimized" && valueView.TryGet(nValue)) {
124 SetMinimized(nValue != 0);
125 return true;
126 }
127 else
128 return false;
129}
130
131auto TrackView::GetSubViews( const wxRect &rect ) -> Refinement
132{
133 return { { rect.GetTop(), shared_from_this() } };
134}
135
137{
138 return false;
139}
140
141void TrackView::DoSetMinimized(bool isMinimized)
142{
143 mMinimized = isMinimized;
144}
145
146std::shared_ptr<TrackVRulerControls> TrackView::GetVRulerControls()
147{
148 if (!mpVRulerControls)
149 // create on demand
151 return mpVRulerControls;
152}
153
154std::shared_ptr<const TrackVRulerControls> TrackView::GetVRulerControls() const
155{
156 return const_cast< TrackView* >( this )->GetVRulerControls();
157}
158
160{
161 mY = y;
162}
163
165{
166 if ( GetMinimized() )
167 return GetMinimizedHeight();
168
169 return mHeight;
170}
171
173{
174 DoSetHeight(h);
175 FindTrack()->AdjustPositions();
176}
177
179{
180 mHeight = h;
181}
182
183std::shared_ptr<CommonTrackCell> TrackView::GetAffordanceControls()
184{
185 return {};
186}
187
188namespace {
189
195{
197
198 explicit TrackPositioner( AudacityProject &project )
199 : mProject{ project }
200 {
201 mSubscription = TrackList::Get( project )
202 .Subscribe(*this, &TrackPositioner::OnUpdate);
203 }
204 TrackPositioner( const TrackPositioner & ) PROHIBITED;
206
207 void OnUpdate(const TrackListEvent & e)
208 {
209 switch (e.mType) {
214 break;
215 default:
216 return;
217 }
218 auto iter =
219 TrackList::Get( mProject ).Find( e.mpTrack.lock().get() );
220 if ( !*iter )
221 return;
222
223 auto prev = iter;
224 auto yy = TrackView::GetCumulativeHeight( *--prev );
225
226 while( auto pTrack = *iter ) {
227 auto &view = TrackView::Get( *pTrack );
228 view.SetCumulativeHeightBefore( yy );
229 yy += view.GetHeight();
230 ++iter;
231 }
232 }
233
235};
236
238 []( AudacityProject &project ){
239 return std::make_shared< TrackPositioner >( project );
240 }
241};
242
243}
244
246 return nullptr;
247}
248
250 return nullptr;
251}
Utility ClientData::Site to register hooks into a host class that attach client data.
int min(int a, int b)
declares abstract base class Track, TrackList, and iterators over TrackList
static const AttachedTrackObjects::RegisteredFactory key
Definition: TrackView.cpp:63
DEFINE_ATTACHED_VIRTUAL(DoGetView)
Definition: TrackView.cpp:245
Class template generates single-dispatch, open method registry tables.
static Return Call(This &obj, Arguments &&...arguments)
Invoke the method – but only after static initialization time.
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
std::shared_ptr< Track > FindTrack()
Subscription Subscribe(Callback callback)
Connect a callback to the Publisher; later-connected are called earlier.
Definition: Observer.h:199
A move-only handle representing a connection to a Publisher.
Definition: Observer.h:70
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:225
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:1330
auto Find(Track *pTrack) -> TrackIter< TrackType >
Turn a pointer into a TrackIter (constant time); get end iterator if this does not own the track.
Definition: Track.h:1391
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1429
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:467
static auto Channels(TrackType *pTrack) -> TrackIterRange< TrackType >
Definition: Track.h:1533
std::shared_ptr< TrackVRulerControls > GetVRulerControls()
Definition: TrackView.cpp:146
void SetMinimized(bool minimized)
Definition: TrackView.cpp:93
void WriteXMLAttributes(XMLWriter &) const override
Serialize persistent attributes.
Definition: TrackView.cpp:104
static int GetChannelGroupHeight(const Track *pTrack)
Definition: TrackView.cpp:34
int GetHeight() const
Definition: TrackView.cpp:164
std::shared_ptr< TrackVRulerControls > mpVRulerControls
Definition: TrackView.h:125
virtual void DoSetMinimized(bool isMinimized)
Definition: TrackView.cpp:141
bool GetMinimized() const
Definition: TrackView.h:52
void DoSetHeight(int h)
Definition: TrackView.cpp:178
int GetExpandedHeight() const
Definition: TrackView.h:60
void DoSetY(int y)
Definition: TrackView.cpp:159
static TrackView * Find(Track *)
Definition: TrackView.cpp:79
static int GetTotalHeight(const TrackList &list)
Definition: TrackView.cpp:47
virtual bool IsSpectral() const
Definition: TrackView.cpp:136
bool HandleXMLAttribute(const std::string_view &attr, const XMLAttributeValueView &valueView) override
Deserialize an attribute, returning true if recognized.
Definition: TrackView.cpp:110
static int GetCumulativeHeight(const Track *pTrack)
Definition: TrackView.cpp:39
virtual ~TrackView()=0
Definition: TrackView.cpp:25
void SetExpandedHeight(int height)
Definition: TrackView.cpp:172
TrackView(const TrackView &)=delete
void CopyTo(Track &track) const override
Copy state, for undo/redo purposes.
Definition: TrackView.cpp:52
virtual Refinement GetSubViews(const wxRect &rect)
Definition: TrackView.cpp:131
virtual std::shared_ptr< TrackVRulerControls > DoGetVRulerControls()=0
bool mMinimized
Definition: TrackView.h:128
static int GetTrackHeight(const Track *pTrack)
Definition: TrackView.cpp:29
int mHeight
Definition: TrackView.h:130
static TrackView & Get(Track &)
Definition: TrackView.cpp:69
virtual std::shared_ptr< CommonTrackCell > GetAffordanceControls()
Definition: TrackView.cpp:183
virtual int GetMinimizedHeight() const =0
std::vector< std::pair< wxCoord, std::shared_ptr< TrackView > > > Refinement
Definition: TrackView.h:104
A view into an attribute value. The class does not take the ownership of the data.
bool TryGet(bool &value) const noexcept
Try to get a boolean value from the view.
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26
void WriteAttr(const wxString &name, const Identifier &value)
Definition: XMLWriter.h:37
A convenient default parameter for class template Site.
Definition: ClientData.h:28
Notification of changes in individual tracks of TrackList, or of TrackList's composition.
Definition: Track.h:1282
const std::weak_ptr< Track > mpTrack
Definition: Track.h:1318
const Type mType
Definition: Track.h:1317
@ RESIZING
Posted when some track changed its height.
Definition: Track.h:1298
@ DELETION
Posted when a track has been deleted from a tracklist. Also posted when one track replaces another.
Definition: Track.h:1305
@ ADDITION
Posted when a track has been added to a tracklist. Also posted when one track replaces another.
Definition: Track.h:1301
@ PERMUTED
Posted when tracks are reordered but otherwise unchanged.
Definition: Track.h:1295
TrackPositioner & operator=(const TrackPositioner &) PROHIBITED
TrackPositioner(const TrackPositioner &) PROHIBITED