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 "XMLTagHandler.h"
17 #include "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.GetCumulativeHeightBefore() + 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"), GetExpandedHeight());
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  SetExpandedHeight(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 std::shared_ptr<CommonTrackCell> TrackView::GetAffordanceControls()
169 {
170  return {};
171 }
172 
173 namespace {
174 
179 struct TrackPositioner final : ClientData::Base, wxEvtHandler
180 {
182 
183  explicit TrackPositioner( AudacityProject &project )
184  : mProject{ project }
185  {
186  TrackList::Get( project ).Bind(
187  EVT_TRACKLIST_ADDITION, &TrackPositioner::OnUpdate, this );
188  TrackList::Get( project ).Bind(
189  EVT_TRACKLIST_DELETION, &TrackPositioner::OnUpdate, this );
190  TrackList::Get( project ).Bind(
191  EVT_TRACKLIST_PERMUTED, &TrackPositioner::OnUpdate, this );
192  TrackList::Get( project ).Bind(
193  EVT_TRACKLIST_RESIZING, &TrackPositioner::OnUpdate, this );
194  }
195  TrackPositioner( const TrackPositioner & ) PROHIBITED;
196  TrackPositioner &operator=( const TrackPositioner & ) PROHIBITED;
197 
199  {
200  e.Skip();
201 
202  auto iter =
203  TrackList::Get( mProject ).Find( e.mpTrack.lock().get() );
204  if ( !*iter )
205  return;
206 
207  auto prev = iter;
208  auto yy = TrackView::GetCumulativeHeight( *--prev );
209 
210  while( auto pTrack = *iter ) {
211  auto &view = TrackView::Get( *pTrack );
212  view.SetCumulativeHeightBefore( yy );
213  yy += view.GetHeight();
214  ++iter;
215  }
216  }
217 };
218 
220  []( AudacityProject &project ){
221  return std::make_shared< TrackPositioner >( project );
222  }
223 };
224 
225 }
226 
228  return nullptr;
229 }
230 
232  return nullptr;
233 }
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:123
XMLWriter.h
anonymous_namespace{TrackView.cpp}::TrackPositioner::OnUpdate
void OnUpdate(TrackListEvent &e)
Definition: TrackView.cpp:198
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:157
AttachedVirtualFunction::Call
static Return Call(This &obj, Arguments &&...arguments)
Invoke the method – but only after static initialization time.
Definition: AttachedVirtualFunction.h:223
AttachedVirtualFunction
Class template generates single-dispatch, open method registry tables.
Definition: AttachedVirtualFunction.h:159
TrackView.h
TrackView::GetMinimized
bool GetMinimized() const
Definition: TrackView.h:49
Project.h
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:149
CommonTrackPanelCell::FindTrack
std::shared_ptr< Track > FindTrack()
Definition: CommonTrackPanelCell.h:46
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
TrackView::SetExpandedHeight
void SetExpandedHeight(int height)
Definition: TrackView.cpp:157
TrackView::Refinement
std::vector< std::pair< wxCoord, std::shared_ptr< TrackView > > > Refinement
Definition: TrackView.h:99
TrackView::WriteXMLAttributes
void WriteXMLAttributes(XMLWriter &) const override
Definition: TrackView.cpp:88
TrackView::GetTrackHeight
static int GetTrackHeight(const Track *pTrack)
Definition: TrackView.cpp:29
anonymous_namespace{TrackView.cpp}::TrackPositioner
Definition: TrackView.cpp:180
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:1333
ClientData.h
Utility ClientData::Site to register hooks into a host class that attach client data.
anonymous_namespace{TrackView.cpp}::TrackPositioner::TrackPositioner
TrackPositioner(AudacityProject &project)
Definition: TrackView.cpp:183
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
anonymous_namespace{TrackView.cpp}::TrackPositioner::mProject
AudacityProject & mProject
Definition: TrackView.cpp:181
XMLTagHandler.h
TrackListEvent
Notification of changes in individual tracks of TrackList, or of TrackList's composition.
Definition: Track.h:1222
TrackView::mY
int mY
Definition: TrackView.h:124
TrackView::mpVRulerControls
std::shared_ptr< TrackVRulerControls > mpVRulerControls
Definition: TrackView.h:120
TrackView::GetAffordanceControls
virtual std::shared_ptr< CommonTrackCell > GetAffordanceControls()
Definition: TrackView.cpp:168
Track::GetTrackView
const std::shared_ptr< CommonTrackCell > & GetTrackView()
Definition: Track.cpp:144
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
TrackView::GetExpandedHeight
int GetExpandedHeight() const
Definition: TrackView.h:57
min
int min(int a, int b)
Definition: CompareAudioCommand.cpp:106
TrackList::Get
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:506
Track
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:239
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:92
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:219
TrackView::GetChannelGroupHeight
static int GetChannelGroupHeight(const Track *pTrack)
Definition: TrackView.cpp:34
TrackListEvent::mpTrack
std::weak_ptr< Track > mpTrack
Definition: Track.h:1238
DEFINE_ATTACHED_VIRTUAL
DEFINE_ATTACHED_VIRTUAL(DoGetView)
Definition: TrackView.cpp:227
TrackList::Any
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1371
TrackView::GetMinimizedHeight
virtual int GetMinimizedHeight() const =0
TrackView::mHeight
int mHeight
Definition: TrackView.h:125
TrackView::IsSpectral
virtual bool IsSpectral() const
Definition: TrackView.cpp:121
TrackView::GetHeight
int GetHeight() const
Definition: TrackView.cpp:149
CommonTrackCell
Definition: CommonTrackPanelCell.h:99
TrackView::DoGetVRulerControls
virtual std::shared_ptr< TrackVRulerControls > DoGetVRulerControls()=0