MuseScore Plugins 3.3
Plugins API for MuseScore
Loading...
Searching...
No Matches
scoreelement.h
1//=============================================================================
2// MuseScore
3// Music Composition & Notation
4//
5// Copyright (C) 2012 Werner Schweer
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License version 2
9// as published by the Free Software Foundation and appearing in
10// the file LICENCE.GPL
11//=============================================================================
12
13#ifndef __PLUGIN_API_SCOREELEMENT_H__
14#define __PLUGIN_API_SCOREELEMENT_H__
15
16#include "libmscore/property.h"
17
18namespace Ms {
19
20class ScoreElement;
21
22namespace PluginAPI {
23
24//---------------------------------------------------------
25// Ownership
30//---------------------------------------------------------
31
32enum class Ownership {
33 PLUGIN,
34 SCORE,
35 };
36
37//---------------------------------------------------------
38// ScoreElement
40//---------------------------------------------------------
41
42class ScoreElement : public QObject {
43 Q_OBJECT
48 Q_PROPERTY(int type READ type)
54 Q_PROPERTY(QString name READ name)
55
56 Ownership _ownership;
57
58 qreal spatium() const;
59
60 protected:
62 Ms::ScoreElement* const e;
64
65 public:
67 ScoreElement(Ms::ScoreElement* _e = nullptr, Ownership own = Ownership::PLUGIN)
68 : QObject(), _ownership(own), e(_e) {}
69 ScoreElement(const ScoreElement&) = delete;
70 ScoreElement& operator=(const ScoreElement&) = delete;
71 virtual ~ScoreElement();
72
73 Ownership ownership() const { return _ownership; }
74 void setOwnership(Ownership o) { _ownership = o; }
75
76 Ms::ScoreElement* element() { return e; };
77 const Ms::ScoreElement* element() const { return e; };
78
79 QString name() const;
80 int type() const;
81
82 QVariant get(Ms::Pid pid) const;
83 void set(Ms::Pid pid, QVariant val);
85
86 Q_INVOKABLE QString userName() const;
88 Q_INVOKABLE bool is(Ms::PluginAPI::ScoreElement* other) { return other && element() == other->element(); }
89 };
90
91//---------------------------------------------------------
92// wrap
96//---------------------------------------------------------
97
98template <class Wrapper, class T>
99Wrapper* wrap(T* t, Ownership own = Ownership::SCORE)
100 {
101 Wrapper* w = t ? new Wrapper(t, own) : nullptr;
102 // All wrapper objects should belong to JavaScript code.
103 QQmlEngine::setObjectOwnership(w, QQmlEngine::JavaScriptOwnership);
104 return w;
105 }
106
107extern ScoreElement* wrap(Ms::ScoreElement* se, Ownership own = Ownership::SCORE);
108
109//---------------------------------------------------------
110// customWrap
116//---------------------------------------------------------
117
118template <class Wrapper, class T, typename... Args>
119Wrapper* customWrap(T* t, Args... args)
120 {
121 Wrapper* w = t ? new Wrapper(t, std::forward<Args>(args)...) : nullptr;
122 // All wrapper objects should belong to JavaScript code.
123 QQmlEngine::setObjectOwnership(w, QQmlEngine::JavaScriptOwnership);
124 return w;
125 }
126
127//---------------------------------------------------------
131//---------------------------------------------------------
132
133template <typename T, class Container>
134class QmlListAccess : public QQmlListProperty<T> {
135public:
137 QmlListAccess(QObject* obj, Container& container)
138 : QQmlListProperty<T>(obj, const_cast<void*>(static_cast<const void*>(&container)), &count, &at) {}
139
140 static int count(QQmlListProperty<T>* l) { return int(static_cast<Container*>(l->data)->size()); }
141 static T* at(QQmlListProperty<T>* l, int i)
142 {
143 auto el = static_cast<Container*>(l->data)->at(i);
144 // If a polymorphic wrap() function is available
145 // for the requested type, use it for wrapping.
146 if (std::is_same<T*, decltype(wrap(el, Ownership::SCORE))>::value)
147 return static_cast<T*>(wrap(el, Ownership::SCORE));
148 // Otherwise, wrap directly to the requested wrapper type.
149 return wrap<T>(el, Ownership::SCORE);
150 }
152 };
153
155template<typename T, class Container>
156QmlListAccess<T, Container> wrapContainerProperty(QObject* obj, Container& c)
157 {
158 return QmlListAccess<T, Container>(obj, c);
159 }
160} // namespace PluginAPI
161} // namespace Ms
162#endif
QML access to containers.
Definition scoreelement.h:134
Base class for most of object wrappers exposed to QML.
Definition scoreelement.h:42
Q_INVOKABLE bool is(Ms::PluginAPI::ScoreElement *other)
Checks whether two variables represent the same object.
Definition scoreelement.h:88
Q_INVOKABLE QString userName() const
Human-readable element type name.
Definition scoreelement.cpp:50
QString name
Name of this element's type, not localized.
Definition scoreelement.h:54
int type
Type of this element.
Definition scoreelement.h:48
Contains items exposed to the QML plugins framework.
Definition cursor.cpp:31
Definition cursor.cpp:30