MuseScore/libmscore/tempotext.cpp

257 lines
8.9 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 "score.h"
#include "tempotext.h"
#include "tempo.h"
#include "system.h"
#include "measure.h"
2014-04-09 16:09:21 +02:00
#include "xml.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
//---------------------------------------------------------
// TempoText
//---------------------------------------------------------
TempoText::TempoText(Score* s)
: Text(s)
{
2012-07-25 11:49:34 +02:00
_tempo = 2.0; // propertyDefault(P_TEMPO).toDouble();
2012-05-26 14:26:10 +02:00
_followText = false;
setPlacement(Element::Placement::ABOVE);
setTextStyleType(TextStyleType::TEMPO);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void TempoText::write(Xml& xml) const
{
xml.stag("Tempo");
xml.tag("tempo", _tempo);
if (_followText)
xml.tag("followText", _followText);
Text::writeProperties(xml);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void TempoText::read(XmlReader& e)
2012-05-26 14:26:10 +02:00
{
2013-01-11 18:10:18 +01:00
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
2012-05-26 14:26:10 +02:00
if (tag == "tempo")
2013-01-11 18:10:18 +01:00
_tempo = e.readDouble();
2012-05-26 14:26:10 +02:00
else if (tag == "followText")
2013-01-11 18:10:18 +01:00
_followText = e.readInt();
2012-05-26 14:26:10 +02:00
else if (!Text::readProperties(e))
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
if (score()->mscVersion() < 119) {
//
// Reset text in old version to
// style.
//
//TODO if (textStyle() != TextStyleType::INVALID) {
2012-05-26 14:26:10 +02:00
// setStyled(true);
// styleChanged();
// }
}
}
//---------------------------------------------------------
// TempoPattern
//---------------------------------------------------------
struct TempoPattern {
const char* pattern;
qreal f;
TDuration d;
TempoPattern(const char* s, qreal v, TDuration::DurationType val, int dots = 0) : pattern(s), f(v), d(val) { d.setDots(dots); }
2012-05-26 14:26:10 +02:00
};
2014-05-04 21:48:03 +02:00
// note: findTempoDuration requires the longer patterns to be before the shorter patterns in tp
static const TempoPattern tp[] = {
TempoPattern("<sym>unicodeNoteHalfUp</sym>\\s*<sym>unicodeAugmentationDot</sym>", 1.5/30.0, TDuration::DurationType::V_HALF, 1), // dotted 1/2
TempoPattern("<sym>unicodeNoteHalfUp</sym><sym>space</sym><sym>unicodeAugmentationDot</sym>", 1.5/30.0, TDuration::DurationType::V_HALF, 1), // dotted 1/2
TempoPattern("<sym>unicodeNoteQuarterUp</sym>\\s*<sym>unicodeAugmentationDot</sym>", 1.5/60.0, TDuration::DurationType::V_QUARTER, 1), // dotted 1/4
TempoPattern("<sym>unicodeNoteQuarterUp</sym><sym>space</sym><sym>unicodeAugmentationDot</sym>", 1.5/60.0, TDuration::DurationType::V_QUARTER, 1), // dotted 1/4
TempoPattern("<sym>unicodeNote8thUp</sym>\\s*<sym>unicodeAugmentationDot</sym>", 1.5/120.0, TDuration::DurationType::V_EIGHTH, 1), // dotted 1/8
TempoPattern("<sym>unicodeNote8thUp</sym><sym>space</sym><sym>unicodeAugmentationDot</sym>", 1.5/120.0, TDuration::DurationType::V_EIGHTH, 1), // dotted 1/8
TempoPattern("<sym>unicodeNoteHalfUp</sym>", 1.0/30.0, TDuration::DurationType::V_HALF), // 1/2
TempoPattern("<sym>unicodeNoteQuarterUp</sym>", 1.0/60.0, TDuration::DurationType::V_QUARTER), // 1/4
TempoPattern("<sym>unicodeNote8thUp</sym>", 1.0/120.0, TDuration::DurationType::V_EIGHTH), // 1/8
};
//---------------------------------------------------------
// findTempoDuration
// find the duration part (note + dot) of a tempo text in string s
// return the match position or -1 if not found
// set len to the match length and dur to the duration value
//---------------------------------------------------------
int TempoText::findTempoDuration(const QString& s, int& len, TDuration& dur)
{
len = 0;
dur = TDuration();
for (unsigned i = 0; i < sizeof(tp)/sizeof(*tp); ++i) {
QRegExp re(tp[i].pattern);
int pos = re.indexIn(s);
if (pos != -1) {
len = re.matchedLength();
dur = tp[i].d;
return pos;
}
}
return -1;
}
//---------------------------------------------------------
// duration2tempoTextString
// find the tempoText string representation for duration
//---------------------------------------------------------
QString TempoText::duration2tempoTextString(const TDuration dur)
{
for (unsigned i = 0; i < sizeof(tp)/sizeof(*tp); ++i) {
if (tp[i].d == dur) {
QString res = tp[i].pattern;
res.remove("\\s*");
return res;
}
}
return "";
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// textChanged
// text may have changed
//---------------------------------------------------------
void TempoText::textChanged()
{
if (!_followText)
return;
2013-02-26 15:50:36 +01:00
QString s = text();
2012-05-26 14:26:10 +02:00
for (unsigned i = 0; i < sizeof(tp)/sizeof(*tp); ++i) {
QRegExp re(QString(tp[i].pattern)+"\\s*=\\s*(\\d+)"); // 1/4
2012-05-26 14:26:10 +02:00
if (re.indexIn(s) != -1) {
QStringList sl = re.capturedTexts();
if (sl.size() == 2) {
qreal nt = qreal(sl[1].toInt()) * tp[i].f;
if (nt != _tempo) {
_tempo = qreal(sl[1].toInt()) * tp[i].f;
if(segment())
score()->setTempo(segment(), _tempo);
2012-05-26 14:26:10 +02:00
score()->setPlaylistDirty(true);
}
break;
}
}
}
}
2012-07-25 11:49:34 +02:00
//---------------------------------------------------------
// undoSetTempo
//---------------------------------------------------------
void TempoText::undoSetTempo(qreal v)
{
2014-05-26 18:18:01 +02:00
score()->undoChangeProperty(this, P_ID::TEMPO, v);
2012-07-25 11:49:34 +02:00
}
//---------------------------------------------------------
// undoSetFollowText
//---------------------------------------------------------
void TempoText::undoSetFollowText(bool v)
{
2014-05-26 18:18:01 +02:00
score()->undoChangeProperty(this, P_ID::TEMPO_FOLLOW_TEXT, v);
2012-07-25 11:49:34 +02:00
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant TempoText::getProperty(P_ID propertyId) const
{
switch(propertyId) {
2014-05-26 18:18:01 +02:00
case P_ID::TEMPO: return _tempo;
case P_ID::TEMPO_FOLLOW_TEXT: return _followText;
2012-07-25 11:49:34 +02:00
default:
return Text::getProperty(propertyId);
2012-07-25 11:49:34 +02:00
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool TempoText::setProperty(P_ID propertyId, const QVariant& v)
{
switch(propertyId) {
2014-05-26 18:18:01 +02:00
case P_ID::TEMPO:
2012-07-25 11:49:34 +02:00
_tempo = v.toDouble();
score()->setTempo(segment(), _tempo);
2012-07-25 11:49:34 +02:00
break;
2014-05-26 18:18:01 +02:00
case P_ID::TEMPO_FOLLOW_TEXT:
2012-07-25 11:49:34 +02:00
_followText = v.toBool();
break;
default:
if (!Text::setProperty(propertyId, v))
2012-07-25 11:49:34 +02:00
return false;
break;
}
score()->setLayoutAll(true);
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant TempoText::propertyDefault(P_ID id) const
{
switch(id) {
2014-05-26 18:18:01 +02:00
case P_ID::TEMPO: return 120;
case P_ID::TEMPO_FOLLOW_TEXT: return false;
case P_ID::PLACEMENT: return int(Element::Placement::ABOVE);
default: return Text::propertyDefault(id);
2012-07-25 11:49:34 +02:00
}
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void TempoText::layout()
{
Text::layout();
if (placement() == Element::Placement::BELOW) {
rypos() = -rypos() + 4 * spatium();
// rUserYoffset() *= -1;
// text height ?
}
}
2012-07-25 11:49:34 +02:00
2013-05-13 18:49:17 +02:00
}