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
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2019 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include "elements.h"
#include "score.h"
#include "libmscore/property.h"
namespace Ms {
namespace PluginAPI {
//---------------------------------------------------------
// Element::setOffsetX
//---------------------------------------------------------
void Element::setOffsetX(qreal offX)
{
offX *= element()->spatium();
QPointF off(element()->offset());
off.rx() += offX;
set(Ms::Pid::OFFSET, off);
}
//---------------------------------------------------------
// Element::setOffsetY
//---------------------------------------------------------
void Element::setOffsetY(qreal offY)
{
offY *= element()->spatium();
QPointF off(element()->offset());
off.ry() += offY;
set(Ms::Pid::OFFSET, off);
}
//---------------------------------------------------------
// Segment::elementAt
//---------------------------------------------------------
Element* Segment::elementAt(int track)
{
Ms::Element* el = segment()->elementAt(track);
if (!el)
return nullptr;
return wrap(el, Ownership::SCORE);
}
//---------------------------------------------------------
// Note::setTpc
//---------------------------------------------------------
void Note::setTpc(int val)
{
if (!tpcIsValid(val)) {
qWarning("PluginAPI::Note::setTpc: invalid tpc: %d", val);
return;
}
if (note()->concertPitch())
set(Pid::TPC1, val);
else
set(Pid::TPC2, 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
/// at runtime based on the actual element type.
//---------------------------------------------------------
Element* wrap(Ms::Element* e, Ownership own)
{
if (!e)
return nullptr;
using Ms::ElementType;
switch(e->type()) {
case ElementType::NOTE:
return wrap<Note>(toNote(e), own);
case ElementType::CHORD:
return wrap<Chord>(toChord(e), own);
case ElementType::SEGMENT:
return wrap<Segment>(toSegment(e), own);
case ElementType::MEASURE:
return wrap<Measure>(toMeasure(e), own);
default:
break;
}
return wrap<Element>(e, own);
}
}
}
|