Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "vibrato.h"
23 :
24 : #include <cmath>
25 :
26 : #include "types/typesconv.h"
27 :
28 : #include "dummyparent.h"
29 : #include "iengravingfont.h"
30 :
31 : #include "score.h"
32 : #include "system.h"
33 : #include "trill.h"
34 :
35 : #include "log.h"
36 :
37 : using namespace mu;
38 : using namespace mu::engraving;
39 :
40 : namespace mu::engraving {
41 28 : VibratoSegment::VibratoSegment(Vibrato* sp)
42 28 : : LineSegment(ElementType::VIBRATO_SEGMENT, sp, ElementFlag::MOVABLE | ElementFlag::ON_STAFF)
43 : {
44 28 : }
45 :
46 : //---------------------------------------------------------
47 : // symbolLine
48 : //---------------------------------------------------------
49 :
50 28 : void VibratoSegment::symbolLine(SymId start, SymId fill)
51 : {
52 28 : double x1 = 0;
53 28 : double x2 = pos2().x();
54 28 : double w = x2 - x1;
55 28 : double mag = magS();
56 28 : IEngravingFontPtr f = score()->engravingFont();
57 :
58 28 : m_symbols.clear();
59 28 : m_symbols.push_back(start);
60 28 : double w1 = f->advance(start, mag);
61 28 : double w2 = f->advance(fill, mag);
62 28 : int n = lrint((w - w1) / w2);
63 274 : for (int i = 0; i < n; ++i) {
64 246 : m_symbols.push_back(fill);
65 : }
66 28 : RectF r(f->bbox(m_symbols, mag));
67 28 : setbbox(r);
68 28 : }
69 :
70 0 : void VibratoSegment::symbolLine(SymId start, SymId fill, SymId end)
71 : {
72 0 : double x1 = 0;
73 0 : double x2 = pos2().x();
74 0 : double w = x2 - x1;
75 0 : double mag = magS();
76 0 : IEngravingFontPtr f = score()->engravingFont();
77 :
78 0 : m_symbols.clear();
79 0 : m_symbols.push_back(start);
80 0 : double w1 = f->bbox(start, mag).width();
81 0 : double w2 = f->width(fill, mag);
82 0 : double w3 = f->width(end, mag);
83 0 : int n = lrint((w - w1 - w3) / w2);
84 0 : for (int i = 0; i < n; ++i) {
85 0 : m_symbols.push_back(fill);
86 : }
87 0 : m_symbols.push_back(end);
88 0 : RectF r(f->bbox(m_symbols, mag));
89 0 : setbbox(r);
90 0 : }
91 :
92 : //---------------------------------------------------------
93 : // propertyDelegate
94 : //---------------------------------------------------------
95 :
96 156 : PropertyDelegate VibratoSegment::resolvePropertyDelegate(Pid pid) const
97 : {
98 156 : if (pid == Pid::VIBRATO_TYPE || pid == Pid::PLACEMENT) {
99 0 : return PropertyDelegate(spanner());
100 : }
101 156 : return LineSegment::resolvePropertyDelegate(pid);
102 : }
103 :
104 : //---------------------------------------------------------
105 : // vibratoStyle
106 : //---------------------------------------------------------
107 :
108 : static const ElementStyle vibratoStyle {
109 : { Sid::vibratoPlacement, Pid::PLACEMENT },
110 : };
111 :
112 : //---------------------------------------------------------
113 : // Vibrato
114 : //---------------------------------------------------------
115 :
116 32 : Vibrato::Vibrato(DummyParent* parent)
117 32 : : SLine(ElementType::VIBRATO, parent)
118 : {
119 32 : initElementStyle(&vibratoStyle);
120 32 : m_vibratoType = VibratoType::GUITAR_VIBRATO;
121 32 : }
122 :
123 56 : Vibrato::~Vibrato()
124 : {
125 56 : }
126 :
127 : static const ElementStyle vibratoSegmentStyle {
128 : { Sid::vibratoMinDistance, Pid::MIN_DISTANCE },
129 : };
130 :
131 : //---------------------------------------------------------
132 : // createLineSegment
133 : //---------------------------------------------------------
134 :
135 28 : LineSegment* Vibrato::createLineSegment()
136 : {
137 28 : VibratoSegment* seg = new VibratoSegment(this);
138 28 : seg->setTrack(track());
139 28 : seg->setColor(lineColor());
140 28 : seg->initElementStyle(&vibratoSegmentStyle);
141 28 : return seg;
142 : }
143 :
144 56 : PointF Vibrato::linePos(Grip grip, System** system) const
145 : {
146 56 : return Trill::trillLinePos(this, grip, system);
147 : }
148 :
149 : //---------------------------------------------------------
150 : // vibratoTypeName
151 : //---------------------------------------------------------
152 :
153 0 : String Vibrato::vibratoTypeUserName() const
154 : {
155 0 : return TConv::translatedUserName(vibratoType());
156 : }
157 :
158 : //---------------------------------------------------------
159 : // subtypeUserName
160 : //---------------------------------------------------------
161 :
162 0 : muse::TranslatableString Vibrato::subtypeUserName() const
163 : {
164 0 : return TConv::userName(vibratoType());
165 : }
166 :
167 30 : Sid Vibrato::defaultPosSid() const
168 : {
169 30 : return placeAbove() ? Sid::vibratoPosAbove : Sid::vibratoPosBelow;
170 : }
171 :
172 : //---------------------------------------------------------
173 : // getProperty
174 : //---------------------------------------------------------
175 :
176 344 : PropertyValue Vibrato::getProperty(Pid propertyId) const
177 : {
178 344 : switch (propertyId) {
179 0 : case Pid::VIBRATO_TYPE:
180 0 : return int(vibratoType());
181 344 : default:
182 344 : break;
183 : }
184 344 : return SLine::getProperty(propertyId);
185 : }
186 :
187 : //---------------------------------------------------------
188 : // setProperty
189 : //---------------------------------------------------------
190 :
191 35 : bool Vibrato::setProperty(Pid propertyId, const PropertyValue& val)
192 : {
193 35 : switch (propertyId) {
194 0 : case Pid::VIBRATO_TYPE:
195 0 : setVibratoType(VibratoType(val.toInt()));
196 0 : break;
197 0 : case Pid::COLOR:
198 0 : setColor(val.value<Color>());
199 0 : setLineColor(val.value<Color>());
200 0 : break;
201 35 : default:
202 35 : return SLine::setProperty(propertyId, val);
203 : }
204 0 : triggerLayout();
205 0 : return true;
206 : }
207 :
208 : //---------------------------------------------------------
209 : // propertyDefault
210 : //---------------------------------------------------------
211 :
212 340 : PropertyValue Vibrato::propertyDefault(Pid propertyId) const
213 : {
214 340 : switch (propertyId) {
215 0 : case Pid::VIBRATO_TYPE:
216 0 : return static_cast<int>(VibratoType::GUITAR_VIBRATO);
217 2 : case Pid::PLACEMENT:
218 2 : return style().styleV(Sid::vibratoPlacement);
219 338 : default:
220 338 : return SLine::propertyDefault(propertyId);
221 : }
222 : }
223 :
224 : //---------------------------------------------------------
225 : // accessibleInfo
226 : //---------------------------------------------------------
227 :
228 0 : String Vibrato::accessibleInfo() const
229 : {
230 0 : return String(u"%1: %2").arg(EngravingItem::accessibleInfo(), vibratoTypeUserName());
231 : }
232 : }
|