MuseScore Plugins  3.2
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 
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 
92 template <class Wrapper, class T>
93 Wrapper* 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 
101 extern ScoreElement* wrap(Ms::ScoreElement* se, Ownership own = Ownership::SCORE);
102 
103 //---------------------------------------------------------
107 //---------------------------------------------------------
108 
109 template <typename T, class Container>
110 class QmlListAccess : public QQmlListProperty<T> {
111 public:
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) { return wrap<T>(static_cast<Container*>(l->data)->at(i), Ownership::SCORE); }
119  };
120 
122 template<typename T, class Container>
123 QmlListAccess<T, Container> wrapContainerProperty(QObject* obj, Container& c)
124  {
125  return QmlListAccess<T, Container>(obj, c);
126  }
127 } // namespace PluginAPI
128 } // namespace Ms
129 #endif
Base class for most of object wrappers exposed to QML.
Definition: scoreelement.h:42
QML access to containers.
Definition: scoreelement.h:110
Definition: cursor.cpp:29