File: plugin-remove-el.diff

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-19
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 218,192 kB
  • sloc: cpp: 291,369; xml: 200,226; sh: 3,779; ansic: 1,447; python: 393; makefile: 249; perl: 82; pascal: 79
file content (146 lines) | stat: -rw-r--r-- 5,679 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Origin: backport, commit:19677e38768db0cd46832f30038d70012bc750a9
Author: Dale Larson <dlarson42@gmail.com>
Description: Fix #291790, fix #267604: Restore Chord.remove() and Chord.add() methods.
 Restore the add and remove QML methods for the
 Chord object. Adds exposed Element.parent property.
 These existed in v2.x. Finally it adds a removeElement
 method to PluginAPI for general element disposal.

--- a/mscore/plugin/api/elements.cpp
+++ b/mscore/plugin/api/elements.cpp
@@ -11,6 +11,7 @@
 //=============================================================================
 
 #include "elements.h"
+#include "score.h"
 #include "libmscore/property.h"
 
 namespace Ms {
@@ -70,6 +71,50 @@ void Note::setTpc(int val)
       }
 
 //---------------------------------------------------------
+//   Chord::add
+//---------------------------------------------------------
+
+void Chord::add(Ms::PluginAPI::Element* wrapped)
+      {
+      Ms::Element* s = wrapped->element();
+      if (s)
+            {
+            // Ensure that the object has the expected ownership
+            if (wrapped->ownership() == Ownership::SCORE) {
+                  qWarning("Chord::add: Cannot add this element. The element is already part of the score.");
+                  return;        // Don't allow operation.
+                  }
+            // Score now owns the object.
+            wrapped->setOwnership(Ownership::SCORE);
+            // Provide parentage for element.
+            s->setParent(chord());
+            // If a note, ensure the element has proper Tpc values. (Will crash otherwise)
+            if (s->isNote()) {
+                  s->setTrack(chord()->track());
+                  toNote(s)->setTpcFromPitch();
+                  }
+            // Create undo op and add the element.
+            chord()->score()->undoAddElement(s);
+            }
+      }
+
+//---------------------------------------------------------
+//   Chord::remove
+//---------------------------------------------------------
+
+void Chord::remove(Ms::PluginAPI::Element* wrapped)
+      {
+      Ms::Element* s = wrapped->element();
+      if (s->parent() != chord())
+            qWarning("PluginAPI::Chord::remove: The element is not a child of this chord. Use removeElement() instead.");
+      else if (chord()->notes().size() <= 1 && s->type() == ElementType::NOTE)
+            qWarning("PluginAPI::Chord::remove: Removal of final note is not allowed.");
+      else if (s)
+            chord()->score()->deleteItem(s); // Create undo op and remove the element.
+      }
+
+
+//---------------------------------------------------------
 //   wrap
 ///   \cond PLUGIN_API \private \endcond
 ///   Wraps Ms::Element choosing the correct wrapper type
--- a/mscore/plugin/api/elements.h
+++ b/mscore/plugin/api/elements.h
@@ -22,6 +22,7 @@
 #include "libmscore/notedot.h"
 #include "libmscore/segment.h"
 #include "libmscore/accidental.h"
+#include "libmscore/types.h"
 
 namespace Ms {
 namespace PluginAPI {
@@ -78,6 +79,11 @@ class Element : public Ms::PluginAPI::Sc
        * \see Element::offset
        */
       Q_PROPERTY(qreal offsetY READ offsetY WRITE setOffsetY)
+      /**
+       * Parent element for this element.
+       * \since 3.3
+       */
+      Q_PROPERTY(Ms::PluginAPI::Element* parent READ parent)
 
       API_PROPERTY( subtype,                 SUBTYPE                   )
       API_PROPERTY_READ_ONLY_T( bool, selected, SELECTED               )
@@ -316,6 +322,8 @@ class Element : public Ms::PluginAPI::Sc
       void setOffsetX(qreal offX);
       void setOffsetY(qreal offY);
 
+      Ms::PluginAPI::Element* parent() const { return wrap(element()->parent()); }
+
    public:
       /// \cond MS_INTERNAL
       Element(Ms::Element* e = nullptr, Ownership own = Ownership::PLUGIN)
@@ -449,6 +457,13 @@ class Chord : public Element {
       //QQmlListProperty<Element> hook()         { return wrapContainerProperty<Element>(this, chord()->hook());      }
       Ms::NoteType noteType()                  { return chord()->noteType(); }
       /// \endcond
+
+      /// Add to a chord's elements.
+      /// \since MuseScore 3.3
+      Q_INVOKABLE void add(Ms::PluginAPI::Element* wrapped);
+      /// Remove a chord's element.
+      /// \since MuseScore 3.3
+      Q_INVOKABLE void remove(Ms::PluginAPI::Element* wrapped);
       };
 
 //---------------------------------------------------------
--- a/mscore/plugin/api/qmlpluginapi.cpp
+++ b/mscore/plugin/api/qmlpluginapi.cpp
@@ -178,6 +178,19 @@ Element* PluginAPI::newElement(int eleme
       }
 
 //---------------------------------------------------------
+//   removeElement
+///   Disposes of an Element and its children.
+///   \param Element type.
+///   \since MuseScore 3.3
+//---------------------------------------------------------
+
+void PluginAPI::removeElement(Ms::PluginAPI::Element* wrapped)
+      {
+      Ms::Score* score = wrapped->element()->score();
+      score->deleteItem(wrapped->element());
+      }
+
+//---------------------------------------------------------
 //   newScore
 //---------------------------------------------------------
 
--- a/mscore/plugin/api/qmlpluginapi.h
+++ b/mscore/plugin/api/qmlpluginapi.h
@@ -165,6 +165,7 @@ class PluginAPI : public Ms::QmlPlugin {
 
       Q_INVOKABLE Ms::PluginAPI::Score* newScore(const QString& name, const QString& part, int measures);
       Q_INVOKABLE Ms::PluginAPI::Element* newElement(int);
+      Q_INVOKABLE void removeElement(Ms::PluginAPI::Element* wrapped);
       Q_INVOKABLE void cmd(const QString&);
       /** \cond PLUGIN_API \private \endcond */
       Q_INVOKABLE Ms::PluginAPI::MsProcess* newQProcess();