MuseScore/libmscore/spacer.cpp

255 lines
6.9 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 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 "spacer.h"
#include "score.h"
#include "mscore.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
//---------------------------------------------------------
// LayoutBreak
//---------------------------------------------------------
Spacer::Spacer(Score* score)
: Element(score)
{
_spacerType = SpacerType::UP;
2012-05-26 14:26:10 +02:00
_gap = 0.0;
}
Spacer::Spacer(const Spacer& s)
: Element(s)
{
2016-10-12 20:06:46 +02:00
_gap = s._gap;
path = s.path;
_spacerType = s._spacerType;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void Spacer::draw(QPainter* painter) const
{
if (score()->printing() || !score()->showUnprintable())
return;
QPen pen(selected() ? MScore::selectColor[0] : MScore::layoutBreakColor,
2013-05-15 14:37:24 +02:00
spatium() * 0.3);
2012-05-26 14:26:10 +02:00
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
painter->drawPath(path);
}
//---------------------------------------------------------
// layout0
//---------------------------------------------------------
void Spacer::layout0()
{
qreal _spatium = spatium();
path = QPainterPath();
qreal w = _spatium;
qreal b = w * .5;
qreal h = _gap;
2016-10-12 20:06:46 +02:00
switch (spacerType()) {
case SpacerType::DOWN:
path.lineTo(w, 0.0);
path.moveTo(b, 0.0);
path.lineTo(b, h);
path.lineTo(0.0, h-b);
path.moveTo(b, h);
path.lineTo(w, h-b);
break;
case SpacerType::UP:
path.moveTo(b, 0.0);
path.lineTo(0.0, b);
path.moveTo(b, 0.0);
path.lineTo(w, b);
path.moveTo(b, 0.0);
path.lineTo(b, h);
path.moveTo(0.0, h);
path.lineTo(w, h);
break;
case SpacerType::FIXED:
path.lineTo(w, 0.0);
path.moveTo(b, 0.0);
path.lineTo(b, h);
path.moveTo(0.0, h);
path.lineTo(w, h);
break;
2012-05-26 14:26:10 +02:00
}
qreal lw = _spatium * 0.4;
QRectF bb(0, 0, w, h);
bb.adjust(-lw, -lw, lw, lw);
setbbox(bb);
}
//---------------------------------------------------------
// setGap
//---------------------------------------------------------
void Spacer::setGap(qreal sp)
{
_gap = sp;
layout0();
}
//---------------------------------------------------------
// spatiumChanged
//---------------------------------------------------------
void Spacer::spatiumChanged(qreal ov, qreal nv)
{
_gap = (_gap / ov) * nv;
layout0();
}
2017-03-31 13:03:15 +02:00
//---------------------------------------------------------
// startEdit
//---------------------------------------------------------
void Spacer::startEdit(EditData& ed)
{
ed.grips = 1;
ed.curGrip = Grip::START;
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// editDrag
//---------------------------------------------------------
2017-03-31 13:03:15 +02:00
void Spacer::editDrag(EditData& ed)
2012-05-26 14:26:10 +02:00
{
qreal s = ed.delta.y();
2016-10-12 20:06:46 +02:00
switch (spacerType()) {
case SpacerType::DOWN:
case SpacerType::FIXED:
_gap += s;
break;
case SpacerType::UP:
_gap -= s;
break;
}
2012-05-26 14:26:10 +02:00
if (_gap < spatium() * 2.0)
_gap = spatium() * 2;
layout0();
2016-03-02 13:20:19 +01:00
score()->setLayoutAll();
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// updateGrips
//---------------------------------------------------------
2017-03-31 13:03:15 +02:00
void Spacer::updateGrips(EditData& ed) const
2012-05-26 14:26:10 +02:00
{
qreal _spatium = spatium();
QPointF p;
2016-10-12 20:06:46 +02:00
switch (spacerType()) {
case SpacerType::DOWN:
case SpacerType::FIXED:
p = QPointF(_spatium * .5, _gap);
break;
case SpacerType::UP:
p = QPointF(_spatium * .5, 0.0);
break;
}
2017-03-31 13:03:15 +02:00
ed.grip[0].translate(pagePos() + p);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void Spacer::write(XmlWriter& xml) const
2012-05-26 14:26:10 +02:00
{
xml.stag(name());
xml.tag("subtype", int(_spacerType));
2012-05-26 14:26:10 +02:00
Element::writeProperties(xml);
xml.tag("space", _gap / spatium());
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void Spacer::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());
if (tag == "subtype")
_spacerType = SpacerType(e.readInt());
2013-01-11 18:10:18 +01:00
else if (tag == "space")
_gap = e.readDouble() * spatium();
2012-05-26 14:26:10 +02:00
else if (!Element::readProperties(e))
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
layout0();
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant Spacer::getProperty(P_ID propertyId) const
{
2016-10-12 20:06:46 +02:00
switch (propertyId) {
case P_ID::SPACE:
return gap();
2012-05-26 14:26:10 +02:00
default:
return Element::getProperty(propertyId);
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool Spacer::setProperty(P_ID propertyId, const QVariant& v)
{
2016-10-12 20:06:46 +02:00
switch (propertyId) {
2014-05-26 18:18:01 +02:00
case P_ID::SPACE:
2012-05-26 14:26:10 +02:00
setGap(v.toDouble());
break;
default:
if (!Element::setProperty(propertyId, v))
return false;
break;
}
layout0();
2016-03-02 13:20:19 +01:00
score()->setLayoutAll();
2012-05-26 14:26:10 +02:00
setGenerated(false);
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant Spacer::propertyDefault(P_ID id) const
{
2016-10-12 20:06:46 +02:00
switch (id) {
case P_ID::SPACE:
return QVariant(0.0);
2012-05-26 14:26:10 +02:00
default:
return Element::propertyDefault(id);
}
}
2013-05-13 18:49:17 +02:00
}