MuseScore  3.4
Music composition and notation
scoreelement.h
Go to the documentation of this file.
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  protected:
60  Ms::ScoreElement* const e;
62 
63  public:
66  : QObject(), _ownership(own), e(_e) {}
67  ScoreElement(const ScoreElement&) = delete;
68  ScoreElement& operator=(const ScoreElement&) = delete;
69  virtual ~ScoreElement();
70 
71  Ownership ownership() const { return _ownership; }
72  void setOwnership(Ownership o) { _ownership = o; }
73 
74  Ms::ScoreElement* element() { return e; };
75  const Ms::ScoreElement* element() const { return e; };
76 
77  QString name() const;
78  int type() const;
79 
80  QVariant get(Ms::Pid pid) const;
81  void set(Ms::Pid pid, QVariant val);
83 
84  Q_INVOKABLE QString userName() const;
86  Q_INVOKABLE bool is(Ms::PluginAPI::ScoreElement* other) { return other && element() == other->element(); }
87  };
88 
89 //---------------------------------------------------------
90 // wrap
94 //---------------------------------------------------------
95 
96 template <class Wrapper, class T>
97 Wrapper* wrap(T* t, Ownership own = Ownership::SCORE)
98  {
99  Wrapper* w = t ? new Wrapper(t, own) : nullptr;
100  // All wrapper objects should belong to JavaScript code.
101  QQmlEngine::setObjectOwnership(w, QQmlEngine::JavaScriptOwnership);
102  return w;
103  }
104 
106 
107 //---------------------------------------------------------
111 //---------------------------------------------------------
112 
113 template <typename T, class Container>
114 class QmlListAccess : public QQmlListProperty<T> {
115 public:
117  QmlListAccess(QObject* obj, Container& container)
118  : QQmlListProperty<T>(obj, const_cast<void*>(static_cast<const void*>(&container)), &count, &at) {};
119 
120  static int count(QQmlListProperty<T>* l) { return int(static_cast<Container*>(l->data)->size()); }
121  static T* at(QQmlListProperty<T>* l, int i)
122  {
123  auto el = static_cast<Container*>(l->data)->at(i);
124  // If a polymorphic wrap() function is available
125  // for the requested type, use it for wrapping.
126  if (std::is_same<T*, decltype(wrap(el, Ownership::SCORE))>::value)
127  return static_cast<T*>(wrap(el, Ownership::SCORE));
128  // Otherwise, wrap directly to the requested wrapper type.
129  return wrap<T>(el, Ownership::SCORE);
130  }
132  };
133 
135 template<typename T, class Container>
137  {
138  return QmlListAccess<T, Container>(obj, c);
139  }
140 } // namespace PluginAPI
141 } // namespace Ms
142 #endif
Wrapper * wrap(T *t, Ownership own=Ownership::SCORE)
Definition: scoreelement.h:97
Base class for most of object wrappers exposed to QML.
Definition: scoreelement.h:42
Pid
Definition: property.h:62
Ms::ScoreElement * element()
Definition: scoreelement.h:74
Definition: scoreElement.h:173
QML access to containers.
Definition: scoreelement.h:114
Ownership ownership() const
Definition: scoreelement.h:71
Ownership
Definition: scoreelement.h:32
const Ms::ScoreElement * element() const
Definition: scoreelement.h:75
Definition: aeolus.cpp:26
Q_INVOKABLE bool is(Ms::PluginAPI::ScoreElement *other)
Checks whether two variables represent the same object.
Definition: scoreelement.h:86
static int count(QQmlListProperty< T > *l)
Definition: scoreelement.h:120
Element * wrap(Ms::Element *e, Ownership own)
Wraps Ms::Element choosing the correct wrapper type at runtime based on the actual element type...
Definition: elements.cpp:247
QmlListAccess(QObject *obj, Container &container)
Definition: scoreelement.h:117
static T * at(QQmlListProperty< T > *l, int i)
Definition: scoreelement.h:121
void setOwnership(Ownership o)
Definition: scoreelement.h:72
QmlListAccess< T, Container > wrapContainerProperty(QObject *obj, Container &c)
Definition: scoreelement.h:136