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 :
23 : #include "ottava.h"
24 :
25 : #include "types/translatablestring.h"
26 :
27 : #include "chordrest.h"
28 : #include "dummyparent.h"
29 : #include "measure.h"
30 : #include "score.h"
31 : #include "staff.h"
32 : #include "system.h"
33 : #include "text.h"
34 :
35 : #include "log.h"
36 :
37 : using namespace mu::engraving;
38 :
39 : //---------------------------------------------------------
40 : // ottavaStyle
41 : //---------------------------------------------------------
42 :
43 : static const ElementStyle ottavaStyle {
44 : { Sid::ottavaNumbersOnly, Pid::NUMBERS_ONLY },
45 : { Sid::ottava8VAPlacement, Pid::PLACEMENT },
46 : { Sid::ottava8VAText, Pid::BEGIN_TEXT },
47 : { Sid::ottava8VAContinueText, Pid::CONTINUE_TEXT },
48 : { Sid::ottavaHookAbove, Pid::END_HOOK_HEIGHT },
49 : { Sid::ottavaFontFace, Pid::BEGIN_FONT_FACE },
50 : { Sid::ottavaFontFace, Pid::CONTINUE_FONT_FACE },
51 : { Sid::ottavaFontFace, Pid::END_FONT_FACE },
52 : { Sid::ottavaFontSize, Pid::BEGIN_FONT_SIZE },
53 : { Sid::ottavaFontSize, Pid::CONTINUE_FONT_SIZE },
54 : { Sid::ottavaFontSize, Pid::END_FONT_SIZE },
55 : { Sid::ottavaFontStyle, Pid::BEGIN_FONT_STYLE },
56 : { Sid::ottavaFontStyle, Pid::CONTINUE_FONT_STYLE },
57 : { Sid::ottavaFontStyle, Pid::END_FONT_STYLE },
58 : { Sid::ottavaMusicalSymbolsScale, Pid::BEGIN_TEXT_MUSICAL_SYMBOLS_SCALE },
59 : { Sid::ottavaMusicalSymbolsScale, Pid::CONTINUE_TEXT_MUSICAL_SYMBOLS_SCALE },
60 : { Sid::ottavaMusicalSymbolsScale, Pid::END_TEXT_MUSICAL_SYMBOLS_SCALE },
61 : { Sid::dummyMusicalSymbolSize, Pid::BEGIN_TEXT_MUSIC_SYMBOLS_SIZE },
62 : { Sid::dummyMusicalSymbolSize, Pid::CONTINUE_TEXT_MUSIC_SYMBOLS_SIZE },
63 : { Sid::dummyMusicalSymbolSize, Pid::END_TEXT_MUSIC_SYMBOLS_SIZE },
64 : { Sid::ottavaTextAlignAbove, Pid::BEGIN_TEXT_ALIGN },
65 : { Sid::ottavaTextAlignAbove, Pid::CONTINUE_TEXT_ALIGN },
66 : { Sid::ottavaTextAlignAbove, Pid::END_TEXT_ALIGN },
67 : { Sid::ottavaPosition, Pid::BEGIN_TEXT_POSITION },
68 : { Sid::ottavaPosition, Pid::CONTINUE_TEXT_POSITION },
69 : { Sid::ottavaPosition, Pid::END_TEXT_POSITION },
70 : { Sid::ottavaLineWidth, Pid::LINE_WIDTH },
71 : { Sid::ottavaLineStyle, Pid::LINE_STYLE },
72 : { Sid::ottavaDashLineLen, Pid::DASH_LINE_LEN },
73 : { Sid::ottavaDashGapLen, Pid::DASH_GAP_LEN },
74 : { Sid::ottavaFontSpatiumDependent, Pid::TEXT_SIZE_SPATIUM_DEPENDENT },
75 : { Sid::ottavaEndLineArrowHeight, Pid::END_LINE_ARROW_HEIGHT },
76 : { Sid::ottavaEndLineArrowWidth, Pid::END_LINE_ARROW_WIDTH },
77 : { Sid::ottavaBeginLineArrowHeight, Pid::BEGIN_LINE_ARROW_HEIGHT },
78 : { Sid::ottavaBeginLineArrowWidth, Pid::BEGIN_LINE_ARROW_WIDTH },
79 : { Sid::ottavaEndFilledArrowHeight, Pid::END_FILLED_ARROW_HEIGHT },
80 : { Sid::ottavaEndFilledArrowWidth, Pid::END_FILLED_ARROW_WIDTH },
81 : { Sid::ottavaBeginFilledArrowHeight, Pid::BEGIN_FILLED_ARROW_HEIGHT },
82 : { Sid::ottavaBeginFilledArrowWidth, Pid::BEGIN_FILLED_ARROW_WIDTH },
83 : };
84 :
85 110 : OttavaSegment::OttavaSegment(Ottava* sp)
86 110 : : TextLineBaseSegment(ElementType::OTTAVA_SEGMENT, sp, ElementFlag::MOVABLE | ElementFlag::ON_STAFF)
87 : {
88 110 : m_text->setTextStyleType(propertyDefault(Pid::TEXT_STYLE).value<TextStyleType>());
89 110 : m_endText->setTextStyleType(propertyDefault(Pid::TEXT_STYLE).value<TextStyleType>());
90 110 : }
91 :
92 : //---------------------------------------------------------
93 : // propertyDelegate
94 : //---------------------------------------------------------
95 :
96 883 : PropertyDelegate OttavaSegment::resolvePropertyDelegate(Pid pid) const
97 : {
98 883 : if (pid == Pid::OTTAVA_TYPE || pid == Pid::NUMBERS_ONLY) {
99 0 : return PropertyDelegate(spanner());
100 : }
101 883 : return TextLineBaseSegment::resolvePropertyDelegate(pid);
102 : }
103 :
104 : //---------------------------------------------------------
105 : // setOttavaType
106 : //---------------------------------------------------------
107 :
108 252 : void Ottava::setOttavaType(OttavaType val)
109 : {
110 252 : if (m_ottavaType == val) {
111 213 : return;
112 : }
113 39 : m_ottavaType = val;
114 39 : styleChanged();
115 : }
116 :
117 : //---------------------------------------------------------
118 : // setNumbersOnly
119 : //---------------------------------------------------------
120 :
121 0 : void Ottava::setNumbersOnly(bool val)
122 : {
123 0 : m_numbersOnly = val;
124 0 : }
125 :
126 : //---------------------------------------------------------
127 : // setPlacement
128 : //---------------------------------------------------------
129 :
130 0 : void Ottava::setPlacement(PlacementV p)
131 : {
132 0 : TextLineBase::setPlacement(p);
133 0 : }
134 :
135 : //---------------------------------------------------------
136 : // undoChangeProperty
137 : //---------------------------------------------------------
138 :
139 0 : void OttavaSegment::undoChangeProperty(Pid id, const PropertyValue& v, PropertyFlags ps)
140 : {
141 0 : if (id == Pid::OTTAVA_TYPE || id == Pid::NUMBERS_ONLY) {
142 0 : EngravingObject::undoChangeProperty(id, v, ps);
143 : } else {
144 0 : EngravingObject::undoChangeProperty(id, v, ps);
145 : }
146 0 : }
147 :
148 5 : void Ottava::undoChangeProperty(Pid id, const PropertyValue& v, PropertyFlags ps)
149 : {
150 5 : if (id == Pid::OTTAVA_TYPE || id == Pid::NUMBERS_ONLY) {
151 0 : TextLineBase::undoChangeProperty(id, v, ps);
152 0 : styleChanged(); // these properties may change style settings
153 : } else {
154 5 : TextLineBase::undoChangeProperty(id, v, ps);
155 : }
156 5 : }
157 :
158 9491 : Sid Ottava::getPropertyStyle(Pid pid) const
159 : {
160 : static_assert(int(OttavaType::OTTAVA_22MB) - int(OttavaType::OTTAVA_8VA) == 5);
161 :
162 : static const std::vector<Sid> ss = {
163 : Sid::ottava8VAPlacement,
164 : Sid::ottava8VAnoText,
165 : Sid::ottava8VAnoContinueText,
166 : Sid::ottava8VBPlacement,
167 : Sid::ottava8VBnoText,
168 : Sid::ottava8VBnoContinueText,
169 : Sid::ottava15MAPlacement,
170 : Sid::ottava15MAnoText,
171 : Sid::ottava15MAnoContinueText,
172 : Sid::ottava15MBPlacement,
173 : Sid::ottava15MBnoText,
174 : Sid::ottava15MBnoContinueText,
175 : Sid::ottava22MAPlacement,
176 : Sid::ottava22MAnoText,
177 : Sid::ottava22MAnoContinueText,
178 : Sid::ottava22MBPlacement,
179 : Sid::ottava22MBnoText,
180 : Sid::ottava22MBnoContinueText,
181 :
182 : Sid::ottava8VAPlacement,
183 : Sid::ottava8VAText,
184 : Sid::ottava8VAContinueText,
185 : Sid::ottava8VBPlacement,
186 : Sid::ottava8VBText,
187 : Sid::ottava8VBContinueText,
188 : Sid::ottava15MAPlacement,
189 : Sid::ottava15MAText,
190 : Sid::ottava15MAContinueText,
191 : Sid::ottava15MBPlacement,
192 : Sid::ottava15MBText,
193 : Sid::ottava15MBContinueText,
194 : Sid::ottava22MAPlacement,
195 : Sid::ottava22MAText,
196 : Sid::ottava22MAContinueText,
197 : Sid::ottava22MBPlacement,
198 : Sid::ottava22MBText,
199 : Sid::ottava22MBContinueText,
200 9501 : };
201 :
202 9491 : size_t idx = size_t(m_ottavaType) * 3 + (m_numbersOnly ? 0 : ss.size() / 2);
203 9491 : switch (pid) {
204 243 : case Pid::PLACEMENT:
205 243 : return ss[idx];
206 239 : case Pid::BEGIN_TEXT:
207 239 : return ss[idx + 1]; // BEGIN_TEXT
208 239 : case Pid::CONTINUE_TEXT:
209 239 : return ss[idx + 2]; // CONTINUE_TEXT
210 239 : case Pid::END_HOOK_HEIGHT:
211 239 : if (isStyled(Pid::PLACEMENT)) {
212 238 : return style().styleV(ss[idx]).value<PlacementV>() == PlacementV::ABOVE
213 238 : ? Sid::ottavaHookAbove
214 238 : : Sid::ottavaHookBelow;
215 : } else {
216 1 : return placeAbove() ? Sid::ottavaHookAbove : Sid::ottavaHookBelow;
217 : }
218 714 : case Pid::BEGIN_TEXT_ALIGN:
219 : case Pid::CONTINUE_TEXT_ALIGN:
220 : case Pid::END_TEXT_ALIGN:
221 714 : return placeAbove() ? Sid::ottavaTextAlignAbove : Sid::ottavaTextAlignBelow;
222 7817 : default:
223 7817 : return TextLineBase::getPropertyStyle(pid);
224 : }
225 : }
226 :
227 : //---------------------------------------------------------
228 : // Ottava
229 : //---------------------------------------------------------
230 :
231 187 : Ottava::Ottava(DummyParent* parent)
232 187 : : TextLineBase(ElementType::OTTAVA, parent, ElementFlag::ON_STAFF | ElementFlag::MOVABLE)
233 : {
234 187 : m_ottavaType = OttavaType::OTTAVA_8VA;
235 187 : m_numbersOnly = false;
236 187 : setBeginTextPlace(TextPlace::LEFT);
237 187 : setContinueTextPlace(TextPlace::LEFT);
238 187 : setEndHookType(HookType::HOOK_90);
239 187 : setLineVisible(true);
240 187 : setBeginHookHeight(0_sp);
241 187 : setEndText(u"");
242 :
243 187 : initElementStyle(&ottavaStyle);
244 187 : }
245 :
246 4 : Ottava::Ottava(const Ottava& o)
247 4 : : TextLineBase(o)
248 : {
249 4 : setOttavaType(o.m_ottavaType);
250 4 : m_numbersOnly = o.m_numbersOnly;
251 4 : }
252 :
253 : //---------------------------------------------------------
254 : // pitchShift
255 : //---------------------------------------------------------
256 :
257 223 : int Ottava::pitchShift() const
258 : {
259 223 : return ottavaDefault[int(m_ottavaType)].shift;
260 : }
261 :
262 : //---------------------------------------------------------
263 : // createLineSegment
264 : //---------------------------------------------------------
265 :
266 : static const ElementStyle ottavaSegmentStyle {
267 : { Sid::ottavaMinDistance, Pid::MIN_DISTANCE },
268 : };
269 :
270 110 : LineSegment* Ottava::createLineSegment()
271 : {
272 110 : OttavaSegment* os = new OttavaSegment(this);
273 110 : os->setTrack(track());
274 110 : os->initElementStyle(&ottavaSegmentStyle);
275 110 : return os;
276 : }
277 :
278 : //---------------------------------------------------------
279 : // getProperty
280 : //---------------------------------------------------------
281 :
282 2814 : PropertyValue Ottava::getProperty(Pid propertyId) const
283 : {
284 2814 : switch (propertyId) {
285 108 : case Pid::OTTAVA_TYPE:
286 108 : return int(ottavaType());
287 :
288 6 : case Pid::NUMBERS_ONLY:
289 6 : return m_numbersOnly;
290 :
291 108 : case Pid::END_TEXT_PLACE: // HACK
292 108 : return TextPlace::LEFT;
293 :
294 2592 : default:
295 2592 : break;
296 : }
297 2592 : return TextLineBase::getProperty(propertyId);
298 : }
299 :
300 : //---------------------------------------------------------
301 : // setProperty
302 : //---------------------------------------------------------
303 :
304 9359 : bool Ottava::setProperty(Pid propertyId, const PropertyValue& val)
305 : {
306 9359 : switch (propertyId) {
307 0 : case Pid::PLAY:
308 0 : setPlaySpanner(val.toBool());
309 0 : staff()->updateOttava();
310 0 : break;
311 :
312 4 : case Pid::OTTAVA_TYPE:
313 4 : setOttavaType(OttavaType(val.toInt()));
314 4 : break;
315 :
316 238 : case Pid::NUMBERS_ONLY:
317 238 : m_numbersOnly = val.toBool();
318 238 : break;
319 :
320 4 : case Pid::SPANNER_TICKS:
321 4 : setTicks(val.value<Fraction>());
322 4 : staff()->updateOttava();
323 4 : break;
324 :
325 2 : case Pid::SPANNER_TICK:
326 2 : setTick(val.value<Fraction>());
327 2 : staff()->updateOttava();
328 2 : break;
329 :
330 9111 : default:
331 9111 : if (!TextLineBase::setProperty(propertyId, val)) {
332 0 : return false;
333 : }
334 9111 : break;
335 : }
336 9359 : triggerLayout();
337 9359 : return true;
338 : }
339 :
340 : //---------------------------------------------------------
341 : // propertyDefault
342 : //---------------------------------------------------------
343 :
344 2902 : PropertyValue Ottava::propertyDefault(Pid pid) const
345 : {
346 2902 : switch (pid) {
347 108 : case Pid::OTTAVA_TYPE:
348 108 : return PropertyValue();
349 108 : case Pid::END_HOOK_TYPE:
350 108 : return HookType::HOOK_90;
351 108 : case Pid::LINE_VISIBLE:
352 108 : return true;
353 324 : case Pid::BEGIN_TEXT_OFFSET:
354 : case Pid::CONTINUE_TEXT_OFFSET:
355 : case Pid::END_TEXT_OFFSET:
356 324 : return PropertyValue::fromValue(PointF());
357 324 : case Pid::BEGIN_TEXT_PLACE:
358 : case Pid::CONTINUE_TEXT_PLACE:
359 : case Pid::END_TEXT_PLACE:
360 324 : return TextPlace::LEFT;
361 108 : case Pid::BEGIN_HOOK_TYPE:
362 108 : return HookType::NONE;
363 108 : case Pid::BEGIN_HOOK_HEIGHT:
364 108 : return 0_sp;
365 108 : case Pid::END_TEXT:
366 216 : return String();
367 7 : case Pid::PLACEMENT:
368 7 : return styleValue(Pid::PLACEMENT, getPropertyStyle(Pid::PLACEMENT));
369 220 : case Pid::TEXT_STYLE:
370 220 : return TextStyleType::OTTAVA;
371 :
372 1379 : default:
373 1379 : return TextLineBase::propertyDefault(pid);
374 : }
375 : }
376 :
377 : //---------------------------------------------------------
378 : // accessibleInfo
379 : //---------------------------------------------------------
380 :
381 0 : String Ottava::accessibleInfo() const
382 : {
383 0 : return String(u"%1: %2").arg(EngravingItem::accessibleInfo(), String::fromUtf8(ottavaDefault[static_cast<int>(ottavaType())].name));
384 : }
385 :
386 : //---------------------------------------------------------
387 : // subtypeUserName
388 : //---------------------------------------------------------
389 :
390 0 : muse::TranslatableString Ottava::subtypeUserName() const
391 : {
392 0 : return ottavaDefault[int(ottavaType())].userName;
393 : }
394 :
395 0 : void OttavaSegment::rebaseOffsetsOnAnchorChanged(Grip grip, const PointF& oldPos, System* sys)
396 : {
397 0 : if (grip == Grip::MIDDLE || grip == Grip::END) {
398 0 : ottava()->computeEndElement();
399 : }
400 0 : LineSegment::rebaseOffsetsOnAnchorChanged(grip, oldPos, sys);
401 0 : }
402 :
403 : //---------------------------------------------------------
404 : // ottavaTypeName
405 : //---------------------------------------------------------
406 :
407 108 : const char* Ottava::ottavaTypeName(OttavaType type)
408 : {
409 108 : return ottavaDefault[int(type)].name;
410 : }
411 :
412 308 : PointF Ottava::linePos(Grip grip, System** system) const
413 : {
414 308 : if (grip == Grip::START) {
415 155 : return TextLineBase::linePos(grip, system);
416 : }
417 :
418 153 : bool extendToEndOfDuration = false; // TODO: style
419 153 : if (extendToEndOfDuration) {
420 0 : return SLine::linePos(grip, system);
421 : }
422 :
423 153 : ChordRest* endCr = endElement() && endElement()->isChordRest() ? toChordRest(endElement()) : nullptr;
424 153 : if (!endCr) {
425 0 : return PointF();
426 : }
427 :
428 153 : Segment* seg = endCr->segment();
429 :
430 153 : *system = seg->measure()->system();
431 :
432 : // End 1sp after the right edge of the end chord, but don't overlap followig segments
433 153 : Shape staffShape = seg->staffShape(endCr->vStaffIdx());
434 556 : staffShape.remove_if([](ShapeElement& el) { return el.height() == 0; });
435 153 : double x = staffShape.right() + seg->x() + seg->measure()->x() + spatium();
436 153 : Segment* followingCRseg = score()->tick2segment(endCr->endTick(), true, SegmentType::ChordRest);
437 153 : if (followingCRseg && followingCRseg->system() == seg->system()) {
438 141 : x = std::min(x, followingCRseg->x() + followingCRseg->measure()->x());
439 : }
440 :
441 153 : x -= 0.5 * absoluteFromSpatium(lineWidth());
442 :
443 153 : return PointF(x, 0.0);
444 153 : }
445 :
446 313 : void Ottava::doComputeEndElement()
447 : {
448 313 : setEndElement(score()->findChordRestEndingBeforeTickInStaff(tick2(), track2staff(track())));
449 313 : }
450 :
451 165 : Sid Ottava::defaultPosSid() const
452 : {
453 165 : return placeAbove() ? Sid::ottavaPosAbove : Sid::ottavaPosBelow;
454 : }
|