MuseScore/libmscore/scoreElement.cpp

451 lines
22 KiB
C++
Raw Normal View History

//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2015 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 "scoreElement.h"
#include "score.h"
#include "undo.h"
#include "xml.h"
2017-03-31 13:03:15 +02:00
#include "bracket.h"
#include "bracketItem.h"
namespace Ms {
2017-01-18 14:16:33 +01:00
//
// list has to be synchronized with ElementType enum
//
static const ElementName elementNames[] = {
{ ElementType::INVALID, "invalid", QT_TRANSLATE_NOOP("elementName", "invalid") },
2017-03-31 13:03:15 +02:00
{ ElementType::BRACKET_ITEM, "BracketItem", QT_TRANSLATE_NOOP("elementName", "BracketItem") },
2017-01-18 14:16:33 +01:00
{ ElementType::PART, "Part", QT_TRANSLATE_NOOP("elementName", "Part") },
{ ElementType::STAFF, "Staff", QT_TRANSLATE_NOOP("elementName", "Staff") },
{ ElementType::SCORE, "Score", QT_TRANSLATE_NOOP("elementName", "Score") },
{ ElementType::SYMBOL, "Symbol", QT_TRANSLATE_NOOP("elementName", "Symbol") },
{ ElementType::TEXT, "Text", QT_TRANSLATE_NOOP("elementName", "Text") },
{ ElementType::INSTRUMENT_NAME, "InstrumentName", QT_TRANSLATE_NOOP("elementName", "Instrument Name") },
{ ElementType::SLUR_SEGMENT, "SlurSegment", QT_TRANSLATE_NOOP("elementName", "Slur Segment") },
{ ElementType::TIE_SEGMENT, "TieSegment", QT_TRANSLATE_NOOP("elementName", "Tie Segment") },
{ ElementType::STAFF_LINES, "StaffLines", QT_TRANSLATE_NOOP("elementName", "Staff Lines") },
{ ElementType::BAR_LINE, "BarLine", QT_TRANSLATE_NOOP("elementName", "Barline") },
{ ElementType::SYSTEM_DIVIDER, "SystemDivider", QT_TRANSLATE_NOOP("elementName", "System Divider") },
{ ElementType::STEM_SLASH, "StemSlash", QT_TRANSLATE_NOOP("elementName", "Stem Slash") },
{ ElementType::LINE, "Line", QT_TRANSLATE_NOOP("elementName", "Line") },
{ ElementType::ARPEGGIO, "Arpeggio", QT_TRANSLATE_NOOP("elementName", "Arpeggio") },
{ ElementType::ACCIDENTAL, "Accidental", QT_TRANSLATE_NOOP("elementName", "Accidental") },
{ ElementType::LEDGER_LINE, "LedgerLine", QT_TRANSLATE_NOOP("elementName", "Ledger Line") },
{ ElementType::STEM, "Stem", QT_TRANSLATE_NOOP("elementName", "Stem") },
{ ElementType::NOTE, "Note", QT_TRANSLATE_NOOP("elementName", "Note") },
{ ElementType::CLEF, "Clef", QT_TRANSLATE_NOOP("elementName", "Clef") },
{ ElementType::KEYSIG, "KeySig", QT_TRANSLATE_NOOP("elementName", "Key Signature") },
{ ElementType::AMBITUS, "Ambitus", QT_TRANSLATE_NOOP("elementName", "Ambitus") },
{ ElementType::TIMESIG, "TimeSig", QT_TRANSLATE_NOOP("elementName", "Time Signature") },
{ ElementType::REST, "Rest", QT_TRANSLATE_NOOP("elementName", "Rest") },
{ ElementType::BREATH, "Breath", QT_TRANSLATE_NOOP("elementName", "Breath") },
{ ElementType::REPEAT_MEASURE, "RepeatMeasure", QT_TRANSLATE_NOOP("elementName", "Repeat Measure") },
{ ElementType::TIE, "Tie", QT_TRANSLATE_NOOP("elementName", "Tie") },
{ ElementType::ARTICULATION, "Articulation", QT_TRANSLATE_NOOP("elementName", "Articulation") },
{ ElementType::CHORDLINE, "ChordLine", QT_TRANSLATE_NOOP("elementName", "Chord Line") },
{ ElementType::DYNAMIC, "Dynamic", QT_TRANSLATE_NOOP("elementName", "Dynamic") },
{ ElementType::BEAM, "Beam", QT_TRANSLATE_NOOP("elementName", "Beam") },
{ ElementType::HOOK, "Hook", QT_TRANSLATE_NOOP("elementName", "Hook") },
{ ElementType::LYRICS, "Lyrics", QT_TRANSLATE_NOOP("elementName", "Lyrics") },
{ ElementType::FIGURED_BASS, "FiguredBass", QT_TRANSLATE_NOOP("elementName", "Figured Bass") },
{ ElementType::MARKER, "Marker", QT_TRANSLATE_NOOP("elementName", "Marker") },
{ ElementType::JUMP, "Jump", QT_TRANSLATE_NOOP("elementName", "Jump") },
{ ElementType::FINGERING, "Fingering", QT_TRANSLATE_NOOP("elementName", "Fingering") },
{ ElementType::TUPLET, "Tuplet", QT_TRANSLATE_NOOP("elementName", "Tuplet") },
{ ElementType::TEMPO_TEXT, "Tempo", QT_TRANSLATE_NOOP("elementName", "Tempo") },
{ ElementType::STAFF_TEXT, "StaffText", QT_TRANSLATE_NOOP("elementName", "Staff Text") },
2017-01-20 11:05:52 +01:00
{ ElementType::SYSTEM_TEXT, "SystemText", QT_TRANSLATE_NOOP("elementName", "System Text") },
2017-01-18 14:16:33 +01:00
{ ElementType::REHEARSAL_MARK, "RehearsalMark", QT_TRANSLATE_NOOP("elementName", "Rehearsal Mark") },
{ ElementType::INSTRUMENT_CHANGE, "InstrumentChange", QT_TRANSLATE_NOOP("elementName", "Instrument Change") },
2017-03-16 00:58:46 +01:00
{ ElementType::STAFFTYPE_CHANGE, "StaffTypeChange", QT_TRANSLATE_NOOP("elementName", "Staff Type Change") },
2017-01-18 14:16:33 +01:00
{ ElementType::HARMONY, "Harmony", QT_TRANSLATE_NOOP("elementName", "Chord Symbol") },
{ ElementType::FRET_DIAGRAM, "FretDiagram", QT_TRANSLATE_NOOP("elementName", "Fretboard Diagram") },
{ ElementType::BEND, "Bend", QT_TRANSLATE_NOOP("elementName", "Bend") },
{ ElementType::TREMOLOBAR, "TremoloBar", QT_TRANSLATE_NOOP("elementName", "Tremolo Bar") },
{ ElementType::VOLTA, "Volta", QT_TRANSLATE_NOOP("elementName", "Volta") },
{ ElementType::HAIRPIN_SEGMENT, "HairpinSegment", QT_TRANSLATE_NOOP("elementName", "Hairpin Segment") },
{ ElementType::OTTAVA_SEGMENT, "OttavaSegment", QT_TRANSLATE_NOOP("elementName", "Ottava Segment") },
{ ElementType::TRILL_SEGMENT, "TrillSegment", QT_TRANSLATE_NOOP("elementName", "Trill Segment") },
{ ElementType::TEXTLINE_SEGMENT, "TextLineSegment", QT_TRANSLATE_NOOP("elementName", "Text Line Segment") },
{ ElementType::VOLTA_SEGMENT, "VoltaSegment", QT_TRANSLATE_NOOP("elementName", "Volta Segment") },
{ ElementType::PEDAL_SEGMENT, "PedalSegment", QT_TRANSLATE_NOOP("elementName", "Pedal Segment") },
{ ElementType::LYRICSLINE_SEGMENT, "LyricsLineSegment", QT_TRANSLATE_NOOP("elementName", "Melisma Line Segment") },
{ ElementType::GLISSANDO_SEGMENT, "GlissandoSegment", QT_TRANSLATE_NOOP("elementName", "Glissando Segment") },
{ ElementType::LAYOUT_BREAK, "LayoutBreak", QT_TRANSLATE_NOOP("elementName", "Layout Break") },
{ ElementType::SPACER, "Spacer", QT_TRANSLATE_NOOP("elementName", "Spacer") },
{ ElementType::STAFF_STATE, "StaffState", QT_TRANSLATE_NOOP("elementName", "Staff State") },
{ ElementType::NOTEHEAD, "NoteHead", QT_TRANSLATE_NOOP("elementName", "Notehead") },
{ ElementType::NOTEDOT, "NoteDot", QT_TRANSLATE_NOOP("elementName", "Note Dot") },
{ ElementType::TREMOLO, "Tremolo", QT_TRANSLATE_NOOP("elementName", "Tremolo") },
{ ElementType::IMAGE, "Image", QT_TRANSLATE_NOOP("elementName", "Image") },
{ ElementType::MEASURE, "Measure", QT_TRANSLATE_NOOP("elementName", "Measure") },
{ ElementType::SELECTION, "Selection", QT_TRANSLATE_NOOP("elementName", "Selection") },
{ ElementType::LASSO, "Lasso", QT_TRANSLATE_NOOP("elementName", "Lasso") },
{ ElementType::SHADOW_NOTE, "ShadowNote", QT_TRANSLATE_NOOP("elementName", "Shadow Note") },
{ ElementType::TAB_DURATION_SYMBOL, "TabDurationSymbol", QT_TRANSLATE_NOOP("elementName", "Tab Duration Symbol") },
{ ElementType::FSYMBOL, "FSymbol", QT_TRANSLATE_NOOP("elementName", "Font Symbol") },
{ ElementType::PAGE, "Page", QT_TRANSLATE_NOOP("elementName", "Page") },
{ ElementType::HAIRPIN, "HairPin", QT_TRANSLATE_NOOP("elementName", "Hairpin") },
{ ElementType::OTTAVA, "Ottava", QT_TRANSLATE_NOOP("elementName", "Ottava") },
{ ElementType::PEDAL, "Pedal", QT_TRANSLATE_NOOP("elementName", "Pedal") },
{ ElementType::TRILL, "Trill", QT_TRANSLATE_NOOP("elementName", "Trill") },
{ ElementType::TEXTLINE, "TextLine", QT_TRANSLATE_NOOP("elementName", "Text Line") },
{ ElementType::TEXTLINE_BASE, "TextLineBase", QT_TRANSLATE_NOOP("elementName", "Text Line Base") }, // remove
{ ElementType::NOTELINE, "NoteLine", QT_TRANSLATE_NOOP("elementName", "Note Line") },
{ ElementType::LYRICSLINE, "LyricsLine", QT_TRANSLATE_NOOP("elementName", "Melisma Line") },
{ ElementType::GLISSANDO, "Glissando", QT_TRANSLATE_NOOP("elementName", "Glissando") },
{ ElementType::BRACKET, "Bracket", QT_TRANSLATE_NOOP("elementName", "Bracket") },
{ ElementType::SEGMENT, "Segment", QT_TRANSLATE_NOOP("elementName", "Segment") },
{ ElementType::SYSTEM, "System", QT_TRANSLATE_NOOP("elementName", "System") },
{ ElementType::COMPOUND, "Compound", QT_TRANSLATE_NOOP("elementName", "Compound") },
{ ElementType::CHORD, "Chord", QT_TRANSLATE_NOOP("elementName", "Chord") },
{ ElementType::SLUR, "Slur", QT_TRANSLATE_NOOP("elementName", "Slur") },
{ ElementType::ELEMENT, "Element", QT_TRANSLATE_NOOP("elementName", "Element") },
{ ElementType::ELEMENT_LIST, "ElementList", QT_TRANSLATE_NOOP("elementName", "Element List") },
{ ElementType::STAFF_LIST, "StaffList", QT_TRANSLATE_NOOP("elementName", "Staff List") },
{ ElementType::MEASURE_LIST, "MeasureList", QT_TRANSLATE_NOOP("elementName", "Measure List") },
{ ElementType::HBOX, "HBox", QT_TRANSLATE_NOOP("elementName", "Horizontal Frame") },
{ ElementType::VBOX, "VBox", QT_TRANSLATE_NOOP("elementName", "Vertical Frame") },
{ ElementType::TBOX, "TBox", QT_TRANSLATE_NOOP("elementName", "Text Frame") },
{ ElementType::FBOX, "FBox", QT_TRANSLATE_NOOP("elementName", "Fretboard Diagram Frame") },
{ ElementType::ICON, "Icon", QT_TRANSLATE_NOOP("elementName", "Icon") },
{ ElementType::OSSIA, "Ossia", QT_TRANSLATE_NOOP("elementName", "Ossia") },
{ ElementType::BAGPIPE_EMBELLISHMENT,"BagpipeEmbellishment", QT_TRANSLATE_NOOP("elementName", "Bagpipe Embellishment") }
};
2016-03-30 22:33:04 +02:00
//---------------------------------------------------------
// ScoreElement
//---------------------------------------------------------
ScoreElement::ScoreElement(const ScoreElement& se)
{
_score = se._score;
_links = 0;
}
//---------------------------------------------------------
2017-03-31 13:03:15 +02:00
// ~Element
2016-03-30 22:33:04 +02:00
//---------------------------------------------------------
ScoreElement::~ScoreElement()
{
if (_links) {
_links->removeOne(this);
if (_links->empty()) {
delete _links;
_links = 0;
}
}
}
//---------------------------------------------------------
// resetProperty
//---------------------------------------------------------
void ScoreElement::resetProperty(P_ID id)
{
QVariant v = propertyDefault(id);
if (v.isValid())
setProperty(id, v);
}
2017-01-18 14:16:33 +01:00
//---------------------------------------------------------
// undoResetProperty
//---------------------------------------------------------
void ScoreElement::undoResetProperty(P_ID id)
{
PropertyFlags f = propertyFlags(id);
if (f == PropertyFlags::UNSTYLED) {
printf("reset property ==\n");
2017-01-18 14:16:33 +01:00
undoChangeProperty(id, propertyDefault(id), PropertyFlags::STYLED);
}
2017-01-18 14:16:33 +01:00
else
undoChangeProperty(id, propertyDefault(id), f);
}
//---------------------------------------------------------
// undoChangeProperty
//---------------------------------------------------------
2017-01-18 14:16:33 +01:00
void ScoreElement::undoChangeProperty(P_ID id, const QVariant& v)
{
2017-01-18 14:16:33 +01:00
undoChangeProperty(id, v, propertyFlags(id));
}
void ScoreElement::undoChangeProperty(P_ID id, const QVariant& v, PropertyFlags ps)
{
2017-03-31 13:03:15 +02:00
if (isBracket()) {
// brackets do not survive layout() and therefore cannot be on
// the undo stack; delegate to BracketItem:
BracketItem* bi = toBracket(this)->bracketItem();
bi->undoChangeProperty(id, v, ps);
return;
}
if (id == P_ID::AUTOPLACE && v.toBool() && !getProperty(id).toBool()) {
2017-01-18 14:16:33 +01:00
// special case: if we switch to autoplace, we must save
// user offset values
undoResetProperty(P_ID::USER_OFF);
if (isSlurSegment()) {
undoResetProperty(P_ID::SLUR_UOFF1);
undoResetProperty(P_ID::SLUR_UOFF2);
undoResetProperty(P_ID::SLUR_UOFF3);
undoResetProperty(P_ID::SLUR_UOFF4);
}
}
else if (id == P_ID::SUB_STYLE) {
//
// change a list of properties
//
auto l = subStyle(SubStyle(v.toInt()));
// Change to SubStyle defaults
for (const StyledProperty& p : l)
score()->undoChangeProperty(this, p.propertyIdx, score()->styleV(p.styleIdx), PropertyFlags::STYLED);
}
score()->undoChangeProperty(this, id, v, ps);
}
//---------------------------------------------------------
// undoPushProperty
//---------------------------------------------------------
void ScoreElement::undoPushProperty(P_ID id)
{
QVariant val = getProperty(id);
2016-03-10 10:41:31 +01:00
score()->undoStack()->push1(new ChangeProperty(this, id, val));
}
//---------------------------------------------------------
// writeProperty
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void ScoreElement::writeProperty(XmlWriter& xml, P_ID id) const
{
2016-12-28 16:23:10 +01:00
if (propertyType(id) == P_TYPE::SP_REAL) {
qreal _spatium = score()->spatium();
2017-02-23 11:41:53 +01:00
xml.tag(id, QVariant(getProperty(id).toReal()/_spatium),
QVariant(propertyDefault(id).toReal()/_spatium));
2016-12-28 16:23:10 +01:00
}
else
xml.tag(id, getProperty(id), propertyDefault(id));
}
//---------------------------------------------------------
// linkTo
//---------------------------------------------------------
void ScoreElement::linkTo(ScoreElement* element)
{
Q_ASSERT(element != this);
if (!_links) {
if (element->links()) {
_links = element->_links;
Q_ASSERT(_links->contains(element));
}
else {
_links = new LinkedElements(score());
_links->append(element);
element->_links = _links;
}
Q_ASSERT(!_links->contains(this));
_links->append(this);
}
else {
_links->append(element);
element->_links = _links;
}
}
//---------------------------------------------------------
// unlink
//---------------------------------------------------------
void ScoreElement::unlink()
{
if (_links) {
Q_ASSERT(_links->contains(this));
_links->removeOne(this);
// if link list is empty, remove list
if (_links->size() <= 1) {
if (!_links->empty()) // abnormal case: only "this" is in list
_links->front()->_links = 0;
delete _links;
}
_links = 0;
}
}
//---------------------------------------------------------
// undoUnlink
//---------------------------------------------------------
void ScoreElement::undoUnlink()
{
2016-12-02 10:47:09 +01:00
if (_links)
_score->undo(new Unlink(this));
}
//---------------------------------------------------------
// linkList
//---------------------------------------------------------
QList<ScoreElement*> ScoreElement::linkList() const
{
QList<ScoreElement*> el;
if (links())
el.append(*links());
else
el.append((Element*)this);
return el;
}
//---------------------------------------------------------
// LinkedElements
//---------------------------------------------------------
LinkedElements::LinkedElements(Score* score)
{
_lid = score->linkId(); // create new unique id
}
LinkedElements::LinkedElements(Score* score, int id)
{
_lid = id;
score->linkId(id); // remember used id
}
//---------------------------------------------------------
// setLid
//---------------------------------------------------------
void LinkedElements::setLid(Score* score, int id)
{
_lid = id;
score->linkId(id);
}
2016-03-10 10:41:31 +01:00
//---------------------------------------------------------
// masterScore
//---------------------------------------------------------
MasterScore* ScoreElement::masterScore() const
{
return _score->masterScore();
}
//---------------------------------------------------------
// propertyStyle
//---------------------------------------------------------
2017-01-16 20:51:12 +01:00
PropertyFlags ScoreElement::propertyFlags(P_ID) const
{
2017-01-16 20:51:12 +01:00
return PropertyFlags::NOSTYLE;
}
//---------------------------------------------------------
// getPropertyStyle
//---------------------------------------------------------
StyleIdx ScoreElement::getPropertyStyle(P_ID) const
{
return StyleIdx::NOSTYLE;
}
2017-01-18 14:16:33 +01:00
//---------------------------------------------------------
// name
//---------------------------------------------------------
const char* ScoreElement::name() const
{
return name(type());
}
//---------------------------------------------------------
// name
//---------------------------------------------------------
const char* ScoreElement::name(ElementType type)
{
return elementNames[int(type)].name;
}
//---------------------------------------------------------
// userName
//---------------------------------------------------------
QString ScoreElement::userName() const
{
return qApp->translate("elementName", elementNames[int(type())].userName);
}
//---------------------------------------------------------
// name2type
//---------------------------------------------------------
ElementType ScoreElement::name2type(const QStringRef& s)
{
for (int i = 0; i < int(ElementType::MAXTYPE); ++i) {
if (s == elementNames[i].name)
return ElementType(i);
}
2017-03-31 13:03:15 +02:00
qDebug("unknown type <%s>", qPrintable(s.toString()));
2017-01-18 14:16:33 +01:00
return ElementType::INVALID;
}
//---------------------------------------------------------
// isSLine
//---------------------------------------------------------
bool ScoreElement::isSLine() const
{
return isHairpin() || isOttava() || isPedal()
|| isTrill() || isVolta() || isTextLine() || isNoteLine() || isGlissando();
}
//---------------------------------------------------------
// isSLineSegment
//---------------------------------------------------------
bool ScoreElement::isSLineSegment() const
{
return isHairpinSegment() || isOttavaSegment() || isPedalSegment()
|| isTrillSegment() || isVoltaSegment() || isTextLineSegment()
|| isGlissandoSegment();
}
//---------------------------------------------------------
// isText
//---------------------------------------------------------
bool ScoreElement::isText() const
{
return type() == ElementType::TEXT
|| type() == ElementType::LYRICS
|| type() == ElementType::DYNAMIC
|| type() == ElementType::FINGERING
|| type() == ElementType::HARMONY
|| type() == ElementType::MARKER
|| type() == ElementType::JUMP
|| type() == ElementType::STAFF_TEXT
|| type() == ElementType::REHEARSAL_MARK
|| type() == ElementType::INSTRUMENT_CHANGE
|| type() == ElementType::FIGURED_BASS
|| type() == ElementType::TEMPO_TEXT
|| type() == ElementType::INSTRUMENT_NAME
;
}
}