LCOV - code coverage report
Current view: top level - src/engraving/api/v1 - cursor.h Coverage Total Hit
Test: filtered_coverage.info Lines: 16.7 % 6 1
Test Date: 2026-11-05 04:07:02

            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              : #pragma once
      23              : 
      24              : #include "apistructs.h"
      25              : 
      26              : #include "engraving/dom/input.h"
      27              : 
      28              : Q_MOC_INCLUDE("engraving/api/v1/elements.h")
      29              : Q_MOC_INCLUDE("engraving/api/v1/score.h")
      30              : 
      31              : namespace mu::engraving {
      32              : class EngravingItem;
      33              : class InputState;
      34              : class Score;
      35              : class Chord;
      36              : class Rest;
      37              : class Note;
      38              : class Segment;
      39              : class ChordRest;
      40              : class StaffText;
      41              : class Measure;
      42              : 
      43              : enum class SegmentType;
      44              : }
      45              : 
      46              : namespace mu::engraving::apiv1 {
      47              : class EngravingItem;
      48              : class Measure;
      49              : class Segment;
      50              : class Score;
      51              : class Staff;
      52              : 
      53              : //---------------------------------------------------------
      54              : //   @@ Cursor
      55              : ///   Cursor can be used by plugins to manipulate the score.
      56              : ///   Cursor object for a score can be obtained with
      57              : ///   \ref Score.newCursor method. After creating a cursor
      58              : ///   it does not point to any location in a score. To define
      59              : ///   its initial location use \ref rewind or \ref rewindToTick
      60              : ///   methods. Alternatively, you can set its
      61              : ///   \ref inputStateMode to \ref INPUT_STATE_SYNC_WITH_SCORE "Cursor.INPUT_STATE_SYNC_WITH_SCORE"
      62              : ///   to make cursor location be synchronized with
      63              : ///   user-visible note input state.
      64              : //---------------------------------------------------------
      65              : 
      66              : class Cursor : public QObject
      67              : {
      68              :     Q_OBJECT
      69              :     /// Current track
      70              :     Q_PROPERTY(int track READ track WRITE setTrack)
      71              :     /// Current staff number (#track / 4)
      72              :     Q_PROPERTY(int staffIdx READ staffIdx WRITE setStaffIdx)
      73              :     /// Current \ref Staff the cursor is on.
      74              :     /// \since MuseScore 4.6
      75              :     Q_PROPERTY(apiv1::Staff * staff READ staff WRITE setStaff)
      76              :     /// Current voice (#track % 4)
      77              :     Q_PROPERTY(int voice READ voice WRITE setVoice)
      78              :     /// Segment type filter, a bitmask from
      79              :     /// PluginAPI::PluginAPI::Segment values.
      80              :     /// Determines which segments this cursor will move to
      81              :     /// on next() and nextMeasure() operations. The default
      82              :     /// value is mu::engraving::SegmentType::ChordRest so only segments
      83              :     /// containing chords and rests are handled by default.
      84              :     Q_PROPERTY(int filter READ filter WRITE setFilter)
      85              : 
      86              :     /// MIDI tick position, read only
      87              :     /// As of MuseScore 4.6, this property is deprecated and should
      88              :     /// not be used to read the cursor position. Use \ref cursor.fraction
      89              :     /// instead. However, this property can still sometimes be useful to
      90              :     /// compare position values using operators.
      91              :     Q_PROPERTY(int tick READ tick)
      92              :     /// MIDI tick position, accounting for repeats.
      93              :     /// \since MuseScore 4.6
      94              :     Q_PROPERTY(int utick READ utick)
      95              :     /// Time position in this score, measured as a fraction of a
      96              :     /// whole note (the same units MuseScore's code uses internally).
      97              :     /// \since MuseScore 4.6
      98              :     Q_PROPERTY(apiv1::Fraction * fraction READ qmlFraction)
      99              : 
     100              :     /// Tempo at current tick, read only
     101              :     Q_PROPERTY(qreal tempo READ tempo)
     102              : 
     103              :     /// Key signature of current staff at tick pos. (read only)
     104              :     Q_PROPERTY(int keySignature READ qmlKeySignature)
     105              :     /// Associated score
     106              :     Q_PROPERTY(apiv1::Score * score READ score WRITE setScore)
     107              : 
     108              :     /// Current element at track, read only
     109              :     Q_PROPERTY(apiv1::EngravingItem * element READ element) //todo set
     110              :     /// Current segment, read only
     111              :     Q_PROPERTY(apiv1::Segment * segment READ qmlSegment)
     112              :     /// Current measure, read only
     113              :     Q_PROPERTY(apiv1::Measure * measure READ measure)
     114              :     /// A physical string number where this cursor currently at. This is useful
     115              :     /// in conjunction with \ref InputStateMode.INPUT_STATE_SYNC_WITH_SCORE
     116              :     /// cursor mode.
     117              :     /// \since MuseScore 3.5
     118              :     Q_PROPERTY(int stringNumber READ inputStateString WRITE setInputStateString)
     119              : 
     120              : public:
     121              :     enum RewindMode {
     122              :         SCORE_START = 0,     ///< Rewind to the start of a score
     123              :         SELECTION_START = 1,     ///< Rewind to the start of a selection
     124              :         SELECTION_END = 2     ///< Rewind to the end of a selection
     125              :     };
     126            0 :     Q_ENUM(RewindMode);
     127              : 
     128              :     /// \since MuseScore 3.5
     129              :     enum InputStateMode {
     130              :         INPUT_STATE_INDEPENDENT,     ///< Input state of cursor is independent of score input state (default)
     131              :         INPUT_STATE_SYNC_WITH_SCORE     ///< Input state of cursor is synchronized with score input state
     132              :     };
     133            0 :     Q_ENUM(InputStateMode);
     134              : 
     135              : private:
     136              :     /// Behavior of input state (position, notes duration etc.) of this cursor
     137              :     /// with respect to input state of the score. By default any changes in
     138              :     /// score and in this Cursor are not synchronized.
     139              :     /// \since MuseScore 3.5
     140              :     Q_PROPERTY(InputStateMode inputStateMode READ inputStateMode WRITE setInputStateMode)
     141              : 
     142              :     mu::engraving::Score* m_score = nullptr;
     143              : //       bool m_expandRepeats; // used?
     144              :     engraving::SegmentType m_filter;
     145              :     std::unique_ptr<engraving::InputState> is;
     146              :     InputStateMode m_inputStateMode = INPUT_STATE_INDEPENDENT;
     147              : 
     148              :     // utility methods
     149              :     void prevInTrack();
     150              :     void nextInTrack();
     151              :     void setScore(mu::engraving::Score* s);
     152              :     mu::engraving::EngravingItem* currentElement() const;
     153              : 
     154              :     engraving::InputState& inputState();
     155           83 :     const engraving::InputState& inputState() const { return const_cast<Cursor*>(this)->inputState(); }
     156              : 
     157              :     mu::engraving::Segment* segment() const;
     158              :     void setSegment(mu::engraving::Segment* seg);
     159              : 
     160              :     mu::engraving::Fraction fraction() const;
     161              : 
     162              :     int inputStateString() const;
     163              :     void setInputStateString(int);
     164              : 
     165              : public:
     166              :     /// \cond MS_INTERNAL
     167              :     Cursor(mu::engraving::Score* s = nullptr);
     168              : //       Cursor(Score*, bool); // not implemented? what is bool?
     169              : 
     170              :     Score* score() const;
     171              :     void setScore(Score* s);
     172              : 
     173              :     int track() const;
     174              :     void setTrack(int v);
     175              : 
     176              :     int staffIdx() const;
     177              :     void setStaffIdx(int v);
     178              : 
     179              :     Staff* staff() const;
     180              :     void setStaff(Staff* s);
     181              : 
     182              :     int voice() const;
     183              :     void setVoice(int v);
     184              : 
     185            0 :     int filter() const { return int(m_filter); }
     186            0 :     void setFilter(int f) { m_filter = engraving::SegmentType(f); }
     187              : 
     188            0 :     InputStateMode inputStateMode() const { return m_inputStateMode; }
     189              :     void setInputStateMode(InputStateMode val);
     190              : 
     191              :     EngravingItem* element() const;
     192              :     Segment* qmlSegment() const;
     193              :     Measure* measure() const;
     194              : 
     195              :     int tick();
     196              :     int utick();
     197              :     Fraction* qmlFraction() const;
     198              :     qreal tempo();
     199              : 
     200              :     int qmlKeySignature();
     201              :     /// \endcond
     202              : 
     203              :     Q_INVOKABLE void rewind(RewindMode mode);
     204              :     Q_INVOKABLE void rewindToTick(int tick);
     205              :     Q_INVOKABLE void rewindToFraction(apiv1::Fraction* f);
     206              :     Q_INVOKABLE double time(bool includeRepeats = false);
     207              : 
     208              :     Q_INVOKABLE bool next();
     209              :     Q_INVOKABLE bool nextMeasure();
     210              :     Q_INVOKABLE bool prev();
     211              :     Q_INVOKABLE void add(apiv1::EngravingItem*);
     212              : 
     213              :     Q_INVOKABLE void addNote(int pitch, bool addToChord = false);
     214              :     Q_INVOKABLE void addRest();
     215              :     Q_INVOKABLE void addTuplet(apiv1::Fraction* ratio, apiv1::Fraction* duration);
     216              : 
     217              :     /// set duration
     218              :     ///   z: numerator
     219              :     ///   n: denominator
     220              :     ///   Quarter, if n == 0
     221              :     Q_INVOKABLE void setDuration(int z, int n);
     222              : };
     223              : }
        

Generated by: LCOV version 2.0-1