MuseScore/libmscore/tremolobar.cpp

272 lines
8.2 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2010-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 "tremolobar.h"
#include "score.h"
#include "undo.h"
#include "staff.h"
#include "chord.h"
#include "note.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
//---------------------------------------------------------
// TremoloBar
//---------------------------------------------------------
TremoloBar::TremoloBar(Score* s)
: Element(s)
{
setFlags(ElementFlag::MOVABLE | ElementFlag::SELECTABLE | ElementFlag::ON_STAFF);
2016-09-20 17:13:54 +02:00
setLineWidth(score()->styleS(StyleIdx::tremoloBarLineWidth));
lineWidthStyle = PropertyStyle::STYLED;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void TremoloBar::layout()
{
qreal _spatium = spatium();
2016-09-20 17:13:54 +02:00
setPos(0.0, -_spatium * 3.0);
2016-09-20 14:44:43 +02:00
/* we place the tremolo bars starting slightly before the
* notehead, and end it slightly after, drawing above the
* note. The values specified in Guitar Pro are very large, too
* large for the scale used in Musescore. We used the
* timeFactor and pitchFactor below to reduce these values down
* consistently to values that make sense to draw with the
* Musescore scale. */
2016-09-20 17:13:54 +02:00
qreal timeFactor = _userMag / 1.0;
qreal pitchFactor = -_spatium * .02;
2016-09-20 14:44:43 +02:00
2016-09-20 17:13:54 +02:00
polygon.clear();
2016-09-20 14:44:43 +02:00
for (auto v : _points)
2016-09-20 17:13:54 +02:00
polygon << QPointF(v.time * timeFactor, v.pitch * pitchFactor);
2016-09-20 14:44:43 +02:00
2016-09-20 17:13:54 +02:00
qreal w = _lw.val() * _spatium;
setbbox(polygon.boundingRect().adjusted(-w, -w, w, w));
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void TremoloBar::draw(QPainter* painter) const
{
2016-09-20 17:13:54 +02:00
QPen pen(curColor(), _lw.val() * spatium(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
2012-05-26 14:26:10 +02:00
painter->setPen(pen);
2016-09-20 17:13:54 +02:00
painter->drawPolyline(polygon);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void TremoloBar::write(Xml& xml) const
{
xml.stag("TremoloBar");
writeProperty(xml, P_ID::MAG);
2016-09-20 17:13:54 +02:00
writeProperty(xml, P_ID::LINE_WIDTH);
writeProperty(xml, P_ID::PLAY);
for (const PitchValue& v : _points) {
2012-05-26 14:26:10 +02:00
xml.tagE(QString("point time=\"%1\" pitch=\"%2\" vibrato=\"%3\"")
.arg(v.time).arg(v.pitch).arg(v.vibrato));
}
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void TremoloBar::read(XmlReader& e)
2012-05-26 14:26:10 +02:00
{
2013-01-11 18:10:18 +01:00
while (e.readNextStartElement()) {
2016-09-20 17:13:54 +02:00
auto tag = e.name();
if (tag == "point") {
2012-05-26 14:26:10 +02:00
PitchValue pv;
2013-01-11 18:10:18 +01:00
pv.time = e.intAttribute("time");
pv.pitch = e.intAttribute("pitch");
pv.vibrato = e.intAttribute("vibrato");
2012-05-26 14:26:10 +02:00
_points.append(pv);
e.readNext();
2012-05-26 14:26:10 +02:00
}
2016-09-20 17:13:54 +02:00
else if (tag == "mag")
_userMag = e.readDouble(0.1, 10.0);
2016-09-20 17:13:54 +02:00
else if (tag == "lineWidth")
setLineWidth(Spatium(e.readDouble()));
else if (tag == "play")
setPlay(e.readInt());
2012-05-26 14:26:10 +02:00
else
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
}
//---------------------------------------------------------
// undoSetUserMag
//---------------------------------------------------------
void TremoloBar::undoSetUserMag(qreal val)
{
2016-06-09 09:26:13 +02:00
undoChangeProperty(P_ID::MAG, val);
}
2016-09-20 17:13:54 +02:00
//---------------------------------------------------------
// undoSetLineWidth
//---------------------------------------------------------
void TremoloBar::undoSetLineWidth(Spatium val)
{
undoChangeProperty(P_ID::LINE_WIDTH, val);
}
//---------------------------------------------------------
// undoSetPlay
//---------------------------------------------------------
void TremoloBar::undoSetPlay(bool val)
{
undoChangeProperty(P_ID::PLAY, val);
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant TremoloBar::getProperty(P_ID propertyId) const
{
switch (propertyId) {
2016-09-20 17:13:54 +02:00
case P_ID::LINE_WIDTH:
return lineWidth();
case P_ID::MAG:
return userMag();
case P_ID::PLAY:
return play();
default:
return Element::getProperty(propertyId);
}
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant TremoloBar::propertyDefault(P_ID propertyId) const
{
switch (propertyId) {
2016-09-20 17:13:54 +02:00
case P_ID::LINE_WIDTH:
return score()->style(StyleIdx::voltaLineWidth);
case P_ID::MAG:
return 1.0;
case P_ID::PLAY:
return true;
default:
return Element::propertyDefault(propertyId);
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool TremoloBar::setProperty(P_ID propertyId, const QVariant& v)
{
switch (propertyId) {
2016-09-20 17:13:54 +02:00
case P_ID::LINE_WIDTH:
lineWidthStyle = PropertyStyle::UNSTYLED;
setLineWidth(v.value<Spatium>());
break;
case P_ID::MAG:
setUserMag(v.toDouble());
break;
2016-09-20 17:13:54 +02:00
case P_ID::PLAY:
setPlay(v.toBool());
score()->setPlaylistDirty();
break;
default:
return Element::setProperty(propertyId, v);
}
2016-03-02 13:20:19 +01:00
score()->setLayoutAll();
return true;
}
2016-09-20 17:13:54 +02:00
//---------------------------------------------------------
// propertyStyle
//---------------------------------------------------------
PropertyStyle TremoloBar::propertyStyle(P_ID id) const
{
switch (id) {
case P_ID::LINE_WIDTH:
return lineWidthStyle;
default:
return Element::propertyStyle(id);
}
}
//---------------------------------------------------------
// resetProperty
//---------------------------------------------------------
void TremoloBar::resetProperty(P_ID id)
{
switch (id) {
case P_ID::LINE_WIDTH:
setProperty(id, propertyDefault(id));
lineWidthStyle = PropertyStyle::STYLED;
break;
default:
return Element::resetProperty(id);
}
}
//---------------------------------------------------------
// styleChanged
// reset all styled values to actual style
//---------------------------------------------------------
void TremoloBar::styleChanged()
{
if (lineWidthStyle == PropertyStyle::STYLED)
setLineWidth(score()->styleS(StyleIdx::voltaLineWidth));
}
//---------------------------------------------------------
// reset
//---------------------------------------------------------
void TremoloBar::reset()
{
if (lineWidthStyle == PropertyStyle::UNSTYLED)
undoChangeProperty(P_ID::LINE_WIDTH, propertyDefault(P_ID::LINE_WIDTH), PropertyStyle::STYLED);
Element::reset();
}
//---------------------------------------------------------
// spatiumChanged
//---------------------------------------------------------
void TremoloBar::spatiumChanged(qreal oldValue, qreal newValue)
{
_lw *= (newValue / oldValue);
Element::spatiumChanged(oldValue, newValue);
}
2013-05-13 18:49:17 +02:00
}