1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
//=============================================================================
// 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 "bend.h"
#include "score.h"
#include "undo.h"
#include "staff.h"
#include "chord.h"
#include "note.h"
#include "xml.h"
namespace Ms {
//---------------------------------------------------------
// label
//---------------------------------------------------------
static const char* label[] = {
"", "1/4", "1/2", "3/4", "full",
"1 1/4", "1 1/2", "1 3/4", "2",
"2 1/4", "2 1/2", "2 3/4", "3"
};
static const ElementStyle bendStyle {
{ Sid::bendFontSize, Pid::FONT_SIZE },
{ Sid::bendFontStyle, Pid::FONT_STYLE },
{ Sid::bendLineWidth, Pid::LINE_WIDTH },
};
//---------------------------------------------------------
// Bend
//---------------------------------------------------------
Bend::Bend(Score* s)
: Element(s, ElementFlag::MOVABLE)
{
initElementStyle(&bendStyle);
}
//---------------------------------------------------------
// font
//---------------------------------------------------------
QFont Bend::font(qreal sp) const
{
QFont f(_fontFace);
f.setBold(_fontStyle & FontStyle::Bold);
f.setItalic(_fontStyle & FontStyle::Italic);
f.setUnderline(_fontStyle & FontStyle::Underline);
qreal m = _fontSize;
m *= sp / SPATIUM20;
f.setPointSizeF(m);
return f;
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void Bend::layout()
{
// during mtest, there may be no score. If so, exit.
if (!score())
return;
qreal _spatium = spatium();
if (staff() && !staff()->isTabStaff(tick())) {
if (!parent()) {
noteWidth = -_spatium*2;
notePos = QPointF(0.0, _spatium*3);
}
}
qreal _lw = _lineWidth;
Note* note = toNote(parent());
if (note == 0) {
noteWidth = 0.0;
notePos = QPointF();
}
else {
notePos = note->pos();
noteWidth = note->width();
}
QRectF bb;
QFontMetricsF fm(font(_spatium), MScore::paintDevice());
int n = _points.size();
qreal x = noteWidth;
qreal y = -_spatium * .8;
qreal x2, y2;
qreal aw = _spatium * .5;
QPolygonF arrowUp;
arrowUp << QPointF(0, 0) << QPointF(aw*.5, aw) << QPointF(-aw*.5, aw);
QPolygonF arrowDown;
arrowDown << QPointF(0, 0) << QPointF(aw*.5, -aw) << QPointF(-aw*.5, -aw);
for (int pt = 0; pt < n; ++pt) {
if (pt == (n-1))
break;
int pitch = _points[pt].pitch;
if (pt == 0 && pitch) {
y2 = -notePos.y() -_spatium * 2;
x2 = x;
bb |= QRectF(x, y, x2-x, y2-y);
bb |= arrowUp.translated(x2, y2 + _spatium * .2).boundingRect();
int idx = (pitch + 12)/25;
const char* l = label[idx];
bb |= fm.boundingRect(QRectF(x2, y2, 0, 0),
Qt::AlignHCenter | Qt::AlignBottom | Qt::TextDontClip, QString(l));
y = y2;
}
if (pitch == _points[pt+1].pitch) {
if (pt == (n-2))
break;
x2 = x + _spatium;
y2 = y;
bb |= QRectF(x, y, x2-x, y2-y);
}
else if (pitch < _points[pt+1].pitch) {
// up
x2 = x + _spatium*.5;
y2 = -notePos.y() -_spatium * 2;
qreal dx = x2 - x;
qreal dy = y2 - y;
QPainterPath path;
path.moveTo(x, y);
path.cubicTo(x+dx/2, y, x2, y+dy/4, x2, y2);
bb |= path.boundingRect();
bb |= arrowUp.translated(x2, y2 + _spatium * .2).boundingRect();
int idx = (_points[pt+1].pitch + 12)/25;
const char* l = label[idx];
QRectF r;
bb |= fm.boundingRect(QRectF(x2, y2, 0, 0),
Qt::AlignHCenter | Qt::AlignBottom | Qt::TextDontClip, QString(l));
}
else {
// down
x2 = x + _spatium*.5;
y2 = y + _spatium * 3;
qreal dx = x2 - x;
qreal dy = y2 - y;
QPainterPath path;
path.moveTo(x, y);
path.cubicTo(x+dx/2, y, x2, y+dy/4, x2, y2);
bb |= path.boundingRect();
bb |= arrowDown.translated(x2, y2 - _spatium * .2).boundingRect();
}
x = x2;
y = y2;
}
bb.adjust(-_lw, -_lw, _lw, _lw);
setbbox(bb);
setPos(0.0, 0.0);
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void Bend::draw(QPainter* painter) const
{
qreal _spatium = spatium();
qreal _lw = _lineWidth;
QPen pen(curColor(), _lw, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter->setPen(pen);
painter->setBrush(QBrush(curColor()));
QFont f = font(_spatium * MScore::pixelRatio);
painter->setFont(f);
QFontMetrics fm(f, MScore::paintDevice());
qreal x = noteWidth + _spatium * .2;
qreal y = -_spatium * .8;
qreal x2, y2;
qreal aw = score()->styleP(Sid::bendArrowWidth);
QPolygonF arrowUp;
arrowUp << QPointF(0, 0) << QPointF(aw * .5, aw) << QPointF(-aw *.5, aw);
QPolygonF arrowDown;
arrowDown << QPointF(0, 0) << QPointF(aw * .5, -aw) << QPointF(-aw *.5, -aw);
int n = _points.size();
for (int pt = 0; pt < n-1; ++pt) {
int pitch = _points[pt].pitch;
if (pt == 0 && pitch) {
y2 = -notePos.y() -_spatium * 2;
x2 = x;
painter->drawLine(QLineF(x, y, x2, y2));
painter->setBrush(curColor());
painter->drawPolygon(arrowUp.translated(x2, y2));
int idx = (pitch + 12)/25;
const char* l = label[idx];
QString s(l);
qreal textWidth = fm.width(s);
qreal textHeight = fm.height();
painter->drawText(QRectF(x2 - textWidth / 2, y2 - textHeight / 2, .0, .0), Qt::AlignVCenter|Qt::TextDontClip, s);
y = y2;
}
if (pitch == _points[pt+1].pitch) {
if (pt == (n-2))
break;
x2 = x + _spatium;
y2 = y;
painter->drawLine(QLineF(x, y, x2, y2));
}
else if (pitch < _points[pt+1].pitch) {
// up
x2 = x + _spatium*.5;
y2 = -notePos.y() -_spatium * 2;
qreal dx = x2 - x;
qreal dy = y2 - y;
QPainterPath path;
path.moveTo(x, y);
path.cubicTo(x+dx/2, y, x2, y+dy/4, x2, y2);
painter->setBrush(Qt::NoBrush);
painter->drawPath(path);
painter->setBrush(curColor());
painter->drawPolygon(arrowUp.translated(x2, y2 ));
int idx = (_points[pt+1].pitch + 12)/25;
const char* l = label[idx];
qreal ty = y2; // - _spatium;
painter->drawText(QRectF(x2, ty, .0, .0),
Qt::AlignHCenter | Qt::AlignBottom | Qt::TextDontClip, QString(l));
}
else {
// down
x2 = x + _spatium*.5;
y2 = y + _spatium * 3;
qreal dx = x2 - x;
qreal dy = y2 - y;
QPainterPath path;
path.moveTo(x, y);
path.cubicTo(x+dx/2, y, x2, y+dy/4, x2, y2);
painter->setBrush(Qt::NoBrush);
painter->drawPath(path);
painter->setBrush(curColor());
painter->drawPolygon(arrowDown.translated(x2, y2));
}
x = x2;
y = y2;
}
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void Bend::write(XmlWriter& xml) const
{
xml.stag(this);
for (const PitchValue& v : _points) {
xml.tagE(QString("point time=\"%1\" pitch=\"%2\" vibrato=\"%3\"")
.arg(v.time).arg(v.pitch).arg(v.vibrato));
}
writeStyledProperties(xml);
writeProperty(xml, Pid::PLAY);
Element::writeProperties(xml);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void Bend::read(XmlReader& e)
{
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (readStyledProperty(e, tag))
;
else if (tag == "point") {
PitchValue pv;
pv.time = e.intAttribute("time");
pv.pitch = e.intAttribute("pitch");
pv.vibrato = e.intAttribute("vibrato");
_points.append(pv);
e.readNext();
}
else if (tag == "play")
setPlayBend(e.readBool());
else if (!Element::readProperties(e))
e.unknown();
}
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant Bend::getProperty(Pid id) const
{
switch (id) {
case Pid::FONT_FACE:
return _fontFace;
case Pid::FONT_SIZE:
return _fontSize;
case Pid::FONT_STYLE:
return int(_fontStyle);
case Pid::PLAY:
return bool(playBend());
case Pid::LINE_WIDTH:
return _lineWidth;
default:
return Element::getProperty(id);
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool Bend::setProperty(Pid id, const QVariant& v)
{
switch (id) {
case Pid::FONT_FACE:
_fontFace = v.toString();
break;
case Pid::FONT_SIZE:
_fontSize = v.toReal();
break;
case Pid::FONT_STYLE:
_fontStyle = FontStyle(v.toInt());
break;
case Pid::PLAY:
setPlayBend(v.toBool());
break;
case Pid::LINE_WIDTH:
_lineWidth = v.toReal();
break;
default:
return Element::setProperty(id, v);
}
triggerLayout();
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant Bend::propertyDefault(Pid id) const
{
switch (id) {
case Pid::PLAY:
return true;
default:
return Element::propertyDefault(id);
}
}
}
|