MuseScore/libmscore/marker.cpp

365 lines
11 KiB
C++
Raw Normal View History

2013-02-25 14:56:34 +01:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2013 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 "marker.h"
#include "score.h"
2013-11-11 16:53:03 +01:00
#include "sym.h"
2014-04-09 16:09:21 +02:00
#include "xml.h"
#include "measure.h"
2013-02-25 14:56:34 +01:00
2013-05-13 18:49:17 +02:00
namespace Ms {
//must be in sync with Marker::Type enum
const MarkerTypeItem markerTypeTable[] = {
{ Marker::Type::SEGNO , QT_TRANSLATE_NOOP("markerType", "Segno") },
2014-10-15 20:43:06 +02:00
{ Marker::Type::VARSEGNO, QT_TRANSLATE_NOOP("markerType", "Segno variation")},
{ Marker::Type::CODA , QT_TRANSLATE_NOOP("markerType", "Coda") },
{ Marker::Type::VARCODA , QT_TRANSLATE_NOOP("markerType", "Varied coda") },
{ Marker::Type::CODETTA , QT_TRANSLATE_NOOP("markerType", "Codetta") },
{ Marker::Type::FINE , QT_TRANSLATE_NOOP("markerType", "Fine") },
{ Marker::Type::TOCODA , QT_TRANSLATE_NOOP("markerType", "To Coda") },
{ Marker::Type::USER , QT_TRANSLATE_NOOP("markerType", "Custom") }
};
int markerTypeTableSize()
{
return sizeof(markerTypeTable)/sizeof(MarkerTypeItem) - 1; //-1 for the user defined
}
2013-02-25 14:56:34 +01:00
//---------------------------------------------------------
// Marker
//---------------------------------------------------------
Marker::Marker(Score* s)
2017-12-27 11:51:00 +01:00
: TextBase(s)
2013-02-25 14:56:34 +01:00
{
2017-12-27 11:51:00 +01:00
init(SubStyle::REPEAT_LEFT);
_markerType = Type::FINE;
setFlags(ElementFlag::MOVABLE | ElementFlag::SELECTABLE | ElementFlag::ON_STAFF);
setLayoutToParentWidth(true);
2013-02-25 14:56:34 +01:00
}
//---------------------------------------------------------
// setMarkerType
//---------------------------------------------------------
void Marker::setMarkerType(Type t)
2013-02-25 14:56:34 +01:00
{
_markerType = t;
2014-08-06 15:28:12 +02:00
const char* txt = 0;
2014-01-16 12:33:23 +01:00
switch (t) {
case Type::SEGNO:
2014-08-06 15:28:12 +02:00
txt = "<sym>segno</sym>";
2013-02-25 14:56:34 +01:00
setLabel("segno");
break;
case Type::VARSEGNO:
2014-08-06 15:28:12 +02:00
txt = "<sym>segnoSerpent1</sym>";
2013-02-25 14:56:34 +01:00
setLabel("varsegno");
break;
case Type::CODA:
2014-08-06 15:28:12 +02:00
txt = "<sym>coda</sym>";
2013-02-25 14:56:34 +01:00
setLabel("codab");
break;
case Type::VARCODA:
2014-08-06 15:28:12 +02:00
txt = "<sym>codaSquare</sym>";
2013-02-25 14:56:34 +01:00
setLabel("varcoda");
break;
case Type::CODETTA:
2014-08-06 15:28:12 +02:00
txt = "<sym>coda</sym><sym>coda</sym>";
2013-02-25 14:56:34 +01:00
setLabel("codetta");
break;
case Type::FINE:
2014-08-06 15:28:12 +02:00
txt = "Fine";
2017-01-16 20:51:12 +01:00
initSubStyle(SubStyle::REPEAT_RIGHT);
2013-02-25 14:56:34 +01:00
setLabel("fine");
break;
case Type::TOCODA:
2014-08-06 15:28:12 +02:00
txt = "To Coda";
2017-01-16 20:51:12 +01:00
initSubStyle(SubStyle::REPEAT_RIGHT);
2013-02-25 14:56:34 +01:00
setLabel("coda");
break;
case Type::USER:
2013-02-25 14:56:34 +01:00
break;
default:
2015-10-27 11:30:09 +01:00
qDebug("unknown marker type %d", int(t));
2013-02-25 14:56:34 +01:00
break;
}
2016-02-06 22:03:43 +01:00
if (empty() && txt)
setXmlText(txt);
2013-02-25 14:56:34 +01:00
}
2016-02-04 17:06:32 +01:00
QString Marker::markerTypeUserName() const
{
2014-08-26 17:39:31 +02:00
return qApp->translate("markerType", markerTypeTable[static_cast<int>(_markerType)].name.toUtf8().constData());
}
2013-02-25 14:56:34 +01:00
//---------------------------------------------------------
// styleChanged
//---------------------------------------------------------
2014-01-16 12:33:23 +01:00
void Marker::styleChanged()
{
setMarkerType(_markerType);
}
2013-02-25 14:56:34 +01:00
//---------------------------------------------------------
// adjustReadPos
//---------------------------------------------------------
void Marker::adjustReadPos()
{
if (!readPos().isNull()) {
2013-02-25 14:56:34 +01:00
QPointF uo;
/*
2013-02-25 14:56:34 +01:00
if (score()->mscVersion() <= 114) {
// rebase from Measure to Segment
uo = userOff();
uo.rx() -= segment()->pos().x();
// 1.2 is always HCENTER aligned
2017-01-05 11:23:47 +01:00
if ((textStyle().align() & Align::HMASK) == 0) // Align::LEFT
2013-02-25 14:56:34 +01:00
uo.rx() -= bbox().width() * .5;
}
else
*/
2013-02-25 14:56:34 +01:00
uo = readPos() - ipos();
setUserOff(uo);
setReadPos(QPointF());
}
}
//---------------------------------------------------------
// markerType
//---------------------------------------------------------
Marker::Type Marker::markerType(const QString& s) const
2013-02-25 14:56:34 +01:00
{
if (s == "segno")
return Type::SEGNO;
2013-02-25 14:56:34 +01:00
else if (s == "varsegno")
return Type::VARSEGNO;
2013-02-25 14:56:34 +01:00
else if (s == "codab")
return Type::CODA;
2013-02-25 14:56:34 +01:00
else if (s == "varcoda")
return Type::VARCODA;
2013-02-25 14:56:34 +01:00
else if (s == "codetta")
return Type::CODETTA;
2013-02-25 14:56:34 +01:00
else if (s == "fine")
return Type::FINE;
2013-02-25 14:56:34 +01:00
else if (s == "coda")
return Type::TOCODA;
2013-02-25 14:56:34 +01:00
else
return Type::USER;
2013-02-25 14:56:34 +01:00
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void Marker::layout()
{
2017-02-10 15:13:23 +01:00
setPos(QPointF(0.0, score()->styleP(StyleIdx::markerPosAbove)));
2017-12-27 11:51:00 +01:00
TextBase::layout1();
2018-01-30 10:09:46 +01:00
// although normally laid out to parent (measure) width,
// force to center over barline if left-aligned
2017-08-10 12:19:45 +02:00
if (layoutToParentWidth() && !(align() & (Align::RIGHT | Align::HCENTER)))
rxpos() -= width() * 0.5;
2018-01-30 10:09:46 +01:00
if (parent() && autoplace()) {
setUserOff(QPointF());
int si = staffIdx();
qreal minDistance = 0.5 * spatium(); // score()->styleP(StyleIdx::tempoMinDistance);
2018-01-30 21:08:38 +01:00
Shape& s1 = measure()->staffShape(si);
2018-01-30 10:09:46 +01:00
Shape s2 = shape().translated(pos());
if (placeAbove()) {
qreal d = s2.minVerticalDistance(s1);
2018-01-30 21:08:38 +01:00
if (d > -minDistance) {
2018-02-01 10:37:12 +01:00
qreal yd = -d - minDistance;
rUserYoffset() = yd;
s2.translate(QPointF(0.0, yd));
2018-01-30 21:08:38 +01:00
}
2018-01-30 10:09:46 +01:00
}
else {
qreal d = s1.minVerticalDistance(s2);
2018-01-30 21:08:38 +01:00
if (d > -minDistance) {
2018-02-01 10:37:12 +01:00
qreal yd = d + minDistance;
rUserYoffset() = yd;
s2.translate(QPointF(0.0, yd));
2018-01-30 21:08:38 +01:00
}
2018-01-30 10:09:46 +01:00
}
2018-01-30 21:08:38 +01:00
s1.add(s2);
2018-01-30 10:09:46 +01:00
}
else {
adjustReadPos();
}
}
2013-02-25 14:56:34 +01:00
//---------------------------------------------------------
// read
//---------------------------------------------------------
void Marker::read(XmlReader& e)
{
Type mt = Type::SEGNO;
2013-02-25 14:56:34 +01:00
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "label") {
QString s(e.readElementText());
setLabel(s);
mt = markerType(s);
}
2017-12-27 11:51:00 +01:00
else if (!TextBase::readProperties(e))
2013-02-25 14:56:34 +01:00
e.unknown();
}
2014-01-16 12:33:23 +01:00
setMarkerType(mt);
2013-02-25 14:56:34 +01:00
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void Marker::write(XmlWriter& xml) const
2013-02-25 14:56:34 +01:00
{
xml.stag(name());
2017-12-27 11:51:00 +01:00
TextBase::writeProperties(xml);
2013-02-25 14:56:34 +01:00
xml.tag("label", _label);
xml.etag();
}
//---------------------------------------------------------
// undoSetLabel
//---------------------------------------------------------
void Marker::undoSetLabel(const QString& s)
{
2016-06-09 09:26:13 +02:00
undoChangeProperty(P_ID::LABEL, s);
2013-02-25 14:56:34 +01:00
}
//---------------------------------------------------------
// undoSetMarkerType
//---------------------------------------------------------
void Marker::undoSetMarkerType(Type t)
2013-02-25 14:56:34 +01:00
{
2016-06-09 09:26:13 +02:00
undoChangeProperty(P_ID::MARKER_TYPE, int(t));
2013-02-25 14:56:34 +01:00
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant Marker::getProperty(P_ID propertyId) const
{
switch (propertyId) {
2014-05-26 18:18:01 +02:00
case P_ID::LABEL:
2013-02-25 14:56:34 +01:00
return label();
2014-05-26 18:18:01 +02:00
case P_ID::MARKER_TYPE:
2013-02-25 14:56:34 +01:00
return int(markerType());
default:
break;
}
2017-12-27 11:51:00 +01:00
return TextBase::getProperty(propertyId);
2013-02-25 14:56:34 +01:00
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool Marker::setProperty(P_ID propertyId, const QVariant& v)
{
switch (propertyId) {
2014-05-26 18:18:01 +02:00
case P_ID::LABEL:
2013-02-25 14:56:34 +01:00
setLabel(v.toString());
break;
2014-05-26 18:18:01 +02:00
case P_ID::MARKER_TYPE:
setMarkerType(Type(v.toInt()));
2013-02-25 14:56:34 +01:00
break;
default:
2017-12-27 11:51:00 +01:00
if (!TextBase::setProperty(propertyId, v))
2013-02-25 14:56:34 +01:00
return false;
break;
}
2016-03-02 13:20:19 +01:00
score()->setLayoutAll();
2013-02-25 14:56:34 +01:00
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant Marker::propertyDefault(P_ID propertyId) const
{
switch (propertyId) {
2014-05-26 18:18:01 +02:00
case P_ID::LABEL:
2013-02-25 14:56:34 +01:00
return QString();
2014-05-26 18:18:01 +02:00
case P_ID::MARKER_TYPE:
return int(Type::FINE);
2018-01-30 10:09:46 +01:00
case P_ID::PLACEMENT:
return int(Placement::ABOVE);
2013-02-25 14:56:34 +01:00
default:
break;
}
2017-12-27 11:51:00 +01:00
return TextBase::propertyDefault(propertyId);
2013-02-25 14:56:34 +01:00
}
//---------------------------------------------------------
// nextSegmentElement
//---------------------------------------------------------
Element* Marker::nextSegmentElement()
{
Segment* seg;
if (markerType() == Marker::Type::FINE) {
seg = measure()->last();
return seg->firstElement(staffIdx());
}
Measure* prevMeasure = measure()->prevMeasureMM();
if (prevMeasure) {
seg = prevMeasure->last();
return seg->firstElement(staffIdx());
}
return Element::nextSegmentElement();
}
//---------------------------------------------------------
// prevSegmentElement
//---------------------------------------------------------
2013-02-25 14:56:34 +01:00
Element* Marker::prevSegmentElement()
{
//it's the same barline
return nextSegmentElement();
}
2013-02-25 14:56:34 +01:00
//---------------------------------------------------------
// accessibleInfo
//---------------------------------------------------------
2016-02-04 17:06:32 +01:00
QString Marker::accessibleInfo() const
{
return QString("%1: %2").arg(Element::accessibleInfo()).arg(markerTypeUserName());
}
2013-05-13 18:49:17 +02:00
}