MuseScore/libmscore/symbol.cpp

314 lines
8.9 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// 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 "symbol.h"
#include "sym.h"
#include "xml.h"
#include "system.h"
#include "staff.h"
#include "measure.h"
#include "page.h"
#include "score.h"
#include "image.h"
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// Symbol
//---------------------------------------------------------
Symbol::Symbol(Score* s)
: BSymbol(s)
{
2012-09-14 10:09:06 +02:00
_sym = sharpSym; // arbitrary valid default
2012-05-26 14:26:10 +02:00
setZ(SYMBOL * 100);
}
Symbol::Symbol(Score* s, SymId sy)
2012-05-26 14:26:10 +02:00
: BSymbol(s)
{
_sym = sy;
setZ(SYMBOL * 100);
}
Symbol::Symbol(const Symbol& s)
: BSymbol(s)
{
_sym = s._sym;
setZ(SYMBOL * 100);
}
//---------------------------------------------------------
// setAbove
//---------------------------------------------------------
void Symbol::setAbove(bool val)
{
setYoff(val ? -2.0 : 7.0);
}
//---------------------------------------------------------
// layout
// height() and width() should return sensible
// values when calling this method
//---------------------------------------------------------
void Symbol::layout()
{
// qreal m = parent() ? parent()->mag() : 1.0;
// if (_small)
// m *= score()->styleD(ST_smallNoteMag);
// setMag(m);
foreach(Element* e, leafs())
e->layout();
ElementLayout::layout(this);
BSymbol::layout();
setbbox(symbols[score()->symIdx()][_sym].bbox(magS()));
}
//---------------------------------------------------------
// Symbol::draw
//---------------------------------------------------------
void Symbol::draw(QPainter* p) const
{
if (type() != NOTEDOT || !staff()->isTabStaff()) {
2012-05-26 14:26:10 +02:00
p->setPen(curColor());
symbols[score()->symIdx()][_sym].draw(p, magS());
}
}
//---------------------------------------------------------
// Symbol::write
//---------------------------------------------------------
void Symbol::write(Xml& xml) const
{
xml.stag(name());
xml.tag("name", Sym::id2name(_sym));
2013-01-23 14:14:09 +01:00
BSymbol::writeProperties(xml);
2012-05-26 14:26:10 +02:00
xml.etag();
}
//---------------------------------------------------------
// Symbol::read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void Symbol::read(XmlReader& e)
2012-05-26 14:26:10 +02:00
{
QPointF pos;
SymId s = noSym;
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 == "name") {
2013-01-11 18:10:18 +01:00
QString val(e.readElementText());
2012-12-18 09:09:11 +01:00
if (val == "acc dot") // compatibility hack
val = "accordion.accDot";
2013-01-28 10:17:15 +01:00
else if (val == "acc old ee")
val = "accordion.accOldEE";
s = Sym::name2id(val);
if (s == noSym) {
// if symbol name not found, fall back to mnames
s = Sym::userName2id(val);
if (s == noSym) {
2012-12-18 09:09:11 +01:00
qDebug("unknown symbol <%s> (%d symbols), falling back to default symbol",
qPrintable(val), symbols[0].size());
// set a default symbol, or layout() will crash
2012-09-19 10:02:47 +02:00
s = s1miHeadSym;
}
2012-05-26 14:26:10 +02:00
}
}
else if (tag == "Symbol") {
Symbol* s = new Symbol(score());
s->read(e);
s->adjustReadPos();
add(s);
}
else if (tag == "Image") {
2013-01-17 12:56:14 +01:00
Image* image = new Image(score());
2012-05-26 14:26:10 +02:00
QString path;
2013-01-17 12:56:14 +01:00
image->read(e);
add(image);
2012-05-26 14:26:10 +02:00
}
2013-06-04 18:29:14 +02:00
else if (tag == "small" || tag == "subtype") // obsolete
e.skipCurrentElement();
2013-01-23 14:14:09 +01:00
else if (!BSymbol::readProperties(e))
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
if (s == noSym)
2012-12-18 09:09:11 +01:00
qDebug("unknown symbol");
2012-05-26 14:26:10 +02:00
setPos(pos);
setSym(s);
}
//---------------------------------------------------------
// dragAnchor
//---------------------------------------------------------
QLineF BSymbol::dragAnchor() const
{
if (parent() && parent()->type() == SEGMENT) {
System* system = segment()->measure()->system();
qreal y = system->staff(staffIdx())->y() + system->y();
QPointF anchor(segment()->pageX(), y);
return QLineF(canvasPos(), anchor);
}
else {
return QLineF(canvasPos(), parent()->canvasPos());
}
}
//---------------------------------------------------------
// pagePos
//---------------------------------------------------------
QPointF BSymbol::pagePos() const
{
if (parent() && (parent()->type() == SEGMENT)) {
QPointF p(pos());
System* system = segment()->measure()->system();
if (system) {
p.ry() += system->staff(staffIdx())->y() + system->y();
}
p.rx() = pageX();
return p;
}
else
return Element::pagePos();
}
//---------------------------------------------------------
// canvasPos
//---------------------------------------------------------
QPointF BSymbol::canvasPos() const
{
if (parent() && (parent()->type() == SEGMENT)) {
QPointF p(pos());
Segment* s = static_cast<Segment*>(parent());
System* system = s->measure()->system();
if (system) {
int si = staffIdx();
p.ry() += system->staff(si)->y() + system->y();
Page* page = system->page();
if (page)
p.ry() += page->y();
}
p.rx() = canvasX();
return p;
}
else
return Element::canvasPos();
}
//---------------------------------------------------------
// FSymbol
//---------------------------------------------------------
FSymbol::FSymbol(Score* s)
2013-01-23 14:14:09 +01:00
: BSymbol(s)
2012-05-26 14:26:10 +02:00
{
_code = 0;
2012-05-26 14:26:10 +02:00
_font.setStyleStrategy(QFont::NoFontMerging);
}
FSymbol::FSymbol(const FSymbol& s)
2013-01-23 14:14:09 +01:00
: BSymbol(s)
2012-05-26 14:26:10 +02:00
{
_font = s._font;
_code = s._code;
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void FSymbol::draw(QPainter* painter) const
{
QString s;
painter->setFont(_font);
if (_code & 0xffff0000) {
s = QChar(QChar::highSurrogate(_code));
s += QChar(QChar::lowSurrogate(_code));
}
else
s = QChar(_code);
painter->drawText(QPointF(0, 0), s);
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void FSymbol::write(Xml& xml) const
{
xml.stag(name());
xml.tag("font", _font.family());
xml.tag("fontsize", _font.pixelSize());
xml.tag("code", _code);
2013-01-23 14:14:09 +01:00
BSymbol::writeProperties(xml);
2012-05-26 14:26:10 +02:00
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void FSymbol::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 == "font")
2013-01-11 18:10:18 +01:00
_font.setFamily(e.readElementText());
2012-05-26 14:26:10 +02:00
else if (tag == "fontsize")
2013-01-11 18:10:18 +01:00
_font.setPixelSize(e.readInt());
2012-05-26 14:26:10 +02:00
else if (tag == "code")
2013-01-11 18:10:18 +01:00
_code = e.readInt();
2013-01-23 14:14:09 +01:00
else if (!BSymbol::readProperties(e))
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
setPos(QPointF());
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void FSymbol::layout()
{
QString s;
if (_code & 0xffff0000) {
s = QChar(QChar::highSurrogate(_code));
s += QChar(QChar::lowSurrogate(_code));
}
else
s = QChar(_code);
QFontMetricsF fm(_font);
setbbox(fm.boundingRect(s));
adjustReadPos();
}
//---------------------------------------------------------
// setFont
//---------------------------------------------------------
void FSymbol::setFont(const QFont& f)
{
_font = f;
_font.setStyleStrategy(QFont::NoFontMerging);
}
2013-05-13 18:49:17 +02:00
}