Debian Codegraph @

src/tracks/playabletrack/wavetrack/ui/WaveformAppearance.cpp

raw
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
16 static const AttachedTrackObjects::RegisteredFactory keyWA{
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
25 WaveformAppearance &WaveformAppearance::Get(WaveTrack &track)
26 {
27 return track.AttachedObjects::Get<WaveformAppearance>(keyWA);
28 }
29
30 const WaveformAppearance &WaveformAppearance::Get(const WaveTrack &track)
31 {
32 return Get(const_cast<WaveTrack&>(track));
33 }
34
35 WaveformAppearance &WaveformAppearance::Get(WaveChannel &channel)
36 {
37 return Get(channel.GetTrack());
38 }
39
40 const WaveformAppearance &WaveformAppearance::Get(const WaveChannel &channel)
41 {
42 return Get(const_cast<WaveChannel &>(channel));
43 }
44
45 WaveformAppearance::WaveformAppearance(WaveTrack &track)
46 : mwTrack{
47 std::static_pointer_cast<WaveTrack>(track.shared_from_this()) }
48 {
49 Subscribe(mwTrack.lock());
50 }
51
52 WaveformAppearance::~WaveformAppearance() = default;
53
54 void WaveformAppearance::Subscribe(const std::shared_ptr<WaveTrack> &pTrack)
55 {
56 if (pTrack)
57 mSubscription =
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
68 mSubscription.Reset();
69 }
70
71 void WaveformAppearance::CopyTo(Track &track) const
72 {
73 if (const auto pTrack = dynamic_cast<WaveTrack *>(&track)) {
74 auto &other = Get(*pTrack);
75 other.mColorIndex = mColorIndex;
76 }
77 }
78
79 void WaveformAppearance::Reparent(const std::shared_ptr<Track> &parent)
80 {
81 mwTrack = std::dynamic_pointer_cast<WaveTrack>(parent);
82 Subscribe(mwTrack.lock());
83 }
84
85 static constexpr auto ColorIndex_attr = "colorindex";
86
87 void WaveformAppearance::WriteXMLAttributes(XMLWriter &writer) const
88 {
89 writer.WriteAttr(ColorIndex_attr, mColorIndex);
90 }
91
92 bool WaveformAppearance::HandleXMLAttribute(
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
101 void WaveformAppearance::SetColorIndex(int colorIndex)
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
113 WaveColorAttachment::WaveColorAttachment()
114 {
115 }
116
117 WaveColorAttachment::~WaveColorAttachment()
118 {
119 }
120
121 std::unique_ptr<WaveClipListener> WaveColorAttachment::Clone() const
122 {
123 auto result = std::make_unique<WaveColorAttachment>(*this);
124 return result;
125 }
126
127 static WaveClip::Attachments::RegisteredFactory keyWCA{ [](WaveClip &clip) {
128 return std::make_unique<WaveColorAttachment>();
129 } };
130
131 WaveColorAttachment &WaveColorAttachment::Get(WaveClip &clip)
132 {
133 return clip.Attachments::Get<WaveColorAttachment>(keyWCA);
134 }
135
136 const WaveColorAttachment &WaveColorAttachment::Get(const WaveClip &clip)
137 {
138 return const_cast<WaveClip&>(clip)
139 .Attachments::Get<WaveColorAttachment>(keyWCA);
140 }
141
142 WaveColorAttachment &WaveColorAttachment::Get(WaveChannelInterval &clip)
143 {
144 return Get(clip.GetClip());
145 }
146
147 const WaveColorAttachment &WaveColorAttachment::Get(const WaveChannelInterval &clip)
148 {
149 return Get(clip.GetClip());
150 }
151
152 void WaveColorAttachment::MarkChanged() noexcept {}
153
154 void WaveColorAttachment::Invalidate() {}
155
156 void WaveColorAttachment::WriteXMLAttributes(XMLWriter &writer) const
157 {
158 writer.WriteAttr(ColorIndex_attr, mColorIndex);
159 }
160
161 bool 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 }