LCOV - code coverage report
Current view: top level - src/engraving/dom - pedal.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 93.8 % 129 121
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 "pedal.h"
      24              : 
      25              : #include "chord.h"
      26              : #include "chordrest.h"
      27              : #include "dummyparent.h"
      28              : #include "measure.h"
      29              : #include "note.h"
      30              : #include "score.h"
      31              : #include "system.h"
      32              : #include "text.h"
      33              : #include "rest.h"
      34              : 
      35              : #include "log.h"
      36              : 
      37              : using namespace mu;
      38              : 
      39              : namespace mu::engraving {
      40              : static const ElementStyle pedalStyle {
      41              :     { Sid::pedalText,                          Pid::BEGIN_TEXT },
      42              :     { Sid::pedalContinueText,                  Pid::CONTINUE_TEXT },
      43              :     { Sid::pedalRosetteEndText,                Pid::END_TEXT },
      44              :     { Sid::pedalFontFace,                      Pid::BEGIN_FONT_FACE },
      45              :     { Sid::pedalFontFace,                      Pid::CONTINUE_FONT_FACE },
      46              :     { Sid::pedalFontFace,                      Pid::END_FONT_FACE },
      47              :     { Sid::pedalFontSize,                      Pid::BEGIN_FONT_SIZE },
      48              :     { Sid::pedalFontSize,                      Pid::CONTINUE_FONT_SIZE },
      49              :     { Sid::pedalFontSize,                      Pid::END_FONT_SIZE },
      50              :     { Sid::pedalFontStyle,                     Pid::BEGIN_FONT_STYLE },
      51              :     { Sid::pedalFontStyle,                     Pid::CONTINUE_FONT_STYLE },
      52              :     { Sid::pedalFontStyle,                     Pid::END_FONT_STYLE },
      53              :     { Sid::pedalMusicalSymbolsScale,           Pid::BEGIN_TEXT_MUSICAL_SYMBOLS_SCALE },
      54              :     { Sid::pedalMusicalSymbolsScale,           Pid::CONTINUE_TEXT_MUSICAL_SYMBOLS_SCALE },
      55              :     { Sid::pedalMusicalSymbolsScale,           Pid::END_TEXT_MUSICAL_SYMBOLS_SCALE },
      56              :     { Sid::dummyMusicalSymbolSize,             Pid::BEGIN_TEXT_MUSIC_SYMBOLS_SIZE },
      57              :     { Sid::dummyMusicalSymbolSize,             Pid::CONTINUE_TEXT_MUSIC_SYMBOLS_SIZE },
      58              :     { Sid::dummyMusicalSymbolSize,             Pid::END_TEXT_MUSIC_SYMBOLS_SIZE },
      59              :     { Sid::pedalTextAlign,                     Pid::BEGIN_TEXT_ALIGN },
      60              :     { Sid::pedalTextAlign,                     Pid::CONTINUE_TEXT_ALIGN },
      61              :     { Sid::pedalTextAlign,                     Pid::END_TEXT_ALIGN },
      62              :     { Sid::pedalPosition,                      Pid::BEGIN_TEXT_POSITION },
      63              :     { Sid::pedalPosition,                      Pid::CONTINUE_TEXT_POSITION },
      64              :     { Sid::pedalPosition,                      Pid::END_TEXT_POSITION },
      65              :     { Sid::pedalHookHeight,                    Pid::BEGIN_HOOK_HEIGHT },
      66              :     { Sid::pedalHookHeight,                    Pid::END_HOOK_HEIGHT },
      67              :     { Sid::pedalLineWidth,                     Pid::LINE_WIDTH },
      68              :     { Sid::pedalDashLineLen,                   Pid::DASH_LINE_LEN },
      69              :     { Sid::pedalDashGapLen,                    Pid::DASH_GAP_LEN },
      70              :     { Sid::pedalPlacement,                     Pid::PLACEMENT },
      71              :     { Sid::pedalLineStyle,                     Pid::LINE_STYLE },
      72              :     { Sid::pedalFontSpatiumDependent,          Pid::TEXT_SIZE_SPATIUM_DEPENDENT },
      73              :     { Sid::pedalEndLineArrowHeight,            Pid::END_LINE_ARROW_HEIGHT },
      74              :     { Sid::pedalEndLineArrowWidth,             Pid::END_LINE_ARROW_WIDTH },
      75              :     { Sid::pedalBeginLineArrowHeight,          Pid::BEGIN_LINE_ARROW_HEIGHT },
      76              :     { Sid::pedalBeginLineArrowWidth,           Pid::BEGIN_LINE_ARROW_WIDTH },
      77              :     { Sid::pedalEndFilledArrowHeight,          Pid::END_FILLED_ARROW_HEIGHT },
      78              :     { Sid::pedalEndFilledArrowWidth,           Pid::END_FILLED_ARROW_WIDTH },
      79              :     { Sid::pedalBeginFilledArrowHeight,        Pid::BEGIN_FILLED_ARROW_HEIGHT },
      80              :     { Sid::pedalBeginFilledArrowWidth,         Pid::BEGIN_FILLED_ARROW_WIDTH },
      81              : };
      82              : 
      83              : const String Pedal::PEDAL_SYMBOL = u"<sym>keyboardPedalPed</sym>";
      84              : const String Pedal::STAR_SYMBOL = u"<sym>keyboardPedalUp</sym>";
      85              : 
      86           74 : PedalSegment::PedalSegment(Pedal* sp)
      87           74 :     : TextLineBaseSegment(ElementType::PEDAL_SEGMENT, sp, ElementFlag::MOVABLE | ElementFlag::ON_STAFF)
      88              : {
      89           74 :     m_text->setTextStyleType(propertyDefault(Pid::TEXT_STYLE).value<TextStyleType>());
      90           74 :     m_endText->setTextStyleType(propertyDefault(Pid::TEXT_STYLE).value<TextStyleType>());
      91           74 : }
      92              : 
      93         7248 : Sid Pedal::getPropertyStyle(Pid pid) const
      94              : {
      95         7248 :     switch (pid) {
      96          483 :     case Pid::END_TEXT:
      97          483 :         return lineVisible() ? Sid::pedalEndText : Sid::pedalRosetteEndText;
      98          352 :     case Pid::BEGIN_TEXT:
      99          352 :         return beginHookType() == HookType::NONE ? Sid::pedalText : Sid::pedalHookText;
     100          327 :     case Pid::CONTINUE_TEXT:
     101          327 :         return beginHookType() == HookType::NONE ? Sid::pedalContinueText : Sid:: pedalContinueHookText;
     102         6086 :     default:
     103         6086 :         return TextLineBase::getPropertyStyle(pid);
     104              :     }
     105              : }
     106              : 
     107              : //---------------------------------------------------------
     108              : //   Pedal
     109              : //---------------------------------------------------------
     110              : 
     111          155 : Pedal::Pedal(DummyParent* parent)
     112          155 :     : TextLineBase(ElementType::PEDAL, parent)
     113              : {
     114          155 :     initElementStyle(&pedalStyle);
     115          155 :     setLineVisible(true);
     116              : 
     117          155 :     resetProperty(Pid::LINE_WIDTH);
     118          155 :     resetProperty(Pid::LINE_STYLE);
     119              : 
     120          155 :     resetProperty(Pid::BEGIN_HOOK_TYPE);
     121          155 :     resetProperty(Pid::END_HOOK_TYPE);
     122              : 
     123          155 :     resetProperty(Pid::BEGIN_TEXT_PLACE);
     124          155 :     resetProperty(Pid::LINE_VISIBLE);
     125          155 :     resetProperty(Pid::END_TEXT);
     126          155 : }
     127              : 
     128              : //---------------------------------------------------------
     129              : //   createLineSegment
     130              : //---------------------------------------------------------
     131              : 
     132              : static const ElementStyle pedalSegmentStyle {
     133              :     { Sid::pedalMinDistance, Pid::MIN_DISTANCE },
     134              : };
     135              : 
     136           74 : LineSegment* Pedal::createLineSegment()
     137              : {
     138           74 :     PedalSegment* p = new PedalSegment(this);
     139           74 :     p->setTrack(track());
     140           74 :     p->initElementStyle(&pedalSegmentStyle);
     141           74 :     return p;
     142              : }
     143              : 
     144              : //---------------------------------------------------------
     145              : //   propertyDefault
     146              : //---------------------------------------------------------
     147              : 
     148         3213 : engraving::PropertyValue Pedal::propertyDefault(Pid propertyId) const
     149              : {
     150         3213 :     switch (propertyId) {
     151          158 :     case Pid::LINE_WIDTH:
     152          158 :         return style().styleS(Sid::pedalLineWidth);              // return point, not spatium
     153              : 
     154          158 :     case Pid::LINE_STYLE:
     155          158 :         return style().styleV(Sid::pedalLineStyle);
     156              : 
     157          687 :     case Pid::BEGIN_TEXT:
     158              :     case Pid::CONTINUE_TEXT:
     159              :     case Pid::END_TEXT:
     160          687 :         return style().styleV(getPropertyStyle(propertyId));
     161              : 
     162          344 :     case Pid::BEGIN_TEXT_PLACE:
     163              :     case Pid::CONTINUE_TEXT_PLACE:
     164              :     case Pid::END_TEXT_PLACE:
     165          344 :         return TextPlace::LEFT;
     166              : 
     167          189 :     case Pid::BEGIN_TEXT_OFFSET:
     168              :     case Pid::CONTINUE_TEXT_OFFSET:
     169              :     case Pid::END_TEXT_OFFSET:
     170          189 :         return PropertyValue::fromValue(PointF(0, 0));
     171              : 
     172          436 :     case Pid::BEGIN_HOOK_TYPE:
     173              :     case Pid::END_HOOK_TYPE:
     174          436 :         return HookType::NONE;
     175              : 
     176          218 :     case Pid::LINE_VISIBLE:
     177          218 :         return true;
     178              : 
     179            9 :     case Pid::PLACEMENT:
     180            9 :         return style().styleV(Sid::pedalPlacement);
     181              : 
     182          148 :     case Pid::TEXT_STYLE:
     183          148 :         return TextStyleType::PEDAL;
     184              : 
     185          866 :     default:
     186          866 :         return TextLineBase::propertyDefault(propertyId);
     187              :     }
     188              : }
     189              : 
     190         7753 : bool Pedal::setProperty(Pid propertyId, const PropertyValue& v)
     191              : {
     192              :     // Update style flag for text
     193         7753 :     if (propertyId == Pid::BEGIN_HOOK_TYPE) {
     194          158 :         setBeginHookType(v.value<HookType>());
     195              : 
     196          316 :         PropertyFlags beginTextStyleFlag = beginText() == propertyDefault(Pid::BEGIN_TEXT).value<String>()
     197          158 :                                            ? PropertyFlags::STYLED : PropertyFlags::UNSTYLED;
     198          158 :         setPropertyFlags(Pid::BEGIN_TEXT, beginTextStyleFlag);
     199          316 :         PropertyFlags continueTextStyleFlag = continueText() == propertyDefault(Pid::CONTINUE_TEXT).value<String>()
     200          158 :                                               ? PropertyFlags::STYLED : PropertyFlags::UNSTYLED;
     201          158 :         setPropertyFlags(Pid::CONTINUE_TEXT, continueTextStyleFlag);
     202         7595 :     } else if (propertyId == Pid::LINE_VISIBLE) {
     203          158 :         setLineVisible(v.toBool());
     204          316 :         PropertyFlags endTextStyleFlag = endText() == propertyDefault(Pid::END_TEXT).value<String>()
     205          158 :                                          ? PropertyFlags::STYLED : PropertyFlags::UNSTYLED;
     206          158 :         setPropertyFlags(Pid::END_TEXT, endTextStyleFlag);
     207              :     } else {
     208         7437 :         return TextLineBase::setProperty(propertyId, v);
     209              :     }
     210              : 
     211          316 :     triggerLayout();
     212          316 :     return true;
     213              : }
     214              : 
     215          115 : Pedal* Pedal::findNextInStaff() const
     216              : {
     217          115 :     Fraction endTick = tick2();
     218          115 :     auto spanners = score()->spannerMap().findOverlapping(endTick.ticks(), score()->endTick().ticks());
     219          831 :     for (auto element : spanners) {
     220          733 :         Spanner* spanner = element.value;
     221          733 :         if (spanner->isPedal() && spanner != this && spanner->staffIdx() == staffIdx() && spanner->tick() == endTick) {
     222           17 :             return toPedal(spanner);
     223              :         }
     224              :     }
     225              : 
     226           98 :     return nullptr;
     227          115 : }
     228              : 
     229           24 : bool Pedal::connect45HookToNext() const
     230              : {
     231           24 :     if (endHookType() != HookType::HOOK_45) {
     232           24 :         return false;
     233              :     }
     234              : 
     235            0 :     Pedal* nextPedal = findNextInStaff();
     236              : 
     237            0 :     return nextPedal && nextPedal->tick() == tick2() && nextPedal->beginHookType() == HookType::HOOK_45;
     238              : }
     239              : 
     240              : //---------------------------------------------------------
     241              : //   linePos
     242              : //    return System() coordinates
     243              : //---------------------------------------------------------
     244              : 
     245          230 : PointF Pedal::linePos(Grip grip, System** sys) const
     246              : {
     247          230 :     bool start = grip == Grip::START;
     248              : 
     249          230 :     if (start) {
     250          115 :         Segment* startSeg = startSegment();
     251          115 :         if (!startSeg) {
     252            0 :             return PointF();
     253              :         }
     254          115 :         *sys = startSeg->measure()->system();
     255          115 :         double x = startSeg->x() + startSeg->measure()->x();
     256          115 :         if (beginText() == "<sym>keyboardPedalPed</sym>") {
     257           85 :             x -= 0.5 * spatium();
     258           30 :         } else if (beginHookType() == HookType::HOOK_90 || beginHookType() == HookType::HOOK_90T) {
     259           17 :             x += 0.5 * absoluteFromSpatium(lineWidth());
     260           13 :         } else if (beginHookType() == HookType::HOOK_45) {
     261            8 :             EngravingItem* item = startElement();
     262            8 :             if (item && item->isChord()) {
     263            8 :                 Note* downNote = toChord(item)->downNote();
     264            8 :                 x += 0.5 * downNote->headWidth();
     265            0 :             } else if (item && item->isRest()) {
     266            0 :                 x += toRest(item)->centerX();
     267              :             }
     268              :         }
     269          115 :         return PointF(x, 0.0);
     270              :     }
     271              : 
     272          115 :     Segment* endSeg = endSegment();
     273          115 :     if (!endSeg) {
     274            0 :         return PointF();
     275              :     }
     276              : 
     277          115 :     Pedal* nextPedal = findNextInStaff();
     278              : 
     279          115 :     if (nextPedal && endHookType() == HookType::HOOK_45) {
     280            8 :         *sys = endSeg->measure()->system();
     281            8 :         double x = endSeg->x() + endSeg->measure()->x();
     282            8 :         EngravingItem* item = endElement();
     283            8 :         if (item && item->isChord()) {
     284            8 :             Note* downNote = toChord(item)->downNote();
     285            8 :             x += 0.5 * downNote->headWidth();
     286            0 :         } else if (item && item->isRest()) {
     287            0 :             x += toRest(item)->centerX();
     288              :         }
     289            8 :         return PointF(x, 0.0);
     290              :     }
     291              : 
     292          107 :     if (endSeg->rtick() == Fraction(0, 1)) {
     293           56 :         Segment* prevSeg = endSeg->prev1(SegmentType::EndBarLine);
     294           56 :         if (prevSeg) {
     295           56 :             endSeg = prevSeg;
     296              :         }
     297              :     }
     298              : 
     299          107 :     *sys = endSeg->measure()->system();
     300          107 :     double x = endSeg->x() + endSeg->measure()->x();
     301              : 
     302          107 :     x -= (endSeg->isChordRestType() && nextPedal ? 1.25 : 0.75) * spatium();
     303              : 
     304          107 :     return PointF(x, 0.0);
     305              : }
     306              : 
     307          122 : Sid Pedal::defaultPosSid() const
     308              : {
     309          122 :     return placeAbove() ? Sid::pedalPosAbove : Sid::pedalPosBelow;
     310              : }
     311              : }
        

Generated by: LCOV version 2.0-1