LCOV - code coverage report
Current view: top level - src/engraving/dom - trill.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 74.6 % 213 159
Test Date: 2026-11-19 04:14:36

            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 "trill.h"
      24              : 
      25              : #include <cmath>
      26              : 
      27              : #include "../editing/addremoveelement.h"
      28              : #include "types/typesconv.h"
      29              : 
      30              : #include "dummyparent.h"
      31              : #include "iengravingfont.h"
      32              : 
      33              : #include "accidental.h"
      34              : #include "chord.h"
      35              : #include "clef.h"
      36              : #include "factory.h"
      37              : #include "measure.h"
      38              : #include "ornament.h"
      39              : #include "score.h"
      40              : #include "system.h"
      41              : 
      42              : using namespace mu::engraving;
      43              : 
      44              : //---------------------------------------------------------
      45              : //   trillStyle
      46              : //---------------------------------------------------------
      47              : 
      48              : static const ElementStyle trillStyle {
      49              :     { Sid::trillPlacement, Pid::PLACEMENT },
      50              : };
      51              : 
      52           63 : TrillSegment::TrillSegment(Trill* sp)
      53           63 :     : LineSegment(ElementType::TRILL_SEGMENT, sp, ElementFlag::MOVABLE | ElementFlag::ON_STAFF)
      54              : {
      55           63 : }
      56              : 
      57              : //---------------------------------------------------------
      58              : //   remove
      59              : //---------------------------------------------------------
      60              : 
      61            0 : void TrillSegment::remove(EngravingItem* e)
      62              : {
      63            0 :     if (trill()->accidental() == e) {
      64              :         // accidental is part of trill
      65            0 :         trill()->setAccidental(nullptr);
      66            0 :         e->removed();
      67              :     }
      68            0 : }
      69              : 
      70              : //---------------------------------------------------------
      71              : //   symbolLine
      72              : //---------------------------------------------------------
      73              : 
      74          108 : void TrillSegment::symbolLine(SymId start, SymId fill)
      75              : {
      76          108 :     double x1 = 0.0;
      77          108 :     double x2 = pos2().x();
      78          108 :     double w   = x2 - x1;
      79          108 :     double mag = magS();
      80          108 :     IEngravingFontPtr f = score()->engravingFont();
      81              : 
      82          108 :     m_symbols.clear();
      83          108 :     m_symbols.push_back(start);
      84          108 :     double w1 = f->advance(start, mag);
      85          108 :     double w2 = f->advance(fill, mag);
      86          108 :     int n    = lrint((w - w1) / w2);
      87         1491 :     for (int i = 0; i < n; ++i) {
      88         1383 :         m_symbols.push_back(fill);
      89              :     }
      90          108 :     RectF r(f->bbox(m_symbols, mag));
      91          108 :     setbbox(r);
      92          108 : }
      93              : 
      94            8 : void TrillSegment::symbolLine(SymId start, SymId fill, SymId end)
      95              : {
      96            8 :     double x1 = 0.0;
      97            8 :     double x2 = pos2().x();
      98            8 :     double w   = x2 - x1;
      99            8 :     double mag = magS();
     100            8 :     IEngravingFontPtr f = score()->engravingFont();
     101              : 
     102            8 :     m_symbols.clear();
     103            8 :     m_symbols.push_back(start);
     104            8 :     double w1 = f->advance(start, mag);
     105            8 :     double w2 = f->advance(fill, mag);
     106            8 :     double w3 = f->advance(end, mag);
     107            8 :     int n    = lrint((w - w1 - w3) / w2);
     108          104 :     for (int i = 0; i < n; ++i) {
     109           96 :         m_symbols.push_back(fill);
     110              :     }
     111            8 :     m_symbols.push_back(end);
     112            8 :     RectF r(f->bbox(m_symbols, mag));
     113            8 :     setbbox(r);
     114            8 : }
     115              : 
     116            0 : void TrillSegment::rebaseAnchors(EditData& ed, Grip grip)
     117              : {
     118            0 :     EngravingItem* startElement = spanner()->startElement();
     119            0 :     if (startElement && startElement->isChord() && toChord(startElement)->staffMove() != 0) {
     120              :         // This trill is on a cross-staff chord. Don't try to rebase its anchors when dragging.
     121            0 :         return;
     122              :     }
     123              : 
     124            0 :     LineSegment::rebaseAnchors(ed, grip);
     125              : }
     126              : 
     127              : //---------------------------------------------------------
     128              : //   scanElements
     129              : //---------------------------------------------------------
     130              : 
     131            1 : void TrillSegment::scanElements(std::function<void(EngravingItem*)> func)
     132              : {
     133            1 :     func(this);
     134            1 :     if (isSingleType() || isBeginType()) {
     135            1 :         Accidental* a = trill()->accidental();
     136            1 :         if (a) {
     137            0 :             func(a);
     138              :         }
     139            1 :         Chord* cueNoteChord = trill()->cueNoteChord();
     140            1 :         if (cueNoteChord) {
     141            0 :             cueNoteChord->scanElements(func);
     142              :         }
     143              :     }
     144            1 : }
     145              : 
     146              : //---------------------------------------------------------
     147              : //   propertyDelegate
     148              : //---------------------------------------------------------
     149              : 
     150          195 : PropertyDelegate TrillSegment::resolvePropertyDelegate(Pid pid) const
     151              : {
     152          195 :     if (pid == Pid::TRILL_TYPE || pid == Pid::ORNAMENT_STYLE || pid == Pid::PLACEMENT) {
     153            0 :         return PropertyDelegate(spanner());
     154              :     }
     155          195 :     return LineSegment::resolvePropertyDelegate(pid);
     156              : }
     157              : 
     158              : //---------------------------------------------------------
     159              : //   Trill
     160              : //---------------------------------------------------------
     161              : 
     162           67 : Trill::Trill(DummyParent* parent)
     163           67 :     : SLine(ElementType::TRILL, parent)
     164              : {
     165           67 :     m_trillType     = TrillType::TRILL_LINE;
     166           67 :     m_ornament = nullptr;
     167           67 :     m_accidental = nullptr;
     168           67 :     m_cueNoteChord = nullptr;
     169           67 :     m_ornamentStyle = OrnamentStyle::DEFAULT;
     170           67 :     initElementStyle(&trillStyle);
     171           67 : }
     172              : 
     173            2 : Trill::Trill(const Trill& t)
     174            2 :     : SLine(t)
     175              : {
     176            2 :     m_trillType = t.m_trillType;
     177            2 :     m_ornament = t.m_ornament ? t.m_ornament->clone() : nullptr;
     178            2 :     m_ornamentStyle = t.m_ornamentStyle;
     179            2 :     initElementStyle(&trillStyle);
     180            2 : }
     181              : 
     182            2 : EngravingItem* Trill::linkedClone()
     183              : {
     184            2 :     Trill* linkedTrill = clone();
     185            2 :     Ornament* linkedOrnament = toOrnament(m_ornament->linkedClone());
     186            2 :     linkedTrill->setOrnament(linkedOrnament);
     187            2 :     linkedTrill->setAutoplace(true);
     188            2 :     score()->undo(new Link(linkedTrill, this));
     189            2 :     return linkedTrill;
     190              : }
     191              : 
     192              : //---------------------------------------------------------
     193              : //   remove
     194              : //---------------------------------------------------------
     195              : 
     196            0 : void Trill::remove(EngravingItem* e)
     197              : {
     198            0 :     if (e == m_accidental) {
     199            0 :         m_accidental = nullptr;
     200            0 :         e->removed();
     201              :     }
     202            0 : }
     203              : 
     204          115 : void Trill::setTrack(track_idx_t n)
     205              : {
     206          115 :     EngravingItem::setTrack(n);
     207              : 
     208          118 :     for (SpannerSegment* ss : spannerSegments()) {
     209            3 :         ss->setTrack(n);
     210              :     }
     211              : 
     212          115 :     if (m_ornament) {
     213           54 :         m_ornament->setTrack(n);
     214              :     }
     215          115 : }
     216              : 
     217            2 : void Trill::setScore(Score* s)
     218              : {
     219            2 :     Spanner::setScore(s);
     220            2 :     if (m_ornament) {
     221            2 :         m_ornament->setScore(s);
     222              :     }
     223            2 : }
     224              : 
     225          212 : void Trill::computeStartElement()
     226              : {
     227          212 :     Spanner::computeStartElement();
     228          212 :     if (startElement() && startElement()->isChord() && m_ornament) {
     229          194 :         m_ornament->setOwnershipParent(toChord(startElement()));
     230              : 
     231          194 :         Chord* cueChord = m_ornament->cueNoteChord();
     232          194 :         if (cueChord) {
     233           13 :             cueChord->setOwnershipParent(toChord(startElement())->segment());
     234              :         }
     235              :     }
     236          212 : }
     237              : 
     238          286 : PointF Trill::trillLinePos(const SLine* line, Grip grip, System** system)
     239              : {
     240          286 :     if (!line) {
     241            0 :         return PointF();
     242              :     }
     243              : 
     244          286 :     bool start = grip == Grip::START;
     245          286 :     bool mmRest = line->style().styleB(Sid::createMultiMeasureRests);
     246          286 :     double graceOffset = 0.0;
     247          286 :     double clefOffset = 0.0;
     248              : 
     249          286 :     Segment* segment = start ? line->startSegment() : line->endSegment();
     250          286 :     if (!segment) {
     251            0 :         return PointF();
     252              :     }
     253              : 
     254          286 :     if (start) {
     255          143 :         *system = segment->measure()->system();
     256          143 :         double x = segment->x() + segment->measure()->x();
     257          143 :         return PointF(x, 0.0);
     258              :     }
     259              : 
     260          143 :     Segment* clefSeg = segment->isClefType() ? segment : nullptr;
     261          143 :     Fraction curTick = segment->tick();
     262              :     while (true) {
     263          219 :         Segment* prevSeg = mmRest ? segment->prev1MM() : segment->prev1();
     264          219 :         if (prevSeg && prevSeg->tick() == curTick) {
     265           76 :             clefSeg = prevSeg->isClefType() ? prevSeg : clefSeg;
     266           76 :             segment = prevSeg;
     267              :         } else {
     268          143 :             break;
     269              :         }
     270           76 :     }
     271              : 
     272              :     // Stop line before clefs
     273          143 :     if (clefSeg) {
     274            0 :         EngravingItem* clefItem = clefSeg->element(line->track2());
     275            0 :         if (clefItem && clefItem->isClef()) {
     276            0 :             Clef* clef = toClef(clefItem);
     277            0 :             SymId clefSym = ClefInfo::symId(clef->clefType());
     278            0 :             Shape clefShape = line->symShapeWithCutouts(clefSym).translated(clef->pos());
     279            0 :             clefOffset = segment->pageX() - clef->pageX() + clefShape.leftMostEdgeAtTop();
     280            0 :         }
     281              :     }
     282              : 
     283              :     // Stop line before grace notes
     284          143 :     if (const EngravingItem* preAppendedItem = segment->preAppendedItem(line->track2());
     285          143 :         preAppendedItem&& preAppendedItem->isGraceNotesGroup()) {
     286              :         // get x position of leftmost grace note
     287            0 :         const Chord* leftMostGraceChord = nullptr;
     288            0 :         const GraceNotesGroup* graceGroup = toGraceNotesGroup(preAppendedItem);
     289            0 :         for (const Chord* graceChord : *graceGroup) {
     290            0 :             leftMostGraceChord = leftMostGraceChord
     291            0 :                                  && leftMostGraceChord->x() < graceChord->x() ? leftMostGraceChord : graceChord;
     292              :         }
     293            0 :         if (leftMostGraceChord) {
     294            0 :             graceOffset = segment->pageX() - leftMostGraceChord->pageX();
     295              :         }
     296              :     }
     297              : 
     298          143 :     double offset = std::max(graceOffset, clefOffset);
     299              : 
     300          143 :     *system = segment->measure()->system();
     301          143 :     double x = segment->x() + segment->measure()->x() - line->spatium() - offset;
     302          143 :     return PointF(x, 0.0);
     303              : }
     304              : 
     305          230 : PointF Trill::linePos(Grip grip, System** system) const
     306              : {
     307          230 :     return trillLinePos(this, grip, system);
     308              : }
     309              : 
     310           53 : void Trill::setTrillType(TrillType tt)
     311              : {
     312           53 :     m_trillType = tt;
     313           53 :     if (!m_ornament) {
     314              :         // ornament parent will be explicitely set at layout stage
     315           53 :         m_ornament = Factory::createOrnament(score()->dummy());
     316              :     }
     317           53 :     m_ornament->setTrack(track());
     318           53 :     m_ornament->setSymId(Ornament::fromTrillType(tt));
     319           53 : }
     320              : 
     321              : //---------------------------------------------------------
     322              : //   createLineSegment
     323              : //---------------------------------------------------------
     324              : 
     325              : static const ElementStyle trillSegmentStyle {
     326              :     { Sid::trillMinDistance, Pid::MIN_DISTANCE },
     327              : };
     328              : 
     329           63 : LineSegment* Trill::createLineSegment()
     330              : {
     331           63 :     TrillSegment* seg = new TrillSegment(this);
     332           63 :     seg->setTrack(track());
     333           63 :     seg->setColor(lineColor());
     334           63 :     seg->initElementStyle(&trillSegmentStyle);
     335           63 :     return seg;
     336              : }
     337              : 
     338              : //---------------------------------------------------------
     339              : //   trillTypeName
     340              : //---------------------------------------------------------
     341              : 
     342            0 : String Trill::trillTypeUserName() const
     343              : {
     344            0 :     return TConv::translatedUserName(trillType());
     345              : }
     346              : 
     347              : //---------------------------------------------------------
     348              : //   getProperty
     349              : //---------------------------------------------------------
     350              : 
     351          259 : PropertyValue Trill::getProperty(Pid propertyId) const
     352              : {
     353          259 :     switch (propertyId) {
     354            0 :     case Pid::TRILL_TYPE:
     355            0 :         return int(trillType());
     356           18 :     case Pid::ORNAMENT_STYLE:
     357           18 :         return ornamentStyle();
     358          241 :     default:
     359          241 :         break;
     360              :     }
     361          241 :     return SLine::getProperty(propertyId);
     362              : }
     363              : 
     364              : //---------------------------------------------------------
     365              : //   setProperty
     366              : //---------------------------------------------------------
     367              : 
     368          110 : bool Trill::setProperty(Pid propertyId, const PropertyValue& val)
     369              : {
     370          110 :     switch (propertyId) {
     371            0 :     case Pid::TRILL_TYPE:
     372            0 :         setTrillType(TrillType(val.toInt()));
     373            0 :         break;
     374            0 :     case Pid::ORNAMENT_STYLE:
     375            0 :         setOrnamentStyle(val.value<OrnamentStyle>());
     376            0 :         break;
     377            0 :     case Pid::COLOR:
     378            0 :         setColor(val.value<Color>());
     379            0 :         setLineColor(val.value<Color>());
     380            0 :         break;
     381          110 :     default:
     382          110 :         return SLine::setProperty(propertyId, val);
     383              :     }
     384            0 :     triggerLayout();
     385            0 :     return true;
     386              : }
     387              : 
     388              : //---------------------------------------------------------
     389              : //   propertyDefault
     390              : //---------------------------------------------------------
     391              : 
     392          255 : PropertyValue Trill::propertyDefault(Pid propertyId) const
     393              : {
     394          255 :     switch (propertyId) {
     395            0 :     case Pid::TRILL_TYPE:
     396            0 :         return static_cast<int>(TrillType::TRILL_LINE);
     397           18 :     case Pid::ORNAMENT_STYLE:
     398           18 :         return OrnamentStyle::DEFAULT;
     399            2 :     case Pid::PLACEMENT:
     400            2 :         return style().styleV(Sid::trillPlacement);
     401              : 
     402          235 :     default:
     403          235 :         return SLine::propertyDefault(propertyId);
     404              :     }
     405              : }
     406              : 
     407              : //---------------------------------------------------------
     408              : //   subtypeUserName
     409              : //---------------------------------------------------------
     410              : 
     411            1 : muse::TranslatableString Trill::subtypeUserName() const
     412              : {
     413            1 :     return TConv::userName(trillType());
     414              : }
     415              : 
     416              : //---------------------------------------------------------
     417              : //   accessibleInfo
     418              : //---------------------------------------------------------
     419              : 
     420            0 : String Trill::accessibleInfo() const
     421              : {
     422            0 :     return String(u"%1: %2").arg(EngravingItem::accessibleInfo(), trillTypeUserName());
     423              : }
     424              : 
     425          118 : Sid Trill::defaultPosSid() const
     426              : {
     427          118 :     return placeAbove() ? Sid::trillPosAbove : Sid::trillPosBelow;
     428              : }
     429              : 
     430          211 : void Trill::doComputeEndElement()
     431              : {
     432          211 :     setEndElement(score()->findChordRestEndingBeforeTickInStaffAndVoice(tick2(), track2staff(track2()), voice()));
     433          211 : }
        

Generated by: LCOV version 2.0-1