Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #298126: added the ability to rotate and offset instrument names #5868

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions libmscore/part.cpp
Expand Up @@ -125,6 +125,10 @@ bool Part::readProperties(XmlReader& e)
else if (tag == "preferSharpFlat")
_preferSharpFlat =
e.readElementText() == "sharps" ? PreferSharpFlat::SHARPS : PreferSharpFlat::FLATS;
else if (tag == "instrumentNameRotation")
_instrumentNameRotation = e.readInt();
else if (tag == "instrumentNameOffset")
_instrumentNameOffset = e.readPoint();
else
return false;
return true;
Expand Down Expand Up @@ -162,6 +166,8 @@ void Part::write(XmlWriter& xml) const
xml.tag("preferSharpFlat",
_preferSharpFlat == PreferSharpFlat::SHARPS ? "sharps" : "flats");
instrument()->write(xml, this);
xml.tag("instrumentNameRotation", _instrumentNameRotation, 0);
xml.tag("instrumentNameOffset", _instrumentNameOffset, QPointF(0, 0));
xml.etag();
}

Expand Down
6 changes: 6 additions & 0 deletions libmscore/part.h
Expand Up @@ -55,6 +55,8 @@ enum class PreferSharpFlat : char {
class Part final : public ScoreElement {
QString _partName; ///< used in tracklist (mixer)
InstrumentList _instruments;
int _instrumentNameRotation { 0 };
QPointF _instrumentNameOffset {0, 0};
QList<Staff*> _staves;
QString _id; ///< used for MusicXml import
bool _show; ///< show part in partitur if true
Expand Down Expand Up @@ -100,6 +102,10 @@ class Part final : public ScoreElement {
void setPlainLongName(const QString& s);
void setPlainShortName(const QString& s);

int instrumentNameRotation() const { return _instrumentNameRotation; }
void setInstrumentNameRotation(int r) { _instrumentNameRotation = r; }
QPointF instrumentNameOffset() const { return _instrumentNameOffset; }
void setInstrumentNameOffset(int x, int y) { _instrumentNameOffset.setX(x); _instrumentNameOffset.setY(y); }
void setStaves(int);

int midiProgram() const;
Expand Down
2 changes: 2 additions & 0 deletions libmscore/system.cpp
Expand Up @@ -785,6 +785,8 @@ void System::setInstrumentNames(bool longName, Fraction tick)
iname->setTrack(staffIdx * VOICES);
iname->setInstrumentNameType(longName ? InstrumentNameType::LONG : InstrumentNameType::SHORT);
iname->setLayoutPos(sn.pos());
iname->setRotation(part->instrumentNameRotation());
iname->setOffsets(part->instrumentNameOffset().x(), part->instrumentNameOffset().y());
score()->addElement(iname);
}
iname->setXmlText(sn.name());
Expand Down
62 changes: 61 additions & 1 deletion libmscore/textbase.cpp
Expand Up @@ -1446,10 +1446,12 @@ void TextBase::layout1()
t.setY(t.y() + yoff);

bb.translate(0.0, yoff);

_basebbox = bb;
setbbox(bb);
if (hasFrame())
layoutFrame();
applyRotation();
applyOffsets();
score()->addRefresh(canvasBoundingRect());
}

Expand Down Expand Up @@ -1504,6 +1506,7 @@ void TextBase::layoutFrame()
frame.adjust(-w, -w, w, w);
w = frameWidth().val() * _spatium;
setbbox(frame.adjusted(-w, -w, w, w));
_basebbox = Element::bbox();
}

//---------------------------------------------------------
Expand Down Expand Up @@ -2609,6 +2612,59 @@ TextCursor* TextBase::cursor(const EditData& ed)
return &ted->cursor;
}

//---------------------------------------------------------
// applyRotation
//---------------------------------------------------------

void TextBase::applyRotation()
{
//
// because the bounding box is rotated by resizing the rectangle and the QPainter is rotated normally (TextBase::draw(Qpainter*)),
// there is small offset between the text and the bounding box of the text, that's where offsetCorrection comes in
//
qreal offsetCorrection = (_basebbox.height() / 2) * abs(sin(qDegreesToRadians(static_cast<double>(_rotation))));
if(_rotation < 0)
Element::bbox().setCoords(_basebbox.x() + ((_basebbox.width() - _basebbox.height()) * abs(sin(qDegreesToRadians(static_cast<double>(_rotation))))),
_basebbox.y(),
_basebbox.x() + _basebbox.width() + offsetCorrection,
_basebbox.y() + _basebbox.height() + ((_basebbox.width() - _basebbox.height()) * -sin(qDegreesToRadians(static_cast<double>(_rotation)))));
else if(_rotation > 0)
Element::bbox().setCoords(_basebbox.x() + ((_basebbox.width() - _basebbox.height()) * abs(sin(qDegreesToRadians(static_cast<double>(_rotation))))),
_basebbox.y() - ((_basebbox.width() - _basebbox.height()) * sin(qDegreesToRadians(static_cast<double>(_rotation)))),
_basebbox.x() + _basebbox.width() + offsetCorrection,
_basebbox.y() + _basebbox.height());
}

