MuseScore Plugins  3.5
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 
18 namespace Ms {
19 
20 class ScoreElement;
21 
22 namespace PluginAPI {
23 
24 //---------------------------------------------------------
25 // Ownership
30 //---------------------------------------------------------
31 
32 enum class Ownership {
33  PLUGIN,
34  SCORE,
35  };
36 
37 //---------------------------------------------------------
38 // ScoreElement
40 //---------------------------------------------------------
41 
42 class 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 
98 template <class Wrapper, class T>
99 Wrapper* 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 
107 extern ScoreElement* wrap(Ms::ScoreElement* se, Ownership own = Ownership::SCORE);
108 
109 //---------------------------------------------------------
110 // customWrap
116 //---------------------------------------------------------
117 
118 template <class Wrapper, class T, typename... Args>
119 Wrapper* 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 
133 template <typename T, class Container>
134 class QmlListAccess : public QQmlListProperty<T> {
135 public:
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 
155 template<typename T, class Container>
156 QmlListAccess<T, Container> wrapContainerProperty(QObject* obj, Container& c)
157  {
158  return QmlListAccess<T, Container>(obj, c);
159  }
160 } // namespace PluginAPI
161 } // namespace Ms
162 #endif
Base class for most of object wrappers exposed to QML.
Definition: scoreelement.h:42
QML access to containers.
Definition: scoreelement.h:134
Q_INVOKABLE bool is(Ms::PluginAPI::ScoreElement *other)
Checks whether two variables represent the same object.
Definition: scoreelement.h:88
Definition: cursor.cpp:30