MuseScore Plugins 3.2.3
Plugins API for MuseScore
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
59 protected:
60 Ms::ScoreElement* const e;
61
62 public:
63 ScoreElement(Ms::ScoreElement* _e = nullptr, Ownership own = Ownership::PLUGIN)
64 : QObject(), _ownership(own), e(_e) {}
65 ScoreElement(const ScoreElement&) = delete;
66 ScoreElement& operator=(const ScoreElement&) = delete;
67 virtual ~ScoreElement();
68
69 Ownership ownership() const { return _ownership; }
70 void setOwnership(Ownership o) { _ownership = o; }
71
72 Ms::ScoreElement* element() { return e; };
73 const Ms::ScoreElement* element() const { return e; };
74
75 QString name() const;
76 int type() const;
77
78 QVariant get(Ms::Pid pid) const;
79 void set(Ms::Pid pid, QVariant val);
81
82 Q_INVOKABLE QString userName() const;
83 };
84
85//---------------------------------------------------------
86// wrap
90//---------------------------------------------------------
91
92template <class Wrapper, class T>
93Wrapper* wrap(T* t, Ownership own = Ownership::SCORE)
94 {
95 Wrapper* w = t ? new Wrapper(t, own) : nullptr;
96 // All wrapper objects should belong to JavaScript code.
97 QQmlEngine::setObjectOwnership(w, QQmlEngine::JavaScriptOwnership);
98 return w;
99 }
100
101extern ScoreElement* wrap(Ms::ScoreElement* se, Ownership own = Ownership::SCORE);
102
103//---------------------------------------------------------
107//---------------------------------------------------------
108
109template <typename T, class Container>
110class QmlListAccess : public QQmlListProperty<T> {
111public:
113 QmlListAccess(QObject* obj, Container& container)
114 : QQmlListProperty<T>(obj, const_cast<void*>(static_cast<const void*>(&container)), &count, &at) {};
115
116 static int count(QQmlListProperty<T>* l) { return int(static_cast<Container*>(l->data)->size()); }
117 static T* at(QQmlListProperty<T>* l, int i)
118 {
119 auto el = static_cast<Container*>(l->data)->at(i);
120 // If a polymorphic wrap() function is available
121 // for the requested type, use it for wrapping.
122 if (std::is_same<T*, decltype(wrap(el, Ownership::SCORE))>::value)
123 return static_cast<T*>(wrap(el, Ownership::SCORE));
124 // Otherwise, wrap directly to the requested wrapper type.
125 return wrap<T>(el, Ownership::SCORE);
126 }
128 };
129
131template<typename T, class Container>
132QmlListAccess<T, Container> wrapContainerProperty(QObject* obj, Container& c)
133 {
134 return QmlListAccess<T, Container>(obj, c);
135 }
136} // namespace PluginAPI
137} // namespace Ms
138#endif
QML access to containers.
Definition: scoreelement.h:110
Base class for most of object wrappers exposed to QML.
Definition: scoreelement.h:42
QString name
Name of this element's type, not localized.
Definition: scoreelement.h:54
int type
Type of this element.
Definition: scoreelement.h:48
Definition: cursor.cpp:29