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 : #ifndef MU_ENGRAVING_APIV1_PLAYEVENT_H
24 : #define MU_ENGRAVING_APIV1_PLAYEVENT_H
25 :
26 : #include <QQmlEngine>
27 : #include <QQmlListProperty>
28 :
29 : #include "engraving/dom/noteevent.h"
30 :
31 : namespace mu::engraving::apiv1 {
32 : class Note;
33 :
34 : //---------------------------------------------------------
35 : // PlayEvent
36 : // Wrapper class for internal mu::engraving::NoteEvent
37 : //
38 : // This is based on the wrapper in excerpt.h.
39 : /// \since MuseScore 3.3
40 : //---------------------------------------------------------
41 :
42 : class PlayEvent : public QObject
43 : {
44 : Q_OBJECT
45 : /// The relative pitch to the note pitch.
46 : /// This is added to the parent note's actual pitch.
47 : /// \since MuseScore 3.3
48 : Q_PROPERTY(int pitch READ pitch WRITE setPitch)
49 : /// Time to turn on the note event.
50 : /// This value is expressed in 1/1000 of the nominal note length.
51 : /// \since MuseScore 3.3
52 : Q_PROPERTY(int ontime READ ontime WRITE setOntime)
53 : /// The length of time for the event.
54 : /// This value is expressed in 1/1000 of the nominal note length.
55 : /// \since MuseScore 3.3
56 : Q_PROPERTY(int len READ len WRITE setLen)
57 : /// Time note will turn off.
58 : /// This value derived from onTime and len. This value is expressed
59 : /// in 1/1000 of the nominal note length.
60 : /// \since MuseScore 3.3
61 : Q_PROPERTY(int offtime READ offtime)
62 : /// \cond MS_INTERNAL
63 :
64 : protected:
65 : mu::engraving::NoteEvent* ne;
66 : Note* parentNote;
67 :
68 : public:
69 :
70 0 : PlayEvent(mu::engraving::NoteEvent* m_ne = new mu::engraving::NoteEvent(), Note* m_parent = nullptr)
71 0 : : QObject(), ne(m_ne), parentNote(m_parent) {}
72 : // Delete the NoteEvent if parentless.
73 0 : virtual ~PlayEvent()
74 0 : {
75 0 : if (parentNote == nullptr) {
76 0 : delete ne;
77 : }
78 0 : }
79 :
80 0 : const mu::engraving::NoteEvent& getNoteEvent() { return *ne; }
81 : void setParentNote(Note* parent) { this->parentNote = parent; }
82 : Note* note() { return parentNote; }
83 :
84 0 : int pitch() const { return ne->pitch(); }
85 0 : int ontime() const { return ne->ontime(); }
86 0 : int offtime() const { return ne->offtime(); }
87 0 : int len() const { return ne->len(); }
88 : void setPitch(int v);
89 : void setOntime(int v);
90 : void setLen(int v);
91 : /// \endcond
92 : };
93 :
94 : //---------------------------------------------------------
95 : // wrap
96 : /// \cond PLUGIN_API \private \endcond
97 : /// \relates mu::engraving::NoteEvent
98 : //---------------------------------------------------------
99 :
100 0 : inline PlayEvent* playEventWrap(mu::engraving::NoteEvent* t, Note* parent)
101 : {
102 0 : PlayEvent* w = t ? new PlayEvent(t, parent) : nullptr;
103 : // All wrapper objects should belong to JavaScript code.
104 0 : QQmlEngine::setObjectOwnership(w, QQmlEngine::JavaScriptOwnership);
105 0 : return w;
106 : }
107 :
108 : //---------------------------------------------------------
109 : // QML access to containers of NoteEvent
110 : //
111 : // QmlNoteEventsListAccess provides a convenience interface
112 : // for QQmlListProperty providing access to plugins for NoteEvent
113 : // Containers.
114 : //
115 : // Based on QmlListAccess in excerpt.h
116 : //---------------------------------------------------------
117 :
118 : class QmlPlayEventsListAccess : public QQmlListProperty<PlayEvent>
119 : {
120 : public:
121 0 : QmlPlayEventsListAccess(QObject* obj, engraving::NoteEventList& container)
122 0 : : QQmlListProperty<PlayEvent>(obj, &container, &append, &count, &at, &clear) {}
123 :
124 0 : static qsizetype count(QQmlListProperty<PlayEvent>* l) { return static_cast<engraving::NoteEventList*>(l->data)->size(); }
125 0 : static PlayEvent* at(QQmlListProperty<PlayEvent>* l, qsizetype i)
126 : {
127 0 : return playEventWrap(&(*(static_cast<engraving::NoteEventList*>(l->data)))[i], reinterpret_cast<Note*>(l->object));
128 : }
129 :
130 : static void clear(QQmlListProperty<PlayEvent>* l);
131 : static void append(QQmlListProperty<PlayEvent>* l, PlayEvent* v);
132 : };
133 :
134 : /// \cond PLUGIN_API \private \endcond
135 0 : inline QmlPlayEventsListAccess wrapPlayEventsContainerProperty(QObject* obj, engraving::NoteEventList& c)
136 : {
137 0 : return QmlPlayEventsListAccess(obj, c);
138 : }
139 : }
140 :
141 : #endif
|