MuseScore/libmscore/textframe.cpp

183 lines
4.9 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 "box.h"
#include "text.h"
#include "score.h"
#include "barline.h"
#include "repeat.h"
#include "symbol.h"
#include "system.h"
#include "image.h"
#include "layoutbreak.h"
#include "fret.h"
#include "mscore.h"
#include "textframe.h"
2014-10-16 11:56:06 +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
//---------------------------------------------------------
// TBox
//---------------------------------------------------------
TBox::TBox(Score* score)
: VBox(score)
{
2014-10-16 11:56:06 +02:00
setBoxHeight(Spatium(1));
2018-08-01 11:46:07 +02:00
_text = new Text(score, Tid::FRAME);
_text->setLayoutToParentWidth(true);
2014-10-16 11:56:06 +02:00
_text->setParent(this);
}
TBox::TBox(const TBox& tbox)
: VBox(tbox)
{
_text = new Text(*(tbox._text));
}
2014-10-16 11:56:06 +02:00
TBox::~TBox()
{
delete _text;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// layout
/// The text box layout() adjusts the frame height to text
/// height.
//---------------------------------------------------------
void TBox::layout()
{
setPos(QPointF()); // !?
2017-02-23 11:41:53 +01:00
bbox().setRect(0.0, 0.0, system()->width(), 0);
2014-10-16 11:56:06 +02:00
_text->layout();
2017-02-23 11:41:53 +01:00
qreal h = _text->height();
if (_text->empty()) {
QFontMetricsF fm = QFontMetricsF(_text->font(), MScore::paintDevice());
h = fm.ascent();
}
else
h = _text->height();
2017-02-23 11:41:53 +01:00
qreal y = topMargin() * DPMM;
#if 0
if (_text->align() & Align::BOTTOM)
y += h;
else if (_text->align() & Align::VCENTER)
y += h * .5;
else
; // y = 0;
#endif
_text->setPos(leftMargin() * DPMM, y);
h += topMargin() * DPMM + bottomMargin() * DPMM;
2014-10-16 11:56:06 +02:00
bbox().setRect(0.0, 0.0, system()->width(), h);
2017-02-23 11:41:53 +01:00
2012-05-26 14:26:10 +02:00
MeasureBase::layout(); // layout LayoutBreak's
}
//---------------------------------------------------------
2014-10-16 11:56:06 +02:00
// write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void TBox::write(XmlWriter& xml) const
2014-10-16 11:56:06 +02:00
{
xml.stag(name());
Box::writeProperties(xml);
_text->write(xml);
xml.etag();
}
//---------------------------------------------------------
// read
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2014-10-16 11:56:06 +02:00
void TBox::read(XmlReader& e)
2012-05-26 14:26:10 +02:00
{
2014-10-16 11:56:06 +02:00
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "Text")
_text->read(e);
else if (Box::readProperties(e))
;
else
e.unknown();
2012-05-26 14:26:10 +02:00
}
}
//---------------------------------------------------------
2014-10-16 11:56:06 +02:00
// scanElements
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2014-10-16 11:56:06 +02:00
void TBox::scanElements(void* data, void (*func)(void*, Element*), bool all)
2012-05-26 14:26:10 +02:00
{
2014-10-16 11:56:06 +02:00
_text->scanElements(data, func, all);
Box::scanElements(data, func, all);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// drop
//---------------------------------------------------------
2017-03-31 13:03:15 +02:00
Element* TBox::drop(EditData& data)
{
Element* e = data.element;
switch (e->type()) {
2017-01-18 14:16:33 +01:00
case ElementType::TEXT:
_text->undoChangeProperty(Pid::TEXT, toText(e)->xmlText());
delete e;
return _text;
default:
return VBox::drop(data);
}
}
//---------------------------------------------------------
// add
/// Add new Element \a el to TBox
//---------------------------------------------------------
void TBox::add(Element* e)
{
2017-02-23 11:41:53 +01:00
if (e->isText()) {
// does not normally happen, since drop() handles this directly
_text->undoChangeProperty(Pid::TEXT, toText(e)->xmlText());
}
else {
VBox::add(e);
}
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void TBox::remove(Element* el)
{
if (el == _text) {
// does not normally happen, since Score::deleteItem() handles this directly
// but if it does:
// replace with new empty text element
// this keeps undo/redo happier than just clearing the text
qDebug("TBox::remove() - replacing _text");
2018-08-01 11:46:07 +02:00
_text = new Text(score(), Tid::FRAME);
_text->setLayoutToParentWidth(true);
_text->setParent(this);
}
else {
VBox::remove(el);
}
}
2013-05-13 18:49:17 +02:00
}