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 "connectorinforeader.h"
23 :
24 : #include "../../infrastructure/rtti.h"
25 :
26 : #include "../../dom/factory.h"
27 : #include "../../dom/score.h"
28 : #include "../../dom/engravingitem.h"
29 : #include "../../dom/spanner.h"
30 : #include "../../dom/chordrest.h"
31 : #include "../../dom/measure.h"
32 : #include "../../dom/note.h"
33 : #include "../../dom/noteline.h"
34 : #include "../../dom/tie.h"
35 : #include "../../dom/chord.h"
36 : #include "../../dom/staff.h"
37 :
38 : #include "../types/typesconv.h"
39 :
40 : #include "../xmlreader.h"
41 :
42 : #include "tread.h"
43 :
44 : #include "log.h"
45 :
46 : using namespace mu::engraving;
47 : using namespace mu::engraving::read460;
48 :
49 : //---------------------------------------------------------
50 : // ConnectorInfoReader
51 : //---------------------------------------------------------
52 :
53 336 : ConnectorInfoReader::ConnectorInfoReader(XmlReader& e, ReadContext* ctx, EngravingItem* current, int track)
54 336 : : ConnectorInfo(current, track), m_reader(&e), m_ctx(ctx), m_connector(nullptr), m_connectorReceiver(current)
55 336 : {}
56 :
57 : //---------------------------------------------------------
58 : // readPositionInfo
59 : //---------------------------------------------------------
60 :
61 0 : static Location readPositionInfo(ReadContext* ctx, int track)
62 : {
63 0 : Location info = ctx->location();
64 0 : info.setTrack(track);
65 0 : return info;
66 : }
67 :
68 : //---------------------------------------------------------
69 : // ConnectorInfoReader
70 : //---------------------------------------------------------
71 :
72 0 : ConnectorInfoReader::ConnectorInfoReader(XmlReader& e, ReadContext* ctx, Score* current, int track)
73 0 : : ConnectorInfo(current, readPositionInfo(ctx, track)), m_reader(&e), m_ctx(ctx), m_connector(nullptr), m_connectorReceiver(current)
74 : {
75 0 : setCurrentUpdated(true);
76 0 : }
77 :
78 : //---------------------------------------------------------
79 : // ConnectorInfoReader::read
80 : //---------------------------------------------------------
81 :
82 336 : bool ConnectorInfoReader::read()
83 : {
84 336 : XmlReader& e = *m_reader;
85 336 : const AsciiStringView name(e.asciiAttribute("type"));
86 336 : m_type = TConv::fromXml(name, ElementType::INVALID);
87 :
88 336 : m_ctx->fillLocation(m_currentLoc);
89 :
90 840 : while (e.readNextStartElement()) {
91 504 : const AsciiStringView tag(e.name());
92 :
93 504 : if (tag == "prev") {
94 168 : readEndpointLocation(m_prevLoc);
95 336 : } else if (tag == "next") {
96 168 : readEndpointLocation(m_nextLoc);
97 : } else {
98 168 : if (tag == name) {
99 168 : m_connector = Factory::createItemByName(tag, m_connectorReceiver->score()->dummy());
100 : } else {
101 0 : LOGW("ConnectorInfoReader::read: element tag (%s) does not match connector type (%s). Is the file corrupted?",
102 : tag.ascii(), name.ascii());
103 : }
104 :
105 168 : if (!m_connector) {
106 0 : e.unknown();
107 0 : return false;
108 : }
109 168 : m_connector->setTrack(m_currentLoc.track());
110 168 : TRead::readItem(m_connector, e, *m_ctx);
111 : }
112 : }
113 336 : return true;
114 : }
115 :
116 : //---------------------------------------------------------
117 : // ConnectorInfoReader::readEndpointLocation
118 : //---------------------------------------------------------
119 :
120 336 : void ConnectorInfoReader::readEndpointLocation(Location& l)
121 : {
122 336 : XmlReader& e = *m_reader;
123 672 : while (e.readNextStartElement()) {
124 336 : const AsciiStringView tag(e.name());
125 336 : if (tag == "location") {
126 336 : l = Location::relative();
127 336 : TRead::read(&l, e, *m_ctx);
128 : } else {
129 0 : e.unknown();
130 : }
131 : }
132 336 : }
133 :
134 0 : Fraction ConnectorInfoReader::curTick() const
135 : {
136 0 : return m_ctx->tick();
137 : }
138 :
139 : //---------------------------------------------------------
140 : // ConnectorInfoReader::update
141 : //---------------------------------------------------------
142 :
143 336 : void ConnectorInfoReader::update()
144 : {
145 336 : if (!currentUpdated()) {
146 336 : updateCurrentInfo(m_ctx->pasteMode());
147 : }
148 336 : if (hasPrevious()) {
149 168 : m_prevLoc.toAbsolute(m_currentLoc);
150 : }
151 336 : if (hasNext()) {
152 168 : m_nextLoc.toAbsolute(m_currentLoc);
153 : }
154 336 : }
155 :
156 : //---------------------------------------------------------
157 : // ConnectorInfoReader::addToScore
158 : //---------------------------------------------------------
159 :
160 : class ReadAddConnectorVisitor : public rtti::Visitor<ReadAddConnectorVisitor>
161 : {
162 : public:
163 :
164 : template<typename T>
165 804 : static bool doVisit(EngravingObject* item, ConnectorInfoReader* info, bool pasteMode)
166 : {
167 804 : if (T::classof(item) || (std::is_same<T, ChordRest>::value && item->isChordRest())) {
168 336 : ConnectorInfoReader::readAddConnector(static_cast<T*>(item), info, pasteMode);
169 336 : return true;
170 : }
171 468 : return false;
172 : }
173 : };
174 :
175 : using ReadAddConnectorTypes = rtti::TypeList<ChordRest, Measure, Note, Score>;
176 :
177 168 : void ConnectorInfoReader::addToScore(bool pasteMode)
178 : {
179 168 : ConnectorInfoReader* r = this;
180 192 : while (r->prev()) {
181 24 : r = r->prev();
182 : }
183 504 : while (r) {
184 336 : bool found = ReadAddConnectorVisitor::visit(ReadAddConnectorTypes {}, r->m_connectorReceiver, r, pasteMode);
185 336 : DO_ASSERT(found);
186 336 : r = r->next();
187 : }
188 168 : }
189 :
190 : //---------------------------------------------------------
191 : // ConnectorInfoReader::readConnector
192 : //---------------------------------------------------------
193 :
194 336 : void ConnectorInfoReader::readConnector(std::shared_ptr<ConnectorInfoReader> info, XmlReader& e, ReadContext& ctx)
195 : {
196 336 : if (!info->read()) {
197 0 : e.skipCurrentElement();
198 0 : return;
199 : }
200 336 : ctx.addConnectorInfoLater(info);
201 : }
202 :
203 : //---------------------------------------------------------
204 : // ConnectorInfoReader::connector
205 : //---------------------------------------------------------
206 :
207 336 : EngravingItem* ConnectorInfoReader::connector()
208 : {
209 : // connector should be contained in the first node normally.
210 336 : ConnectorInfo* i = findFirst();
211 336 : if (i) {
212 336 : return static_cast<ConnectorInfoReader*>(i)->m_connector;
213 : }
214 0 : return nullptr;
215 : }
216 :
217 : //---------------------------------------------------------
218 : // ConnectorInfoReader::connector
219 : //---------------------------------------------------------
220 :
221 0 : const EngravingItem* ConnectorInfoReader::connector() const
222 : {
223 0 : return const_cast<ConnectorInfoReader*>(this)->connector();
224 : }
225 :
226 : //---------------------------------------------------------
227 : // ConnectorInfoReader::releaseConnector
228 : //---------------------------------------------------------
229 :
230 0 : EngravingItem* ConnectorInfoReader::releaseConnector()
231 : {
232 0 : ConnectorInfoReader* i = static_cast<ConnectorInfoReader*>(findFirst());
233 0 : if (!i) {
234 : // circular connector?
235 0 : ConnectorInfoReader* ii = this;
236 0 : EngravingItem* c = nullptr;
237 0 : while (ii->prev()) {
238 0 : if (ii->m_connector) {
239 0 : c = ii->m_connector;
240 0 : ii->m_connector = nullptr;
241 : }
242 0 : ii = ii->prev();
243 0 : if (ii == this) {
244 0 : break;
245 : }
246 : }
247 0 : return c;
248 : }
249 0 : EngravingItem* c = i->m_connector;
250 0 : i->m_connector = nullptr;
251 0 : return c;
252 : }
253 :
254 60 : void ConnectorInfoReader::readAddConnector(ChordRest* item, ConnectorInfoReader* info, bool pasteMode)
255 : {
256 60 : const ElementType type = info->type();
257 60 : switch (type) {
258 60 : case ElementType::SLUR:
259 : case ElementType::HAMMER_ON_PULL_OFF:
260 : {
261 60 : Spanner* spanner = toSpanner(info->connector());
262 60 : const Location& l = info->location();
263 :
264 60 : if (info->isStart()) {
265 30 : spanner->setTrack(l.track());
266 : // trillCueNotes have unreliable tick() while reading so use instead tick from readContext
267 30 : Fraction startTick = item->isChord() && toChord(item)->isTrillCueNote() ? info->curTick() : item->tick();
268 30 : spanner->setTick(startTick);
269 30 : spanner->setStartElement(item);
270 30 : if (pasteMode) {
271 0 : item->score()->undoAddElement(spanner);
272 0 : for (EngravingObject* linkedSpanner : spanner->linkList()) {
273 0 : if (linkedSpanner == spanner) {
274 0 : continue;
275 : }
276 0 : Spanner* ls = toSpanner(linkedSpanner);
277 0 : ls->setTick(spanner->tick());
278 0 : for (EngravingObject* linkedCR : item->linkList()) {
279 0 : ChordRest* cr = toChordRest(linkedCR);
280 0 : if (cr->score() == linkedSpanner->score() && cr->staffIdx() == ls->staffIdx()) {
281 0 : ls->setTrack(cr->track());
282 0 : if (ls->isSlur()) {
283 0 : ls->setStartElement(cr);
284 : }
285 0 : break;
286 : }
287 0 : }
288 0 : }
289 : } else {
290 30 : item->score()->addSpanner(spanner);
291 : }
292 30 : } else if (info->isEnd()) {
293 30 : spanner->setTrack2(l.track());
294 30 : spanner->setTick2(item->tick());
295 30 : spanner->setEndElement(item);
296 30 : if (pasteMode) {
297 0 : for (EngravingObject* linkedSpanner : spanner->linkList()) {
298 0 : if (linkedSpanner == spanner) {
299 0 : continue;
300 : }
301 0 : Spanner* ls = toSpanner(linkedSpanner);
302 0 : ls->setTick2(spanner->tick2());
303 0 : for (EngravingObject* linkedCR : item->linkList()) {
304 0 : ChordRest* cr = toChordRest(linkedCR);
305 0 : if (cr->score() == linkedSpanner->score() && cr->staffIdx() == ls->staffIdx()) {
306 0 : ls->setTrack2(cr->track());
307 0 : if (ls->isSlur()) {
308 0 : ls->setEndElement(cr);
309 : }
310 0 : break;
311 : }
312 0 : }
313 0 : }
314 : }
315 : } else {
316 0 : LOGD("ChordRest::readAddConnector(): Slur end is neither start nor end");
317 : }
318 : }
319 60 : break;
320 0 : default:
321 0 : break;
322 : }
323 60 : }
324 :
325 84 : void ConnectorInfoReader::readAddConnector(Measure* item, ConnectorInfoReader* info, bool pasteMode)
326 : {
327 84 : const ElementType type = info->type();
328 84 : switch (type) {
329 84 : case ElementType::HAIRPIN:
330 : case ElementType::PEDAL:
331 : case ElementType::OTTAVA:
332 : case ElementType::TRILL:
333 : case ElementType::TEXTLINE:
334 : case ElementType::LET_RING:
335 : case ElementType::GRADUAL_TEMPO_CHANGE:
336 : case ElementType::VIBRATO:
337 : case ElementType::PALM_MUTE:
338 : case ElementType::PARTIAL_LYRICSLINE:
339 : case ElementType::WHAMMY_BAR:
340 : case ElementType::RASGUEADO:
341 : case ElementType::HARMONIC_MARK:
342 : case ElementType::PICK_SCRAPE:
343 : case ElementType::VOLTA:
344 : {
345 84 : Spanner* sp = toSpanner(info->connector());
346 84 : const Location& l = info->location();
347 84 : Fraction lTick = l.frac();
348 84 : Fraction spTick = pasteMode ? lTick : (item->tick() + lTick);
349 84 : if (info->isStart()) {
350 42 : sp->setTrack(l.track());
351 42 : sp->setTrack2(sp->track());
352 42 : sp->setTick(spTick);
353 : // Defer computing the end element: the end anchor (tick2/track2) is only known once the
354 : // matching end connector is processed (below). Computing it here would cache a wrong value
355 : // (tick2 == tick) that nothing recomputes until layout. See readAddConnector end branch.
356 42 : item->score()->addSpanner(sp, /*computeStartEnd=*/ false);
357 42 : sp->computeStartElement();
358 42 : } else if (info->isEnd()) {
359 42 : sp->setTrack2(l.track());
360 42 : sp->setTick2(spTick);
361 : // Now that the end anchor is set, compute the end element once with the final value.
362 42 : sp->computeEndElement();
363 : }
364 : }
365 84 : break;
366 0 : default:
367 0 : break;
368 : }
369 84 : }
370 :
371 192 : void ConnectorInfoReader::readAddConnector(Note* item, ConnectorInfoReader* info, bool pasteMode)
372 : {
373 192 : const ElementType type = info->type();
374 192 : const Location& l = info->location();
375 192 : switch (type) {
376 192 : case ElementType::TIE:
377 : case ElementType::TEXTLINE:
378 : case ElementType::GLISSANDO:
379 : case ElementType::GUITAR_BEND:
380 : case ElementType::NOTELINE:
381 : {
382 192 : Spanner* sp = toSpanner(info->connector());
383 192 : if (info->isStart()) {
384 96 : sp->setTrack(l.track());
385 96 : sp->setTick(item->tick());
386 96 : if (sp->isTie()) {
387 25 : Note* n = item;
388 25 : while (n->tieFor()) {
389 0 : n = n->tieFor()->endNote();
390 : }
391 25 : Tie* tie = toTie(sp);
392 25 : tie->setOwnershipParent(n);
393 25 : tie->setStartNote(n);
394 25 : n->setTieFor(tie);
395 : } else {
396 71 : sp->setStartElement(item);
397 71 : item->addSpannerFor(sp);
398 71 : sp->setOwnershipParent(item);
399 : }
400 96 : } else if (info->isEnd()) {
401 96 : sp->setTrack2(l.track());
402 96 : sp->setTick2(item->tick());
403 96 : sp->setEndElement(item);
404 96 : if (sp->isTie()) {
405 25 : Tie* tie = toTie(sp);
406 25 : item->setTieBack(tie);
407 25 : if (pasteMode) {
408 0 : tie->updatePossibleJumpPoints();
409 : }
410 : } else {
411 71 : item->addSpannerBack(sp);
412 : }
413 :
414 : // As spanners get added after being fully read, they
415 : // do not get cloned with the note when pasting to
416 : // linked staves. So add this spanner explicitly.
417 96 : if (pasteMode) {
418 0 : item->score()->undoAddElement(sp);
419 : }
420 : }
421 : }
422 : default:
423 192 : break;
424 : }
425 192 : }
426 :
427 0 : void ConnectorInfoReader::readAddConnector(Score* item, ConnectorInfoReader* info, bool pasteMode)
428 : {
429 0 : if (!pasteMode) {
430 : // How did we get there?
431 0 : LOGD("Score::readAddConnector is called not in paste mode.");
432 0 : return;
433 : }
434 :
435 0 : if (info->connector()->systemFlag()) {
436 0 : return;
437 : }
438 :
439 0 : const ElementType type = info->type();
440 0 : switch (type) {
441 0 : case ElementType::HAIRPIN:
442 : case ElementType::PEDAL:
443 : case ElementType::OTTAVA:
444 : case ElementType::TRILL:
445 : case ElementType::TEXTLINE:
446 : case ElementType::PALM_MUTE:
447 : case ElementType::PARTIAL_LYRICSLINE:
448 : case ElementType::WHAMMY_BAR:
449 : case ElementType::RASGUEADO:
450 : case ElementType::HARMONIC_MARK:
451 : case ElementType::PICK_SCRAPE:
452 : case ElementType::LET_RING:
453 : case ElementType::VIBRATO:
454 : {
455 0 : Spanner* sp = toSpanner(info->connector());
456 0 : const Location& l = info->location();
457 0 : if (info->isStart()) {
458 0 : sp->setTrack(l.track());
459 0 : sp->setTrack2(l.track());
460 0 : sp->setTick(l.frac());
461 0 : } else if (info->isEnd()) {
462 0 : sp->setTick2(l.frac());
463 0 : item->undoAddElement(sp);
464 0 : if (sp->isOttava()) {
465 0 : sp->staff()->updateOttava();
466 : }
467 : }
468 : }
469 0 : break;
470 0 : default:
471 0 : break;
472 : }
473 : }
|