//---------------------------------------------------------
// setRotation
//---------------------------------------------------------

void TextBase::setRotation(int r)
{
_rotation = r;
applyRotation();
}

//---------------------------------------------------------
// applyOffsets
//---------------------------------------------------------

void TextBase::applyOffsets()
{
Element::bbox().translate(_offset.x(), _offset.y());
}

//---------------------------------------------------------
// setOffsets
//---------------------------------------------------------

void TextBase::setOffsets(int x, int y)
{
_offset.setX(x);
_offset.setY(y);
applyOffsets();
}

//---------------------------------------------------------
// draw
//---------------------------------------------------------
Expand Down Expand Up @@ -2642,8 +2698,12 @@ void TextBase::draw(QPainter* p) const
}
p->setBrush(Qt::NoBrush);
p->setPen(textColor());
p->translate(_offset.x(), _offset.y());
p->rotate(_rotation);
for (const TextBlock& t : _layout)
t.draw(p, this);
p->rotate(-_rotation);
p->translate(-_offset.x(), -_offset.y());
}

//---------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions libmscore/textbase.h
Expand Up @@ -243,11 +243,17 @@ class TextBase : public Element {
int hexState { -1 };
bool _primed { 0 };

QRectF _basebbox; // used to rotate and offset the text
QPointF _offset { 0, 0 };
int _rotation { 0 };

void drawSelection(QPainter*, const QRectF&) const;
void insert(TextCursor*, uint code);
void genText() const;
virtual int getPropertyFlagsIdx(Pid id) const override;
QString stripText(bool, bool, bool) const;
void applyRotation();
void applyOffsets();

protected:
QColor textColor() const;
Expand Down Expand Up @@ -359,6 +365,11 @@ class TextBase : public Element {
QList<TextBlock>& textBlockList() { return _layout; }
int rows() const { return _layout.size(); }

void setRotation(int r);
int rotation() const { return _rotation; }
void setOffsets(int x, int y);
QPointF offset() const { return _offset; }

void setTextInvalid() { textInvalid = true; }
bool isTextInvalid() const { return textInvalid; }
void setLayoutInvalid() { layoutInvalid = true; }
Expand Down
15 changes: 14 additions & 1 deletion mscore/editstaff.cpp
Expand Up @@ -162,6 +162,9 @@ void EditStaff::updateStaffType()
showTimesig->setChecked(staffType->genTimesig());
showBarlines->setChecked(staffType->showBarlines());
staffGroupName->setText(qApp->translate("Staff type group name", staffType->groupName()));
rotationInput->setValue(orgStaff->part()->instrumentNameRotation());
spinBoxOffsetX->setValue(orgStaff->part()->instrumentNameOffset().x());
spinBoxOffsetY->setValue(orgStaff->part()->instrumentNameOffset().y());
}

//---------------------------------------------------------
Expand Down Expand Up @@ -354,6 +357,16 @@ void EditStaff::apply()

instrument.setSingleNoteDynamics(singleNoteDynamics->isChecked());

bool partChanged = false;
if(rotationInput->value() != part->instrumentNameRotation()){
part->setInstrumentNameRotation(rotationInput->value());
partChanged = true;
}
if(spinBoxOffsetX->value() != part->instrumentNameOffset().x() || spinBoxOffsetY->value() != part->instrumentNameOffset().y()) {
part->setInstrumentNameOffset(spinBoxOffsetX->value(), spinBoxOffsetY->value());
partChanged = true;
}

bool inv = invisible->isChecked();
ClefTypeList clefType = orgStaff->defaultClefType();
qreal userDist = spinExtraDistance->value();
Expand All @@ -371,7 +384,7 @@ void EditStaff::apply()
Interval v1 = instrument.transpose();
Interval v2 = part->instrument(_tickStart)->transpose();

if (instrumentFieldChanged || part->partName() != newPartName) {
if (instrumentFieldChanged || part->partName() != newPartName || partChanged) {
// instrument has changed

if (_tickStart == Fraction(-1, 1)) {
Expand Down