LCOV - code coverage report
Current view: top level - src/notation/internal - notationnoteinput.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 0.0 % 616 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              : #include "notationnoteinput.h"
      23              : 
      24              : #include "../notationerrors.h"
      25              : 
      26              : #include "engraving/dom/articulation.h"
      27              : #include "engraving/dom/chord.h"
      28              : #include "engraving/dom/drumset.h"
      29              : #include "engraving/dom/input.h"
      30              : #include "engraving/dom/masterscore.h"
      31              : #include "engraving/dom/mscore.h"
      32              : #include "engraving/dom/note.h"
      33              : #include "engraving/dom/part.h"
      34              : #include "engraving/dom/segment.h"
      35              : #include "engraving/dom/slur.h"
      36              : #include "engraving/dom/staff.h"
      37              : #include "engraving/dom/stafftype.h"
      38              : #include "engraving/dom/system.h"
      39              : #include "engraving/dom/tuplet.h"
      40              : #include "engraving/dom/utils.h"
      41              : 
      42              : #include "engraving/editing/editnote.h"
      43              : #include "engraving/editing/edittie.h"
      44              : #include "engraving/editing/noteinput.h"
      45              : #include "engraving/editing/transaction/transaction.h"
      46              : 
      47              : #include "log.h"
      48              : 
      49              : using namespace mu::notation;
      50              : using namespace mu::engraving;
      51              : using namespace muse;
      52              : using namespace muse::async;
      53              : 
      54            0 : TranslatableString nameOfNoteInputMethod(NoteInputMethod method)
      55              : {
      56            0 :     switch (method) {
      57            0 :     case NoteInputMethod::UNKNOWN:          break;
      58            0 :     case NoteInputMethod::DEFAULT:          return TranslatableString("noteInputMethod", "Input by default mode");
      59            0 :     case NoteInputMethod::BY_NOTE_NAME:     return TranslatableString("noteInputMethod", "Input by note name mode");
      60            0 :     case NoteInputMethod::BY_DURATION:      return TranslatableString("noteInputMethod", "Input by duration mode");
      61            0 :     case NoteInputMethod::REPITCH:          return TranslatableString("noteInputMethod", "Re-pitch existing notes mode");
      62            0 :     case NoteInputMethod::RHYTHM:           return TranslatableString("noteInputMethod", "Rhythm-only input mode");
      63            0 :     case NoteInputMethod::REALTIME_AUTO:    return TranslatableString("noteInputMethod", "Metronome real-time input mode");
      64            0 :     case NoteInputMethod::REALTIME_MANUAL:  return TranslatableString("noteInputMethod", "Pedal real-time input mode");
      65            0 :     case NoteInputMethod::TIMEWISE:         return TranslatableString("noteInputMethod", "Insert mode (grow measures)");
      66              :         // No default case. We want a compiler warning if an enum value is not handled here.
      67              :     }
      68              : 
      69            0 :     UNREACHABLE;
      70              :     return TranslatableString("noteInputMethod", "Unknown note input mode");
      71              : }
      72              : 
      73            0 : static bool noteInputMethodAvailable(NoteInputMethod method, const Staff* staff, const Fraction& tick)
      74              : {
      75            0 :     if (method == NoteInputMethod::BY_DURATION) {
      76            0 :         return staff && !staff->isTabStaff(tick);
      77              :     }
      78              : 
      79            0 :     return true;
      80              : }
      81              : 
      82            0 : NotationNoteInput::NotationNoteInput(const IGetScore* getScore, INotationInteraction* interaction, INotationUndoStackPtr undoStack
      83            0 :                                      , const modularity::ContextPtr& iocCtx)
      84            0 :     : muse::Contextable(iocCtx), m_getScore(getScore), m_interaction(interaction), m_undoStack(undoStack)
      85              : {
      86            0 :     m_interaction->selectionChanged().onNotify(this, [this]() {
      87            0 :         if (!isNoteInputMode()) {
      88            0 :             updateInputState();
      89            0 :         } else if (shouldSetupInputNote()) {
      90            0 :             const NoteInputState& is = state();
      91            0 :             const staff_idx_t prevStaffIdx = track2staff(is.prevTrack());
      92              : 
      93            0 :             if (prevStaffIdx != is.staffIdx()) {
      94            0 :                 setupInputNote();
      95              :             }
      96              :         }
      97            0 :     });
      98            0 : }
      99              : 
     100            0 : bool NotationNoteInput::isNoteInputMode() const
     101              : {
     102            0 :     return score()->inputState().noteEntryMode();
     103              : }
     104              : 
     105            0 : const NoteInputState& NotationNoteInput::state() const
     106              : {
     107            0 :     return score()->inputState();
     108              : }
     109              : 
     110              : //! NOTE Copied from `void ScoreView::startNoteEntry()`
     111            0 : void NotationNoteInput::startNoteInput(NoteInputMethod method, bool focusNotation)
     112              : {
     113            0 :     TRACEFUNC;
     114              : 
     115            0 :     if (isNoteInputMode()) {
     116            0 :         return;
     117              :     }
     118              : 
     119            0 :     EngravingItem* el = resolveNoteInputStartPosition();
     120            0 :     if (!el) {
     121            0 :         return;
     122              :     }
     123              : 
     124            0 :     if (Part* part = el->part(); part && part->isSharedPart()) {
     125            0 :         return;
     126              :     }
     127              : 
     128            0 :     m_interaction->select({ el }, SelectType::SINGLE, 0);
     129              : 
     130            0 :     InputState& is = score()->inputState();
     131              : 
     132            0 :     Duration d(is.duration());
     133            0 :     if (!d.isValid() || d.isZero() || d.type() == DurationType::V_MEASURE) {
     134            0 :         is.setDuration(Duration(DurationType::V_QUARTER));
     135              :     }
     136            0 :     is.setAccidentalType(AccidentalType::NONE);
     137              : 
     138            0 :     is.setRest(false);
     139            0 :     is.setNoteEntryMode(true);
     140              : 
     141            0 :     const Staff* staff = score()->staff(is.track() / VOICES);
     142              : 
     143            0 :     if (noteInputMethodAvailable(method, staff, is.tick())) {
     144            0 :         is.setNoteEntryMethod(method);
     145              :     } else {
     146            0 :         is.setNoteEntryMethod(NoteInputMethod::BY_NOTE_NAME); // fallback
     147              :     }
     148              : 
     149            0 :     if (shouldSetupInputNote()) {
     150            0 :         setupInputNote();
     151              :     }
     152              : 
     153            0 :     switch (staff->staffType(is.tick())->group()) {
     154            0 :     case StaffGroup::STANDARD:
     155              :     case StaffGroup::JIANPU:
     156            0 :         break;
     157            0 :     case StaffGroup::TAB: {
     158            0 :         int strg = 0;                           // assume topmost string as current string
     159              :         // if entering note entry with a note selected and the note has a string
     160              :         // set InputState::_string to note physical string
     161            0 :         if (el->isNote()) {
     162            0 :             strg = (toNote(el))->string();
     163              :         }
     164            0 :         is.setString(strg);
     165            0 :         break;
     166              :     }
     167            0 :     case StaffGroup::PERCUSSION:
     168            0 :         break;
     169              :     }
     170              : 
     171            0 :     notifyAboutNoteInputStarted(focusNotation);
     172            0 :     notifyAboutStateChanged();
     173              : 
     174            0 :     m_interaction->showItem(el);
     175            0 :     accessibilityController()->announce(nameOfNoteInputMethod(method).translated());
     176            0 : }
     177              : 
     178            0 : EngravingItem* NotationNoteInput::resolveNoteInputStartPosition() const
     179              : {
     180            0 :     ChordRest* topLeftChordRest = nullptr;
     181              : 
     182            0 :     if (score()->selection().isNone() && m_getViewRectFunc) {
     183              :         // no selection
     184              :         // choose page in current view (favor top left quadrant if possible)
     185              :         // select first (top/left) chordrest of that page in current view
     186              :         // or, CR at last selected position if that is in view
     187            0 :         RectF viewRect = m_getViewRectFunc();
     188            0 :         PointF topLeft = viewRect.topLeft();
     189              : 
     190            0 :         std::vector<PointF> points;
     191            0 :         points.push_back({ topLeft.x() + viewRect.width() * 0.25, topLeft.y() + viewRect.height() * 0.25 });
     192            0 :         points.push_back(topLeft);
     193            0 :         points.push_back(viewRect.bottomLeft());
     194            0 :         points.push_back(viewRect.topRight());
     195            0 :         points.push_back(viewRect.bottomRight());
     196              : 
     197            0 :         Page* page = nullptr;
     198            0 :         for (const PointF& point : points) {
     199            0 :             page = score()->searchPage(point);
     200            0 :             if (page) {
     201            0 :                 break;
     202              :             }
     203              :         }
     204              : 
     205            0 :         if (page) {
     206            0 :             qreal tlY = 0.0;
     207            0 :             Fraction tlTick = Fraction(0, 1);
     208            0 :             RectF pageRect  = page->ldata()->bbox().translated(page->x(), page->y());
     209            0 :             RectF intersect = viewRect & pageRect;
     210            0 :             intersect.translate(-page->x(), -page->y());
     211            0 :             std::vector<EngravingItem*> el = page->items(intersect);
     212              : 
     213            0 :             const ChordRest* lastSelected = score()->selection().currentCR();
     214              : 
     215            0 :             if (lastSelected && lastSelected->voice()) {
     216              :                 // if last selected CR was not in voice 1,
     217              :                 // find CR in voice 1 instead
     218            0 :                 track_idx_t track = trackZeroVoice(lastSelected->track());
     219            0 :                 const Segment* s = lastSelected->segment();
     220            0 :                 if (s) {
     221            0 :                     lastSelected = s->nextChordRest(track, true);
     222              :                 }
     223              :             }
     224              : 
     225            0 :             for (EngravingItem* e : el) {
     226              :                 // loop through visible elements
     227              :                 // looking for the CR in voice 1 with earliest tick and highest staff position
     228              :                 // but stop if we find the last selected CR
     229            0 :                 ElementType et = e->type();
     230            0 :                 if (et == ElementType::NOTE || et == ElementType::REST) {
     231            0 :                     if (e->voice()) {
     232            0 :                         continue;
     233              :                     }
     234              :                     ChordRest* cr;
     235            0 :                     if (et == ElementType::NOTE) {
     236            0 :                         cr = toChordRest(e->parent());
     237            0 :                         if (!cr) {
     238            0 :                             continue;
     239              :                         }
     240              :                     } else {
     241            0 :                         cr = toChordRest(e);
     242              :                     }
     243            0 :                     if (cr == lastSelected) {
     244            0 :                         topLeftChordRest = cr;
     245            0 :                         break;
     246              :                     }
     247              :                     // compare ticks rather than x position
     248              :                     // to make sure we favor earlier rather than later systems
     249              :                     // even though later system might have note farther to left
     250            0 :                     Fraction crTick = Fraction(0, 1);
     251            0 :                     if (cr->segment()) {
     252            0 :                         crTick = cr->segment()->tick();
     253              :                     } else {
     254            0 :                         continue;
     255              :                     }
     256              :                     // compare staff Y position rather than note Y position
     257              :                     // to be sure we do not reject earliest note
     258              :                     // just because it is lower in pitch than subsequent notes
     259            0 :                     qreal crY = 0.0;
     260            0 :                     if (cr->measure() && cr->measure()->system()) {
     261            0 :                         crY = cr->measure()->system()->staffYpage(cr->staffIdx());
     262              :                     } else {
     263            0 :                         continue;
     264              :                     }
     265            0 :                     if (topLeftChordRest) {
     266            0 :                         if (crTick <= tlTick && crY <= tlY) {
     267            0 :                             topLeftChordRest = cr;
     268            0 :                             tlTick = crTick;
     269            0 :                             tlY = crY;
     270              :                         }
     271              :                     } else {
     272            0 :                         topLeftChordRest = cr;
     273            0 :                         tlTick = crTick;
     274            0 :                         tlY = crY;
     275              :                     }
     276              :                 }
     277              :             }
     278            0 :         }
     279            0 :     }
     280              : 
     281            0 :     EngravingItem* el = topLeftChordRest ? topLeftChordRest : score()->selection().element();
     282            0 :     if (!el) {
     283            0 :         el = score()->selection().firstChordRest();
     284              :     }
     285              : 
     286            0 :     const InputState& is = score()->inputState();
     287              : 
     288            0 :     if (!el) {
     289            0 :         if (const Segment* segment = is.lastSegment()) {
     290            0 :             el = segment->element(is.track());
     291              :         }
     292              :     }
     293              : 
     294            0 :     if (!el || (!el->isChordRest() && !el->isNote())) {
     295              :         // if no note/rest is selected, start with voice 0
     296            0 :         engraving::track_idx_t track = is.track() == muse::nidx ? 0 : (is.track() / VOICES) * VOICES;
     297              :         // try to find an appropriate measure to start in
     298            0 :         Fraction tick = el ? el->tick() : Fraction(0, 1);
     299            0 :         el = score()->searchNote(tick, track);
     300            0 :         if (!el) {
     301            0 :             el = score()->searchNote(Fraction(0, 1), track);
     302              :         }
     303              :     }
     304              : 
     305            0 :     if (!el) {
     306            0 :         return nullptr;
     307              :     }
     308              : 
     309            0 :     if (el->isChord()) {
     310            0 :         Chord* c = toChord(el);
     311            0 :         Note* note = c->selectedNote();
     312            0 :         if (note == 0) {
     313            0 :             note = c->upNote();
     314              :         }
     315            0 :         el = note;
     316              :     }
     317              : 
     318            0 :     return el;
     319              : }
     320              : 
     321            0 : bool NotationNoteInput::shouldSetupInputNote() const
     322              : {
     323            0 :     if (!score()->inputState().isValid()) {
     324            0 :         return false;
     325              :     }
     326            0 :     return usingNoteInputMethod(NoteInputMethod::BY_DURATION)
     327            0 :            || usingNoteInputMethod(NoteInputMethod::RHYTHM);
     328              : }
     329              : 
     330            0 : void NotationNoteInput::setupInputNote()
     331              : {
     332            0 :     InputState& is = score()->inputState();
     333            0 :     const EngravingItem* selectedItem = score()->selection().element();
     334              : 
     335            0 :     if (selectedItem && selectedItem->isNote()) {
     336            0 :         is.setNotes({ toNote(selectedItem)->noteVal() });
     337            0 :         return;
     338              :     }
     339              : 
     340            0 :     Staff* staff = is.staff();
     341            0 :     IF_ASSERT_FAILED(staff) {
     342            0 :         return;
     343              :     }
     344              : 
     345            0 :     const Fraction tick = is.tick();
     346            0 :     NoteVal nval;
     347              : 
     348            0 :     if (staff->isTabStaff(tick)) {
     349            0 :         if (const StringData* stringData = staff->part()->stringData(tick, is.staffIdx())) {
     350            0 :             nval.fret = 0;
     351            0 :             nval.string = is.string();
     352            0 :             nval.pitch = stringData->getPitch(nval.string, nval.fret, staff);
     353              :         }
     354            0 :     } else if (staff->isDrumStaff(tick)) {
     355            0 :         if (const Drumset* drumset = is.drumset()) {
     356            0 :             nval.pitch = is.drumNote();
     357              : 
     358            0 :             if (nval.pitch < 0) {
     359            0 :                 nval.pitch = drumset->nextPitch(nval.pitch);
     360              :             }
     361              : 
     362            0 :             if (drumset->isValid(nval.pitch)) {
     363            0 :                 nval.headGroup = drumset->noteHead(nval.pitch);
     364              :             }
     365              :         }
     366              :     } else {
     367            0 :         nval = noteValForLine(staff->middleLine(tick));
     368              :     }
     369              : 
     370            0 :     if (nval.pitch > 0) {
     371            0 :         is.setNotes({ nval });
     372              :     }
     373              : }
     374              : 
     375            0 : NoteVal NotationNoteInput::noteValForLine(int line) const
     376              : {
     377            0 :     const InputState& is = score()->inputState();
     378              : 
     379            0 :     Position pos;
     380            0 :     pos.segment = is.segment();
     381            0 :     pos.staffIdx = is.staffIdx();
     382            0 :     pos.line = line;
     383              : 
     384            0 :     bool error = false;
     385            0 :     const NoteVal nval = NoteInput::noteValForPosition(score(), pos, is.accidentalType(), error);
     386            0 :     if (error) {
     387            0 :         LOGE() << "Could not find note val for position, staffIdx: " << pos.staffIdx << ", line: " << pos.line;
     388              :     }
     389              : 
     390            0 :     return nval;
     391              : }
     392              : 
     393            0 : void NotationNoteInput::endNoteInput(bool resetState)
     394              : {
     395            0 :     TRACEFUNC;
     396              : 
     397            0 :     if (!isNoteInputMode()) {
     398            0 :         return;
     399              :     }
     400              : 
     401            0 :     InputState& is = score()->inputState();
     402            0 :     is.setNoteEntryMode(false);
     403              : 
     404            0 :     if (is.slur()) {
     405            0 :         const std::vector<SpannerSegment*>& el = is.slur()->spannerSegments();
     406            0 :         if (!el.empty()) {
     407            0 :             el.front()->setSelected(false);
     408              :         }
     409            0 :         is.setSlur(0);
     410              :     }
     411              : 
     412            0 :     if (resetState) {
     413            0 :         is.setTrack(muse::nidx);
     414            0 :         is.setString(-1);
     415            0 :         is.setSegment(nullptr);
     416            0 :         is.setNotes({});
     417              :     }
     418              : 
     419            0 :     notifyAboutNoteInputEnded();
     420            0 :     updateInputState();
     421            0 :     accessibilityController()->announce(TranslatableString("noteInputMethod", "Normal mode").translated());
     422            0 : }
     423              : 
     424            0 : Channel</*focusNotation*/ bool> NotationNoteInput::noteInputStarted() const
     425              : {
     426            0 :     return m_noteInputStarted;
     427              : }
     428              : 
     429            0 : Notification NotationNoteInput::noteInputEnded() const
     430              : {
     431            0 :     return m_noteInputEnded;
     432              : }
     433              : 
     434            0 : bool NotationNoteInput::usingNoteInputMethod(NoteInputMethod method) const
     435              : {
     436            0 :     return score()->usingNoteEntryMethod(method);
     437              : }
     438              : 
     439            0 : void NotationNoteInput::setNoteInputMethod(NoteInputMethod method)
     440              : {
     441            0 :     TRACEFUNC;
     442              : 
     443            0 :     NoteInputState& is = score()->inputState();
     444            0 :     if (is.usingNoteEntryMethod(method)) {
     445            0 :         return;
     446              :     }
     447              : 
     448            0 :     if (!noteInputMethodAvailable(method, is.staff(), is.tick())) {
     449            0 :         return;
     450              :     }
     451              : 
     452            0 :     is.setNoteEntryMethod(method);
     453            0 :     if (shouldSetupInputNote()) {
     454            0 :         setupInputNote();
     455              :     }
     456              : 
     457            0 :     notifyAboutStateChanged();
     458            0 : }
     459              : 
     460            0 : Ret NotationNoteInput::canAddNote(NoteAddingMode addingMode) const
     461              : {
     462            0 :     const bool addToUpOnCurrentChord = addingMode == NoteAddingMode::CurrentChord;
     463            0 :     if (NoteInput::addPitchRemovesMeasureRepeat(score(), addToUpOnCurrentChord)) {
     464            0 :         return make_ret(notation::Err::NoteInputRemovesMeasureRepeat);
     465              :     }
     466              : 
     467            0 :     return make_ok();
     468              : }
     469              : 
     470            0 : void NotationNoteInput::addNote(const NoteInputParams& params, NoteAddingMode addingMode)
     471              : {
     472            0 :     TRACEFUNC;
     473              : 
     474            0 :     m_interaction->hideShadowNote();
     475              : 
     476            0 :     bool addToUpOnCurrentChord = addingMode == NoteAddingMode::CurrentChord;
     477            0 :     bool insertNewChord = addingMode == NoteAddingMode::InsertChord;
     478              : 
     479            0 :     if (addToUpOnCurrentChord) {
     480            0 :         startEdit(TranslatableString("undoableAction", "Add note to chord"));
     481            0 :     } else if (insertNewChord) {
     482            0 :         startEdit(TranslatableString("undoableAction", "Insert note"));
     483              :     } else {
     484            0 :         startEdit(TranslatableString("undoableAction", "Enter note"));
     485              :     }
     486            0 :     NoteInput::addPitch(score()->transactionManager()->currentOrDummyTransaction(), score(), params, addToUpOnCurrentChord,
     487              :                         insertNewChord);
     488              : 
     489            0 :     apply();
     490              : 
     491            0 :     if (shouldSetupInputNote()) {
     492            0 :         setupInputNote();
     493              :     }
     494              : 
     495            0 :     notifyNoteAddedChanged();
     496            0 :     notifyAboutStateChanged();
     497              : 
     498            0 :     m_interaction->checkAndShowError();
     499            0 :     m_interaction->showItem(state().cr());
     500            0 : }
     501              : 
     502            0 : void NotationNoteInput::applyNoteValueChange(const muse::TranslatableString& actionName,
     503              :                                              const std::function<void(mu::engraving::Transaction&)>& change)
     504              : {
     505            0 :     TRACEFUNC;
     506              : 
     507            0 :     m_interaction->hideShadowNote();
     508              : 
     509            0 :     startEdit(actionName);
     510            0 :     change(score()->transactionManager()->currentOrDummyTransaction());
     511            0 :     apply();
     512              : 
     513            0 :     notifyAboutStateChanged();
     514              : 
     515            0 :     m_interaction->checkAndShowError();
     516            0 : }
     517              : 
     518            0 : void NotationNoteInput::setDuration(DurationType duration)
     519              : {
     520            0 :     applyNoteValueChange(muse::TranslatableString("undoableAction", "Set duration"), [this, duration](mu::engraving::Transaction& tx) {
     521            0 :         NoteInput::setDuration(tx, score(), duration);
     522            0 :     });
     523            0 : }
     524              : 
     525            0 : void NotationNoteInput::toggleRest()
     526              : {
     527            0 :     applyNoteValueChange(muse::TranslatableString("undoableAction", "Toggle rest"), [this](mu::engraving::Transaction& tx) {
     528            0 :         NoteInput::toggleRest(tx, score());
     529            0 :     });
     530            0 : }
     531              : 
     532            0 : void NotationNoteInput::toggleDots(int dots)
     533              : {
     534            0 :     applyNoteValueChange(muse::TranslatableString("undoableAction", "Toggle dots"), [this, dots](mu::engraving::Transaction& tx) {
     535            0 :         NoteInput::toggleDots(tx, score(), dots);
     536            0 :     });
     537            0 : }
     538              : 
     539            0 : Ret NotationNoteInput::canPutNote(const PointF& pos) const
     540              : {
     541            0 :     if (NoteInput::putNoteRemovesMeasureRepeat(score(), pos)) {
     542            0 :         return make_ret(notation::Err::NoteInputRemovesMeasureRepeat);
     543              :     }
     544              : 
     545            0 :     return make_ok();
     546              : }
     547              : 
     548            0 : Ret NotationNoteInput::putNote(const PointF& pos, bool replace, bool insert)
     549              : {
     550            0 :     TRACEFUNC;
     551              : 
     552            0 :     startEdit(TranslatableString("undoableAction", "Enter note"));
     553            0 :     Ret ret = NoteInput::putNote(score()->transactionManager()->currentOrDummyTransaction(), score(), pos, replace, insert);
     554            0 :     apply();
     555              : 
     556            0 :     if (ret) {
     557            0 :         if (shouldSetupInputNote()) {
     558            0 :             setupInputNote();
     559              :         }
     560              :     }
     561              : 
     562            0 :     notifyNoteAddedChanged();
     563            0 :     notifyAboutStateChanged();
     564              : 
     565            0 :     m_interaction->checkAndShowError();
     566              : 
     567            0 :     return ret;
     568            0 : }
     569              : 
     570            0 : void NotationNoteInput::removeNote(const PointF& pos)
     571              : {
     572            0 :     TRACEFUNC;
     573              : 
     574            0 :     InputState& inputState = score()->inputState();
     575            0 :     bool restMode = inputState.rest();
     576              : 
     577            0 :     startEdit(TranslatableString("undoableAction", "Delete note"));
     578            0 :     inputState.setRest(!restMode);
     579            0 :     NoteInput::putNote(score()->transactionManager()->currentOrDummyTransaction(), score(), pos, false, false);
     580            0 :     inputState.setRest(restMode);
     581            0 :     apply();
     582              : 
     583            0 :     notifyAboutStateChanged();
     584              : 
     585            0 :     m_interaction->checkAndShowError();
     586            0 : }
     587              : 
     588            0 : void NotationNoteInput::setInputNote(const NoteInputParams& params)
     589              : {
     590            0 :     TRACEFUNC;
     591              : 
     592            0 :     NoteInputState& is = score()->inputState();
     593            0 :     IF_ASSERT_FAILED(is.isValid()) {
     594            0 :         return;
     595              :     }
     596            0 :     const Staff* staff = is.staff();
     597            0 :     const Fraction tick = is.tick();
     598              : 
     599            0 :     NoteVal nval;
     600              : 
     601            0 :     if (staff->isDrumStaff(tick)) {
     602            0 :         const Drumset* drumset = is.drumset();
     603            0 :         if (drumset && drumset->isValid(params.drumPitch)) {
     604            0 :             nval.pitch = params.drumPitch;
     605            0 :             nval.headGroup = drumset->noteHead(params.drumPitch);
     606              :         }
     607              :     } else {
     608            0 :         nval = noteValForLine(relStep(params.step, staff->clef(tick)));
     609              :     }
     610              : 
     611            0 :     if (nval.pitch > 0) {
     612            0 :         setInputNotes({ nval });
     613              :     }
     614            0 : }
     615              : 
     616            0 : void NotationNoteInput::setInputNotes(const NoteValList& notes)
     617              : {
     618            0 :     TRACEFUNC;
     619              : 
     620            0 :     NoteInputState& is = score()->inputState();
     621              : 
     622            0 :     if (is.notes() == notes) {
     623            0 :         return;
     624              :     }
     625              : 
     626            0 :     if (!notes.empty()) {
     627            0 :         if (const Drumset* drumset = is.drumset()) {
     628            0 :             const int pitch = notes.front().pitch;
     629            0 :             is.setDrumNote(pitch);
     630            0 :             is.setVoice(drumset->voice(pitch));
     631              :         }
     632              :     }
     633              : 
     634            0 :     is.setNotes(notes);
     635            0 :     notifyAboutStateChanged();
     636            0 : }
     637              : 
     638            0 : void NotationNoteInput::moveInputNotes(bool up, PitchMode mode)
     639              : {
     640            0 :     TRACEFUNC;
     641              : 
     642            0 :     InputState& is = score()->inputState();
     643            0 :     IF_ASSERT_FAILED(is.isValid()) {
     644            0 :         return;
     645              :     }
     646              : 
     647            0 :     is.setAccidentalType(AccidentalType::NONE);
     648              : 
     649            0 :     const Staff* staff = is.staff();
     650            0 :     const Fraction tick = is.tick();
     651              : 
     652            0 :     NoteValList notes;
     653              : 
     654            0 :     for (const NoteVal& val : is.notes()) {
     655            0 :         NoteVal newVal;
     656              : 
     657            0 :         if (staff->isDrumStaff(tick)) {
     658            0 :             if (const Drumset* drumset = is.drumset()) {
     659            0 :                 newVal.pitch = up ? drumset->nextPitch(val.pitch) : drumset->prevPitch(val.pitch);
     660              : 
     661            0 :                 if (drumset->isValid(newVal.pitch)) {
     662            0 :                     newVal.headGroup = drumset->noteHead(newVal.pitch);
     663            0 :                     notes.push_back(newVal);
     664              :                 }
     665              :             }
     666            0 :             continue;
     667            0 :         }
     668              : 
     669            0 :         switch (mode) {
     670            0 :         case PitchMode::CHROMATIC:
     671            0 :             newVal.pitch = val.pitch + (up ? 1 : -1);
     672            0 :             break;
     673            0 :         case PitchMode::DIATONIC: {
     674            0 :             const int oldLine = noteValToLine(val, staff, tick);
     675            0 :             const int newLine = oldLine + (up ? -1 : 1);
     676            0 :             newVal = noteValForLine(newLine);
     677            0 :         } break;
     678            0 :         case PitchMode::OCTAVE:
     679            0 :             newVal = val;
     680            0 :             newVal.pitch += up ? PITCH_DELTA_OCTAVE : -PITCH_DELTA_OCTAVE;
     681            0 :             break;
     682              :         }
     683              : 
     684            0 :         newVal.pitch = clampPitch(newVal.pitch);
     685            0 :         notes.push_back(newVal);
     686              :     }
     687              : 
     688            0 :     setInputNotes(notes);
     689            0 : }
     690              : 
     691            0 : void NotationNoteInput::setRestMode(bool rest)
     692              : {
     693            0 :     InputState& is = score()->inputState();
     694            0 :     if (is.rest() == rest) {
     695            0 :         return;
     696              :     }
     697              : 
     698            0 :     is.setRest(rest);
     699            0 :     notifyAboutStateChanged();
     700              : }
     701              : 
     702            0 : void NotationNoteInput::setAccidental(AccidentalType accidentalType)
     703              : {
     704            0 :     TRACEFUNC;
     705              : 
     706            0 :     EditNote::toggleAccidental(score(), accidentalType);
     707              : 
     708            0 :     notifyAboutStateChanged();
     709              : 
     710            0 :     m_interaction->checkAndShowError();
     711            0 : }
     712              : 
     713            0 : void NotationNoteInput::setArticulation(SymbolId articulationSymbolId)
     714              : {
     715            0 :     TRACEFUNC;
     716              : 
     717            0 :     InputState& inputState = score()->inputState();
     718              : 
     719              :     std::set<SymbolId> articulations = updateArticulations(
     720            0 :         inputState.articulationIds(), articulationSymbolId, ArticulationsUpdateMode::Remove);
     721            0 :     inputState.setArticulationIds(articulations);
     722              : 
     723            0 :     notifyAboutStateChanged();
     724              : 
     725            0 :     m_interaction->checkAndShowError();
     726            0 : }
     727              : 
     728            0 : void NotationNoteInput::setDrumNote(int note)
     729              : {
     730            0 :     TRACEFUNC;
     731              : 
     732            0 :     InputState& is = score()->inputState();
     733            0 :     if (is.drumNote() == note) {
     734            0 :         return;
     735              :     }
     736              : 
     737            0 :     is.setDrumNote(note);
     738            0 :     if (shouldSetupInputNote()) {
     739            0 :         setupInputNote();
     740              :     }
     741              : 
     742            0 :     notifyAboutStateChanged();
     743            0 : }
     744              : 
     745            0 : void NotationNoteInput::setCurrentVoice(voice_idx_t voiceIndex)
     746              : {
     747            0 :     TRACEFUNC;
     748              : 
     749            0 :     InputState& inputState = score()->inputState();
     750            0 :     if (!isVoiceIndexValid(voiceIndex) || voiceIndex == inputState.voice()) {
     751            0 :         return;
     752              :     }
     753              : 
     754            0 :     inputState.setVoice(voiceIndex);
     755            0 :     notifyAboutStateChanged();
     756            0 : }
     757              : 
     758            0 : void NotationNoteInput::setCurrentTrack(track_idx_t trackIndex)
     759              : {
     760            0 :     TRACEFUNC;
     761              : 
     762            0 :     score()->inputState().setTrack(trackIndex);
     763            0 :     notifyAboutStateChanged();
     764            0 : }
     765              : 
     766            0 : void NotationNoteInput::addTuplet(const TupletOptions& options)
     767              : {
     768            0 :     TRACEFUNC;
     769              : 
     770            0 :     const InputState& inputState = score()->inputState();
     771              : 
     772            0 :     const bool durationAsSubdivision = options.selectedDurationAs == TupletSelectedDuration::SUBDIVISION;
     773            0 :     if (durationAsSubdivision
     774            0 :         && !score()->canAddTuplet(inputState.segment(), inputState.track(), options.ratio, inputState.duration())) {
     775            0 :         MScore::setError(MsError::NO_SPACE_FOR_TUPLET);
     776            0 :         m_interaction->checkAndShowError();
     777            0 :         return;
     778              :     }
     779              : 
     780            0 :     if (!durationAsSubdivision && !m_interaction->canAddTupletToSelectedChordRests()) {
     781            0 :         MScore::setError(MsError::TUPLET_NOTE_VALUE_TOO_SHORT);
     782            0 :         m_interaction->checkAndShowError();
     783            0 :         return;
     784              :     }
     785              : 
     786            0 :     startEdit(TranslatableString("undoableAction", "Add tuplet"));
     787            0 :     score()->expandVoice();
     788            0 :     ChordRest* chordRest = inputState.cr();
     789            0 :     if (chordRest) {
     790            0 :         Tuplet* tuplet = nullptr;
     791            0 :         if (durationAsSubdivision) {
     792            0 :             tuplet = score()->addTuplet(chordRest, options.ratio, inputState.duration(), options.numberType, options.bracketType);
     793              :         } else {
     794            0 :             Fraction ratio = options.ratio;
     795            0 :             if (options.autoBaseLen) {
     796            0 :                 ratio.setDenominator(Tuplet::computeTupletDenominator(ratio.numerator(), inputState.ticks()));
     797              :             }
     798            0 :             score()->changeCRlen(chordRest, inputState.duration());
     799            0 :             tuplet = score()->addTuplet(chordRest, ratio, options.numberType, options.bracketType);
     800              :         }
     801            0 :         if (tuplet) {
     802            0 :             score()->selectNewTuplet(tuplet);
     803              :         }
     804              :     }
     805            0 :     apply();
     806              : 
     807            0 :     notifyAboutStateChanged();
     808              : 
     809            0 :     m_interaction->checkAndShowError();
     810            0 : }
     811              : 
     812            0 : static muse::RectF segmentContentRect(const Segment* segment, track_idx_t track)
     813              : {
     814            0 :     RectF result;
     815              : 
     816            0 :     const EngravingItem* el = segment->element(track);
     817            0 :     if (!el) {
     818            0 :         return result;
     819              :     }
     820              : 
     821            0 :     if (el->isChord()) {
     822            0 :         const Chord* chord = toChord(el);
     823            0 :         for (const Note* note: chord->notes()) {
     824            0 :             result.unite(note->ldata()->bbox().translated(note->pos()));
     825              :         }
     826              : 
     827            0 :         if (Hook* hook = chord->hook()) {
     828            0 :             result.unite(hook->ldata()->bbox().translated(hook->pos()));
     829              :         }
     830            0 :     } else if (el->isRest() && !toRest(el)->isFullMeasureRest()) {
     831            0 :         result.unite(el->ldata()->bbox());
     832              :     }
     833              : 
     834            0 :     return result;
     835              : }
     836              : 
     837            0 : muse::RectF NotationNoteInput::cursorRect() const
     838              : {
     839            0 :     TRACEFUNC;
     840              : 
     841            0 :     if (!isNoteInputMode()) {
     842            0 :         return {};
     843              :     }
     844              : 
     845            0 :     const InputState& inputState = score()->inputState();
     846            0 :     const Segment* segment = inputState.segment();
     847            0 :     if (!segment) {
     848            0 :         return {};
     849              :     }
     850              : 
     851            0 :     const System* system = segment->measure()->system();
     852            0 :     if (!system) {
     853            0 :         return {};
     854              :     }
     855              : 
     856            0 :     const track_idx_t track = inputState.track() == muse::nidx ? 0 : inputState.track();
     857            0 :     const staff_idx_t staffIdx = track2staff(track);
     858              : 
     859            0 :     const Staff* staff = score()->staff(staffIdx);
     860            0 :     if (!staff) {
     861            0 :         return {};
     862              :     }
     863              : 
     864            0 :     const double globalSpatium = score()->style().spatium();
     865              : 
     866            0 :     const StaffType* staffType = staff->staffType(inputState.tick());
     867            0 :     const bool isTabStaff = staffType->isTabStaff();
     868            0 :     const double localSpatium = staffType->spatium();
     869              : 
     870            0 :     const double defaultWidth = score()->noteHeadWidth() * localSpatium / globalSpatium;
     871              : 
     872            0 :     const RectF segmentContentRect = ::segmentContentRect(segment, track);
     873            0 :     double x = segmentContentRect.x() + segment->pagePos().x();
     874              : 
     875            0 :     const double barNoteDist = score()->style().styleAbsolute(Sid::barNoteDistance);
     876            0 :     if (segment->x() < barNoteDist) {
     877            0 :         x += barNoteDist - segment->x();
     878              :     }
     879              : 
     880            0 :     double y = system->staffCanvasYpage(staffIdx);
     881            0 :     double w = segmentContentRect.width() > 0 ? segmentContentRect.width() : defaultWidth;
     882            0 :     double h = 0.0;
     883              : 
     884            0 :     if (inputState.beyondScore()) {
     885            0 :         const Measure* lastMeasure = score()->lastMeasure();
     886            0 :         x = lastMeasure->pageBoundingRect().right() + globalSpatium;
     887            0 :         w = defaultWidth;
     888              :     }
     889              : 
     890            0 :     double sideMargin = 0.0;
     891              : 
     892            0 :     if (isTabStaff) {
     893            0 :         const double minWidth = 2.0 * globalSpatium;
     894            0 :         sideMargin = std::max(0.3 * globalSpatium, 0.5 * (minWidth - w));
     895              :     } else {
     896            0 :         sideMargin = 0.5 * globalSpatium;
     897              :     }
     898              : 
     899              :     // Don't extend further to the left than the center between the current and previous segment
     900            0 :     const engraving::Segment* prevSeg = segment->prev1WithElemsOnTrack(track, engraving::SegmentType::ChordRest);
     901            0 :     if (prevSeg && prevSeg->measure() == segment->measure()) {
     902            0 :         const RectF prevSegContentRect = ::segmentContentRect(prevSeg, track);
     903            0 :         if (prevSegContentRect.width() > 0) {
     904            0 :             const double centerBetweenPrevSegRightAndCurrSegLeft = (prevSeg->pagePos().x() + prevSegContentRect.right() + x) * 0.5;
     905            0 :             sideMargin = std::min(sideMargin, x - centerBetweenPrevSegRightAndCurrSegLeft);
     906              :         }
     907              :     }
     908              : 
     909            0 :     x -= sideMargin;
     910            0 :     w += 2 * sideMargin;
     911              : 
     912            0 :     const double yOffset = staffType->yoffset().val() * localSpatium;
     913            0 :     y += yOffset;
     914              : 
     915            0 :     const int inputStateStringsCount = inputState.string();
     916            0 :     const int instrumentStringsCount = static_cast<int>(staff->part()->instrument()->stringData()->strings());
     917              : 
     918            0 :     const double lineDist = staffType->lineDistance().val() * localSpatium;
     919              : 
     920            0 :     if (isTabStaff && inputStateStringsCount >= 0 && inputStateStringsCount <= instrumentStringsCount) {
     921            0 :         h = lineDist;
     922            0 :         y += staffType->physStringToYOffset(inputStateStringsCount).toAbsolute(localSpatium);
     923            0 :         y -= (staffType->onLines() ? lineDist * 0.5 : lineDist);
     924              :     } else {
     925            0 :         const double skylineMargin = 0.75 * localSpatium;
     926            0 :         const int lines = staffType->lines();
     927              : 
     928            0 :         h = (lines - 1) * lineDist + 2 * skylineMargin;
     929            0 :         y -= skylineMargin;
     930              :     }
     931              : 
     932            0 :     RectF result { x, y, w, h };
     933              : 
     934            0 :     if (configuration()->canvasOrientation().val == muse::Orientation::Horizontal) {
     935            0 :         result.translate(system->page()->pos());
     936              :     }
     937              : 
     938            0 :     return result;
     939            0 : }
     940              : 
     941            0 : void NotationNoteInput::addSlur(Slur* slur)
     942              : {
     943            0 :     TRACEFUNC;
     944              : 
     945            0 :     InputState& inputState = score()->inputState();
     946            0 :     inputState.setSlur(slur);
     947              : 
     948            0 :     if (slur) {
     949            0 :         std::vector<SpannerSegment*> slurSpannerSegments = slur->spannerSegments();
     950            0 :         if (!slurSpannerSegments.empty()) {
     951            0 :             slurSpannerSegments.front()->setSelected(true);
     952              :         }
     953            0 :     }
     954              : 
     955            0 :     notifyAboutStateChanged();
     956            0 : }
     957              : 
     958            0 : void NotationNoteInput::resetSlur()
     959              : {
     960            0 :     TRACEFUNC;
     961              : 
     962            0 :     InputState& inputState = score()->inputState();
     963            0 :     Slur* slur = inputState.slur();
     964            0 :     if (!slur) {
     965            0 :         return;
     966              :     }
     967              : 
     968            0 :     score()->deselect(slur);
     969              : 
     970            0 :     addSlur(nullptr);
     971            0 : }
     972              : 
     973            0 : void NotationNoteInput::addTie()
     974              : {
     975            0 :     TRACEFUNC;
     976              : 
     977              :     // Calls `startEdit` internally
     978            0 :     EditTie::cmdAddTie(score());
     979              : 
     980            0 :     notifyAboutStateChanged();
     981            0 :     m_interaction->checkAndShowError();
     982            0 : }
     983              : 
     984            0 : void NotationNoteInput::addLaissezVib()
     985              : {
     986            0 :     TRACEFUNC;
     987              : 
     988              :     // Calls `startEdit` internally
     989            0 :     EditTie::cmdToggleLaissezVib(score());
     990              : 
     991            0 :     notifyAboutStateChanged();
     992            0 :     m_interaction->checkAndShowError();
     993            0 : }
     994              : 
     995            0 : Notification NotationNoteInput::noteAdded() const
     996              : {
     997            0 :     return m_noteAdded;
     998              : }
     999              : 
    1000            0 : Notification NotationNoteInput::stateChanged() const
    1001              : {
    1002            0 :     return m_stateChanged;
    1003              : }
    1004              : 
    1005            0 : void NotationNoteInput::setGetViewRectFunc(const std::function<RectF()>& func)
    1006              : {
    1007            0 :     m_getViewRectFunc = func;
    1008            0 : }
    1009              : 
    1010            0 : Score* NotationNoteInput::score() const
    1011              : {
    1012            0 :     return m_getScore->score();
    1013              : }
    1014              : 
    1015            0 : void NotationNoteInput::startEdit(const muse::TranslatableString& actionName)
    1016              : {
    1017            0 :     m_undoStack->prepareChanges(actionName);
    1018            0 : }
    1019              : 
    1020            0 : void NotationNoteInput::apply()
    1021              : {
    1022            0 :     m_undoStack->commitChanges();
    1023              : 
    1024            0 :     if (ChordRest* chordRest = score()->inputState().cr()) {
    1025            0 :         m_interaction->showItem(chordRest);
    1026              :     }
    1027            0 : }
    1028              : 
    1029            0 : void NotationNoteInput::updateInputState()
    1030              : {
    1031            0 :     TRACEFUNC;
    1032              : 
    1033            0 :     NoteInputState& is = score()->inputState();
    1034            0 :     is.update(score()->selection());
    1035              : 
    1036            0 :     if (!configuration()->addAccidentalDotsArticulationsToNextNoteEntered()) {
    1037            0 :         is.setAccidentalType(AccidentalType::NONE);
    1038            0 :         is.setDots(0);
    1039            0 :         is.setArticulationIds({});
    1040              :     }
    1041              : 
    1042            0 :     notifyAboutStateChanged();
    1043            0 : }
    1044              : 
    1045            0 : void NotationNoteInput::notifyAboutStateChanged()
    1046              : {
    1047            0 :     m_stateChanged.notify();
    1048            0 : }
    1049              : 
    1050            0 : void NotationNoteInput::notifyNoteAddedChanged()
    1051              : {
    1052            0 :     m_noteAdded.notify();
    1053            0 : }
    1054              : 
    1055            0 : void NotationNoteInput::notifyAboutNoteInputStarted(bool focusNotation)
    1056              : {
    1057            0 :     m_noteInputStarted.send(focusNotation);
    1058            0 : }
    1059              : 
    1060            0 : void NotationNoteInput::notifyAboutNoteInputEnded()
    1061              : {
    1062            0 :     m_noteInputEnded.notify();
    1063            0 : }
    1064              : 
    1065            0 : void NotationNoteInput::doubleNoteInputDuration()
    1066              : {
    1067            0 :     TRACEFUNC;
    1068              : 
    1069            0 :     startEdit(TranslatableString("undoableAction", "Double note input duration"));
    1070            0 :     NoteInput::increaseDuration(score()->transactionManager()->currentOrDummyTransaction(), score());
    1071            0 :     apply();
    1072              : 
    1073            0 :     notifyAboutStateChanged();
    1074            0 :     m_interaction->checkAndShowError();
    1075            0 : }
    1076              : 
    1077            0 : void NotationNoteInput::halveNoteInputDuration()
    1078              : {
    1079            0 :     TRACEFUNC;
    1080              : 
    1081            0 :     startEdit(TranslatableString("undoableAction", "Halve note input duration"));
    1082            0 :     NoteInput::decreaseDuration(score()->transactionManager()->currentOrDummyTransaction(), score());
    1083            0 :     apply();
    1084              : 
    1085            0 :     notifyAboutStateChanged();
    1086            0 :     m_interaction->checkAndShowError();
    1087            0 : }
        

Generated by: LCOV version 2.0-1