LCOV - code coverage report
Current view: top level - src/notation/internal - masternotation.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 0.0 % 448 0
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 "masternotation.h"
      24              : 
      25              : #include <QFileInfo>
      26              : 
      27              : #include "async/notifylist.h"
      28              : #include "log.h"
      29              : #include "realfn.h"
      30              : #include "translation.h"
      31              : 
      32              : #include "engraving/style/defaultstyle.h"
      33              : #include "engraving/style/pagestyle.h"
      34              : 
      35              : #include "engraving/dom/factory.h"
      36              : #include "engraving/dom/masterscore.h"
      37              : #include "engraving/dom/part.h"
      38              : #include "engraving/dom/staff.h"
      39              : #include "engraving/dom/excerpt.h"
      40              : #include "engraving/dom/measure.h"
      41              : #include "engraving/dom/box.h"
      42              : #include "engraving/dom/keysig.h"
      43              : #include "engraving/dom/sig.h"
      44              : #include "engraving/dom/tempotext.h"
      45              : 
      46              : #include "engraving/editing/autospatium.h"
      47              : #include "engraving/editing/editkeysig.h"
      48              : #include "engraving/editing/edittimesig.h"
      49              : #include "engraving/editing/transaction/transaction.h"
      50              : 
      51              : #include "inotationelements.h" // IWYU pragma: keep
      52              : #include "inotationparts.h"
      53              : #include "inotationsolomutestate.h"
      54              : #include "excerptnotation.h"
      55              : #include "notationautomation.h"
      56              : #include "types/scorecreateoptions.h"
      57              : 
      58              : #ifdef MUE_BUILD_ENGRAVING_PLAYBACK
      59              : #include "notationplayback.h"
      60              : #else
      61              : #include "notationplaybackstub.h"
      62              : #endif
      63              : 
      64              : #include "../notationerrors.h"
      65              : 
      66              : using namespace mu::notation;
      67              : using namespace mu::engraving;
      68              : using namespace muse;
      69              : using namespace muse::async;
      70              : 
      71            0 : static ExcerptNotation* get_impl(const IExcerptNotationPtr& excerpt)
      72              : {
      73            0 :     return static_cast<ExcerptNotation*>(excerpt.get());
      74              : }
      75              : 
      76            0 : static IExcerptNotationPtr createAndInitExcerptNotation(MasterNotation* master, mu::engraving::Excerpt* excerpt,
      77              :                                                         const muse::modularity::ContextPtr& iocCtx)
      78              : {
      79            0 :     auto excerptNotation = std::make_shared<ExcerptNotation>(master, excerpt, iocCtx);
      80            0 :     excerptNotation->init();
      81              : 
      82            0 :     return excerptNotation;
      83            0 : }
      84              : 
      85            0 : MasterNotation::MasterNotation(project::INotationProject* project, const muse::modularity::ContextPtr& iocCtx)
      86            0 :     : Notation(this, iocCtx), m_project(project)
      87              : {
      88              : #ifdef MUE_BUILD_ENGRAVING_PLAYBACK
      89            0 :     m_notationPlayback = std::make_shared<NotationPlayback>(this, m_notationChanged, iocCtx);
      90              : #else
      91              :     m_notationPlayback = std::make_shared<NotationPlaybackStub>();
      92              : #endif
      93              : 
      94            0 :     m_notationAutomation = std::make_shared<NotationAutomation>(undoStack());
      95              : 
      96            0 :     async::NotifyList<const Part*> partList = m_parts->partList();
      97              : 
      98            0 :     partList.onChanged(this, [this]() {
      99            0 :         onPartsChanged();
     100            0 :     });
     101              : 
     102            0 :     partList.onItemAdded(this, [this](const Part*) {
     103            0 :         onPartsChanged();
     104            0 :     });
     105              : 
     106            0 :     partList.onItemRemoved(this, [this](const Part*) {
     107            0 :         onPartsChanged();
     108            0 :     });
     109            0 : }
     110              : 
     111            0 : MasterNotation::~MasterNotation()
     112              : {
     113            0 :     m_parts = nullptr;
     114              : 
     115            0 :     unloadExcerpts(m_potentialExcerpts);
     116            0 : }
     117              : 
     118            0 : mu::project::INotationProject* MasterNotation::project() const
     119              : {
     120            0 :     return m_project;
     121              : }
     122              : 
     123            0 : int MasterNotation::mscVersion() const
     124              : {
     125            0 :     if (!masterScore()) {
     126            0 :         return 0;
     127              :     }
     128              : 
     129            0 :     return masterScore()->mscVersion();
     130              : }
     131              : 
     132            0 : INotationPtr MasterNotation::notation()
     133              : {
     134            0 :     return shared_from_this();
     135              : }
     136              : 
     137            0 : void MasterNotation::initAfterSettingScore(const MasterScore* score, bool disablePlayback)
     138              : {
     139            0 :     IF_ASSERT_FAILED(score) {
     140            0 :         return;
     141              :     }
     142              : 
     143            0 :     TRACEFUNC;
     144              : 
     145            0 :     score->changesChannel().onReceive(this, [this](const ScoreChanges& changes) {
     146            0 :         if (!changes.isTextEditing) {
     147            0 :             updateExcerpts();
     148              :         }
     149            0 :     });
     150              : 
     151            0 :     if (!disablePlayback) {
     152            0 :         m_notationPlayback->init();
     153              :     }
     154              : 
     155            0 :     initExcerptNotations(score->excerpts());
     156            0 : }
     157              : 
     158            0 : void MasterNotation::setMasterScore(mu::engraving::MasterScore* score, bool disablePlayback)
     159              : {
     160            0 :     if (masterScore() == score) {
     161            0 :         return;
     162              :     }
     163              : 
     164            0 :     TRACEFUNC;
     165              : 
     166            0 :     setScore(score);
     167            0 :     std::static_pointer_cast<NotationAutomation>(m_notationAutomation)->setMasterScore(score);
     168              : 
     169            0 :     score->updateSwing();
     170              : 
     171            0 :     initAfterSettingScore(score, disablePlayback);
     172            0 : }
     173              : 
     174            0 : mu::engraving::MasterScore* MasterNotation::masterScore() const
     175              : {
     176            0 :     return dynamic_cast<mu::engraving::MasterScore*>(score());
     177              : }
     178              : 
     179            0 : static void clearMeasures(mu::engraving::MasterScore* masterScore)
     180              : {
     181            0 :     TRACEFUNC;
     182              : 
     183            0 :     for (mu::engraving::Score* score : masterScore->scoreList()) {
     184            0 :         for (Part* part : score->parts()) {
     185            0 :             part->removeNonPrimaryInstruments();
     186              :         }
     187              : 
     188            0 :         mu::engraving::MeasureBaseList* measures = score->measures();
     189            0 :         for (mu::engraving::MeasureBase* measure = measures->first(); measure; measure = measure->next()) {
     190            0 :             measure->deleteLater();
     191              :         }
     192              : 
     193            0 :         auto spanners = score->spanner();
     194            0 :         for (auto spanner = spanners.begin(); spanner != spanners.end(); spanner = ++spanner) {
     195            0 :             score->removeSpanner(spanner->second);
     196              :         }
     197              : 
     198            0 :         measures->clear();
     199            0 :     }
     200              : 
     201            0 :     masterScore->invalidateRepeatList();
     202            0 :     masterScore->updateRepeatList();
     203            0 : }
     204              : 
     205            0 : static void createMeasures(MasterScore* masterScore, const ScoreCreateOptions& scoreOptions)
     206              : {
     207            0 :     TRACEFUNC;
     208              : 
     209            0 :     masterScore->sigmap()->add(0, scoreOptions.globalTimesig);
     210              : 
     211            0 :     KeySigEvent keySigEvent;
     212            0 :     if (scoreOptions.key == Key::INVALID) {
     213              :         // Make atonal key signature
     214            0 :         keySigEvent.setCustom(true);
     215            0 :         keySigEvent.setMode(KeyMode::NONE);
     216              :     } else {
     217            0 :         keySigEvent.setConcertKey(scoreOptions.key);
     218              :     }
     219              : 
     220              :     // Ensure all key sigs that may have been in the template file are cleared
     221            0 :     for (Staff* staff : masterScore->staves()) {
     222            0 :         KeyList& keys = staff->keyList();
     223            0 :         keys.clear();
     224              :     }
     225              : 
     226            0 :     const int totalMeasures = scoreOptions.withPickupMeasure ? scoreOptions.totalMeasures + 1 : scoreOptions.totalMeasures;
     227            0 :     for (int i = 0; i < totalMeasures; ++i) {
     228            0 :         MeasureBase* measureBase = masterScore->insertMeasure();
     229            0 :         Measure* measure = measureBase && measureBase->isMeasure() ? toMeasure(measureBase) : nullptr;
     230            0 :         IF_ASSERT_FAILED(measure) {
     231            0 :             break;
     232              :         }
     233              : 
     234            0 :         measure->setTimesig(scoreOptions.globalTimesig);
     235              : 
     236            0 :         if (i > 0) {
     237            0 :             measure->setTicks(scoreOptions.globalTimesig);
     238            0 :             continue;
     239              :         }
     240              : 
     241              :         // Special handling for first measure (apply pickups, timesigs, and keysigs)...
     242              : 
     243            0 :         measure->setExcludeFromNumbering(scoreOptions.withPickupMeasure);
     244            0 :         measure->adjustToLen(scoreOptions.withPickupMeasure ? scoreOptions.pickupTimesig : scoreOptions.globalTimesig);
     245              : 
     246              :         // Add timesigs...
     247            0 :         TimeSig* timesig = Factory::createTimeSig(masterScore->dummy());
     248            0 :         timesig->setSig(scoreOptions.globalTimesig, scoreOptions.timesigType);
     249            0 :         Transaction& tx = masterScore->transactionManager()->currentOrDummyTransaction();
     250            0 :         EditTimeSig::addTimeSig(tx, masterScore, measure, /*staffIdx*/ 0, timesig, /*local*/ false);
     251              : 
     252            0 :         for (Staff* staff : masterScore->staves()) {
     253              :             // Add keysig for each staff...
     254            0 :             mu::engraving::EditKeySig::undoChangeKeySig(tx, masterScore, staff, measure->tick(), keySigEvent);
     255              :         }
     256              :     }
     257            0 : }
     258              : 
     259              : //! NOTE: this method with all of its dependencies was copied from MU3
     260              : //! source: file.cpp, MuseScore::getNewFile()
     261            0 : Ret MasterNotation::setupNewScore(mu::engraving::MasterScore* score, const ScoreCreateOptions& scoreOptions)
     262              : {
     263            0 :     TRACEFUNC;
     264              : 
     265            0 :     setScore(score);
     266            0 :     std::static_pointer_cast<NotationAutomation>(m_notationAutomation)->setMasterScore(score);
     267              : 
     268            0 :     undoStack()->lock();
     269              : 
     270            0 :     parts()->setParts(scoreOptions.parts, scoreOptions.order);
     271              : 
     272            0 :     score->checkChordList();
     273            0 :     score->updateSwing();
     274            0 :     score->updateCapo();
     275              : 
     276            0 :     applyOptions(score, scoreOptions);
     277              : 
     278            0 :     score->initAutomation();
     279              : 
     280            0 :     initAfterSettingScore(score);
     281            0 :     addExcerptsToMasterScore(score->excerpts());
     282              : 
     283            0 :     undoStack()->unlock();
     284              : 
     285            0 :     return make_ret(Err::NoError);
     286            0 : }
     287              : 
     288            0 : void MasterNotation::applyOptions(mu::engraving::MasterScore* score, const ScoreCreateOptions& scoreOptions, bool createdFromTemplate)
     289              : {
     290            0 :     TRACEFUNC;
     291              : 
     292            0 :     mu::engraving::VBox* nvb = nullptr;
     293              : 
     294            0 :     if (createdFromTemplate) {
     295            0 :         mu::engraving::MeasureBase* mb = score->first();
     296            0 :         if (mb && mb->isVBox()) {
     297            0 :             mu::engraving::VBox* tvb = toVBox(mb);
     298            0 :             nvb = Factory::createTitleVBox(score);
     299            0 :             nvb->setBoxHeight(tvb->boxHeight());
     300            0 :             nvb->setBoxWidth(tvb->boxWidth());
     301            0 :             nvb->setTopGap(tvb->topGap());
     302            0 :             nvb->setBottomGap(tvb->bottomGap());
     303            0 :             nvb->setTopMargin(tvb->topMargin());
     304            0 :             nvb->setBottomMargin(tvb->bottomMargin());
     305            0 :             nvb->setLeftMargin(tvb->leftMargin());
     306            0 :             nvb->setRightMargin(tvb->rightMargin());
     307            0 :             nvb->setAutoSizeEnabled(tvb->isAutoSizeEnabled());
     308              :         }
     309              : 
     310            0 :         for (mu::engraving::Score* s : score->scoreList()) {
     311            0 :             s->clearSystemLocks();
     312            0 :         }
     313            0 :         clearMeasures(score);
     314              : 
     315              :         // for templates using built-in base page style, set score page style to default (may be user-defined)
     316            0 :         for (auto st : pageStyles()) {
     317            0 :             if (st != Sid::pageTwosided && RealIsEqual(score->style().styleD(st), DefaultStyle::baseStyle().styleD(st))) {
     318            0 :                 score->style().set(st, DefaultStyle::defaultStyle().value(st));
     319              :             }
     320              :         }
     321              :     }
     322              : 
     323            0 :     score->checkChordList();
     324              : 
     325            0 :     createMeasures(score, scoreOptions);
     326              : 
     327              :     {
     328            0 :         const QString title = score->metaTag(u"workTitle");
     329            0 :         const QString subtitle = score->metaTag(u"subtitle");
     330            0 :         const QString composer = score->metaTag(u"composer");
     331            0 :         const QString lyricist = score->metaTag(u"lyricist");
     332              : 
     333            0 :         if (!title.isEmpty() || !subtitle.isEmpty() || !composer.isEmpty() || !lyricist.isEmpty()) {
     334            0 :             mu::engraving::MeasureBase* measure = score->measures()->first();
     335            0 :             if (!measure->isVBox()) {
     336            0 :                 if (!nvb) {
     337            0 :                     nvb = Factory::createTitleVBox(score);
     338              :                 }
     339            0 :                 nvb->setTick(mu::engraving::Fraction(0, 1));
     340            0 :                 nvb->setNext(measure);
     341            0 :                 score->measures()->add(nvb);
     342            0 :             } else if (nvb) {
     343            0 :                 delete nvb;
     344            0 :                 nvb = nullptr;
     345              :             }
     346              : 
     347            0 :             auto setText = [score](mu::engraving::TextStyleType textItemId, const QString& text) {
     348            0 :                 mu::engraving::TextBase* textItem = score->getText(textItemId);
     349              : 
     350            0 :                 if (!textItem) {
     351            0 :                     textItem = score->addText(textItemId);
     352              :                 }
     353              : 
     354            0 :                 if (textItem) {
     355            0 :                     textItem->setPlainText(text);
     356              :                 }
     357            0 :             };
     358              : 
     359            0 :             if (!title.isEmpty()) {
     360            0 :                 setText(mu::engraving::TextStyleType::TITLE, title);
     361              :             }
     362            0 :             if (!subtitle.isEmpty()) {
     363            0 :                 setText(mu::engraving::TextStyleType::SUBTITLE, subtitle);
     364              :             }
     365            0 :             if (!composer.isEmpty()) {
     366            0 :                 setText(mu::engraving::TextStyleType::COMPOSER, composer);
     367              :             }
     368            0 :             if (!lyricist.isEmpty()) {
     369            0 :                 setText(mu::engraving::TextStyleType::LYRICIST, lyricist);
     370              :             }
     371            0 :         } else if (nvb) {
     372            0 :             delete nvb;
     373            0 :             nvb = nullptr;
     374              :         }
     375              : 
     376            0 :         if (nvb) {
     377            0 :             nvb->manageExclusionFromParts(/*exclude*/ false);
     378              :         }
     379            0 :     }
     380              : 
     381            0 :     if (scoreOptions.withTempo) {
     382            0 :         String text { u"<sym>metNoteQuarterUp</sym> = %1" };
     383            0 :         const double bpm = scoreOptions.tempo.valueBpm;
     384            0 :         double qpm = bpm; // internal tempo value is always expressed in quarter notes
     385              : 
     386            0 :         const bool withDot = scoreOptions.tempo.withDot;
     387            0 :         switch (scoreOptions.tempo.duration) {
     388            0 :         case DurationType::V_WHOLE:
     389            0 :             text = "<sym>metNoteWhole</sym> = %1";
     390            0 :             qpm *= 4.0;
     391            0 :             break;
     392            0 :         case DurationType::V_HALF:
     393            0 :             if (withDot) {
     394            0 :                 text = "<sym>metNoteHalfUp</sym><sym>space</sym><sym>metAugmentationDot</sym> = %1";
     395            0 :                 qpm *= 3.0;
     396              :             } else {
     397            0 :                 text = "<sym>metNoteHalfUp</sym> = %1";
     398            0 :                 qpm *= 2.0;
     399              :             }
     400            0 :             break;
     401            0 :         case DurationType::V_QUARTER:
     402            0 :             if (withDot) {
     403            0 :                 text = "<sym>metNoteQuarterUp</sym><sym>space</sym><sym>metAugmentationDot</sym> = %1";
     404            0 :                 qpm *= 1.5;
     405              :             } else {
     406            0 :                 text = "<sym>metNoteQuarterUp</sym> = %1";
     407              :                 //qpm *= 1.0;
     408              :             }
     409            0 :             break;
     410            0 :         case DurationType::V_EIGHTH:
     411            0 :             if (withDot) {
     412            0 :                 text = "<sym>metNote8thUp</sym><sym>space</sym><sym>metAugmentationDot</sym> = %1";
     413            0 :                 qpm *= 0.75;
     414              :             } else {
     415            0 :                 text = "<sym>metNote8thUp</sym> = %1";
     416            0 :                 qpm *= 0.5;
     417              :             }
     418            0 :             break;
     419            0 :         case DurationType::V_16TH:
     420            0 :             if (withDot) {
     421            0 :                 text = "<sym>metNote16thUp</sym><sym>space</sym><sym>metAugmentationDot</sym> = %1";
     422            0 :                 qpm *= 0.375;
     423              :             } else {
     424            0 :                 text = "<sym>metNote16thUp</sym> = %1";
     425            0 :                 qpm *= 0.25;
     426              :             }
     427            0 :             break;
     428            0 :         default:
     429            0 :             break;
     430              :         }
     431              : 
     432            0 :         engraving::Segment* seg = score->firstMeasure()->first(engraving::SegmentType::ChordRest);
     433            0 :         engraving::TempoText* tt = new engraving::TempoText(seg);
     434            0 :         tt->setXmlText(text.arg(bpm));
     435            0 :         tt->setTempo(qpm / 60.0); // qpm -> qps
     436            0 :         tt->setFollowText(true);
     437            0 :         tt->setTrack(0);
     438            0 :         seg->add(tt);
     439            0 :         for (auto staff : score->systemObjectStaves()) {
     440            0 :             TempoText* linkedTt = toTempoText(tt->linkedClone());
     441            0 :             linkedTt->setScore(score);
     442            0 :             linkedTt->setTrack(staff->idx() * VOICES);
     443            0 :             seg->add(linkedTt);
     444              :         }
     445            0 :     }
     446              : 
     447            0 :     score->updateTicksAndTimeSigMap();
     448            0 :     AutoSpatium::update(score);
     449              : 
     450              :     {
     451            0 :         mu::engraving::ScoreLoad sl;
     452            0 :         for (mu::engraving::Score* s : score->scoreList()) {
     453            0 :             s->doLayout();
     454            0 :         }
     455            0 :     }
     456              : 
     457            0 :     if (m_notationPlayback) {
     458            0 :         m_notationPlayback->reload();
     459              :     }
     460            0 : }
     461              : 
     462            0 : void MasterNotation::unloadExcerpts(ExcerptNotationList& excerpts)
     463              : {
     464            0 :     for (const IExcerptNotationPtr& ptr : excerpts) {
     465            0 :         Excerpt* excerpt = get_impl(ptr)->excerpt();
     466            0 :         if (!excerpt) {
     467            0 :             continue;
     468              :         }
     469              : 
     470            0 :         if (Score* score = excerpt->excerptScore()) {
     471            0 :             delete score;
     472            0 :             excerpt->setExcerptScore(nullptr);
     473              :         }
     474              :     }
     475              : 
     476            0 :     excerpts.clear();
     477            0 : }
     478              : 
     479            0 : void MasterNotation::initExcerpts(const ExcerptNotationList& excerpts)
     480              : {
     481            0 :     for (const IExcerptNotationPtr& excerptNotation : excerpts) {
     482            0 :         ExcerptNotation* impl = get_impl(excerptNotation);
     483              : 
     484            0 :         masterScore()->initExcerpt(impl->excerpt());
     485            0 :         impl->init();
     486              : 
     487            0 :         initNotationSoloMuteState(impl->notation());
     488              :     }
     489            0 : }
     490              : 
     491            0 : void MasterNotation::setExcerpts(const ExcerptNotationList& excerpts)
     492              : {
     493            0 :     TRACEFUNC;
     494              : 
     495            0 :     if (m_excerpts == excerpts) {
     496            0 :         return;
     497              :     }
     498              : 
     499            0 :     MasterScore* score = masterScore();
     500            0 :     IF_ASSERT_FAILED(score) {
     501            0 :         return;
     502              :     }
     503              : 
     504            0 :     undoStack()->prepareChanges(TranslatableString("undoableAction", "Add/remove parts"));
     505              : 
     506              :     // Delete old excerpts (that are not included in the new list)
     507            0 :     for (const IExcerptNotationPtr& excerptNotation : m_excerpts) {
     508            0 :         auto it = std::find(excerpts.begin(), excerpts.end(), excerptNotation);
     509            0 :         if (it != excerpts.end()) {
     510            0 :             continue;
     511              :         }
     512              : 
     513            0 :         score->deleteExcerpt(get_impl(excerptNotation)->excerpt());
     514              :     }
     515              : 
     516              :     // Init new excerpts
     517            0 :     for (size_t i = 0; i < excerpts.size(); ++i) {
     518            0 :         const IExcerptNotationPtr& excerptNotation = excerpts.at(i);
     519            0 :         ExcerptNotation* excerptNotationImpl = get_impl(excerptNotation);
     520              : 
     521            0 :         auto it = std::find(m_excerpts.cbegin(), m_excerpts.cend(), excerptNotation);
     522            0 :         if (it != m_excerpts.end()) {
     523            0 :             std::vector<Excerpt*>& msExcerpts = score->excerpts();
     524            0 :             muse::moveItem(msExcerpts, muse::indexOf(msExcerpts, excerptNotationImpl->excerpt()), i);
     525            0 :             continue;
     526            0 :         }
     527              : 
     528            0 :         score->initAndAddExcerpt(excerptNotationImpl->excerpt(), false);
     529            0 :         excerptNotationImpl->init();
     530              : 
     531            0 :         initNotationSoloMuteState(excerptNotationImpl->notation());
     532              :     }
     533              : 
     534            0 :     score->setExcerptsChanged(false);
     535              : 
     536            0 :     undoStack()->commitChanges();
     537              : 
     538            0 :     doSetExcerpts(excerpts);
     539            0 : }
     540              : 
     541            0 : void MasterNotation::resetExcerpt(IExcerptNotationPtr& excerptNotation)
     542              : {
     543            0 :     if (!excerptNotation || !excerptNotation->isInited()) {
     544            0 :         return;
     545              :     }
     546              : 
     547            0 :     TRACEFUNC;
     548              : 
     549            0 :     undoStack()->prepareChanges(TranslatableString("undoableAction", "Reset part"));
     550              : 
     551            0 :     mu::engraving::Excerpt* oldExcerpt = get_impl(excerptNotation)->excerpt();
     552            0 :     masterScore()->deleteExcerpt(oldExcerpt);
     553              : 
     554            0 :     mu::engraving::Excerpt* newExcerpt = new mu::engraving::Excerpt(*oldExcerpt, false);
     555            0 :     masterScore()->initAndAddExcerpt(newExcerpt, false);
     556              : 
     557            0 :     newExcerpt->excerptScore()->setIsOpen(oldExcerpt->excerptScore()->isOpen());
     558              : 
     559            0 :     get_impl(excerptNotation)->reinit(newExcerpt);
     560              : 
     561            0 :     masterScore()->setExcerptsChanged(false);
     562              : 
     563            0 :     undoStack()->commitChanges();
     564            0 : }
     565              : 
     566            0 : void MasterNotation::sortExcerpts(ExcerptNotationList& excerpts)
     567              : {
     568            0 :     TRACEFUNC;
     569              : 
     570            0 :     std::vector<ID> partIdList;
     571            0 :     for (const Part* part : masterScore()->parts()) {
     572            0 :         partIdList.push_back(part->id());
     573              :     }
     574              : 
     575            0 :     std::sort(excerpts.begin(), excerpts.end(), [&partIdList](const IExcerptNotationPtr& f, const IExcerptNotationPtr& s) {
     576              :         //! NOTE: excertps created by users are always at the end of the list in alphabetical order
     577            0 :         if (f->isCustom() && !s->isCustom()) {
     578            0 :             return false;
     579            0 :         } else if (!f->isCustom() && s->isCustom()) {
     580            0 :             return true;
     581            0 :         } else if (f->isCustom() && s->isCustom()) {
     582            0 :             return f->name() < s->name();
     583              :         }
     584              : 
     585              :         //! NOTE: sort standard excerpts in the order of their initial parts in the main score
     586            0 :         const ID& initialPart1 = get_impl(f)->excerpt()->initialPartId();
     587            0 :         const ID& initialPart2 = get_impl(s)->excerpt()->initialPartId();
     588              : 
     589            0 :         size_t index1 = muse::indexOf(partIdList, initialPart1);
     590            0 :         size_t index2 = muse::indexOf(partIdList, initialPart2);
     591              : 
     592            0 :         return index1 < index2;
     593              :     });
     594            0 : }
     595              : 
     596            0 : void MasterNotation::setExcerptIsOpen(const INotationPtr excerptNotation, bool open)
     597              : {
     598            0 :     if (excerptNotation->isOpen() == open) {
     599            0 :         return;
     600              :     }
     601              : 
     602            0 :     excerptNotation->setIsOpen(open);
     603              : 
     604            0 :     if (open) {
     605            0 :         excerptNotation->elements()->msScore()->doLayout();
     606              :     }
     607              : }
     608              : 
     609            0 : void MasterNotation::doSetExcerpts(const ExcerptNotationList& excerpts)
     610              : {
     611            0 :     TRACEFUNC;
     612              : 
     613            0 :     m_excerpts = excerpts;
     614            0 :     m_excerptsChanged.notify();
     615              : 
     616            0 :     updatePotentialExcerpts();
     617            0 : }
     618              : 
     619            0 : void MasterNotation::updateExcerpts()
     620              : {
     621            0 :     if (!masterScore()->excerptsChanged()) {
     622            0 :         return;
     623              :     }
     624              : 
     625            0 :     TRACEFUNC;
     626              : 
     627            0 :     ExcerptNotationList updatedExcerpts;
     628              : 
     629            0 :     const std::vector<mu::engraving::Excerpt*>& excerpts = masterScore()->excerpts();
     630              : 
     631              :     // exclude notations for old excerpts
     632            0 :     for (const IExcerptNotationPtr& excerptNotation : m_excerpts) {
     633            0 :         ExcerptNotation* impl = get_impl(excerptNotation);
     634              : 
     635            0 :         if (muse::contains(excerpts, impl->excerpt())) {
     636            0 :             updatedExcerpts.push_back(excerptNotation);
     637            0 :             continue;
     638              :         }
     639              :     }
     640              : 
     641              :     // create notations for new excerpts
     642            0 :     for (mu::engraving::Excerpt* excerpt : excerpts) {
     643            0 :         if (containsExcerpt(excerpt)) {
     644            0 :             continue;
     645              :         }
     646              : 
     647            0 :         IExcerptNotationPtr excerptNotation = createAndInitExcerptNotation(this, excerpt, iocContext());
     648            0 :         bool open = excerpt->excerptScore()->isOpen();
     649            0 :         if (open) {
     650            0 :             excerptNotation->notation()->elements()->msScore()->doLayout();
     651              :         }
     652              : 
     653            0 :         initNotationSoloMuteState(excerptNotation->notation());
     654              : 
     655            0 :         updatedExcerpts.push_back(excerptNotation);
     656            0 :     }
     657              : 
     658            0 :     doSetExcerpts(updatedExcerpts);
     659              : 
     660            0 :     masterScore()->setExcerptsChanged(false);
     661            0 : }
     662              : 
     663            0 : void MasterNotation::updatePotentialExcerpts() const
     664              : {
     665            0 :     TRACEFUNC;
     666              : 
     667            0 :     auto findExcerptByPart = [](const ExcerptNotationList& excerpts, const Part* part) {
     668            0 :         return std::find_if(excerpts.cbegin(), excerpts.cend(), [part](const IExcerptNotationPtr& notation) {
     669            0 :             return get_impl(notation)->excerpt()->initialPartId() == part->id();
     670            0 :         });
     671              :     };
     672              : 
     673            0 :     ExcerptNotationList potentialExcerpts;
     674            0 :     std::vector<Part*> partsWithoutExcerpt;
     675              : 
     676            0 :     for (Part* part : score()->parts()) {
     677            0 :         if (part->isSharedPart() || findExcerptByPart(m_excerpts, part) != m_excerpts.end()) {
     678            0 :             continue;
     679              :         }
     680              : 
     681            0 :         if (!m_potentialExcerptsForcedDirty) {
     682            0 :             auto it = findExcerptByPart(m_potentialExcerpts, part);
     683            0 :             if (it != m_potentialExcerpts.cend()) {
     684            0 :                 potentialExcerpts.push_back(*it);
     685            0 :                 continue;
     686              :             }
     687              :         }
     688              : 
     689            0 :         partsWithoutExcerpt.push_back(part);
     690              :     }
     691              : 
     692            0 :     std::vector<mu::engraving::Excerpt*> excerpts = mu::engraving::Excerpt::createExcerptsFromParts(partsWithoutExcerpt, masterScore());
     693              : 
     694            0 :     for (mu::engraving::Excerpt* excerpt : excerpts) {
     695            0 :         auto excerptNotation = std::make_shared<ExcerptNotation>(const_cast<MasterNotation*>(this), excerpt, iocContext());
     696            0 :         potentialExcerpts.push_back(excerptNotation);
     697            0 :     }
     698              : 
     699            0 :     m_potentialExcerpts = std::move(potentialExcerpts);
     700            0 :     m_potentialExcerptsForcedDirty = false;
     701            0 : }
     702              : 
     703            0 : bool MasterNotation::containsExcerpt(const mu::engraving::Excerpt* excerpt) const
     704              : {
     705            0 :     for (const IExcerptNotationPtr& excerptNotation : m_excerpts) {
     706            0 :         if (get_impl(excerptNotation)->excerpt() == excerpt) {
     707            0 :             return true;
     708              :         }
     709              :     }
     710              : 
     711            0 :     return false;
     712              : }
     713              : 
     714            0 : void MasterNotation::onPartsChanged()
     715              : {
     716            0 :     m_hasPartsChanged.notify();
     717            0 :     m_potentialExcerptsForcedDirty = true;
     718            0 : }
     719              : 
     720            0 : IExcerptNotationPtr MasterNotation::createEmptyExcerpt(const QString& name) const
     721              : {
     722              :     auto excerptNotation
     723            0 :         = std::make_shared<ExcerptNotation>(const_cast<MasterNotation*>(this), new mu::engraving::Excerpt(masterScore()), iocContext());
     724            0 :     excerptNotation->setName(name);
     725              : 
     726            0 :     return excerptNotation;
     727            0 : }
     728              : 
     729            0 : const ExcerptNotationList& MasterNotation::excerpts() const
     730              : {
     731            0 :     return m_excerpts;
     732              : }
     733              : 
     734            0 : muse::async::Notification MasterNotation::excerptsChanged() const
     735              : {
     736            0 :     return m_excerptsChanged;
     737              : }
     738              : 
     739            0 : INotationPartsPtr MasterNotation::parts() const
     740              : {
     741            0 :     return m_parts;
     742              : }
     743              : 
     744            0 : bool MasterNotation::hasParts() const
     745              : {
     746            0 :     return m_parts && m_parts->hasParts();
     747              : }
     748              : 
     749            0 : Notification MasterNotation::hasPartsChanged() const
     750              : {
     751            0 :     return m_hasPartsChanged;
     752              : }
     753              : 
     754            0 : INotationPlaybackPtr MasterNotation::playback() const
     755              : {
     756            0 :     return m_notationPlayback;
     757              : }
     758              : 
     759            0 : INotationAutomationPtr MasterNotation::automation() const
     760              : {
     761            0 :     return m_notationAutomation;
     762              : }
     763              : 
     764            0 : void MasterNotation::initNotationSoloMuteState(const INotationPtr notation)
     765              : {
     766            0 :     IF_ASSERT_FAILED(notation) {
     767            0 :         return;
     768              :     }
     769              : 
     770            0 :     const INotationSoloMuteStatePtr excerptSoloMuteState = notation->soloMuteState();
     771              : 
     772              :     // Iterate over all parts (not just the excerpt parts) - tracks that do exist in the master notation but
     773              :     // not in the excerpt should be muted in the excerpt...
     774            0 :     for (const Part* masterPart : parts()->partList()) {
     775            0 :         const Part* excerptPart = notation->parts()->part(masterPart->id());
     776            0 :         const bool shouldMute = !excerptPart || !excerptPart->isVisible();
     777            0 :         const INotationSoloMuteState::SoloMuteState state = { shouldMute, /*solo*/ false };
     778            0 :         for (const InstrumentTrackId& trackId : masterPart->instrumentTrackIdSet()) {
     779            0 :             excerptSoloMuteState->setTrackSoloMuteState(trackId, state);
     780            0 :         }
     781            0 :         if (masterPart->hasChordSymbol()) {
     782            0 :             const InstrumentTrackId& chordsId = playback()->chordSymbolsTrackId(masterPart->id());
     783            0 :             excerptSoloMuteState->setTrackSoloMuteState(chordsId, state);
     784            0 :         }
     785            0 :     }
     786            0 : }
     787              : 
     788            0 : const ExcerptNotationList& MasterNotation::potentialExcerpts() const
     789              : {
     790            0 :     updatePotentialExcerpts();
     791              : 
     792            0 :     return m_potentialExcerpts;
     793              : }
     794              : 
     795            0 : void MasterNotation::initExcerptNotations(const std::vector<mu::engraving::Excerpt*>& excerpts)
     796              : {
     797            0 :     TRACEFUNC;
     798              : 
     799            0 :     ExcerptNotationList notationExcerpts;
     800              : 
     801            0 :     for (mu::engraving::Excerpt* excerpt : excerpts) {
     802            0 :         if (excerpt->isEmpty()) {
     803            0 :             masterScore()->initEmptyExcerpt(excerpt);
     804              :         }
     805              : 
     806            0 :         IExcerptNotationPtr excerptNotation = createAndInitExcerptNotation(this, excerpt, iocContext());
     807            0 :         notationExcerpts.push_back(excerptNotation);
     808            0 :     }
     809              : 
     810            0 :     masterScore()->setExcerptsChanged(false);
     811              : 
     812            0 :     doSetExcerpts(notationExcerpts);
     813            0 : }
     814              : 
     815            0 : void MasterNotation::addExcerptsToMasterScore(const std::vector<mu::engraving::Excerpt*>& excerpts)
     816              : {
     817            0 :     TRACEFUNC;
     818              : 
     819            0 :     for (mu::engraving::Excerpt* excerpt : excerpts) {
     820            0 :         masterScore()->initAndAddExcerpt(excerpt, false);
     821              :     }
     822              : 
     823            0 :     masterScore()->setExcerptsChanged(false);
     824            0 : }
        

Generated by: LCOV version 2.0-1