Skip to content

Commit

Permalink
Fix #291790, fix #267604: Restore Chord.remove() and Chord.add() meth…
Browse files Browse the repository at this point in the history
…ods.

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.
  • Loading branch information
DLLarson committed Jul 29, 2019
1 parent c25a17e commit 19677e3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
44 changes: 44 additions & 0 deletions mscore/plugin/api/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,50 @@ void Chord::setPlayEventType(Ms::PlayEventType v)
}
}

//---------------------------------------------------------
// 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
Expand Down
15 changes: 15 additions & 0 deletions mscore/plugin/api/elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "libmscore/score.h"
#include "libmscore/undo.h"
#include "playevent.h"
#include "libmscore/types.h"

namespace Ms {
namespace PluginAPI {
Expand Down Expand Up @@ -82,6 +83,11 @@ class Element : public Ms::PluginAPI::ScoreElement {
* \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 )
Expand Down Expand Up @@ -320,6 +326,8 @@ class Element : public Ms::PluginAPI::ScoreElement {
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)
Expand Down Expand Up @@ -468,6 +476,13 @@ class Chord : public Element {
Ms::PlayEventType playEventType() { return chord()->playEventType(); }
void setPlayEventType(Ms::PlayEventType v);
/// \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);
};

//---------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions mscore/plugin/api/qmlpluginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ Element* PluginAPI::newElement(int elementType)
return wrap(e, Ownership::PLUGIN);
}

//---------------------------------------------------------
// 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
//---------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions mscore/plugin/api/qmlpluginapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,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();
Expand Down

0 comments on commit 19677e3

Please sign in to comment.