MuseScore/libmscore/duration.cpp

162 lines
4.6 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2008-2011 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 "duration.h"
#include "measure.h"
2012-05-26 14:26:10 +02:00
#include "tuplet.h"
#include "score.h"
#include "undo.h"
#include "staff.h"
2014-04-09 16:09:21 +02:00
#include "xml.h"
#include "property.h"
2012-05-26 14:26:10 +02:00
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// DurationElement
//---------------------------------------------------------
2018-03-27 14:40:34 +02:00
DurationElement::DurationElement(Score* s, ElementFlags f)
: Element(s, f)
2012-05-26 14:26:10 +02:00
{
_tuplet = 0;
}
//---------------------------------------------------------
// DurationElement
//---------------------------------------------------------
DurationElement::DurationElement(const DurationElement& e)
: Element(e)
{
_tuplet = 0; // e._tuplet;
2012-05-26 14:26:10 +02:00
_duration = e._duration;
}
//---------------------------------------------------------
2019-03-19 13:48:38 +01:00
// ~DurationElement
//---------------------------------------------------------
DurationElement::~DurationElement()
{
if (_tuplet && _tuplet->contains(this)) {
while (Tuplet* t = topTuplet()) // delete tuplets from top to bottom
delete t; // Tuplet destructor removes references to the deleted object
}
}
//---------------------------------------------------------
// topTuplet
//---------------------------------------------------------
Tuplet* DurationElement::topTuplet() const
{
Tuplet* t = tuplet();
if (t) {
while (t->tuplet())
t = t->tuplet();
}
return t;
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// globalTicks
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
Fraction DurationElement::globalTicks() const
2012-05-26 14:26:10 +02:00
{
Fraction f(_duration);
for (Tuplet* t = tuplet(); t; t = t->tuplet())
f /= t->ratio();
return f;
}
//---------------------------------------------------------
// actualTicks
//---------------------------------------------------------
Fraction DurationElement::actualTicks() const
{
return globalTicks() / staff()->timeStretch(tick());
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2018-06-09 06:19:36 +02:00
// readAddTuplet
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2018-06-09 06:19:36 +02:00
void DurationElement::readAddTuplet(Tuplet* t)
2012-05-26 14:26:10 +02:00
{
setTuplet(t);
if (!score()->undoStack()->active()) // HACK, also added in Undo::AddElement()
t->add(this);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
2018-06-09 06:19:36 +02:00
// writeTupletStart
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2018-06-09 06:19:36 +02:00
void DurationElement::writeTupletStart(XmlWriter& xml) const
2012-05-26 14:26:10 +02:00
{
2018-06-09 06:19:36 +02:00
if (tuplet() && tuplet()->elements().front() == this) {
tuplet()->writeTupletStart(xml); // recursion
tuplet()->write(xml);
}
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
2018-06-09 06:19:36 +02:00
// writeTupletEnd
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2018-06-09 06:19:36 +02:00
void DurationElement::writeTupletEnd(XmlWriter& xml) const
2012-05-26 14:26:10 +02:00
{
2018-06-09 06:19:36 +02:00
if (tuplet() && tuplet()->elements().back() == this) {
xml.tagE("endTuplet");
tuplet()->writeTupletEnd(xml); // recursion
2012-05-26 14:26:10 +02:00
}
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
QVariant DurationElement::getProperty(Pid propertyId) const
{
switch (propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::DURATION:
return QVariant::fromValue(_duration);
default:
return Element::getProperty(propertyId);
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
2018-03-27 15:36:00 +02:00
bool DurationElement::setProperty(Pid propertyId, const QVariant& v)
{
switch (propertyId) {
2018-03-27 15:36:00 +02:00
case Pid::DURATION: {
Fraction f(v.value<Fraction>());
setTicks(f);
triggerLayout();
}
break;
default:
return Element::setProperty(propertyId, v);
}
return true;
}
2013-05-13 18:49:17 +02:00
}