Audacity  3.0.3
TrackView.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 TrackView.cpp
6 
7 Paul 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 "../../xml/XMLTagHandler.h"
17 #include "../../xml/XMLWriter.h"
18 
19 TrackView::TrackView( const std::shared_ptr<Track> &pTrack )
20  : CommonTrackCell{ pTrack }
21 {
22  DoSetHeight( GetDefaultTrackHeight::Call( *pTrack ) );
23 }
24 
26 {
27 }
28 
29 int TrackView::GetTrackHeight( const Track *pTrack )
30 {
31  return pTrack ? Get( *pTrack ).GetHeight() : 0;
32 }
33 
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.GetY() + view.GetHeight();
45 }
46 
48 {
49  return GetCumulativeHeight( *list.Any().rbegin() );
50 }
51 
52 void 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 {
65  auto pView = std::static_pointer_cast<TrackView>( track.GetTrackView() );
66  if (!pView)
67  // create on demand
68  track.SetTrackView( pView = DoGetView::Call( track ) );
69  return *pView;
70 }
71 
72 const TrackView &TrackView::Get( const Track &track )
73 {
74  return Get( const_cast< Track& >( track ) );
75 }
76 
77 void TrackView::SetMinimized(bool isMinimized)
78 {
79  // Do special changes appropriate to subclass
80  DoSetMinimized(isMinimized);
81 
82  // Update positions and heights starting from the first track in the group
83  auto leader = *TrackList::Channels( FindTrack().get() ).begin();
84  if ( leader )
85  leader->AdjustPositions();
86 }
87 
89 {
90  xmlFile.WriteAttr(wxT("height"), GetActualHeight());
91  xmlFile.WriteAttr(wxT("minimized"), GetMinimized());
92 }
93 
94 bool TrackView::HandleXMLAttribute( const wxChar *attr, const wxChar *value )
95 {
96  wxString strValue( value );
97  long nValue;
98  if (!wxStrcmp(attr, wxT("height")) &&
99  XMLValueChecker::IsGoodInt(strValue) && strValue.ToLong(&nValue)) {
100  // Bug 2803: Extreme values for track height (caused by integer overflow)
101  // will stall Audacity as it tries to create an enormous vertical ruler.
102  // So clamp to reasonable values.
103  nValue = std::max( 40l, std::min( nValue, 1000l ));
104  SetHeight(nValue);
105  return true;
106  }
107  else if (!wxStrcmp(attr, wxT("minimized")) &&
108  XMLValueChecker::IsGoodInt(strValue) && strValue.ToLong(&nValue)) {
109  SetMinimized(nValue != 0);
110  return true;
111  }
112  else
113  return false;
114 }
115 
116 auto TrackView::GetSubViews( const wxRect &rect ) -> Refinement
117 {
118  return { { rect.GetTop(), shared_from_this() } };
119 }
120 
122 {
123  return false;
124 }
125 
126 void TrackView::DoSetMinimized(bool isMinimized)
127 {
128  mMinimized = isMinimized;
129 }
130 
131 std::shared_ptr<TrackVRulerControls> TrackView::GetVRulerControls()
132 {
133  if (!mpVRulerControls)
134  // create on demand
136  return mpVRulerControls;
137 }
138 
139 std::shared_ptr<const TrackVRulerControls> TrackView::GetVRulerControls() const
140 {
141  return const_cast< TrackView* >( this )->GetVRulerControls();
142 }
143 
144 void TrackView::DoSetY(int y)
145 {
146  mY = y;
147 }
148 
150 {
151  if ( GetMinimized() )
152  return GetMinimizedHeight();
153 
154  return mHeight;
155 }
156 
158 {
159  DoSetHeight(h);
160  FindTrack()->AdjustPositions();
161 }
162 
164 {
165  mHeight = h;
166 }
167 
168 namespace {
169 
170 // Attach an object to each project. It receives track list events and updates
171 // track Y coordinates
172 struct TrackPositioner final : ClientData::Base, wxEvtHandler
173 {
175 
176  explicit TrackPositioner( AudacityProject &project )
177  : mProject{ project }
178  {
179  TrackList::Get( project ).Bind(
180  EVT_TRACKLIST_ADDITION, &TrackPositioner::OnUpdate, this );
181  TrackList::Get( project ).Bind(
182  EVT_TRACKLIST_DELETION, &TrackPositioner::OnUpdate, this );
183  TrackList::Get( project ).Bind(
184  EVT_TRACKLIST_PERMUTED, &TrackPositioner::OnUpdate, this );
185  TrackList::Get( project ).Bind(
186  EVT_TRACKLIST_RESIZING, &TrackPositioner::OnUpdate, this );
187  }
188  TrackPositioner( const TrackPositioner & ) PROHIBITED;
189  TrackPositioner &operator=( const TrackPositioner & ) PROHIBITED;
190 
192  {
193  e.Skip();
194 
195  auto iter =
196  TrackList::Get( mProject ).Find( e.mpTrack.lock().get() );
197  if ( !*iter )
198  return;
199 
200  auto prev = iter;
201  auto yy = TrackView::GetCumulativeHeight( *--prev );
202 
203  while( auto pTrack = *iter ) {
204  auto &view = TrackView::Get( *pTrack );
205  view.SetY( yy );
206  yy += view.GetHeight();
207  ++iter;
208  }
209  }
210 };
211 
213  []( AudacityProject &project ){
214  return std::make_shared< TrackPositioner >( project );
215  }
216 };
217 
218 }
219 
220 template<> auto DoGetView::Implementation() -> Function {
221  return nullptr;
222 }
224 
225 template<> auto GetDefaultTrackHeight::Implementation() -> Function {
226  return nullptr;
227 }
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:23
TrackView::TrackView
TrackView(const TrackView &)=delete
TrackView::DoSetHeight
void DoSetHeight(int h)
Definition: TrackView.cpp:163
TrackView::mMinimized
bool mMinimized
Definition: TrackView.h:95
anonymous_namespace{TrackView.cpp}::TrackPositioner::OnUpdate
void OnUpdate(TrackListEvent &e)
Definition: TrackView.cpp:191
TrackView::Get
static TrackView & Get(Track &)
Definition: TrackView.cpp:63
TrackView::SetMinimized
void SetMinimized(bool minimized)
Definition: TrackView.cpp:77
XMLValueChecker::IsGoodInt
static bool IsGoodInt(const wxString &strInt)
Check that the supplied string can be converted to a long (32bit) integer.
Definition: XMLTagHandler.cpp:158
AttachedVirtualFunction::Call
static Return Call(This &obj, Arguments &&...arguments)
Invoke the method – but only after static initialization time.
Definition: AttachedVirtualFunction.h:229
AttachedVirtualFunction
Class template generates single-dispatch, open method registry tables.
Definition: AttachedVirtualFunction.h:161
TrackView.h
TrackView::GetMinimized
bool GetMinimized() const
Definition: TrackView.h:49
TrackView::GetCumulativeHeight
static int GetCumulativeHeight(const Track *pTrack)
Definition: TrackView.cpp:39
Track::SetTrackView
void SetTrackView(const std::shared_ptr< CommonTrackCell > &pView)
Definition: Track.cpp:150
CommonTrackPanelCell::FindTrack
std::shared_ptr< Track > FindTrack()
Definition: CommonTrackPanelCell.h:44
TrackList::Channels
static auto Channels(TrackType *pTrack) -> TrackIterRange< TrackType >
Definition: Track.h:1467
TrackList
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:1263
TrackView::Refinement
std::vector< std::pair< wxCoord, std::shared_ptr< TrackView > > > Refinement
Definition: TrackView.h:73
TrackView::WriteXMLAttributes
void WriteXMLAttributes(XMLWriter &) const override
Definition: TrackView.cpp:88
TrackView::SetHeight
void SetHeight(int height)
Definition: TrackView.cpp:157
TrackView::GetTrackHeight
static int GetTrackHeight(const Track *pTrack)
Definition: TrackView.cpp:29
anonymous_namespace{TrackView.cpp}::TrackPositioner
Definition: TrackView.cpp:173
TrackView::HandleXMLAttribute
bool HandleXMLAttribute(const wxChar *attr, const wxChar *value) override
Definition: TrackView.cpp:94
ClientData::Base
A convenient default parameter for class template Site.
Definition: ClientData.h:28
TrackView::~TrackView
virtual ~TrackView()=0
Definition: TrackView.cpp:25
TrackList::Find
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:1316
anonymous_namespace{TrackView.cpp}::TrackPositioner::TrackPositioner
TrackPositioner(AudacityProject &project)
Definition: TrackView.cpp:176
registerGetDefaultTrackHeight
static GetDefaultTrackHeight registerGetDefaultTrackHeight
Definition: TrackView.cpp:228
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
registerDoGetView
static DoGetView registerDoGetView
Definition: TrackView.cpp:223
anonymous_namespace{TrackView.cpp}::TrackPositioner::mProject
AudacityProject & mProject
Definition: TrackView.cpp:174
TrackListEvent
Notification of changes in individual tracks of TrackList, or of TrackList's composition.
Definition: Track.h:1205
TrackView::GetActualHeight
int GetActualHeight() const
Definition: TrackView.h:53
TrackView::mY
int mY
Definition: TrackView.h:96
TrackView::mpVRulerControls
std::shared_ptr< TrackVRulerControls > mpVRulerControls
Definition: TrackView.h:92
Track::GetTrackView
const std::shared_ptr< CommonTrackCell > & GetTrackView()
Definition: Track.cpp:145
TrackView::GetTotalHeight
static int GetTotalHeight(const TrackList &list)
Definition: TrackView.cpp:47
TrackView::GetVRulerControls
std::shared_ptr< TrackVRulerControls > GetVRulerControls()
Definition: TrackView.cpp:131
TrackView::DoSetMinimized
virtual void DoSetMinimized(bool isMinimized)
Definition: TrackView.cpp:126
anonymous_namespace{TrackView.cpp}::TrackPositioner::operator=
TrackPositioner & operator=(const TrackPositioner &) PROHIBITED
anonymous_namespace{TrackView.cpp}::TrackPositioner::TrackPositioner
TrackPositioner(const TrackPositioner &) PROHIBITED
AttachedVirtualFunction::Implementation
static Function Implementation()
A function returning a std::function, which you must define so that the program links.
Definition: TimeShiftHandle.cpp:260
min
int min(int a, int b)
Definition: CompareAudioCommand.cpp:106
TrackList::Get
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:495
Track
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:238
TrackView
Definition: TrackView.h:24
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:112
TrackView::DoSetY
void DoSetY(int y)
Definition: TrackView.cpp:144
TrackView::GetSubViews
virtual Refinement GetSubViews(const wxRect &rect)
Definition: TrackView.cpp:116
XMLWriter::WriteAttr
void WriteAttr(const wxString &name, const Identifier &value)
Definition: XMLWriter.h:34
TrackView::CopyTo
void CopyTo(Track &track) const override
Definition: TrackView.cpp:52
anonymous_namespace{TrackView.cpp}::key
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: TrackView.cpp:212
TrackView::GetChannelGroupHeight
static int GetChannelGroupHeight(const Track *pTrack)
Definition: TrackView.cpp:34
TrackListEvent::mpTrack
std::weak_ptr< Track > mpTrack
Definition: Track.h:1221
TrackList::Any
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1354
TrackView::GetMinimizedHeight
virtual int GetMinimizedHeight() const =0
TrackView::mHeight
int mHeight
Definition: TrackView.h:97
TrackView::IsSpectral
virtual bool IsSpectral() const
Definition: TrackView.cpp:121
TrackView::GetHeight
int GetHeight() const
Definition: TrackView.cpp:149
CommonTrackCell
Definition: CommonTrackPanelCell.h:59
TrackView::DoGetVRulerControls
virtual std::shared_ptr< TrackVRulerControls > DoGetVRulerControls()=0