Audacity 3.2.0
ChannelView.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5ChannelView.cpp
6
7Paul Licameli split from TrackPanel.cpp
8
9**********************************************************************/
10#include "ChannelView.h"
11#include "ChannelAttachments.h"
13
14#include "ClientData.h"
15#include "PendingTracks.h"
16#include "Project.h"
17#include "XMLTagHandler.h"
18#include "XMLWriter.h"
19
20
21ChannelView::ChannelView(const std::shared_ptr<Channel> &pChannel)
22 : CommonChannelCell{ pChannel }
23{
25}
26
28{
29}
30
32 const std::shared_ptr<Track> &parent, size_t iChannel)
33{
36 mpVRulerControls->Reparent(parent, iChannel);
37}
38
40{
41 const auto GetChannelHeight = [](const auto &pChannel) -> int {
42 return pChannel ? Get(*pChannel).GetHeight() : 0;
43 };
44 return pTrack ? pTrack->Channels().sum(GetChannelHeight) : 0;
45}
46
48{
49 if (!pChannel)
50 return 0;
51 auto &view = ChannelView::Get(*pChannel);
52 return view.GetCumulativeHeightBefore() + view.GetHeight();
53}
54
56{
57 if (!pTrack)
58 return 0;
59 return GetCumulativeHeight((*pTrack->Channels().rbegin()).get());
60}
61
63{
64 return GetCumulativeHeight(*list.rbegin());
65}
66
67void ChannelView::CopyTo(Track &track, size_t index) const
68{
69 auto &other = GetFromChannelGroup(track, index);
70
71 other.mMinimized = mMinimized;
72 other.vrulerSize = vrulerSize;
73
74 // Let mY remain 0 -- TrackPositioner corrects it later
75 other.mY = 0;
76 other.mHeight = mHeight;
77}
78
80
82 [](Track &track){
83 return std::make_shared<ChannelViewAttachments>(track,
84 [](Track &track, size_t iChannel) {
85 assert(iChannel < track.NChannels());
86 return DoGetView::Call(track, iChannel);
87 }
88 );
89 }
90};
91
93 ChannelGroup &group, size_t iChannel)
94{
95 auto &track = static_cast<Track&>(group);
97}
98
100 ChannelGroup *pGroup, size_t iChannel)
101{
103 keyC, static_cast<Track*>(pGroup), iChannel);
104}
105
106void ChannelView::SetMinimized(bool isMinimized)
107{
108 // Do special changes appropriate to subclass
109 DoSetMinimized(isMinimized);
111}
112
114{
115 // Update positions and heights starting from the first track in the group,
116 // causing TrackList events
117 if (const auto pTrack = FindTrack())
118 pTrack->AdjustPositions();
119}
120
121namespace {
122// Append a channel number to a base attribute name unless it is 0
123std::string AttributeName(const std::string& name, size_t index) {
124 if (index == 0)
125 return name;
126
127 return name + std::to_string(index);
128}
129std::string HeightAttributeName(size_t index) {
130 return AttributeName("height", index);
131}
132std::string MinimizedAttributeName(size_t index) {
133 return AttributeName("minimized", index);
134}
135}
136
137void ChannelView::WriteXMLAttributes(XMLWriter &xmlFile, size_t index) const
138{
141}
142
144 const std::string_view& attr, const XMLAttributeValueView& valueView,
145 size_t index)
146{
147 long nValue;
148
149 if (attr == HeightAttributeName(index) && valueView.TryGet(nValue)) {
150 // Bug 2803: Extreme values for track height (caused by integer overflow)
151 // will stall Audacity as it tries to create an enormous vertical ruler.
152 // So clamp to reasonable values.
153 nValue = std::max( 40l, std::min( nValue, 1000l ));
154 SetExpandedHeight(nValue);
155 return true;
156 }
157 else if (attr == MinimizedAttributeName(index) && valueView.TryGet(nValue)) {
158 SetMinimized(nValue != 0);
159 return true;
160 }
161 else
162 return false;
163}
164
165auto ChannelView::GetSubViews(const wxRect &rect) -> Refinement
166{
167 return { { rect.GetTop(), shared_from_this() } };
168}
169
171{
172 return false;
173}
174
175void ChannelView::DoSetMinimized(bool isMinimized)
176{
177 mMinimized = isMinimized;
178}
179
180std::shared_ptr<ChannelVRulerControls> ChannelView::GetVRulerControls()
181{
182 if (!mpVRulerControls)
183 // create on demand
185 return mpVRulerControls;
186}
187
188std::shared_ptr<const ChannelVRulerControls>
190{
191 return const_cast<ChannelView*>(this)->GetVRulerControls();
192}
193
195{
196 mY = y;
197}
198
200{
201 if ( GetMinimized() )
202 return GetMinimizedHeight();
203
204 return mHeight;
205}
206
208{
209 DoSetHeight(h);
211}
212
214{
215 mHeight = h;
216}
217
218std::shared_ptr<CommonTrackCell> ChannelView::GetAffordanceControls()
219{
220 return {};
221}
222
224{
225 return
227}
228
230{
231 return Get(const_cast<Channel&>(channel));
232}
233
235{
236 if (!pChannel)
237 return nullptr;
239 &pChannel->GetChannelGroup(), pChannel->GetChannelIndex());
240}
241
242const ChannelView *ChannelView::Find(const Channel *pChannel)
243{
244 return Find(const_cast<Channel*>(pChannel));
245}
246
247namespace {
248
254{
256
258 : mProject{ project }
259 {
260 mSubscription = PendingTracks::Get(project)
261 .Subscribe(*this, &TrackPositioner::OnUpdate);
262 }
263 TrackPositioner( const TrackPositioner & ) = delete;
265
266 void OnUpdate(const TrackListEvent & e)
267 {
268 switch (e.mType) {
273 break;
274 default:
275 return;
276 }
277 auto iter =
278 TrackList::Get(mProject).Find(e.mpTrack.lock().get());
279 if (!*iter)
280 return;
281
282 auto prev = iter;
283 auto yy = ChannelView::GetCumulativeHeight(*--prev);
284
285 while (auto pTrack = *iter) {
286 for (auto pChannel : (*iter)->Channels()) {
287 auto &view = ChannelView::Get(*pChannel);
288 view.SetCumulativeHeightBefore(yy);
289 yy += view.GetHeight();
290 }
291 ++iter;
292 }
293 }
294
296};
297
300 return std::make_shared< TrackPositioner >( project );
301 }
302};
303
304}
305
307 return nullptr;
308}
309
311 return nullptr;
312}
Adapts TrackAttachment interface with extra channel index argument.
static const AttachedTrackObjects::RegisteredFactory keyC
Definition: ChannelView.cpp:81
DEFINE_ATTACHED_VIRTUAL(DoGetView)
Utility ClientData::Site to register hooks into a host class that attach client data.
int min(int a, int b)
wxString name
Definition: TagsEditor.cpp:166
const auto project
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:90
Holds multiple objects of the parameter type as a single attachment to Track.
static Attachment * Find(const AttachedTrackObjects::RegisteredFactory &key, Track *pTrack, size_t iChannel)
static Attachment & Get(const AttachedTrackObjects::RegisteredFactory &key, Track &track, size_t iChannel)
IteratorRange< ChannelIterator< ChannelType > > Channels()
Get range of channels with mutative access.
Definition: Channel.h:384
virtual size_t NChannels() const =0
Report the number of channels.
ChannelGroup & GetChannelGroup()
Channel object's lifetime is assumed to be nested in its Track's.
Definition: Channel.cpp:43
size_t GetChannelIndex() const
Definition: Channel.cpp:25
virtual std::shared_ptr< ChannelVRulerControls > DoGetVRulerControls()=0
static ChannelView * Find(Channel *pChannel)
void Reparent(const std::shared_ptr< Track > &parent, size_t iChannel) override
Object may be shared among tracks but hold a special back-pointer to one of them; reassign it.
Definition: ChannelView.cpp:31
static int GetTotalHeight(const TrackList &list)
Definition: ChannelView.cpp:62
static ChannelView & Get(Channel &channel)
virtual std::shared_ptr< CommonTrackCell > GetAffordanceControls()
bool mMinimized
Definition: ChannelView.h:158
std::shared_ptr< ChannelVRulerControls > mpVRulerControls
Definition: ChannelView.h:144
static ChannelView & GetFromChannelGroup(ChannelGroup &group, size_t iChannel)
Definition: ChannelView.cpp:92
virtual int GetMinimizedHeight() const =0
bool GetMinimized() const
Definition: ChannelView.h:69
bool HandleXMLAttribute(const std::string_view &attr, const XMLAttributeValueView &valueView, size_t iChannel) override
Deserialize an attribute, returning true if recognized.
virtual bool IsSpectral() const
virtual void DoSetMinimized(bool isMinimized)
void AdjustPositions()
static int GetCumulativeHeight(const Channel *pChannel)
Definition: ChannelView.cpp:47
void CopyTo(Track &track, size_t iChannel) const override
Copy state, for undo/redo purposes.
Definition: ChannelView.cpp:67
std::vector< std::pair< wxCoord, std::shared_ptr< ChannelView > > > Refinement
Definition: ChannelView.h:121
void SetMinimized(bool minimized)
int GetExpandedHeight() const
Definition: ChannelView.h:77
void DoSetY(int y)
virtual ~ChannelView()=0
Definition: ChannelView.cpp:27
virtual Refinement GetSubViews(const wxRect &rect)
int GetHeight() const
std::shared_ptr< ChannelVRulerControls > GetVRulerControls()
std::pair< int, int > vrulerSize
Definition: ChannelView.h:129
void DoSetHeight(int h)
void WriteXMLAttributes(XMLWriter &writer, size_t iChannel) const override
Serialize persistent attributes.
static int GetChannelGroupHeight(const Track *pTrack)
Definition: ChannelView.cpp:39
void SetExpandedHeight(int height)
ChannelView(const ChannelView &)=delete
static ChannelView * FindFromChannelGroup(ChannelGroup *pGroup, size_t iChannel=0)
Definition: ChannelView.cpp:99
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:275
void Reparent(const std::shared_ptr< Track > &parent, size_t iChannel) override
Object may be shared among tracks but hold a special back-pointer to one of them; reassign it.
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
static PendingTracks & Get(AudacityProject &project)
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:110
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:850
TrackIter< Track > Find(Track *pTrack)
Definition: Track.cpp:470
reverse_iterator rbegin()
Definition: Track.h:915
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:314
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:25
void WriteAttr(const wxString &name, const Identifier &value)
Definition: XMLWriter.h:36
std::string AttributeName(const std::string &name, size_t index)
std::string MinimizedAttributeName(size_t index)
std::string HeightAttributeName(size_t index)
static const AudacityProject::AttachedObjects::RegisteredFactory key
A convenient default parameter for class template Site.
Definition: ClientData.h:29
Notification of changes in individual tracks of TrackList, or of TrackList's composition.
Definition: Track.h:803
const std::weak_ptr< Track > mpTrack
Definition: Track.h:838
const Type mType
Definition: Track.h:837
@ RESIZING
Posted when some track changed its height.
Definition: Track.h:816
@ DELETION
Posted when a track has been deleted from a tracklist. Also posted when one track replaces another.
Definition: Track.h:825
@ ADDITION
Posted when a track has been added to a tracklist. Also posted when one track replaces another.
Definition: Track.h:819
@ PERMUTED
Posted when tracks are reordered but otherwise unchanged.
Definition: Track.h:813
TrackPositioner(const TrackPositioner &)=delete
TrackPositioner & operator=(const TrackPositioner &)=delete