MuseScore/libmscore/hairpin.cpp

259 lines
7.8 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
// $Id: hairpin.cpp 5305 2012-02-09 12:09:20Z wschweer $
//
// Copyright (C) 2002-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 "hairpin.h"
#include "style.h"
#include "xml.h"
#include "utils.h"
#include "score.h"
#include "measure.h"
#include "segment.h"
#include "system.h"
#include "undo.h"
#include "staff.h"
#include "mscore.h"
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void HairpinSegment::layout()
{
QTransform t;
qreal _spatium = spatium();
qreal h1 = score()->styleS(ST_hairpinHeight).val() * _spatium * .5;
qreal h2 = score()->styleS(ST_hairpinContHeight).val() * _spatium * .5;
qreal len;
qreal x = pos2().x();
if (x < _spatium) // minimum size of hairpin
x = _spatium;
qreal y = pos2().y();
len = sqrt(x * x + y * y);
t.rotateRadians(asin(y/len));
if (hairpin()->subtype() == 0) {
// crescendo
switch (subtype()) {
case SEGMENT_SINGLE:
case SEGMENT_BEGIN:
2012-08-23 17:40:04 +02:00
l1.setLine(.0, .0, len, h1);
l2.setLine(.0, .0, len, - h1);
2012-05-26 14:26:10 +02:00
break;
case SEGMENT_MIDDLE:
case SEGMENT_END:
2012-08-23 17:40:04 +02:00
l1.setLine(.0, h2, len, h1);
l2.setLine(.0, -h2, len, - h1);
2012-05-26 14:26:10 +02:00
break;
}
}
else {
// decrescendo
switch(subtype()) {
case SEGMENT_SINGLE:
case SEGMENT_END:
2012-08-23 17:40:04 +02:00
l1.setLine(.0, h1, len, 0.0);
l2.setLine(.0, -h1, len, 0.0);
2012-05-26 14:26:10 +02:00
break;
case SEGMENT_BEGIN:
case SEGMENT_MIDDLE:
2012-08-23 17:40:04 +02:00
l1.setLine(.0, h1, len, + h2);
l2.setLine(.0, -h1, len, - h2);
2012-05-26 14:26:10 +02:00
break;
}
}
l1 = t.map(l1);
l2 = t.map(l2);
QRectF r = QRectF(l1.p1(), l1.p2()).normalized() | QRectF(l2.p1(), l2.p2()).normalized();
qreal w = point(score()->styleS(ST_hairpinWidth));
setbbox(r.adjusted(-w*.5, -w*.5, w, w));
2012-11-08 12:59:30 +01:00
if (parent())
rypos() += score()->styleS(ST_hairpinY).val() * _spatium;
2012-10-27 14:46:35 +02:00
adjustReadPos();
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void HairpinSegment::draw(QPainter* painter) const
{
QPen pen(curColor(), point(score()->styleS(ST_hairpinWidth)));
painter->setPen(pen);
2012-08-23 17:40:04 +02:00
painter->drawLine(l1);
painter->drawLine(l2);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// Hairpin
//---------------------------------------------------------
Hairpin::Hairpin(Score* s)
: SLine(s)
{
_subtype = CRESCENDO;
_veloChange = 10;
_dynRange = DYNAMIC_PART;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// layout
// compute segments from tick() to _tick2
//---------------------------------------------------------
void Hairpin::layout()
{
setPos(0.0, 0.0);
SLine::layout();
}
//---------------------------------------------------------
// createLineSegment
//---------------------------------------------------------
LineSegment* Hairpin::createLineSegment()
{
return new HairpinSegment(score());
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void Hairpin::write(Xml& xml) const
{
xml.stag(QString("%1 id=\"%2\"").arg(name()).arg(id()));
xml.tag("subtype", _subtype);
xml.tag("veloChange", _veloChange);
2012-10-31 11:46:58 +01:00
writeProperty(xml, P_DYNAMIC_RANGE);
writeProperty(xml, P_PLACEMENT);
2012-05-26 14:26:10 +02:00
SLine::writeProperties(xml);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void Hairpin::read(const QDomElement& de)
{
foreach(SpannerSegment* seg, spannerSegments())
delete seg;
spannerSegments().clear();
setId(de.attribute("id", "-1").toInt());
for (QDomElement e = de.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
const QString& tag(e.tagName());
const QString& val(e.text());
if (tag == "subtype")
_subtype = HairpinType(val.toInt());
2012-05-26 14:26:10 +02:00
else if (tag == "veloChange")
_veloChange = val.toInt();
else if (tag == "dynType")
_dynRange = DynamicRange(val.toInt());
2012-05-26 14:26:10 +02:00
else if (!SLine::readProperties(e))
domError(e);
}
}
//---------------------------------------------------------
// undoSetSubtype
//---------------------------------------------------------
void Hairpin::undoSetSubtype(HairpinType val)
{
score()->undoChangeProperty(this, P_HAIRPIN_TYPE, val);
}
//---------------------------------------------------------
// undoSetVeloChange
//---------------------------------------------------------
void Hairpin::undoSetVeloChange(int val)
{
score()->undoChangeProperty(this, P_VELO_CHANGE, val);
}
//---------------------------------------------------------
// undoSetDynType
//---------------------------------------------------------
void Hairpin::undoSetDynRange(DynamicRange val)
{
score()->undoChangeProperty(this, P_DYNAMIC_RANGE, val);
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
2012-08-10 17:01:35 +02:00
QVariant Hairpin::getProperty(P_ID id) const
2012-05-26 14:26:10 +02:00
{
switch(id) {
case P_HAIRPIN_TYPE:
return _subtype;
case P_VELO_CHANGE:
return _veloChange;
case P_DYNAMIC_RANGE:
return _dynRange;
default:
return SLine::getProperty(id);
}
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
2012-08-10 17:01:35 +02:00
bool Hairpin::setProperty(P_ID id, const QVariant& v)
2012-05-26 14:26:10 +02:00
{
switch(id) {
case P_HAIRPIN_TYPE:
_subtype = HairpinType(v.toInt());
setGenerated(false);
break;
case P_VELO_CHANGE:
_veloChange = v.toInt();
break;
case P_DYNAMIC_RANGE:
_dynRange = DynamicRange(v.toInt());
break;
default:
return SLine::setProperty(id, v);
2012-05-26 14:26:10 +02:00
}
return true;
2012-05-26 14:26:10 +02:00
}
2012-10-27 14:46:35 +02:00
//---------------------------------------------------------
// setYoff
//---------------------------------------------------------
void Hairpin::setYoff(qreal val)
{
rUserYoffset() += (val - score()->styleS(ST_hairpinY).val()) * spatium();
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant Hairpin::propertyDefault(P_ID id) const
{
switch(id) {
2012-10-31 11:46:58 +01:00
case P_DYNAMIC_RANGE: return DYNAMIC_PART;
default: return SLine::propertyDefault(id);
}
}
2012-10-27 14:46:35 +02:00