LCOV - code coverage report
Current view: top level - src/engraving/dom - tuplet.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 69.5 % 456 317
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 "tuplet.h"
      24              : 
      25              : #include "../editing/elementeditdata.h"
      26              : #include "style/textstyle.h"
      27              : 
      28              : #include "beam.h"
      29              : #include "chord.h"
      30              : #include "engravingitem.h"
      31              : #include "factory.h"
      32              : #include "measure.h"
      33              : #include "note.h"
      34              : #include "rest.h"
      35              : #include "score.h"
      36              : #include "text.h"
      37              : 
      38              : #include "log.h"
      39              : 
      40              : using namespace mu;
      41              : using namespace mu::engraving;
      42              : 
      43              : namespace mu::engraving {
      44              : //---------------------------------------------------------
      45              : //   tupletStyle
      46              : //---------------------------------------------------------
      47              : 
      48              : static const ElementStyle tupletStyle {
      49              :     { Sid::tupletDirection,                    Pid::DIRECTION },
      50              :     { Sid::tupletNumberType,                   Pid::NUMBER_TYPE },
      51              :     { Sid::tupletBracketType,                  Pid::BRACKET_TYPE },
      52              :     { Sid::tupletBracketWidth,                 Pid::LINE_WIDTH },
      53              :     { Sid::tupletFontFace,                     Pid::FONT_FACE },
      54              :     { Sid::tupletFontSize,                     Pid::FONT_SIZE },
      55              :     { Sid::tupletFontStyle,                    Pid::FONT_STYLE },
      56              :     { Sid::tupletAlign,                        Pid::ALIGN },
      57              :     { Sid::tupletPosition,                     Pid::POSITION },
      58              :     { Sid::tupletMinDistance,                  Pid::MIN_DISTANCE },
      59              :     { Sid::tupletFontSpatiumDependent,         Pid::SIZE_SPATIUM_DEPENDENT },
      60              : };
      61              : 
      62              : //---------------------------------------------------------
      63              : //   Tuplet
      64              : //---------------------------------------------------------
      65              : 
      66         1070 : Tuplet::Tuplet(DummyParentOr<Measure> parent)
      67         1070 :     : DurationElement(ElementType::TUPLET, parent)
      68              : {
      69         1070 :     m_direction    = DirectionV::AUTO;
      70         1070 :     m_numberType   = TupletNumberType::SHOW_NUMBER;
      71         1070 :     m_bracketType  = TupletBracketType::AUTO_BRACKET;
      72         1070 :     m_ratio        = Fraction(1, 1);
      73         1070 :     m_number       = 0;
      74         1070 :     m_hasBracket   = false;
      75         1070 :     m_isUp         = true;
      76              :     // Temporary value to prevent initElementStyle laying out from the start of the score when m_tick is 0
      77         1070 :     m_tick = parent.as<Measure>() ? parent.as<Measure>()->tick() : Fraction(0, 1);
      78         1070 :     initElementStyle(&tupletStyle);
      79         1070 : }
      80              : 
      81           52 : Tuplet::Tuplet(const Tuplet& t)
      82           52 :     : DurationElement(t)
      83              : {
      84           52 :     m_tick         = t.m_tick;
      85           52 :     m_hasBracket   = t.m_hasBracket;
      86           52 :     m_ratio        = t.m_ratio;
      87           52 :     m_baseLen      = t.m_baseLen;
      88           52 :     m_direction    = t.m_direction;
      89           52 :     m_numberType   = t.m_numberType;
      90           52 :     m_bracketType  = t.m_bracketType;
      91           52 :     m_bracketWidth = t.m_bracketWidth;
      92              : 
      93           52 :     m_isUp          = t.m_isUp;
      94              : 
      95           52 :     m_p1             = t.m_p1;
      96           52 :     m_p2             = t.m_p2;
      97           52 :     m_userP1            = t.m_userP1;
      98           52 :     m_userP2            = t.m_userP2;
      99              : 
     100              :     // recreated on layout
     101           52 :     m_number = 0;
     102           52 : }
     103              : 
     104              : //---------------------------------------------------------
     105              : //   ~Tuplet
     106              : //---------------------------------------------------------
     107              : 
     108         1288 : Tuplet::~Tuplet()
     109              : {
     110          644 :     m_beingDestructed = true;
     111              : 
     112          746 :     for (DurationElement* de : m_allElements) {
     113          102 :         assert(de->tuplet() == this);
     114          102 :         de->setTuplet(nullptr);
     115              :     }
     116          644 :     delete m_number;
     117         1288 : }
     118              : 
     119           96 : void Tuplet::setOwnershipParent(DummyParentOr<Measure> parent)
     120              : {
     121           96 :     EngravingItem::setOwnershipParent(parent);
     122           96 : }
     123              : 
     124              : //---------------------------------------------------------
     125              : //   setSelected
     126              : //---------------------------------------------------------
     127              : 
     128          522 : void Tuplet::setSelected(bool f)
     129              : {
     130          522 :     EngravingItem::setSelected(f);
     131          522 :     if (m_number) {
     132          479 :         m_number->setSelected(f);
     133              :     }
     134          522 : }
     135              : 
     136              : //---------------------------------------------------------
     137              : //   setVisible
     138              : //---------------------------------------------------------
     139              : 
     140            0 : void Tuplet::setVisible(bool f)
     141              : {
     142            0 :     EngravingItem::setVisible(f);
     143            0 :     if (m_number) {
     144            0 :         m_number->setVisible(f);
     145              :     }
     146            0 : }
     147              : 
     148              : //---------------------------------------------------------
     149              : //   setColor
     150              : //---------------------------------------------------------
     151              : 
     152            3 : void Tuplet::setColor(const Color& col)
     153              : {
     154            3 :     EngravingItem::setColor(col);
     155            3 :     if (m_number) {
     156            0 :         m_number->setColor(col);
     157              :     }
     158            3 : }
     159              : 
     160        48568 : PropertyDelegate Tuplet::resolvePropertyDelegate(Pid id) const
     161              : {
     162        48568 :     switch (id) {
     163        20178 :     case Pid::FONT_FACE:
     164              :     case Pid::FONT_STYLE:
     165              :     case Pid::FONT_SIZE:
     166              :     case Pid::ALIGN:
     167              :     case Pid::POSITION:
     168              :     case Pid::SIZE_SPATIUM_DEPENDENT:
     169        20178 :         return PropertyDelegate(m_number);
     170        28390 :     default:
     171        28390 :         return PropertyDelegate();
     172              :     }
     173              : }
     174              : 
     175              : //---------------------------------------------------------
     176              : //   rtick
     177              : //---------------------------------------------------------
     178              : 
     179          528 : Fraction Tuplet::rtick() const
     180              : {
     181          528 :     return tick() - measure()->tick();
     182              : }
     183              : 
     184              : //---------------------------------------------------------
     185              : //   resetNumberProperty
     186              : //   reset number properties to default values
     187              : //   Set FONT_ITALIC to true, because for tuplets number should be italic
     188              : //---------------------------------------------------------
     189              : 
     190          401 : void Tuplet::resetNumberProperty()
     191              : {
     192          401 :     resetNumberProperty(m_number);
     193          401 : }
     194              : 
     195          808 : void Tuplet::resetNumberProperty(Text* number)
     196              : {
     197         5656 :     for (auto p : { Pid::FONT_FACE, Pid::FONT_STYLE, Pid::FONT_SIZE, Pid::ALIGN, Pid::POSITION, Pid::SIZE_SPATIUM_DEPENDENT }) {
     198         4848 :         number->resetProperty(p);
     199              :     }
     200          808 : }
     201              : 
     202              : //---------------------------------------------------------
     203              : //   calcHasBracket
     204              : //---------------------------------------------------------
     205              : 
     206         1909 : bool Tuplet::calcHasBracket(const DurationElement* cr1, const DurationElement* cr2) const
     207              : {
     208         1909 :     if (m_bracketType != TupletBracketType::AUTO_BRACKET) {
     209          204 :         return m_bracketType != TupletBracketType::SHOW_NO_BRACKET;
     210              :     }
     211         1705 :     if (cr1 == cr2) { // Degenerate tuplet
     212           27 :         return false;
     213              :     }
     214         1678 :     if (!cr1->isChord() || !cr2->isChord()) {
     215          522 :         return true;
     216              :     }
     217         1156 :     const Chord* c1 = toChord(cr1);
     218         1156 :     const Chord* c2 = toChord(cr2);
     219              : 
     220         1156 :     Beam const* beamStart = c1->beam();
     221         1156 :     Beam const* beamEnd = c2->beam();
     222         1156 :     if (!beamStart || !beamEnd || beamStart != beamEnd) {
     223          236 :         return true;
     224              :     }
     225          920 :     bool tupletStartsBeam = beamStart->elements().front() == c1;
     226          920 :     bool tupletEndsBeam = beamEnd->elements().back() == c2;
     227          920 :     bool headSide = isUp() != (c1->up() || c2->up());
     228          920 :     bool isCross = c1->vStaffIdx() != c2->vStaffIdx();
     229          920 :     if (tupletStartsBeam && tupletEndsBeam && (!headSide || isCross)) {
     230          674 :         return false;
     231              :     }
     232              : 
     233          246 :     int beamCount = -1;
     234          978 :     for (DurationElement* e : m_currentElements) {
     235          761 :         if (e->isTuplet() || e->isRest()) {
     236           29 :             return true;
     237          735 :         } else if (e->isChordRest()) {
     238          735 :             ChordRest* cr = toChordRest(e);
     239          735 :             if (cr->beam() == 0) {
     240            0 :                 return true;
     241              :             }
     242          735 :             if (beamCount == -1) {
     243          236 :                 beamCount = cr->beams();
     244          499 :             } else if (beamCount != cr->beams()) {
     245            3 :                 return true;
     246              :             }
     247              :         }
     248              :     }
     249          217 :     if (beamCount < 1) {
     250            0 :         return true;
     251              :     }
     252              : 
     253          217 :     bool startChordBreaks32 = false;
     254          217 :     bool startChordBreaks64 = false;
     255          217 :     Chord* prevStartChord = c1->prev();
     256          217 :     if (prevStartChord) {
     257          209 :         beamStart->calcBeamBreaks(c1, prevStartChord, beamCount - 1, startChordBreaks32, startChordBreaks64);
     258              :     }
     259          217 :     bool startChordDefinesTuplet = startChordBreaks32 || startChordBreaks64 || tupletStartsBeam;
     260          217 :     if (prevStartChord) {
     261          209 :         startChordDefinesTuplet = startChordDefinesTuplet || prevStartChord->beams() < beamCount;
     262              :     }
     263              : 
     264          217 :     bool endChordBreaks32 = false;
     265          217 :     bool endChordBreaks64 = false;
     266          217 :     Chord* nextEndChord = c2->next();
     267          217 :     if (nextEndChord) {
     268          150 :         beamEnd->calcBeamBreaks(nextEndChord, c2, beamCount - 1, endChordBreaks32, endChordBreaks64);
     269              :     }
     270          217 :     bool endChordDefinesTuplet = endChordBreaks32 || endChordBreaks64 || tupletEndsBeam;
     271          217 :     if (nextEndChord) {
     272          150 :         endChordDefinesTuplet = endChordDefinesTuplet || nextEndChord->beams() < beamCount;
     273              :     }
     274              : 
     275          217 :     if (startChordDefinesTuplet && endChordDefinesTuplet && (!headSide || isCross)) {
     276          193 :         return false;
     277              :     }
     278              : 
     279           24 :     return true;
     280              : }
     281              : 
     282              : //---------------------------------------------------------
     283              : //   scanElements
     284              : //---------------------------------------------------------
     285              : 
     286         1280 : void Tuplet::scanElements(std::function<void(EngravingItem*)> func)
     287              : {
     288         1280 :     if (m_number) {
     289          746 :         m_number->scanElements(func);
     290              :     }
     291              : 
     292         1280 :     func(this);
     293         1280 : }
     294              : 
     295              : //---------------------------------------------------------
     296              : //   add
     297              : //---------------------------------------------------------
     298              : 
     299         4260 : void Tuplet::add(EngravingItem* e)
     300              : {
     301         4260 :     switch (e->type()) {
     302         4260 :     case ElementType::CHORD:
     303              :     case ElementType::REST:
     304              :     case ElementType::TUPLET: {
     305         4260 :         DurationElement* de = toDurationElement(e);
     306         4260 :         IF_ASSERT_FAILED(!contains(de)) {
     307            0 :             return;
     308              :         }
     309         4260 :         de->setTuplet(this);
     310              : 
     311         4260 :         bool found = false;
     312         4260 :         Fraction tick = de->rtick();
     313         4260 :         if (tick != Fraction(-1, 1)) {
     314        11531 :             for (unsigned int i = 0; i < m_currentElements.size(); ++i) {
     315         7439 :                 if (m_currentElements[i]->rtick() > tick) {
     316          168 :                     m_currentElements.insert(m_currentElements.begin() + i, de);
     317          168 :                     found = true;
     318          168 :                     break;
     319              :                 }
     320              :             }
     321              :         }
     322         4260 :         if (!found) {
     323         4092 :             m_currentElements.push_back(de);
     324              :         }
     325              :     }
     326         4260 :     break;
     327              : 
     328            0 :     default:
     329            0 :         LOGD("Tuplet::add() unknown element");
     330            0 :         return;
     331              :     }
     332              : 
     333         4260 :     e->added();
     334              : }
     335              : 
     336              : //---------------------------------------------------------
     337              : //   remove
     338              : //---------------------------------------------------------
     339              : 
     340          530 : void Tuplet::remove(EngravingItem* e)
     341              : {
     342          530 :     switch (e->type()) {
     343              : //            case ElementType::TEXT:
     344              : //                  if (e == _number)
     345              : //                        _number = 0;
     346              : //                  break;
     347          530 :     case ElementType::CHORD:
     348              :     case ElementType::REST:
     349              :     case ElementType::TUPLET: {
     350          530 :         auto i = std::find(m_currentElements.begin(), m_currentElements.end(), toDurationElement(e));
     351          530 :         if (i == m_currentElements.end()) {
     352            0 :             LOGD("Tuplet::remove: cannot find element <%s>", e->typeName());
     353            0 :             LOGD("  elements %zu", m_currentElements.size());
     354              :         } else {
     355          530 :             m_currentElements.erase(i);
     356          530 :             e->removed();
     357              :         }
     358              :     }
     359          530 :     break;
     360            0 :     default:
     361            0 :         LOGD("Tuplet::remove: unknown element");
     362            0 :         break;
     363              :     }
     364          530 : }
     365              : 
     366         5876 : void Tuplet::addDurationElement(DurationElement* de)
     367              : {
     368         5876 :     assert(!m_beingDestructed);
     369         5876 :     m_allElements.insert(de);
     370         5876 : }
     371              : 
     372         4334 : void Tuplet::removeDurationElement(DurationElement* de)
     373              : {
     374         4334 :     if (m_beingDestructed) {
     375          102 :         return;
     376              :     }
     377              : 
     378         4232 :     m_allElements.erase(de);
     379              : }
     380              : 
     381              : //---------------------------------------------------------
     382              : //   isEditable
     383              : //---------------------------------------------------------
     384              : 
     385            0 : bool Tuplet::isEditable() const
     386              : {
     387            0 :     return m_hasBracket;
     388              : }
     389              : 
     390              : //---------------------------------------------------------
     391              : //   startDragGrip
     392              : //---------------------------------------------------------
     393              : 
     394            0 : void Tuplet::startDragGrip(EditData& ed)
     395              : {
     396            0 :     DurationElement::startDragGrip(ed);
     397            0 :     ElementEditDataPtr eed = ed.getData(this);
     398              : 
     399            0 :     eed->pushProperty(Pid::P1);
     400            0 :     eed->pushProperty(Pid::P2);
     401            0 : }
     402              : 
     403              : //---------------------------------------------------------
     404              : //   dragGrip
     405              : //---------------------------------------------------------
     406              : 
     407            0 : void Tuplet::dragGrip(EditData& ed)
     408              : {
     409            0 :     if (ed.curGrip == Grip::START) {
     410            0 :         m_userP1 += ed.delta;
     411            0 :     } else if (ed.curGrip == Grip::END) {
     412            0 :         m_userP2 += ed.delta;
     413            0 :     } else if (ed.curGrip == Grip::MIDDLE) {
     414            0 :         setOffset(offset() + ed.delta);
     415              :     } else {
     416            0 :         UNREACHABLE;
     417              :         return;
     418              :     }
     419              : 
     420            0 :     setGenerated(false);
     421            0 :     triggerLayout();
     422              : }
     423              : 
     424              : //---------------------------------------------------------
     425              : //   gripsPositions
     426              : //---------------------------------------------------------
     427              : 
     428            0 : int Tuplet::gripsCount() const
     429              : {
     430            0 :     return m_hasBracket ? 3 : 0;
     431              : }
     432              : 
     433            0 : std::vector<PointF> Tuplet::gripsPositions(const EditData&) const
     434              : {
     435            0 :     IF_ASSERT_FAILED(gripsCount() != 0) {
     436            0 :         return std::vector<PointF>();
     437              :     }
     438              : 
     439            0 :     const PointF pp(pagePos());
     440            0 :     PointF left = pp + m_p1;
     441            0 :     PointF right = pp + m_p2;
     442            0 :     PointF middle = 0.5 * (left + right);
     443              : 
     444            0 :     return { left, right, middle };
     445              : }
     446              : 
     447              : //---------------------------------------------------------
     448              : //   reset
     449              : //---------------------------------------------------------
     450              : 
     451            0 : void Tuplet::reset()
     452              : {
     453            0 :     undoChangeProperty(Pid::P1, PointF());
     454            0 :     undoChangeProperty(Pid::P2, PointF());
     455            0 :     EngravingItem::reset();
     456            0 : }
     457              : 
     458              : //---------------------------------------------------------
     459              : //   dump
     460              : //---------------------------------------------------------
     461              : 
     462            0 : void Tuplet::dump() const
     463              : {
     464            0 :     EngravingItem::dump();
     465            0 :     LOGD() << "ratio: " << m_ratio.toString();
     466            0 : }
     467              : 
     468              : //---------------------------------------------------------
     469              : //   setTrack
     470              : //---------------------------------------------------------
     471              : 
     472         2457 : void Tuplet::setTrack(track_idx_t val)
     473              : {
     474         2457 :     if (tuplet()) {
     475          303 :         tuplet()->setTrack(val);
     476              :     }
     477         2457 :     if (m_number) {
     478          740 :         m_number->setTrack(val);
     479              :     }
     480         2457 :     EngravingItem::setTrack(val);
     481         2457 : }
     482              : 
     483              : //---------------------------------------------------------
     484              : //   tickGreater
     485              : //---------------------------------------------------------
     486              : 
     487          611 : static bool tickGreater(const DurationElement* a, const DurationElement* b)
     488              : {
     489          611 :     return a->tick() < b->tick();
     490              : }
     491              : 
     492              : //---------------------------------------------------------
     493              : //   sortElements
     494              : //---------------------------------------------------------
     495              : 
     496           83 : void Tuplet::sortElements()
     497              : {
     498           83 :     std::sort(m_currentElements.begin(), m_currentElements.end(), tickGreater);
     499           83 : }
     500              : 
     501              : //---------------------------------------------------------
     502              : //   cross
     503              : //---------------------------------------------------------
     504              : 
     505         5491 : bool Tuplet::cross() const
     506              : {
     507        24500 :     for (DurationElement* de : m_currentElements) {
     508        19132 :         if (!de) {
     509            0 :             continue;
     510        19132 :         } else if (de->isChordRest()) {
     511        17439 :             if (toChordRest(de)->staffMove()) {
     512          123 :                 return true;
     513              :             }
     514         1693 :         } else if (de->isTuplet()) {
     515         1693 :             if (toTuplet(de)->cross()) {
     516            0 :                 return true;
     517              :             }
     518              :         }
     519              :     }
     520         5368 :     return false;
     521              : }
     522              : 
     523              : //---------------------------------------------------------
     524              : //   vStaffIdx
     525              : ///  Staff index for layout based on the first ChordRest
     526              : //---------------------------------------------------------
     527              : 
     528         4038 : staff_idx_t Tuplet::vStaffIdx() const
     529              : {
     530         4038 :     if (elements().empty()) {
     531          148 :         return muse::nidx;
     532              :     }
     533              : 
     534         3890 :     const DurationElement* cr = elements().front();
     535         3890 :     if (!cr) {
     536            0 :         return muse::nidx;
     537              :     }
     538              : 
     539         4122 :     while (cr->isTuplet()) {
     540          232 :         const Tuplet* t = toTuplet(cr);
     541          232 :         if (t->elements().empty()) {
     542            0 :             return muse::nidx;
     543              :         }
     544          232 :         cr = t->elements().front();
     545              :     }
     546              : 
     547         3890 :     return cr->vStaffIdx();
     548              : }
     549              : 
     550              : //---------------------------------------------------------
     551              : //   elementsDuration
     552              : ///  Get the sum of the element fraction in the tuplet,
     553              : ///  even if the tuplet is not complete yet
     554              : //---------------------------------------------------------
     555              : 
     556           57 : Fraction Tuplet::elementsDuration()
     557              : {
     558           57 :     Fraction f;
     559          174 :     for (DurationElement* el : m_currentElements) {
     560          117 :         f += el->ticks();
     561              :     }
     562           57 :     return f;
     563              : }
     564              : 
     565              : //---------------------------------------------------------
     566              : //   getProperty
     567              : //---------------------------------------------------------
     568              : 
     569         8157 : PropertyValue Tuplet::getProperty(Pid propertyId) const
     570              : {
     571         8157 :     if (const PropertyDelegate d = resolvePropertyDelegate(propertyId); d.hasDelegate) {
     572           60 :         return d.delegate ? d.delegate->getProperty(propertyId) : propertyDefault(propertyId);
     573              :     }
     574         8097 :     switch (propertyId) {
     575          159 :     case Pid::DIRECTION:
     576          159 :         return PropertyValue::fromValue<DirectionV>(m_direction);
     577           28 :     case Pid::NUMBER_TYPE:
     578           28 :         return int(m_numberType);
     579           26 :     case Pid::BRACKET_TYPE:
     580           26 :         return int(m_bracketType);
     581            1 :     case Pid::LINE_WIDTH:
     582            1 :         return m_bracketWidth;
     583          601 :     case Pid::NORMAL_NOTES:
     584          601 :         return m_ratio.denominator();
     585          601 :     case Pid::ACTUAL_NOTES:
     586          601 :         return m_ratio.numerator();
     587          601 :     case Pid::P1:
     588          601 :         return m_userP1;
     589          601 :     case Pid::P2:
     590          601 :         return m_userP2;
     591         5479 :     default:
     592         5479 :         break;
     593              :     }
     594         5479 :     return DurationElement::getProperty(propertyId);
     595              : }
     596              : 
     597              : //---------------------------------------------------------
     598              : //   setProperty
     599              : //---------------------------------------------------------
     600              : 
     601        12376 : bool Tuplet::setProperty(Pid propertyId, const PropertyValue& v)
     602              : {
     603        12376 :     if (const PropertyDelegate d = resolvePropertyDelegate(propertyId); d.hasDelegate) {
     604         6456 :         return d.delegate ? d.delegate->setProperty(propertyId, v) : false;
     605              :     }
     606         5920 :     switch (propertyId) {
     607         1216 :     case Pid::DIRECTION:
     608         1216 :         setDirection(v.value<DirectionV>());
     609         1216 :         break;
     610         1358 :     case Pid::NUMBER_TYPE:
     611         1358 :         setNumberType(TupletNumberType(v.toInt()));
     612         1358 :         break;
     613         1101 :     case Pid::BRACKET_TYPE:
     614         1101 :         setBracketType(TupletBracketType(v.toInt()));
     615         1101 :         break;
     616         1083 :     case Pid::LINE_WIDTH:
     617         1083 :         setBracketWidth(v.value<Spatium>());
     618         1083 :         break;
     619           27 :     case Pid::NORMAL_NOTES:
     620           27 :         m_ratio.setDenominator(v.toInt());
     621           27 :         break;
     622           27 :     case Pid::ACTUAL_NOTES:
     623           27 :         m_ratio.setNumerator(v.toInt());
     624           27 :         break;
     625            0 :     case Pid::P1:
     626            0 :         m_userP1 = v.value<PointF>();
     627            0 :         break;
     628            0 :     case Pid::P2:
     629            0 :         m_userP2 = v.value<PointF>();
     630            0 :         break;
     631           23 :     case Pid::TICK:
     632           23 :         setTick(v.value<Fraction>());
     633           23 :         break;
     634         1085 :     default:
     635         1085 :         return DurationElement::setProperty(propertyId, v);
     636              :     }
     637         4835 :     if (!m_currentElements.empty()) {
     638           57 :         m_currentElements.front()->triggerLayout();
     639           57 :         m_currentElements.back()->triggerLayout();
     640              :     }
     641         4835 :     return true;
     642              : }
     643              : 
     644              : //---------------------------------------------------------
     645              : //   propertyDefault
     646              : //---------------------------------------------------------
     647              : 
     648        13836 : PropertyValue Tuplet::propertyDefault(Pid id) const
     649              : {
     650        13836 :     switch (id) {
     651            0 :     case Pid::TEXT_STYLE:
     652            0 :         return TextStyleType::TUPLET;
     653            0 :     case Pid::SYSTEM_FLAG:
     654            0 :         return false;
     655          591 :     case Pid::TEXT:
     656         1182 :         return String();
     657         1202 :     case Pid::NORMAL_NOTES:
     658              :     case Pid::ACTUAL_NOTES:
     659         1202 :         return 0;
     660         1202 :     case Pid::P1:
     661              :     case Pid::P2:
     662         2404 :         return PointF();
     663          828 :     case Pid::POSITION:
     664          828 :         return style().styleV(Sid::tupletPosition);
     665          844 :     case Pid::ALIGN:
     666          844 :         return style().styleV(Sid::tupletAlign);
     667          916 :     case Pid::FONT_FACE:
     668          916 :         return style().styleV(Sid::tupletFontFace);
     669          916 :     case Pid::FONT_SIZE:
     670          916 :         return style().styleV(Sid::tupletFontSize);
     671          916 :     case Pid::FONT_STYLE:
     672          916 :         return style().styleV(Sid::tupletFontStyle);
     673          828 :     case Pid::SIZE_SPATIUM_DEPENDENT:
     674          828 :         return style().styleV(Sid::tupletFontSpatiumDependent);
     675         5593 :     default:
     676         5593 :         break;
     677              :     }
     678              : 
     679              :     // TODO: what does this do? Why TextStyleType::DEFAULT?
     680        88470 :     for (const auto& spp : *textStyle(TextStyleType::DEFAULT)) {
     681        83478 :         if (spp.pid == id) {
     682          601 :             return styleValue(id, spp.sid);
     683              :         }
     684              :     }
     685              : 
     686         4992 :     return DurationElement::propertyDefault(id);
     687              : }
     688              : 
     689              : //---------------------------------------------------------
     690              : //   sanitizeTuplet
     691              : ///    Check validity of tuplets and coherence between duration
     692              : ///    and baselength. Needed for importing old files due to a bug
     693              : ///    in the released version for corner-case tuplets.
     694              : ///    See issue #136406 and Pull request #2881
     695              : //---------------------------------------------------------
     696              : 
     697           28 : void Tuplet::sanitizeTuplet()
     698              : {
     699           28 :     if (ratio().numerator() == ratio().reduced().numerator()) { // return if the ratio is an irreducible fraction
     700           16 :         return;
     701              :     }
     702           12 :     Fraction baseLenDuration = (Fraction(ratio().denominator(), 1) * baseLen().fraction()).reduced();
     703              : 
     704              :     // Due to a bug present in 2.1 (and before), a tuplet with non-reduced ratio could be
     705              :     // in a corrupted state (mismatch between duration and base length).
     706              :     // A tentative will now be made to retrieve the correct duration by summing up all the
     707              :     // durations of the elements constituting the tuplet. This does not work for
     708              :     // not-completely filled tuplets, such as tuplets in voices > 0 with
     709              :     // gaps (for example, a tuplet in second voice with a deleted chordrest element)
     710              : 
     711           12 :     Fraction testDuration(0, 1);
     712           89 :     for (DurationElement* de : elements()) {
     713           77 :         if (!de) {
     714            0 :             continue;
     715              :         }
     716           77 :         Fraction elementDuration(0, 1);
     717           77 :         if (de->isTuplet()) {
     718            1 :             Tuplet* t = toTuplet(de);
     719            1 :             t->sanitizeTuplet();
     720            1 :             elementDuration = t->ticks();
     721              :         } else {
     722           76 :             elementDuration = de->ticks();
     723              :         }
     724           77 :         testDuration += elementDuration;
     725              :     }
     726           12 :     testDuration = testDuration / ratio();
     727           12 :     testDuration.reduce();
     728           12 :     if (elements().back()->endTick() - elements().front()->tick() > testDuration) {
     729            0 :         return;         // this tuplet has missing elements; do not sanitize
     730              :     }
     731           12 :     if (!(testDuration == baseLenDuration && baseLenDuration == ticks())) {
     732           10 :         Fraction f = testDuration * Fraction(1, ratio().denominator());
     733           10 :         f.reduce();
     734           10 :         Fraction fbl(1, f.denominator());
     735           10 :         if (TDuration::isValid(fbl)) {
     736           10 :             setTicks(testDuration);
     737           10 :             setBaseLen(fbl);
     738           10 :             LOGD("Tuplet %p sanitized duration %d/%d   baseLen %d/%d", this,
     739              :                  testDuration.numerator(), testDuration.denominator(),
     740              :                  1, fbl.denominator());
     741              :         } else {
     742            0 :             LOGD("Impossible to sanitize the tuplet");
     743              :         }
     744              :     }
     745              : }
     746              : 
     747              : //---------------------------------------------------------
     748              : //   addMissingElement
     749              : //     Add a rest with the given start and end ticks.
     750              : //     Should only be called from Tuplet::addMissingElements().
     751              : //     Needed for importing files that saved incomplete tuplets.
     752              : //---------------------------------------------------------
     753              : 
     754            0 : Fraction Tuplet::addMissingElement(const Fraction& startTick, const Fraction& endTick)
     755              : {
     756            0 :     Fraction f = (endTick - startTick) * ratio();
     757            0 :     TDuration d = TDuration(f, true);
     758            0 :     if (!d.isValid()) {
     759            0 :         LOGD("Tuplet::addMissingElement(): invalid duration: %d/%d", f.numerator(), f.denominator());
     760            0 :         return Fraction::fromTicks(0);
     761              :     }
     762            0 :     f = d.fraction();
     763            0 :     Segment* segment = measure()->getSegment(SegmentType::ChordRest, startTick);
     764            0 :     Rest* rest = Factory::createRest(segment);
     765            0 :     rest->setDurationType(d);
     766            0 :     rest->setTicks(f);
     767            0 :     rest->setTrack(track());
     768            0 :     rest->setVisible(false);
     769              : 
     770            0 :     segment->add(rest);
     771            0 :     add(rest);
     772            0 :     return f;
     773              : }
     774              : 
     775              : //---------------------------------------------------------
     776              : //   addMissingElements
     777              : //     Make this tuplet complete by filling in holes where
     778              : //     there ought to be rests. Needed for importing files
     779              : //     that saved incomplete tuplets.
     780              : //---------------------------------------------------------
     781              : 
     782           27 : void Tuplet::addMissingElements()
     783              : {
     784           27 :     if (tuplet()) {
     785           27 :         return;         // do not correct nested tuplets
     786              :     }
     787           20 :     if (voice() == 0) {
     788           20 :         return;         // nothing to do for tuplets in voice 1
     789              :     }
     790              : 
     791            0 :     Fraction missingElementsDuration = ticks() * ratio() - elementsDuration();
     792            0 :     if (missingElementsDuration.isZero()) {
     793            0 :         return;
     794              :     }
     795              : 
     796              :     // first, fill in any holes in the middle of the tuplet
     797            0 :     Fraction expectedTick = elements().front()->tick();
     798              : 
     799            0 :     const std::vector<DurationElement*> elementsCopy = elements(); // mofified during loop
     800            0 :     for (const DurationElement* de : elementsCopy) {
     801            0 :         if (!de) {
     802            0 :             continue;
     803              :         }
     804            0 :         if (de->tick() != expectedTick) {
     805            0 :             missingElementsDuration -= addMissingElement(expectedTick, de->tick());
     806            0 :             if (missingElementsDuration.isZero()) {
     807            0 :                 return;
     808              :             }
     809              :         }
     810            0 :         expectedTick += de->actualTicks();
     811              :     }
     812              : 
     813              :     // calculate the tick where we would expect a tuplet of this duration to start
     814              :     // TODO: check:
     815            0 :     expectedTick = elements().front()->tick() - Fraction::fromTicks(elements().front()->tick().ticks() % ticks().ticks());
     816            0 :     if (expectedTick != elements().front()->tick()) {
     817              :         // try to fill a hole at the beginning of the tuplet
     818            0 :         Fraction firstAvailableTick = measure()->tick();
     819            0 :         Segment* segment = measure()->findSegment(SegmentType::ChordRest, elements().front()->tick());
     820            0 :         ChordRest* prevChordRest = segment && segment->prev() ? segment->prev()->nextChordRest(track(), true) : nullptr;
     821            0 :         if (prevChordRest && prevChordRest->measure() == measure()) {
     822            0 :             firstAvailableTick = prevChordRest->endTick();
     823              :         }
     824            0 :         if (firstAvailableTick != elements().front()->tick()) {
     825            0 :             Fraction f = missingElementsDuration / ratio();
     826            0 :             Fraction ticksRequired = f;
     827            0 :             Fraction endTick = elements().front()->tick();
     828            0 :             Fraction startTick = std::max(firstAvailableTick, endTick - ticksRequired);
     829            0 :             if (expectedTick > startTick) {
     830            0 :                 startTick = expectedTick;
     831              :             }
     832            0 :             missingElementsDuration -= addMissingElement(startTick, endTick);
     833            0 :             if (missingElementsDuration.isZero()) {
     834            0 :                 return;
     835              :             }
     836              :         }
     837              :     }
     838              :     // now fill a hole at the end of the tuplet
     839            0 :     Fraction startTick = elements().back()->endTick();
     840            0 :     Fraction endTick = elements().front()->tick() + ticks();
     841              :     // just to be safe, find the next ChordRest in the track, and adjust endTick if necessary
     842            0 :     Segment* segment = measure()->findSegment(SegmentType::ChordRest, elements().back()->tick());
     843            0 :     ChordRest* nextChordRest = segment && segment->next() ? segment->next()->nextChordRest(track(), false) : nullptr;
     844            0 :     if (nextChordRest && nextChordRest->tick() < endTick) {
     845            0 :         endTick = nextChordRest->tick();
     846              :     }
     847            0 :     missingElementsDuration -= addMissingElement(startTick, endTick);
     848            0 :     if (!missingElementsDuration.isZero()) {
     849            0 :         LOGD("Tuplet::addMissingElements(): still missing duration of %d/%d",
     850              :              missingElementsDuration.numerator(), missingElementsDuration.denominator());
     851              :     }
     852            0 : }
     853              : 
     854           11 : int Tuplet::computeTupletDenominator(int numerator, Fraction totalDuration)
     855              : {
     856           11 :     if (numerator <= 0) {
     857            0 :         return 0;
     858              :     }
     859           11 :     Fraction noteDuration = totalDuration / numerator;
     860           11 :     Fraction baseLen(1, 1);
     861           11 :     Fraction ratio = (totalDuration / baseLen).reduced();
     862           46 :     while (ratio.denominator() != 1 || baseLen / 2 >= noteDuration) {
     863           35 :         baseLen /= 2;
     864           35 :         ratio = (totalDuration / baseLen).reduced();
     865              :     }
     866           11 :     return ratio.numerator();
     867              : }
     868              : 
     869            9 : int Tuplet::conventionalTupletDenominator(int numerator)
     870              : {
     871            9 :     if (numerator <= 0) {
     872            1 :         return 0;
     873              :     }
     874            8 :     if (numerator == 2) {
     875            1 :         return 3;
     876              :     }
     877            7 :     int normal = 1;
     878           20 :     while (normal * 2 < numerator) {
     879           13 :         normal *= 2;
     880              :     }
     881            7 :     return normal * 2 == numerator ? numerator * 3 / 4 : normal;
     882              : }
     883              : 
     884            0 : EngravingItem* Tuplet::nextElement()
     885              : {
     886            0 :     ChordRest* firstElement = toChordRest(elements().front());
     887            0 :     if (firstElement->isChord()) {
     888            0 :         Chord* chord = toChord(firstElement);
     889            0 :         return chord->firstGraceOrNote();
     890              :     }
     891            0 :     return firstElement;
     892              : }
     893              : 
     894            0 : EngravingItem* Tuplet::prevElement()
     895              : {
     896            0 :     ChordRest* firstElement = toChordRest(elements().front());
     897            0 :     staff_idx_t staffId = firstElement->staffIdx();
     898            0 :     EngravingItem* prevItem = firstElement->segment()->prevElement(staffId);
     899            0 :     return prevItem;
     900              : }
     901              : } // namespace mu::engraving
        

Generated by: LCOV version 2.0-1