Audacity 3.2.0
WaveformAppearance.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/**********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 @file WaveformApperance.cpp
7
8 Paul Licameli
9
10 **********************************************************************/
11
12#include "WaveformAppearance.h"
13#include "WaveClip.h"
14
15
17 [](Track &track) -> std::shared_ptr<WaveformAppearance> {
18 if (const auto pTrack = dynamic_cast<WaveTrack *>(&track))
19 return std::make_shared<WaveformAppearance>(*pTrack);
20 else
21 return nullptr;
22 }
23};
24
26{
27 return track.AttachedObjects::Get<WaveformAppearance>(keyWA);
28}
29
31{
32 return Get(const_cast<WaveTrack&>(track));
33}
34
36{
37 return Get(channel.GetTrack());
38}
39
41{
42 return Get(const_cast<WaveChannel &>(channel));
43}
44
46 : mwTrack{
47 std::static_pointer_cast<WaveTrack>(track.shared_from_this()) }
48{
49 Subscribe(mwTrack.lock());
50}
51
53
54void WaveformAppearance::Subscribe(const std::shared_ptr<WaveTrack> &pTrack)
55{
56 if (pTrack)
58 pTrack->Subscribe([this](const WaveTrackMessage &message){
59 switch (message.type) {
60 case WaveTrackMessage::New:
61 case WaveTrackMessage::Deserialized:
62 WaveColorAttachment::Get(*message.pClip).SetColorIndex(mColorIndex);
63 default:
64 break;
65 }
66 });
67 else
69}
70
72{
73 if (const auto pTrack = dynamic_cast<WaveTrack *>(&track)) {
74 auto &other = Get(*pTrack);
75 other.mColorIndex = mColorIndex;
76 }
77}
78
79void WaveformAppearance::Reparent(const std::shared_ptr<Track> &parent)
80{
81 mwTrack = std::dynamic_pointer_cast<WaveTrack>(parent);
82 Subscribe(mwTrack.lock());
83}
84
85static constexpr auto ColorIndex_attr = "colorindex";
86
88{
90}
91
93 const std::string_view& attr, const XMLAttributeValueView& valueView)
94{
95 long nValue;
96 if (attr == ColorIndex_attr && valueView.TryGet(nValue))
97 mColorIndex = nValue;
98 return false;
99}
100
102{
103 mColorIndex = colorIndex;
104 const auto pTrack = mwTrack.lock();
105 if (!pTrack)
106 return;
107 for (const auto &pInterval : pTrack->Intervals())
108 for (const auto &pChannel : pInterval->Channels())
109 WaveColorAttachment::Get(*pChannel)
110 .SetColorIndex(colorIndex);
111}
112
114{
115}
116
118{
119}
120
121std::unique_ptr<WaveClipListener> WaveColorAttachment::Clone() const
122{
123 auto result = std::make_unique<WaveColorAttachment>(*this);
124 return result;
125}
126
127static WaveClip::Attachments::RegisteredFactory keyWCA{ [](WaveClip &clip) {
128 return std::make_unique<WaveColorAttachment>();
129} };
130
132{
133 return clip.Attachments::Get<WaveColorAttachment>(keyWCA);
134}
135
137{
138 return const_cast<WaveClip&>(clip)
139 .Attachments::Get<WaveColorAttachment>(keyWCA);
140}
141
143{
144 return Get(clip.GetClip());
145}
146
148{
149 return Get(clip.GetClip());
150}
151
153
155
157{
159}
160
161bool WaveColorAttachment::HandleXMLAttribute(const std::string_view &attr,
162 const XMLAttributeValueView &valueView)
163{
164 long longValue;
165 if (attr == ColorIndex_attr) {
166 if (!valueView.TryGet(longValue))
167 return false;
168 SetColorIndex(longValue);
169 return true;
170 }
171 return false;
172}
static const AttachedTrackObjects::RegisteredFactory keyWA
static constexpr auto ColorIndex_attr
static WaveClip::Attachments::RegisteredFactory keyWCA
Client code makes static instance from a factory of attachments; passes it to Get or Find as a retrie...
Definition: ClientData.h:275
void Reset() noexcept
Breaks the connection (constant time)
Definition: Observer.cpp:101
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:110
WaveTrack & GetTrack()
Definition: WaveTrack.h:841
WaveClip & GetClip()
Definition: WaveClip.h:96
This allows multiple clips to be a part of one WaveTrack.
Definition: WaveClip.h:238
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
Persistent appearance settings that apply to all channels of a track.
bool HandleXMLAttribute(const std::string_view &attr, const XMLAttributeValueView &valueView) override
Deserialize an attribute, returning true if recognized.
void WriteXMLAttributes(XMLWriter &writer) const override
Serialize persistent attributes.
void Reparent(const std::shared_ptr< Track > &parent) override
Object may be shared among tracks but hold a special back-pointer to one of them; reassign it.
static WaveformAppearance & Get(WaveTrack &track)
std::weak_ptr< WaveTrack > mwTrack
Observer::Subscription mSubscription
~WaveformAppearance() override
void Subscribe(const std::shared_ptr< WaveTrack > &pTrack)
WaveformAppearance(WaveTrack &track)
void CopyTo(Track &track) const override
Copy state, for undo/redo purposes.
void SetColorIndex(int colorIndex)
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
STL namespace.
void MarkChanged() noexcept override
void WriteXMLAttributes(XMLWriter &writer) const override
static WaveColorAttachment & Get(WaveClip &clip)
std::unique_ptr< WaveClipListener > Clone() const override
void SetColorIndex(int colorIndex)
void Invalidate() override
bool HandleXMLAttribute(const std::string_view &attr, const XMLAttributeValueView &valueView) override
enum WaveTrackMessage::Type type